-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathak-peel_slice-euclidean.cpp
324 lines (282 loc) · 10.1 KB
/
ak-peel_slice-euclidean.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
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
#include "pipeline.hpp"
#include <glm/gtx/norm.hpp>
#include <glm/gtx/hash.hpp>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
void ak::peel_slice(
Parameters const ¶meters,
Model const &model,
std::vector< std::vector< EmbeddedVertex > > const &active_chains,
Model *slice_,
std::vector< EmbeddedVertex > *slice_on_model_,
std::vector< std::vector< uint32_t > > *slice_active_chains_,
std::vector< std::vector< uint32_t > > *slice_next_chains_,
std::vector< bool > *used_boundary_
) {
assert(slice_);
auto &slice = *slice_;
slice.clear();
assert(slice_on_model_);
auto &slice_on_model = *slice_on_model_;
slice_on_model.clear();
assert(slice_active_chains_);
auto &slice_active_chains = *slice_active_chains_;
slice_active_chains.clear();
assert(slice_next_chains_);
auto &slice_next_chains = *slice_next_chains_;
slice_next_chains.clear();
{
//DEBUG:
uint32_t loops = 0;
uint32_t lines = 0;
for (auto const &chain : active_chains) {
if (chain[0] == chain.back()) ++loops;
else ++lines;
}
std::cout << "---- peel slice on [" << loops << " loops and " << lines << " lines] ----" << std::endl;
}
Model clipped;
std::vector< ak::EmbeddedVertex > clipped_on_model;
ak::trim_model(model, active_chains, std::vector< std::vector< ak::EmbeddedVertex > >(), &clipped, &clipped_on_model);
//This version of the code just uses the 3D distance to the curve.
//might have problems with models that get really close to themselves.
std::vector< float > values(clipped.vertices.size(), std::numeric_limits< float >::infinity());
auto do_seg = [&values,&clipped](glm::vec3 const &a, glm::vec3 const &b) {
if (a == b) return;
glm::vec3 ab = b-a;
float limit = glm::dot(ab,ab);
float inv_limit = 1.0f / limit;
for (auto const &v : clipped.vertices) {
float amt = glm::dot(v-a, ab);
amt = std::max(0.0f, std::min(limit, amt));
glm::vec3 pt = (amt * inv_limit) * (b-a) + a;
float dis2 = glm::length2(v - pt);
float &best2 = values[&v - &clipped.vertices[0]];
best2 = std::min(best2, dis2);
}
};
for (auto const &chain : active_chains) {
for (uint32_t i = 0; i + 1 < chain.size(); ++i) {
do_seg(chain[i].interpolate(model.vertices), chain[i+1].interpolate(model.vertices));
}
}
for (auto &v : values) {
v = std::sqrt(v);
}
//TODO: blur/smooth distance fn somehow?
//extract chains:
std::vector< std::vector< ak::EmbeddedVertex > > next_chains;
{
float level = 2.0f * parameters.stitch_height_mm / parameters.model_units_mm;
std::vector< std::vector< EmbeddedVertex > > level_chains;
ak::extract_level_chains(clipped, values, level, &level_chains);
{ //(sort-of) hack: make all chains into loops by including portions of the boundary if needed:
std::unordered_map< glm::uvec2, uint32_t > next;
auto do_edge = [&next](uint32_t a, uint32_t b, uint32_t c) {
auto ret = next.insert(std::make_pair(glm::uvec2(a,b), c));
assert(ret.second);
};
for (auto const &tri : clipped.triangles) {
do_edge(tri.x, tri.y, tri.z);
do_edge(tri.y, tri.z, tri.x);
do_edge(tri.z, tri.x, tri.y);
}
struct ChainEnd {
ChainEnd(float along_, uint32_t chain_, bool is_start_) : along(along_), chain(chain_), is_start(is_start_) { }
float along;
uint32_t chain;
bool is_start;
};
std::unordered_map< glm::uvec2 , std::vector< ChainEnd > > on_edge;
auto do_end = [&](EmbeddedVertex const &ev, uint32_t chain, bool is_start) {
assert(ev.simplex.x != -1U);
assert(ev.simplex.y != -1U);
assert(ev.simplex.z == -1U);
glm::uvec2 e = glm::uvec2(ev.simplex.x, ev.simplex.y);
float amt = ev.weights.y;
if (next.count(e)) {
e = glm::uvec2(ev.simplex.y, ev.simplex.x);
amt = ev.weights.x;
}
assert(next.count(e) + next.count(glm::uvec2(e.y,e.x)) == 1); //e should be a boundary edge
assert(next.count(e) == 0);
on_edge[e].emplace_back(amt, chain, is_start);
};
for (auto &chain : level_chains) {
if (chain[0] == chain.back()) continue; //ignore loops
do_end(chain[0], &chain - &level_chains[0], true);
do_end(chain.back(), &chain - &level_chains[0], false);
}
std::vector< uint32_t > append(level_chains.size(), -1U);
std::vector< bool > used_boundary(level_chains.size(), true);
//loops marked as such before connection-making:
for (uint32_t c = 0; c < level_chains.size(); ++c) {
if (level_chains[c][0] == level_chains[c].back()) {
append[c] = c;
used_boundary[c] = false;
}
}
auto chase_path = [&](glm::uvec2 begin_e, ChainEnd begin_ce) {
assert(!begin_ce.is_start); //start at the end of a path, chase to the start of another
std::vector< EmbeddedVertex > path;
//path.emplace_back(EmbeddedVertex::on_edge(begin_e, begin_ce.along));
ChainEnd const *end_ce = nullptr;
glm::uvec2 e = begin_e;
ChainEnd ce = begin_ce;
while (true) {
{ //find next point along edge, if it exists:
ChainEnd const *found_ce = nullptr;
auto f = on_edge.find(e);
if (f != on_edge.end()) {
for (auto const &nce : f->second) {
if (nce.along <= ce.along) continue;
if (found_ce == nullptr || nce.along < found_ce->along) {
found_ce = &nce;
}
}
}
if (found_ce) {
assert(found_ce->is_start);
end_ce = found_ce;
break;
}
}
//next point is end of edge.
//add vertex:
path.emplace_back(EmbeddedVertex::on_vertex(e.y));
//circulate to next edge:
glm::uvec2 old_e = e;
while (true) {
auto f = next.find(glm::uvec2(e.y, e.x));
if (f == next.end()) break;
e = glm::uvec2(f->second, e.y);
}
assert(e.y == old_e.y);
assert(e.x != old_e.x);
e = glm::uvec2(e.y, e.x);
ce.chain = -1U;
ce.along = 0.0f;
}
assert(end_ce);
assert(end_ce->is_start); //start of (another?) path.
path.emplace_back(level_chains[end_ce->chain][0]);
level_chains[begin_ce.chain].insert(level_chains[begin_ce.chain].end(), path.begin(), path.end());
//flag that append code should append other chain:
assert(append[begin_ce.chain] == -1U);
append[begin_ce.chain] = end_ce->chain;
};
for (auto const &seed_ece : on_edge) {
for (auto const &seed : seed_ece.second) {
if (!seed.is_start) {
chase_path(seed_ece.first, seed);
}
}
}
for (uint32_t c = 0; c < level_chains.size(); ++c) {
if (append[c] == -1U) continue; //marked for discard
while (append[c] != c) {
uint32_t a = append[c];
assert(a < level_chains.size());
assert(!level_chains[a].empty());
assert(level_chains[c].back() == level_chains[a][0]); //already have first vertex
level_chains[c].insert(level_chains[c].end(), level_chains[a].begin()+1, level_chains[a].end());
append[c] = append[a];
append[a] = -1U; //mark for discard
level_chains[a].clear();
}
}
for (uint32_t c = 0; c < level_chains.size(); /* later */) {
if (append[c] == -1U) {
assert(level_chains[c].empty());
used_boundary[c] = used_boundary.back();
used_boundary.pop_back();
level_chains[c] = level_chains.back();
level_chains.pop_back();
} else {
assert(!level_chains[c].empty());
assert(level_chains[c][0] == level_chains[c].back());
++c;
}
}
if (used_boundary_) *used_boundary_ = used_boundary;
}
uint32_t loops = 0;
uint32_t lines = 0;
next_chains.reserve(level_chains.size());
for (auto &chain : level_chains) {
//chain is embedded on 'clipped' which is embedded on 'model'; re-embed on just 'model':
for (auto &v : chain) {
glm::uvec3 simplex = clipped_on_model[v.simplex.x].simplex;
if (v.simplex.y != -1U) simplex = ak::EmbeddedVertex::common_simplex(simplex, clipped_on_model[v.simplex.y].simplex);
if (v.simplex.z != -1U) simplex = ak::EmbeddedVertex::common_simplex(simplex, clipped_on_model[v.simplex.z].simplex);
glm::vec3 weights = v.weights.x * clipped_on_model[v.simplex.x].weights_on(simplex);
if (v.simplex.y != -1U) weights += v.weights.y * clipped_on_model[v.simplex.y].weights_on(simplex);
if (v.simplex.z != -1U) weights += v.weights.z * clipped_on_model[v.simplex.z].weights_on(simplex);
v.simplex = simplex;
v.weights = weights;
}
//subdivide chain and add to outputs:
if (chain[0] == chain.back()) ++loops;
else ++lines;
next_chains.emplace_back();
sample_chain(parameters.get_chain_sample_spacing(), model, chain, &next_chains.back());
}
std::cout << " extracted " << loops << " loops and " << lines << " lines." << std::endl;
}
//PARANOIA:
for (auto const &chain : active_chains) {
for (auto const &v : chain) {
assert(v.simplex.x < model.vertices.size());
assert(v.simplex.y == -1U || v.simplex.y < model.vertices.size());
assert(v.simplex.z == -1U || v.simplex.z < model.vertices.size());
}
for (uint32_t i = 1; i < chain.size(); ++i) {
assert(chain[i-1] != chain[i]);
}
}
for (auto const &chain : next_chains) {
for (auto const &v : chain) {
assert(v.simplex.x < model.vertices.size());
assert(v.simplex.y == -1U || v.simplex.y < model.vertices.size());
assert(v.simplex.z == -1U || v.simplex.z < model.vertices.size());
}
for (uint32_t i = 1; i < chain.size(); ++i) {
assert(chain[i-1] != chain[i]);
}
}
//end PARANOIA
//now actually pull out the proper slice:
ak::trim_model(model, active_chains, next_chains, &slice, &slice_on_model, &slice_active_chains, &slice_next_chains);
//sometimes this can combine vertices, in which case the output chains should be trimmed:
uint32_t trimmed = 0;
for (auto &chain : slice_active_chains) {
for (auto v : chain) {
assert(v < slice.vertices.size());
}
for (uint32_t i = 1; i < chain.size(); /* later */) {
if (chain[i-1] == chain[i]) {
chain.erase(chain.begin() + i);
++trimmed;
} else {
++i;
}
}
}
for (auto &chain : slice_next_chains) {
for (auto v : chain) {
assert(v < slice.vertices.size());
}
for (uint32_t i = 1; i < chain.size(); ++i) {
if (chain[i-1] == chain[i]) {
chain.erase(chain.begin() + i);
++trimmed;
} else {
++i;
}
}
}
if (trimmed) {
std::cout << "Trimmed " << trimmed << " too-close-for-epm vertices from slice chains." << std::endl;
}
}