CrystalSpace

Public API Reference

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

polyedge.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 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_POLYEDGE_H__
00020 #define __CS_POLYEDGE_H__
00021 
00028 #include "csextern.h"
00029 
00030 #include "csgeom/math2d.h"
00031 #include "csgeom/segment.h"
00032 
00037 class CS_CSGEOM_EXPORT csPoly2DEdges
00038 {
00039 protected:
00041   csSegment2* edges;
00043   int num_edges;
00045   int max_edges;
00046 
00047 public:
00051   csPoly2DEdges (int start_size = 10);
00052 
00054   csPoly2DEdges (csPoly2DEdges& copy);
00055 
00057   virtual ~csPoly2DEdges ();
00058 
00062   void MakeEmpty ();
00063 
00067   int GetEdgeCount () { return num_edges; }
00068 
00072   csSegment2* GetEdges () { return edges; }
00073 
00077   csSegment2* GetEdge (int i)
00078   {
00079     if (i<0 || i>=num_edges) return 0;
00080     return &edges[i];
00081   }
00082 
00086   csSegment2& operator[] (int i)
00087   {
00088     CS_ASSERT (i >= 0 && i < num_edges);
00089     return edges[i];
00090   }
00091 
00095   csSegment2* GetFirst ()
00096   { if (num_edges<=0) return 0;  else return edges; }
00097 
00101   csSegment2* GetLast ()
00102   { if (num_edges<=0) return 0;  else return &edges[num_edges-1]; }
00103 
00107   bool In (const csVector2& v);
00108 
00112   static bool In (csSegment2* poly, int num_edge, const csVector2& v);
00113 
00117   void MakeRoom (int new_max);
00118 
00122   void SetEdgeCount (int n) { MakeRoom (n); num_edges = n; }
00123 
00128   int AddEdge (const csSegment2& e) { return AddEdge (e.Start (), e.End ()); }
00129 
00134   int AddEdge (const csVector2& v1, const csVector2& v2);
00135 
00146   void Intersect (const csPlane2& plane,
00147         csPoly2DEdges& left, csPoly2DEdges& right, bool& onplane) const;
00148 };
00149 
00156 class csPoly2DEdgesPool
00157 {
00158 private:
00159   struct PoolObj
00160   {
00161     PoolObj* next;
00162     csPoly2DEdges* pol2d;
00163   };
00165   PoolObj* alloced;
00167   PoolObj* freed;
00168 
00169 public:
00171   csPoly2DEdgesPool () : alloced (0), freed (0) { }
00172 
00174   ~csPoly2DEdgesPool ()
00175   {
00176     while (alloced)
00177     {
00178       PoolObj* n = alloced->next;
00179       //delete alloced->pol2d; @@@ This free is not valid!
00180       // We should use a ref count on the pool itself so that we
00181       // now when all objects in the pool are freed and the
00182       // 'alloced' list will be empty.
00183       delete alloced;
00184       alloced = n;
00185     }
00186     while (freed)
00187     {
00188       PoolObj* n = freed->next;
00189       delete freed->pol2d;
00190       delete freed;
00191       freed = n;
00192     }
00193   }
00194 
00196   csPoly2DEdges* Alloc ()
00197   {
00198     PoolObj* pnew;
00199     if (freed)
00200     {
00201       pnew = freed;
00202       freed = freed->next;
00203     }
00204     else
00205     {
00206       pnew = new PoolObj ();
00207       pnew->pol2d = new csPoly2DEdges ();
00208     }
00209     pnew->next = alloced;
00210     alloced = pnew;
00211     return pnew->pol2d;
00212   }
00213 
00219   void Free (csPoly2DEdges* pol)
00220   {
00221     if (alloced)
00222     {
00223       PoolObj* po = alloced;
00224       alloced = alloced->next;
00225       po->pol2d = pol;
00226       po->next = freed;
00227       freed = po;
00228     }
00229     else
00230     {
00231       // Cannot happen!
00232       CS_ASSERT (false);
00233     }
00234   }
00235 };
00236 
00239 #endif // __CS_POLYEDGE_H__

Generated for Crystal Space by doxygen 1.3.9.1