00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CS_POLY3D_H__
00020 #define __CS_POLY3D_H__
00021
00022 #include "csgeom/math3d.h"
00023
00024
00025 #define POL_SAME_PLANE 0
00026 #define POL_FRONT 1
00027 #define POL_BACK 2
00028 #define POL_SPLIT_NEEDED 3
00029
00030 class csPoly2D;
00031
00035 class csPoly3D
00036 {
00037 protected:
00039 csVector3* vertices;
00041 int num_vertices;
00043 int max_vertices;
00044
00045 public:
00049 csPoly3D (int start_size = 10);
00050
00052 csPoly3D (const csPoly3D& copy);
00053
00055 virtual ~csPoly3D ();
00056
00060 void MakeEmpty ();
00061
00065 int GetVertexCount () const { return num_vertices; }
00066
00070 csVector3* GetVertices () const { return vertices; }
00071
00075 csVector3* GetVertex (int i) const
00076 {
00077 if (i<0 || i>=num_vertices) return NULL;
00078 return &vertices[i];
00079 }
00080
00084 csVector3& operator[] (int i)
00085 {
00086 CS_ASSERT (i >= 0 && i < num_vertices);
00087 return vertices[i];
00088 }
00089
00093 csVector3& operator[] (int i) const
00094 {
00095 CS_ASSERT (i >= 0 && i < num_vertices);
00096 return vertices[i];
00097 }
00098
00102 csVector3* GetFirst () const
00103 { if (num_vertices<=0) return NULL; else return vertices; }
00104
00108 csVector3* GetLast () const
00109 { if (num_vertices<=0) return NULL; else return &vertices[num_vertices-1]; }
00110
00114 bool In (const csVector3& v) const;
00115
00119 static bool In (csVector3* poly, int num_poly, const csVector3& v);
00120
00124 void MakeRoom (int new_max);
00125
00129 void SetVertexCount (int n) { MakeRoom (n); num_vertices = n; }
00130
00135 int AddVertex (const csVector3& v) { return AddVertex (v.x, v.y, v.z); }
00136
00141 int AddVertex (float x, float y, float z);
00142
00146 void SetVertices (csVector3 const* v, int num)
00147 { MakeRoom (num); memcpy (vertices, v, (num_vertices = num) * sizeof (csVector3)); }
00148
00156 bool ProjectXPlane (const csVector3& point, float plane_x,
00157 csPoly2D* poly2d) const;
00158
00166 bool ProjectYPlane (const csVector3& point, float plane_y,
00167 csPoly2D* poly2d) const;
00168
00176 bool ProjectZPlane (const csVector3& point, float plane_z,
00177 csPoly2D* poly2d) const;
00178
00185 bool ProjectAxisPlane (const csVector3& point, int plane_nr,
00186 float plane_pos, csPoly2D* poly2d) const
00187 {
00188 switch (plane_nr)
00189 {
00190 case 0: return ProjectXPlane (point, plane_pos, poly2d);
00191 case 1: return ProjectYPlane (point, plane_pos, poly2d);
00192 case 2: return ProjectZPlane (point, plane_pos, poly2d);
00193 }
00194 return false;
00195 }
00196
00204 int Classify (const csPlane3& pl) const;
00205
00207 int ClassifyX (float x) const;
00208
00210 int ClassifyY (float y) const;
00211
00213 int ClassifyZ (float z) const;
00214
00216 void CutToPlane (const csPlane3& split_plane);
00217
00219 void SplitWithPlane (csPoly3D& front, csPoly3D& back,
00220 const csPlane3& split_plane) const;
00221
00223 void SplitWithPlaneX (csPoly3D& front, csPoly3D& back, float x) const;
00224
00226 void SplitWithPlaneY (csPoly3D& front, csPoly3D& back, float y) const;
00227
00229 void SplitWithPlaneZ (csPoly3D& front, csPoly3D& back, float z) const;
00230
00232 static csVector3 ComputeNormal (csVector3* vertices, int num);
00233
00235 csVector3 ComputeNormal () const
00236 {
00237 return ComputeNormal (vertices, num_vertices);
00238 }
00239
00241 static csPlane3 ComputePlane (csVector3* vertices, int num);
00242
00244 csPlane3 ComputePlane () const
00245 {
00246 return ComputePlane (vertices, num_vertices);
00247 }
00248
00252 float GetSignedArea() const;
00253
00257 csVector3 GetCenter () const;
00258 };
00259
00266 class csVector3Array : public csPoly3D
00267 {
00268 public:
00269 csVector3Array (int start_size = 10) : csPoly3D (start_size) { }
00270
00275 int AddVertexSmart (const csVector3& v)
00276 { return AddVertexSmart (v.x, v.y, v.z); }
00277
00282 int AddVertexSmart (float x, float y, float z);
00283 };
00284
00285 #endif // __CS_POLY3D_H__