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/] [common/] [shapes.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 "shapes.h"
32
#include <iostream>
33
 
34
namespace p2t {
35
 
36
Triangle::Triangle(Point& a, Point& b, Point& c)
37
{
38
  points_[0] = &a; points_[1] = &b; points_[2] = &c;
39
  neighbors_[0] = NULL; neighbors_[1] = NULL; neighbors_[2] = NULL;
40
  constrained_edge[0] = constrained_edge[1] = constrained_edge[2] = false;
41
  delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
42
  interior_ = false;
43
}
44
 
45
// Update neighbor pointers
46
void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t)
47
{
48
  if ((p1 == points_[2] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[2]))
49
    neighbors_[0] = t;
50
  else if ((p1 == points_[0] && p2 == points_[2]) || (p1 == points_[2] && p2 == points_[0]))
51
    neighbors_[1] = t;
52
  else if ((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0]))
53
    neighbors_[2] = t;
54
  else
55
    assert(0);
56
}
57
 
58
// Exhaustive search to update neighbor pointers
59
void Triangle::MarkNeighbor(Triangle& t)
60
{
61
  if (t.Contains(points_[1], points_[2])) {
62
    neighbors_[0] = &t;
63
    t.MarkNeighbor(points_[1], points_[2], this);
64
  } else if (t.Contains(points_[0], points_[2])) {
65
    neighbors_[1] = &t;
66
    t.MarkNeighbor(points_[0], points_[2], this);
67
  } else if (t.Contains(points_[0], points_[1])) {
68
    neighbors_[2] = &t;
69
    t.MarkNeighbor(points_[0], points_[1], this);
70
  }
71
}
72
 
73
/**
74
 * Clears all references to all other triangles and points
75
 */
76
void Triangle::Clear()
77
{
78
    Triangle *t;
79
    for( int i=0; i<3; i++ )
80
    {
81
        t = neighbors_[i];
82
        if( t != NULL )
83
        {
84
            t->ClearNeighbor( this );
85
        }
86
    }
87
    ClearNeighbors();
88
    points_[0]=points_[1]=points_[2] = NULL;
89
}
90
 
91
void Triangle::ClearNeighbor(Triangle *triangle )
92
{
93
    if( neighbors_[0] == triangle )
94
    {
95
        neighbors_[0] = NULL;
96
    }
97
    else if( neighbors_[1] == triangle )
98
    {
99
        neighbors_[1] = NULL;
100
    }
101
    else
102
    {
103
        neighbors_[2] = NULL;
104
    }
105
}
106
 
107
void Triangle::ClearNeighbors()
108
{
109
  neighbors_[0] = NULL;
110
  neighbors_[1] = NULL;
111
  neighbors_[2] = NULL;
112
}
113
 
114
void Triangle::ClearDelunayEdges()
115
{
116
  delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
117
}
118
 
119
Point* Triangle::OppositePoint(Triangle& t, Point& p)
120
{
121
  Point *cw = t.PointCW(p);
122
/*  double x = cw->x;
123
  double y = cw->y;
124
  x = p.x;
125
  y = p.y;*/
126
  return PointCW(*cw);
127
}
128
 
129
// Legalized triangle by rotating clockwise around point(0)
130
void Triangle::Legalize(Point& point)
131
{
132
  points_[1] = points_[0];
133
  points_[0] = points_[2];
134
  points_[2] = &point;
135
}
136
 
137
// Legalize triagnle by rotating clockwise around oPoint
138
void Triangle::Legalize(Point& opoint, Point& npoint)
139
{
140
  if (&opoint == points_[0]) {
141
    points_[1] = points_[0];
142
    points_[0] = points_[2];
143
    points_[2] = &npoint;
144
  } else if (&opoint == points_[1]) {
145
    points_[2] = points_[1];
146
    points_[1] = points_[0];
147
    points_[0] = &npoint;
148
  } else if (&opoint == points_[2]) {
149
    points_[0] = points_[2];
150
    points_[2] = points_[1];
151
    points_[1] = &npoint;
152
  } else {
153
    assert(0);
154
  }
155
}
156
 
157
int Triangle::Index(const Point* p)
158
{
159
  if (p == points_[0]) {
160
    return 0;
161
  } else if (p == points_[1]) {
162
    return 1;
163
  } else if (p == points_[2]) {
164
    return 2;
165
  }
166
  assert(0);
167
}
168
 
169
int Triangle::EdgeIndex(const Point* p1, const Point* p2)
170
{
171
  if (points_[0] == p1) {
172
    if (points_[1] == p2) {
173
      return 2;
174
    } else if (points_[2] == p2) {
175
      return 1;
176
    }
177
  } else if (points_[1] == p1) {
178
    if (points_[2] == p2) {
179
      return 0;
180
    } else if (points_[0] == p2) {
181
      return 2;
182
    }
183
  } else if (points_[2] == p1) {
184
    if (points_[0] == p2) {
185
      return 1;
186
    } else if (points_[1] == p2) {
187
      return 0;
188
    }
189
  }
190
  return -1;
191
}
192
 
193
void Triangle::MarkConstrainedEdge(const int index)
194
{
195
  constrained_edge[index] = true;
196
}
197
 
198
void Triangle::MarkConstrainedEdge(Edge& edge)
199
{
200
  MarkConstrainedEdge(edge.p, edge.q);
201
}
202
 
