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
00028 #include "csgeom/math3d.h"
00029
00030
00031 #define CS_POL_SAME_PLANE 0
00032 #define CS_POL_FRONT 1
00033 #define CS_POL_BACK 2
00034 #define CS_POL_SPLIT_NEEDED 3
00035
00036 class csPoly2D;
00037
00041 class csPoly3D
00042 {
00043 protected:
00045 csVector3* vertices;
00047 int num_vertices;
00049 int max_vertices;
00050
00051 public:
00055 csPoly3D (int start_size = 10);
00056
00058 csPoly3D (const csPoly3D& copy);
00059
00061 virtual ~csPoly3D ();
00062
00066 void MakeEmpty ();
00067
00071 int GetVertexCount () const { return num_vertices; }
00072
00076 csVector3* GetVertices () const { return vertices; }
00077
00081 csVector3* GetVertex (int i) const
00082 {
00083 if (i<0 || i>=num_vertices) return 0;
00084 return &vertices[i];
00085 }
00086
00090 csVector3& operator[] (int i)
00091 {
00092 CS_ASSERT (i >= 0 && i < num_vertices);
00093 return vertices[i];
00094 }
00095
00099 csVector3& operator[] (int i) const
00100 {
00101 CS_ASSERT (i >= 0 && i < num_vertices);
00102 return vertices[i];
00103 }
00104
00108 csVector3* GetFirst () const
00109 { if (num_vertices<=0) return 0; else return vertices; }
00110
00114 csVector3* GetLast () const
00115 { if (num_vertices<=0) return 0; else return &vertices[num_vertices-1]; }
00116
00120 bool In (const csVector3& v) const;
00121
00125 static bool In (csVector3* poly, int num_poly, const csVector3& v);
00126
00130 void MakeRoom (int new_max);
00131
00135 void SetVertexCount (int n) { MakeRoom (n); num_vertices = n; }
00136
00141 int AddVertex (const csVector3& v) { return AddVertex (v.x, v.y, v.z); }
00142
00147 int AddVertex (float x, float y, float z);
00148
00152 void SetVertices (csVector3 const* v, int num)
00153 {
00154 MakeRoom (num);
00155 memcpy (vertices, v, (num_vertices = num) * sizeof (csVector3));
00156 }
00157
00165 bool ProjectXPlane (const csVector3& point, float plane_x,
00166 csPoly2D* poly2d) const;
00167
00175 bool ProjectYPlane (const csVector3& point, float plane_y,
00176 csPoly2D* poly2d) const;
00177
00185 bool ProjectZPlane (const csVector3& point, float plane_z,
00186 csPoly2D* poly2d) const;
00187
00194 bool ProjectAxisPlane (const csVector3& point, int plane_nr,
00195 float plane_pos, csPoly2D* poly2d) const
00196 {
00197 switch (plane_nr)
00198 {
00199 case 0: return ProjectXPlane (point, plane_pos, poly2d);
00200 case 1: return ProjectYPlane (point, plane_pos, poly2d);
00201 case 2: return ProjectZPlane (point, plane_pos, poly2d);
00202 }
00203 return false;
00204 }
00205
00213 static int Classify (const csPlane3& pl,
00214 csVector3* vertices, int num_vertices);
00215
00223 int Classify (const csPlane3& pl) const
00224 {
00225 return Classify (pl, vertices, num_vertices);
00226 }
00227
00229 int ClassifyX (float x) const;
00230
00232 int ClassifyY (float y) const;
00233
00235 int ClassifyZ (float z) const;
00236
00238 void CutToPlane (const csPlane3& split_plane);
00239
00241 void SplitWithPlane (csPoly3D& front, csPoly3D& back,
00242 const csPlane3& split_plane) const;
00243
00245 void SplitWithPlaneX (csPoly3D& front, csPoly3D& back, float x) const;
00246
00248 void SplitWithPlaneY (csPoly3D& front, csPoly3D& back, float y) const;
00249
00251 void SplitWithPlaneZ (csPoly3D& front, csPoly3D& back, float z) const;
00252
00254 static csVector3 ComputeNormal (csVector3* vertices, int num);
00255
00257 csVector3 ComputeNormal () const
00258 {
00259 return ComputeNormal (vertices, num_vertices);
00260 }
00261
00263 static csPlane3 ComputePlane (csVector3* vertices, int num);
00264
00266 csPlane3 ComputePlane () const
00267 {
00268 return ComputePlane (vertices, num_vertices);
00269 }
00270
00274 float GetSignedArea() const;
00275
00279 csVector3 GetCenter () const;
00280 };
00281
00288 class csVector3Array : public csPoly3D
00289 {
00290 public:
00291 csVector3Array (int start_size = 10) : csPoly3D (start_size) { }
00292
00297 int AddVertexSmart (const csVector3& v)
00298 { return AddVertexSmart (v.x, v.y, v.z); }
00299
00304 int AddVertexSmart (float x, float y, float z);
00305 };
00306
00309 #endif // __CS_POLY3D_H__