turned template class into normal class, too many issues at the moment

This commit is contained in:
ejcoumans
2006-05-25 20:46:30 +00:00
parent 23580cbaed
commit 18ac5c40ce
3 changed files with 244 additions and 238 deletions

View File

@@ -79,15 +79,15 @@ public:
mP3.Set( &vertices[i3*3] ); mP3.Set( &vertices[i3*3] );
} }
Vector3d<float> mP1; Vector3d mP1;
Vector3d<float> mP2; Vector3d mP2;
Vector3d<float> mP3; Vector3d mP3;
Vector3d<float> mNormal; Vector3d mNormal;
}; };
void addTri(VertexLookup vl,UintVector &list,const Vector3d<float> &p1,const Vector3d<float> &p2,const Vector3d<float> &p3) void addTri(VertexLookup vl,UintVector &list,const Vector3d &p1,const Vector3d &p2,const Vector3d &p3)
{ {
unsigned int i1 = Vl_getIndex(vl, p1.Ptr() ); unsigned int i1 = Vl_getIndex(vl, p1.Ptr() );
unsigned int i2 = Vl_getIndex(vl, p2.Ptr() ); unsigned int i2 = Vl_getIndex(vl, p2.Ptr() );
@@ -232,7 +232,7 @@ void doConvexDecomposition(unsigned int vcount,
for (float z=-1; z<1; z+=0.04f) for (float z=-1; z<1; z+=0.04f)
{ {
float d = x*plane[0] + y*plane[1] + z*plane[2] + plane[3]; float d = x*plane[0] + y*plane[1] + z*plane[2] + plane[3];
Vector3d<float> p(x,y,z); Vector3d p(x,y,z);
if ( d >= 0 ) if ( d >= 0 )
callback->ConvexDebugPoint(p.Ptr(), 0.02f, 0x00FF00); callback->ConvexDebugPoint(p.Ptr(), 0.02f, 0x00FF00);
else else
@@ -254,19 +254,19 @@ void doConvexDecomposition(unsigned int vcount,
FaceTri t(vertices, i1, i2, i3 ); FaceTri t(vertices, i1, i2, i3 );
Vector3d<float> front[4]; Vector3d front[4];
Vector3d<float> back[4]; Vector3d back[4];
unsigned int fcount=0; unsigned int fcount=0;
unsigned int bcount=0; unsigned int bcount=0;
PlaneTriResult result; PlaneTriResult result;
result = planeTriIntersection(plane,t.mP1.Ptr(),sizeof(Vector3d<float>),0.00001f,front[0].Ptr(),fcount,back[0].Ptr(),bcount ); result = planeTriIntersection(plane,t.mP1.Ptr(),sizeof(Vector3d),0.00001f,front[0].Ptr(),fcount,back[0].Ptr(),bcount );
if( fcount > 4 || bcount > 4 ) if( fcount > 4 || bcount > 4 )
{ {
result = planeTriIntersection(plane,t.mP1.Ptr(),sizeof(Vector3d<float>),0.00001f,front[0].Ptr(),fcount,back[0].Ptr(),bcount ); result = planeTriIntersection(plane,t.mP1.Ptr(),sizeof(Vector3d),0.00001f,front[0].Ptr(),fcount,back[0].Ptr(),bcount );
} }
switch ( result ) switch ( result )

View File

@@ -50,7 +50,7 @@ namespace ConvexDecomposition
const float DEG_TO_RAD = ((2.0f * 3.14152654f) / 360.0f); const float DEG_TO_RAD = ((2.0f * 3.14152654f) / 360.0f);
const float RAD_TO_DEG = (360.0f / (2.0f * 3.141592654f)); const float RAD_TO_DEG = (360.0f / (2.0f * 3.141592654f));
template <class Type> class Vector3d class Vector3d
{ {
public: public:
Vector3d(void) { }; // null constructor, does not inialize point. Vector3d(void) { }; // null constructor, does not inialize point.
@@ -62,7 +62,7 @@ public:
z = a.z; z = a.z;
}; };
Vector3d(Type a,Type b,Type c) // construct with initial point. Vector3d(float a,float b,float c) // construct with initial point.
{ {
x = a; x = a;
y = b; y = b;
@@ -83,12 +83,12 @@ public:
z = t[2]; z = t[2];
}; };
bool operator==(const Vector3d<Type> &a) const bool operator==(const Vector3d &a) const
{ {
return( a.x == x && a.y == y && a.z == z ); return( a.x == x && a.y == y && a.z == z );
}; };
bool operator!=(const Vector3d<Type> &a) const bool operator!=(const Vector3d &a) const
{ {
return( a.x != x || a.y != y || a.z != z ); return( a.x != x || a.y != y || a.z != z );
}; };
@@ -125,7 +125,7 @@ public:
return(Scaled); return(Scaled);
}; };
void operator /= (Type A) // ACCUMULATED VECTOR ADDITION (/=) void operator /= (float A) // ACCUMULATED VECTOR ADDITION (/=)
{ x/=A; y/=A; z/=A; }; { x/=A; y/=A; z/=A; };
void operator += (const Vector3d A) // ACCUMULATED VECTOR ADDITION (+=) void operator += (const Vector3d A) // ACCUMULATED VECTOR ADDITION (+=)
@@ -143,26 +143,26 @@ public:
{ Vector3d Negated(-x, -y, -z); { Vector3d Negated(-x, -y, -z);
return(Negated); }; return(Negated); };
Type operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY. float operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
{ return( (i==0)?x:((i==1)?y:z) ); }; { return( (i==0)?x:((i==1)?y:z) ); };
Type & operator [] (const int i) float & operator [] (const int i)
{ return( (i==0)?x:((i==1)?y:z) ); }; { return( (i==0)?x:((i==1)?y:z) ); };
// //
// accessor methods. // accessor methods.
Type GetX(void) const { return x; }; float GetX(void) const { return x; };
Type GetY(void) const { return y; }; float GetY(void) const { return y; };
Type GetZ(void) const { return z; }; float GetZ(void) const { return z; };
Type X(void) const { return x; }; float X(void) const { return x; };
Type Y(void) const { return y; }; float Y(void) const { return y; };
Type Z(void) const { return z; }; float Z(void) const { return z; };
void SetX(Type t) { x = t; }; void SetX(float t) { x = t; };
void SetY(Type t) { y = t; }; void SetY(float t) { y = t; };
void SetZ(Type t) { z = t; }; void SetZ(float t) { z = t; };
bool IsSame(const Vector3d<float> &v,float epsilon) const bool IsSame(const Vector3d &v,float epsilon) const
{ {
float dx = fabsf( x - v.x ); float dx = fabsf( x - v.x );
if ( dx > epsilon ) return false; if ( dx > epsilon ) return false;
@@ -174,9 +174,9 @@ public:
} }
float ComputeNormal(const Vector3d<float> &A, float ComputeNormal(const Vector3d &A,
const Vector3d<float> &B, const Vector3d &B,
const Vector3d<float> &C) const Vector3d &C)
{ {
float vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag; float vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag;
@@ -211,7 +211,7 @@ public:
} }
void ScaleSumScale(float c0,float c1,const Vector3d<float> &pos) void ScaleSumScale(float c0,float c1,const Vector3d &pos)
{ {
x = (x*c0) + (pos.x*c1); x = (x*c0) + (pos.x*c1);
y = (y*c0) + (pos.y*c1); y = (y*c0) + (pos.y*c1);
@@ -225,7 +225,7 @@ public:
z = t; z = t;
}; };
void Get(Type *v) const void Get(float *v) const
{ {
v[0] = x; v[0] = x;
v[1] = y; v[1] = y;
@@ -234,20 +234,20 @@ public:
void Set(const int *p) void Set(const int *p)
{ {
x = (Type) p[0]; x = (float) p[0];
y = (Type) p[1]; y = (float) p[1];
z = (Type) p[2]; z = (float) p[2];
} }
void Set(const float *p) void Set(const float *p)
{ {
x = (Type) p[0]; x = (float) p[0];
y = (Type) p[1]; y = (float) p[1];
z = (Type) p[2]; z = (float) p[2];
} }
void Set(Type a,Type b,Type c) void Set(float a,float b,float c)
{ {
x = a; x = a;
y = b; y = b;
@@ -259,8 +259,8 @@ public:
x = y = z = 0; x = y = z = 0;
}; };
const Type* Ptr() const { return &x; } const float* Ptr() const { return &x; }
Type* Ptr() { return &x; } float* Ptr() { return &x; }
// return -(*this). // return -(*this).
@@ -273,22 +273,22 @@ public:
return result; return result;
} }
Type Magnitude(void) const float Magnitude(void) const
{ {
return Type(sqrt(x * x + y * y + z * z)); return float(sqrt(x * x + y * y + z * z));
}; };
Type FastMagnitude(void) const float FastMagnitude(void) const
{ {
return Type(fast_sqrt(x * x + y * y + z * z)); return float(sqrtf(x * x + y * y + z * z));
}; };
Type FasterMagnitude(void) const float FasterMagnitude(void) const
{ {
return Type(faster_sqrt(x * x + y * y + z * z)); return float(sqrtf(x * x + y * y + z * z));
}; };
void Lerp(const Vector3d<Type>& from,const Vector3d<Type>& to,float slerp) void Lerp(const Vector3d& from,const Vector3d& to,float slerp)
{ {
x = ((to.x - from.x) * slerp) + from.x; x = ((to.x - from.x) * slerp) + from.x;
y = ((to.y - from.y) * slerp) + from.y; y = ((to.y - from.y) * slerp) + from.y;
@@ -300,7 +300,7 @@ public:
// Reason for existance is so that when a bullet collides with a wall, for // Reason for existance is so that when a bullet collides with a wall, for
// example, you can generate a graphic effect slightly *before* it hit the // example, you can generate a graphic effect slightly *before* it hit the
// wall so that the effect doesn't sort into the wall itself. // wall so that the effect doesn't sort into the wall itself.
void Interpolate(const Vector3d<float> &from,const Vector3d<float> &to,float offset) void Interpolate(const Vector3d &from,const Vector3d &to,float offset)
{ {
x = to.x-from.x; x = to.x-from.x;
y = to.y-from.y; y = to.y-from.y;
@@ -316,7 +316,7 @@ public:
z = z*d + from.z; z = z*d + from.z;
}; };
bool BinaryEqual(const Vector3d<float> &p) const bool BinaryEqual(const Vector3d &p) const
{ {
const int *source = (const int *) &x; const int *source = (const int *) &x;
const int *dest = (const int *) &p.x; const int *dest = (const int *) &p.x;
@@ -328,18 +328,20 @@ public:
return false; return false;
}; };
bool BinaryEqual(const Vector3d<int> &p) const /*bool BinaryEqual(const Vector3d<int> &p) const
{ {
if ( x == p.x && y == p.y && z == p.z ) return true; if ( x == p.x && y == p.y && z == p.z ) return true;
return false; return false;
} }
*/
/** Computes the reflection vector between two vectors.*/ /** Computes the reflection vector between two vectors.*/
void Reflection(const Vector3d<Type> &a,const Vector3d<Type> &b)// compute reflection vector. void Reflection(const Vector3d &a,const Vector3d &b)// compute reflection vector.
{ {
Vector3d<float> c; Vector3d c;
Vector3d<float> d; Vector3d d;
float dot = a.Dot(b) * 2.0f; float dot = a.Dot(b) * 2.0f;
@@ -352,22 +354,22 @@ public:
z = -d.z; z = -d.z;
}; };
void AngleAxis(Type angle,const Vector3d<Type>& axis) void AngleAxis(float angle,const Vector3d& axis)
{ {
x = axis.x*angle; x = axis.x*angle;
y = axis.y*angle; y = axis.y*angle;
z = axis.z*angle; z = axis.z*angle;
}; };
Type Length(void) const // length of vector. float Length(void) const // length of vector.
{ {
return Type(sqrt( x*x + y*y + z*z )); return float(sqrt( x*x + y*y + z*z ));
}; };
float ComputePlane(const Vector3d<float> &A, float ComputePlane(const Vector3d &A,
const Vector3d<float> &B, const Vector3d &B,
const Vector3d<float> &C) const Vector3d &C)
{ {
float vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag; float vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag;
@@ -405,43 +407,43 @@ public:
} }
Type FastLength(void) const // length of vector. float FastLength(void) const // length of vector.
{ {
return Type(fast_sqrt( x*x + y*y + z*z )); return float(sqrtf( x*x + y*y + z*z ));
}; };
Type FasterLength(void) const // length of vector. float FasterLength(void) const // length of vector.
{ {
return Type(faster_sqrt( x*x + y*y + z*z )); return float(sqrtf( x*x + y*y + z*z ));
}; };
Type Length2(void) const // squared distance, prior to square root. float Length2(void) const // squared distance, prior to square root.
{ {
Type l2 = x*x+y*y+z*z; float l2 = x*x+y*y+z*z;
return l2; return l2;
}; };
Type Distance(const Vector3d<Type> &a) const // distance between two points. float Distance(const Vector3d &a) const // distance between two points.
{ {
Vector3d<Type> d(a.x-x,a.y-y,a.z-z); Vector3d d(a.x-x,a.y-y,a.z-z);
return d.Length(); return d.Length();
} }
Type FastDistance(const Vector3d<Type> &a) const // distance between two points. float FastDistance(const Vector3d &a) const // distance between two points.
{ {
Vector3d<Type> d(a.x-x,a.y-y,a.z-z); Vector3d d(a.x-x,a.y-y,a.z-z);
return d.FastLength(); return d.FastLength();
} }
Type FasterDistance(const Vector3d<Type> &a) const // distance between two points. float FasterDistance(const Vector3d &a) const // distance between two points.
{ {
Vector3d<Type> d(a.x-x,a.y-y,a.z-z); Vector3d d(a.x-x,a.y-y,a.z-z);
return d.FasterLength(); return d.FasterLength();
} }
Type DistanceXY(const Vector3d<Type> &a) const float DistanceXY(const Vector3d &a) const
{ {
float dx = a.x - x; float dx = a.x - x;
float dy = a.y - y; float dy = a.y - y;
@@ -449,7 +451,7 @@ public:
return dist; return dist;
} }
Type Distance2(const Vector3d<Type> &a) const // squared distance. float Distance2(const Vector3d &a) const // squared distance.
{ {
float dx = a.x - x; float dx = a.x - x;
float dy = a.y - y; float dy = a.y - y;
@@ -457,14 +459,14 @@ public:
return dx*dx + dy*dy + dz*dz; return dx*dx + dy*dy + dz*dz;
}; };
Type Partial(const Vector3d<Type> &p) const float Partial(const Vector3d &p) const
{ {
return (x*p.y) - (p.x*y); return (x*p.y) - (p.x*y);
} }
Type Area(const Vector3d<Type> &p1,const Vector3d<Type> &p2) const float Area(const Vector3d &p1,const Vector3d &p2) const
{ {
Type A = Partial(p1); float A = Partial(p1);
A+= p1.Partial(p2); A+= p1.Partial(p2);
A+= p2.Partial(*this); A+= p2.Partial(*this);
return A*0.5f; return A*0.5f;
@@ -489,7 +491,7 @@ public:
inline float FastNormalize(void) // normalize to a unit vector, returns distance. inline float FastNormalize(void) // normalize to a unit vector, returns distance.
{ {
float d = fast_sqrt( static_cast< float >( x*x + y*y + z*z ) ); float d = sqrt( static_cast< float >( x*x + y*y + z*z ) );
if ( d > 0 ) if ( d > 0 )
{ {
float r = 1.0f / d; float r = 1.0f / d;
@@ -506,7 +508,7 @@ public:
inline float FasterNormalize(void) // normalize to a unit vector, returns distance. inline float FasterNormalize(void) // normalize to a unit vector, returns distance.
{ {
float d = faster_sqrt( static_cast< float >( x*x + y*y + z*z ) ); float d = sqrtf( static_cast< float >( x*x + y*y + z*z ) );
if ( d > 0 ) if ( d > 0 )
{ {
float r = 1.0f / d; float r = 1.0f / d;
@@ -524,20 +526,20 @@ public:
Type Dot(const Vector3d<Type> &a) const // computes dot product. float Dot(const Vector3d &a) const // computes dot product.
{ {
return (x * a.x + y * a.y + z * a.z ); return (x * a.x + y * a.y + z * a.z );
}; };
Vector3d<Type> Cross( const Vector3d<Type>& other ) const Vector3d Cross( const Vector3d& other ) const
{ {
Vector3d<Type> result( y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x ); Vector3d result( y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x );
return result; return result;
} }
void Cross(const Vector3d<Type> &a,const Vector3d<Type> &b) // cross two vectors result in this one. void Cross(const Vector3d &a,const Vector3d &b) // cross two vectors result in this one.
{ {
x = a.y*b.z - a.z*b.y; x = a.y*b.z - a.z*b.y;
y = a.z*b.x - a.x*b.z; y = a.z*b.x - a.x*b.z;
@@ -550,7 +552,7 @@ public:
// Edge from a to b is already in face // Edge from a to b is already in face
// Edge from b to c is being considered for addition to face // Edge from b to c is being considered for addition to face
/******************************************/ /******************************************/
bool Concave(const Vector3d<float>& a,const Vector3d<float>& b) bool Concave(const Vector3d& a,const Vector3d& b)
{ {
float vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag,nx,ny,nz,mag_a,mag_b; float vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag,nx,ny,nz,mag_a,mag_b;
@@ -600,7 +602,7 @@ public:
return(true); return(true);
}; };
bool PointTestXY(const Vector3d<float> &i,const Vector3d<float> &j) const bool PointTestXY(const Vector3d &i,const Vector3d &j) const
{ {
if (((( i.y <= y ) && ( y < j.y )) || if (((( i.y <= y ) && ( y < j.y )) ||
(( j.y <= y ) && ( y < i.y ))) && (( j.y <= y ) && ( y < i.y ))) &&
@@ -610,9 +612,9 @@ public:
// test to see if this point is inside the triangle specified by // test to see if this point is inside the triangle specified by
// these three points on the X/Y plane. // these three points on the X/Y plane.
bool PointInTriXY(const Vector3d<float> &p1, bool PointInTriXY(const Vector3d &p1,
const Vector3d<float> &p2, const Vector3d &p2,
const Vector3d<float> &p3) const const Vector3d &p3) const
{ {
float ax = p3.x - p2.x; float ax = p3.x - p2.x;
float ay = p3.y - p2.y; float ay = p3.y - p2.y;
@@ -636,9 +638,9 @@ public:
// test to see if this point is inside the triangle specified by // test to see if this point is inside the triangle specified by
// these three points on the X/Y plane. // these three points on the X/Y plane.
bool PointInTriYZ(const Vector3d<float> &p1, bool PointInTriYZ(const Vector3d &p1,
const Vector3d<float> &p2, const Vector3d &p2,
const Vector3d<float> &p3) const const Vector3d &p3) const
{ {
float ay = p3.y - p2.y; float ay = p3.y - p2.y;
float az = p3.z - p2.z; float az = p3.z - p2.z;
@@ -663,9 +665,9 @@ public:
// test to see if this point is inside the triangle specified by // test to see if this point is inside the triangle specified by
// these three points on the X/Y plane. // these three points on the X/Y plane.
bool PointInTriXZ(const Vector3d<float> &p1, bool PointInTriXZ(const Vector3d &p1,
const Vector3d<float> &p2, const Vector3d &p2,
const Vector3d<float> &p3) const const Vector3d &p3) const
{ {
float az = p3.z - p2.z; float az = p3.z - p2.z;
float ax = p3.x - p2.x; float ax = p3.x - p2.x;
@@ -689,36 +691,36 @@ public:
// Given a point and a line (defined by two points), compute the closest point // Given a point and a line (defined by two points), compute the closest point
// in the line. (The line is treated as infinitely long.) // in the line. (The line is treated as infinitely long.)
void NearestPointInLine(const Vector3d<Type> &point, void NearestPointInLine(const Vector3d &point,
const Vector3d<Type> &line0, const Vector3d &line0,
const Vector3d<Type> &line1) const Vector3d &line1)
{ {
Vector3d<Type> &nearestPoint = *this; Vector3d &nearestPoint = *this;
Vector3d<Type> lineDelta = line1 - line0; Vector3d lineDelta = line1 - line0;
// Handle degenerate lines // Handle degenerate lines
if ( lineDelta == Vector3d<Type>(0, 0, 0) ) if ( lineDelta == Vector3d(0, 0, 0) )
{ {
nearestPoint = line0; nearestPoint = line0;
} }
else else
{ {
float delta = (point-line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta); float delta = (point-line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta);
nearestPoint = line0 + delta*lineDelta; nearestPoint = line0 + lineDelta*delta;
} }
} }
// Given a point and a line segment (defined by two points), compute the closest point // Given a point and a line segment (defined by two points), compute the closest point
// in the line. Cap the point at the endpoints of the line segment. // in the line. Cap the point at the endpoints of the line segment.
void NearestPointInLineSegment(const Vector3d<Type> &point, void NearestPointInLineSegment(const Vector3d &point,
const Vector3d<Type> &line0, const Vector3d &line0,
const Vector3d<Type> &line1) const Vector3d &line1)
{ {
Vector3d<Type> &nearestPoint = *this; Vector3d &nearestPoint = *this;
Vector3d<Type> lineDelta = line1 - line0; Vector3d lineDelta = line1 - line0;
// Handle degenerate lines // Handle degenerate lines
if ( lineDelta == Vector3d<float>(0, 0, 0) ) if ( lineDelta == Vector3d(0, 0, 0) )
{ {
nearestPoint = line0; nearestPoint = line0;
} }
@@ -732,56 +734,56 @@ public:
else if ( delta > 1 ) else if ( delta > 1 )
delta = 1; delta = 1;
nearestPoint = line0 + delta*lineDelta; nearestPoint = line0 + lineDelta*delta;
} }
} }
// Given a point and a plane (defined by three points), compute the closest point // Given a point and a plane (defined by three points), compute the closest point
// in the plane. (The plane is unbounded.) // in the plane. (The plane is unbounded.)
void NearestPointInPlane(const Vector3d<Type> &point, void NearestPointInPlane(const Vector3d &point,
const Vector3d<Type> &triangle0, const Vector3d &triangle0,
const Vector3d<Type> &triangle1, const Vector3d &triangle1,
const Vector3d<Type> &triangle2) const Vector3d &triangle2)
{ {
Vector3d<Type> &nearestPoint = *this; Vector3d &nearestPoint = *this;
Vector3d<Type> lineDelta0 = triangle1 - triangle0; Vector3d lineDelta0 = triangle1 - triangle0;
Vector3d<Type> lineDelta1 = triangle2 - triangle0; Vector3d lineDelta1 = triangle2 - triangle0;
Vector3d<Type> pointDelta = point - triangle0; Vector3d pointDelta = point - triangle0;
Vector3d<Type> normal; Vector3d normal;
// Get the normal of the polygon (doesn't have to be a unit vector) // Get the normal of the polygon (doesn't have to be a unit vector)
normal.Cross(lineDelta0, lineDelta1); normal.Cross(lineDelta0, lineDelta1);
float delta = normal.Dot(pointDelta) / normal.Dot(normal); float delta = normal.Dot(pointDelta) / normal.Dot(normal);
nearestPoint = point - delta*normal; nearestPoint = point - normal*delta;
} }
// Given a point and a plane (defined by a coplanar point and a normal), compute the closest point // Given a point and a plane (defined by a coplanar point and a normal), compute the closest point
// in the plane. (The plane is unbounded.) // in the plane. (The plane is unbounded.)
void NearestPointInPlane(const Vector3d<Type> &point, void NearestPointInPlane(const Vector3d &point,
const Vector3d<Type> &planePoint, const Vector3d &planePoint,
const Vector3d<Type> &planeNormal) const Vector3d &planeNormal)
{ {
Vector3d<Type> &nearestPoint = *this; Vector3d &nearestPoint = *this;
Vector3d<Type> pointDelta = point - planePoint; Vector3d pointDelta = point - planePoint;
float delta = planeNormal.Dot(pointDelta) / planeNormal.Dot(planeNormal); float delta = planeNormal.Dot(pointDelta) / planeNormal.Dot(planeNormal);
nearestPoint = point - delta*planeNormal; nearestPoint = point - planeNormal*delta;
} }
// Given a point and a triangle (defined by three points), compute the closest point // Given a point and a triangle (defined by three points), compute the closest point
// in the triangle. Clamp the point so it's confined to the area of the triangle. // in the triangle. Clamp the point so it's confined to the area of the triangle.
void NearestPointInTriangle(const Vector3d<Type> &point, void NearestPointInTriangle(const Vector3d &point,
const Vector3d<Type> &triangle0, const Vector3d &triangle0,
const Vector3d<Type> &triangle1, const Vector3d &triangle1,
const Vector3d<Type> &triangle2) const Vector3d &triangle2)
{ {
static const Vector3d<Type> zeroVector(0, 0, 0); static const Vector3d zeroVector(0, 0, 0);
Vector3d<Type> &nearestPoint = *this; Vector3d &nearestPoint = *this;
Vector3d<Type> lineDelta0 = triangle1 - triangle0; Vector3d lineDelta0 = triangle1 - triangle0;
Vector3d<Type> lineDelta1 = triangle2 - triangle0; Vector3d lineDelta1 = triangle2 - triangle0;
// Handle degenerate triangles // Handle degenerate triangles
if ( (lineDelta0 == zeroVector) || (lineDelta1 == zeroVector) ) if ( (lineDelta0 == zeroVector) || (lineDelta1 == zeroVector) )
@@ -795,20 +797,20 @@ public:
else else
{ {
static Vector3d<Type> axis[3]; static Vector3d axis[3];
axis[0].NearestPointInLine(triangle0, triangle1, triangle2); axis[0].NearestPointInLine(triangle0, triangle1, triangle2);
axis[1].NearestPointInLine(triangle1, triangle0, triangle2); axis[1].NearestPointInLine(triangle1, triangle0, triangle2);
axis[2].NearestPointInLine(triangle2, triangle0, triangle1); axis[2].NearestPointInLine(triangle2, triangle0, triangle1);
Type axisDot[3]; float axisDot[3];
axisDot[0] = (triangle0-axis[0]).Dot(point-axis[0]); axisDot[0] = (triangle0-axis[0]).Dot(point-axis[0]);
axisDot[1] = (triangle1-axis[1]).Dot(point-axis[1]); axisDot[1] = (triangle1-axis[1]).Dot(point-axis[1]);
axisDot[2] = (triangle2-axis[2]).Dot(point-axis[2]); axisDot[2] = (triangle2-axis[2]).Dot(point-axis[2]);
bool bForce = true; bool bForce = true;
Type bestMagnitude2 = 0; float bestMagnitude2 = 0;
Type closeMagnitude2; float closeMagnitude2;
Vector3d<float> closePoint; Vector3d closePoint;
if ( axisDot[0] < 0 ) if ( axisDot[0] < 0 )
{ {
@@ -848,15 +850,15 @@ public:
// inside the triangle; use the nearest-point-on-a-plane equation // inside the triangle; use the nearest-point-on-a-plane equation
if ( bForce ) if ( bForce )
{ {
Vector3d<Type> normal; Vector3d normal;
// Get the normal of the polygon (doesn't have to be a unit vector) // Get the normal of the polygon (doesn't have to be a unit vector)
normal.Cross(lineDelta0, lineDelta1); normal.Cross(lineDelta0, lineDelta1);
Vector3d<float> pointDelta = point - triangle0; Vector3d pointDelta = point - triangle0;
float delta = normal.Dot(pointDelta) / normal.Dot(normal); float delta = normal.Dot(pointDelta) / normal.Dot(normal);
nearestPoint = point - delta*normal; nearestPoint = point - normal*delta;
} }
} }
} }
@@ -864,13 +866,13 @@ public:
//private: //private:
Type x; float x;
Type y; float y;
Type z; float z;
}; };
template <class Type> class Vector2d class Vector2d
{ {
public: public:
Vector2d(void) { }; // null constructor, does not inialize point. Vector2d(void) { }; // null constructor, does not inialize point.
@@ -888,14 +890,14 @@ public:
}; };
Vector2d(Type a,Type b) // construct with initial point. Vector2d(float a,float b) // construct with initial point.
{ {
x = a; x = a;
y = b; y = b;
}; };
const Type* Ptr() const { return &x; } const float* Ptr() const { return &x; }
Type* Ptr() { return &x; } float* Ptr() { return &x; }
Vector2d & operator+=(const Vector2d &a) // += operator. Vector2d & operator+=(const Vector2d &a) // += operator.
{ {
@@ -925,7 +927,7 @@ public:
return *this; return *this;
}; };
bool operator==(const Vector2d<Type> &a) const bool operator==(const Vector2d &a) const
{ {
if ( a.x == x && a.y == y ) return true; if ( a.x == x && a.y == y ) return true;
return false; return false;
@@ -963,9 +965,9 @@ public:
return a; return a;
}; };
Vector2d operator*(Type c) const Vector2d operator*(float c) const
{ {
Vector2d<Type> a; Vector2d a;
a.x = x * c; a.x = x * c;
a.y = y * c; a.y = y * c;
@@ -981,18 +983,18 @@ public:
}; };
Type Dot(const Vector2d<Type> &a) const // computes dot product. float Dot(const Vector2d &a) const // computes dot product.
{ {
return (x * a.x + y * a.y ); return (x * a.x + y * a.y );
}; };
Type GetX(void) const { return x; }; float GetX(void) const { return x; };
Type GetY(void) const { return y; }; float GetY(void) const { return y; };
void SetX(Type t) { x = t; }; void SetX(float t) { x = t; };
void SetY(Type t) { y = t; }; void SetY(float t) { y = t; };
void Set(Type a,Type b) void Set(float a,float b)
{ {
x = a; x = a;
y = b; y = b;
@@ -1000,7 +1002,7 @@ public:
void Zero(void) void Zero(void)
{ {
x = y = z = 0; x = y = 0;
}; };
Vector2d negative(void) const Vector2d negative(void) const
@@ -1011,93 +1013,93 @@ public:
return result; return result;
} }
Type magnitude(void) const float magnitude(void) const
{ {
return (Type) sqrtf(x * x + y * y ); return (float) sqrtf(x * x + y * y );
} }
Type fastmagnitude(void) const float fastmagnitude(void) const
{ {
return (Type) fast_sqrt(x * x + y * y ); return (float) sqrtf(x * x + y * y );
} }
Type fastermagnitude(void) const float fastermagnitude(void) const
{ {
return (Type) faster_sqrt( x * x + y * y ); return (float) sqrtf( x * x + y * y );
} }
void Reflection(Vector2d &a,Vector2d &b); // compute reflection vector. void Reflection(Vector2d &a,Vector2d &b); // compute reflection vector.
Type Length(void) const // length of vector. float Length(void) const // length of vector.
{ {
return Type(sqrtf( x*x + y*y )); return float(sqrtf( x*x + y*y ));
}; };
Type FastLength(void) const // length of vector. float FastLength(void) const // length of vector.
{ {
return Type(fast_sqrt( x*x + y*y )); return float(sqrtf( x*x + y*y ));
}; };
Type FasterLength(void) const // length of vector. float FasterLength(void) const // length of vector.
{ {
return Type(faster_sqrt( x*x + y*y )); return float(sqrtf( x*x + y*y ));
}; };
Type Length2(void) // squared distance, prior to square root. float Length2(void) // squared distance, prior to square root.
{ {
return x*x+y*y; return x*x+y*y;
} }
Type Distance(const Vector2d &a) const // distance between two points. float Distance(const Vector2d &a) const // distance between two points.
{ {
Type dx = a.x - x; float dx = a.x - x;
Type dy = a.y - y; float dy = a.y - y;
Type d = dx*dx+dy*dy; float d = dx*dx+dy*dy;
return sqrtf(d); return sqrtf(d);
}; };
Type FastDistance(const Vector2d &a) const // distance between two points. float FastDistance(const Vector2d &a) const // distance between two points.
{ {
Type dx = a.x - x; float dx = a.x - x;
Type dy = a.y - y; float dy = a.y - y;
Type d = dx*dx+dy*dy; float d = dx*dx+dy*dy;
return fast_sqrt(d); return sqrtf(d);
}; };
Type FasterDistance(const Vector2d &a) const // distance between two points. float FasterDistance(const Vector2d &a) const // distance between two points.
{ {
Type dx = a.x - x; float dx = a.x - x;
Type dy = a.y - y; float dy = a.y - y;
Type d = dx*dx+dy*dy; float d = dx*dx+dy*dy;
return faster_sqrt(d); return sqrtf(d);
}; };
Type Distance2(Vector2d &a) // squared distance. float Distance2(Vector2d &a) // squared distance.
{ {
Type dx = a.x - x; float dx = a.x - x;
Type dy = a.y - y; float dy = a.y - y;
return dx*dx + dy *dy; return dx*dx + dy *dy;
}; };
void Lerp(const Vector2d<Type>& from,const Vector2d<Type>& to,float slerp) void Lerp(const Vector2d& from,const Vector2d& to,float slerp)
{ {
x = ((to.x - from.x)*slerp) + from.x; x = ((to.x - from.x)*slerp) + from.x;
y = ((to.y - from.y)*slerp) + from.y; y = ((to.y - from.y)*slerp) + from.y;
}; };
void Cross(const Vector2d<Type> &a,const Vector2d<Type> &b) // cross two vectors result in this one. void Cross(const Vector2d &a,const Vector2d &b) // cross two vectors result in this one.
{ {
x = a.y*b.x - a.x*b.y; x = a.y*b.x - a.x*b.y;
y = a.x*b.x - a.x*b.x; y = a.x*b.x - a.x*b.x;
}; };
Type Normalize(void) // normalize to a unit vector, returns distance. float Normalize(void) // normalize to a unit vector, returns distance.
{ {
Type l = Length(); float l = Length();
if ( l != 0 ) if ( l != 0 )
{ {
l = Type( 1 ) / l; l = float( 1 ) / l;
x*=l; x*=l;
y*=l; y*=l;
} }
@@ -1108,12 +1110,12 @@ public:
return l; return l;
}; };
Type FastNormalize(void) // normalize to a unit vector, returns distance. float FastNormalize(void) // normalize to a unit vector, returns distance.
{ {
Type l = FastLength(); float l = FastLength();
if ( l != 0 ) if ( l != 0 )
{ {
l = Type( 1 ) / l; l = float( 1 ) / l;
x*=l; x*=l;
y*=l; y*=l;
} }
@@ -1124,12 +1126,12 @@ public:
return l; return l;
}; };
Type FasterNormalize(void) // normalize to a unit vector, returns distance. float FasterNormalize(void) // normalize to a unit vector, returns distance.
{ {
Type l = FasterLength(); float l = FasterLength();
if ( l != 0 ) if ( l != 0 )
{ {
l = Type( 1 ) / l; l = float( 1 ) / l;
x*=l; x*=l;
y*=l; y*=l;
} }
@@ -1141,38 +1143,42 @@ public:
}; };
Type x; float x;
Type y; float y;
}; };
class Line class Line
{ {
public: public:
Line(const Vector3d<float> &from,const Vector3d<float> &to) Line(const Vector3d &from,const Vector3d &to)
{ {
mP1 = from; mP1 = from;
mP2 = to; mP2 = to;
}; };
// JWR Test for the intersection of two lines. // JWR Test for the intersection of two lines.
bool Intersect(const Line& src,Vector3d<float> &sect); bool Intersect(const Line& src,Vector3d &sect);
private: private:
Vector3d<float> mP1; Vector3d mP1;
Vector3d<float> mP2; Vector3d mP2;
}; };
typedef std::vector< Vector3d<float> > Vector3dVector; typedef std::vector< Vector3d > Vector3dVector;
typedef std::vector< Vector2d<float> > Vector2dVector; typedef std::vector< Vector2d > Vector2dVector;
template <class Type> Vector3d<Type> operator * (Type s, const Vector3d<Type> &v ) inline Vector3d operator * (float s, const Vector3d &v )
{ Vector3d <Type> Scaled(v.x*s, v.y*s, v.z*s); {
return(Scaled); }; Vector3d Scaled(v.x*s, v.y*s, v.z*s);
return(Scaled);
};
template <class Type> Vector2d<Type> operator * (Type s, const Vector2d<Type> &v ) inline Vector2d operator * (float s, const Vector2d &v )
{ Vector2d <Type> Scaled(v.x*s, v.y*s); {
return(Scaled); }; Vector2d Scaled(v.x*s, v.y*s);
return(Scaled);
};
}; };

View File

@@ -86,13 +86,13 @@ unsigned int getDebugColor(void)
class Wpoint class Wpoint
{ {
public: public:
Wpoint(const Vector3d<float> &p,float w) Wpoint(const Vector3d &p,float w)
{ {
mPoint = p; mPoint = p;
mWeight = w; mWeight = w;
} }
Vector3d<float> mPoint; Vector3d mPoint;
float mWeight; float mWeight;
}; };
@@ -159,9 +159,9 @@ public:
} }
// clip this line segment against this triangle. // clip this line segment against this triangle.
bool clip(const Vector3d<float> &start,Vector3d<float> &end) const bool clip(const Vector3d &start,Vector3d &end) const
{ {
Vector3d<float> sect; Vector3d sect;
bool hit = lineIntersectsTriangle(start.Ptr(), end.Ptr(), mP1.Ptr(), mP2.Ptr(), mP3.Ptr(), sect.Ptr() ); bool hit = lineIntersectsTriangle(start.Ptr(), end.Ptr(), mP1.Ptr(), mP2.Ptr(), mP3.Ptr(), sect.Ptr() );
@@ -172,7 +172,7 @@ public:
return hit; return hit;
} }
bool Concave(const Vector3d<float> &p,float &distance,Vector3d<float> &n) const bool Concave(const Vector3d &p,float &distance,Vector3d &n) const
{ {
n.NearestPointInTriangle(p,mP1,mP2,mP3); n.NearestPointInTriangle(p,mP1,mP2,mP3);
distance = p.Distance(n); distance = p.Distance(n);
@@ -213,9 +213,9 @@ public:
unsigned int color = getDebugColor(); unsigned int color = getDebugColor();
#if 0 #if 0
Vector3d<float> d1 = mNear1; Vector3d d1 = mNear1;
Vector3d<float> d2 = mNear2; Vector3d d2 = mNear2;
Vector3d<float> d3 = mNear3; Vector3d d3 = mNear3;
callback->ConvexDebugPoint(mP1.Ptr(),0.01f,0x00FF00); callback->ConvexDebugPoint(mP1.Ptr(),0.01f,0x00FF00);
callback->ConvexDebugPoint(mP2.Ptr(),0.01f,0x00FF00); callback->ConvexDebugPoint(mP2.Ptr(),0.01f,0x00FF00);
@@ -251,7 +251,7 @@ public:
} }
float raySect(const Vector3d<float> &p,const Vector3d<float> &dir,Vector3d<float> &sect) const float raySect(const Vector3d &p,const Vector3d &dir,Vector3d &sect) const
{ {
float plane[4]; float plane[4];
@@ -260,7 +260,7 @@ public:
plane[2] = mNormal.z; plane[2] = mNormal.z;
plane[3] = mPlaneD; plane[3] = mPlaneD;
Vector3d<float> dest = p+dir*100000; Vector3d dest = p+dir*100000;
intersect( p.Ptr(), dest.Ptr(), sect.Ptr(), plane ); intersect( p.Ptr(), dest.Ptr(), sect.Ptr(), plane );
@@ -268,7 +268,7 @@ public:
} }
float planeDistance(const Vector3d<float> &p) const float planeDistance(const Vector3d &p) const
{ {
float plane[4]; float plane[4];
@@ -339,9 +339,9 @@ public:
Wpoint p2(mP2,mC2); Wpoint p2(mP2,mC2);
Wpoint p3(mP3,mC3); Wpoint p3(mP3,mC3);
Vector3d<float> d1 = mNear1 - mP1; Vector3d d1 = mNear1 - mP1;
Vector3d<float> d2 = mNear2 - mP2; Vector3d d2 = mNear2 - mP2;
Vector3d<float> d3 = mNear3 - mP3; Vector3d d3 = mNear3 - mP3;
d1*=WSCALE; d1*=WSCALE;
d2*=WSCALE; d2*=WSCALE;
@@ -375,9 +375,9 @@ public:
callback->ConvexDebugTri(mP2.Ptr(), d2.Ptr(), d2.Ptr(),0x00FF00); callback->ConvexDebugTri(mP2.Ptr(), d2.Ptr(), d2.Ptr(),0x00FF00);
callback->ConvexDebugTri(mP3.Ptr(), d3.Ptr(), d3.Ptr(),0x00FF00); callback->ConvexDebugTri(mP3.Ptr(), d3.Ptr(), d3.Ptr(),0x00FF00);
Vector3d<float> np1 = mP1 + mNormal*0.05f; Vector3d np1 = mP1 + mNormal*0.05f;
Vector3d<float> np2 = mP2 + mNormal*0.05f; Vector3d np2 = mP2 + mNormal*0.05f;
Vector3d<float> np3 = mP3 + mNormal*0.05f; Vector3d np3 = mP3 + mNormal*0.05f;
callback->ConvexDebugTri(mP1.Ptr(), np1.Ptr(), np1.Ptr(), 0xFF00FF ); callback->ConvexDebugTri(mP1.Ptr(), np1.Ptr(), np1.Ptr(), 0xFF00FF );
callback->ConvexDebugTri(mP2.Ptr(), np2.Ptr(), np2.Ptr(), 0xFF00FF ); callback->ConvexDebugTri(mP2.Ptr(), np2.Ptr(), np2.Ptr(), 0xFF00FF );
@@ -393,13 +393,13 @@ public:
} }
Vector3d<float> mP1; Vector3d mP1;
Vector3d<float> mP2; Vector3d mP2;
Vector3d<float> mP3; Vector3d mP3;
Vector3d<float> mNear1; Vector3d mNear1;
Vector3d<float> mNear2; Vector3d mNear2;
Vector3d<float> mNear3; Vector3d mNear3;
Vector3d<float> mNormal; Vector3d mNormal;
float mPlaneD; float mPlaneD;
float mConcavity; float mConcavity;
float mC1; float mC1;
@@ -459,7 +459,7 @@ bool featureMatch(CTri &m,const CTriVector &tris,ConvexDecompInterface *callback
neardot = dot; neardot = dot;
Vector3d<float> n1,n2,n3; Vector3d n1,n2,n3;
t.raySect( m.mP1, m.mNormal, m.mNear1 ); t.raySect( m.mP1, m.mNormal, m.mNear1 );
t.raySect( m.mP2, m.mNormal, m.mNear2 ); t.raySect( m.mP2, m.mNormal, m.mNear2 );
@@ -608,7 +608,7 @@ float computeConcavity(unsigned int vcount,
float dy = bmax[1] - bmin[1]; float dy = bmax[1] - bmin[1];
float dz = bmax[2] - bmin[2]; float dz = bmax[2] - bmin[2];
Vector3d<float> center; Vector3d center;
center.x = bmin[0] + dx*0.5f; center.x = bmin[0] + dx*0.5f;
center.y = bmin[1] + dy*0.5f; center.y = bmin[1] + dy*0.5f;