|
poly3d.h00001 /* 00002 Copyright (C) 1998 by Jorrit Tyberghein 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_POLY3D_H__ 00020 #define __CS_POLY3D_H__ 00021 00022 #include "csgeom/math3d.h" 00023 00024 // Values returned by classify. 00025 #define CS_POL_SAME_PLANE 0 00026 #define CS_POL_FRONT 1 00027 #define CS_POL_BACK 2 00028 #define CS_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 static int Classify (const csPlane3& pl, 00205 csVector3* vertices, int num_vertices); 00206 00214 int Classify (const csPlane3& pl) const 00215 { 00216 return Classify (pl, vertices, num_vertices); 00217 } 00218 00220 int ClassifyX (float x) const; 00221 00223 int ClassifyY (float y) const; 00224 00226 int ClassifyZ (float z) const; 00227 00229 void CutToPlane (const csPlane3& split_plane); 00230 00232 void SplitWithPlane (csPoly3D& front, csPoly3D& back, 00233 const csPlane3& split_plane) const; 00234 00236 void SplitWithPlaneX (csPoly3D& front, csPoly3D& back, float x) const; 00237 00239 void SplitWithPlaneY (csPoly3D& front, csPoly3D& back, float y) const; 00240 00242 void SplitWithPlaneZ (csPoly3D& front, csPoly3D& back, float z) const; 00243 00245 static csVector3 ComputeNormal (csVector3* vertices, int num); 00246 00248 csVector3 ComputeNormal () const 00249 { 00250 return ComputeNormal (vertices, num_vertices); 00251 } 00252 00254 static csPlane3 ComputePlane (csVector3* vertices, int num); 00255 00257 csPlane3 ComputePlane () const 00258 { 00259 return ComputePlane (vertices, num_vertices); 00260 } 00261 00265 float GetSignedArea() const; 00266 00270 csVector3 GetCenter () const; 00271 }; 00272 00279 class csVector3Array : public csPoly3D 00280 { 00281 public: 00282 csVector3Array (int start_size = 10) : csPoly3D (start_size) { } 00283 00288 int AddVertexSmart (const csVector3& v) 00289 { return AddVertexSmart (v.x, v.y, v.z); } 00290 00295 int AddVertexSmart (float x, float y, float z); 00296 }; 00297 00298 #endif // __CS_POLY3D_H__ Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000 |