csgeom/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 "csgeom/math2d.h" 00029 #include "csgeom/segment.h" 00030 00035 class csPoly2DEdges 00036 { 00037 protected: 00039 csSegment2* edges; 00041 int num_edges; 00043 int max_edges; 00044 00045 public: 00049 csPoly2DEdges (int start_size = 10); 00050 00052 csPoly2DEdges (csPoly2DEdges& copy); 00053 00055 virtual ~csPoly2DEdges (); 00056 00060 void MakeEmpty (); 00061 00065 int GetEdgeCount () { return num_edges; } 00066 00070 csSegment2* GetEdges () { return edges; } 00071 00075 csSegment2* GetEdge (int i) 00076 { 00077 if (i<0 || i>=num_edges) return 0; 00078 return &edges[i]; 00079 } 00080 00084 csSegment2& operator[] (int i) 00085 { 00086 CS_ASSERT (i >= 0 && i < num_edges); 00087 return edges[i]; 00088 } 00089 00093 csSegment2* GetFirst () 00094 { if (num_edges<=0) return 0; else return edges; } 00095 00099 csSegment2* GetLast () 00100 { if (num_edges<=0) return 0; else return &edges[num_edges-1]; } 00101 00105 bool In (const csVector2& v); 00106 00110 static bool In (csSegment2* poly, int num_edge, const csVector2& v); 00111 00115 void MakeRoom (int new_max); 00116 00120 void SetEdgeCount (int n) { MakeRoom (n); num_edges = n; } 00121 00126 int AddEdge (const csSegment2& e) { return AddEdge (e.Start (), e.End ()); } 00127 00132 int AddEdge (const csVector2& v1, const csVector2& v2); 00133 00144 void Intersect (const csPlane2& plane, 00145 csPoly2DEdges& left, csPoly2DEdges& right, bool& onplane) const; 00146 }; 00147 00154 class csPoly2DEdgesPool 00155 { 00156 private: 00157 struct PoolObj 00158 { 00159 PoolObj* next; 00160 csPoly2DEdges* pol2d; 00161 }; 00163 PoolObj* alloced; 00165 PoolObj* freed; 00166 00167 public: 00169 csPoly2DEdgesPool () : alloced (0), freed (0) { } 00170 00172 ~csPoly2DEdgesPool () 00173 { 00174 while (alloced) 00175 { 00176 PoolObj* n = alloced->next; 00177 //delete alloced->pol2d; @@@ This free is not valid! 00178 // We should use a ref count on the pool itself so that we 00179 // now when all objects in the pool are freed and the 00180 // 'alloced' list will be empty. 00181 delete alloced; 00182 alloced = n; 00183 } 00184 while (freed) 00185 { 00186 PoolObj* n = freed->next; 00187 delete freed->pol2d; 00188 delete freed; 00189 freed = n; 00190 } 00191 } 00192 00194 csPoly2DEdges* Alloc () 00195 { 00196 PoolObj* pnew; 00197 if (freed) 00198 { 00199 pnew = freed; 00200 freed = freed->next; 00201 } 00202 else 00203 { 00204 pnew = new PoolObj (); 00205 pnew->pol2d = new csPoly2DEdges (); 00206 } 00207 pnew->next = alloced; 00208 alloced = pnew; 00209 return pnew->pol2d; 00210 } 00211 00217 void Free (csPoly2DEdges* pol) 00218 { 00219 if (alloced) 00220 { 00221 PoolObj* po = alloced; 00222 alloced = alloced->next; 00223 po->pol2d = pol; 00224 po->next = freed; 00225 freed = po; 00226 } 00227 else 00228 { 00229 // Cannot happen! 00230 CS_ASSERT (false); 00231 } 00232 } 00233 }; 00234 00237 #endif // __CS_POLYEDGE_H__
Generated for Crystal Space by doxygen 1.2.18