93 # pragma warning( push )
94 # pragma warning(disable:4786)
101 inline float fast_sqrt(
float f ) {
return sqrtf(f); }
102 inline float faster_sqrt(
float f ) {
return sqrtf(f); }
104 inline double fast_sqrt(
double f ) {
return sqrt(f); }
105 inline double faster_sqrt(
double f ) {
return sqrt(f); }
107 namespace ConvexDecomposition
111 const double DEG_TO_RAD = ((2.0f * 3.14152654f) / 360.0f);
112 const double RAD_TO_DEG = (360.0f / (2.0f * 3.141592654f));
114 template <
class Type>
class Vector3d
123 Vector3d(
const Vector3d &a)
137 Vector3d(
const double *t)
144 Vector3d(
const int *t)
151 bool operator==(
const Vector3d<Type> &a)
const
153 return( a.x == x && a.y == y && a.z == z );
156 bool operator!=(
const Vector3d<Type> &a)
const
158 return( a.x != x || a.y != y || a.z != z );
162 Vector3d& operator = (
const Vector3d& A)
163 { x=A.x; y=A.y; z=A.z;
167 { Vector3d Sum(x+A.x, y+A.y, z+A.z);
171 { Vector3d Diff(x-A.x, y-A.y, z-A.z);
175 { Vector3d Scaled(x*s, y*s, z*s);
180 { Vector3d Scaled(x+s, y+s, z+s);
186 Vector3d operator / (
const double s)
const
189 Vector3d Scaled(x*r, y*r, z*r);
193 void operator /= (
Type A)
194 { x/=A; y/=A; z/=A; }
197 { x+=A.x; y+=A.y; z+=A.z; }
198 void operator -= (
const Vector3d A)
199 { x-=A.x; y-=A.y; z-=A.z; }
200 void operator *= (
const double s)
204 { x+=A; y+=A; z+=A; }
208 { Vector3d Negated(-x, -y, -z);
211 Type operator [] (
const int i)
const
212 {
return( (i==0)?x:((i==1)?y:z) ); }
213 Type & operator [] (
const int i)
214 {
return( (i==0)?x:((i==1)?y:z) ); }
218 Type GetX(
void)
const {
return x; }
219 Type GetY(
void)
const {
return y; }
220 Type GetZ(
void)
const {
return z; }
222 Type X(
void)
const {
return x; }
223 Type Y(
void)
const {
return y; }
224 Type Z(
void)
const {
return z; }
226 void SetX(
Type t) { x = t; }
227 void SetY(
Type t) { y = t; }
228 void SetZ(
Type t) { z = t; }
230 bool IsSame(
const Vector3d<double> &v,
double epsilon)
const
232 double dx = fabsf( x - v.x );
233 if ( dx > epsilon )
return false;
234 double dy = fabsf( y - v.y );
235 if ( dy > epsilon )
return false;
236 double dz = fabsf( z - v.z );
237 if ( dz > epsilon )
return false;
242 double ComputeNormal(
const Vector3d<double> &A,
243 const Vector3d<double> &B,
244 const Vector3d<double> &C)
246 double vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag;
256 vw_x = vy * wz - vz * wy;
257 vw_y = vz * wx - vx * wz;
258 vw_z = vx * wy - vy * wx;
260 mag = std::sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
262 if ( mag < 0.000001f )
279 void ScaleSumScale(
double c0,
double c1,
const Vector3d<double> &pos)
281 x = (x*c0) + (pos.x*c1);
282 y = (y*c0) + (pos.y*c1);
283 z = (z*c0) + (pos.z*c1);
293 void Get(
Type *v)
const
300 void Set(
const int *p)
307 void Set(
const double *p)
327 const Type* Ptr()
const {
return &x; }
328 Type* Ptr() {
return &x; }
332 Vector3d negative(
void)
const
341 Type Magnitude(
void)
const
343 return Type(sqrt(x * x + y * y + z * z));
346 Type FastMagnitude(
void)
const
348 return Type(fast_sqrt(x * x + y * y + z * z));
351 Type FasterMagnitude(
void)
const
353 return Type(faster_sqrt(x * x + y * y + z * z));
356 void Lerp(
const Vector3d<Type>& from,
const Vector3d<Type>& to,
double slerp)
358 x = ((to.x - from.x) * slerp) + from.x;
359 y = ((to.y - from.y) * slerp) + from.y;
360 z = ((to.z - from.z) * slerp) + from.z;
368 void Interpolate(
const Vector3d<double> &from,
const Vector3d<double> &to,
double offset)
373 double d = std::sqrt( x*x + y*y + z*z );
374 double recip = 1.0f / d;
384 bool BinaryEqual(
const Vector3d<double> &p)
const
386 const int *source = (
const int *) &x;
387 const int *dest = (
const int *) &p.x;
389 if ( source[0] == dest[0] &&
390 source[1] == dest[1] &&
391 source[2] == dest[2] )
return true;
396 bool BinaryEqual(
const Vector3d<int> &p)
const
398 if ( x == p.x && y == p.y && z == p.z )
return true;
404 void Reflection(
const Vector3d<Type> &a,
const Vector3d<Type> &b)
409 double dot = a.Dot(b) * 2.0f;
420 void AngleAxis(
Type angle,
const Vector3d<Type>& axis)
427 Type Length(
void)
const
429 return Type(sqrt( x*x + y*y + z*z ));
433 double ComputePlane(
const Vector3d<double> &A,
434 const Vector3d<double> &B,
435 const Vector3d<double> &C)
437 double vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag;
447 vw_x = vy * wz - vz * wy;
448 vw_y = vz * wx - vx * wz;
449 vw_z = vx * wy - vy * wx;
451 mag = sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
453 if ( mag < 0.000001f )
467 double D = 0.0f - ((x*A.x)+(y*A.y)+(z*A.z));
473 Type FastLength(
void)
const
475 return Type(fast_sqrt( x*x + y*y + z*z ));
479 Type FasterLength(
void)
const
481 return Type(faster_sqrt( x*x + y*y + z*z ));
484 Type Length2(
void)
const
486 Type l2 = x*x+y*y+z*z;
490 Type Distance(
const Vector3d<Type> &a)
const
492 Vector3d<Type> d(a.x-x,a.y-y,a.z-z);
496 Type FastDistance(
const Vector3d<Type> &a)
const
498 Vector3d<Type> d(a.x-x,a.y-y,a.z-z);
499 return d.FastLength();
502 Type FasterDistance(
const Vector3d<Type> &a)
const
504 Vector3d<Type> d(a.x-x,a.y-y,a.z-z);
505 return d.FasterLength();
509 Type DistanceXY(
const Vector3d<Type> &a)
const
513 double dist = dx*dx + dy*dy;
517 Type Distance2(
const Vector3d<Type> &a)
const
522 return dx*dx + dy*dy + dz*dz;
525 Type Partial(
const Vector3d<Type> &p)
const
527 return (x*p.y) - (p.x*y);
530 Type Area(
const Vector3d<Type> &p1,
const Vector3d<Type> &p2)
const
532 Type A = Partial(p1);
534 A+= p2.Partial(*
this);
538 inline double Normalize(
void)
540 double d = std::sqrt(
static_cast< double >( x*x + y*y + z*z ) );
555 inline double FastNormalize(
void)
557 double d = fast_sqrt(
static_cast< double >( x*x + y*y + z*z ) );
572 inline double FasterNormalize(
void)
574 double d = faster_sqrt(
static_cast< double >( x*x + y*y + z*z ) );
592 Type Dot(
const Vector3d<Type> &a)
const
594 return (x * a.x + y * a.y + z * a.z );
598 Vector3d<Type> Cross(
const Vector3d<Type>& other )
const
600 Vector3d<Type> result( y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x );
605 void Cross(
const Vector3d<Type> &a,
const Vector3d<Type> &b)
607 x = a.y*b.z - a.z*b.y;
608 y = a.z*b.x - a.x*b.z;
609 z = a.x*b.y - a.y*b.x;
618 bool Concave(
const Vector3d<double>& a,
const Vector3d<double>& b)
620 double vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag,nx,ny,nz,mag_a,mag_b;
626 mag_a = (double) std::sqrt((wx * wx) + (wy * wy) + (wz * wz));
632 mag_b = (double) std::sqrt((vx * vx) + (vy * vy) + (vz * vz));
634 vw_x = (vy * wz) - (vz * wy);
635 vw_y = (vz * wx) - (vx * wz);
636 vw_z = (vx * wy) - (vy * wx);
638 mag = (double) std::sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
648 if (mag/(mag_a*mag_b) <= 0.0f )
return true;
661 mag = ( x * nx) + ( y * ny) + ( z * nz);
663 if (mag > 0.0f )
return false;
668 bool PointTestXY(
const Vector3d<double> &i,
const Vector3d<double> &j)
const
670 if (((( i.y <= y ) && ( y < j.y )) ||
671 (( j.y <= y ) && ( y < i.y ))) &&
672 ( x < (j.x - i.x) * (y - i.y) / (j.y - i.y) + i.x))
return true;
678 bool PointInTriXY(
const Vector3d<double> &p1,
679 const Vector3d<double> &p2,
680 const Vector3d<double> &p3)
const
682 double ax = p3.x - p2.x;
683 double ay = p3.y - p2.y;
684 double bx = p1.x - p3.x;
685 double by = p1.y - p3.y;
686 double cx = p2.x - p1.x;
687 double cy = p2.y - p1.y;
688 double apx = x - p1.x;
689 double apy = y - p1.y;
690 double bpx = x - p2.x;
691 double bpy = y - p2.y;
692 double cpx = x - p3.x;
693 double cpy = y - p3.y;
695 double aCROSSbp = ax*bpy - ay*bpx;
696 double cCROSSap = cx*apy - cy*apx;
697 double bCROSScp = bx*cpy - by*cpx;
699 return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
704 bool PointInTriYZ(
const Vector3d<double> &p1,
705 const Vector3d<double> &p2,
706 const Vector3d<double> &p3)
const
708 double ay = p3.y - p2.y;
709 double az = p3.z - p2.z;
710 double by = p1.y - p3.y;
711 double bz = p1.z - p3.z;
712 double cy = p2.y - p1.y;
713 double cz = p2.z - p1.z;
714 double apy = y - p1.y;
715 double apz = z - p1.z;
716 double bpy = y - p2.y;
717 double bpz = z - p2.z;
718 double cpy = y - p3.y;
719 double cpz = z - p3.z;
721 double aCROSSbp = ay*bpz - az*bpy;
722 double cCROSSap = cy*apz - cz*apy;
723 double bCROSScp = by*cpz - bz*cpy;
725 return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
731 bool PointInTriXZ(
const Vector3d<double> &p1,
732 const Vector3d<double> &p2,
733 const Vector3d<double> &p3)
const
735 double az = p3.z - p2.z;
736 double ax = p3.x - p2.x;
737 double bz = p1.z - p3.z;
738 double bx = p1.x - p3.x;
739 double cz = p2.z - p1.z;
740 double cx = p2.x - p1.x;
741 double apz = z - p1.z;
742 double apx = x - p1.x;
743 double bpz = z - p2.z;
744 double bpx = x - p2.x;
745 double cpz = z - p3.z;
746 double cpx = x - p3.x;
748 double aCROSSbp = az*bpx - ax*bpz;
749 double cCROSSap = cz*apx - cx*apz;
750 double bCROSScp = bz*cpx - bx*cpz;
752 return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
757 void NearestPointInLine(
const Vector3d<Type> &point,
758 const Vector3d<Type> &line0,
759 const Vector3d<Type> &line1)
761 Vector3d<Type> &nearestPoint = *
this;
762 Vector3d<Type> lineDelta = line1 - line0;
765 if ( lineDelta == Vector3d<Type>(0, 0, 0) )
767 nearestPoint = line0;
771 double delta = (point-line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta);
772 nearestPoint = line0 + delta*lineDelta;
778 void NearestPointInLineSegment(
const Vector3d<Type> &point,
779 const Vector3d<Type> &line0,
780 const Vector3d<Type> &line1)
782 Vector3d<Type> &nearestPoint = *
this;
783 Vector3d<Type> lineDelta = line1 - line0;
786 if ( lineDelta == Vector3d<double>(0, 0, 0) )
788 nearestPoint = line0;
792 double delta = (point-line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta);
797 else if ( delta > 1 )
800 nearestPoint = line0 + delta*lineDelta;
806 void NearestPointInPlane(
const Vector3d<Type> &point,
807 const Vector3d<Type> &triangle0,
808 const Vector3d<Type> &triangle1,
809 const Vector3d<Type> &triangle2)
811 Vector3d<Type> &nearestPoint = *
this;
812 Vector3d<Type> lineDelta0 = triangle1 - triangle0;
813 Vector3d<Type> lineDelta1 = triangle2 - triangle0;
814 Vector3d<Type> pointDelta = point - triangle0;
815 Vector3d<Type> normal;
818 normal.Cross(lineDelta0, lineDelta1);
820 double delta = normal.Dot(pointDelta) / normal.Dot(normal);
821 nearestPoint = point - delta*normal;
826 void NearestPointInPlane(
const Vector3d<Type> &point,
827 const Vector3d<Type> &planePoint,
828 const Vector3d<Type> &planeNormal)
830 Vector3d<Type> &nearestPoint = *
this;
831 Vector3d<Type> pointDelta = point - planePoint;
833 double delta = planeNormal.Dot(pointDelta) / planeNormal.Dot(planeNormal);
834 nearestPoint = point - delta*planeNormal;
839 void NearestPointInTriangle(
const Vector3d<Type> &point,
840 const Vector3d<Type> &triangle0,
841 const Vector3d<Type> &triangle1,
842 const Vector3d<Type> &triangle2)
844 static const Vector3d<Type> zeroVector(0, 0, 0);
846 Vector3d<Type> &nearestPoint = *
this;
848 Vector3d<Type> lineDelta0 = triangle1 - triangle0;
849 Vector3d<Type> lineDelta1 = triangle2 - triangle0;
852 if ( (lineDelta0 == zeroVector) || (lineDelta1 == zeroVector) )
854 nearestPoint.NearestPointInLineSegment(point, triangle1, triangle2);
856 else if ( lineDelta0 == lineDelta1 )
858 nearestPoint.NearestPointInLineSegment(point, triangle0, triangle1);
863 static Vector3d<Type> axis[3];
864 axis[0].NearestPointInLine(triangle0, triangle1, triangle2);
865 axis[1].NearestPointInLine(triangle1, triangle0, triangle2);
866 axis[2].NearestPointInLine(triangle2, triangle0, triangle1);
869 axisDot[0] = (triangle0-axis[0]).Dot(point-axis[0]);
870 axisDot[1] = (triangle1-axis[1]).Dot(point-axis[1]);
871 axisDot[2] = (triangle2-axis[2]).Dot(point-axis[2]);
874 Type bestMagnitude2 = 0;
875 Type closeMagnitude2;
876 Vector3d<double> closePoint;
878 if ( axisDot[0] < 0 )
880 closePoint.NearestPointInLineSegment(point, triangle1, triangle2);
881 closeMagnitude2 = point.Distance2(closePoint);
882 if ( bForce || (bestMagnitude2 > closeMagnitude2) )
885 bestMagnitude2 = closeMagnitude2;
886 nearestPoint = closePoint;
889 if ( axisDot[1] < 0 )
891 closePoint.NearestPointInLineSegment(point, triangle0, triangle2);
892 closeMagnitude2 = point.Distance2(closePoint);
893 if ( bForce || (bestMagnitude2 > closeMagnitude2) )
896 bestMagnitude2 = closeMagnitude2;
897 nearestPoint = closePoint;
900 if ( axisDot[2] < 0 )
902 closePoint.NearestPointInLineSegment(point, triangle0, triangle1);
903 closeMagnitude2 = point.Distance2(closePoint);
904 if ( bForce || (bestMagnitude2 > closeMagnitude2) )
907 bestMagnitude2 = closeMagnitude2;
908 nearestPoint = closePoint;
916 Vector3d<Type> normal;
919 normal.Cross(lineDelta0, lineDelta1);
921 Vector3d<double> pointDelta = point - triangle0;
922 double delta = normal.Dot(pointDelta) / normal.Dot(normal);
924 nearestPoint = point - delta*normal;
938 template <
class Type>
class Vector2d
943 Vector2d(
const Vector2d &a)
949 Vector2d(
const double *t)
962 const Type* Ptr()
const {
return &x; }
963 Type* Ptr() {
return &x; }
972 Vector2d & operator-=(
const Vector2d &a)
979 Vector2d & operator*=(
const Vector2d &a)
986 Vector2d & operator/=(
const Vector2d &a)
993 bool operator==(
const Vector2d<Type> &a)
const
995 if ( a.x == x && a.y == y )
return true;
1001 if ( a.x != x || a.y != y )
return true;
1041 Vector2d operator/(Vector2d a)
const
1049 Type Dot(
const Vector2d<Type> &a)
const
1051 return (x * a.x + y * a.y );
1054 Type GetX(
void)
const {
return x; }
1055 Type GetY(
void)
const {
return y; }
1057 void SetX(
Type t) { x = t; }
1058 void SetY(
Type t) { y = t; }
1071 Vector2d negative(
void)
const
1079 Type magnitude(
void)
const
1081 return (
Type) sqrtf(x * x + y * y );
1084 Type fastmagnitude(
void)
const
1086 return (
Type) fast_sqrt(x * x + y * y );
1089 Type fastermagnitude(
void)
const
1091 return (
Type) faster_sqrt( x * x + y * y );
1094 void Reflection(Vector2d &a,Vector2d &b);
1096 Type Length(
void)
const
1098 return Type(sqrtf( x*x + y*y ));
1101 Type FastLength(
void)
const
1103 return Type(fast_sqrt( x*x + y*y ));
1106 Type FasterLength(
void)
const
1108 return Type(faster_sqrt( x*x + y*y ));
1116 Type Distance(
const Vector2d &a)
const
1120 Type d = dx*dx+dy*dy;
1124 Type FastDistance(
const Vector2d &a)
const
1128 Type d = dx*dx+dy*dy;
1129 return fast_sqrt(d);
1132 Type FasterDistance(
const Vector2d &a)
const
1136 Type d = dx*dx+dy*dy;
1137 return faster_sqrt(d);
1140 Type Distance2(Vector2d &a)
1144 return dx*dx + dy *dy;
1147 void Lerp(
const Vector2d<Type>& from,
const Vector2d<Type>& to,
double slerp)
1149 x = ((to.x - from.x)*slerp) + from.x;
1150 y = ((to.y - from.y)*slerp) + from.y;
1154 void Cross(
const Vector2d<Type> &a,
const Vector2d<Type> &b)
1156 x = a.y*b.x - a.x*b.y;
1160 Type Normalize(
void)
1176 Type FastNormalize(
void)
1178 Type l = FastLength();
1192 Type FasterNormalize(
void)
1194 Type l = FasterLength();
1216 Line(
const Vector3d<double> &from,
const Vector3d<double> &to)
1223 bool Intersect(
const Line& src,Vector3d<double> §);
1225 Vector3d<double> mP1;
1226 Vector3d<double> mP2;
1231 template <
class Type> Vector3d<Type>
operator * (
Type s,
const Vector3d<Type> &v )
1232 { Vector3d <Type> Scaled(v.x*s, v.y*s, v.z*s);
1235 template <
class Type> Vector2d<Type>
operator * (
Type s,
const Vector2d<Type> &v )
1236 { Vector2d <Type> Scaled(v.x*s, v.y*s);
1242 # pragma warning( pop )
AGXCABLE_EXPORT SegmentDamage & operator+=(SegmentDamage &lhs, const SegmentDamage &rhs)
ItT operator-(const ItT &it, const agx::ItOp &inc)
AgXString< T > operator+(const Indentation &indentation, const AgXString< T > &str)
bool operator!=(T val, InvalidIndexStruct)
bool operator==(T val, InvalidIndexStruct)
Vec3T< T > operator*(const Vec3T< T > &v, const AffineMatrix4x4T< T > &m)