## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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
    00029 #include "csextern.h"
    00030
    00031 #include "csgeom/math2d.h"
    00032 #include "csgeom/segment.h"
    00033
    00038 class CS_CRYSTALSPACE_EXPORT csPoly2DEdges
    00039 {
    00040 protected:
    00042   csSegment2* edges;
    00044   int num_edges;
    00046   int max_edges;
    00047
    00048 public:
    00052   csPoly2DEdges (int start_size = 10);
    00053
    00055   csPoly2DEdges (csPoly2DEdges& copy);
    00056
    00058   virtual ~csPoly2DEdges ();
    00059
    00063   void MakeEmpty ();
    00064
    00068   int GetEdgeCount () { return num_edges; }
    00069
    00073   csSegment2* GetEdges () { return edges; }
    00074
    00078   csSegment2* GetEdge (int i)
    00079   {
    00080     if (i<0 || i>=num_edges) return 0;
    00081     return &edges[i];
    00082   }
    00083
    00087   csSegment2& operator[] (int i)
    00088   {
    00089     CS_ASSERT (i >= 0 && i < num_edges);
    00090     return edges[i];
    00091   }
    00092
    00096   csSegment2* GetFirst ()
    00097   { if (num_edges<=0) return 0;  else return edges; }
    00098
    00102   csSegment2* GetLast ()
    00103   { if (num_edges<=0) return 0;  else return &edges[num_edges-1]; }
    00104
    00108   bool In (const csVector2& v);
    00109
    00113   static bool In (csSegment2* poly, int num_edge, const csVector2& v);
    00114
    00118   void MakeRoom (int new_max);
    00119
    00123   void SetEdgeCount (int n) { MakeRoom (n); num_edges = n; }
    00124
    00129   int AddEdge (const csSegment2& e) { return AddEdge (e.Start (), e.End ()); }
    00130
    00135   int AddEdge (const csVector2& v1, const csVector2& v2);
    00136
    00147   void Intersect (const csPlane2& plane,
    00148         csPoly2DEdges& left, csPoly2DEdges& right, bool& onplane) const;
    00149 };
    00150
    00157 class csPoly2DEdgesPool
    00158 {
    00159 private:
    00160   struct PoolObj
    00161   {
    00162     PoolObj* next;
    00163     csPoly2DEdges* pol2d;
    00164   };
    00166   PoolObj* alloced;
    00168   PoolObj* freed;
    00169
    00170 public:
    00172   csPoly2DEdgesPool () : alloced (0), freed (0) { }
    00173
    00175   ~csPoly2DEdgesPool ()
    00176   {
    00177     while (alloced)
    00178     {
    00179       PoolObj* n = alloced->next;
    00180       //delete alloced->pol2d; @@@ This free is not valid!
    00181       // We should use a ref count on the pool itself so that we
    00182       // now when all objects in the pool are freed and the
    00183       // 'alloced' list will be empty.
    00184       delete alloced;
    00185       alloced = n;
    00186     }
    00187     while (freed)
    00188     {
    00189       PoolObj* n = freed->next;
    00190       delete freed->pol2d;
    00191       delete freed;
    00192       freed = n;
    00193     }
    00194   }
    00195
    00197   csPoly2DEdges* Alloc ()
    00198   {
    00199     PoolObj* pnew;
    00200     if (freed)
    00201     {
    00202       pnew = freed;
    00203       freed = freed->next;
    00204     }
    00205     else
    00206     {
    00207       pnew = new PoolObj ();
    00208       pnew->pol2d = new csPoly2DEdges ();
    00209     }
    00210     pnew->next = alloced;
    00211     alloced = pnew;
    00212     return pnew->pol2d;
    00213   }
    00214
    00220   void Free (csPoly2DEdges* pol)
    00221   {
    00222     if (alloced)
    00223     {
    00224       PoolObj* po = alloced;
    00225       alloced = alloced->next;
    00226       po->pol2d = pol;
    00227       po->next = freed;
    00228       freed = po;
    00229     }
    00230     else
    00231     {
    00232       // Cannot happen!
    00233       CS_ASSERT (false);
    00234     }
    00235   }
    00236 };
    00237
    00240 #endif // __CS_POLYEDGE_H__