From ea442a887d562e95fa91186a4333c830ab5543e7 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 2 May 2012 22:39:43 +0200 Subject: Structs continued --- src/math/const.h | 5 +- src/math/func.h | 25 +++++----- src/math/matrix.h | 27 +++++------ src/math/point.h | 84 +++++++++++++++++++++++++++++---- src/math/test/matrix_test.cpp | 85 ++++++++++++++++++++++++++++++++- src/math/vector.h | 107 +++++++++++++++++++++++++++++++++++------- 6 files changed, 276 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/math/const.h b/src/math/const.h index 79ad564..4f2549e 100644 --- a/src/math/const.h +++ b/src/math/const.h @@ -29,8 +29,11 @@ namespace Math //! Tolerance level -- minimum accepted float value const float TOLERANCE = 1e-6f; + //! Huge number + const float HUGE = 1.0e+38f; + //! PI - const float PI = 3.14159265358979323846f; + const float PI = 3.14159265358979323846f; //! 2 * PI const float PI_MUL_2 = 6.28318530717958623200f; //! PI / 2 diff --git a/src/math/func.h b/src/math/func.h index 8dcae99..ef90800 100644 --- a/src/math/func.h +++ b/src/math/func.h @@ -21,7 +21,6 @@ #pragma once #include "const.h" -#include "point.h" #include #include @@ -34,13 +33,13 @@ namespace Math /* @{ */ // start of group //! Compares \a a and \a b within \a tolerance -inline bool IsEqual(float a, float b, float tolerance = Math::TOLERANCE) +inline bool IsEqual(float a, float b, float tolerance = TOLERANCE) { return fabs(a - b) < tolerance; } //! Compares \a a to zero within \a tolerance -inline bool IsZero(float a, float tolerance = Math::TOLERANCE) +inline bool IsZero(float a, float tolerance = TOLERANCE) { return IsEqual(a, 0.0f, tolerance); } @@ -117,16 +116,6 @@ inline void Swap(float &a, float &b) b = c; } -//! Permutes two points -inline void Swap(Point &a, Point &b) -{ - Point c; - - c = a; - a = b; - b = c; -} - //! Returns the modulo of a floating point number /** Mod(8.1, 4) = 0.1 Mod(n, 1) = fractional part of n */ @@ -177,6 +166,16 @@ inline float Direction(float a, float g) return g-a; } +//! Returns the angle between point (x,y) and (0,0) +float RotateAngle(float x, float y) +{ + float result = std::atan2(x, y); + if (result < 0) + result = PI_MUL_2 + result; + + return result; +} + //! Returns a random value between 0 and 1. inline float Rand() { diff --git a/src/math/matrix.h b/src/math/matrix.h index f02c1cf..96fb00e 100644 --- a/src/math/matrix.h +++ b/src/math/matrix.h @@ -42,7 +42,8 @@ namespace Math The internal representation is a 16-value table in column-major order, thus: - \verbatim m[0 ] m[4 ] m[8 ] m[12] + \verbatim +m[0 ] m[4 ] m[8 ] m[12] m[1 ] m[5 ] m[9 ] m[13] m[2 ] m[6 ] m[10] m[14] m[3 ] m[7 ] m[11] m[15] \endverbatim @@ -110,14 +111,12 @@ struct Matrix //! Transposes the matrix inline void Transpose() { - Matrix temp = *this; - for (int r = 0; r < 4; ++r) - { - for (int c = 0; c < 4; ++c) - { - m[4*r+c] = temp.m[4*c+r]; - } - } + /* (2,1) <-> (1,2) */ Swap(m[1 ], m[4 ]); + /* (3,1) <-> (1,3) */ Swap(m[2 ], m[8 ]); + /* (4,1) <-> (1,4) */ Swap(m[3 ], m[12]); + /* (3,2) <-> (2,3) */ Swap(m[6 ], m[9 ]); + /* (4,2) <-> (2,4) */ Swap(m[7 ], m[13]); + /* (4,3) <-> (3,4) */ Swap(m[11], m[14]); } //! Calculates the determinant of the matrix @@ -369,7 +368,7 @@ struct Matrix Vector view = at - from; float length = view.Length(); - assert(! Math::IsZero(length) ); + assert(! IsZero(length) ); // Normalize the z basis vector view /= length; @@ -506,7 +505,7 @@ struct Matrix { float cos = cosf(angle); float sin = sinf(angle); - Vector v = Math::Normalize(dir); + Vector v = Normalize(dir); LoadIdentity(); @@ -581,7 +580,7 @@ inline Vector MatrixVectorMultiply(const Matrix &m, const Vector &v) float x = v.x * m.m[0 ] + v.y * m.m[4 ] + v.z * m.m[8 ] + m.m[12]; float y = v.x * m.m[1 ] + v.y * m.m[5 ] + v.z * m.m[9 ] + m.m[13]; float z = v.x * m.m[2 ] + v.y * m.m[6 ] + v.z * m.m[10] + m.m[14]; - float w = v.x * m.m[4 ] + v.y * m.m[7 ] + v.z * m.m[11] + m.m[15]; + float w = v.x * m.m[3 ] + v.y * m.m[7 ] + v.z * m.m[11] + m.m[15]; if (IsZero(w)) return Vector(x, y, z); @@ -594,8 +593,8 @@ inline Vector MatrixVectorMultiply(const Matrix &m, const Vector &v) } //! Checks if two matrices are equal within given \a tolerance -inline bool MatricesEqual(const Math::Matrix &m1, const Math::Matrix &m2, - float tolerance = Math::TOLERANCE) +inline bool MatricesEqual(const Matrix &m1, const Matrix &m2, + float tolerance = TOLERANCE) { for (int i = 0; i < 16; ++i) { diff --git a/src/math/point.h b/src/math/point.h index fa411d0..8b9dcba 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -20,6 +20,9 @@ #pragma once +#include "const.h" +#include "func.h" + #include @@ -29,18 +32,9 @@ FPOINT RotatePoint(float angle, FPOINT p); FPOINT RotatePoint(float angle, float dist); void RotatePoint(float cx, float cy, float angle, float &px, float &py); - void RotatePoint(D3DVECTOR center, float angleH, float angleV, D3DVECTOR &p); - void RotatePoint2(D3DVECTOR center, float angleH, float angleV, D3DVECTOR &p); - float RotateAngle(float x, float y); float RotateAngle(FPOINT center, FPOINT p1, FPOINT p2); - float MidPoint(FPOINT a, FPOINT b, float px); - BOOL IsInsideTriangle(FPOINT a, FPOINT b, FPOINT c, FPOINT p); - - BOOL LineFunction(FPOINT p1, FPOINT p2, float &a, float &b); - - float IsInsideTriangle(FPOINT a, FPOINT b, FPOINT c); - + */ // Math module namespace @@ -91,12 +85,82 @@ struct Point } }; +//! Permutes two points +inline void Swap(Point &a, Point &b) +{ + Point c; + + c = a; + a = b; + b = c; +} + //! Returns the distance between two points inline float Distance(const Point &a, const Point &b) { return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } +//! Returns py up on the line \a a - \a b +inline float MidPoint(const Point &a, const Point &b, float px) +{ + if (IsEqual(a.x, b.x)) + { + if (a.y < b.y) + return HUGE; + else + return -HUGE; + } + return (b.y-a.y) * (px-a.x) / (b.x-a.x) + a.y; +} + +//! Calculates the parameters a and b of the linear function passing through \a p1 and \a p2 +/** Returns \c false if the line is vertical. + \param p1,p2 points + \param a,b linear function parameters */ +inline bool LinearFunction(const Point &p1, const Point &p2, float &a, float &b) +{ + if ( IsZero(p1.x-p2.x) ) + { + a = HUGE; + b = p2.x; + return false; + } + + a = (p2.y-p1.y) / (p2.x-p1.x); + b = p2.y - p2.x*a; + + return true; +} + +//! Checks if the point is inside triangle defined by vertices \a a, \a b, \a c +inline bool IsInsideTriangle(Point a, Point b, Point c, const Point &p) +{ + if ( p.x < a.x && p.x < b.x && p.x < c.x ) return false; + if ( p.x > a.x && p.x > b.x && p.x > c.x ) return false; + if ( p.y < a.y && p.y < b.y && p.y < c.y ) return false; + if ( p.y > a.y && p.y > b.y && p.y > c.y ) return false; + + if ( a.x > b.x ) Swap(a,b); + if ( a.x > c.x ) Swap(a,c); + if ( c.x < a.x ) Swap(c,a); + if ( c.x < b.x ) Swap(c,b); + + float n, m; + + n = MidPoint(a, b, p.x); + m = MidPoint(a, c, p.x); + if ( (n>p.y || p.y>m) && (np.y || p.y>m) && (n 0.1f || + std::fabs(n1.y-n2.y) > 0.1f || + std::fabs(n1.z-n2.z) > 0.1f ) + return false; + + float dist = DistanceToPlane(plane1[0], plane1[1], plane1[2], plane2[0]); + if ( dist > 0.1f ) + return false; + + return true; +} + +//! Calculates the projection of the point \a p on a straight line \a a to \a b. +/** \a p point to project + \a a,b two ends of the line */ +inline Vector Projection(const Vector &a, const Vector &b, const Vector &p) +{ + float k = DotProduct(b - a, p - a); + k /= DotProduct(b - a, b - a); + + return a + k*(b-a); +} + +//! Returns a point on the line \a p1 - \a p2, in \a dist distance from \a p1 +/** \a p1,p2 line start and end + \a dist scaling factor from \a p1, relative to distance between \a p1 and \a p2 */ +inline Vector SegmentPoint(const Vector &p1, const Vector &p2, float dist) { - return Math::IsEqual(a.x, b.x, tolerance) - && Math::IsEqual(a.y, b.y, tolerance) - && Math::IsEqual(a.z, b.z, tolerance); + return p1 + (p2 - p1) * dist; } /* @} */ // end of group -- cgit v1.2.3-1-g7c22