Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

polyint.h

00001 /*
00002     Copyright (C) 1998-2000 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_POLYINT_H__
00020 #define __CS_POLYINT_H__
00021 
00022 #include "csgeom/math3d.h"
00023 #include "csgeom/poly3d.h"
00024 
00025 class csPolygonIntPool;
00026 
00037 class csPolygonInt
00038 {
00039   friend class csPolygonIntPool;
00040 
00041 private:
00042   // Reference counter.
00043   int ref_count;
00044 
00045 public:
00047   csPolygonInt () { ref_count = 1; }
00048 
00052   void IncRefCount () { ref_count++; }
00053 
00059   bool DecRefCount () { ref_count--; return ref_count > 0; }
00060 
00064   bool IsReferenced () { return ref_count > 0; }
00065 
00069   virtual csPlane3* GetPolyPlane () = 0;
00070 
00078   virtual int Classify (const csPlane3& pl) = 0;
00079 
00085   virtual int ClassifyX (float x) { return Classify (csPlane3 (1, 0, 0, -x)); }
00086 
00092   virtual int ClassifyY (float y) { return Classify (csPlane3 (0, 1, 0, -y)); }
00093 
00099   virtual int ClassifyZ (float z) { return Classify (csPlane3 (0, 0, 1, -z)); }
00100 
00108   virtual void SplitWithPlane (csPolygonInt** front, csPolygonInt** back,
00109         const csPlane3& plane) = 0;
00110 
00116   virtual void SplitWithPlaneX (csPolygonInt** front, csPolygonInt** back,
00117         float x)
00118   {
00119     SplitWithPlane (front, back, csPlane3 (1, 0, 0, -x));
00120   }
00121 
00127   virtual void SplitWithPlaneY (csPolygonInt** front, csPolygonInt** back,
00128         float y)
00129   {
00130     SplitWithPlane (front, back, csPlane3 (0, 1, 0, -y));
00131   }
00132 
00138   virtual void SplitWithPlaneZ (csPolygonInt** front, csPolygonInt** back,
00139         float z)
00140   {
00141     SplitWithPlane (front, back, csPlane3 (0, 0, 1, -z));
00142   }
00143 
00150   virtual int GetType () = 0;
00151 
00155   virtual int GetVertexCount () = 0;
00156 
00160   virtual int* GetVertexIndices () = 0;
00161 
00166   virtual csPolygonInt* GetUnsplitPolygon () = 0;
00167 
00176   virtual bool Overlaps (csPolygonInt* overlapped) = 0;
00177 };
00178 
00182 class csPolygonIntArray
00183 {
00184 private:
00186   csPolygonInt** polygons;
00188   int num;
00190   int max;
00191 
00192 public:
00194   csPolygonIntArray ();
00195 
00200   ~csPolygonIntArray ();
00201 
00203   void Reset () { num = 0; }
00204 
00206   void AddPolygon (csPolygonInt* polygon);
00207 
00209   csPolygonInt* GetPolygon (int idx) { return polygons[idx]; }
00210 
00212   csPolygonInt** GetPolygons () { return polygons; }
00213 
00215   int GetPolygonCount () { return num; }
00216 
00222   void SetPolygonCount (int n) { num = n; }
00223 };
00224 
00232 class csPolygonIntFactory
00233 {
00234 public:
00236   virtual csPolygonInt* Create () = 0;
00238   virtual void Init (csPolygonInt* p) = 0;
00239 };
00240 
00247 class csPolygonIntPool
00248 {
00249 private:
00250   struct PoolObj
00251   {
00252     PoolObj* next;
00253     csPolygonInt* pi;
00254   };
00256   PoolObj* alloced;
00258   PoolObj* freed;
00260   csPolygonIntFactory* poly_fact;
00261 
00262 public:
00264   csPolygonIntPool (csPolygonIntFactory* fact) : alloced (NULL),
00265         freed (NULL), poly_fact (fact) { }
00266 
00268   ~csPolygonIntPool ()
00269   {
00270     while (alloced)
00271     {
00272       PoolObj* n = alloced->next;
00273       //delete alloced->pi; @@@ This free is not valid!
00274       // We should use a ref count on the pool itself so that we
00275       // now when all objects in the pool are freed and the
00276       // 'alloced' list will be empty.
00277       delete alloced;
00278       alloced = n;
00279     }
00280     while (freed)
00281     {
00282       PoolObj* n = freed->next;
00283       if (!freed->pi->DecRefCount ()) delete freed->pi;
00284       delete freed;
00285       freed = n;
00286     }
00287   }
00288 
00292   csPolygonInt* Alloc ()
00293   {
00294     PoolObj* pnew;
00295     if (freed)
00296     {
00297       pnew = freed;
00298       freed = freed->next;
00299     }
00300     else
00301     {
00302       pnew = new PoolObj ();
00303       pnew->pi = poly_fact->Create ();
00304     }
00305     poly_fact->Init (pnew->pi);
00306     pnew->next = alloced;
00307     alloced = pnew;
00308     pnew->pi->ref_count = 1;
00309     return pnew->pi;
00310   }
00311 
00317   void Free (csPolygonInt* pi)
00318   {
00319     if (pi->DecRefCount ()) return;
00320     if (alloced)
00321     {
00322       PoolObj* po = alloced;
00323       alloced = alloced->next;
00324       po->pi = pi;
00325       po->next = freed;
00326       freed = po;
00327     }
00328     else
00329     {
00330       // Cannot happen!
00331     }
00332   }
00333 
00335   void Dump ()
00336   {
00337     int cnt;
00338     cnt = 0;
00339     PoolObj* po = alloced;
00340     while (po) { cnt++; po = po->next; }
00341     printf ("PolyInt pool: %d allocated, ", cnt);
00342     cnt = 0;
00343     po = freed;
00344     while (po) { cnt++; po = po->next; }
00345     printf ("%d freed.\n", cnt);
00346   }
00347 };
00348 
00349 #endif // __CS_POLYINT_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000