Added editorconfig file for VS C++ code style formatting.

Applied C++ auto formatting.
This commit is contained in:
2022-08-08 00:20:45 -07:00
parent 870a796f39
commit 82a892cede
25 changed files with 2876 additions and 2480 deletions

136
.editorconfig Normal file
View File

@@ -0,0 +1,136 @@
# Visual Studio generated .editorconfig file with C++ settings.
root = true
[*.{c++,cc,cpp,cppm,cxx,h,h++,hh,hpp,hxx,inl,ipp,ixx,tlh,tli}]
# Visual C++ Code Style settings
cpp_generate_documentation_comments = xml
# Visual C++ Formatting settings
cpp_indent_braces = false
cpp_indent_multi_line_relative_to = innermost_parenthesis
cpp_indent_within_parentheses = indent
cpp_indent_preserve_within_parentheses = true
cpp_indent_case_contents = true
cpp_indent_case_labels = false
cpp_indent_case_contents_when_block = false
cpp_indent_lambda_braces_when_parameter = true
cpp_indent_goto_labels = one_left
cpp_indent_preprocessor = leftmost_column
cpp_indent_access_specifiers = false
cpp_indent_namespace_contents = false
cpp_indent_preserve_comments = false
cpp_new_line_before_open_brace_namespace = ignore
cpp_new_line_before_open_brace_type = ignore
cpp_new_line_before_open_brace_function = ignore
cpp_new_line_before_open_brace_block = false
cpp_new_line_before_open_brace_lambda = ignore
cpp_new_line_scope_braces_on_separate_lines = false
cpp_new_line_close_brace_same_line_empty_type = false
cpp_new_line_close_brace_same_line_empty_function = false
cpp_new_line_before_catch = true
cpp_new_line_before_else = true
cpp_new_line_before_while_in_do_while = false
cpp_space_before_function_open_parenthesis = remove
cpp_space_within_parameter_list_parentheses = false
cpp_space_between_empty_parameter_list_parentheses = false
cpp_space_after_keywords_in_control_flow_statements = true
cpp_space_within_control_flow_statement_parentheses = false
cpp_space_before_lambda_open_parenthesis = false
cpp_space_within_cast_parentheses = false
cpp_space_after_cast_close_parenthesis = false
cpp_space_within_expression_parentheses = false
cpp_space_before_block_open_brace = true
cpp_space_between_empty_braces = false
cpp_space_before_initializer_list_open_brace = false
cpp_space_within_initializer_list_braces = true
cpp_space_preserve_in_initializer_list = true
cpp_space_before_open_square_bracket = false
cpp_space_within_square_brackets = false
cpp_space_before_empty_square_brackets = false
cpp_space_between_empty_square_brackets = false
cpp_space_group_square_brackets = true
cpp_space_within_lambda_brackets = false
cpp_space_between_empty_lambda_brackets = false
cpp_space_before_comma = false
cpp_space_after_comma = true
cpp_space_remove_around_member_operators = true
cpp_space_before_inheritance_colon = true
cpp_space_before_constructor_colon = true
cpp_space_remove_before_semicolon = true
cpp_space_after_semicolon = true
cpp_space_remove_around_unary_operator = true
cpp_space_around_binary_operator = insert
cpp_space_around_assignment_operator = insert
cpp_space_pointer_reference_alignment = left
cpp_space_around_ternary_operator = insert
cpp_wrap_preserve_blocks = one_liners
[*.{c++,cc,cpp,cppm,cxx,h,h++,hh,hpp,hxx,inl,ipp,ixx,tlh,tli}]
# Visual C++ Code Style settings
cpp_generate_documentation_comments = xml
# Visual C++ Formatting settings
cpp_indent_braces = false
cpp_indent_multi_line_relative_to = outermost_parenthesis
cpp_indent_within_parentheses = indent
cpp_indent_preserve_within_parentheses = true
cpp_indent_case_contents = true
cpp_indent_case_labels = false
cpp_indent_case_contents_when_block = false
cpp_indent_lambda_braces_when_parameter = true
cpp_indent_goto_labels = one_left
cpp_indent_preprocessor = leftmost_column
cpp_indent_access_specifiers = false
cpp_indent_namespace_contents = false
cpp_indent_preserve_comments = false
cpp_new_line_before_open_brace_namespace = same_line
cpp_new_line_before_open_brace_type = new_line
cpp_new_line_before_open_brace_function = new_line
cpp_new_line_before_open_brace_block = same_line
cpp_new_line_before_open_brace_lambda = same_line
cpp_new_line_scope_braces_on_separate_lines = true
cpp_new_line_close_brace_same_line_empty_type = false
cpp_new_line_close_brace_same_line_empty_function = false
cpp_new_line_before_catch = false
cpp_new_line_before_else = false
cpp_new_line_before_while_in_do_while = false
cpp_space_before_function_open_parenthesis = remove
cpp_space_within_parameter_list_parentheses = false
cpp_space_between_empty_parameter_list_parentheses = false
cpp_space_after_keywords_in_control_flow_statements = true
cpp_space_within_control_flow_statement_parentheses = false
cpp_space_before_lambda_open_parenthesis = false
cpp_space_within_cast_parentheses = false
cpp_space_after_cast_close_parenthesis = false
cpp_space_within_expression_parentheses = false
cpp_space_before_block_open_brace = true
cpp_space_between_empty_braces = false
cpp_space_before_initializer_list_open_brace = false
cpp_space_within_initializer_list_braces = true
cpp_space_preserve_in_initializer_list = true
cpp_space_before_open_square_bracket = false
cpp_space_within_square_brackets = false
cpp_space_before_empty_square_brackets = false
cpp_space_between_empty_square_brackets = false
cpp_space_group_square_brackets = true
cpp_space_within_lambda_brackets = false
cpp_space_between_empty_lambda_brackets = false
cpp_space_before_comma = false
cpp_space_after_comma = true
cpp_space_remove_around_member_operators = true
cpp_space_before_inheritance_colon = true
cpp_space_before_constructor_colon = true
cpp_space_remove_before_semicolon = true
cpp_space_after_semicolon = true
cpp_space_remove_around_unary_operator = true
cpp_space_around_binary_operator = insert
cpp_space_around_assignment_operator = insert
cpp_space_pointer_reference_alignment = left
cpp_space_around_ternary_operator = insert
cpp_wrap_preserve_blocks = never

View File

