-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segments2Cylinders.cpp
201 lines (187 loc) · 6.07 KB
/
Segments2Cylinders.cpp
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
#include "Segments2Cylinders.hpp"
#include "GeometryUtils.hpp"
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <math.h>
inline static size_t n_cyl_verts(const size_t n) { return 2*n; }
inline static size_t n_cyl_faces(const size_t n) { return 2*n; }
inline static size_t n_cyl_end_faces(const size_t n) { return n-2+(n%2); }
template <class HDS>
static void add_cyl_vertices(CGAL::Polyhedron_incremental_builder_3<HDS>& B, const Segment3& seg, const double r, const size_t n)
{
const Point3& p1 = seg.source(), p2 = seg.target();
const Plane3 h(p1, p2-p1);
const Vector3 a = r * normalized(h.base1()), b = r * normalized(h.base2());
// Top circle vertices
for (size_t i = 0; i < n; ++i)
{
double theta = i * 2 * M_PI / n;
B.add_vertex(p1 + cos(theta) * a + sin(theta) * b);
}
// Bottom circle vertices
for (size_t i = 0; i < n; ++i)
{
double theta = i * 2 * M_PI / n;
B.add_vertex(p2 + cos(theta) * a + sin(theta) * b);
}
}
template <class HDS>
static void add_cyl_faces(CGAL::Polyhedron_incremental_builder_3<HDS>& B, size_t n, size_t off=0)
{
off *= n_cyl_verts(n);
// Add triangles around cylinder
for (size_t i = 0; i < n-1; ++i)
{
// clockwise... should it be counter-clockwise?
B.begin_facet();
B.add_vertex_to_facet(off+i+0);
B.add_vertex_to_facet(off+i+1);
B.add_vertex_to_facet(off+i+n);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(off+i+n);
B.add_vertex_to_facet(off+i+1);
B.add_vertex_to_facet(off+i+n+1);
B.end_facet();
}
B.begin_facet();
B.add_vertex_to_facet(off+n-1);
B.add_vertex_to_facet(off+0);
B.add_vertex_to_facet(off+2*n-1);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(off+0);
B.add_vertex_to_facet(off+n);
B.add_vertex_to_facet(off+2*n-1);
B.end_facet();
}
template <class HDS>
static void add_cyl_end_face_ccw(CGAL::Polyhedron_incremental_builder_3<HDS>& B, size_t n, size_t off=0)
{
off *= n_cyl_verts(n);
const size_t n2 = (n + 1) / 2 - 1;
// Add top of cylinder (counter-clockwise)
B.begin_facet();
B.add_vertex_to_facet(off+0);
B.add_vertex_to_facet(off+n-1);
B.add_vertex_to_facet(off+1);
B.end_facet();
for (size_t i = 1; i < n2; ++i)
{
B.begin_facet();
B.add_vertex_to_facet(off+i+0);
B.add_vertex_to_facet(off+n-i);
B.add_vertex_to_facet(off+i+1);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(off+i+1);
B.add_vertex_to_facet(off+n-i);
B.add_vertex_to_facet(off+n-i-1);
B.end_facet();
}
if ((n % 2) == 0)
{
B.begin_facet();
B.add_vertex_to_facet(off+n2+0);
B.add_vertex_to_facet(off+n2+2);
B.add_vertex_to_facet(off+n2+1);
B.end_facet();
}
}
template <class HDS>
static void add_cyl_end_face_cw(CGAL::Polyhedron_incremental_builder_3<HDS>& B, size_t n, size_t off=0)
{
off *= n_cyl_verts(n);
const size_t n2 = (n + 1) / 2 - 1;
// Add bottom of cylinder (clockwise)
B.begin_facet();
B.add_vertex_to_facet(off+n+0);
B.add_vertex_to_facet(off+n+1);
B.add_vertex_to_facet(off+n+n-1);
B.end_facet();
for (size_t i = 1; i < n2; ++i)
{
B.begin_facet();
B.add_vertex_to_facet(off+n+i+0);
B.add_vertex_to_facet(off+n+i+1);
B.add_vertex_to_facet(off+n+n-i);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(off+n+i+1);
B.add_vertex_to_facet(off+n+n-i-1);
B.add_vertex_to_facet(off+n+n-i);
B.end_facet();
}
if ((n % 2) == 0)
{
B.begin_facet();
B.add_vertex_to_facet(off+n+n2+0);
B.add_vertex_to_facet(off+n+n2+1);
B.add_vertex_to_facet(off+n+n2+2);
B.end_facet();
}
}
template <class HDS>
class Build_cylinder : public CGAL::Modifier_base<HDS>
{
typedef typename CGAL::Polyhedron_incremental_builder_3<HDS> Builder;
const Segment3& seg;
const double r;
const size_t n;
public:
Build_cylinder(const Segment3& seg, const double r, const size_t n) : seg(seg), r(r), n(n) {}
void operator()(HDS& hds)
{
Builder B(hds, true);
B.begin_surface(n_cyl_verts(n), n_cyl_faces(n) + 2*n_cyl_end_faces(n));
add_cyl_vertices(B, seg, r, n);
add_cyl_faces(B, n);
add_cyl_end_face_ccw(B, n);
add_cyl_end_face_cw(B, n);
B.end_surface();
}
};
template <class HDS>
class Build_cylinder_merged : public CGAL::Modifier_base<HDS>
{
typedef typename CGAL::Polyhedron_incremental_builder_3<HDS> Builder;
const std::vector<Segment3>& segs;
const double r;
const size_t n;
public:
Build_cylinder_merged(const std::vector<Segment3>& segs, const double r, const size_t n) : segs(segs), r(r), n(n) {}
void operator()(HDS& hds)
{
size_t nsegs = segs.size();
Builder B(hds, true);
B.begin_surface(nsegs*n_cyl_verts(n), nsegs*n_cyl_faces(n) + 2*n_cyl_end_faces(n));
for (size_t i = 0; i < nsegs; ++i) { add_cyl_vertices(B, segs[i], r, n); }
add_cyl_end_face_ccw(B, n, 0);
for (size_t i = 0; i < nsegs; ++i) { add_cyl_faces(B, n, i); }
add_cyl_end_face_cw(B, n, nsegs-1);
B.end_surface();
}
};
Polyhedron3* segment2cylinder(const Segment3& seg, double r, size_t n)
{
Build_cylinder<Polyhedron3::HalfedgeDS> bc(seg, r, n);
Polyhedron3* P = new Polyhedron3();
P->delegate(bc);
return P;
}
Polyhedron3* segments2cylinders_merged(const std::vector<Segment3>& segs, double r, size_t n)
{
Build_cylinder_merged<Polyhedron3::HalfedgeDS> bcm(segs, r, n);
Polyhedron3* P = new Polyhedron3();
P->delegate(bcm);
return P;
}
std::vector<Polyhedron3*> segments2cylinders(const std::vector<Segment3>& segs, double r, size_t n)
{
std::vector<Polyhedron3*> cyls;
cyls.reserve(segs.size());
for (std::vector<Segment3>::const_iterator itr = segs.begin(), end = segs.end(); itr != end; ++itr)
{
cyls.push_back(segment2cylinder(*itr, r, n));
}
return cyls;
}