203
// Mark edge as constrained
204
void Triangle::MarkConstrainedEdge(Point* p, Point* q)
205
{
206
  if ((q == points_[0] && p == points_[1]) || (q == points_[1] && p == points_[0])) {
207
    constrained_edge[2] = true;
208
  } else if ((q == points_[0] && p == points_[2]) || (q == points_[2] && p == points_[0])) {
209
    constrained_edge[1] = true;
210
  } else if ((q == points_[1] && p == points_[2]) || (q == points_[2] && p == points_[1])) {
211
    constrained_edge[0] = true;
212
  }
213
}
214
 
215
// The point counter-clockwise to given point
216
Point* Triangle::PointCW(Point& point)
217
{
218
  if (&point == points_[0]) {
219
    return points_[2];
220
  } else if (&point == points_[1]) {
221
    return points_[0];
222
  } else if (&point == points_[2]) {
223
    return points_[1];
224
  }
225
  assert(0);
226
}
227
 
228
// The point counter-clockwise to given point
229
Point* Triangle::PointCCW(Point& point)
230
{
231
  if (&point == points_[0]) {
232
    return points_[1];
233
  } else if (&point == points_[1]) {
234
    return points_[2];
235
  } else if (&point == points_[2]) {
236
    return points_[0];
237
  }
238
  assert(0);
239
}
240
 
241
// The neighbor clockwise to given point
242
Triangle* Triangle::NeighborCW(Point& point)
243
{
244
  if (&point == points_[0]) {
245
    return neighbors_[1];
246
  } else if (&point == points_[1]) {
247
    return neighbors_[2];
248
  }
249
  return neighbors_[0];
250
}
251
 
252
// The neighbor counter-clockwise to given point
253
Triangle* Triangle::NeighborCCW(Point& point)
254
{
255
  if (&point == points_[0]) {
256
    return neighbors_[2];
257
  } else if (&point == points_[1]) {
258
    return neighbors_[0];
259
  }
260
  return neighbors_[1];
261
}
262
 
263
bool Triangle::GetConstrainedEdgeCCW(Point& p)
264
{
265
  if (&p == points_[0]) {
266
    return constrained_edge[2];
267
  } else if (&p == points_[1]) {
268
    return constrained_edge[0];
269
  }
270
  return constrained_edge[1];
271
}
272
 
273
bool Triangle::GetConstrainedEdgeCW(Point& p)
274
{
275
  if (&p == points_[0]) {
276
    return constrained_edge[1];
277
  } else if (&p == points_[1]) {
278
    return constrained_edge[2];
279
  }
280
  return constrained_edge[0];
281
}
282
 
283
void Triangle::SetConstrainedEdgeCCW(Point& p, bool ce)
284
{
285
  if (&p == points_[0]) {
286
    constrained_edge[2] = ce;
287
  } else if (&p == points_[1]) {
288
    constrained_edge[0] = ce;
289
  } else {
290
    constrained_edge[1] = ce;
291
  }
292
}
293
 
294
void Triangle::SetConstrainedEdgeCW(Point& p, bool ce)
295
{
296
  if (&p == points_[0]) {
297
    constrained_edge[1] = ce;
298
  } else if (&p == points_[1]) {
299
    constrained_edge[2] = ce;
300
  } else {
301
    constrained_edge[0] = ce;
302
  }
303
}
304
 
305
bool Triangle::GetDelunayEdgeCCW(Point& p)
306
{
307
  if (&p == points_[0]) {
308
    return delaunay_edge[2];
309
  } else if (&p == points_[1]) {
310
    return delaunay_edge[0];
311
  }
312
  return delaunay_edge[1];
313
}
314
 
315
bool Triangle::GetDelunayEdgeCW(Point& p)
316
{
317
  if (&p == points_[0]) {
318
    return delaunay_edge[1];
319
  } else if (&p == points_[1]) {
320
    return delaunay_edge[2];
321
  }
322
  return delaunay_edge[0];
323
}
324
 
325
void Triangle::SetDelunayEdgeCCW(Point& p, bool e)
326
{
327
  if (&p == points_[0]) {
328
    delaunay_edge[2] = e;
329
  } else if (&p == points_[1]) {
330
    delaunay_edge[0] = e;
331
  } else {
332
    delaunay_edge[1] = e;
333
  }
334
}
335
 
336
void Triangle::SetDelunayEdgeCW(Point& p, bool e)
337
{
338
  if (&p == points_[0]) {
339
    delaunay_edge[1] = e;
340
  } else if (&p == points_[1]) {
341
    delaunay_edge[2] = e;
342
  } else {
343
    delaunay_edge[0] = e;
344
  }
345
}
346
 
347
// The neighbor across to given point
348
Triangle& Triangle::NeighborAcross(Point& opoint)
349
{
350
  if (&opoint == points_[0]) {
351
    return *neighbors_[0];
352
  } else if (&opoint == points_[1]) {
353
    return *neighbors_[1];
354
  }
355
  return *neighbors_[2];
356
}
357
 
358
void Triangle::DebugPrint()
359
{
360
  using namespace std;
361
  cout << points_[0]->x << "," << points_[0]->y << " ";
362
  cout << points_[1]->x << "," << points_[1]->y << " ";
363
  cout << points_[2]->x << "," << points_[2]->y << endl;
364
}
365
 
366
}
367
 

powered by: WebSVN 2.1.0

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