1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)
#include "SegmentGraph.h"
#include "Wm5Memory.h"
//----------------------------------------------------------------------------
SegmentGraph::SegmentGraph ()
{
}
//----------------------------------------------------------------------------
SegmentGraph::~SegmentGraph ()
{
VMap::iterator iter = mVertexMap.begin();
VMap::iterator end = mVertexMap.end();
for (/**/; iter != end; ++iter)
{
Vertex* vertex = iter->second;
delete0(vertex);
}
}
//----------------------------------------------------------------------------
SegmentGraph::VMap& SegmentGraph::GetVertices ()
{
return mVertexMap;
}
//----------------------------------------------------------------------------
SegmentGraph::ESet& SegmentGraph::GetEdges ()
{
return mEdgeSet;
}
//----------------------------------------------------------------------------
void SegmentGraph::InsertEdge (const RPoint2& point0, const RPoint2& point1)
{
// Insert vertices into the vertex set. If the points already exist,
// the function call is just a lookup; otherwise, new vertices are
// allocated.
Vertex* vertex0 = InsertVertex(point0);
Vertex* vertex1 = InsertVertex(point1);
Edge edge(vertex0, vertex1);
std::set<Edge>::iterator iter = mEdgeSet.find(edge);
if (iter == mEdgeSet.end())
{
// The edge does not exist, insert it into the set.
mEdgeSet.insert(edge);
// Update the vertex-adjacency information.
vertex0->InsertAdjacent(vertex1);
vertex1->InsertAdjacent(vertex0);
}
}
//----------------------------------------------------------------------------
bool SegmentGraph::RemoveEdge (const RPoint2& point0, const RPoint2& point1)
{
// Look up the vertices. If one or the other does not exist, there is
// nothing to do.
Vertex* vertex0 = GetVertex(point0);
if (!vertex0)
{
return false;
}
Vertex* vertex1 = GetVertex(point1);
if (!vertex1)
{
return false;
}
Edge edge(vertex0, vertex1);
std::set<Edge>::iterator iter = mEdgeSet.find(edge);
if (iter != mEdgeSet.end())
{
// The edge exists, remove it from the set.
mEdgeSet.erase(edge);
// Update the vertex-adjacency information.
vertex0->RemoveAdjacent(vertex1);
vertex1->RemoveAdjacent(vertex0);
return true;
}
return false;
}
//----------------------------------------------------------------------------
void SegmentGraph::ExtractEnvelope (std::vector<RPoint2>& envelope)
{
// Get the left-most (min x) and bottom-most (min y) vertex of the graph.
VMap::iterator iter = mVertexMap.begin();
VMap::iterator end = mVertexMap.end();
Vertex* vMin = iter->second;
for (++iter; iter != end; ++iter)
{
Vertex* vertex = iter->second;
if (vertex->Position.X() < vMin->Position.X())
{
vMin = vertex;
}
else if (vertex->Position.X() == vMin->Position.X())
{
if (vertex->Position.Y() < vMin->Position.Y())
{
vMin = vertex;
}
}
}
// Traverse the envelope. If Vcurr is the current vertex and Vprev is
// its predecessor in the envelope, then the successor Vnext is selected
// to be a vertex adjacent to Vcurr according to the following. The
// current edge has direction Dcurr = Vcurr - Vprev. Vnext is an adjacent
// vertex of Vcurr, selected so that the direction Dnext = Vnext - Vcurr
// has the property that Dnext forms the largest interior angle at Vcurr
// compared to -Dcurr, the angle measured by rotating -Dcurr clockwise
// about Vcurr. The starting vertex Vmin does not have a predecessor, but
// the construction of Vmin implies a starting direction Dcurr = (0,-1).
Vertex* vCurr = vMin;
Vertex* vPrev = 0;
Vertex* vNext = 0;
RPoint2 dirCurr(0,-1), dirNext;
bool vCurrConvex = true;
// The loop could be a 'while' loop, terminating when the Vnext becomes
// Vmin, but to trap potential errors that cause an infinite loop, a for
// loop is used to guarantee bounded iterations. The upper bound of the
// loop is the worst-case behavior (each vertex connected to all other
// vertices).
const int numVertices = (int)mVertexMap.size();
const int maxNumVertices = numVertices*(numVertices - 1)/2;
int i;
for (i = 0; i < maxNumVertices; ++i)
{
envelope.push_back(vCurr->Position);
// Search the adjacent vertices for Vnext.
for (int j = 0; j < vCurr->NumVertices; ++j)
{
Vertex* vertex = vCurr->V[j];
if (vertex == vPrev)
{
continue;
}
RPoint2 dir = vertex->Position - vCurr->Position;
if (!vNext)
{
vNext = vertex;
dirNext = dir;
vCurrConvex = (dirNext.DotPerp(dirCurr) <= 0);
continue;
}
if (vCurrConvex)
{
if (dirCurr.DotPerp(dir) < 0 || dirNext.DotPerp(dir) < 0)
{
vNext = vertex;
dirNext = dir;
vCurrConvex = (dirNext.DotPerp(dirCurr) <= 0);
}
}
else
{
if (dirCurr.DotPerp(dir) < 0 && dirNext.DotPerp(dir) < 0)
{
vNext = vertex;
dirNext = dir;
vCurrConvex = (dirNext.DotPerp(dirCurr) <= 0);
}
}
}
assertion(vNext != 0, "Unexpected condition.\n");
vPrev = vCurr;
vCurr = vNext;
vNext = 0;
dirCurr = dirNext;
if (vCurr == vMin)
{
break;
}
}
assertion(i < numVertices, "Unexpected condition.\n");
}
//----------------------------------------------------------------------------
SegmentGraph::Vertex* SegmentGraph::GetVertex (const RPoint2& point)
{
VMap::iterator iter = mVertexMap.find(point);
return (iter != mVertexMap.end() ? iter->second : 0);
}
//----------------------------------------------------------------------------
SegmentGraph::Vertex* SegmentGraph::InsertVertex (const RPoint2& point)
{
VMap::iterator iter = mVertexMap.find(point);
if (iter != mVertexMap.end())
{
return iter->second;
}
// Insert the vertex into the vertex set. The adjacency array has already
// been initialized to empty.
Vertex* vertex = new0 Vertex();
vertex->Position = point;
mVertexMap[point] = vertex;
return vertex;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// SegmentGraph::Vertex
//----------------------------------------------------------------------------
SegmentGraph::Vertex::Vertex ()
:
NumVertices(0),
V(0)
{
// Position is uninitialized.
}
//----------------------------------------------------------------------------
SegmentGraph::Vertex::~Vertex ()
{
delete1(V);
}
//----------------------------------------------------------------------------
void SegmentGraph::Vertex::InsertAdjacent (Vertex* adjacent)
{
// Grow the vertex array if necessary.
if ((NumVertices % VG_CHUNK) == 0)
{
size_t numBytes = NumVertices*sizeof(Vertex*);
Vertex** save = V;
V = new1<Vertex*>(NumVertices + VG_CHUNK);
if (save)
{
memcpy(V, save, numBytes);
delete1(save);
}
}
V[NumVertices++] = adjacent;
}
//----------------------------------------------------------------------------
void SegmentGraph::Vertex::RemoveAdjacent (Vertex* adjacent)
{
for (int i = 0; i < NumVertices; ++i)
{
if (adjacent == V[i])
{
// Maintain a compact array.
if (i < --NumVertices)
{
V[i] = V[NumVertices];
}
V[NumVertices] = 0;
return;
}
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// SegmentGraph::Edge
//----------------------------------------------------------------------------
SegmentGraph::Edge::Edge ()
{
mV[0] = 0;
mV[1] = 0;
}
//----------------------------------------------------------------------------
SegmentGraph::Edge::Edge (Vertex* vertex0, Vertex* vertex1)
{
SetVertices(vertex0, vertex1);
}
//----------------------------------------------------------------------------
void SegmentGraph::Edge::SetVertices (Vertex* vertex0, Vertex* vertex1)
{
if (vertex0 < vertex1)
{
// V0 is minimum.
mV[0] = vertex0;
mV[1] = vertex1;
}
else
{
// V1 is minimum.
mV[0] = vertex1;
mV[1] = vertex0;
}
}
//----------------------------------------------------------------------------
SegmentGraph::Vertex* SegmentGraph::Edge::GetVertex (int i) const
{
return mV[i];
}
//----------------------------------------------------------------------------
bool SegmentGraph::Edge::operator== (const Edge& edge) const
{
return mV[0] == edge.mV[0] && mV[1] == edge.mV[1];
}
//----------------------------------------------------------------------------
bool SegmentGraph::Edge::operator< (const Edge& edge) const
{
if (mV[1] < edge.mV[1])
{
return true;
}
if (mV[1] > edge.mV[1])
{
return false;
}
return mV[0] < edge.mV[0];
}
//----------------------------------------------------------------------------
|