2
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
3
* http://code.google.com/p/poly2tri/
7
* Redistribution and use in source and binary forms, with or without modification,
8
* are permitted provided that the following conditions are met:
10
* * Redistributions of source code must retain the above copyright notice,
11
* this list of conditions and the following disclaimer.
12
* * Redistributions in binary form must reproduce the above copyright notice,
13
* this list of conditions and the following disclaimer in the documentation
14
* and/or other materials provided with the distribution.
15
* * Neither the name of Poly2Tri nor the names of its contributors may be
16
* used to endorse or promote products derived from this software without specific
17
* prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
#include "sweep_context.h"
33
#include "advancing_front.h"
37
SweepContext::SweepContext(const std::vector<Point*>& polyline) : points_(polyline),
48
void SweepContext::AddHole(const std::vector<Point*>& polyline)
51
for(unsigned int i = 0; i < polyline.size(); i++) {
52
points_.push_back(polyline[i]);
56
void SweepContext::AddPoint(Point* point) {
57
points_.push_back(point);
60
std::vector<Triangle*> &SweepContext::GetTriangles()
65
std::list<Triangle*> &SweepContext::GetMap()
70
void SweepContext::InitTriangulation()
72
double xmax(points_[0]->x), xmin(points_[0]->x);
73
double ymax(points_[0]->y), ymin(points_[0]->y);
76
for (unsigned int i = 0; i < points_.size(); i++) {
77
Point& p = *points_[i];
88
double dx = kAlpha * (xmax - xmin);
89
double dy = kAlpha * (ymax - ymin);
90
head_ = new Point(xmax + dx, ymin - dy);
91
tail_ = new Point(xmin - dx, ymin - dy);
93
// Sort points along y-axis
94
std::sort(points_.begin(), points_.end(), cmp);
98
void SweepContext::InitEdges(const std::vector<Point*>& polyline)
100
size_t num_points = polyline.size();
101
for (size_t i = 0; i < num_points; i++) {
102
size_t j = i < num_points - 1 ? i + 1 : 0;
103
edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
107
Point* SweepContext::GetPoint(size_t index)
109
return points_[index];
112
void SweepContext::AddToMap(Triangle* triangle)
114
map_.push_back(triangle);
117
Node& SweepContext::LocateNode(const Point& point)
119
// TODO implement search tree
120
return *front_->LocateNode(point.x);
123
void SweepContext::CreateAdvancingFront(const std::vector<Node*>& nodes)
128
Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
130
map_.push_back(triangle);
132
af_head_ = new Node(*triangle->GetPoint(1), *triangle);
133
af_middle_ = new Node(*triangle->GetPoint(0), *triangle);
134
af_tail_ = new Node(*triangle->GetPoint(2));
135
front_ = new AdvancingFront(*af_head_, *af_tail_);
137
// TODO: More intuitive if head is middles next and not previous?
138
// so swap head and tail
139
af_head_->next = af_middle_;
140
af_middle_->next = af_tail_;
141
af_middle_->prev = af_head_;
142
af_tail_->prev = af_middle_;
145
void SweepContext::RemoveNode(Node* node)
150
void SweepContext::MapTriangleToNodes(Triangle& t)
152
for (int i = 0; i < 3; i++) {
153
if (!t.GetNeighbor(i)) {
154
Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i)));
161
void SweepContext::RemoveFromMap(Triangle* triangle)
163
map_.remove(triangle);
166
void SweepContext::MeshClean(Triangle& triangle)
168
std::vector<Triangle *> triangles;
169
triangles.push_back(&triangle);
171
while(!triangles.empty()){
172
Triangle *t = triangles.back();
173
triangles.pop_back();
175
if (t != NULL && !t->IsInterior()) {
177
triangles_.push_back(t);
178
for (int i = 0; i < 3; i++) {
179
if (!t->constrained_edge[i])
180
triangles.push_back(t->GetNeighbor(i));
186
SweepContext::~SweepContext()
198
typedef std::list<Triangle*> type_list;
200
for(type_list::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
201
Triangle* ptr = *iter;
205
for(unsigned int i = 0; i < edge_list.size(); i++) {