00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_MATH2D_H__
00021 #define __CS_MATH2D_H__
00022
00029
00030 #ifndef __CS_POLY_MACROS__
00031 #define __CS_POLY_MACROS__
00032 #define CS_POLY_IN 1
00033 #define CS_POLY_ON 0
00034 #define CS_POLY_OUT -1
00035 #endif
00036
00037 #include "csgeom/vector2.h"
00038 #include "csgeom/plane2.h"
00039 #include "csgeom/segment.h"
00040
00041 class csBox2;
00042 class csPoly2D;
00043
00048 class csMath2
00049 {
00050 public:
00057 static int WhichSide2D (const csVector2& v,
00058 const csVector2& s1, const csVector2& s2)
00059 {
00060 float k = (s1.y - v.y)*(s2.x - s1.x);
00061 float k1 = (s1.x - v.x)*(s2.y - s1.y);
00062 if (k < k1) return -1;
00063 else if (k > k1) return 1;
00064 else return 0;
00065 }
00066
00073 static int WhichSide2D (const csVector2& v,
00074 const csSegment2& s)
00075 {
00076 return WhichSide2D (v, s.Start (), s.End ());
00077 }
00078
00086 static int InPoly2D (const csVector2& v,
00087 csVector2* P, int n, csBox2* bounding_box);
00088
00094 static float Area2 (const csVector2& a,
00095 const csVector2& b,
00096 const csVector2& c)
00097 {
00098 return
00099 a.x * b.y - a.y * b.x +
00100 a.y * c.x - a.x * c.y +
00101 b.x * c.y - c.x * b.y;
00102 }
00103
00109 static float Right (const csVector2& a,
00110 const csVector2& b,
00111 const csVector2& c)
00112 {
00113 return Area2 (a, b, c) <= -SMALL_EPSILON;
00114 }
00115
00121 static float Left (const csVector2& a,
00122 const csVector2& b,
00123 const csVector2& c)
00124 {
00125 return Area2 (a, b, c) >= SMALL_EPSILON;
00126 }
00127
00133 static bool Visible (const csVector2& p, const csPlane2& pl)
00134 { return pl.Classify (p) <= 0; }
00135
00142 static bool PlanesEqual (const csPlane2& p1, const csPlane2& p2)
00143 {
00144 return ( ( p1.norm - p2.norm) < (float).001 ) &&
00145 ( ABS (p1.CC-p2.CC) < (float).001 );
00146 }
00147
00153 static bool PlanesClose (const csPlane2& p1, const csPlane2& p2);
00154 };
00155
00161 class csIntersect2
00162 {
00163 public:
00170 static bool IntersectPolygon (const csPlane2& plane, csPoly2D* poly,
00171 csSegment2& segment);
00172
00178 static bool Segments (
00179 const csSegment2& a, const csSegment2& b,
00180 csVector2& isect, float& dist);
00181
00187 static bool SegmentLine (
00188 const csSegment2& a,
00189 const csSegment2& b,
00190 csVector2& isect, float& dist);
00191
00196 static bool Lines (
00197
00198 const csSegment2& a, const csSegment2& b,
00199 csVector2& isect);
00200
00209 static bool Plane (
00210 const csVector2& u, const csVector2& v,
00211 const csPlane2& p,
00212 csVector2& isect,
00213 float& dist);
00214
00223 static bool Plane (
00224 const csSegment2& uv,
00225 const csPlane2& p,
00226 csVector2& isect,
00227 float& dist)
00228 {
00229 return Plane (uv.Start (), uv.End (), p, isect, dist);
00230 }
00231
00236 static void PlaneNoTest (const csVector2& u, const csVector2& v,
00237 const csPlane2& p, csVector2& isect, float& dist)
00238 {
00239 float x,y, denom;
00240 x = v.x-u.x; y = v.y-u.y;
00241 denom = p.norm.x*x + p.norm.y*y;
00242 dist = -(p.norm*u + p.CC) / denom;
00243 isect.x = u.x + dist*x; isect.y = u.y + dist*y;
00244 }
00245
00250 static void PlaneNoTest (const csSegment2& uv,
00251 const csPlane2& p, csVector2& isect, float& dist)
00252 {
00253 PlaneNoTest (uv.Start (), uv.End (), p, isect, dist);
00254 }
00255
00261 static bool Planes (const csPlane2& p1, const csPlane2& p2,
00262 csVector2& isect);
00263
00264 };
00265
00268 #endif // __CS_MATH2D_H__