-
Notifications
You must be signed in to change notification settings - Fork 1
/
carve-util.h
288 lines (244 loc) · 8.85 KB
/
carve-util.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
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2014 Blender Foundation.
* All rights reserved.
*
* Contributor(s): Blender Foundation,
* Sergey Sharybin
*
* ***** END GPL LICENSE BLOCK *****
*/
#ifndef __CARVE_UTIL_H__
#define __CARVE_UTIL_H__
#include <carve/csg.hpp>
#include <carve/geom3d.hpp>
#include <carve/interpolator.hpp>
#include <carve/mesh.hpp>
#include <carve/triangulator.hpp>
#include "carve-capi.h"
struct TriIdxCompare {
bool operator() (const carve::triangulate::tri_idx &left,
const carve::triangulate::tri_idx &right) const {
if (left.a < right.a) {
return true;
}
else if (left.a > right.a) {
return false;
}
if (left.b < right.b) {
return true;
}
else if (left.b > right.b) {
return false;
}
if (left.c < right.c) {
return true;
}
else if (left.c > right.c) {
return false;
}
return false;
}
};
typedef std::set<carve::triangulate::tri_idx, TriIdxCompare> TrianglesStorage;
void carve_getRescaleMinMax(const carve::mesh::MeshSet<3> *left,
const carve::mesh::MeshSet<3> *right,
carve::geom3d::Vector *min,
carve::geom3d::Vector *max);
typedef void (*UnionIntersectionsCallback) (const carve::mesh::MeshSet<3> *left,
const carve::mesh::MeshSet<3> *right,
void *userdata);
void carve_unionIntersections(carve::csg::CSG *csg,
carve::mesh::MeshSet<3> **left_r,
carve::mesh::MeshSet<3> **right_r,
UnionIntersectionsCallback callback,
void *user_data);
bool carve_checkPolyPlanarAndGetNormal(const std::vector<carve::mesh::MeshSet<3>::vertex_t> &vertex_storage,
const int verts_per_poly,
const int *verts_of_poly,
carve::math::Matrix3 *axis_matrix_r);
int carve_triangulatePoly(struct ImportMeshData *import_data,
CarveMeshImporter *mesh_importer,
const std::vector<carve::mesh::MeshSet<3>::vertex_t> &vertex_storage,
const int verts_per_poly,
const int *verts_of_poly,
const carve::math::Matrix3 &axis_matrix,
std::vector<int> *face_indices,
TrianglesStorage *triangles_storage);
namespace carve {
namespace interpolate {
template<typename attr_t>
class VertexAttr : public Interpolator {
public:
typedef const meshset_t::vertex_t *key_t;
protected:
typedef std::unordered_map<key_t, attr_t> attrmap_t;
attrmap_t attrs;
virtual void resultFace(const carve::csg::CSG &csg,
const meshset_t::face_t *new_face,
const meshset_t::face_t *orig_face,
bool flipped)
{
typedef meshset_t::face_t::const_edge_iter_t const_edge_iter_t;
for (const_edge_iter_t new_edge_iter = new_face->begin();
new_edge_iter != new_face->end();
++new_edge_iter)
{
typename attrmap_t::const_iterator found =
attrs.find(new_edge_iter->vert);
if (found == attrs.end()) {
for (const_edge_iter_t orig_edge_iter = orig_face->begin();
orig_edge_iter != orig_face->end();
++orig_edge_iter)
{
if ((orig_edge_iter->vert->v - new_edge_iter->vert->v).length2() < 1e-5) {
attrs[new_edge_iter->vert] = attrs[orig_edge_iter->vert];
}
}
}
}
}
public:
bool hasAttribute(const meshset_t::vertex_t *v) {
return attrs.find(v) != attrs.end();
}
const attr_t &getAttribute(const meshset_t::vertex_t *v, const attr_t &def = attr_t()) {
typename attrmap_t::const_iterator found = attrs.find(v);
if (found != attrs.end()) {
return found->second;
}
return def;
}
void setAttribute(const meshset_t::vertex_t *v, const attr_t &attr) {
attrs[v] = attr;
}
};
template<typename attr_t>
class SimpleFaceEdgeAttr : public Interpolator {
public:
typedef std::pair<const meshset_t::face_t *, unsigned> key_t;
protected:
typedef std::pair<const meshset_t::vertex_t *, const meshset_t::vertex_t *> vpair_t;
struct key_hash {
size_t operator()(const key_t &v) const {
return size_t(v.first) ^ size_t(v.second);
}
};
struct vpair_hash {
size_t operator()(const vpair_t &v) const {
return size_t(v.first) ^ size_t(v.second);
}
};
typedef std::unordered_map<key_t, attr_t, key_hash> attrmap_t;
typedef std::unordered_map<vpair_t, key_t, vpair_hash> edgedivmap_t;
attrmap_t attrs;
struct Hook : public Interpolator::Hook {
public:
virtual unsigned hookBits() const {
return carve::csg::CSG::Hooks::PROCESS_OUTPUT_FACE_BIT;
}
Hook(Interpolator *_interpolator, const carve::csg::CSG &_csg) : Interpolator::Hook(_interpolator, _csg) {
}
virtual ~Hook() {
}
};
virtual Interpolator::Hook *makeHook(carve::csg::CSG &csg) {
return new Hook(this, csg);
}
virtual void processOutputFace(const carve::csg::CSG &csg,
std::vector<carve::mesh::MeshSet<3>::face_t *> &new_faces,
const meshset_t::face_t *orig_face,
bool flipped) {
edgedivmap_t undiv;
for (meshset_t::face_t::const_edge_iter_t e = orig_face->begin(); e != orig_face->end(); ++e) {
key_t k(orig_face, e.idx());
typename attrmap_t::const_iterator attr_i = attrs.find(k);
if (attr_i == attrs.end()) {
continue;
} else {
undiv[vpair_t(e->v1(), e->v2())] = k;
}
}
for (size_t fnum = 0; fnum < new_faces.size(); ++fnum) {
const carve::mesh::MeshSet<3>::face_t *new_face = new_faces[fnum];
for (meshset_t::face_t::const_edge_iter_t e = new_face->begin(); e != new_face->end(); ++e) {
key_t k(new_face, e.idx());
vpair_t vp;
if (!flipped) {
vp = vpair_t(e->v1(), e->v2());
} else {
vp = vpair_t(e->v2(), e->v1());
}
typename edgedivmap_t::const_iterator vp_i;
if ((vp_i = undiv.find(vp)) != undiv.end()) {
attrs[k] = attrs[vp_i->second];
}
}
}
}
public:
bool hasAttribute(const meshset_t::face_t *f, unsigned e) {
return attrs.find(std::make_pair(f, e)) != attrs.end();
}
attr_t getAttribute(const meshset_t::face_t *f, unsigned e, const attr_t &def = attr_t()) {
typename attrmap_t::const_iterator fv = attrs.find(std::make_pair(f, e));
if (fv != attrs.end()) {
return (*fv).second;
}
return def;
}
void setAttribute(const meshset_t::face_t *f, unsigned e, const attr_t &attr) {
attrs[std::make_pair(f, e)] = attr;
}
void copyAttribute(const meshset_t::face_t *face,
unsigned edge,
SimpleFaceEdgeAttr<attr_t> *interpolator) {
key_t key(face, edge);
typename attrmap_t::const_iterator fv = interpolator->attrs.find(key);
if (fv != interpolator->attrs.end()) {
attrs[key] = (*fv).second;
}
}
void swapAttributes(SimpleFaceEdgeAttr<attr_t> *interpolator) {
attrs.swap(interpolator->attrs);
}
SimpleFaceEdgeAttr() : Interpolator() {
}
virtual ~SimpleFaceEdgeAttr() {
}
};
template<typename attr_t>
class SwapableFaceEdgeAttr : public FaceEdgeAttr<attr_t> {
public:
typedef carve::mesh::MeshSet<3> meshset_t;
void copyAttribute(const meshset_t::face_t *face,
unsigned edge,
SwapableFaceEdgeAttr<attr_t> *interpolator) {
typename FaceEdgeAttr<attr_t>::key_t key(face, edge);
typename FaceEdgeAttr<attr_t>::attrmap_t::const_iterator fv = interpolator->attrs.find(key);
if (fv != interpolator->attrs.end()) {
this->attrs[key] = (*fv).second;
}
}
void swapAttributes(SwapableFaceEdgeAttr<attr_t> *interpolator) {
this->attrs.swap(interpolator->attrs);
}
};
} // namespace interpolate
} // namespace carve
#endif // __CARVE_UTIL_H__