|
polyedge.h00001 /* 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 00022 #include "csgeom/math2d.h" 00023 #include "csgeom/segment.h" 00024 00029 class csPoly2DEdges 00030 { 00031 protected: 00033 csSegment2* edges; 00035 int num_edges; 00037 int max_edges; 00038 00039 public: 00043 csPoly2DEdges (int start_size = 10); 00044 00046 csPoly2DEdges (csPoly2DEdges& copy); 00047 00049 virtual ~csPoly2DEdges (); 00050 00054 void MakeEmpty (); 00055 00059 int GetEdgeCount () { return num_edges; } 00060 00064 csSegment2* GetEdges () { return edges; } 00065 00069 csSegment2* GetEdge (int i) 00070 { 00071 if (i<0 || i>=num_edges) return NULL; 00072 return &edges[i]; 00073 } 00074 00078 csSegment2& operator[] (int i) 00079 { 00080 CS_ASSERT (i >= 0 && i < num_edges); 00081 return edges[i]; 00082 } 00083 00087 csSegment2* GetFirst () 00088 { if (num_edges<=0) return NULL; else return edges; } 00089 00093 csSegment2* GetLast () 00094 { if (num_edges<=0) return NULL; else return &edges[num_edges-1]; } 00095 00099 bool In (const csVector2& v); 00100 00104 static bool In (csSegment2* poly, int num_edge, const csVector2& v); 00105 00109 void MakeRoom (int new_max); 00110 00114 void SetEdgeCount (int n) { MakeRoom (n); num_edges = n; } 00115 00120 int AddEdge (const csSegment2& e) { return AddEdge (e.Start (), e.End ()); } 00121 00126 int AddEdge (const csVector2& v1, const csVector2& v2); 00127 00138 void Intersect (const csPlane2& plane, 00139 csPoly2DEdges& left, csPoly2DEdges& right, bool& onplane) const; 00140 }; 00141 00148 class csPoly2DEdgesPool 00149 { 00150 private: 00151 struct PoolObj 00152 { 00153 PoolObj* next; 00154 csPoly2DEdges* pol2d; 00155 }; 00157 PoolObj* alloced; 00159 PoolObj* freed; 00160 00161 public: 00163 csPoly2DEdgesPool () : alloced (NULL), freed (NULL) { } 00164 00166 ~csPoly2DEdgesPool () 00167 { 00168 while (alloced) 00169 { 00170 PoolObj* n = alloced->next; 00171 //delete alloced->pol2d; @@@ This free is not valid! 00172 // We should use a ref count on the pool itself so that we 00173 // now when all objects in the pool are freed and the 00174 // 'alloced' list will be empty. 00175 delete alloced; 00176 alloced = n; 00177 } 00178 while (freed) 00179 { 00180 PoolObj* n = freed->next; 00181 delete freed->pol2d; 00182 delete freed; 00183 freed = n; 00184 } 00185 } 00186 00188 csPoly2DEdges* Alloc () 00189 { 00190 PoolObj* pnew; 00191 if (freed) 00192 { 00193 pnew = freed; 00194 freed = freed->next; 00195 } 00196 else 00197 { 00198 pnew = new PoolObj (); 00199 pnew->pol2d = new csPoly2DEdges (); 00200 } 00201 pnew->next = alloced; 00202 alloced = pnew; 00203 return pnew->pol2d; 00204 } 00205 00211 void Free (csPoly2DEdges* pol) 00212 { 00213 if (alloced) 00214 { 00215 PoolObj* po = alloced; 00216 alloced = alloced->next; 00217 po->pol2d = pol; 00218 po->next = freed; 00219 freed = po; 00220 } 00221 else 00222 { 00223 // Cannot happen! 00224 CS_ASSERT (false); 00225 } 00226 } 00227 }; 00228 00229 #endif // __CS_POLYEDGE_H__ Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000 |