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
|
// 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 "BspPolygon2.h"
#include "BspTree2.h"
#include "Wm5Memory.h"
using namespace Wm5;
//----------------------------------------------------------------------------
BspPolygon2::BspPolygon2 ()
{
mTree = 0;
}
//----------------------------------------------------------------------------
BspPolygon2::BspPolygon2 (const BspPolygon2& polygon)
{
mTree = 0;
*this = polygon;
}
//----------------------------------------------------------------------------
BspPolygon2::~BspPolygon2 ()
{
delete0(mTree);
}
//----------------------------------------------------------------------------
BspPolygon2& BspPolygon2::operator= (const BspPolygon2& polygon)
{
mVMap = polygon.mVMap;
mVArray = polygon.mVArray;
mEMap = polygon.mEMap;
mEArray = polygon.mEArray;
delete0(mTree);
mTree = (polygon.mTree ? polygon.mTree->GetCopy() : 0);
return *this;
}
//----------------------------------------------------------------------------
int BspPolygon2::InsertVertex (const Vector2d& vertex)
{
VIterator iter = mVMap.find(vertex);
if (iter != mVMap.end())
{
// Vertex already in map, just return its unique index.
return iter->second;
}
// Vertex not in map, insert it and assign it a unique index.
int i = (int)mVArray.size();
mVMap.insert(std::make_pair(vertex, i));
mVArray.push_back(vertex);
return i;
}
//----------------------------------------------------------------------------
int BspPolygon2::InsertEdge (const Edge2& edge)
{
assertion(edge.I0 != edge.I1, "Degenerate edges not allowed.\n");
EIterator iter = mEMap.find(edge);
if (iter != mEMap.end())
{
// Edge already in map, just return its unique index.
return iter->second;
}
// Edge not in map, insert it and assign it a unique index.
int i = (int)mEArray.size();
mEMap.insert(std::make_pair(edge, i));
mEArray.push_back(edge);
return i;
}
//----------------------------------------------------------------------------
void BspPolygon2::SplitEdge (int v0, int v1, int vmid)
{
// Find the edge in the map to get the edge-array index.
EIterator iter = mEMap.find(Edge2(v0, v1));
assertion(iter != mEMap.end(), "Edge does not exist in the map.\n");
int eIndex = iter->second;
// Delete edge <V0,V1>.
mEMap.erase(iter);
// Insert edge <V0,VM>.
mEArray[eIndex].I1 = vmid;
mEMap.insert(std::make_pair(mEArray[eIndex], eIndex));
// Insert edge <VM,V1>.
InsertEdge(Edge2(vmid, v1));
}
//----------------------------------------------------------------------------
void BspPolygon2::Finalize ()
{
delete0(mTree);
mTree = new0 BspTree2(*this, mEArray);
}
//----------------------------------------------------------------------------
int BspPolygon2::GetNumVertices () const
{
return (int)mVMap.size();
}
//----------------------------------------------------------------------------
bool BspPolygon2::GetVertex (int i, Vector2d& vertex) const
{
if (0 <= i && i < (int)mVArray.size())
{
vertex = mVArray[i];
return true;
}
return false;
}
//----------------------------------------------------------------------------
int BspPolygon2::GetNumEdges () const
{
return (int)mEMap.size();
}
//----------------------------------------------------------------------------
bool BspPolygon2::GetEdge (int i, Edge2& edge) const
{
if (0 <= i && i < (int)mEArray.size())
{
edge = mEArray[i];
return true;
}
return false;
}
//----------------------------------------------------------------------------
void BspPolygon2::GetInsideEdgesFrom (const BspPolygon2& polygon,
BspPolygon2& inside) const
{
assertion(mTree != 0, "Tree must exist.\n");
BspPolygon2 ignore;
const int numEdges = polygon.GetNumEdges();
for (int i = 0; i < numEdges; ++i)
{
int v0 = polygon.mEArray[i].I0;
int v1 = polygon.mEArray[i].I1;
Vector2d vertex0 = polygon.mVArray[v0];
Vector2d vertex1 = polygon.mVArray[v1];
mTree->GetPartition(*this, vertex0, vertex1, ignore, inside, inside,
ignore);
}
}
//----------------------------------------------------------------------------
BspPolygon2 BspPolygon2::operator~ () const
{
assertion(mTree != 0, "Tree must exist.\n");
// negation
BspPolygon2 neg;
neg.mVMap = mVMap;
neg.mVArray = mVArray;
ECIterator iter = mEMap.begin();
ECIterator end = mEMap.end();
for (/**/; iter != end; ++iter)
{
neg.InsertEdge(Edge2(iter->first.I1, iter->first.I0));
}
neg.mTree = mTree->GetCopy();
neg.mTree->Negate();
return neg;
}
//----------------------------------------------------------------------------
BspPolygon2 BspPolygon2::operator& (const BspPolygon2& polygon) const
{
assertion(mTree != 0, "Tree must exist.\n");
// intersection
BspPolygon2 intersect;
GetInsideEdgesFrom(polygon, intersect);
polygon.GetInsideEdgesFrom(*this, intersect);
intersect.Finalize();
return intersect;
}
//----------------------------------------------------------------------------
BspPolygon2 BspPolygon2::operator| (const BspPolygon2& polygon) const
{
// union
const BspPolygon2& thisPolygon = *this;
return ~(~thisPolygon & ~polygon);
}
//----------------------------------------------------------------------------
BspPolygon2 BspPolygon2::operator- (const BspPolygon2& polygon) const
{
// difference
const BspPolygon2& thisPolygon = *this;
return thisPolygon & ~polygon;
}
//----------------------------------------------------------------------------
BspPolygon2 BspPolygon2::operator^ (const BspPolygon2& polygon) const
{
// exclusive or
const BspPolygon2& thisPolygon = *this;
return (thisPolygon - polygon) | (polygon - thisPolygon);
}
//----------------------------------------------------------------------------
int BspPolygon2::PointLocation (const Vector2d& vertex) const
{
assertion(mTree != 0, "Tree must exist.\n");
return mTree->PointLocation(*this, vertex);
}
//----------------------------------------------------------------------------
void BspPolygon2::Print (const char* filename) const
{
std::ofstream outFile(filename);
const int numVertices = (int)mVArray.size();
outFile << "vquantity = " << numVertices << std::endl;
int i;
for (i = 0; i < numVertices; ++i)
{
outFile << i << " (" << mVArray[i].X() << ',' << mVArray[i].Y()
<< ')' << std::endl;
}
outFile << std::endl;
const int numEdges = (int)mEArray.size();
outFile << "equantity = " << numEdges << std::endl;
for (i = 0; i < numEdges; ++i)
{
outFile << " <" << mEArray[i].I0 << ',' << mEArray[i].I1
<< '>' << std::endl;
}
outFile << std::endl;
outFile << "bsp tree" << std::endl;
if (mTree)
{
mTree->Print(outFile, 0, 'r');
}
outFile << std::endl;
}
//----------------------------------------------------------------------------
|