@@ -43,32 +43,33 @@ namespace kraken {
class Matrix4;
class AABB {
class AABB
{
public:
Vector3 min;
Vector3 max;
void init(const Vector3 &minPoint, const Vector3 &maxPoint);
void init(const Vector3 &corner1, const Vector3 &corner2, const Matrix4 &modelMatrix);
void init(const Vector3& minPoint, const Vector3& maxPoint);
void init(const Vector3& corner1, const Vector3& corner2, const Matrix4& modelMatrix);
void init();
static AABB Create(const Vector3 &minPoint, const Vector3 &maxPoint);
static AABB Create(const Vector3 &corner1, const Vector3 &corner2, const Matrix4 &modelMatrix);
static AABB Create(const Vector3& minPoint, const Vector3& maxPoint);
static AABB Create(const Vector3& corner1, const Vector3& corner2, const Matrix4& modelMatrix);
static AABB Create();
void scale(const Vector3 &s);
void scale(const Vector3& s);
void scale(float s);
Vector3 center() const;
Vector3 size() const;
float volume() const;
bool intersects(const AABB& b) const;
bool contains(const AABB &b) const;
bool contains(const Vector3 &v) const;
bool contains(const AABB& b) const;
bool contains(const Vector3& v) const;
bool intersectsLine(const Vector3 &v1, const Vector3 &v2) const;
bool intersectsRay(const Vector3 &v1, const Vector3 &dir) const;
bool intersectsSphere(const Vector3 &center, float radius) const;
void encapsulate(const AABB & b);
bool intersectsLine(const Vector3& v1, const Vector3& v2) const;
bool intersectsRay(const Vector3& v1, const Vector3& dir) const;
bool intersectsSphere(const Vector3& center, float radius) const;
void encapsulate(const AABB& b);
bool operator ==(const AABB& b) const;
bool operator !=(const AABB& b) const;
@@ -81,23 +82,24 @@ public:
static AABB Zero();
float longest_radius() const;
Vector3 nearestPoint(const Vector3 & v) const;
Vector3 nearestPoint(const Vector3& v) const;
};
static_assert(std::is_pod<AABB>::value, "kraken::AABB must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::AABB> {
public:
size_t operator()(const kraken::AABB &s) const
template<>
struct hash<kraken::AABB>
{
public:
size_t operator()(const kraken::AABB& s) const
{
size_t h1 = hash<kraken::Vector3>()(s.min);
size_t h2 = hash<kraken::Vector3>()(s.max);
return h1 ^ ( h2 << 1 );
return h1 ^ (h2 << 1);
}
};
};
} // namespace std

View File

@@ -38,23 +38,24 @@ class KRNode;
namespace kraken {
class HitInfo {
class HitInfo
{
public:
HitInfo();
HitInfo(const Vector3 &position, const Vector3 &normal, const float distance);
HitInfo(const Vector3 &position, const Vector3 &normal, const float distance, KRNode *node);
HitInfo(const Vector3& position, const Vector3& normal, const float distance);
HitInfo(const Vector3& position, const Vector3& normal, const float distance, KRNode* node);
~HitInfo();
Vector3 getPosition() const;
Vector3 getNormal() const;
float getDistance() const;
KRNode *getNode() const;
KRNode* getNode() const;
bool didHit() const;
HitInfo& operator =(const HitInfo& b);
private:
KRNode *m_node;
KRNode* m_node;
Vector3 m_position;
Vector3 m_normal;
float m_distance;

View File

@@ -36,11 +36,14 @@
namespace kraken {
class Matrix2 {
class Matrix2
{
public:
union {
struct {
union
{
struct
{
Vector2 axis_x, axis_y;
};
// Matrix components, in column-major order
@@ -50,40 +53,40 @@ public:
// Default initializer - Creates an identity matrix
void init();
void init(float *pMat);
void init(float* pMat);
void init(const Vector2 &new_axis_x, const Vector2 &new_axis_y);
void init(const Vector2& new_axis_x, const Vector2& new_axis_y);
void init(const Matrix2 &m);
void init(const Matrix2& m);
// Overload comparison operator
bool operator==(const Matrix2 &m) const;
bool operator==(const Matrix2& m) const;
// Overload compound multiply operator
Matrix2& operator*=(const Matrix2 &m);
Matrix2& operator*=(const Matrix2& m);
float& operator[](unsigned i);
float operator[](unsigned i) const;
// Overload multiply operator
//Matrix4& operator*(const Matrix4 &m);
Matrix2 operator*(const Matrix2 &m) const;
Matrix2 operator*(const Matrix2& m) const;
float *getPointer();
float* getPointer();
void scale(float x, float y);
void scale(const Vector2 &v);
void scale(const Vector2& v);
void scale(float s);
void rotate(float angle);
bool invert();
void transpose();
static Matrix2 Invert(const Matrix2 &m);
static Matrix2 Transpose(const Matrix2 &m);
static Vector2 Dot(const Matrix2 &m, const Vector2 &v);
static Matrix2 Invert(const Matrix2& m);
static Matrix2 Transpose(const Matrix2& m);
static Vector2 Dot(const Matrix2& m, const Vector2& v);
static Matrix2 Rotation(float);
static Matrix2 Scaling(const Vector2 &v);
static Matrix2 Scaling(const Vector2& v);
static Matrix2 Identity();
};
static_assert(std::is_pod<Matrix2>::value, "kraken::Matrix2 must be a POD type.");
@@ -91,16 +94,17 @@ static_assert(std::is_pod<Matrix2>::value, "kraken::Matrix2 must be a POD type."
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Matrix2> {
public:
size_t operator()(const kraken::Matrix2 &s) const
template<>
struct hash<kraken::Matrix2>
{
public:
size_t operator()(const kraken::Matrix2& s) const
{
size_t h1 = hash<kraken::Vector2>()(s.axis_x);
size_t h2 = hash<kraken::Vector2>()(s.axis_y);
return h1 ^ (h2 << 1);
}
};
};
} // namespace std
#endif // KRAKEN_MATRIX2_H

View File

@@ -37,11 +37,14 @@
namespace kraken {
class Matrix2x3 {
class Matrix2x3
{
public:
union {
struct {
union
{
struct
{
Vector2 axis_x, axis_y, transform;
};
// Matrix components, in column-major order
@@ -51,41 +54,41 @@ public:
// Default initializer - Creates an identity matrix
void init();
void init(float *pMat);
void init(float* pMat);
void init(const Vector2 &new_axis_x, const Vector2 &new_axis_y, const Vector2 &new_transform);
void init(const Vector2& new_axis_x, const Vector2& new_axis_y, const Vector2& new_transform);
void init(const Matrix2x3 &m);
void init(const Matrix2x3& m);
// Overload comparison operator
bool operator==(const Matrix2x3 &m) const;
bool operator==(const Matrix2x3& m) const;
// Overload compound multiply operator
Matrix2x3& operator*=(const Matrix2x3 &m);
Matrix2x3& operator*=(const Matrix2x3& m);
float& operator[](unsigned i);
float operator[](unsigned i) const;
// Overload multiply operator
Matrix2x3 operator*(const Matrix2x3 &m) const;
Matrix2x3 operator*(const Matrix2x3& m) const;
float *getPointer();
float* getPointer();
void translate(float x, float y);
void translate(const Vector2 &v);
void translate(const Vector2& v);
void scale(float x, float y);
void scale(const Vector2 &v);
void scale(const Vector2& v);
void scale(float s);
void rotate(float angle);
bool invert();
static Vector2 DotNoTranslate(const Matrix2x3 &m, const Vector2 &v); // Dot product without including translation; useful for transforming normals and tangents
static Matrix2x3 Invert(const Matrix2x3 &m);
static Vector2 Dot(const Matrix2x3 &m, const Vector2 &v);
static Vector2 DotNoTranslate(const Matrix2x3& m, const Vector2& v); // Dot product without including translation; useful for transforming normals and tangents
static Matrix2x3 Invert(const Matrix2x3& m);
static Vector2 Dot(const Matrix2x3& m, const Vector2& v);
static Matrix2x3 Translation(const Vector2 &v);
static Matrix2x3 Translation(const Vector2& v);
static Matrix2x3 Rotation(float angle);
static Matrix2x3 Scaling(const Vector2 &v);
static Matrix2x3 Scaling(const Vector2& v);
static Matrix2x3 Identity();
};
static_assert(std::is_pod<Matrix2x3>::value, "kraken::Matrix2x3 must be a POD type.");
@@ -93,17 +96,18 @@ static_assert(std::is_pod<Matrix2x3>::value, "kraken::Matrix2x3 must be a POD ty
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Matrix2x3> {
public:
size_t operator()(const kraken::Matrix2x3 &s) const
template<>
struct hash<kraken::Matrix2x3>
{
public:
size_t operator()(const kraken::Matrix2x3& s) const
{
size_t h1 = hash<kraken::Vector2>()(s.axis_x);
size_t h2 = hash<kraken::Vector2>()(s.axis_y);
size_t h3 = hash<kraken::Vector2>()(s.transform);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
};
} // namespace std
#endif // KRAKEN_MATRIX2X3_H

View File

@@ -38,7 +38,8 @@
namespace kraken {
enum class AXIS {
enum class AXIS
{
X_AXIS,
Y_AXIS,
Z_AXIS
@@ -46,11 +47,14 @@ enum class AXIS {
class Quaternion;
class Matrix4 {
class Matrix4
{
public:
union {
struct {
union
{
struct
{
Vector4 axis_x, axis_y, axis_z, transform;
};
// Matrix components, in column-major order
@@ -60,54 +64,54 @@ public:
// Default initializer - Creates an identity matrix
void init();
void init(float *pMat);
void init(const Vector3 &new_axis_x, const Vector3 &new_axis_y, const Vector3 &new_axis_z, const Vector3 &new_transform);
void init(const Matrix4 &m);
void init(float* pMat);
void init(const Vector3& new_axis_x, const Vector3& new_axis_y, const Vector3& new_axis_z, const Vector3& new_transform);
void init(const Matrix4& m);
static Matrix4 Create(float *pMat);
static Matrix4 Create(const Vector3 &new_axis_x, const Vector3 &new_axis_y, const Vector3 &new_axis_z, const Vector3 &new_transform);
static Matrix4 Create(float* pMat);
static Matrix4 Create(const Vector3& new_axis_x, const Vector3& new_axis_y, const Vector3& new_axis_z, const Vector3& new_transform);
// Overload comparison operator
bool operator==(const Matrix4 &m) const;
bool operator==(const Matrix4& m) const;
// Overload compound multiply operator
Matrix4& operator*=(const Matrix4 &m);
Matrix4& operator*=(const Matrix4& m);
float& operator[](unsigned i);
float operator[](unsigned i) const;
// Overload multiply operator
//Matrix4& operator*(const Matrix4 &m);
Matrix4 operator*(const Matrix4 &m) const;
Matrix4 operator*(const Matrix4& m) const;
float *getPointer();
float* getPointer();
void perspective(float fov, float aspect, float nearz, float farz);
void ortho(float left, float right, float top, float bottom, float nearz, float farz);
void translate(float x, float y, float z);
void translate(const Vector3 &v);
void translate(const Vector3& v);
void scale(float x, float y, float z);
void scale(const Vector3 &v);
void scale(const Vector3& v);
void scale(float s);
void rotate(float angle, AXIS axis);
void rotate(const Quaternion &q);
void rotate(const Quaternion& q);
void bias();
bool invert();
void transpose();
static Vector3 DotNoTranslate(const Matrix4 &m, const Vector3 &v); // Dot product without including translation; useful for transforming normals and tangents
static Matrix4 Invert(const Matrix4 &m);
static Matrix4 Transpose(const Matrix4 &m);
static Vector3 Dot(const Matrix4 &m, const Vector3 &v);
static Vector4 Dot4(const Matrix4 &m, const Vector4 &v);
static float DotW(const Matrix4 &m, const Vector3 &v);
static Vector3 DotWDiv(const Matrix4 &m, const Vector3 &v);
static Vector3 DotNoTranslate(const Matrix4& m, const Vector3& v); // Dot product without including translation; useful for transforming normals and tangents
static Matrix4 Invert(const Matrix4& m);
static Matrix4 Transpose(const Matrix4& m);
static Vector3 Dot(const Matrix4& m, const Vector3& v);
static Vector4 Dot4(const Matrix4& m, const Vector4& v);
static float DotW(const Matrix4& m, const Vector3& v);
static Vector3 DotWDiv(const Matrix4& m, const Vector3& v);
static Matrix4 LookAt(const Vector3 &cameraPos, const Vector3 &lookAtPos, const Vector3 &upDirection);
static Matrix4 LookAt(const Vector3& cameraPos, const Vector3& lookAtPos, const Vector3& upDirection);
static Matrix4 Translation(const Vector3 &v);
static Matrix4 Rotation(const Vector3 &v);
static Matrix4 Scaling(const Vector3 &v);
static Matrix4 Translation(const Vector3& v);
static Matrix4 Rotation(const Vector3& v);
static Matrix4 Scaling(const Vector3& v);
static Matrix4 Identity();
};
static_assert(std::is_pod<Matrix4>::value, "kraken::Matrix4 must be a POD type.");
@@ -115,10 +119,11 @@ static_assert(std::is_pod<Matrix4>::value, "kraken::Matrix4 must be a POD type."
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Matrix4> {
public:
size_t operator()(const kraken::Matrix4 &s) const
template<>
struct hash<kraken::Matrix4>
{
public:
size_t operator()(const kraken::Matrix4& s) const
{
size_t h1 = hash<kraken::Vector4>()(s.axis_x);
size_t h2 = hash<kraken::Vector4>()(s.axis_y);
@@ -126,7 +131,7 @@ namespace std {
size_t h4 = hash<kraken::Vector4>()(s.transform);
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
}
};
};
} // namespace std
#endif // KRAKEN_MATRIX4_H

View File

@@ -36,10 +36,13 @@
namespace kraken {
class Quaternion {
class Quaternion
{
public:
union {
struct {
union
{
struct
{
float w, x, y, z;
};
float c[4];
@@ -48,20 +51,20 @@ public:
void init();
void init(float w, float x, float y, float z);
void init(const Quaternion& p);
void init(const Vector3 &euler);
void init(const Vector3 &from_vector, const Vector3 &to_vector);
void init(const Vector3& euler);
void init(const Vector3& from_vector, const Vector3& to_vector);
static Quaternion Create();
static Quaternion Create(float w, float x, float y, float z);
static Quaternion Create(const Quaternion& p);
static Quaternion Create(const Vector3 &euler);
static Quaternion Create(const Vector3 &from_vector, const Vector3 &to_vector);
static Quaternion Create(const Vector3& euler);
static Quaternion Create(const Vector3& from_vector, const Vector3& to_vector);
Quaternion operator +(const Quaternion &v) const;
Quaternion operator -(const Quaternion &v) const;
Quaternion operator +(const Quaternion& v) const;
Quaternion operator -(const Quaternion& v) const;
Quaternion operator +() const;
Quaternion operator -() const;
Quaternion operator *(const Quaternion &v);
Quaternion operator *(const Quaternion& v);
Quaternion operator *(float num) const;
Quaternion operator /(float num) const;
@@ -71,40 +74,41 @@ public:
Quaternion& operator *=(const float& v);
Quaternion& operator /=(const float& v);
friend bool operator ==(Quaternion &v1, Quaternion &v2);
friend bool operator !=(Quaternion &v1, Quaternion &v2);
friend bool operator ==(Quaternion& v1, Quaternion& v2);
friend bool operator !=(Quaternion& v1, Quaternion& v2);
float& operator [](unsigned i);
float operator [](unsigned i) const;
void setEulerXYZ(const Vector3 &euler);
void setEulerZYX(const Vector3 &euler);
void setEulerXYZ(const Vector3& euler);
void setEulerZYX(const Vector3& euler);
Vector3 eulerXYZ() const;
Matrix4 rotationMatrix() const;
void normalize();
static Quaternion Normalize(const Quaternion &v1);
static Quaternion Normalize(const Quaternion& v1);
void conjugate();
static Quaternion Conjugate(const Quaternion &v1);
static Quaternion Conjugate(const Quaternion& v1);
void invert();
static Quaternion Invert(const Quaternion &v1);
static Quaternion Invert(const Quaternion& v1);
static Quaternion FromAngleAxis(const Vector3 &axis, float angle);
static Quaternion FromRotationMatrix(const Matrix4 &m);
static Quaternion Lerp(const Quaternion &a, const Quaternion &b, float t);
static Quaternion Slerp(const Quaternion &a, const Quaternion &b, float t);
static float Dot(const Quaternion &v1, const Quaternion &v2);
static Quaternion FromAngleAxis(const Vector3& axis, float angle);
static Quaternion FromRotationMatrix(const Matrix4& m);
static Quaternion Lerp(const Quaternion& a, const Quaternion& b, float t);
static Quaternion Slerp(const Quaternion& a, const Quaternion& b, float t);
static float Dot(const Quaternion& v1, const Quaternion& v2);
};
static_assert(std::is_pod<Quaternion>::value, "kraken::Quaternion must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Quaternion> {
public:
size_t operator()(const kraken::Quaternion &s) const
template<>
struct hash<kraken::Quaternion>
{
public:
size_t operator()(const kraken::Quaternion& s) const
{
size_t h1 = hash<float>()(s.c[0]);
size_t h2 = hash<float>()(s.c[1]);
@@ -112,7 +116,7 @@ namespace std {
size_t h4 = hash<float>()(s.c[3]);
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
}
};
};
} // namespace std
#endif // KRAKEN_QUATERNION_H

View File

@@ -34,8 +34,8 @@
namespace kraken {
float SmoothStep(float a, float b, float t);
float Lerp(float a, float b, float t);
float SmoothStep(float a, float b, float t);
float Lerp(float a, float b, float t);
}; // namespace kraken

View File

@@ -41,10 +41,10 @@ class Triangle3
public:
Vector3 vert[3];
void init(const Triangle3 &tri);
void init(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3);
static Triangle3 Create(const Triangle3 &tri);
static Triangle3 Create(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3);
void init(const Triangle3& tri);
void init(const Vector3& v1, const Vector3& v2, const Vector3& v3);
static Triangle3 Create(const Triangle3& tri);
static Triangle3 Create(const Vector3& v1, const Vector3& v2, const Vector3& v3);
Vector3 calculateNormal() const;
@@ -53,28 +53,29 @@ public:
Vector3& operator[](unsigned int i);
Vector3 operator[](unsigned int i) const;
bool rayCast(const Vector3 &start, const Vector3 &dir, Vector3 &hit_point) const;
bool sphereCast(const Vector3 &start, const Vector3 &dir, float radius, Vector3 &hit_point, float &hit_distance) const;
bool rayCast(const Vector3& start, const Vector3& dir, Vector3& hit_point) const;
bool sphereCast(const Vector3& start, const Vector3& dir, float radius, Vector3& hit_point, float& hit_distance) const;
bool containsPoint(const Vector3 &p) const;
Vector3 closestPointOnTriangle(const Vector3 &p) const;
bool containsPoint(const Vector3& p) const;
Vector3 closestPointOnTriangle(const Vector3& p) const;
};
static_assert(std::is_pod<Triangle3>::value, "kraken::Triangle3 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Triangle3> {
public:
size_t operator()(const kraken::Triangle3 &s) const
template<>
struct hash<kraken::Triangle3>
{
public:
size_t operator()(const kraken::Triangle3& s) const
{
size_t h1 = hash<kraken::Vector3>()(s.vert[0]);
size_t h2 = hash<kraken::Vector3>()(s.vert[1]);
size_t h3 = hash<kraken::Vector3>()(s.vert[2]);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
};
} // namespace std
#endif // KRAKEN_TRIANGLE3_H

View File

@@ -38,11 +38,14 @@
namespace kraken {
class Vector2 {
class Vector2
{
public:
union {
struct {
union
{
struct
{
float x, y;
};
float c[2];
@@ -51,19 +54,19 @@ public:
void init();
void init(float X, float Y);
void init(float v);
void init(float *v);
void init(const Vector2 &v);
void init(float* v);
void init(const Vector2& v);
static Vector2 Create();
static Vector2 Create(float X, float Y);
static Vector2 Create(float v);
static Vector2 Create(float *v);
static Vector2 Create(const Vector2 &v);
static Vector2 Create(float* v);
static Vector2 Create(const Vector2& v);
// Vector2 swizzle getters
Vector2 yx() const;
// Vector2 swizzle setters
void yx(const Vector2 &v);
void yx(const Vector2& v);
Vector2 operator +(const Vector2& b) const;
Vector2 operator -(const Vector2& b) const;
@@ -91,12 +94,12 @@ public:
float magnitude() const;
void normalize();
static Vector2 Normalize(const Vector2 &v);
static Vector2 Normalize(const Vector2& v);
static float Cross(const Vector2 &v1, const Vector2 &v2);
static float Dot(const Vector2 &v1, const Vector2 &v2);
static Vector2 Min(const Vector2 &v1, const Vector2 &v2);
static Vector2 Max(const Vector2 &v1, const Vector2 &v2);
static float Cross(const Vector2& v1, const Vector2& v2);
static float Dot(const Vector2& v1, const Vector2& v2);
static Vector2 Min(const Vector2& v1, const Vector2& v2);
static Vector2 Max(const Vector2& v1, const Vector2& v2);
static Vector2 Min();
static Vector2 Max();
static Vector2 Zero();
@@ -107,16 +110,17 @@ static_assert(std::is_pod<Vector2>::value, "kraken::Vector2 must be a POD type."
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Vector2> {
public:
size_t operator()(const kraken::Vector2 &s) const
template<>
struct hash<kraken::Vector2>
{
public:
size_t operator()(const kraken::Vector2& s) const
{
size_t h1 = hash<float>()(s.x);
size_t h2 = hash<float>()(s.y);
return h1 ^ (h2 << 1);
}
};
};
} // namespace std
#endif // KRAKEN_VECTOR2_H

View File

@@ -37,11 +37,14 @@
namespace kraken {
class Vector2i {
class Vector2i
{
public:
union {
struct {
union
{
struct
{
int x, y;
};
int c[2];
@@ -50,19 +53,19 @@ public:
void init();
void init(int X, int Y);
void init(int v);
void init(int *v);
void init(const Vector2i &v);
void init(int* v);
void init(const Vector2i& v);
static Vector2i Create();
static Vector2i Create(int X, int Y);
static Vector2i Create(int v);
static Vector2i Create(int *v);
static Vector2i Create(const Vector2i &v);
static Vector2i Create(int* v);
static Vector2i Create(const Vector2i& v);
// Vector2 swizzle getters
Vector2i yx() const;
// Vector2 swizzle setters
void yx(const Vector2i &v);
void yx(const Vector2i& v);
Vector2i operator +(const Vector2i& b) const;
Vector2i operator -(const Vector2i& b) const;
@@ -90,12 +93,12 @@ public:
int magnitude() const;
void normalize();
static Vector2i Normalize(const Vector2i &v);
static Vector2i Normalize(const Vector2i& v);
static int Cross(const Vector2i &v1, const Vector2i &v2);
static int Dot(const Vector2i &v1, const Vector2i &v2);
static Vector2i Min(const Vector2i &v1, const Vector2i &v2);
static Vector2i Max(const Vector2i &v1, const Vector2i &v2);
static int Cross(const Vector2i& v1, const Vector2i& v2);
static int Dot(const Vector2i& v1, const Vector2i& v2);
static Vector2i Min(const Vector2i& v1, const Vector2i& v2);
static Vector2i Max(const Vector2i& v1, const Vector2i& v2);
static Vector2i Min();
static Vector2i Max();
@@ -107,14 +110,15 @@ static_assert(std::is_pod<Vector2i>::value, "kraken::Vector2i must be a POD type
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Vector2i> {
public:
size_t operator()(const kraken::Vector2i &s) const
template<>
struct hash<kraken::Vector2i>
{
public:
size_t operator()(const kraken::Vector2i& s) const
{
size_t h1 = hash<int>()(s.x);
size_t h2 = hash<int>()(s.y);
return h1 ^ (h2 << 1);
}
};
};
} // namespace std

View File

@@ -38,11 +38,14 @@
namespace kraken {
class Vector3 {
class Vector3
{
public:
union {
struct {
union
{
struct
{
float x, y, z;
};
float c[3];
@@ -51,17 +54,17 @@ public:
void init();
void init(float X, float Y, float Z);
void init(float v);
void init(float *v);
void init(double *v);
void init(const Vector3 &v);
void init(const Vector4 &v);
void init(float* v);
void init(double* v);
void init(const Vector3& v);
void init(const Vector4& v);
static Vector3 Create();
static Vector3 Create(float X, float Y, float Z);
static Vector3 Create(float v);
static Vector3 Create(float *v);
static Vector3 Create(double *v);
static Vector3 Create(const Vector3 &v);
static Vector3 Create(const Vector4 &v);
static Vector3 Create(float* v);
static Vector3 Create(double* v);
static Vector3 Create(const Vector3& v);
static Vector3 Create(const Vector4& v);
// Vector2 swizzle getters
@@ -76,12 +79,12 @@ public:
Vector2 zz() const;
// Vector2 swizzle setters
void xy(const Vector2 &v);
void xz(const Vector2 &v);
void yx(const Vector2 &v);
void yz(const Vector2 &v);
void zx(const Vector2 &v);
void zy(const Vector2 &v);
void xy(const Vector2& v);
void xz(const Vector2& v);
void yx(const Vector2& v);
void yz(const Vector2& v);
void zx(const Vector2& v);
void zy(const Vector2& v);
Vector3& operator =(const Vector4& b);
Vector3 operator +(const Vector3& b) const;
@@ -109,14 +112,14 @@ public:
float sqrMagnitude() const; // calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
float magnitude() const;
void scale(const Vector3 &v);
void scale(const Vector3& v);
void normalize();
static Vector3 Normalize(const Vector3 &v);
static Vector3 Normalize(const Vector3& v);
static Vector3 Cross(const Vector3 &v1, const Vector3 &v2);
static float Dot(const Vector3 &v1, const Vector3 &v2);
static Vector3 Min(const Vector3 &v1, const Vector3 &v2);
static Vector3 Max(const Vector3 &v1, const Vector3 &v2);
static Vector3 Cross(const Vector3& v1, const Vector3& v2);
static float Dot(const Vector3& v1, const Vector3& v2);
static Vector3 Min(const Vector3& v1, const Vector3& v2);
static Vector3 Max(const Vector3& v1, const Vector3& v2);
static Vector3 Min();
static Vector3 Max();
@@ -128,25 +131,26 @@ public:
static Vector3 Down();
static Vector3 Left();
static Vector3 Right();
static Vector3 Scale(const Vector3 &v1, const Vector3 &v2);
static Vector3 Lerp(const Vector3 &v1, const Vector3 &v2, float d);
static Vector3 Slerp(const Vector3 &v1, const Vector3 &v2, float d);
static void OrthoNormalize(Vector3 &normal, Vector3 &tangent); // Gram-Schmidt Orthonormalization
static Vector3 Scale(const Vector3& v1, const Vector3& v2);
static Vector3 Lerp(const Vector3& v1, const Vector3& v2, float d);
static Vector3 Slerp(const Vector3& v1, const Vector3& v2, float d);
static void OrthoNormalize(Vector3& normal, Vector3& tangent); // Gram-Schmidt Orthonormalization
};
static_assert(std::is_pod<Vector3>::value, "kraken::Vector3 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Vector3> {
public:
size_t operator()(const kraken::Vector3 &s) const
template<>
struct hash<kraken::Vector3>
{
public:
size_t operator()(const kraken::Vector3& s) const
{
size_t h1 = hash<float>()(s.x);
size_t h2 = hash<float>()(s.y);
size_t h3 = hash<float>()(s.z);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
};
} // namespace std

View File

@@ -37,11 +37,14 @@ namespace kraken {
class Vector3;
class Vector4 {
class Vector4
{
public:
union {
struct {
union
{
struct
{
float x, y, z, w;
};
float c[4];
@@ -50,15 +53,15 @@ public:
void init();
void init(float X, float Y, float Z, float W);
void init(float v);
void init(float *v);
void init(const Vector4 &v);
void init(const Vector3 &v, float W);
void init(float* v);
void init(const Vector4& v);
void init(const Vector3& v, float W);
static Vector4 Create();
static Vector4 Create(float X, float Y, float Z, float W);
static Vector4 Create(float v);
static Vector4 Create(float *v);
static Vector4 Create(const Vector4 &v);
static Vector4 Create(const Vector3 &v, float W);
static Vector4 Create(float* v);
static Vector4 Create(const Vector4& v);
static Vector4 Create(const Vector3& v, float W);
Vector4 operator +(const Vector4& b) const;
Vector4 operator -(const Vector4& b) const;
@@ -86,11 +89,11 @@ public:
float magnitude() const;
void normalize();
static Vector4 Normalize(const Vector4 &v);
static Vector4 Normalize(const Vector4& v);
static float Dot(const Vector4 &v1, const Vector4 &v2);
static Vector4 Min(const Vector4 &v1, const Vector4 &v2);
static Vector4 Max(const Vector4 &v1, const Vector4 &v2);
static float Dot(const Vector4& v1, const Vector4& v2);
static Vector4 Min(const Vector4& v1, const Vector4& v2);
static Vector4 Max(const Vector4& v1, const Vector4& v2);
static Vector4 Min();
static Vector4 Max();
@@ -102,19 +105,20 @@ public:
static Vector4 Down();
static Vector4 Left();
static Vector4 Right();
static Vector4 Lerp(const Vector4 &v1, const Vector4 &v2, float d);
static Vector4 Slerp(const Vector4 &v1, const Vector4 &v2, float d);
static void OrthoNormalize(Vector4 &normal, Vector4 &tangent); // Gram-Schmidt Orthonormalization
static Vector4 Lerp(const Vector4& v1, const Vector4& v2, float d);
static Vector4 Slerp(const Vector4& v1, const Vector4& v2, float d);
static void OrthoNormalize(Vector4& normal, Vector4& tangent); // Gram-Schmidt Orthonormalization
};
static_assert(std::is_pod<Vector4>::value, "kraken::Vector4 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Vector4> {
public:
size_t operator()(const kraken::Vector4 &s) const
template<>
struct hash<kraken::Vector4>
{
public:
size_t operator()(const kraken::Vector4& s) const
{
size_t h1 = hash<float>()(s.x);
size_t h2 = hash<float>()(s.y);
@@ -122,5 +126,5 @@ namespace std {
size_t h4 = hash<float>()(s.w);
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
}
};
};
} // namespace std

View File

@@ -48,43 +48,43 @@ AABB AABB::Create()
return r;
}
void AABB::init(const Vector3 &minPoint, const Vector3 &maxPoint)
void AABB::init(const Vector3& minPoint, const Vector3& maxPoint)
{
min = minPoint;
max = maxPoint;
}
AABB AABB::Create(const Vector3 &minPoint, const Vector3 &maxPoint)
AABB AABB::Create(const Vector3& minPoint, const Vector3& maxPoint)
{
AABB r;
r.init(minPoint, maxPoint);
return r;
}
void AABB::init(const Vector3 &corner1, const Vector3 &corner2, const Matrix4 &modelMatrix)
void AABB::init(const Vector3& corner1, const Vector3& corner2, const Matrix4& modelMatrix)
{
for(int iCorner=0; iCorner<8; iCorner++) {
for (int iCorner = 0; iCorner < 8; iCorner++) {
Vector3 sourceCornerVertex = Matrix4::DotWDiv(modelMatrix, Vector3::Create(
(iCorner & 1) == 0 ? corner1.x : corner2.x,
(iCorner & 2) == 0 ? corner1.y : corner2.y,
(iCorner & 4) == 0 ? corner1.z : corner2.z));
if(iCorner == 0) {
if (iCorner == 0) {
min = sourceCornerVertex;
max = sourceCornerVertex;
} else {
if(sourceCornerVertex.x < min.x) min.x = sourceCornerVertex.x;
if(sourceCornerVertex.y < min.y) min.y = sourceCornerVertex.y;
if(sourceCornerVertex.z < min.z) min.z = sourceCornerVertex.z;
if(sourceCornerVertex.x > max.x) max.x = sourceCornerVertex.x;
if(sourceCornerVertex.y > max.y) max.y = sourceCornerVertex.y;
if(sourceCornerVertex.z > max.z) max.z = sourceCornerVertex.z;
if (sourceCornerVertex.x < min.x) min.x = sourceCornerVertex.x;
if (sourceCornerVertex.y < min.y) min.y = sourceCornerVertex.y;
if (sourceCornerVertex.z < min.z) min.z = sourceCornerVertex.z;
if (sourceCornerVertex.x > max.x) max.x = sourceCornerVertex.x;
if (sourceCornerVertex.y > max.y) max.y = sourceCornerVertex.y;
if (sourceCornerVertex.z > max.z) max.z = sourceCornerVertex.z;
}
}
}
AABB AABB::Create(const Vector3 &corner1, const Vector3 &corner2, const Matrix4 &modelMatrix)
AABB AABB::Create(const Vector3& corner1, const Vector3& corner2, const Matrix4& modelMatrix)
{
AABB r;
r.init(corner1, corner2, modelMatrix);
@@ -117,7 +117,7 @@ float AABB::volume() const
return s.x * s.y * s.z;
}
void AABB::scale(const Vector3 &s)
void AABB::scale(const Vector3& s)
{
Vector3 prev_center = center();
Vector3 prev_size = size();
@@ -136,11 +136,11 @@ void AABB::scale(float s)
bool AABB::operator >(const AABB& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(min > b.min) {
if (min > b.min) {
return true;
} else if(min < b.min) {
} else if (min < b.min) {
return false;
} else if(max > b.max) {
} else if (max > b.max) {
return true;
} else {
return false;
@@ -151,11 +151,11 @@ bool AABB::operator <(const AABB& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(min < b.min) {
if (min < b.min) {
return true;
} else if(min > b.min) {
} else if (min > b.min) {
return false;
} else if(max < b.max) {
} else if (max < b.max) {
return true;
} else {
return false;
@@ -168,13 +168,13 @@ bool AABB::intersects(const AABB& b) const
return min.x <= b.max.x && min.y <= b.max.y && min.z <= b.max.z && max.x >= b.min.x && max.y >= b.min.y && max.z >= b.min.z;
}
bool AABB::contains(const AABB &b) const
bool AABB::contains(const AABB& b) const
{
// Return true if the passed KRAABB is entirely contained within this KRAABB
return b.min.x >= min.x && b.min.y >= min.y && b.min.z >= min.z && b.max.x <= max.x && b.max.y <= max.y && b.max.z <= max.z;
}
bool AABB::contains(const Vector3 &v) const
bool AABB::contains(const Vector3& v) const
{
return v.x >= min.x && v.x <= max.x && v.y >= min.y && v.y <= max.y && v.z >= min.z && v.z <= max.z;
}
@@ -197,7 +197,7 @@ float AABB::longest_radius() const
}
bool AABB::intersectsLine(const Vector3 &v1, const Vector3 &v2) const
bool AABB::intersectsLine(const Vector3& v1, const Vector3& v2) const
{
Vector3 dir = Vector3::Normalize(v2 - v1);
float length = (v2 - v1).magnitude();
@@ -209,38 +209,38 @@ bool AABB::intersectsLine(const Vector3 &v1, const Vector3 &v2) const
// octree node to COUNT as hits.
// You could modify this test to (ray starts inside and ends outside)
// to qualify as a hit if you wanted to NOT count totally internal rays
if( contains( v1 ) || contains( v2 ) )
return true ;
if (contains(v1) || contains(v2))
return true;
// the algorithm says, find 3 t's,
Vector3 t ;
Vector3 t;
// LARGEST t is the only one we need to test if it's on the face.
for(int i = 0 ; i < 3 ; i++) {
if( dir[i] > 0 ) { // CULL BACK FACE
t[i] = ( min[i] - v1[i] ) / dir[i];
for (int i = 0; i < 3; i++) {
if (dir[i] > 0) { // CULL BACK FACE
t[i] = (min[i] - v1[i]) / dir[i];
} else {
t[i] = ( max[i] - v1[i] ) / dir[i];
t[i] = (max[i] - v1[i]) / dir[i];
}
}
int mi = 0;
if(t[1] > t[mi]) mi = 1;
if(t[2] > t[mi]) mi = 2;
if(t[mi] >= 0 && t[mi] <= length) {
if (t[1] > t[mi]) mi = 1;
if (t[2] > t[mi]) mi = 2;
if (t[mi] >= 0 && t[mi] <= length) {
Vector3 pt = v1 + dir * t[mi];
// check it's in the box in other 2 dimensions
int o1 = ( mi + 1 ) % 3 ; // i=0: o1=1, o2=2, i=1: o1=2,o2=0 etc.
int o2 = ( mi + 2 ) % 3 ;
int o1 = (mi + 1) % 3; // i=0: o1=1, o2=2, i=1: o1=2,o2=0 etc.
int o2 = (mi + 2) % 3;
return pt[o1] >= min[o1] && pt[o1] <= max[o1] && pt[o2] >= min[o2] && pt[o2] <= max[o2];
}
return false ; // the ray did not hit the box.
return false; // the ray did not hit the box.
}
bool AABB::intersectsRay(const Vector3 &v1, const Vector3 &dir) const
bool AABB::intersectsRay(const Vector3& v1, const Vector3& dir) const
{
/*
Fast Ray-Box Intersection
@@ -250,7 +250,8 @@ bool AABB::intersectsRay(const Vector3 &v1, const Vector3 &dir) const
// FINDME, TODO - Perhaps there is a more efficient algorithm, as we don't actually need the exact coordinate of the intersection
enum {
enum
{
RIGHT = 0,
LEFT = 1,
MIDDLE = 2
@@ -264,12 +265,12 @@ bool AABB::intersectsRay(const Vector3 &v1, const Vector3 &dir) const
float candidatePlane[3];
// Find candidate planes; this loop can be avoided if rays cast all from the eye(assume perpsective view)
for (int i=0; i<3; i++)
if(v1.c[i] < min.c[i]) {
for (int i = 0; i < 3; i++)
if (v1.c[i] < min.c[i]) {
quadrant[i] = LEFT;
candidatePlane[i] = min.c[i];
inside = false;
} else if(v1.c[i] > max.c[i]) {
} else if (v1.c[i] > max.c[i]) {
quadrant[i] = RIGHT;
candidatePlane[i] = max.c[i];
inside = false;
@@ -286,7 +287,7 @@ bool AABB::intersectsRay(const Vector3 &v1, const Vector3 &dir) const
/* Calculate T distances to candidate planes */
for (int i = 0; i < 3; i++) {
if (quadrant[i] != MIDDLE && dir[i] != 0.0f) {
maxT.c[i] = (candidatePlane[i]-v1.c[i]) / dir[i];
maxT.c[i] = (candidatePlane[i] - v1.c[i]) / dir[i];
} else {
maxT.c[i] = -1.0f;
}
@@ -306,7 +307,7 @@ bool AABB::intersectsRay(const Vector3 &v1, const Vector3 &dir) const
}
for (int i = 0; i < 3; i++) {
if (whichPlane != i) {
coord[i] = v1.c[i] + maxT.c[whichPlane] *dir[i];
coord[i] = v1.c[i] + maxT.c[whichPlane] * dir[i];
if (coord[i] < min.c[i] || coord[i] > max.c[i]) {
return false;
}
@@ -318,7 +319,7 @@ bool AABB::intersectsRay(const Vector3 &v1, const Vector3 &dir) const
return true; /* ray hits box */
}
bool AABB::intersectsSphere(const Vector3 &center, float radius) const
bool AABB::intersectsSphere(const Vector3& center, float radius) const
{
// Arvo's Algorithm
@@ -354,18 +355,18 @@ bool AABB::intersectsSphere(const Vector3 &center, float radius) const
return squaredDistance <= radius;
}
void AABB::encapsulate(const AABB & b)
void AABB::encapsulate(const AABB& b)
{
if(b.min.x < min.x) min.x = b.min.x;
if(b.min.y < min.y) min.y = b.min.y;
if(b.min.z < min.z) min.z = b.min.z;
if (b.min.x < min.x) min.x = b.min.x;
if (b.min.y < min.y) min.y = b.min.y;
if (b.min.z < min.z) min.z = b.min.z;
if(b.max.x > max.x) max.x = b.max.x;
if(b.max.y > max.y) max.y = b.max.y;
if(b.max.z > max.z) max.z = b.max.z;
if (b.max.x > max.x) max.x = b.max.x;
if (b.max.y > max.y) max.y = b.max.y;
if (b.max.z > max.z) max.z = b.max.z;
}
Vector3 AABB::nearestPoint(const Vector3 & v) const
Vector3 AABB::nearestPoint(const Vector3& v) const
{
return Vector3::Create(KRCLAMP(v.x, min.x, max.x), KRCLAMP(v.y, min.y, max.y), KRCLAMP(v.z, min.z, max.z));
}

View File

@@ -41,7 +41,7 @@ HitInfo::HitInfo()
m_node = NULL;
}
HitInfo::HitInfo(const Vector3 &position, const Vector3 &normal, const float distance, KRNode *node)
HitInfo::HitInfo(const Vector3& position, const Vector3& normal, const float distance, KRNode* node)
{
m_position = position;
m_normal = normal;
@@ -49,7 +49,7 @@ HitInfo::HitInfo(const Vector3 &position, const Vector3 &normal, const float dis
m_node = node;
}
HitInfo::HitInfo(const Vector3 &position, const Vector3 &normal, const float distance)
HitInfo::HitInfo(const Vector3& position, const Vector3& normal, const float distance)
{
m_position = position;
m_normal = normal;
@@ -82,7 +82,7 @@ float HitInfo::getDistance() const
return m_distance;
}
KRNode *HitInfo::getNode() const
KRNode* HitInfo::getNode() const
{
return m_node;
}

View File

@@ -35,7 +35,8 @@
namespace kraken {
void Matrix2::init() {
void Matrix2::init()
{
// Default constructor - Initialize with an identity matrix
static const float IDENTITY_MATRIX[] = {
1.0, 0.0,
@@ -45,49 +46,54 @@ void Matrix2::init() {
}
void Matrix2::init(float *pMat) {
void Matrix2::init(float* pMat)
{
memcpy(c, pMat, sizeof(float) * 4);
}
void Matrix2::init(const Vector2 &new_axis_x, const Vector2 &new_axis_y)
void Matrix2::init(const Vector2& new_axis_x, const Vector2& new_axis_y)
{
c[0] = new_axis_x.x; c[1] = new_axis_x.y;
c[2] = new_axis_y.x; c[3] = new_axis_y.y;
}
void Matrix2::init(const Matrix2 &m) {
void Matrix2::init(const Matrix2& m)
{
memcpy(c, m.c, sizeof(float) * 4);
}
float *Matrix2::getPointer() {
float* Matrix2::getPointer()
{
return c;
}
float& Matrix2::operator[](unsigned i) {
float& Matrix2::operator[](unsigned i)
{
return c[i];
}
float Matrix2::operator[](unsigned i) const {
float Matrix2::operator[](unsigned i) const
{
return c[i];
}
// Overload comparison operator
bool Matrix2::operator==(const Matrix2 &m) const {
bool Matrix2::operator==(const Matrix2& m) const
{
return memcmp(c, m.c, sizeof(float) * 4) == 0;
}
// Overload compound multiply operator
Matrix2& Matrix2::operator*=(const Matrix2 &m) {
Matrix2& Matrix2::operator*=(const Matrix2& m)
{
float temp[4];
int x,y;
int x, y;
for (x=0; x < 2; x++)
{
for(y=0; y < 2; y++)
{
temp[y + (x*2)] = (c[x*2] * m.c[y]) +
(c[x*2+1] * m.c[y+2]);
for (x = 0; x < 2; x++) {
for (y = 0; y < 2; y++) {
temp[y + (x * 2)] = (c[x * 2] * m.c[y]) +
(c[x * 2 + 1] * m.c[y + 2]);
}
}
@@ -96,14 +102,16 @@ Matrix2& Matrix2::operator*=(const Matrix2 &m) {
}
// Overload multiply operator
Matrix2 Matrix2::operator*(const Matrix2 &m) const {
Matrix2 Matrix2::operator*(const Matrix2& m) const
{
Matrix2 ret = *this;
ret *= m;
return ret;
}
/* Rotate a matrix by an angle on a X, Y, or Z axis */
void Matrix2::rotate(float angle) {
void Matrix2::rotate(float angle)
{
Matrix2 newMatrix; // Create new identity matrix
newMatrix.init();
@@ -116,7 +124,8 @@ void Matrix2::rotate(float angle) {
}
/* Scale matrix by separate x, y, and z amounts */
void Matrix2::scale(float x, float y) {
void Matrix2::scale(float x, float y)
{
Matrix2 newMatrix; // Create new identity matrix
newMatrix.init();
@@ -126,17 +135,20 @@ void Matrix2::scale(float x, float y) {
*this *= newMatrix;
}
void Matrix2::scale(const Vector2 &v) {
void Matrix2::scale(const Vector2& v)
{
scale(v.x, v.y);
}
/* Scale all dimensions equally */
void Matrix2::scale(float s) {
scale(s,s);
void Matrix2::scale(float s)
{
scale(s, s);
}
/* Replace matrix with its inverse */
bool Matrix2::invert() {
bool Matrix2::invert()
{
float det = c[0] * c[3] - c[1] * c[2];
if (det == 0) {
return false;
@@ -151,28 +163,30 @@ bool Matrix2::invert() {
return true;
}
void Matrix2::transpose() {
void Matrix2::transpose()
{
float tmp = c[1];
c[1] = c[2];
c[2] = tmp;
}
/* Dot Product, returning Vector2 */
Vector2 Matrix2::Dot(const Matrix2 &m, const Vector2 &v) {
Vector2 Matrix2::Dot(const Matrix2& m, const Vector2& v)
{
return Vector2::Create(
v.c[0] * m.c[0] + v.c[1] * m.c[2],
v.c[0] * m.c[1] + v.c[1] * m.c[3]
);
}
Matrix2 Matrix2::Invert(const Matrix2 &m)
Matrix2 Matrix2::Invert(const Matrix2& m)
{
Matrix2 matInvert = m;
matInvert.invert();
return matInvert;
}
Matrix2 Matrix2::Transpose(const Matrix2 &m)
Matrix2 Matrix2::Transpose(const Matrix2& m)
{
Matrix2 matTranspose = m;
matTranspose.transpose();
@@ -187,7 +201,7 @@ Matrix2 Matrix2::Rotation(float angle)
return m;
}
Matrix2 Matrix2::Scaling(const Vector2 &v)
Matrix2 Matrix2::Scaling(const Vector2& v)
{
Matrix2 m;
m.init();

View File

@@ -35,7 +35,8 @@
namespace kraken {
void Matrix2x3::init() {
void Matrix2x3::init()
{
// Default constructor - Initialize with an identity matrix
static const float IDENTITY_MATRIX[] = {
1.0, 0.0,
@@ -46,49 +47,56 @@ void Matrix2x3::init() {
}
void Matrix2x3::init(float *pMat) {
void Matrix2x3::init(float* pMat)
{
memcpy(c, pMat, sizeof(float) * 6);
}
void Matrix2x3::init(const Vector2 &new_axis_x, const Vector2 &new_axis_y, const Vector2 &new_transform)
void Matrix2x3::init(const Vector2& new_axis_x, const Vector2& new_axis_y, const Vector2& new_transform)
{
c[0] = new_axis_x.x; c[1] = new_axis_x.y;
c[2] = new_axis_y.x; c[3] = new_axis_y.y;
c[4] = new_transform.x; c[5] = new_transform.y;
}
void Matrix2x3::init(const Matrix2x3 &m) {
void Matrix2x3::init(const Matrix2x3& m)
{
memcpy(c, m.c, sizeof(float) * 6);
}
float *Matrix2x3::getPointer() {
float* Matrix2x3::getPointer()
{
return c;
}
float& Matrix2x3::operator[](unsigned i) {
float& Matrix2x3::operator[](unsigned i)
{
return c[i];
}
float Matrix2x3::operator[](unsigned i) const {
float Matrix2x3::operator[](unsigned i) const
{
return c[i];
}
// Overload comparison operator
bool Matrix2x3::operator==(const Matrix2x3 &m) const {
bool Matrix2x3::operator==(const Matrix2x3& m) const
{
return memcmp(c, m.c, sizeof(float) * 6) == 0;
}
// Overload compound multiply operator
Matrix2x3& Matrix2x3::operator*=(const Matrix2x3 &m) {
Matrix2x3& Matrix2x3::operator*=(const Matrix2x3& m)
{
float temp[6];
/*
/*
temp[0] = c[0] * m[0] + c[2] * m[1];
temp[1] = c[1] * m[0] + c[3] * m[1];
temp[2] = c[0] * m[2] + c[2] * m[3];
temp[3] = c[1] * m[2] + c[3] * m[3];
temp[4] = c[0] * m[4] + c[2] * m[5] + c[4];
temp[5] = c[1] * m[4] + c[3] * m[5] + c[5];
*/
*/
temp[0] = m[0] * c[0] + m[2] * c[1];
temp[1] = m[1] * c[0] + m[3] * c[1];
temp[2] = m[0] * c[2] + m[2] * c[3];
@@ -101,14 +109,16 @@ Matrix2x3& Matrix2x3::operator*=(const Matrix2x3 &m) {
}
// Overload multiply operator
Matrix2x3 Matrix2x3::operator*(const Matrix2x3 &m) const {
Matrix2x3 Matrix2x3::operator*(const Matrix2x3& m) const
{
Matrix2x3 ret = *this;
ret *= m;
return ret;
}
/* Perform translation operations on a matrix */
void Matrix2x3::translate(float x, float y) {
void Matrix2x3::translate(float x, float y)
{
Matrix2x3 newMatrix; // Create new identity matrix
newMatrix.init();
@@ -118,13 +128,14 @@ void Matrix2x3::translate(float x, float y) {
*this *= newMatrix;
}
void Matrix2x3::translate(const Vector2 &v)
void Matrix2x3::translate(const Vector2& v)
{
translate(v.x, v.y);
}
/* Rotate a matrix by an angle on a X, Y, or Z axis */
void Matrix2x3::rotate(float angle) {
void Matrix2x3::rotate(float angle)
{
Matrix2x3 newMatrix;
newMatrix.init();
newMatrix.c[0] = cosf(angle);
@@ -136,7 +147,8 @@ void Matrix2x3::rotate(float angle) {
}
/* Scale matrix by separate x, y, and z amounts */
void Matrix2x3::scale(float x, float y) {
void Matrix2x3::scale(float x, float y)
{
Matrix2x3 newMatrix; // Create new identity matrix
newMatrix.init();
@@ -146,16 +158,19 @@ void Matrix2x3::scale(float x, float y) {
*this *= newMatrix;
}
void Matrix2x3::scale(const Vector2 &v) {
void Matrix2x3::scale(const Vector2& v)
{
scale(v.x, v.y);
}
/* Scale all dimensions equally */
void Matrix2x3::scale(float s) {
scale(s,s);
void Matrix2x3::scale(float s)
{
scale(s, s);
}
/* Replace matrix with its inverse */
bool Matrix2x3::invert() {
bool Matrix2x3::invert()
{
float det = c[0] * c[3] - c[1] * c[2];
if (det == 0) {
return false;
@@ -174,7 +189,8 @@ bool Matrix2x3::invert() {
}
/* Dot Product, returning Vector2 */
Vector2 Matrix2x3::Dot(const Matrix2x3 &m, const Vector2 &v) {
Vector2 Matrix2x3::Dot(const Matrix2x3& m, const Vector2& v)
{
return Vector2::Create(
v.c[0] * m.c[0] + v.c[1] * m.c[2] + m.c[4],
v.c[0] * m.c[1] + v.c[1] * m.c[3] + m.c[5]
@@ -182,7 +198,7 @@ Vector2 Matrix2x3::Dot(const Matrix2x3 &m, const Vector2 &v) {
}
// Dot product without including translation; useful for transforming normals and tangents
Vector2 Matrix2x3::DotNoTranslate(const Matrix2x3 &m, const Vector2 &v)
Vector2 Matrix2x3::DotNoTranslate(const Matrix2x3& m, const Vector2& v)
{
return Vector2::Create(
v.c[0] * m.c[0] + v.c[1] * m.c[2],
@@ -190,14 +206,14 @@ Vector2 Matrix2x3::DotNoTranslate(const Matrix2x3 &m, const Vector2 &v)
);
}
Matrix2x3 Matrix2x3::Invert(const Matrix2x3 &m)
Matrix2x3 Matrix2x3::Invert(const Matrix2x3& m)
{
Matrix2x3 matInvert = m;
matInvert.invert();
return matInvert;
}
Matrix2x3 Matrix2x3::Translation(const Vector2 &v)
Matrix2x3 Matrix2x3::Translation(const Vector2& v)
{
Matrix2x3 m;
m.init();
@@ -214,7 +230,7 @@ Matrix2x3 Matrix2x3::Rotation(float angle)
return m;
}
Matrix2x3 Matrix2x3::Scaling(const Vector2 &v)
Matrix2x3 Matrix2x3::Scaling(const Vector2& v)
{
Matrix2x3 m;
m.init();

View File

@@ -35,7 +35,8 @@
namespace kraken {
void Matrix4::init() {
void Matrix4::init()
{
// Default constructor - Initialize with an identity matrix
static const float IDENTITY_MATRIX[] = {
1.0, 0.0, 0.0, 0.0,
@@ -47,11 +48,12 @@ void Matrix4::init() {
}
void Matrix4::init(float *pMat) {
void Matrix4::init(float* pMat)
{
memcpy(c, pMat, sizeof(float) * 16);
}
void Matrix4::init(const Vector3 &new_axis_x, const Vector3 &new_axis_y, const Vector3 &new_axis_z, const Vector3 &new_transform)
void Matrix4::init(const Vector3& new_axis_x, const Vector3& new_axis_y, const Vector3& new_axis_z, const Vector3& new_transform)
{
c[0] = new_axis_x.x; c[1] = new_axis_x.y; c[2] = new_axis_x.z; c[3] = 0.0f;
c[4] = new_axis_y.x; c[5] = new_axis_y.y; c[6] = new_axis_y.z; c[7] = 0.0f;
@@ -59,55 +61,59 @@ void Matrix4::init(const Vector3 &new_axis_x, const Vector3 &new_axis_y, const V
c[12] = new_transform.x; c[13] = new_transform.y; c[14] = new_transform.z; c[15] = 1.0f;
}
void Matrix4::init(const Matrix4 &m) {
void Matrix4::init(const Matrix4& m)
{
memcpy(c, m.c, sizeof(float) * 16);
}
Matrix4 Matrix4::Create(float *pMat)
Matrix4 Matrix4::Create(float* pMat)
{
Matrix4 r;
r.init(pMat);
return r;
}
Matrix4 Matrix4::Create(const Vector3 &new_axis_x, const Vector3 &new_axis_y, const Vector3 &new_axis_z, const Vector3 &new_transform)
Matrix4 Matrix4::Create(const Vector3& new_axis_x, const Vector3& new_axis_y, const Vector3& new_axis_z, const Vector3& new_transform)
{
Matrix4 r;
r.init(new_axis_x, new_axis_y, new_axis_z, new_transform);
return r;
}
float *Matrix4::getPointer() {
float* Matrix4::getPointer()
{
return c;
}
float& Matrix4::operator[](unsigned i) {
float& Matrix4::operator[](unsigned i)
{
return c[i];
}
float Matrix4::operator[](unsigned i) const {
float Matrix4::operator[](unsigned i) const
{
return c[i];
}
// Overload comparison operator
bool Matrix4::operator==(const Matrix4 &m) const {
bool Matrix4::operator==(const Matrix4& m) const
{
return memcmp(c, m.c, sizeof(float) * 16) == 0;
}
// Overload compound multiply operator
Matrix4& Matrix4::operator*=(const Matrix4 &m) {
Matrix4& Matrix4::operator*=(const Matrix4& m)
{
float temp[16];
int x,y;
int x, y;
for (x=0; x < 4; x++)
{
for(y=0; y < 4; y++)
{
temp[y + (x*4)] = (c[x*4] * m.c[y]) +
(c[(x*4)+1] * m.c[y+4]) +
(c[(x*4)+2] * m.c[y+8]) +
(c[(x*4)+3] * m.c[y+12]);
for (x = 0; x < 4; x++) {
for (y = 0; y < 4; y++) {
temp[y + (x * 4)] = (c[x * 4] * m.c[y]) +
(c[(x * 4) + 1] * m.c[y + 4]) +
(c[(x * 4) + 2] * m.c[y + 8]) +
(c[(x * 4) + 3] * m.c[y + 12]);
}
}
@@ -116,7 +122,8 @@ Matrix4& Matrix4::operator*=(const Matrix4 &m) {
}
// Overload multiply operator
Matrix4 Matrix4::operator*(const Matrix4 &m) const {
Matrix4 Matrix4::operator*(const Matrix4& m) const
{
Matrix4 ret = *this;
ret *= m;
return ret;
@@ -125,11 +132,12 @@ Matrix4 Matrix4::operator*(const Matrix4 &m) const {
/* Generate a perspective view matrix using a field of view angle fov,
* window aspect ratio, near and far clipping planes */
void Matrix4::perspective(float fov, float aspect, float nearz, float farz) {
void Matrix4::perspective(float fov, float aspect, float nearz, float farz)
{
memset(c, 0, sizeof(float) * 16);
float range= tanf(fov * 0.5f) * nearz;
float range = tanf(fov * 0.5f) * nearz;
c[0] = (2 * nearz) / ((range * aspect) - (-range * aspect));
c[5] = (2 * nearz) / (2 * range);
c[10] = -(farz + nearz) / (farz - nearz);
@@ -149,7 +157,8 @@ void Matrix4::perspective(float fov, float aspect, float nearz, float farz) {
}
/* Perform translation operations on a matrix */
void Matrix4::translate(float x, float y, float z) {
void Matrix4::translate(float x, float y, float z)
{
Matrix4 newMatrix; // Create new identity matrix
newMatrix.init();
@@ -160,13 +169,14 @@ void Matrix4::translate(float x, float y, float z) {
*this *= newMatrix;
}
void Matrix4::translate(const Vector3 &v)
void Matrix4::translate(const Vector3& v)
{
translate(v.x, v.y, v.z);
}
/* Rotate a matrix by an angle on a X, Y, or Z axis */
void Matrix4::rotate(float angle, AXIS axis) {
void Matrix4::rotate(float angle, AXIS axis)
{
const int cos1[3] = { 5, 0, 0 }; // cos(angle)
const int cos2[3] = { 10, 10, 5 }; // cos(angle)
const int sin1[3] = { 9, 2, 4 }; // -sin(angle)
@@ -208,13 +218,14 @@ void Matrix4::rotate(float angle, AXIS axis) {
*this *= newMatrix;
}
void Matrix4::rotate(const Quaternion &q)
void Matrix4::rotate(const Quaternion& q)
{
*this *= q.rotationMatrix();
}
/* Scale matrix by separate x, y, and z amounts */
void Matrix4::scale(float x, float y, float z) {
void Matrix4::scale(float x, float y, float z)
{
Matrix4 newMatrix; // Create new identity matrix
newMatrix.init();
@@ -225,17 +236,20 @@ void Matrix4::scale(float x, float y, float z) {
*this *= newMatrix;
}
void Matrix4::scale(const Vector3 &v) {
void Matrix4::scale(const Vector3& v)
{
scale(v.x, v.y, v.z);
}
/* Scale all dimensions equally */
void Matrix4::scale(float s) {
scale(s,s,s);
void Matrix4::scale(float s)
{
scale(s, s, s);
}
// Initialize with a bias matrix
void Matrix4::bias() {
// Initialize with a bias matrix
void Matrix4::bias()
{
static const float BIAS_MATRIX[] = {
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
@@ -247,7 +261,8 @@ void Matrix4::bias() {
/* Generate an orthographic view matrix */
void Matrix4::ortho(float left, float right, float top, float bottom, float nearz, float farz) {
void Matrix4::ortho(float left, float right, float top, float bottom, float nearz, float farz)
{
memset(c, 0, sizeof(float) * 16);
c[0] = 2.0f / (right - left);
c[5] = 2.0f / (bottom - top);
@@ -257,46 +272,47 @@ void Matrix4::ortho(float left, float right, float top, float bottom, float near
}
/* Replace matrix with its inverse */
bool Matrix4::invert() {
bool Matrix4::invert()
{
// Based on gluInvertMatrix implementation
float inv[16], det;
int i;
inv[0] = c[5]*c[10]*c[15] - c[5]*c[11]*c[14] - c[9]*c[6]*c[15]
+ c[9]*c[7]*c[14] + c[13]*c[6]*c[11] - c[13]*c[7]*c[10];
inv[4] = -c[4]*c[10]*c[15] + c[4]*c[11]*c[14] + c[8]*c[6]*c[15]
- c[8]*c[7]*c[14] - c[12]*c[6]*c[11] + c[12]*c[7]*c[10];
inv[8] = c[4]*c[9]*c[15] - c[4]*c[11]*c[13] - c[8]*c[5]*c[15]
+ c[8]*c[7]*c[13] + c[12]*c[5]*c[11] - c[12]*c[7]*c[9];
inv[12] = -c[4]*c[9]*c[14] + c[4]*c[10]*c[13] + c[8]*c[5]*c[14]
- c[8]*c[6]*c[13] - c[12]*c[5]*c[10] + c[12]*c[6]*c[9];
inv[1] = -c[1]*c[10]*c[15] + c[1]*c[11]*c[14] + c[9]*c[2]*c[15]
- c[9]*c[3]*c[14] - c[13]*c[2]*c[11] + c[13]*c[3]*c[10];
inv[5] = c[0]*c[10]*c[15] - c[0]*c[11]*c[14] - c[8]*c[2]*c[15]
+ c[8]*c[3]*c[14] + c[12]*c[2]*c[11] - c[12]*c[3]*c[10];
inv[9] = -c[0]*c[9]*c[15] + c[0]*c[11]*c[13] + c[8]*c[1]*c[15]
- c[8]*c[3]*c[13] - c[12]*c[1]*c[11] + c[12]*c[3]*c[9];
inv[13] = c[0]*c[9]*c[14] - c[0]*c[10]*c[13] - c[8]*c[1]*c[14]
+ c[8]*c[2]*c[13] + c[12]*c[1]*c[10] - c[12]*c[2]*c[9];
inv[2] = c[1]*c[6]*c[15] - c[1]*c[7]*c[14] - c[5]*c[2]*c[15]
+ c[5]*c[3]*c[14] + c[13]*c[2]*c[7] - c[13]*c[3]*c[6];
inv[6] = -c[0]*c[6]*c[15] + c[0]*c[7]*c[14] + c[4]*c[2]*c[15]
- c[4]*c[3]*c[14] - c[12]*c[2]*c[7] + c[12]*c[3]*c[6];
inv[10] = c[0]*c[5]*c[15] - c[0]*c[7]*c[13] - c[4]*c[1]*c[15]
+ c[4]*c[3]*c[13] + c[12]*c[1]*c[7] - c[12]*c[3]*c[5];
inv[14] = -c[0]*c[5]*c[14] + c[0]*c[6]*c[13] + c[4]*c[1]*c[14]
- c[4]*c[2]*c[13] - c[12]*c[1]*c[6] + c[12]*c[2]*c[5];
inv[3] = -c[1]*c[6]*c[11] + c[1]*c[7]*c[10] + c[5]*c[2]*c[11]
- c[5]*c[3]*c[10] - c[9]*c[2]*c[7] + c[9]*c[3]*c[6];
inv[7] = c[0]*c[6]*c[11] - c[0]*c[7]*c[10] - c[4]*c[2]*c[11]
+ c[4]*c[3]*c[10] + c[8]*c[2]*c[7] - c[8]*c[3]*c[6];
inv[11] = -c[0]*c[5]*c[11] + c[0]*c[7]*c[9] + c[4]*c[1]*c[11]
- c[4]*c[3]*c[9] - c[8]*c[1]*c[7] + c[8]*c[3]*c[5];
inv[15] = c[0]*c[5]*c[10] - c[0]*c[6]*c[9] - c[4]*c[1]*c[10]
+ c[4]*c[2]*c[9] + c[8]*c[1]*c[6] - c[8]*c[2]*c[5];
inv[0] = c[5] * c[10] * c[15] - c[5] * c[11] * c[14] - c[9] * c[6] * c[15]
+ c[9] * c[7] * c[14] + c[13] * c[6] * c[11] - c[13] * c[7] * c[10];
inv[4] = -c[4] * c[10] * c[15] + c[4] * c[11] * c[14] + c[8] * c[6] * c[15]
- c[8] * c[7] * c[14] - c[12] * c[6] * c[11] + c[12] * c[7] * c[10];
inv[8] = c[4] * c[9] * c[15] - c[4] * c[11] * c[13] - c[8] * c[5] * c[15]
+ c[8] * c[7] * c[13] + c[12] * c[5] * c[11] - c[12] * c[7] * c[9];
inv[12] = -c[4] * c[9] * c[14] + c[4] * c[10] * c[13] + c[8] * c[5] * c[14]
- c[8] * c[6] * c[13] - c[12] * c[5] * c[10] + c[12] * c[6] * c[9];
inv[1] = -c[1] * c[10] * c[15] + c[1] * c[11] * c[14] + c[9] * c[2] * c[15]
- c[9] * c[3] * c[14] - c[13] * c[2] * c[11] + c[13] * c[3] * c[10];
inv[5] = c[0] * c[10] * c[15] - c[0] * c[11] * c[14] - c[8] * c[2] * c[15]
+ c[8] * c[3] * c[14] + c[12] * c[2] * c[11] - c[12] * c[3] * c[10];
inv[9] = -c[0] * c[9] * c[15] + c[0] * c[11] * c[13] + c[8] * c[1] * c[15]
- c[8] * c[3] * c[13] - c[12] * c[1] * c[11] + c[12] * c[3] * c[9];
inv[13] = c[0] * c[9] * c[14] - c[0] * c[10] * c[13] - c[8] * c[1] * c[14]
+ c[8] * c[2] * c[13] + c[12] * c[1] * c[10] - c[12] * c[2] * c[9];
inv[2] = c[1] * c[6] * c[15] - c[1] * c[7] * c[14] - c[5] * c[2] * c[15]
+ c[5] * c[3] * c[14] + c[13] * c[2] * c[7] - c[13] * c[3] * c[6];
inv[6] = -c[0] * c[6] * c[15] + c[0] * c[7] * c[14] + c[4] * c[2] * c[15]
- c[4] * c[3] * c[14] - c[12] * c[2] * c[7] + c[12] * c[3] * c[6];
inv[10] = c[0] * c[5] * c[15] - c[0] * c[7] * c[13] - c[4] * c[1] * c[15]
+ c[4] * c[3] * c[13] + c[12] * c[1] * c[7] - c[12] * c[3] * c[5];
inv[14] = -c[0] * c[5] * c[14] + c[0] * c[6] * c[13] + c[4] * c[1] * c[14]
- c[4] * c[2] * c[13] - c[12] * c[1] * c[6] + c[12] * c[2] * c[5];
inv[3] = -c[1] * c[6] * c[11] + c[1] * c[7] * c[10] + c[5] * c[2] * c[11]
- c[5] * c[3] * c[10] - c[9] * c[2] * c[7] + c[9] * c[3] * c[6];
inv[7] = c[0] * c[6] * c[11] - c[0] * c[7] * c[10] - c[4] * c[2] * c[11]
+ c[4] * c[3] * c[10] + c[8] * c[2] * c[7] - c[8] * c[3] * c[6];
inv[11] = -c[0] * c[5] * c[11] + c[0] * c[7] * c[9] + c[4] * c[1] * c[11]
- c[4] * c[3] * c[9] - c[8] * c[1] * c[7] + c[8] * c[3] * c[5];
inv[15] = c[0] * c[5] * c[10] - c[0] * c[6] * c[9] - c[4] * c[1] * c[10]
+ c[4] * c[2] * c[9] + c[8] * c[1] * c[6] - c[8] * c[2] * c[5];
det = c[0]*inv[0] + c[1]*inv[4] + c[2]*inv[8] + c[3]*inv[12];
det = c[0] * inv[0] + c[1] * inv[4] + c[2] * inv[8] + c[3] * inv[12];
if (det == 0) {
return false;
@@ -311,10 +327,11 @@ bool Matrix4::invert() {
return true;
}
void Matrix4::transpose() {
void Matrix4::transpose()
{
float trans[16];
for(int x=0; x<4; x++) {
for(int y=0; y<4; y++) {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
trans[x + y * 4] = c[y + x * 4];
}
}
@@ -322,7 +339,8 @@ void Matrix4::transpose() {
}
/* Dot Product, returning Vector3 */
Vector3 Matrix4::Dot(const Matrix4 &m, const Vector3 &v) {
Vector3 Matrix4::Dot(const Matrix4& m, const Vector3& v)
{
return Vector3::Create(
v.c[0] * m.c[0] + v.c[1] * m.c[4] + v.c[2] * m.c[8] + m.c[12],
v.c[0] * m.c[1] + v.c[1] * m.c[5] + v.c[2] * m.c[9] + m.c[13],
@@ -330,7 +348,8 @@ Vector3 Matrix4::Dot(const Matrix4 &m, const Vector3 &v) {
);
}
Vector4 Matrix4::Dot4(const Matrix4 &m, const Vector4 &v) {
Vector4 Matrix4::Dot4(const Matrix4& m, const Vector4& v)
{
#ifdef KRAKEN_USE_ARM_NEON
Vector4 d;
@@ -350,7 +369,7 @@ Vector4 Matrix4::Dot4(const Matrix4 &m, const Vector4 &v) {
"vst1.32 {d26, d27}, [%2] \n\t" //Q4 = m+12
: /* no output registers */
: "r"(m.c), "r"(v.c), "r"(d.c)
: "q0", "q9", "q10","q11", "q12", "q13", "memory"
: "q0", "q9", "q10", "q11", "q12", "q13", "memory"
);
return d;
#else
@@ -364,7 +383,7 @@ Vector4 Matrix4::Dot4(const Matrix4 &m, const Vector4 &v) {
}
// Dot product without including translation; useful for transforming normals and tangents
Vector3 Matrix4::DotNoTranslate(const Matrix4 &m, const Vector3 &v)
Vector3 Matrix4::DotNoTranslate(const Matrix4& m, const Vector3& v)
{
return Vector3::Create(
v.x * m.c[0] + v.y * m.c[4] + v.z * m.c[8],
@@ -374,17 +393,19 @@ Vector3 Matrix4::DotNoTranslate(const Matrix4 &m, const Vector3 &v)
}
/* Dot Product, returning w component as if it were a Vector4 (This will be deprecated once Vector4 is implemented instead*/
float Matrix4::DotW(const Matrix4 &m, const Vector3 &v) {
return v.x * m.c[0*4 + 3] + v.y * m.c[1*4 + 3] + v.z * m.c[2*4 + 3] + m.c[3*4 + 3];
float Matrix4::DotW(const Matrix4& m, const Vector3& v)
{
return v.x * m.c[0 * 4 + 3] + v.y * m.c[1 * 4 + 3] + v.z * m.c[2 * 4 + 3] + m.c[3 * 4 + 3];
}
/* Dot Product followed by W-divide */
Vector3 Matrix4::DotWDiv(const Matrix4 &m, const Vector3 &v) {
Vector3 Matrix4::DotWDiv(const Matrix4& m, const Vector3& v)
{
Vector4 r = Dot4(m, Vector4::Create(v, 1.0f));
return Vector3::Create(r) / r.w;
}
Matrix4 Matrix4::LookAt(const Vector3 &cameraPos, const Vector3 &lookAtPos, const Vector3 &upDirection)
Matrix4 Matrix4::LookAt(const Vector3& cameraPos, const Vector3& lookAtPos, const Vector3& upDirection)
{
Matrix4 matLookat;
matLookat.init();
@@ -413,32 +434,32 @@ Matrix4 Matrix4::LookAt(const Vector3 &cameraPos, const Vector3 &lookAtPos, cons
return matLookat;
}
Matrix4 Matrix4::Invert(const Matrix4 &m)
Matrix4 Matrix4::Invert(const Matrix4& m)
{
Matrix4 matInvert = m;
matInvert.invert();
return matInvert;
}
Matrix4 Matrix4::Transpose(const Matrix4 &m)
Matrix4 Matrix4::Transpose(const Matrix4& m)
{
Matrix4 matTranspose = m;
matTranspose.transpose();
return matTranspose;
}
Matrix4 Matrix4::Translation(const Vector3 &v)
Matrix4 Matrix4::Translation(const Vector3& v)
{
Matrix4 m;
m.init();
m[12] = v.x;
m[13] = v.y;
m[14] = v.z;
// m.translate(v);
// m.translate(v);
return m;
}
Matrix4 Matrix4::Rotation(const Vector3 &v)
Matrix4 Matrix4::Rotation(const Vector3& v)
{
Matrix4 m;
m.init();
@@ -448,7 +469,7 @@ Matrix4 Matrix4::Rotation(const Vector3 &v)
return m;
}
Matrix4 Matrix4::Scaling(const Vector3 &v)
Matrix4 Matrix4::Scaling(const Vector3& v)
{
Matrix4 m;
m.init();

View File

@@ -35,7 +35,8 @@
namespace kraken {
void Quaternion::init() {
void Quaternion::init()
{
c[0] = 1.0;
c[1] = 0.0;
c[2] = 0.0;
@@ -49,7 +50,8 @@ Quaternion Quaternion::Create()
return r;
}
void Quaternion::init(float w, float x, float y, float z) {
void Quaternion::init(float w, float x, float y, float z)
{
c[0] = w;
c[1] = x;
c[2] = y;
@@ -63,7 +65,8 @@ Quaternion Quaternion::Create(float w, float x, float y, float z)
return r;
}
void Quaternion::init(const Quaternion& p) {
void Quaternion::init(const Quaternion& p)
{
c[0] = p[0];
c[1] = p[1];
c[2] = p[2];
@@ -77,18 +80,20 @@ Quaternion Quaternion::Create(const Quaternion& p)
return r;
}
void Quaternion::init(const Vector3 &euler) {
void Quaternion::init(const Vector3& euler)
{
setEulerZYX(euler);
}
Quaternion Quaternion::Create(const Vector3 &euler)
Quaternion Quaternion::Create(const Vector3& euler)
{
Quaternion r;
r.init(euler);
return r;
}
void Quaternion::init(const Vector3 &from_vector, const Vector3 &to_vector) {
void Quaternion::init(const Vector3& from_vector, const Vector3& to_vector)
{
Vector3 a = Vector3::Cross(from_vector, to_vector);
c[0] = a[0];
@@ -98,21 +103,22 @@ void Quaternion::init(const Vector3 &from_vector, const Vector3 &to_vector) {
normalize();
}
Quaternion Quaternion::Create(const Vector3 &from_vector, const Vector3 &to_vector)
Quaternion Quaternion::Create(const Vector3& from_vector, const Vector3& to_vector)
{
Quaternion r;
r.init(from_vector, to_vector);
return r;
}
void Quaternion::setEulerXYZ(const Vector3 &euler)
void Quaternion::setEulerXYZ(const Vector3& euler)
{
*this = Quaternion::FromAngleAxis(Vector3::Create(1.0f, 0.0f, 0.0f), euler.x)
* Quaternion::FromAngleAxis(Vector3::Create(0.0f, 1.0f, 0.0f), euler.y)
* Quaternion::FromAngleAxis(Vector3::Create(0.0f, 0.0f, 1.0f), euler.z);
}
void Quaternion::setEulerZYX(const Vector3 &euler) {
void Quaternion::setEulerZYX(const Vector3& euler)
{
// ZYX Order!
float c1 = cosf(euler[0] * 0.5f);
float c2 = cosf(euler[1] * 0.5f);
@@ -127,23 +133,26 @@ void Quaternion::setEulerZYX(const Vector3 &euler) {
c[3] = c1 * c2 * s3 - s1 * s2 * c3;
}
float Quaternion::operator [](unsigned i) const {
float Quaternion::operator [](unsigned i) const
{
return c[i];
}
float &Quaternion::operator [](unsigned i) {
float& Quaternion::operator [](unsigned i)
{
return c[i];
}
Vector3 Quaternion::eulerXYZ() const {
Vector3 Quaternion::eulerXYZ() const
{
float a2 = 2 * (c[0] * c[2] - c[1] * c[3]);
if(a2 <= -0.99999) {
if (a2 <= -0.99999) {
return Vector3::Create(
2.0f * atan2f(c[1], c[0]),
-PI * 0.5f,
0
);
} else if(a2 >= 0.99999) {
} else if (a2 >= 0.99999) {
return Vector3::Create(
2.0f * atan2f(c[1], c[0]),
PI * 0.5f,
@@ -160,7 +169,8 @@ Vector3 Quaternion::eulerXYZ() const {
}
bool operator ==(Quaternion &v1, Quaternion &v2) {
bool operator ==(Quaternion& v1, Quaternion& v2)
{
return
v1[0] == v2[0]
&& v1[1] == v2[1]
@@ -168,7 +178,8 @@ bool operator ==(Quaternion &v1, Quaternion &v2) {
&& v1[3] == v2[3];
}
bool operator !=(Quaternion &v1, Quaternion &v2) {
bool operator !=(Quaternion& v1, Quaternion& v2)
{
return
v1[0] != v2[0]
|| v1[1] != v2[1]
@@ -176,44 +187,50 @@ bool operator !=(Quaternion &v1, Quaternion &v2) {
|| v1[3] != v2[3];
}
Quaternion Quaternion::operator *(const Quaternion &v) {
float t0 = (c[3]-c[2])*(v[2]-v[3]);
float t1 = (c[0]+c[1])*(v[0]+v[1]);
float t2 = (c[0]-c[1])*(v[2]+v[3]);
float t3 = (c[3]+c[2])*(v[0]-v[1]);
float t4 = (c[3]-c[1])*(v[1]-v[2]);
float t5 = (c[3]+c[1])*(v[1]+v[2]);
float t6 = (c[0]+c[2])*(v[0]-v[3]);
float t7 = (c[0]-c[2])*(v[0]+v[3]);
float t8 = t5+t6+t7;
float t9 = (t4+t8)/2;
Quaternion Quaternion::operator *(const Quaternion& v)
{
float t0 = (c[3] - c[2]) * (v[2] - v[3]);
float t1 = (c[0] + c[1]) * (v[0] + v[1]);
float t2 = (c[0] - c[1]) * (v[2] + v[3]);
float t3 = (c[3] + c[2]) * (v[0] - v[1]);
float t4 = (c[3] - c[1]) * (v[1] - v[2]);
float t5 = (c[3] + c[1]) * (v[1] + v[2]);
float t6 = (c[0] + c[2]) * (v[0] - v[3]);
float t7 = (c[0] - c[2]) * (v[0] + v[3]);
float t8 = t5 + t6 + t7;
float t9 = (t4 + t8) / 2;
return Quaternion::Create(
t0+t9-t5,
t1+t9-t8,
t2+t9-t7,
t3+t9-t6
t0 + t9 - t5,
t1 + t9 - t8,
t2 + t9 - t7,
t3 + t9 - t6
);
}
Quaternion Quaternion::operator *(float v) const {
Quaternion Quaternion::operator *(float v) const
{
return Quaternion::Create(c[0] * v, c[1] * v, c[2] * v, c[3] * v);
}
Quaternion Quaternion::operator /(float num) const {
Quaternion Quaternion::operator /(float num) const
{
float inv_num = 1.0f / num;
return Quaternion::Create(c[0] * inv_num, c[1] * inv_num, c[2] * inv_num, c[3] * inv_num);
}
Quaternion Quaternion::operator +(const Quaternion &v) const {
Quaternion Quaternion::operator +(const Quaternion& v) const
{
return Quaternion::Create(c[0] + v[0], c[1] + v[1], c[2] + v[2], c[3] + v[3]);
}
Quaternion Quaternion::operator -(const Quaternion &v) const {
Quaternion Quaternion::operator -(const Quaternion& v) const
{
return Quaternion::Create(c[0] - v[0], c[1] - v[1], c[2] - v[2], c[3] - v[3]);
}
Quaternion& Quaternion::operator +=(const Quaternion& v) {
Quaternion& Quaternion::operator +=(const Quaternion& v)
{
c[0] += v[0];
c[1] += v[1];
c[2] += v[2];
@@ -221,7 +238,8 @@ Quaternion& Quaternion::operator +=(const Quaternion& v) {
return *this;
}
Quaternion& Quaternion::operator -=(const Quaternion& v) {
Quaternion& Quaternion::operator -=(const Quaternion& v)
{
c[0] -= v[0];
c[1] -= v[1];
c[2] -= v[2];
@@ -229,27 +247,29 @@ Quaternion& Quaternion::operator -=(const Quaternion& v) {
return *this;
}
Quaternion& Quaternion::operator *=(const Quaternion& v) {
float t0 = (c[3]-c[2])*(v[2]-v[3]);
float t1 = (c[0]+c[1])*(v[0]+v[1]);
float t2 = (c[0]-c[1])*(v[2]+v[3]);
float t3 = (c[3]+c[2])*(v[0]-v[1]);
float t4 = (c[3]-c[1])*(v[1]-v[2]);
float t5 = (c[3]+c[1])*(v[1]+v[2]);
float t6 = (c[0]+c[2])*(v[0]-v[3]);
float t7 = (c[0]-c[2])*(v[0]+v[3]);
float t8 = t5+t6+t7;
float t9 = (t4+t8)/2;
Quaternion& Quaternion::operator *=(const Quaternion& v)
{
float t0 = (c[3] - c[2]) * (v[2] - v[3]);
float t1 = (c[0] + c[1]) * (v[0] + v[1]);
float t2 = (c[0] - c[1]) * (v[2] + v[3]);
float t3 = (c[3] + c[2]) * (v[0] - v[1]);
float t4 = (c[3] - c[1]) * (v[1] - v[2]);
float t5 = (c[3] + c[1]) * (v[1] + v[2]);
float t6 = (c[0] + c[2]) * (v[0] - v[3]);
float t7 = (c[0] - c[2]) * (v[0] + v[3]);
float t8 = t5 + t6 + t7;
float t9 = (t4 + t8) / 2;
c[0] = t0+t9-t5;
c[1] = t1+t9-t8;
c[2] = t2+t9-t7;
c[3] = t3+t9-t6;
c[0] = t0 + t9 - t5;
c[1] = t1 + t9 - t8;
c[2] = t2 + t9 - t7;
c[3] = t3 + t9 - t6;
return *this;
}
Quaternion& Quaternion::operator *=(const float& v) {
Quaternion& Quaternion::operator *=(const float& v)
{
c[0] *= v;
c[1] *= v;
c[2] *= v;
@@ -257,7 +277,8 @@ Quaternion& Quaternion::operator *=(const float& v) {
return *this;
}
Quaternion& Quaternion::operator /=(const float& v) {
Quaternion& Quaternion::operator /=(const float& v)
{
float inv_v = 1.0f / v;
c[0] *= inv_v;
c[1] *= inv_v;
@@ -266,15 +287,18 @@ Quaternion& Quaternion::operator /=(const float& v) {
return *this;
}
Quaternion Quaternion::operator +() const {
Quaternion Quaternion::operator +() const
{
return *this;
}
Quaternion Quaternion::operator -() const {
Quaternion Quaternion::operator -() const
{
return Quaternion::Create(-c[0], -c[1], -c[2], -c[3]);
}
Quaternion Quaternion::Normalize(const Quaternion &v1) {
Quaternion Quaternion::Normalize(const Quaternion& v1)
{
float inv_magnitude = 1.0f / sqrtf(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2] + v1[3] * v1[3]);
return Quaternion::Create(
v1[0] * inv_magnitude,
@@ -284,7 +308,8 @@ Quaternion Quaternion::Normalize(const Quaternion &v1) {
);
}
void Quaternion::normalize() {
void Quaternion::normalize()
{
float inv_magnitude = 1.0f / sqrtf(c[0] * c[0] + c[1] * c[1] + c[2] * c[2] + c[3] * c[3]);
c[0] *= inv_magnitude;
c[1] *= inv_magnitude;
@@ -292,26 +317,31 @@ void Quaternion::normalize() {
c[3] *= inv_magnitude;
}
Quaternion Quaternion::Conjugate(const Quaternion &v1) {
Quaternion Quaternion::Conjugate(const Quaternion& v1)
{
return Quaternion::Create(v1[0], -v1[1], -v1[2], -v1[3]);
}
void Quaternion::conjugate() {
void Quaternion::conjugate()
{
c[1] = -c[1];
c[2] = -c[2];
c[3] = -c[3];
}
void Quaternion::invert() {
void Quaternion::invert()
{
conjugate();
normalize();
}
Quaternion Quaternion::Invert(const Quaternion &v1) {
Quaternion Quaternion::Invert(const Quaternion& v1)
{
return Normalize(Conjugate(v1));
}
Matrix4 Quaternion::rotationMatrix() const {
Matrix4 Quaternion::rotationMatrix() const
{
Matrix4 matRotate;
matRotate.init();
@@ -342,16 +372,16 @@ Matrix4 Quaternion::rotationMatrix() const {
}
Quaternion Quaternion::FromAngleAxis(const Vector3 &axis, float angle)
Quaternion Quaternion::FromAngleAxis(const Vector3& axis, float angle)
{
float ha = angle * 0.5f;
float sha = sinf(ha);
return Quaternion::Create(cosf(ha), axis.x * sha, axis.y * sha, axis.z * sha);
}
Quaternion Quaternion::FromRotationMatrix(const Matrix4 &m)
Quaternion Quaternion::FromRotationMatrix(const Matrix4& m)
{
// see http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
// see http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
const float trace = m[0] + m[5] + m[10];
float w, x, y, z;
if (trace > 0.0) {
@@ -382,12 +412,12 @@ Quaternion Quaternion::FromRotationMatrix(const Matrix4 &m)
return Quaternion::Create(w, x, y, z);
}
float Quaternion::Dot(const Quaternion &v1, const Quaternion &v2)
float Quaternion::Dot(const Quaternion& v1, const Quaternion& v2)
{
return v1.c[0] * v2.c[0] + v1.c[1] * v2.c[1] + v1.c[2] * v2.c[2] + v1.c[3] * v2.c[3];
}
Quaternion Quaternion::Lerp(const Quaternion &a, const Quaternion &b, float t)
Quaternion Quaternion::Lerp(const Quaternion& a, const Quaternion& b, float t)
{
if (t <= 0.0f) {
return a;
@@ -398,7 +428,7 @@ Quaternion Quaternion::Lerp(const Quaternion &a, const Quaternion &b, float t)
return a * (1.0f - t) + b * t;
}
Quaternion Quaternion::Slerp(const Quaternion &a, const Quaternion &b, float t)
Quaternion Quaternion::Slerp(const Quaternion& a, const Quaternion& b, float t)
{
if (t <= 0.0f) {
return a;
@@ -413,12 +443,12 @@ Quaternion Quaternion::Slerp(const Quaternion &a, const Quaternion &b, float t)
// Angle is greater than 180. We can negate the angle/quat to get the
// shorter rotation to reach the same destination.
if ( coshalftheta < 0.0f ) {
if (coshalftheta < 0.0f) {
coshalftheta = -coshalftheta;
c = -c;
}
if ( coshalftheta > (1.0f - std::numeric_limits<float>::epsilon())) {
if (coshalftheta > (1.0f - std::numeric_limits<float>::epsilon())) {
// Angle is tiny - save some computation by lerping instead.
return Lerp(c, b, t);
}

View File

@@ -34,8 +34,8 @@
using namespace kraken;
namespace {
bool _intersectSphere(const Vector3 &start, const Vector3 &dir, const Vector3 &sphere_center, float sphere_radius, float &distance)
{
bool _intersectSphere(const Vector3& start, const Vector3& dir, const Vector3& sphere_center, float sphere_radius, float& distance)
{
// dir must be normalized
// From: http://archive.gamedev.net/archive/reference/articles/article1026.html
@@ -61,10 +61,10 @@ namespace {
}
return true;
}
}
bool _sameSide(const Vector3 &p1, const Vector3 &p2, const Vector3 &a, const Vector3 &b)
{
bool _sameSide(const Vector3& p1, const Vector3& p2, const Vector3& a, const Vector3& b)
{
// TODO - Move to Vector3 class?
// From: http://stackoverflow.com/questions/995445/determine-if-a-3d-point-is-within-a-triangle
@@ -72,10 +72,10 @@ namespace {
Vector3 cp2 = Vector3::Cross(b - a, p2 - a);
if (Vector3::Dot(cp1, cp2) >= 0) return true;
return false;
}
}
Vector3 _closestPointOnLine(const Vector3 &a, const Vector3 &b, const Vector3 &p)
{
Vector3 _closestPointOnLine(const Vector3& a, const Vector3& b, const Vector3& p)
{
// From: http://stackoverflow.com/questions/995445/determine-if-a-3d-point-is-within-a-triangle
// Determine t (the length of the vector from a to p)
@@ -93,33 +93,33 @@ namespace {
// Return the point between a and b
return a + V * t;
}
}
} // anonymous namespace
namespace kraken {
void Triangle3::init(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
void Triangle3::init(const Vector3& v1, const Vector3& v2, const Vector3& v3)
{
vert[0] = v1;
vert[1] = v2;
vert[2] = v3;
}
void Triangle3::init(const Triangle3 &tri)
void Triangle3::init(const Triangle3& tri)
{
vert[0] = tri[0];
vert[1] = tri[1];
vert[2] = tri[2];
}
Triangle3 Triangle3::Create(const Triangle3 &tri)
Triangle3 Triangle3::Create(const Triangle3& tri)
{
Triangle3 r;
r.init(tri);
return r;
}
Triangle3 Triangle3::Create(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
Triangle3 Triangle3::Create(const Vector3& v1, const Vector3& v2, const Vector3& v3)
{
Triangle3 r;
r.init(v1, v2, v3);
@@ -147,7 +147,7 @@ Vector3 Triangle3::operator[](unsigned int i) const
}
bool Triangle3::rayCast(const Vector3 &start, const Vector3 &dir, Vector3 &hit_point) const
bool Triangle3::rayCast(const Vector3& start, const Vector3& dir, Vector3& hit_point) const
{
// algorithm based on Dan Sunday's implementation at http://geomalgorithms.com/a06-_intersect-2.html
const float SMALL_NUM = 0.00000001f; // anything that avoids division overflow
@@ -164,7 +164,7 @@ bool Triangle3::rayCast(const Vector3 &start, const Vector3 &dir, Vector3 &hit_p
w0 = start - vert[0];
a = -Vector3::Dot(n, w0);
b = Vector3::Dot(n,dir);
b = Vector3::Dot(n, dir);
if (fabs(b) < SMALL_NUM) { // ray is parallel to triangle plane
if (a == 0)
return false; // ray lies in triangle plane
@@ -177,19 +177,19 @@ bool Triangle3::rayCast(const Vector3 &start, const Vector3 &dir, Vector3 &hit_p
r = a / b;
if (r < 0.0f) // ray goes away from triangle
return false; // => no intersect
// for a segment, also test if (r > 1.0) => no intersect
// for a segment, also test if (r > 1.0) => no intersect
Vector3 plane_hit_point = start + dir * r; // intersect point of ray and plane
// is plane_hit_point inside triangle?
float uu, uv, vv, wu, wv, D;
uu = Vector3::Dot(u,u);
uv = Vector3::Dot(u,v);
vv = Vector3::Dot(v,v);
uu = Vector3::Dot(u, u);
uv = Vector3::Dot(u, v);
vv = Vector3::Dot(v, v);
w = plane_hit_point - vert[0];
wu = Vector3::Dot(w,u);
wv = Vector3::Dot(w,v);
wu = Vector3::Dot(w, u);
wv = Vector3::Dot(w, v);
D = uv * uv - uu * vv;
// get and test parametric coords
@@ -214,7 +214,7 @@ Vector3 Triangle3::calculateNormal() const
return Vector3::Normalize(Vector3::Cross(v1, v2));
}
Vector3 Triangle3::closestPointOnTriangle(const Vector3 &p) const
Vector3 Triangle3::closestPointOnTriangle(const Vector3& p) const
{
Vector3 a = vert[0];
Vector3 b = vert[1];
@@ -229,16 +229,16 @@ Vector3 Triangle3::closestPointOnTriangle(const Vector3 &p) const
float sd_Rbc = (p - Rbc).sqrMagnitude();
float sd_Rca = (p - Rca).sqrMagnitude();
if(sd_Rab < sd_Rbc && sd_Rab < sd_Rca) {
if (sd_Rab < sd_Rbc && sd_Rab < sd_Rca) {
return Rab;
} else if(sd_Rbc < sd_Rab && sd_Rbc < sd_Rca) {
} else if (sd_Rbc < sd_Rab && sd_Rbc < sd_Rca) {
return Rbc;
} else {
return Rca;
}
}
bool Triangle3::sphereCast(const Vector3 &start, const Vector3 &dir, float radius, Vector3 &hit_point, float &hit_distance) const
bool Triangle3::sphereCast(const Vector3& start, const Vector3& dir, float radius, Vector3& hit_point, float& hit_distance) const
{
// Dir must be normalized
const float SMALL_NUM = 0.001f; // anything that avoids division overflow
@@ -254,12 +254,12 @@ bool Triangle3::sphereCast(const Vector3 &start, const Vector3 &dir, float radiu
float denom = Vector3::Dot(tri_normal, dir);
if(denom > -SMALL_NUM) {
if (denom > -SMALL_NUM) {
return false; // dir is co-planar with the triangle or going in the direction of the normal; no intersection
}
// Detect an embedded plane, caused by a sphere that is already intersecting the plane.
if(cotangent_distance <= 0 && cotangent_distance >= -radius * 2.0f) {
if (cotangent_distance <= 0 && cotangent_distance >= -radius * 2.0f) {
// Embedded plane - Sphere is already intersecting the plane.
// Use the point closest to the origin of the sphere as the intersection
plane_intersect = start - tri_normal * (cotangent_distance + radius);
@@ -272,11 +272,11 @@ bool Triangle3::sphereCast(const Vector3 &start, const Vector3 &dir, float radiu
plane_intersect = start + dir * plane_intersect_distance - tri_normal * radius;
}
if(plane_intersect_distance < 0.0f) {
if (plane_intersect_distance < 0.0f) {
return false;
}
if(containsPoint(plane_intersect)) {
if (containsPoint(plane_intersect)) {
// Triangle contains point
hit_point = plane_intersect;
hit_distance = plane_intersect_distance;
@@ -285,7 +285,7 @@ bool Triangle3::sphereCast(const Vector3 &start, const Vector3 &dir, float radiu
// Triangle does not contain point, cast ray back to sphere from closest point on triangle edge or vertice
Vector3 closest_point = closestPointOnTriangle(plane_intersect);
float reverse_hit_distance;
if(_intersectSphere(closest_point, -dir, start, radius, reverse_hit_distance)) {
if (_intersectSphere(closest_point, -dir, start, radius, reverse_hit_distance)) {
// Reverse cast hit sphere
hit_distance = reverse_hit_distance;
hit_point = closest_point;
@@ -298,7 +298,7 @@ bool Triangle3::sphereCast(const Vector3 &start, const Vector3 &dir, float radiu
}
bool Triangle3::containsPoint(const Vector3 &p) const
bool Triangle3::containsPoint(const Vector3& p) const
{
/*
// From: http://stackoverflow.com/questions/995445/determine-if-a-3d-point-is-within-a-triangle

View File

@@ -34,7 +34,8 @@
namespace kraken {
void Vector2::init() {
void Vector2::init()
{
x = 0.0;
y = 0.0;
}
@@ -46,7 +47,8 @@ Vector2 Vector2::Create()
return r;
}
void Vector2::init(float X, float Y) {
void Vector2::init(float X, float Y)
{
x = X;
y = Y;
}
@@ -54,11 +56,12 @@ void Vector2::init(float X, float Y) {
Vector2 Vector2::Create(float X, float Y)
{
Vector2 r;
r.init(X,Y);
r.init(X, Y);
return r;
}
void Vector2::init(float v) {
void Vector2::init(float v)
{
x = v;
y = v;
}
@@ -70,24 +73,26 @@ Vector2 Vector2::Create(float v)
return r;
}
void Vector2::init(float *v) {
void Vector2::init(float* v)
{
x = v[0];
y = v[1];
}
Vector2 Vector2::Create(float *v)
Vector2 Vector2::Create(float* v)
{
Vector2 r;
r.init(v);
return r;
}
void Vector2::init(const Vector2 &v) {
void Vector2::init(const Vector2& v)
{
x = v.x;
y = v.y;
}
Vector2 Vector2::Create(const Vector2 &v)
Vector2 Vector2::Create(const Vector2& v)
{
Vector2 r;
r.init(v);
@@ -97,64 +102,76 @@ Vector2 Vector2::Create(const Vector2 &v)
// Vector2 swizzle getters
Vector2 Vector2::yx() const
{
return Vector2::Create(y,x);
return Vector2::Create(y, x);
}
// Vector2 swizzle setters
void Vector2::yx(const Vector2 &v)
void Vector2::yx(const Vector2& v)
{
y = v.x;
x = v.y;
}
Vector2 Vector2::Min() {
Vector2 Vector2::Min()
{
return Vector2::Create(-std::numeric_limits<float>::max());
}
Vector2 Vector2::Max() {
Vector2 Vector2::Max()
{
return Vector2::Create(std::numeric_limits<float>::max());
}
Vector2 Vector2::Zero() {
Vector2 Vector2::Zero()
{
return Vector2::Create(0.0f);
}
Vector2 Vector2::One() {
Vector2 Vector2::One()
{
return Vector2::Create(1.0f);
}
Vector2 Vector2::operator +(const Vector2& b) const {
Vector2 Vector2::operator +(const Vector2& b) const
{
return Vector2::Create(x + b.x, y + b.y);
}
Vector2 Vector2::operator -(const Vector2& b) const {
Vector2 Vector2::operator -(const Vector2& b) const
{
return Vector2::Create(x - b.x, y - b.y);
}
Vector2 Vector2::operator +() const {
Vector2 Vector2::operator +() const
{
return *this;
}
Vector2 Vector2::operator -() const {
Vector2 Vector2::operator -() const
{
return Vector2::Create(-x, -y);
}
Vector2 Vector2::operator *(const float v) const {
Vector2 Vector2::operator *(const float v) const
{
return Vector2::Create(x * v, y * v);
}
Vector2 Vector2::operator /(const float v) const {
Vector2 Vector2::operator /(const float v) const
{
float inv_v = 1.0f / v;
return Vector2::Create(x * inv_v, y * inv_v);
}
Vector2& Vector2::operator +=(const Vector2& b) {
Vector2& Vector2::operator +=(const Vector2& b)
{
x += b.x;
y += b.y;
return *this;
}
Vector2& Vector2::operator -=(const Vector2& b) {
Vector2& Vector2::operator -=(const Vector2& b)
{
x -= b.x;
y -= b.y;
return *this;
@@ -162,13 +179,15 @@ Vector2& Vector2::operator -=(const Vector2& b) {
Vector2& Vector2::operator *=(const float v) {
Vector2& Vector2::operator *=(const float v)
{
x *= v;
y *= v;
return *this;
}
Vector2& Vector2::operator /=(const float v) {
Vector2& Vector2::operator /=(const float v)
{
float inv_v = 1.0f / v;
x *= inv_v;
y *= inv_v;
@@ -176,22 +195,24 @@ Vector2& Vector2::operator /=(const float v) {
}
bool Vector2::operator ==(const Vector2& b) const {
bool Vector2::operator ==(const Vector2& b) const
{
return x == b.x && y == b.y;
}
bool Vector2::operator !=(const Vector2& b) const {
bool Vector2::operator !=(const Vector2& b) const
{
return x != b.x || y != b.y;
}
bool Vector2::operator >(const Vector2& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x > b.x) {
if (x > b.x) {
return true;
} else if(x < b.x) {
} else if (x < b.x) {
return false;
} else if(y > b.y) {
} else if (y > b.y) {
return true;
} else {
return false;
@@ -201,19 +222,20 @@ bool Vector2::operator >(const Vector2& b) const
bool Vector2::operator <(const Vector2& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x < b.x) {
if (x < b.x) {
return true;
} else if(x > b.x) {
} else if (x > b.x) {
return false;
} else if(y < b.y) {
} else if (y < b.y) {
return true;
} else {
return false;
}
}
float& Vector2::operator[] (unsigned i) {
switch(i) {
float& Vector2::operator[] (unsigned i)
{
switch (i) {
case 0:
return x;
case 1:
@@ -222,8 +244,9 @@ float& Vector2::operator[] (unsigned i) {
}
}
float Vector2::operator[](unsigned i) const {
switch(i) {
float Vector2::operator[](unsigned i) const
{
switch (i) {
case 0:
return x;
case 1:
@@ -232,39 +255,47 @@ float Vector2::operator[](unsigned i) const {
}
}
void Vector2::normalize() {
void Vector2::normalize()
{
float inv_magnitude = 1.0f / sqrtf(x * x + y * y);
x *= inv_magnitude;
y *= inv_magnitude;
}
float Vector2::sqrMagnitude() const {
float Vector2::sqrMagnitude() const
{
return x * x + y * y;
}
float Vector2::magnitude() const {
float Vector2::magnitude() const
{
return sqrtf(x * x + y * y);
}
Vector2 Vector2::Normalize(const Vector2 &v) {
Vector2 Vector2::Normalize(const Vector2& v)
{
float inv_magnitude = 1.0f / sqrtf(v.x * v.x + v.y * v.y);
return Vector2::Create(v.x * inv_magnitude, v.y * inv_magnitude);
}
float Vector2::Cross(const Vector2 &v1, const Vector2 &v2) {
float Vector2::Cross(const Vector2& v1, const Vector2& v2)
{
return v1.x * v2.y - v1.y * v2.x;
}
float Vector2::Dot(const Vector2 &v1, const Vector2 &v2) {
float Vector2::Dot(const Vector2& v1, const Vector2& v2)
{
return v1.x * v2.x + v1.y * v2.y;
}
Vector2 Vector2::Min(const Vector2 &v1, const Vector2 &v2) {
Vector2 Vector2::Min(const Vector2& v1, const Vector2& v2)
{
return Vector2::Create(KRMIN(v1.x, v2.x), KRMIN(v1.y, v2.y));
}
Vector2 Vector2::Max(const Vector2 &v1, const Vector2 &v2) {
Vector2 Vector2::Max(const Vector2& v1, const Vector2& v2)
{
return Vector2::Create(KRMAX(v1.x, v2.x), KRMAX(v1.y, v2.y));
}

View File

@@ -34,7 +34,8 @@
namespace kraken {
void Vector2i::init() {
void Vector2i::init()
{
x = 0;
y = 0;
}
@@ -46,7 +47,8 @@ Vector2i Vector2i::Create()
return r;
}
void Vector2i::init(int X, int Y) {
void Vector2i::init(int X, int Y)
{
x = X;
y = Y;
}
@@ -54,11 +56,12 @@ void Vector2i::init(int X, int Y) {
Vector2i Vector2i::Create(int X, int Y)
{
Vector2i r;
r.init(X,Y);
r.init(X, Y);
return r;
}
void Vector2i::init(int v) {
void Vector2i::init(int v)
{
x = v;
y = v;
}
@@ -70,24 +73,26 @@ Vector2i Vector2i::Create(int v)
return r;
}
void Vector2i::init(int *v) {
void Vector2i::init(int* v)
{
x = v[0];
y = v[1];
}
Vector2i Vector2i::Create(int *v)
Vector2i Vector2i::Create(int* v)
{
Vector2i r;
r.init(v);
return r;
}
void Vector2i::init(const Vector2i &v) {
void Vector2i::init(const Vector2i& v)
{
x = v.x;
y = v.y;
}
Vector2i Vector2i::Create(const Vector2i &v)
Vector2i Vector2i::Create(const Vector2i& v)
{
Vector2i r;
r.init(v);
@@ -97,96 +102,112 @@ Vector2i Vector2i::Create(const Vector2i &v)
// Vector2 swizzle getters
Vector2i Vector2i::yx() const
{
return Vector2i::Create(y,x);
return Vector2i::Create(y, x);
}
// Vector2 swizzle setters
void Vector2i::yx(const Vector2i &v)
void Vector2i::yx(const Vector2i& v)
{
y = v.x;
x = v.y;
}
Vector2i Vector2i::Min() {
Vector2i Vector2i::Min()
{
return Vector2i::Create(-std::numeric_limits<int>::max());
}
Vector2i Vector2i::Max() {
Vector2i Vector2i::Max()
{
return Vector2i::Create(std::numeric_limits<int>::max());
}
Vector2i Vector2i::Zero() {
Vector2i Vector2i::Zero()
{
return Vector2i::Create(0);
}
Vector2i Vector2i::One() {
Vector2i Vector2i::One()
{
return Vector2i::Create(1);
}
Vector2i Vector2i::operator +(const Vector2i& b) const {
Vector2i Vector2i::operator +(const Vector2i& b) const
{
return Vector2i::Create(x + b.x, y + b.y);
}
Vector2i Vector2i::operator -(const Vector2i& b) const {
Vector2i Vector2i::operator -(const Vector2i& b) const
{
return Vector2i::Create(x - b.x, y - b.y);
}
Vector2i Vector2i::operator +() const {
Vector2i Vector2i::operator +() const
{
return *this;
}
Vector2i Vector2i::operator -() const {
Vector2i Vector2i::operator -() const
{
return Vector2i::Create(-x, -y);
}
Vector2i Vector2i::operator *(const int v) const {
Vector2i Vector2i::operator *(const int v) const
{
return Vector2i::Create(x * v, y * v);
}
Vector2i Vector2i::operator /(const int v) const {
Vector2i Vector2i::operator /(const int v) const
{
return Vector2i::Create(x / v, y / v);
}
Vector2i& Vector2i::operator +=(const Vector2i& b) {
Vector2i& Vector2i::operator +=(const Vector2i& b)
{
x += b.x;
y += b.y;
return *this;
}
Vector2i& Vector2i::operator -=(const Vector2i& b) {
Vector2i& Vector2i::operator -=(const Vector2i& b)
{
x -= b.x;
y -= b.y;
return *this;
}
Vector2i& Vector2i::operator *=(const int v) {
Vector2i& Vector2i::operator *=(const int v)
{
x *= v;
y *= v;
return *this;
}
Vector2i& Vector2i::operator /=(const int v) {
Vector2i& Vector2i::operator /=(const int v)
{
x /= v;
y /= v;
return *this;
}
bool Vector2i::operator ==(const Vector2i& b) const {
bool Vector2i::operator ==(const Vector2i& b) const
{
return x == b.x && y == b.y;
}
bool Vector2i::operator !=(const Vector2i& b) const {
bool Vector2i::operator !=(const Vector2i& b) const
{
return x != b.x || y != b.y;
}
bool Vector2i::operator >(const Vector2i& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x > b.x) {
if (x > b.x) {
return true;
} else if(x < b.x) {
} else if (x < b.x) {
return false;
} else if(y > b.y) {
} else if (y > b.y) {
return true;
} else {
return false;
@@ -196,19 +217,20 @@ bool Vector2i::operator >(const Vector2i& b) const
bool Vector2i::operator <(const Vector2i& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x < b.x) {
if (x < b.x) {
return true;
} else if(x > b.x) {
} else if (x > b.x) {
return false;
} else if(y < b.y) {
} else if (y < b.y) {
return true;
} else {
return false;
}
}
int& Vector2i::operator[] (unsigned i) {
switch(i) {
int& Vector2i::operator[] (unsigned i)
{
switch (i) {
case 0:
return x;
case 1:
@@ -217,8 +239,9 @@ int& Vector2i::operator[] (unsigned i) {
}
}
int Vector2i::operator[](unsigned i) const {
switch(i) {
int Vector2i::operator[](unsigned i) const
{
switch (i) {
case 0:
return x;
case 1:
@@ -227,38 +250,46 @@ int Vector2i::operator[](unsigned i) const {
}
}
void Vector2i::normalize() {
void Vector2i::normalize()
{
int m = magnitude();
x /= m;
y /= m;
}
int Vector2i::sqrMagnitude() const {
int Vector2i::sqrMagnitude() const
{
return x * x + y * y;
}
int Vector2i::magnitude() const {
int Vector2i::magnitude() const
{
return static_cast<int>(sqrtf((float)x * (float)x + (float)y * (float)y));
}
Vector2i Vector2i::Normalize(const Vector2i &v) {
Vector2i Vector2i::Normalize(const Vector2i& v)
{
int m = v.magnitude();
return Vector2i::Create(v.x / m, v.y / m);
}
int Vector2i::Cross(const Vector2i &v1, const Vector2i &v2) {
int Vector2i::Cross(const Vector2i& v1, const Vector2i& v2)
{
return v1.x * v2.y - v1.y * v2.x;
}
int Vector2i::Dot(const Vector2i &v1, const Vector2i &v2) {
int Vector2i::Dot(const Vector2i& v1, const Vector2i& v2)
{
return v1.x * v2.x + v1.y * v2.y;
}
Vector2i Vector2i::Min(const Vector2i &v1, const Vector2i &v2) {
Vector2i Vector2i::Min(const Vector2i& v1, const Vector2i& v2)
{
return Vector2i::Create(KRMIN(v1.x, v2.x), KRMIN(v1.y, v2.y));
}
Vector2i Vector2i::Max(const Vector2i &v1, const Vector2i &v2) {
Vector2i Vector2i::Max(const Vector2i& v1, const Vector2i& v2)
{
return Vector2i::Create(KRMAX(v1.x, v2.x), KRMAX(v1.y, v2.y));
}

View File

@@ -49,39 +49,42 @@ Vector3 Vector3::Create()
return r;
}
void Vector3::init(const Vector3 &v) {
void Vector3::init(const Vector3& v)
{
x = v.x;
y = v.y;
z = v.z;
}
Vector3 Vector3::Create(const Vector3 &v)
Vector3 Vector3::Create(const Vector3& v)
{
Vector3 r;
r.init(v);
return r;
}
void Vector3::init(const Vector4 &v) {
void Vector3::init(const Vector4& v)
{
x = v.x;
y = v.y;
z = v.z;
}
Vector3 Vector3::Create(const Vector4 &v)
Vector3 Vector3::Create(const Vector4& v)
{
Vector3 r;
r.init(v);
return r;
}
void Vector3::init(float *v) {
void Vector3::init(float* v)
{
x = v[0];
y = v[1];
z = v[2];
}
Vector3 Vector3::Create(float *v)
Vector3 Vector3::Create(float* v)
{
Vector3 r;
r.init(v);
@@ -89,13 +92,14 @@ Vector3 Vector3::Create(float *v)
}
void Vector3::init(double *v) {
void Vector3::init(double* v)
{
x = (float)v[0];
y = (float)v[1];
z = (float)v[2];
}
Vector3 Vector3::Create(double *v)
Vector3 Vector3::Create(double* v)
{
Vector3 r;
r.init(v);
@@ -103,7 +107,8 @@ Vector3 Vector3::Create(double *v)
}
void Vector3::init(float v) {
void Vector3::init(float v)
{
x = v;
y = v;
z = v;
@@ -126,166 +131,179 @@ void Vector3::init(float X, float Y, float Z)
Vector3 Vector3::Create(float X, float Y, float Z)
{
Vector3 r;
r.init(X,Y,Z);
r.init(X, Y, Z);
return r;
}
Vector2 Vector3::xx() const
{
return Vector2::Create(x,x);
return Vector2::Create(x, x);
}
Vector2 Vector3::xy() const
{
return Vector2::Create(x,y);
return Vector2::Create(x, y);
}
Vector2 Vector3::xz() const
{
return Vector2::Create(x,z);
return Vector2::Create(x, z);
}
Vector2 Vector3::yx() const
{
return Vector2::Create(y,x);
return Vector2::Create(y, x);
}
Vector2 Vector3::yy() const
{
return Vector2::Create(y,y);
return Vector2::Create(y, y);
}
Vector2 Vector3::yz() const
{
return Vector2::Create(y,z);
return Vector2::Create(y, z);
}
Vector2 Vector3::zx() const
{
return Vector2::Create(z,x);
return Vector2::Create(z, x);
}
Vector2 Vector3::zy() const
{
return Vector2::Create(z,y);
return Vector2::Create(z, y);
}
Vector2 Vector3::zz() const
{
return Vector2::Create(z,z);
return Vector2::Create(z, z);
}
void Vector3::xy(const Vector2 &v)
void Vector3::xy(const Vector2& v)
{
x = v.x;
y = v.y;
}
void Vector3::xz(const Vector2 &v)
void Vector3::xz(const Vector2& v)
{
x = v.x;
z = v.y;
}
void Vector3::yx(const Vector2 &v)
void Vector3::yx(const Vector2& v)
{
y = v.x;
x = v.y;
}
void Vector3::yz(const Vector2 &v)
void Vector3::yz(const Vector2& v)
{
y = v.x;
z = v.y;
}
void Vector3::zx(const Vector2 &v)
void Vector3::zx(const Vector2& v)
{
z = v.x;
x = v.y;
}
void Vector3::zy(const Vector2 &v)
void Vector3::zy(const Vector2& v)
{
z = v.x;
y = v.y;
}
Vector3 Vector3::Min() {
Vector3 Vector3::Min()
{
return Vector3::Create(-std::numeric_limits<float>::max());
}
Vector3 Vector3::Max() {
Vector3 Vector3::Max()
{
return Vector3::Create(std::numeric_limits<float>::max());
}
Vector3 Vector3::Zero() {
Vector3 Vector3::Zero()
{
return Vector3::Create();
}
Vector3 Vector3::One() {
Vector3 Vector3::One()
{
return Vector3::Create(1.0f, 1.0f, 1.0f);
}
Vector3 Vector3::Forward() {
Vector3 Vector3::Forward()
{
return Vector3::Create(0.0f, 0.0f, 1.0f);
}
Vector3 Vector3::Backward() {
Vector3 Vector3::Backward()
{
return Vector3::Create(0.0f, 0.0f, -1.0f);
}
Vector3 Vector3::Up() {
Vector3 Vector3::Up()
{
return Vector3::Create(0.0f, 1.0f, 0.0f);
}
Vector3 Vector3::Down() {
Vector3 Vector3::Down()
{
return Vector3::Create(0.0f, -1.0f, 0.0f);
}
Vector3 Vector3::Left() {
Vector3 Vector3::Left()
{
return Vector3::Create(-1.0f, 0.0f, 0.0f);
}
Vector3 Vector3::Right() {
Vector3 Vector3::Right()
{
return Vector3::Create(1.0f, 0.0f, 0.0f);
}
void Vector3::scale(const Vector3 &v)
void Vector3::scale(const Vector3& v)
{
x *= v.x;
y *= v.y;
z *= v.z;
}
Vector3 Vector3::Scale(const Vector3 &v1, const Vector3 &v2)
Vector3 Vector3::Scale(const Vector3& v1, const Vector3& v2)
{
return Vector3::Create(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
}
Vector3 Vector3::Lerp(const Vector3 &v1, const Vector3 &v2, float d) {
Vector3 Vector3::Lerp(const Vector3& v1, const Vector3& v2, float d)
{
return v1 + (v2 - v1) * d;
}
Vector3 Vector3::Slerp(const Vector3 &v1, const Vector3 &v2, float d) {
Vector3 Vector3::Slerp(const Vector3& v1, const Vector3& v2, float d)
{
// From: http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/
// Dot product - the cosine of the angle between 2 vectors.
float dot = Vector3::Dot(v1, v2);
// Clamp it to be in the range of Acos()
if(dot < -1.0f) dot = -1.0f;
if(dot > 1.0f) dot = 1.0f;
if (dot < -1.0f) dot = -1.0f;
if (dot > 1.0f) dot = 1.0f;
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float theta = acosf(dot)*d;
Vector3 RelativeVec = v2 - v1*dot;
float theta = acosf(dot) * d;
Vector3 RelativeVec = v2 - v1 * dot;
RelativeVec.normalize(); // Orthonormal basis
// The final result.
return ((v1*cosf(theta)) + (RelativeVec*sinf(theta)));
return ((v1 * cosf(theta)) + (RelativeVec * sinf(theta)));
}
void Vector3::OrthoNormalize(Vector3 &normal, Vector3 &tangent) {
void Vector3::OrthoNormalize(Vector3& normal, Vector3& tangent)
{
// Gram-Schmidt Orthonormalization
normal.normalize();
Vector3 proj = normal * Dot(tangent, normal);
@@ -293,36 +311,44 @@ void Vector3::OrthoNormalize(Vector3 &normal, Vector3 &tangent) {
tangent.normalize();
}
Vector3& Vector3::operator =(const Vector4 &b) {
Vector3& Vector3::operator =(const Vector4& b)
{
x = b.x;
y = b.y;
z = b.z;
return *this;
}
Vector3 Vector3::operator +(const Vector3& b) const {
Vector3 Vector3::operator +(const Vector3& b) const
{
return Vector3::Create(x + b.x, y + b.y, z + b.z);
}
Vector3 Vector3::operator -(const Vector3& b) const {
Vector3 Vector3::operator -(const Vector3& b) const
{
return Vector3::Create(x - b.x, y - b.y, z - b.z);
}
Vector3 Vector3::operator +() const {
Vector3 Vector3::operator +() const
{
return *this;
}
Vector3 Vector3::operator -() const {
Vector3 Vector3::operator -() const
{
return Vector3::Create(-x, -y, -z);
}
Vector3 Vector3::operator *(const float v) const {
Vector3 Vector3::operator *(const float v) const
{
return Vector3::Create(x * v, y * v, z * v);
}
Vector3 Vector3::operator /(const float v) const {
Vector3 Vector3::operator /(const float v) const
{
float inv_v = 1.0f / v;
return Vector3::Create(x * inv_v, y * inv_v, z * inv_v);
}
Vector3& Vector3::operator +=(const Vector3& b) {
Vector3& Vector3::operator +=(const Vector3& b)
{
x += b.x;
y += b.y;
z += b.z;
@@ -330,7 +356,8 @@ Vector3& Vector3::operator +=(const Vector3& b) {
return *this;
}
Vector3& Vector3::operator -=(const Vector3& b) {
Vector3& Vector3::operator -=(const Vector3& b)
{
x -= b.x;
y -= b.y;
z -= b.z;
@@ -338,7 +365,8 @@ Vector3& Vector3::operator -=(const Vector3& b) {
return *this;
}
Vector3& Vector3::operator *=(const float v) {
Vector3& Vector3::operator *=(const float v)
{
x *= v;
y *= v;
z *= v;
@@ -346,7 +374,8 @@ Vector3& Vector3::operator *=(const float v) {
return *this;
}
Vector3& Vector3::operator /=(const float v) {
Vector3& Vector3::operator /=(const float v)
{
float inv_v = 1.0f / v;
x *= inv_v;
y *= inv_v;
@@ -355,16 +384,19 @@ Vector3& Vector3::operator /=(const float v) {
return *this;
}
bool Vector3::operator ==(const Vector3& b) const {
bool Vector3::operator ==(const Vector3& b) const
{
return x == b.x && y == b.y && z == b.z;
}
bool Vector3::operator !=(const Vector3& b) const {
bool Vector3::operator !=(const Vector3& b) const
{
return x != b.x || y != b.y || z != b.z;
}
float& Vector3::operator[](unsigned i) {
switch(i) {
float& Vector3::operator[](unsigned i)
{
switch (i) {
case 0:
return x;
case 1:
@@ -375,8 +407,9 @@ float& Vector3::operator[](unsigned i) {
}
}
float Vector3::operator[](unsigned i) const {
switch(i) {
float Vector3::operator[](unsigned i) const
{
switch (i) {
case 0:
return x;
case 1:
@@ -387,56 +420,64 @@ float Vector3::operator[](unsigned i) const {
}
}
float Vector3::sqrMagnitude() const {
float Vector3::sqrMagnitude() const
{
// calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
return x * x + y * y + z * z;
}
float Vector3::magnitude() const {
float Vector3::magnitude() const
{
return sqrtf(x * x + y * y + z * z);
}
void Vector3::normalize() {
void Vector3::normalize()
{
float inv_magnitude = 1.0f / sqrtf(x * x + y * y + z * z);
x *= inv_magnitude;
y *= inv_magnitude;
z *= inv_magnitude;
}
Vector3 Vector3::Normalize(const Vector3 &v) {
Vector3 Vector3::Normalize(const Vector3& v)
{
float inv_magnitude = 1.0f / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
return Vector3::Create(v.x * inv_magnitude, v.y * inv_magnitude, v.z * inv_magnitude);
}
Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) {
Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2)
{
return Vector3::Create(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
}
float Vector3::Dot(const Vector3 &v1, const Vector3 &v2) {
float Vector3::Dot(const Vector3& v1, const Vector3& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
Vector3 Vector3::Min(const Vector3 &v1, const Vector3 &v2) {
Vector3 Vector3::Min(const Vector3& v1, const Vector3& v2)
{
return Vector3::Create(KRMIN(v1.x, v2.x), KRMIN(v1.y, v2.y), KRMIN(v1.z, v2.z));
}
Vector3 Vector3::Max(const Vector3 &v1, const Vector3 &v2) {
Vector3 Vector3::Max(const Vector3& v1, const Vector3& v2)
{
return Vector3::Create(KRMAX(v1.x, v2.x), KRMAX(v1.y, v2.y), KRMAX(v1.z, v2.z));
}
bool Vector3::operator >(const Vector3& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x > b.x) {
if (x > b.x) {
return true;
} else if(x < b.x) {
} else if (x < b.x) {
return false;
} else if(y > b.y) {
} else if (y > b.y) {
return true;
} else if(y < b.y) {
} else if (y < b.y) {
return false;
} else if(z > b.z) {
} else if (z > b.z) {
return true;
} else {
return false;
@@ -446,15 +487,15 @@ bool Vector3::operator >(const Vector3& b) const
bool Vector3::operator <(const Vector3& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x < b.x) {
if (x < b.x) {
return true;
} else if(x > b.x) {
} else if (x > b.x) {
return false;
} else if(y < b.y) {
} else if (y < b.y) {
return true;
} else if(y > b.y) {
} else if (y > b.y) {
return false;
} else if(z < b.z) {
} else if (z < b.z) {
return true;
} else {
return false;

View File

@@ -50,49 +50,53 @@ Vector4 Vector4::Create()
return r;
}
void Vector4::init(const Vector4 &v) {
void Vector4::init(const Vector4& v)
{
x = v.x;
y = v.y;
z = v.z;
w = v.w;
}
Vector4 Vector4::Create(const Vector4 &v)
Vector4 Vector4::Create(const Vector4& v)
{
Vector4 r;
r.init(v);
return r;
}
void Vector4::init(const Vector3 &v, float W) {
void Vector4::init(const Vector3& v, float W)
{
x = v.x;
y = v.y;
z = v.z;
w = W;
}
Vector4 Vector4::Create(const Vector3 &v, float W)
Vector4 Vector4::Create(const Vector3& v, float W)
{
Vector4 r;
r.init(v, W);
return r;
}
void Vector4::init(float *v) {
void Vector4::init(float* v)
{
x = v[0];
y = v[1];
z = v[2];
w = v[3];
}
Vector4 Vector4::Create(float *v)
Vector4 Vector4::Create(float* v)
{
Vector4 r;
r.init(v);
return r;
}
void Vector4::init(float v) {
void Vector4::init(float v)
{
x = v;
y = v;
z = v;
@@ -121,68 +125,81 @@ Vector4 Vector4::Create(float X, float Y, float Z, float W)
return r;
}
Vector4 Vector4::Min() {
Vector4 Vector4::Min()
{
return Vector4::Create(-std::numeric_limits<float>::max());
}
Vector4 Vector4::Max() {
Vector4 Vector4::Max()
{
return Vector4::Create(std::numeric_limits<float>::max());
}
Vector4 Vector4::Zero() {
Vector4 Vector4::Zero()
{
return Vector4::Create();
}
Vector4 Vector4::One() {
Vector4 Vector4::One()
{
return Vector4::Create(1.0f, 1.0f, 1.0f, 1.0f);
}
Vector4 Vector4::Forward() {
Vector4 Vector4::Forward()
{
return Vector4::Create(0.0f, 0.0f, 1.0f, 1.0f);
}
Vector4 Vector4::Backward() {
Vector4 Vector4::Backward()
{
return Vector4::Create(0.0f, 0.0f, -1.0f, 1.0f);
}
Vector4 Vector4::Up() {
Vector4 Vector4::Up()
{
return Vector4::Create(0.0f, 1.0f, 0.0f, 1.0f);
}
Vector4 Vector4::Down() {
Vector4 Vector4::Down()
{
return Vector4::Create(0.0f, -1.0f, 0.0f, 1.0f);
}
Vector4 Vector4::Left() {
Vector4 Vector4::Left()
{
return Vector4::Create(-1.0f, 0.0f, 0.0f, 1.0f);
}
Vector4 Vector4::Right() {
Vector4 Vector4::Right()
{
return Vector4::Create(1.0f, 0.0f, 0.0f, 1.0f);
}
Vector4 Vector4::Lerp(const Vector4 &v1, const Vector4 &v2, float d) {
Vector4 Vector4::Lerp(const Vector4& v1, const Vector4& v2, float d)
{
return v1 + (v2 - v1) * d;
}
Vector4 Vector4::Slerp(const Vector4 &v1, const Vector4 &v2, float d) {
Vector4 Vector4::Slerp(const Vector4& v1, const Vector4& v2, float d)
{
// From: http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/
// Dot product - the cosine of the angle between 2 vectors.
float dot = Vector4::Dot(v1, v2);
// Clamp it to be in the range of Acos()
if(dot < -1.0f) dot = -1.0f;
if(dot > 1.0f) dot = 1.0f;
if (dot < -1.0f) dot = -1.0f;
if (dot > 1.0f) dot = 1.0f;
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float theta = acosf(dot)*d;
Vector4 RelativeVec = v2 - v1*dot;
float theta = acosf(dot) * d;
Vector4 RelativeVec = v2 - v1 * dot;
RelativeVec.normalize(); // Orthonormal basis
// The final result.
return ((v1*cosf(theta)) + (RelativeVec*sinf(theta)));
return ((v1 * cosf(theta)) + (RelativeVec * sinf(theta)));
}
void Vector4::OrthoNormalize(Vector4 &normal, Vector4 &tangent) {
void Vector4::OrthoNormalize(Vector4& normal, Vector4& tangent)
{
// Gram-Schmidt Orthonormalization
normal.normalize();
Vector4 proj = normal * Dot(tangent, normal);
@@ -190,28 +207,35 @@ void Vector4::OrthoNormalize(Vector4 &normal, Vector4 &tangent) {
tangent.normalize();
}
Vector4 Vector4::operator +(const Vector4& b) const {
Vector4 Vector4::operator +(const Vector4& b) const
{
return Vector4::Create(x + b.x, y + b.y, z + b.z, w + b.w);
}
Vector4 Vector4::operator -(const Vector4& b) const {
Vector4 Vector4::operator -(const Vector4& b) const
{
return Vector4::Create(x - b.x, y - b.y, z - b.z, w - b.w);
}
Vector4 Vector4::operator +() const {
Vector4 Vector4::operator +() const
{
return *this;
}
Vector4 Vector4::operator -() const {
Vector4 Vector4::operator -() const
{
return Vector4::Create(-x, -y, -z, -w);
}
Vector4 Vector4::operator *(const float v) const {
Vector4 Vector4::operator *(const float v) const
{
return Vector4::Create(x * v, y * v, z * v, w * v);
}
Vector4 Vector4::operator /(const float v) const {
return Vector4::Create(x / v, y / v, z / v, w/ v);
Vector4 Vector4::operator /(const float v) const
{
return Vector4::Create(x / v, y / v, z / v, w / v);
}
Vector4& Vector4::operator +=(const Vector4& b) {
Vector4& Vector4::operator +=(const Vector4& b)
{
x += b.x;
y += b.y;
z += b.z;
@@ -220,7 +244,8 @@ Vector4& Vector4::operator +=(const Vector4& b) {
return *this;
}
Vector4& Vector4::operator -=(const Vector4& b) {
Vector4& Vector4::operator -=(const Vector4& b)
{
x -= b.x;
y -= b.y;
z -= b.z;
@@ -229,7 +254,8 @@ Vector4& Vector4::operator -=(const Vector4& b) {
return *this;
}
Vector4& Vector4::operator *=(const float v) {
Vector4& Vector4::operator *=(const float v)
{
x *= v;
y *= v;
z *= v;
@@ -238,7 +264,8 @@ Vector4& Vector4::operator *=(const float v) {
return *this;
}
Vector4& Vector4::operator /=(const float v) {
Vector4& Vector4::operator /=(const float v)
{
float inv_v = 1.0f / v;
x *= inv_v;
y *= inv_v;
@@ -248,16 +275,19 @@ Vector4& Vector4::operator /=(const float v) {
return *this;
}
bool Vector4::operator ==(const Vector4& b) const {
bool Vector4::operator ==(const Vector4& b) const
{
return x == b.x && y == b.y && z == b.z && w == b.w;
}
bool Vector4::operator !=(const Vector4& b) const {
bool Vector4::operator !=(const Vector4& b) const
{
return x != b.x || y != b.y || z != b.z || w != b.w;
}
float& Vector4::operator[](unsigned i) {
switch(i) {
float& Vector4::operator[](unsigned i)
{
switch (i) {
case 0:
return x;
case 1:
@@ -270,8 +300,9 @@ float& Vector4::operator[](unsigned i) {
}
}
float Vector4::operator[](unsigned i) const {
switch(i) {
float Vector4::operator[](unsigned i) const
{
switch (i) {
case 0:
return x;
case 1:
@@ -284,23 +315,27 @@ float Vector4::operator[](unsigned i) const {
}
}
float Vector4::sqrMagnitude() const {
float Vector4::sqrMagnitude() const
{
// calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
return x * x + y * y + z * z + w * w;
}
float Vector4::magnitude() const {
float Vector4::magnitude() const
{
return sqrtf(x * x + y * y + z * z + w * w);
}
void Vector4::normalize() {
void Vector4::normalize()
{
float inv_magnitude = 1.0f / sqrtf(x * x + y * y + z * z + w * w);
x *= inv_magnitude;
y *= inv_magnitude;
z *= inv_magnitude;
w *= inv_magnitude;
}
Vector4 Vector4::Normalize(const Vector4 &v) {
Vector4 Vector4::Normalize(const Vector4& v)
{
float inv_magnitude = 1.0f / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
return Vector4::Create(v.x * inv_magnitude,
v.y * inv_magnitude,
@@ -309,35 +344,38 @@ Vector4 Vector4::Normalize(const Vector4 &v) {
}
float Vector4::Dot(const Vector4 &v1, const Vector4 &v2) {
float Vector4::Dot(const Vector4& v1, const Vector4& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
}
Vector4 Vector4::Min(const Vector4 &v1, const Vector4 &v2) {
Vector4 Vector4::Min(const Vector4& v1, const Vector4& v2)
{
return Vector4::Create(KRMIN(v1.x, v2.x), KRMIN(v1.y, v2.y), KRMIN(v1.z, v2.z), KRMIN(v1.w, v2.w));
}
Vector4 Vector4::Max(const Vector4 &v1, const Vector4 &v2) {
Vector4 Vector4::Max(const Vector4& v1, const Vector4& v2)
{
return Vector4::Create(KRMAX(v1.x, v2.x), KRMAX(v1.y, v2.y), KRMAX(v1.z, v2.z), KRMAX(v1.w, v2.w));
}
bool Vector4::operator >(const Vector4& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x != b.x) return x > b.x;
if(y != b.y) return y > b.y;
if(z != b.z) return z > b.z;
if(w != b.w) return w > b.w;
if (x != b.x) return x > b.x;
if (y != b.y) return y > b.y;
if (z != b.z) return z > b.z;
if (w != b.w) return w > b.w;
return false;
}
bool Vector4::operator <(const Vector4& b) const
{
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
if(x != b.x) return x < b.x;
if(y != b.y) return y < b.y;
if(z != b.z) return z < b.z;
if(w != b.w) return w < b.w;
if (x != b.x) return x < b.x;
if (y != b.y) return y < b.y;
if (z != b.z) return z < b.z;
if (w != b.w) return w < b.w;
return false;
}