Code

43195080183e19c5a1365f9a51b7509e95ef5a1c
[inkscape.git] / src / libavoid / shape.cpp
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libavoid - Fast, Incremental, Object-avoiding Line Router
5  * Copyright (C) 2004-2005  Michael Wybrow <mjwybrow@users.sourceforge.net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  * 
21 */
24 #include "libavoid/graph.h"  // For alertConns
25 #include "libavoid/polyutil.h"
28 namespace Avoid {
31 ShapeRefList shapeRefs;
34 ShapeRef::ShapeRef(unsigned int id, Polygn& ply)
35     : _id(id)
36     , _poly(copyPoly(ply))
37     , _active(false)
38     , _firstVert(NULL)
39     , _lastVert(NULL)
40 {
41     bool isShape = true;
42     VertID i = VertID(id, isShape, 0);
43     
44     VertInf *last = NULL;
45     VertInf *node = NULL;
46     for (int pt_i = 0; pt_i < _poly.pn; pt_i++)
47     {
48         node = new VertInf(i, _poly.ps[pt_i]);
50         if (!_firstVert)
51         {
52             _firstVert = node;
53         }
54         else
55         {
56             node->shPrev = last;
57             last->shNext = node;
58             //node->lstPrev = last;
59             //last->lstNext = node;
60         }
61         vertices.addVertex(node);
62         
63         last = node;
64         i++;
65         // Increase total vertices count ++;
66     }
67     _lastVert = node;
68     
69     _lastVert->shNext = _firstVert;
70     _firstVert->shPrev = _lastVert;
71     
72     // Increase total shape count ++;
73     makeActive();
74 }
77 ShapeRef::~ShapeRef()
78 {
79     assert(_firstVert != NULL);
80     
81     VertInf *it = _firstVert;
82     do
83     {
84         VertInf *tmp = it;
85         it = it->shNext;
87         // XXX: This could possibly be done less
88         //      safely but faster, all at once.
89         vertices.removeVertex(tmp);
90         delete tmp;
91     }
92     while (it != _firstVert);
93     _firstVert = _lastVert = NULL;
95     freePoly(_poly);
96     
97     makeInactive();
98 }
101 void ShapeRef::makeActive(void)
103     assert(!_active);
104     
105     // Add to connRefs list.
106     _pos = shapeRefs.insert(shapeRefs.begin(), this);
107     _active = true;
111 void ShapeRef::makeInactive(void)
113     assert(_active);
114     
115     // Remove from connRefs list.
116     shapeRefs.erase(_pos);
117     _active = false;
119     
121 VertInf *ShapeRef::firstVert(void)
123     return _firstVert;
127 VertInf *ShapeRef::lastVert(void)
129     return _lastVert;
133 unsigned int ShapeRef::id(void)
135     return _id;
139 Polygn ShapeRef::poly(void)
141     return _poly;
145 void ShapeRef::removeFromGraph(void)
147     for (VertInf *iter = firstVert(); iter != lastVert()->lstNext; )
148     {
149         VertInf *tmp = iter;
150         iter = iter->lstNext;
151         
152         // For each vertex.
153         EdgeInfList& visList = tmp->visList;
154         EdgeInfList::iterator finish = visList.end();
155         EdgeInfList::iterator edge;
156         while ((edge = visList.begin()) != finish)
157         {
158             // Remove each visibility edge
159             (*edge)->alertConns();
160             delete (*edge);
161         }
163         EdgeInfList& invisList = tmp->invisList;
164         finish = invisList.end();
165         while ((edge = invisList.begin()) != finish)
166         {
167             // Remove each invisibility edge
168             delete (*edge);
169         }
170     }