CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | 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 "csgeom/plane3.h"
00031 #include "csgeom/vector3.h"
00032 #include "csutil/dirtyaccessarray.h"
00033 
00034 class csPoly2D;
00035 
00036 // Values returned by classify.
00037 enum
00038 {
00039   CS_POL_SAME_PLANE = 0,
00040   CS_POL_FRONT = 1,
00041   CS_POL_BACK = 2,
00042   CS_POL_SPLIT_NEEDED = 3
00043 };
00044 
00048 class CS_CRYSTALSPACE_EXPORT csPoly3D
00049 {
00050 protected:
00052   csDirtyAccessArray<csVector3> vertices;
00053 
00054 public:
00058   csPoly3D (size_t start_size = 10);
00059 
00061   csPoly3D (const csPoly3D& copy);
00062 
00064   virtual ~csPoly3D ();
00065 
00069   void MakeEmpty ();
00070 
00074   inline size_t GetVertexCount () const { return vertices.Length (); }
00075 
00079   inline const csVector3* GetVertices () const { return vertices.GetArray (); }
00080 
00084   inline csVector3* GetVertices () { return vertices.GetArray (); }
00085 
00089   inline const csVector3* GetVertex (size_t i) const
00090   {
00091     if (i >= vertices.Length ()) return 0;
00092     return &(vertices.GetArray ()[i]);
00093   }
00094 
00098   inline csVector3& operator[] (size_t i)
00099   {
00100     return vertices[i];
00101   }
00102 
00106   inline const csVector3& operator[] (size_t i) const
00107   {
00108     return vertices[i];
00109   }
00110 
00114   inline const csVector3* GetFirst () const
00115   { 
00116     if (vertices.Length ()<=0) return 0;  
00117     else return vertices.GetArray ();
00118   }
00119 
00123   inline const csVector3* GetLast () const
00124   { 
00125     if (vertices.Length ()<=0) return 0; 
00126     else return 
00127       &(vertices.GetArray ())[vertices.Length ()-1]; 
00128   }
00129 
00133   bool In (const csVector3& v) const;
00134 
00138   static bool In (csVector3* poly, size_t num_poly, const csVector3& v);
00139 
00143   void MakeRoom (size_t new_max);
00144 
00148   inline void SetVertexCount (size_t n) 
00149   { 
00150     MakeRoom (n);
00151     vertices.SetLength (n); 
00152   }
00153 
00158   inline size_t AddVertex (const csVector3& v) { return AddVertex (v.x, v.y, v.z); }
00159 
00164   size_t AddVertex (float x, float y, float z);
00165 
00169   inline void SetVertices (csVector3 const* v, size_t num)
00170   {
00171     MakeRoom (num);
00172     memcpy (vertices.GetArray (), v, num * sizeof (csVector3));
00173   }
00174 
00182   bool ProjectXPlane (const csVector3& point, float plane_x,
00183         csPoly2D* poly2d) const;
00184 
00192   bool ProjectYPlane (const csVector3& point, float plane_y,
00193         csPoly2D* poly2d) const;
00194 
00202   bool ProjectZPlane (const csVector3& point, float plane_z,
00203         csPoly2D* poly2d) const;
00204 
00212   inline bool ProjectAxisPlane (const csVector3& point, int plane_nr,
00213         float plane_pos, csPoly2D* poly2d) const
00214   {
00215     switch (plane_nr)
00216     {
00217       case CS_AXIS_X: return ProjectXPlane (point, plane_pos, poly2d);
00218       case CS_AXIS_Y: return ProjectYPlane (point, plane_pos, poly2d);
00219       case CS_AXIS_Z: return ProjectZPlane (point, plane_pos, poly2d);
00220     }
00221     return false;
00222   }
00223 
00231   static int Classify (const csPlane3& pl,
00232         const csVector3* vertices, size_t num_vertices);
00233 
00241   inline int Classify (const csPlane3& pl) const
00242   {
00243     return Classify (pl, vertices.GetArray (), vertices.Length ());
00244   }
00245 
00247   int ClassifyX (float x) const;
00248 
00250   int ClassifyY (float y) const;
00251 
00253   int ClassifyZ (float z) const;
00254 
00256   inline int ClassifyAxis (int axis, float where) const
00257   {
00258     switch (axis)
00259     {
00260       case CS_AXIS_X: return ClassifyX (where);
00261       case CS_AXIS_Y: return ClassifyY (where);
00262       case CS_AXIS_Z: return ClassifyZ (where);
00263     }
00264     return 0;
00265   }
00266 
00274   int IsAxisAligned (float& where, float epsilon = SMALL_EPSILON) const;
00275 
00280   int ComputeMainNormalAxis () const;
00281 
00283   void CutToPlane (const csPlane3& split_plane);
00284 
00286   void SplitWithPlane (csPoly3D& front, csPoly3D& back,
00287         const csPlane3& split_plane) const;
00288 
00290   void SplitWithPlaneX (csPoly3D& front, csPoly3D& back, float x) const;
00291 
00293   void SplitWithPlaneY (csPoly3D& front, csPoly3D& back, float y) const;
00294 
00296   void SplitWithPlaneZ (csPoly3D& front, csPoly3D& back, float z) const;
00297 
00299   static csVector3 ComputeNormal (const csVector3* vertices, size_t num);
00300 
00302   static csVector3 ComputeNormal (const csArray<csVector3>& poly);
00303 
00305   static csVector3 ComputeNormal (int* poly, size_t num, csVector3* vertices);
00306 
00308   inline csVector3 ComputeNormal () const
00309   {
00310     return ComputeNormal (vertices.GetArray (), vertices.Length ());
00311   }
00312 
00314   static csPlane3 ComputePlane (const csVector3* vertices, size_t num);
00315 
00317   static csPlane3 ComputePlane (const csArray<csVector3>& poly);
00318 
00320   static csPlane3 ComputePlane (int* poly, size_t num, csVector3* vertices);
00321 
00323   inline csPlane3 ComputePlane () const
00324   {
00325     return ComputePlane (vertices.GetArray (), vertices.Length ());
00326   }
00327 
00331   float GetArea() const;
00332 
00336   csVector3 GetCenter () const;
00337 };
00338 
00340 struct csCompressVertex
00341 {
00342   size_t orig_idx;
00343   float x, y, z;
00344   size_t new_idx;
00345   bool used;
00346 };
00347 
00354 class CS_CRYSTALSPACE_EXPORT csVector3Array : public csPoly3D
00355 {
00356 public:
00357   csVector3Array (size_t start_size = 10) : csPoly3D (start_size) { }
00358 
00363   inline size_t AddVertexSmart (const csVector3& v)
00364   { return AddVertexSmart (v.x, v.y, v.z); }
00365 
00370   size_t AddVertexSmart (float x, float y, float z);
00371 
00383   static csCompressVertex* CompressVertices (csVector3* vertices,
00384         size_t num_vertices, csVector3*& new_vertices, size_t& new_count);
00385 
00394   static csCompressVertex* CompressVertices (csArray<csVector3>& vertices);
00395 };
00396 
00399 #endif // __CS_POLY3D_H__

Generated for Crystal Space by doxygen 1.4.4