CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

csgeom/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/math3d.h"
00031 
00032 // Values returned by classify.
00033 #define CS_POL_SAME_PLANE 0
00034 #define CS_POL_FRONT 1
00035 #define CS_POL_BACK 2
00036 #define CS_POL_SPLIT_NEEDED 3
00037 
00038 class csPoly2D;
00039 
00043 class CS_CSGEOM_EXPORT csPoly3D
00044 {
00045 protected:
00047   csVector3* vertices;
00049   int num_vertices;
00051   int max_vertices;
00052 
00053 public:
00057   csPoly3D (int start_size = 10);
00058 
00060   csPoly3D (const csPoly3D& copy);
00061 
00063   virtual ~csPoly3D ();
00064 
00068   void MakeEmpty ();
00069 
00073   int GetVertexCount () const { return num_vertices; }
00074 
00078   csVector3* GetVertices () const { return vertices; }
00079 
00083   csVector3* GetVertex (int i) const
00084   {
00085     if (i<0 || i>=num_vertices) return 0;
00086     return &vertices[i];
00087   }
00088 
00092   csVector3& operator[] (int i)
00093   {
00094     CS_ASSERT (i >= 0 && i < num_vertices);
00095     return vertices[i];
00096   }
00097 
00101   csVector3& operator[] (int i) const
00102   {
00103     CS_ASSERT (i >= 0 && i < num_vertices);
00104     return vertices[i];
00105   }
00106 
00110   csVector3* GetFirst () const
00111   { if (num_vertices<=0) return 0;  else return vertices; }
00112 
00116   csVector3* GetLast () const
00117   { if (num_vertices<=0) return 0; else return &vertices[num_vertices-1]; }
00118 
00122   bool In (const csVector3& v) const;
00123 
00127   static bool In (csVector3* poly, int num_poly, const csVector3& v);
00128 
00132   void MakeRoom (int new_max);
00133 
00137   void SetVertexCount (int n) { MakeRoom (n); num_vertices = n; }
00138 
00143   int AddVertex (const csVector3& v) { return AddVertex (v.x, v.y, v.z); }
00144 
00149   int AddVertex (float x, float y, float z);
00150 
00154   void SetVertices (csVector3 const* v, int num)
00155   {
00156     MakeRoom (num);
00157     memcpy (vertices, v, (num_vertices = num) * sizeof (csVector3));
00158   }
00159 
00167   bool ProjectXPlane (const csVector3& point, float plane_x,
00168         csPoly2D* poly2d) const;
00169 
00177   bool ProjectYPlane (const csVector3& point, float plane_y,
00178         csPoly2D* poly2d) const;
00179 
00187   bool ProjectZPlane (const csVector3& point, float plane_z,
00188         csPoly2D* poly2d) const;
00189 
00196   bool ProjectAxisPlane (const csVector3& point, int plane_nr,
00197         float plane_pos, csPoly2D* poly2d) const
00198   {
00199     switch (plane_nr)
00200     {
00201       case 0: return ProjectXPlane (point, plane_pos, poly2d);
00202       case 1: return ProjectYPlane (point, plane_pos, poly2d);
00203       case 2: return ProjectZPlane (point, plane_pos, poly2d);
00204     }
00205     return false;
00206   }
00207 
00215   static int Classify (const csPlane3& pl,
00216         csVector3* vertices, int num_vertices);
00217 
00225   int Classify (const csPlane3& pl) const
00226   {
00227     return Classify (pl, vertices, num_vertices);
00228   }
00229 
00231   int ClassifyX (float x) const;
00232 
00234   int ClassifyY (float y) const;
00235 
00237   int ClassifyZ (float z) const;
00238 
00240   void CutToPlane (const csPlane3& split_plane);
00241 
00243   void SplitWithPlane (csPoly3D& front, csPoly3D& back,
00244         const csPlane3& split_plane) const;
00245 
00247   void SplitWithPlaneX (csPoly3D& front, csPoly3D& back, float x) const;
00248 
00250   void SplitWithPlaneY (csPoly3D& front, csPoly3D& back, float y) const;
00251 
00253   void SplitWithPlaneZ (csPoly3D& front, csPoly3D& back, float z) const;
00254 
00256   static csVector3 ComputeNormal (csVector3* vertices, int num);
00257 
00259   csVector3 ComputeNormal () const
00260   {
00261     return ComputeNormal (vertices, num_vertices);
00262   }
00263 
00265   static csPlane3 ComputePlane (csVector3* vertices, int num);
00266 
00268   csPlane3 ComputePlane () const
00269   {
00270     return ComputePlane (vertices, num_vertices);
00271   }
00272 
00276   float GetArea() const;
00277 
00281   csVector3 GetCenter () const;
00282 };
00283 
00285 struct csCompressVertex
00286 {
00287   int orig_idx;
00288   float x, y, z;
00289   int new_idx;
00290   bool used;
00291 };
00292 
00299 class CS_CSGEOM_EXPORT csVector3Array : public csPoly3D
00300 {
00301 public:
00302   csVector3Array (int start_size = 10) : csPoly3D (start_size) { }
00303 
00308   int AddVertexSmart (const csVector3& v)
00309   { return AddVertexSmart (v.x, v.y, v.z); }
00310 
00315   int AddVertexSmart (float x, float y, float z);
00316 
00328   static csCompressVertex* CompressVertices (csVector3* vertices,
00329         int num_vertices, csVector3*& new_vertices, int& new_count);
00330 
00339   static csCompressVertex* CompressVertices (csArray<csVector3>& vertices);
00340 };
00341 
00344 #endif // __CS_POLY3D_H__

Generated for Crystal Space by doxygen 1.2.18