Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages
poly3d.h
Go to the documentation of this file.00001 /* 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 00028 #include "csextern.h" 00029 00030 #include "csutil/dirtyaccessarray.h" 00031 #include "csgeom/math3d.h" 00032 00033 // Values returned by classify. 00034 #define CS_POL_SAME_PLANE 0 00035 #define CS_POL_FRONT 1 00036 #define CS_POL_BACK 2 00037 #define CS_POL_SPLIT_NEEDED 3 00038 00039 class csPoly2D; 00040 00044 class CS_CSGEOM_EXPORT csPoly3D 00045 { 00046 protected: 00048 csDirtyAccessArray<csVector3> vertices; 00049 00050 public: 00054 csPoly3D (size_t start_size = 10); 00055 00057 csPoly3D (const csPoly3D& copy); 00058 00060 virtual ~csPoly3D (); 00061 00065 void MakeEmpty (); 00066 00070 size_t GetVertexCount () const { return vertices.Length (); } 00071 00075 const csVector3* GetVertices () const { return vertices.GetArray (); } 00076 00080 csVector3* GetVertices () { return vertices.GetArray (); } 00081 00085 const csVector3* GetVertex (size_t i) const 00086 { 00087 if (i >= vertices.Length ()) return 0; 00088 return &(vertices.GetArray ()[i]); 00089 } 00090 00094 csVector3& operator[] (size_t i) 00095 { 00096 return vertices[i]; 00097 } 00098 00102 const csVector3& operator[] (size_t i) const 00103 { 00104 return vertices[i]; 00105 } 00106 00110 const csVector3* GetFirst () const 00111 { if (vertices.Length ()<=0) return 0; else return vertices.GetArray (); } 00112 00116 const csVector3* GetLast () const 00117 { if (vertices.Length ()<=0) return 0; else return &(vertices.GetArray ())[ 00118 vertices.Length ()-1]; } 00119 00123 bool In (const csVector3& v) const; 00124 00128 static bool In (csVector3* poly, size_t num_poly, const csVector3& v); 00129 00133 void MakeRoom (size_t new_max); 00134 00138 void SetVertexCount (size_t n) 00139 { 00140 MakeRoom (n); 00141 vertices.SetLength (n); 00142 } 00143 00148 size_t AddVertex (const csVector3& v) { return AddVertex (v.x, v.y, v.z); } 00149 00154 size_t AddVertex (float x, float y, float z); 00155 00159 void SetVertices (csVector3 const* v, size_t num) 00160 { 00161 MakeRoom (num); 00162 memcpy (vertices.GetArray (), v, num * sizeof (csVector3)); 00163 } 00164 00172 bool ProjectXPlane (const csVector3& point, float plane_x, 00173 csPoly2D* poly2d) const; 00174 00182 bool ProjectYPlane (const csVector3& point, float plane_y, 00183 csPoly2D* poly2d) const; 00184 00192 bool ProjectZPlane (const csVector3& point, float plane_z, 00193 csPoly2D* poly2d) const; 00194 00202 bool ProjectAxisPlane (const csVector3& point, int plane_nr, 00203 float plane_pos, csPoly2D* poly2d) const 00204 { 00205 switch (plane_nr) 00206 { 00207 case CS_AXIS_X: return ProjectXPlane (point, plane_pos, poly2d); 00208 case CS_AXIS_Y: return ProjectYPlane (point, plane_pos, poly2d); 00209 case CS_AXIS_Z: return ProjectZPlane (point, plane_pos, poly2d); 00210 } 00211 return false; 00212 } 00213 00221 static int Classify (const csPlane3& pl, 00222 const csVector3* vertices, size_t num_vertices); 00223 00231 int Classify (const csPlane3& pl) const 00232 { 00233 return Classify (pl, vertices.GetArray (), vertices.Length ()); 00234 } 00235 00237 int ClassifyX (float x) const; 00238 00240 int ClassifyY (float y) const; 00241 00243 int ClassifyZ (float z) const; 00244 00246 int ClassifyAxis (int axis, float where) const 00247 { 00248 switch (axis) 00249 { 00250 case CS_AXIS_X: return ClassifyX (where); 00251 case CS_AXIS_Y: return ClassifyY (where); 00252 case CS_AXIS_Z: return ClassifyZ (where); 00253 } 00254 return 0; 00255 } 00256 00264 int IsAxisAligned (float& where, float epsilon = SMALL_EPSILON) const; 00265 00267 void CutToPlane (const csPlane3& split_plane); 00268 00270 void SplitWithPlane (csPoly3D& front, csPoly3D& back, 00271 const csPlane3& split_plane) const; 00272 00274 void SplitWithPlaneX (csPoly3D& front, csPoly3D& back, float x) const; 00275 00277 void SplitWithPlaneY (csPoly3D& front, csPoly3D& back, float y) const; 00278 00280 void SplitWithPlaneZ (csPoly3D& front, csPoly3D& back, float z) const; 00281 00283 static csVector3 ComputeNormal (const csVector3* vertices, size_t num); 00284 00286 static csVector3 ComputeNormal (const csArray<csVector3>& poly); 00287 00289 static csVector3 ComputeNormal (int* poly, size_t num, csVector3* vertices); 00290 00292 csVector3 ComputeNormal () const 00293 { 00294 return ComputeNormal (vertices.GetArray (), vertices.Length ()); 00295 } 00296 00298 static csPlane3 ComputePlane (const csVector3* vertices, size_t num); 00299 00301 static csPlane3 ComputePlane (const csArray<csVector3>& poly); 00302 00304 static csPlane3 ComputePlane (int* poly, size_t num, csVector3* vertices); 00305 00307 csPlane3 ComputePlane () const 00308 { 00309 return ComputePlane (vertices.GetArray (), vertices.Length ()); 00310 } 00311 00315 float GetArea() const; 00316 00320 csVector3 GetCenter () const; 00321 }; 00322 00324 struct csCompressVertex 00325 { 00326 size_t orig_idx; 00327 float x, y, z; 00328 size_t new_idx; 00329 bool used; 00330 }; 00331 00338 class CS_CSGEOM_EXPORT csVector3Array : public csPoly3D 00339 { 00340 public: 00341 csVector3Array (size_t start_size = 10) : csPoly3D (start_size) { } 00342 00347 size_t AddVertexSmart (const csVector3& v) 00348 { return AddVertexSmart (v.x, v.y, v.z); } 00349 00354 size_t AddVertexSmart (float x, float y, float z); 00355 00367 static csCompressVertex* CompressVertices (csVector3* vertices, 00368 size_t num_vertices, csVector3*& new_vertices, size_t& new_count); 00369 00378 static csCompressVertex* CompressVertices (csArray<csVector3>& vertices); 00379 }; 00380 00383 #endif // __CS_POLY3D_H__
Generated for Crystal Space by doxygen 1.3.9.1