summaryrefslogtreecommitdiffstats
path: root/src/math
diff options
context:
space:
mode:
authorPiotr Dziwinski <piotrdz@gmail.com>2012-06-13 22:48:35 +0200
committerPiotr Dziwinski <piotrdz@gmail.com>2012-06-13 22:48:35 +0200
commitb735913debff93c1a6444ec731cd4bf99ae2a5c2 (patch)
treeefa532d3b4231e5ffd6df8eddcb0ec8cf5d4b28b /src/math
parentb5d16ef340208bbe1a76f33f7498fb168f6405b6 (diff)
downloadcolobot-b735913debff93c1a6444ec731cd4bf99ae2a5c2.tar.gz
colobot-b735913debff93c1a6444ec731cd4bf99ae2a5c2.tar.bz2
colobot-b735913debff93c1a6444ec731cd4bf99ae2a5c2.zip
FPOINT -> Math::Point & other math functions
- changed FPOINT to Math::Point and some functions from math module to the new implementation - moved old function and FPOINT struct declarations to math3d.cpp - removed some unused functions in math module - fixed some #include dependencies - moved #define STRICT and #define D3D_OVERLOADS to compile options
Diffstat (limited to 'src/math')
-rw-r--r--src/math/conv.h12
-rw-r--r--src/math/func.h6
-rw-r--r--src/math/geometry.h21
-rw-r--r--src/math/old/math3d.cpp158
-rw-r--r--src/math/old/math3d.h99
5 files changed, 157 insertions, 139 deletions
diff --git a/src/math/conv.h b/src/math/conv.h
index 8b77db9..8cfdc6b 100644
--- a/src/math/conv.h
+++ b/src/math/conv.h
@@ -8,9 +8,7 @@
#define D3D_OVERLOADS
#include <d3d.h>
-#include "common/struct.h"
#include "vector.h"
-#include "point.h"
inline D3DVECTOR V_TO_D3D(Math::Vector vec)
{
@@ -21,13 +19,3 @@ inline Math::Vector D3D_TO_V(D3DVECTOR vec)
{
return Math::Vector(vec.x, vec.y, vec.z);
}
-
-inline FPOINT P_TO_FP(Math::Point pt)
-{
- return FPOINT(pt.x, pt.y);
-}
-
-inline Math::Point FP_TO_P(FPOINT pt)
-{
- return Math::Point(pt.x, pt.y);
-}
diff --git a/src/math/func.h b/src/math/func.h
index e5e1321..212f7c1 100644
--- a/src/math/func.h
+++ b/src/math/func.h
@@ -151,7 +151,7 @@ inline bool TestAngle(float angle, float min, float max)
}
//! Calculates a value (radians) proportional between a and b (degrees)
-float PropAngle(int a, int b, float p)
+inline float PropAngle(int a, int b, float p)
{
float aa = (float)a * DEG_TO_RAD;
float bb = (float)b * DEG_TO_RAD;
@@ -185,7 +185,7 @@ inline float Direction(float a, float g)
<---->
dead
out: -1 0 0 1\endverbatim */
-float Neutral(float value, float dead)
+inline float Neutral(float value, float dead)
{
if ( fabs(value) <= dead )
{
@@ -200,7 +200,7 @@ float Neutral(float value, float dead)
//! Gently advances a desired value from its current value
/** Over time, the progression is more rapid. */
-float Smooth(float actual, float hope, float time)
+inline float Smooth(float actual, float hope, float time)
{
float future = actual + (hope-actual)*time;
diff --git a/src/math/geometry.h b/src/math/geometry.h
index 580b9da..72584b0 100644
--- a/src/math/geometry.h
+++ b/src/math/geometry.h
@@ -52,25 +52,6 @@ inline float MidPoint(const Point &a, const Point &b, float px)
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;
-}
-
//! Tests whether the point \a p is inside the triangle (\a a,\a b,\a c)
inline bool IsInsideTriangle(Point a, Point b, Point c, Point p)
{
@@ -198,7 +179,7 @@ inline Vector RotatePoint2(const Vector center, float angleH, float angleV, Vect
}
//! Returns the angle between point (x,y) and (0,0)
-float RotateAngle(float x, float y)
+inline float RotateAngle(float x, float y)
{
if ( (x == 0.0f) && (y == 0.0f) )
return 0.0f;
diff --git a/src/math/old/math3d.cpp b/src/math/old/math3d.cpp
index ad380af..c9eb608 100644
--- a/src/math/old/math3d.cpp
+++ b/src/math/old/math3d.cpp
@@ -30,12 +30,116 @@
#include "math/old/math3d.h"
+// Old defines
+#define MATH3D_PI 3.14159265358979323846f
+#define MATH3D_CHOUIA 1e-6f
+#define MATH3D_BEAUCOUP 1e6f
+
+
+// Old FPOINT struct
+struct FPOINT
+{
+ float x;
+ float y;
+
+ FPOINT() { }
+ FPOINT(float _x, float _y)
+ {
+ x = _x;
+ y = _y;
+ }
+};
+
+
+// === Functions already replaced by new implementation ===
+
+//>>> func.h IsEqual()
+bool IsEqual(float a, float b);
+
+//>>> func.h Min()
+float Min(float a, float b);
+float Min(float a, float b, float c);
+float Min(float a, float b, float c, float d);
+float Min(float a, float b, float c, float d, float e);
+
+//>>> func.h Max()
+float Max(float a, float b);
+float Max(float a, float b, float c);
+float Max(float a, float b, float c, float d);
+float Max(float a, float b, float c, float d, float e);
+
+//>>> func.h Norm()
+float Norm(float a);
+//>>> fabs()
+float Abs(float a);
+
+//>>> func.h Swap()
+void Swap(int &a, int &b);
+//>>> func.h Swap()
+void Swap(float &a, float &b);
+//>>> point.h Swap()
+void Swap(FPOINT &a, FPOINT &b);
+
+//>>> func.h Mod()
+float Mod(float a, float m);
+//>>> func.h NormAngle()
+float NormAngle(float angle);
+//>>> func.h TestAngle()
+bool TestAngle(float angle, float min, float max);
+
+//>>> geometry.h RotateAngle()
+float RotateAngle(FPOINT center, FPOINT p1, FPOINT p2);
+
+//>>> func.h Direction()
+float Direction(float a, float g);
+
+//>>> geometry.h RotatePoint()
+FPOINT RotatePoint(FPOINT center, float angle, FPOINT p);
+//>>> geometry.h RotatePoint()
+FPOINT RotatePoint(float angle, FPOINT p);
+//>>> geometry.h RotatePoint()
+FPOINT RotatePoint(float angle, float dist);
+//>>> geometry.h RotateAngle()
+float RotateAngle(float x, float y);
+//>>> geometry.h RotatePoint()
+void RotatePoint(float cx, float cy, float angle, float &px, float &py);
+
+//>>> geometry.h IsInsideTriangle()
+bool IsInsideTriangle(FPOINT a, FPOINT b, FPOINT c, FPOINT p);
+
+//>>> point.h Distance()
+float Length(FPOINT a, FPOINT b);
+
+//>>> point.h Point::Length()
+float Length(float x, float y);
+
+//>>> func.h Rand()
+float Rand();
+//>>> func.h Neutral()
+float Neutral(float value, float dead);
+
+//>>> func.h PropAngle()
+float Prop(int a, int b, float p);
+//>>> func.h Smooth()
+float Smooth(float actual, float hope, float time);
+//>>> func.h Bounce()
+float Bounce(float progress, float middle=0.3f, float bounce=0.4f);
+
+
+// UNUSED
+float MidPoint(FPOINT a, FPOINT b, float px);
+
+// UNUSED
+bool LineFunction(FPOINT p1, FPOINT p2, float &a, float &b);
+
+
+
// Returns true if two numbers are nearly equal.
bool IsEqual(float a, float b)
{
- return Abs(a-b) < CHOUIA;
+ return Abs(a-b) < MATH3D_CHOUIA;
}
@@ -147,14 +251,14 @@ float Mod(float a, float m)
return a - ((int)(a/m))*m;
}
-// Returns a normalized angle, that is in other words between 0 and 2 * PI.
+// Returns a normalized angle, that is in other words between 0 and 2 * MATH3D_PI.
float NormAngle(float angle)
{
- angle = Mod(angle, PI*2.0f);
+ angle = Mod(angle, MATH3D_PI*2.0f);
if ( angle < 0.0f )
{
- return PI*2.0f + angle;
+ return MATH3D_PI*2.0f + angle;
}
else
{
@@ -191,11 +295,11 @@ float Direction(float a, float g)
if ( a < g )
{
- if ( a+PI*2.0f-g < g-a ) a += PI*2.0f;
+ if ( a+MATH3D_PI*2.0f-g < g-a ) a += MATH3D_PI*2.0f;
}
else
{
- if ( g+PI*2.0f-a < a-g ) g += PI*2.0f;
+ if ( g+MATH3D_PI*2.0f-a < a-g ) g += MATH3D_PI*2.0f;
}
return (g-a);
}
@@ -249,7 +353,7 @@ FPOINT RotatePoint(float angle, float dist)
}
// Calculates the angle of a right triangle.
-// The angle is counterclockwise (CCW), between 0 and 2 * PI.
+// The angle is counterclockwise (CCW), between 0 and 2 * MATH3D_PI.
// For an angle clockwise (CW), just go ahead.
//
// ^
@@ -271,25 +375,25 @@ float RotateAngle(float x, float y)
if ( y >= 0.0f )
{
if ( x > y ) return atanf(y/x);
- else return PI*0.5f - atanf(x/y);
+ else return MATH3D_PI*0.5f - atanf(x/y);
}
else
{
- if ( x > -y ) return PI*2.0f + atanf(y/x);
- else return PI*1.5f - atanf(x/y);
+ if ( x > -y ) return MATH3D_PI*2.0f + atanf(y/x);
+ else return MATH3D_PI*1.5f - atanf(x/y);
}
}
else
{
if ( y >= 0.0f )
{
- if ( -x > y ) return PI*1.0f + atanf(y/x);
- else return PI*0.5f - atanf(x/y);
+ if ( -x > y ) return MATH3D_PI*1.0f + atanf(y/x);
+ else return MATH3D_PI*0.5f - atanf(x/y);
}
else
{
- if ( -x > -y ) return PI*1.0f + atanf(y/x);
- else return PI*1.5f - atanf(x/y);
+ if ( -x > -y ) return MATH3D_PI*1.0f + atanf(y/x);
+ else return MATH3D_PI*1.5f - atanf(x/y);
}
}
#else
@@ -299,11 +403,11 @@ float RotateAngle(float x, float y)
{
if ( y > 0.0f )
{
- return 90.0f*PI/180.0f;
+ return 90.0f*MATH3D_PI/180.0f;
}
else
{
- return 270.0f*PI/180.0f;
+ return 270.0f*MATH3D_PI/180.0f;
}
}
else
@@ -311,7 +415,7 @@ float RotateAngle(float x, float y)
angle = atanf(y/x);
if ( x < 0.0f )
{
- angle += PI;
+ angle += MATH3D_PI;
}
return angle;
}
@@ -335,11 +439,11 @@ float RotateAngle(FPOINT center, FPOINT p1, FPOINT p2)
a1 = asinf((p1.y-center.y)/Length(p1,center));
a2 = asinf((p2.y-center.y)/Length(p2,center));
- if ( p1.x < center.x ) a1 = PI-a1;
- if ( p2.x < center.x ) a2 = PI-a2;
+ if ( p1.x < center.x ) a1 = MATH3D_PI-a1;
+ if ( p2.x < center.x ) a2 = MATH3D_PI-a2;
a = a2-a1;
- if ( a < 0 ) a += PI*2;
+ if ( a < 0 ) a += MATH3D_PI*2;
return a;
}
@@ -347,10 +451,10 @@ float RotateAngle(FPOINT center, FPOINT p1, FPOINT p2)
float MidPoint(FPOINT a, FPOINT b, float px)
{
- if ( Abs(a.x-b.x) < CHOUIA )
+ if ( Abs(a.x-b.x) < MATH3D_CHOUIA )
{
- if ( a.y < b.y ) return BEAUCOUP;
- else return -BEAUCOUP;
+ if ( a.y < b.y ) return MATH3D_BEAUCOUP;
+ else return -MATH3D_BEAUCOUP;
}
return (b.y-a.y)*(px-a.x)/(b.x-a.x)+a.y;
}
@@ -850,8 +954,8 @@ float Prop(int a, int b, float p)
{
float aa, bb;
- aa = (float)a*PI/180.0f;
- bb = (float)b*PI/180.0f;
+ aa = (float)a*MATH3D_PI/180.0f;
+ bb = (float)b*MATH3D_PI/180.0f;
return aa+p*(bb-aa);
}
@@ -896,12 +1000,12 @@ float Bounce(float progress, float middle, float bounce)
if ( progress < middle )
{
progress = progress/middle; // 0..1
- return 0.5f+sinf(progress*PI-PI/2.0f)/2.0f;
+ return 0.5f+sinf(progress*MATH3D_PI-MATH3D_PI/2.0f)/2.0f;
}
else
{
progress = (progress-middle)/(1.0f-middle); // 0..1
- return (1.0f-bounce/2.0f)+sinf((0.5f+progress*2.0f)*PI)*(bounce/2.0f);
+ return (1.0f-bounce/2.0f)+sinf((0.5f+progress*2.0f)*MATH3D_PI)*(bounce/2.0f);
}
}
diff --git a/src/math/old/math3d.h b/src/math/old/math3d.h
index cb17669..6c09f99 100644
--- a/src/math/old/math3d.h
+++ b/src/math/old/math3d.h
@@ -19,147 +19,92 @@
#pragma once
-#define STRICT
-#define D3D_OVERLOADS
#include <math.h>
-#define PI 3.14159265358979323846f
-#define CHOUIA 1e-6f
-#define BEAUCOUP 1e6f
-
-
-
-//>>> func.h IsEqual()
-bool IsEqual(float a, float b);
-
-//>>> func.h Min()
-float Min(float a, float b);
-float Min(float a, float b, float c);
-float Min(float a, float b, float c, float d);
-float Min(float a, float b, float c, float d, float e);
-
-//>>> func.h Max()
-float Max(float a, float b);
-float Max(float a, float b, float c);
-float Max(float a, float b, float c, float d);
-float Max(float a, float b, float c, float d, float e);
-
-//>>> func.h Norm()
-float Norm(float a);
-//>>> fabs()
-float Abs(float a);
-
-//>>> func.h Swap()
-void Swap(int &a, int &b);
-//>>> func.h Swap()
-void Swap(float &a, float &b);
-//>>> point.h Swap() (FPOINT -> Point)
-void Swap(FPOINT &a, FPOINT &b);
-
-//>>> func.h Mod()
-float Mod(float a, float m);
-//>>> func.h NormAngle()
-float NormAngle(float angle);
-//>>> func.h TestAngle()
-bool TestAngle(float angle, float min, float max);
-
-//>>> func.h Direction()
-float Direction(float a, float g);
-//>>> geometry.h RotatePoint()
-FPOINT RotatePoint(FPOINT center, float angle, FPOINT p);
-//>>> geometry.h RotatePoint()
-FPOINT RotatePoint(float angle, FPOINT p);
-//>>> geometry.h RotatePoint()
-FPOINT RotatePoint(float angle, float dist);
-//>>> geometry.h RotateAngle()
-float RotateAngle(float x, float y);
-//>>> geometry.h RotateAngle()
-float RotateAngle(FPOINT center, FPOINT p1, FPOINT p2);
-//>>> geometry.h MidPoint()
-float MidPoint(FPOINT a, FPOINT b, float px);
//>>> geometry.h SegmentPoint()
D3DVECTOR SegmentDist(const D3DVECTOR &p1, const D3DVECTOR &p2, float dist);
-//>>> geometry.h IsInsideTriangle()
-bool IsInsideTriangle(FPOINT a, FPOINT b, FPOINT c, FPOINT p);
+
//>>> geometry.h Intersect()
bool Intersect(D3DVECTOR a, D3DVECTOR b, D3DVECTOR c, D3DVECTOR d, D3DVECTOR e, D3DVECTOR &i);
+
//>>> geometry.h IntersectY()
bool IntersectY(D3DVECTOR a, D3DVECTOR b, D3DVECTOR c, D3DVECTOR &p);
-//>>> geometry.h RotatePoint()
-void RotatePoint(float cx, float cy, float angle, float &px, float &py);
+
//>>> geometry.h RotatePoint()
void RotatePoint(D3DVECTOR center, float angleH, float angleV, D3DVECTOR &p);
+
//>>> geometry.h RotatePoint2()
void RotatePoint2(D3DVECTOR center, float angleH, float angleV, D3DVECTOR &p);
+
//>>> geometry.h RotateView()
// TODO test & verify
D3DVECTOR RotateView(D3DVECTOR center, float angleH, float angleV, float dist);
+
//>>> geometry.h LookatPoint()
// TODO test & verify
D3DVECTOR LookatPoint( D3DVECTOR eye, float angleH, float angleV, float length );
-//>>> point.h Distance()
-float Length(FPOINT a, FPOINT b);
-//>>> point.h Point::Length()
-float Length(float x, float y);
+
//>>> vector.h Vector::Length()
float Length(const D3DVECTOR &u);
+
//>>> vector.h Distance()
float Length(const D3DVECTOR &a, const D3DVECTOR &b);
+
//>>> geometry.h DistanceProjected()
float Length2d(const D3DVECTOR &a, const D3DVECTOR &b);
+
//>>> vector.h Angle()
// TODO test & verify
float Angle( D3DVECTOR u, D3DVECTOR v );
+
//>>> vector.h CrossProduct()
D3DVECTOR Cross( D3DVECTOR u, D3DVECTOR v );
+
//>>> geometry.h NormalToPlane()
D3DVECTOR ComputeNormal( D3DVECTOR p1, D3DVECTOR p2, D3DVECTOR p3 );
+
//>>> geometry.h Transform()
// TODO test & verify
D3DVECTOR Transform(const D3DMATRIX &m, D3DVECTOR p);
+
//>>> geometry.h Projection()
// TODO test & verify
D3DVECTOR Projection(const D3DVECTOR &a, const D3DVECTOR &b, const D3DVECTOR &p);
// TODO
void MappingObject( D3DVERTEX2* pVertices, int nb, float scale );
+
// TODO
void SmoothObject( D3DVERTEX2* pVertices, int nb );
-//>>> geometry.h LinearFunction()
-bool LineFunction(FPOINT p1, FPOINT p2, float &a, float &b);
+
//>>> geometry.h DistanceToPlane()
float DistancePlanPoint(const D3DVECTOR &a, const D3DVECTOR &b, const D3DVECTOR &c, const D3DVECTOR &p);
+
//>>> geometry.h IsSamePlane()
bool IsSamePlane(D3DVECTOR *plan1, D3DVECTOR *plan2);
+
//>>> geometry.h LoadRotationXZYMatrix()
// TODO test & verify
void MatRotateXZY(D3DMATRIX &mat, D3DVECTOR angle);
+
//>>> geometry.h LoadRotationZXYMatrix()
// TODO test & verify
void MatRotateZXY(D3DMATRIX &mat, D3DVECTOR angle);
-//>>> func.h Rand()
-float Rand();
-//>>> func.h Neutral()
-float Neutral(float value, float dead);
-
-//>>> func.h PropAngle()
-float Prop(int a, int b, float p);
-//>>> func.h Smooth()
-float Smooth(float actual, float hope, float time);
-//>>> func.h Bounce()
-float Bounce(float progress, float middle=0.3f, float bounce=0.4f);
// TODO
D3DCOLOR RetColor(float intensity);
+
// TODO
D3DCOLOR RetColor(D3DCOLORVALUE intensity);
+
// TODO
D3DCOLORVALUE RetColor(D3DCOLOR intensity);
// TODO
void RGB2HSV(D3DCOLORVALUE src, ColorHSV &dest);
+
// TODO
void HSV2RGB(ColorHSV src, D3DCOLORVALUE &dest);