-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrt2.h
437 lines (368 loc) · 12.5 KB
/
rt2.h
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//
// Created by grenier on 12/10/23.
//
#ifndef CCVT_TEST_CHARLINE_RT2_H
#define CCVT_TEST_CHARLINE_RT2_H
// CGAL
#include <CGAL/circulator.h>
// local
#include "convex_polygon.h"
#undef min
#undef max
template <class RT>
class CTriangulation : public RT
{
public:
typedef CTriangulation<RT> Rt;
// typedef typename Rt::Kernel_traits Kernel;
typedef typename Rt::Geom_traits Kernel;
typedef typename Kernel::FT FT;
// typedef typename Kernel::Weight Weight;
typedef typename Kernel::Point_2 Point;
typedef typename Kernel::Vector_2 Vector;
typedef typename Kernel::Ray_2 Ray;
typedef typename Kernel::Line_2 Line;
typedef typename Kernel::Segment_2 Segment;
typedef typename Kernel::Triangle_2 Triangle;
typedef typename Kernel::Weighted_point_2 Weighted_point;
typedef typename Rt::Vertex Vertex;
typedef typename Rt::Vertex_handle Vertex_handle;
typedef typename Rt::Vertex_iterator Vertex_iterator;
typedef typename Rt::Vertex_circulator Vertex_circulator;
typedef typename Rt::Finite_vertices_iterator Finite_vertices_iterator;
typedef typename Rt::Edge Edge;
typedef typename Rt::Edge_iterator Edge_iterator;
typedef typename Rt::Edge_circulator Edge_circulator;
typedef typename Rt::Finite_edges_iterator Finite_edges_iterator;
typedef typename Rt::Face Face;
typedef typename Rt::Face_handle Face_handle;
typedef typename Rt::Face_iterator Face_iterator;
typedef typename Rt::Face_circulator Face_circulator;
typedef typename Rt::Finite_faces_iterator Finite_faces_iterator;
typedef CConvexPolygon<Kernel> ConvexPolygon;
private:
ConvexPolygon m_boundary;
public:
CTriangulation()
{
}
void set_boundary(FT dx, FT dy)
{
m_boundary.clear();
m_boundary.init_rectangle(dx, dy);
}
////////////
// ACCESS //
////////////
Vertex_handle get_source(const Edge& edge) const
{
return edge.first->vertex( Rt::ccw(edge.second) );
}
Vertex_handle get_target(const Edge& edge) const
{
return edge.first->vertex( Rt::cw(edge.second) );
}
Vertex_handle get_opposite(const Edge& edge) const // eij -> xj
{
return edge.first->vertex( edge.second );
}
Edge get_twin(const Edge& edge) const // eij -> eji
{
Face_handle f = edge.first;
Vertex_handle v = get_source(edge);
Face_handle nf = f->neighbor(edge.second);
return Edge(nf, Rt::ccw(nf->index(v)));
}
Edge get_next(const Edge& edge) const
{
Face_handle f = edge.first;
int index = Rt::ccw(edge.second);
return Edge(f, index);
}
Edge get_prev(const Edge& edge) const
{
Face_handle f = edge.first;
int index = Rt::cw(edge.second);
return Edge(f, index);
}
FT get_length(const Edge& edge) const
{
Segment segment = get_segment(edge);
return std::sqrt(segment.squared_length());
}
FT get_length(const Segment& segment) const
{
return std::sqrt(segment.squared_length());
}
Segment get_segment(const Edge& edge) const
{
const Point& ps = get_source(edge)->get_position();
const Point& pt = get_target(edge)->get_position();
return Segment(ps, pt);
}
FT get_area(const Face_handle& face) const
{
Triangle triangle = get_triangle(face);
return triangle.area();
}
Triangle get_triangle(const Face_handle& face) const
{
Vertex_handle v0 = face->vertex(0);
Vertex_handle v1 = face->vertex(1);
Vertex_handle v2 = face->vertex(2);
return Triangle(v0->get_position(), v1->get_position(), v2->get_position());
}
Vector get_orthogonal_vector(const Edge& edge) const
{
const Point& ps = get_source(edge)->get_position();
const Point& pt = get_target(edge)->get_position();
Vector vst = pt - ps;
return Vector(-vst.y(), vst.x());
}
FT get_average_length() const
{
unsigned nb = 0;
FT avg_length = 0.0;
for (Finite_edges_iterator
eit = RT::finite_edges_begin();
eit != RT::finite_edges_end();
++eit)
{
Edge edge = *eit;
FT length = get_length(edge);
avg_length += length;
nb++;
}
return (avg_length / nb);
}
//////////
// AREA //
//////////
FT compute_area() const
{
FT area = 0.0;
for (Finite_vertices_iterator
vit = RT::finite_vertices_begin();
vit != RT::finite_vertices_end();
vit++)
{
Vertex_handle vi = vit;
area += vi->compute_area();
}
return area;
}
///////////////////////
// INSIDE / BOUNDARY //
///////////////////////
bool is_inside(Face_handle face) const
{
if (RT::is_infinite(face)) return false;
return true;
}
bool is_inside(const Edge& edge) const
{
Edge twin = get_twin(edge);
bool left = is_inside(edge.first);
bool right = is_inside(twin.first);
return (left || right);
}
bool is_boundary(const Edge& edge) const
{
Edge twin = get_twin(edge);
bool left = is_inside(edge.first);
bool right = is_inside(twin.first);
return (left != right);
}
bool is_boundary(Vertex_handle vertex) const
{
if (vertex->is_hidden()) return false;
Face_circulator fcirc = RT::incident_faces(vertex);
Face_circulator fend = fcirc;
CGAL_For_all(fcirc, fend)
{
Face_handle face = fcirc;
if (!is_inside(face))
return true;
}
return false;
}
//////////
// DUAL //
//////////
Point get_dual(Face_handle face) const
{
return RT::dual(face);
}
Segment get_dual(const Edge& edge) const
{
/*
* primal edge = (s,t)
* dual edge = (right, left)
* primal x dual > 0
*/
Edge twin = get_twin(edge);
Face_handle left_face = edge.first;
Face_handle right_face = twin.first;
bool left_inside = is_inside( left_face);
bool right_inside = is_inside(right_face);
if (left_inside && right_inside)
{
Point left_cw = get_dual( left_face);
Point right_cw = get_dual(right_face);
return Segment(right_cw, left_cw);
}
Vector vec90 = get_orthogonal_vector(edge);
if (!left_inside && !right_inside)
{
Point cw = get_edge_cw(edge);
Line line(cw, vec90);
return Segment(line.point(-100), line.point(100));
}
if (left_inside)
{
Point cw = get_dual(left_face);
Ray ray(cw, -vec90);
return Segment(ray.point(100), cw);
}
Point cw = get_dual(right_face);
Ray ray(cw, vec90);
return Segment(cw, ray.point(100));
}
Point get_edge_cw(const Edge& edge) const // cij
{
Vertex_handle vi = get_source(edge);
Vertex_handle vj = get_target(edge);
const Point& pi = vi->get_position();
const Point& pj = vj->get_position();
if (pi == pj) return pi;
const FT wi = vi->get_weight();
const FT wj = vj->get_weight();
const FT lij = get_length(edge);
const FT dij = 0.5*(lij + (wi - wj)/lij);
Vector vecij = (pj - pi) / lij;
return pi + dij*vecij;
}
/////////////
// BOUNDED //
/////////////
void build_polygon(Vertex_handle vi,
std::vector<Point>& points) const
{
points = vi->get_dual().get_points();
}
bool pre_build_polygon(Vertex_handle vi,
std::vector<Point>& points) const
{
std::vector<Segment> segments;
Edge_circulator ecirc = RT::incident_edges(vi);
Edge_circulator eend = ecirc;
CGAL_For_all(ecirc, eend)
{
Edge edge = *ecirc;
if (!is_inside(edge)) continue;
Edge twin = get_twin(edge);
Segment segment = build_bounded_dual_edge(twin);
if (segment.is_degenerate()) continue;
segments.push_back(segment);
}
if (segments.empty()) return false;
Point center;
Vector size;
m_boundary.compute_bbox(center, size);
Point first_pt = segments.front().source();
Point last_pt = first_pt;
for (unsigned i = 0; i < segments.size(); ++i)
{
Segment segment = segments[i];
Point ps = segment.source();
Point pt = segment.target();
if (ps != last_pt)
{
fill_gap(center, size, last_pt, ps, points);
points.push_back(ps);
}
points.push_back(pt);
last_pt = pt;
}
if (first_pt != last_pt)
{
fill_gap(center, size, last_pt, first_pt, points);
points.push_back(first_pt);
}
return true;
}
void fill_gap(const Point& center, const Vector& size,
const Point& a, const Point& b,
std::vector<Point>& points) const
{
if (a == b) return;
int aside = find_side(center, size, a);
if (aside == -1) return;
int bside = find_side(center, size, b);
if (bside == -1) return;
while (aside != bside)
{
aside = (aside + 1) % 4;
Point q = compute_corner(center, size, aside);
points.push_back(q);
}
}
int find_side(const Point& center, const Vector& size, const Point& a) const
{
FT pw = center.x() + size.x();
FT nw = center.x() - size.x();
FT ph = center.y() + size.y();
FT nh = center.y() - size.y();
// rect
int side = -1;
if (std::abs(a.x() - pw) < EPS) side = 1;
if (std::abs(a.x() - nw) < EPS) side = 3;
if (std::abs(a.y() - ph) < EPS) side = 2;
if (std::abs(a.y() - nh) < EPS) side = 0;
return side;
}
Point compute_corner(const Point& center, const Vector& size, int side) const
{
if (side == 0) return Point(center.x() - size.x(), center.y() - size.y());
if (side == 1) return Point(center.x() + size.x(), center.y() - size.y());
if (side == 2) return Point(center.x() + size.x(), center.y() + size.y());
if (side == 3) return Point(center.x() - size.x(), center.y() + size.y());
return center;
}
Segment build_bounded_dual_edge(const Edge& edge) const
{
Segment dual_segment = get_dual(edge);
dual_segment = m_boundary.clamp(dual_segment);
return dual_segment;
}
////////////
// LOCATE //
////////////
Vertex_handle find_nearest_vertex(const Point& query,
Vertex_handle candidate = Vertex_handle()) const
{
typename Kernel::Compare_power_distance_2 cmp_power_distance =
Rt::geom_traits().compare_power_distance_2_object();
Vertex_handle vertex = candidate;
if (vertex == Vertex_handle()) vertex = Rt::finite_vertex();
Vertex_handle vclosest;
do {
vclosest = vertex;
Weighted_point wp = vertex->point();
Vertex_circulator vcirc = Rt::incident_vertices(vertex);
Vertex_circulator vend = vcirc;
CGAL_For_all(vcirc, vend)
{
Vertex_handle v = vcirc;
if (this->is_infinite(v)) continue;
if (cmp_power_distance(query, v->point(), wp) == CGAL::SMALLER )
{
vertex = v;
break;
}
}
} while (vclosest != vertex);
return vclosest;
}
};
#endif //CCVT_TEST_CHARLINE_RT2_H