OpenCores
URL https://opencores.org/ocsvn/orsoc_graphics_accelerator/orsoc_graphics_accelerator/trunk

Subversion Repositories orsoc_graphics_accelerator

[/] [orsoc_graphics_accelerator/] [trunk/] [sw/] [utils/] [fonter/] [poly2tri/] [sweep/] [sweep_context.cc] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 maiden
/*
2
 * Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
3
 * http://code.google.com/p/poly2tri/
4
 *
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without modification,
8
 * are permitted provided that the following conditions are met:
9
 *
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.
18
 *
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.
30
 */
31
#include "sweep_context.h"
32
#include <algorithm>
33
#include "advancing_front.h"
34
 
35
namespace p2t {
36
 
37
SweepContext::SweepContext(std::vector<Point*> polyline)
38
{
39
  basin = Basin();
40
  edge_event = EdgeEvent();
41
 
42
  points_ = polyline;
43
 
44
  InitEdges(points_);
45
}
46
 
47
void SweepContext::AddHole(std::vector<Point*> polyline)
48
{
49
  InitEdges(polyline);
50
  for(unsigned int i = 0; i < polyline.size(); i++) {
51
    points_.push_back(polyline[i]);
52
  }
53
}
54
 
55
void SweepContext::AddPoint(Point* point) {
56
  points_.push_back(point);
57
}
58
 
59
std::vector<Triangle*> SweepContext::GetTriangles()
60
{
61
  return triangles_;
62
}
63
 
64
std::list<Triangle*> SweepContext::GetMap()
65
{
66
  return map_;
67
}
68
 
69
void SweepContext::InitTriangulation()
70
{
71
  double xmax(points_[0]->x), xmin(points_[0]->x);
72
  double ymax(points_[0]->y), ymin(points_[0]->y);
73
 
74
  // Calculate bounds.
75
  for (unsigned int i = 0; i < points_.size(); i++) {
76
    Point& p = *points_[i];
77
    if (p.x > xmax)
78
      xmax = p.x;
79
    if (p.x < xmin)
80
      xmin = p.x;
81
    if (p.y > ymax)
82
      ymax = p.y;
83
    if (p.y < ymin)
84
      ymin = p.y;
85
  }
86
 
87
  double dx = kAlpha * (xmax - xmin);
88
  double dy = kAlpha * (ymax - ymin);
89
  head_ = new Point(xmax + dx, ymin - dy);
90
  tail_ = new Point(xmin - dx, ymin - dy);
91
 
92
  // Sort points along y-axis
93
  std::sort(points_.begin(), points_.end(), cmp);
94
 
95
}
96
 
97
void SweepContext::InitEdges(std::vector<Point*> polyline)
98
{
99
  int num_points = polyline.size();
100
  for (int i = 0; i < num_points; i++) {
101
    int j = i < num_points - 1 ? i + 1 : 0;
102
    edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
103
  }
104
}
105
 
106
Point* SweepContext::GetPoint(const int& index)
107
{
108
  return points_[index];
109
}
110
 
111
void SweepContext::AddToMap(Triangle* triangle)
112
{
113
  map_.push_back(triangle);
114
}
115
 
116
Node& SweepContext::LocateNode(Point& point)
117
{
118
  // TODO implement search tree
119
  return *front_->LocateNode(point.x);
120
}
121
 
122
void SweepContext::CreateAdvancingFront(std::vector<Node*> nodes)
123
{
124
 
125
  (void) nodes;
126
  // Initial triangle
127
  Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
128
 
129
  map_.push_back(triangle);
130
 
131
  af_head_ = new Node(*triangle->GetPoint(1), *triangle);
132
  af_middle_ = new Node(*triangle->GetPoint(0), *triangle);
133
  af_tail_ = new Node(*triangle->GetPoint(2));
134
  front_ = new AdvancingFront(*af_head_, *af_tail_);
135
 
136
  // TODO: More intuitive if head is middles next and not previous?
137
  //       so swap head and tail
138
  af_head_->next = af_middle_;
139
  af_middle_->next = af_tail_;
140
  af_middle_->prev = af_head_;
141
  af_tail_->prev = af_middle_;
142
}
143
 
144
void SweepContext::RemoveNode(Node* node)
145
{
146
  delete node;
147
}
148
 
149
void SweepContext::MapTriangleToNodes(Triangle& t)
150
{
151
  for (int i = 0; i < 3; i++) {
152
    if (!t.GetNeighbor(i)) {
153
      Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i)));
154
      if (n)
155
        n->triangle = &t;
156
    }
157
  }
158
}
159
 
160
void SweepContext::RemoveFromMap(Triangle* triangle)
161
{
162
  map_.remove(triangle);
163
}
164
 
165
void SweepContext::MeshClean(Triangle& triangle)
166
{
167
  if (&triangle != NULL && !triangle.IsInterior()) {
168
    triangle.IsInterior(true);
169
    triangles_.push_back(&triangle);
170
    for (int i = 0; i < 3; i++) {
171
      if (!triangle.constrained_edge[i])
172
        MeshClean(*triangle.GetNeighbor(i));
173
    }
174
  }
175
}
176
 
177
SweepContext::~SweepContext()
178
{
179
 
180
    // Clean up memory
181
 
182
    delete head_;
183
    delete tail_;
184
    delete front_;
185
    delete af_head_;
186
    delete af_middle_;
187
    delete af_tail_;
188
 
189
    typedef std::list<Triangle*> type_list;
190
 
191
    for(type_list::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
192
        Triangle* ptr = *iter;
193
        delete ptr;
194
    }
195
 
196
     for(unsigned int i = 0; i < edge_list.size(); i++) {
197
        delete edge_list[i];
198
    }
199
 
200
}
201
 
202
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.