00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_MATH3D_H__
00021 #define __CS_MATH3D_H__
00022
00023 #ifndef __CS_CSSYSDEFS_H__
00024 #error "cssysdef.h must be included in EVERY source file!"
00025 #endif
00026
00027 #include "csgeom/vector3.h"
00028 #include "csgeom/plane3.h"
00029 #include "csgeom/plane2.h"
00030 #include "csgeom/segment.h"
00031
00032 class csDVector3;
00033 class csPoly3D;
00034 class csBox3;
00035
00036 inline float fSqr (float f)
00037 {
00038 return f * f;
00039 }
00040
00045 class csMath3
00046 {
00047 public:
00060 static int WhichSide3D (const csVector3& p,
00061 const csVector3& v1, const csVector3& v2)
00062 {
00063
00064 float s = p.x*(v1.y*v2.z-v1.z*v2.y) + p.y*(v1.z*v2.x-v1.x*v2.z) +
00065 p.z*(v1.x*v2.y-v1.y*v2.x);
00066 if (s < 0) return 1;
00067 else if (s > 0) return -1;
00068 else return 0;
00069 }
00070
00076 static bool Visible (const csVector3& p, const csVector3& t1,
00077 const csVector3& t2, const csVector3& t3);
00078
00084 static bool Visible (const csVector3& p, const csPlane3& pl)
00085 { return pl.Classify (p) <= 0; }
00086
00094 static bool FindIntersection(const csVector3 tri1[3],
00095 const csVector3 tri2[3],
00096 csVector3 line[2]);
00097
00107 static void Between (const csVector3& v1, const csVector3& v2, csVector3& v,
00108 float pct, float wid);
00109
00116 static void SetMinMax (const csVector3& v,
00117 csVector3& min, csVector3& max)
00118 {
00119 if (v.x > max.x) max.x = v.x; else if (v.x < min.x ) min.x = v.x;
00120 if (v.y > max.y) max.y = v.y; else if (v.y < min.y ) min.y = v.y;
00121 if (v.z > max.z) max.z = v.z; else if (v.z < min.z ) min.z = v.z;
00122 }
00123
00129 inline static float Area3 (const csVector3 &a, const csVector3 &b,
00130 const csVector3 &c)
00131 {
00132 csVector3 v1 = b - a;
00133 csVector3 v2 = c - a;
00134 return ((v1.y * v2.z + v1.z * v2.x + v1.x * v2.y) -
00135 (v1.y * v2.x + v1.x * v2.z + v1.z * v2.y));
00136 }
00137
00143 inline static void CalcNormal (csVector3& norm, const csVector3& v1,
00144 const csVector3& v2, const csVector3& v3)
00145 {
00146 norm = (v1-v2)%(v1-v3);
00147 }
00148
00154 static void CalcNormal (csVector3& norm,
00155 const csVector3& v, const csVector3& u)
00156 { norm = u%v; }
00157
00164 static void CalcPlane (const csVector3& v1, const csVector3& v2,
00165 const csVector3& v3, csVector3& normal, float& D)
00166 {
00167 CalcNormal (normal, v1, v2, v3);
00168 D = - (normal * v1);
00169 }
00170
00177 static bool PlanesEqual (const csPlane3& p1, const csPlane3& p2)
00178 {
00179 return ( ( p1.norm - p2.norm) < (float).001 ) &&
00180 ( ABS (p1.DD-p2.DD) < (float).001 );
00181 }
00182
00188 static bool PlanesClose (const csPlane3& p1, const csPlane3& p2);
00189
00197 static int OuterPlanes (const csBox3& box1, const csBox3& box2,
00198 csPlane3* planes);
00199
00207 static int FindObserverSides (const csBox3& box1, const csBox3& box2,
00208 int* sides);
00209 };
00210
00215 class csSquaredDist
00216 {
00217 public:
00219 static float PointPoint (const csVector3& p1, const csVector3& p2)
00220 { return fSqr (p1.x - p2.x) + fSqr (p1.y - p2.y) + fSqr (p1.z - p2.z); }
00221
00223 static float PointLine (const csVector3& p,
00224 const csVector3& l1, const csVector3& l2);
00225
00227 static float PointPlane (const csVector3& p, const csPlane3& plane)
00228 { float r = plane.Classify (p); return r * r; }
00229
00236 static float PointPoly (const csVector3& p, csVector3 *V, int n,
00237 const csPlane3& plane, float sqdist = -1);
00238 };
00239
00245 class csIntersect3
00246 {
00247 public:
00254 static bool IntersectPolygon (const csPlane3& plane, csPoly3D* poly,
00255 csSegment3& segment);
00256
00266 static int IntersectSegment (csPlane3* planes, int num_planes,
00267 csSegment3& seg);
00268
00274 static bool IntersectTriangle (const csVector3& tr1,
00275 const csVector3& tr2, const csVector3& tr3,
00276 const csSegment3& seg, csVector3& isect);
00277
00282 static void Plane (
00283 const csVector3& u, const csVector3& v,
00284 const csVector3& normal, const csVector3& a,
00285 csVector3& isect, float& dist);
00286
00295 static bool Plane (
00296 const csVector3& u, const csVector3& v,
00297 const csPlane3& p,
00298 csVector3& isect,
00299 float& dist);
00300
00306 static bool Planes (const csPlane3& p1, const csPlane3& p2,
00307 const csPlane3& p3, csVector3& isect);
00308
00315 static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect);
00316
00323 static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect);
00324
00331 static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect);
00332
00339 static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos,
00340 csPlane2& isect)
00341 {
00342 switch (nr)
00343 {
00344 case 0: return PlaneXPlane (p1, pos, isect);
00345 case 1: return PlaneYPlane (p1, pos, isect);
00346 case 2: return PlaneZPlane (p1, pos, isect);
00347 }
00348 return false;
00349 }
00350
00357 static float Z0Plane (
00358 const csVector3& v1, const csVector3& v2,
00359 csVector3& isect);
00360
00367 static float Z0Plane (
00368 const csSegment3& uv,
00369 csVector3& isect)
00370 {
00371 return Z0Plane (uv.Start (), uv.End (), isect);
00372 }
00373
00380 static float ZPlane (float zval,
00381 const csVector3& u, const csVector3& v,
00382 csVector3& isect);
00383
00390 static float ZPlane (float zval,
00391 const csSegment3& uv,
00392 csVector3& isect)
00393 {
00394 return ZPlane (zval, uv.Start (), uv.End (), isect);
00395 }
00396
00401 static float XFrustum (
00402 float A, const csVector3& u, const csVector3& v, csVector3& isect);
00403
00408 static float XFrustum (
00409 float A, const csSegment3& uv, csVector3& isect)
00410 {
00411 return XFrustum (A, uv.Start (), uv.End (), isect);
00412 }
00413
00418 static float YFrustum (
00419 float B, const csVector3& u, const csVector3& v, csVector3& isect);
00420
00425 static float YFrustum (
00426 float B, const csSegment3& uv, csVector3& isect)
00427 {
00428 return YFrustum (B, uv.Start (), uv.End (), isect);
00429 }
00430
00439 static int BoxSegment (const csBox3& box, const csSegment3& segment,
00440 csVector3& isect, float* pr = NULL);
00441 };
00442
00443 #endif // __CS_MATH3D_H__