forked from VTREEM/Carve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersect_face_division.cpp
More file actions
1760 lines (1469 loc) · 63.2 KB
/
Copy pathintersect_face_division.cpp
File metadata and controls
1760 lines (1469 loc) · 63.2 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Begin License:
// Copyright (C) 2006-2011 Tobias Sargeant (tobias.sargeant@gmail.com).
// All rights reserved.
//
// This file is part of the Carve CSG Library (http://carve-csg.com/)
//
// This file may be used under the terms of the GNU General Public
// License version 2.0 as published by the Free Software Foundation
// and appearing in the file LICENSE.GPL2 included in the packaging of
// this file.
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
// End:
#include <carve/csg.hpp>
#include <carve/polyline.hpp>
#include <carve/debug_hooks.hpp>
#include <carve/timing.hpp>
#include <carve/triangulator.hpp>
#include <list>
#include <set>
#include <iostream>
#include <algorithm>
#include "csg_detail.hpp"
#include "csg_data.hpp"
#include "intersect_common.hpp"
#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
void writePLY(const std::string &out_file, const carve::line::PolylineSet *lines, bool ascii);
#endif
namespace {
#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
template<typename iter_t>
void dumpFacesAndHoles(iter_t f_begin, iter_t f_end,
iter_t h_begin, iter_t h_end,
const std::string &fname) {
std::cerr << "dumping " << std::distance(f_begin, f_end) << " faces, " << std::distance(h_begin, h_end) << " holes." << std::endl;
std::map<carve::mesh::MeshSet<3>::vertex_t *, size_t> v_included;
for (iter_t i = f_begin; i != f_end; ++i) {
for (size_t j = 0; j < (*i).size(); ++j) {
if (v_included.find((*i)[j]) == v_included.end()) {
size_t &p = v_included[(*i)[j]];
p = v_included.size() - 1;
}
}
}
for (iter_t i = h_begin; i != h_end; ++i) {
for (size_t j = 0; j < (*i).size(); ++j) {
if (v_included.find((*i)[j]) == v_included.end()) {
size_t &p = v_included[(*i)[j]];
p = v_included.size() - 1;
}
}
}
carve::line::PolylineSet fh;
fh.vertices.resize(v_included.size());
for (std::map<carve::mesh::MeshSet<3>::vertex_t *, size_t>::const_iterator
i = v_included.begin(); i != v_included.end(); ++i) {
fh.vertices[(*i).second].v = (*i).first->v;
}
{
std::vector<size_t> connected;
for (iter_t i = f_begin; i != f_end; ++i) {
connected.clear();
for (size_t j = 0; j < (*i).size(); ++j) {
connected.push_back(v_included[(*i)[j]]);
}
fh.addPolyline(true, connected.begin(), connected.end());
}
for (iter_t i = h_begin; i != h_end; ++i) {
connected.clear();
for (size_t j = 0; j < (*i).size(); ++j) {
connected.push_back(v_included[(*i)[j]]);
}
fh.addPolyline(true, connected.begin(), connected.end());
}
}
::writePLY(fname, &fh, true);
}
#endif
template<typename T>
void populateVectorFromList(std::list<T> &l, std::vector<T> &v) {
v.clear();
v.reserve(l.size());
for (typename std::list<T>::iterator i = l.begin(); i != l.end(); ++i) {
v.push_back(T());
std::swap(*i, v.back());
}
l.clear();
}
template<typename T>
void populateListFromVector(std::vector<T> &v, std::list<T> &l) {
l.clear();
for (size_t i = 0; i < v.size(); ++i) {
l.push_back(T());
std::swap(v[i], l.back());
}
v.clear();
}
struct GraphEdge {
GraphEdge *next;
GraphEdge *prev;
GraphEdge *loop_next;
carve::mesh::MeshSet<3>::vertex_t *src;
carve::mesh::MeshSet<3>::vertex_t *tgt;
double ang;
int visited;
GraphEdge(carve::mesh::MeshSet<3>::vertex_t *_src, carve::mesh::MeshSet<3>::vertex_t *_tgt) :
next(NULL), prev(NULL), loop_next(NULL),
src(_src), tgt(_tgt),
ang(0.0), visited(-1) {
}
};
struct GraphEdges {
GraphEdge *edges;
carve::geom2d::P2 proj;
GraphEdges() : edges(NULL), proj() {
}
};
struct Graph {
typedef std::unordered_map<carve::mesh::MeshSet<3>::vertex_t *, GraphEdges> graph_t;
graph_t graph;
Graph() : graph() {
}
~Graph() {
int c = 0;
GraphEdge *edge;
for (graph_t::iterator i = graph.begin(), e = graph.end(); i != e; ++i) {
edge = (*i).second.edges;
while (edge) {
GraphEdge *temp = edge;
++c;
edge = edge->next;
delete temp;
}
}
if (c) {
std::cerr << "warning: "
<< c
<< " edges should have already been removed at graph destruction time"
<< std::endl;
}
}
const carve::geom2d::P2 &projection(carve::mesh::MeshSet<3>::vertex_t *v) const {
graph_t::const_iterator i = graph.find(v);
CARVE_ASSERT(i != graph.end());
return (*i).second.proj;
}
void computeProjection(carve::mesh::MeshSet<3>::face_t *face) {
for (graph_t::iterator i = graph.begin(), e = graph.end(); i != e; ++i) {
(*i).second.proj = face->project((*i).first->v);
}
for (graph_t::iterator i = graph.begin(), e = graph.end(); i != e; ++i) {
for (GraphEdge *e = (*i).second.edges; e; e = e->next) {
e->ang = carve::math::ANG(carve::geom2d::atan2(projection(e->tgt) - projection(e->src)));
}
}
}
void print(std::ostream &out, const carve::csg::VertexIntersections *vi) const {
for (graph_t::const_iterator i = graph.begin(), e = graph.end(); i != e; ++i) {
out << (*i).first << (*i).first->v << '(' << projection((*i).first).x << ',' << projection((*i).first).y << ") :";
for (const GraphEdge *e = (*i).second.edges; e; e = e->next) {
out << ' ' << e->tgt << e->tgt->v << '(' << projection(e->tgt).x << ',' << projection(e->tgt).y << ')';
}
out << std::endl;
if (vi) {
carve::csg::VertexIntersections::const_iterator j = vi->find((*i).first);
if (j != vi->end()) {
out << " (int) ";
for (carve::csg::IObjPairSet::const_iterator
k = (*j).second.begin(), ke = (*j).second.end(); k != ke; ++k) {
if ((*k).first < (*k).second) {
out << (*k).first << ".." << (*k).second << "; ";
}
}
out << std::endl;
}
}
}
}
void addEdge(carve::mesh::MeshSet<3>::vertex_t *v1, carve::mesh::MeshSet<3>::vertex_t *v2) {
GraphEdges &edges = graph[v1];
GraphEdge *edge = new GraphEdge(v1, v2);
if (edges.edges) edges.edges->prev = edge;
edge->next = edges.edges;
edges.edges = edge;
}
void removeEdge(GraphEdge *edge) {
if (edge->prev != NULL) {
edge->prev->next = edge->next;
} else {
if (edge->next != NULL) {
GraphEdges &edges = (graph[edge->src]);
edges.edges = edge->next;
} else {
graph.erase(edge->src);
}
}
if (edge->next != NULL) {
edge->next->prev = edge->prev;
}
delete edge;
}
bool empty() const {
return graph.size() == 0;
}
GraphEdge *pickStartEdge() {
// Try and find a vertex from which there is only one outbound edge. Won't always succeed.
for (graph_t::iterator i = graph.begin(); i != graph.end(); ++i) {
GraphEdges &ge = i->second;
if (ge.edges->next == NULL) {
return ge.edges;
}
}
return (*graph.begin()).second.edges;
}
GraphEdge *outboundEdges(carve::mesh::MeshSet<3>::vertex_t *v) {
return graph[v].edges;
}
};
/**
* \brief Take a set of new edges and split a face based upon those edges.
*
* @param[in] face The face to be split.
* @param[in] edges
* @param[out] face_loops Output list of face loops
* @param[out] hole_loops Output list of hole loops
* @param vi
*/
static void splitFace(carve::mesh::MeshSet<3>::face_t *face,
const carve::csg::V2Set &edges,
std::list<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &face_loops,
std::list<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &hole_loops,
const carve::csg::VertexIntersections & /* vi */) {
Graph graph;
for (carve::csg::V2Set::const_iterator
i = edges.begin(), e = edges.end();
i != e;
++i) {
carve::mesh::MeshSet<3>::vertex_t *v1 = ((*i).first), *v2 = ((*i).second);
if (carve::geom::equal(v1->v, v2->v)) std::cerr << "WARNING! " << v1->v << "==" << v2->v << std::endl;
graph.addEdge(v1, v2);
}
graph.computeProjection(face);
while (!graph.empty()) {
GraphEdge *edge;
GraphEdge *start;
start = edge = graph.pickStartEdge();
edge->visited = 0;
int len = 0;
for (;;) {
double in_ang = M_PI + edge->ang;
if (in_ang > M_TWOPI) in_ang -= M_TWOPI;
GraphEdge *opts;
GraphEdge *out = NULL;
double best = M_TWOPI + 1.0;
for (opts = graph.outboundEdges(edge->tgt); opts; opts = opts->next) {
if (opts->tgt == edge->src) {
if (out == NULL && opts->next == NULL) out = opts;
} else {
double out_ang = carve::math::ANG(in_ang - opts->ang);
if (out == NULL || out_ang < best) {
out = opts;
best = out_ang;
}
}
}
CARVE_ASSERT(out != NULL);
edge->loop_next = out;
if (out->visited >= 0) {
while (start != out) {
GraphEdge *e = start;
start = start->loop_next;
e->loop_next = NULL;
e->visited = -1;
}
len = edge->visited - out->visited + 1;
break;
}
out->visited = edge->visited + 1;
edge = out;
}
std::vector<carve::mesh::MeshSet<3>::vertex_t *> loop(len);
std::vector<carve::geom2d::P2> projected(len);
edge = start;
for (int i = 0; i < len; ++i) {
GraphEdge *next = edge->loop_next;
loop[i] = edge->src;
projected[i] = graph.projection(edge->src);
graph.removeEdge(edge);
edge = next;
}
CARVE_ASSERT(edge == start);
if (carve::geom2d::signedArea(projected) < 0) {
face_loops.push_back(std::vector<carve::mesh::MeshSet<3>::vertex_t *>());
face_loops.back().swap(loop);
} else {
hole_loops.push_back(std::vector<carve::mesh::MeshSet<3>::vertex_t *>());
hole_loops.back().swap(loop);
}
}
}
/**
* \brief Determine the relationship between a face loop and a hole loop.
*
* Determine whether a face and hole share an edge, or a vertex,
* or do not touch. Find a hole vertex that is not part of the
* face, and a hole,face vertex pair that are coincident, if such
* a pair exists.
*
* @param[in] f A face loop.
* @param[in] f_sort A vector indexing \a f in address order
* @param[in] h A hole loop.
* @param[in] h_sort A vector indexing \a h in address order
* @param[out] f_idx Index of a face vertex that is shared with the hole.
* @param[out] h_idx Index of the hole vertex corresponding to \a f_idx.
* @param[out] unmatched_h_idx Index of a hole vertex that is not part of the face.
* @param[out] shares_vertex Boolean indicating that the face and the hole share a vertex.
* @param[out] shares_edge Boolean indicating that the face and the hole share an edge.
*/
static void compareFaceLoopAndHoleLoop(const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &f,
const std::vector<unsigned> &f_sort,
const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &h,
const std::vector<unsigned> &h_sort,
unsigned &f_idx,
unsigned &h_idx,
int &unmatched_h_idx,
bool &shares_vertex,
bool &shares_edge) {
const size_t F = f.size();
const size_t H = h.size();
shares_vertex = shares_edge = false;
unmatched_h_idx = -1;
unsigned I, J;
for (I = J = 0; I < F && J < H;) {
unsigned i = f_sort[I], j = h_sort[J];
if (f[i] == h[j]) {
shares_vertex = true;
f_idx = i;
h_idx = j;
if (f[(i + F - 1) % F] == h[(j + 1) % H]) {
shares_edge = true;
}
carve::mesh::MeshSet<3>::vertex_t *t = f[i];
do { ++I; } while (I < F && f[f_sort[I]] == t);
do { ++J; } while (J < H && h[h_sort[J]] == t);
} else if (f[i] < h[j]) {
++I;
} else {
unmatched_h_idx = j;
++J;
}
}
if (J < H) {
unmatched_h_idx = h_sort[J];
}
}
/**
* \brief Compute an embedding for a set of face loops and hole loops.
*
* Because face and hole loops may be contained within each other,
* it must be determined which hole loops are directly contained
* within a face loop.
*
* @param[in] face The face from which these face and hole loops derive.
* @param[in] face_loops
* @param[in] hole_loops
* @param[out] containing_faces A vector which for each hole loop
* lists the indices of the face
* loops it is containined in.
* @param[out] hole_shared_vertices A map from a face,hole pair to
* a shared vertex pair.
*/
static void computeContainment(carve::mesh::MeshSet<3>::face_t *face,
std::vector<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &face_loops,
std::vector<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &hole_loops,
std::vector<std::vector<int> > &containing_faces,
std::map<int, std::map<int, std::pair<unsigned, unsigned> > > &hole_shared_vertices) {
#if defined(CARVE_DEBUG)
std::cerr << "input: "
<< face_loops.size() << "faces, "
<< hole_loops.size() << "holes."
<< std::endl;
#endif
std::vector<std::vector<carve::geom2d::P2> > face_loops_projected, hole_loops_projected;
std::vector<carve::geom::aabb<2> > face_loop_aabb, hole_loop_aabb;
std::vector<std::vector<unsigned> > face_loops_sorted, hole_loops_sorted;
std::vector<double> face_loop_areas, hole_loop_areas;
face_loops_projected.resize(face_loops.size());
face_loops_sorted.resize(face_loops.size());
face_loop_aabb.resize(face_loops.size());
face_loop_areas.resize(face_loops.size());
hole_loops_projected.resize(hole_loops.size());
hole_loops_sorted.resize(hole_loops.size());
hole_loop_aabb.resize(hole_loops.size());
hole_loop_areas.resize(hole_loops.size());
// produce a projection of each face loop onto a 2D plane, and an
// index vector which sorts vertices by address.
for (size_t m = 0; m < face_loops.size(); ++m) {
const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &f_loop = (face_loops[m]);
face_loops_projected[m].reserve(f_loop.size());
face_loops_sorted[m].reserve(f_loop.size());
for (size_t n = 0; n < f_loop.size(); ++n) {
face_loops_projected[m].push_back(face->project(f_loop[n]->v));
face_loops_sorted[m].push_back(n);
}
face_loop_areas.push_back(carve::geom2d::signedArea(face_loops_projected[m]));
std::sort(face_loops_sorted[m].begin(), face_loops_sorted[m].end(),
carve::make_index_sort(face_loops[m].begin()));
face_loop_aabb[m].fit(face_loops_projected[m].begin(), face_loops_projected[m].end());
}
// produce a projection of each hole loop onto a 2D plane, and an
// index vector which sorts vertices by address.
for (size_t m = 0; m < hole_loops.size(); ++m) {
const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &h_loop = (hole_loops[m]);
hole_loops_projected[m].reserve(h_loop.size());
hole_loops_projected[m].reserve(h_loop.size());
for (size_t n = 0; n < h_loop.size(); ++n) {
hole_loops_projected[m].push_back(face->project(h_loop[n]->v));
hole_loops_sorted[m].push_back(n);
}
hole_loop_areas.push_back(carve::geom2d::signedArea(hole_loops_projected[m]));
std::sort(hole_loops_sorted[m].begin(), hole_loops_sorted[m].end(),
carve::make_index_sort(hole_loops[m].begin()));
hole_loop_aabb[m].fit(hole_loops_projected[m].begin(), hole_loops_projected[m].end());
}
containing_faces.resize(hole_loops.size());
for (unsigned i = 0; i < hole_loops.size(); ++i) {
for (unsigned j = 0; j < face_loops.size(); ++j) {
if (!face_loop_aabb[j].completelyContains(hole_loop_aabb[i])) {
#if defined(CARVE_DEBUG)
std::cerr << "face: " << j
<< " hole: " << i
<< " skipped test (aabb fail)"
<< std::endl;
#endif
continue;
}
unsigned f_idx, h_idx;
int unmatched_h_idx;
bool shares_vertex, shares_edge;
compareFaceLoopAndHoleLoop(face_loops[j],
face_loops_sorted[j],
hole_loops[i],
hole_loops_sorted[i],
f_idx, h_idx,
unmatched_h_idx,
shares_vertex,
shares_edge);
#if defined(CARVE_DEBUG)
std::cerr << "face: " << j
<< " hole: " << i
<< " shares_vertex: " << shares_vertex
<< " shares_edge: " << shares_edge
<< std::endl;
#endif
carve::geom3d::Vector test = hole_loops[i][0]->v;
carve::geom2d::P2 test_p = face->project(test);
if (shares_vertex) {
hole_shared_vertices[i][j] = std::make_pair(h_idx, f_idx);
// Hole touches face. Should be able to connect it up
// trivially. Still need to record its containment, so that
// the assignment below works.
if (unmatched_h_idx != -1) {
#if defined(CARVE_DEBUG)
std::cerr << "using unmatched vertex: " << unmatched_h_idx << std::endl;
#endif
test = hole_loops[i][unmatched_h_idx]->v;
test_p = face->project(test);
} else {
// XXX: hole shares ALL vertices with face. Pick a point
// internal to the projected poly.
if (shares_edge) {
// Hole shares edge with face => face can't contain hole.
continue;
}
// XXX: how is this possible? Doesn't share an edge, but
// also doesn't have any vertices that are not in
// common. Degenerate hole?
// XXX: come up with a test case for this.
CARVE_FAIL("implement me");
}
}
// XXX: use loop area to avoid some point-in-poly tests? Loop
// area is faster, but not sure which is more robust.
if (carve::geom2d::pointInPolySimple(face_loops_projected[j], test_p)) {
#if defined(CARVE_DEBUG)
std::cerr << "contains: " << i << " - " << j << std::endl;
#endif
containing_faces[i].push_back(j);
} else {
#if defined(CARVE_DEBUG)
std::cerr << "does not contain: " << i << " - " << j << std::endl;
#endif
}
}
#if defined(CARVE_DEBUG)
if (containing_faces[i].size() == 0) {
//HOOK(drawFaceLoopWireframe(hole_loops[i], face->normal, 1.0, 0.0, 0.0, 1.0););
std::cerr << "hole loop: ";
for (unsigned j = 0; j < hole_loops[i].size(); ++j) {
std::cerr << " " << hole_loops[i][j] << ":" << hole_loops[i][j]->v;
}
std::cerr << std::endl;
for (unsigned j = 0; j < face_loops.size(); ++j) {
//HOOK(drawFaceLoopWireframe(face_loops[j], face->normal, 0.0, 1.0, 0.0, 1.0););
}
}
#endif
// CARVE_ASSERT(containing_faces[i].size() >= 1);
}
}
/**
* \brief Merge face loops and hole loops to produce a set of face loops without holes.
*
* @param[in] face The face from which these face loops derive.
* @param[in,out] f_loops A list of face loops.
* @param[in] h_loops A list of hole loops to be incorporated into face loops.
*/
static void mergeFacesAndHoles(carve::mesh::MeshSet<3>::face_t *face,
std::list<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &f_loops,
std::list<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &h_loops,
carve::csg::CSG::Hooks & /* hooks */) {
std::vector<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > face_loops;
std::vector<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > hole_loops;
std::vector<std::vector<int> > containing_faces;
std::map<int, std::map<int, std::pair<unsigned, unsigned> > > hole_shared_vertices;
#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
dumpFacesAndHoles(f_loops.begin(), f_loops.end(), h_loops.begin(), h_loops.end(), "/tmp/pre_merge.ply");
#endif
{
// move input face and hole loops to temp vectors.
size_t m;
face_loops.resize(f_loops.size());
m = 0;
for (std::list<std::vector<carve::mesh::MeshSet<3>::vertex_t *> >::iterator
i = f_loops.begin(), ie = f_loops.end();
i != ie;
++i, ++m) {
face_loops[m].swap((*i));
}
hole_loops.resize(h_loops.size());
m = 0;
for (std::list<std::vector<carve::mesh::MeshSet<3>::vertex_t *> >::iterator
i = h_loops.begin(), ie = h_loops.end();
i != ie;
++i, ++m) {
hole_loops[m].swap((*i));
}
f_loops.clear();
h_loops.clear();
}
// work out the embedding of holes and faces.
computeContainment(face, face_loops, hole_loops, containing_faces, hole_shared_vertices);
int unassigned = (int)hole_loops.size();
std::vector<std::vector<int> > face_holes;
face_holes.resize(face_loops.size());
for (unsigned i = 0; i < containing_faces.size(); ++i) {
if (containing_faces[i].size() == 0) {
std::map<int, std::map<int, std::pair<unsigned, unsigned> > >::iterator it = hole_shared_vertices.find(i);
if (it != hole_shared_vertices.end()) {
std::map<int, std::pair<unsigned, unsigned> >::iterator it2 = (*it).second.begin();
int f = (*it2).first;
unsigned h_idx = (*it2).second.first;
unsigned f_idx = (*it2).second.second;
// patch the hole into the face directly. because
// f_loop[f_idx] == h_loop[h_idx], we don't need to
// duplicate the f_loop vertex.
std::vector<carve::mesh::MeshSet<3>::vertex_t *> &f_loop = face_loops[f];
std::vector<carve::mesh::MeshSet<3>::vertex_t *> &h_loop = hole_loops[i];
f_loop.insert(f_loop.begin() + f_idx + 1, h_loop.size(), NULL);
unsigned p = f_idx + 1;
for (unsigned a = h_idx + 1; a < h_loop.size(); ++a, ++p) {
f_loop[p] = h_loop[a];
}
for (unsigned a = 0; a <= h_idx; ++a, ++p) {
f_loop[p] = h_loop[a];
}
#if defined(CARVE_DEBUG)
std::cerr << "hook face " << f << " to hole " << i << "(vertex)" << std::endl;
#endif
} else {
std::cerr << "uncontained hole loop does not share vertices with any face loop!" << std::endl;
}
unassigned--;
}
}
// work out which holes are directly contained within which faces.
while (unassigned) {
std::set<int> removed;
for (unsigned i = 0; i < containing_faces.size(); ++i) {
if (containing_faces[i].size() == 1) {
int f = containing_faces[i][0];
face_holes[f].push_back(i);
#if defined(CARVE_DEBUG)
std::cerr << "hook face " << f << " to hole " << i << std::endl;
#endif
removed.insert(f);
unassigned--;
}
}
for (std::set<int>::iterator f = removed.begin(); f != removed.end(); ++f) {
for (unsigned i = 0; i < containing_faces.size(); ++i) {
containing_faces[i].erase(std::remove(containing_faces[i].begin(),
containing_faces[i].end(),
*f),
containing_faces[i].end());
}
}
}
#if 0
// use old templated projection code to patch holes into faces.
for (unsigned i = 0; i < face_loops.size(); ++i) {
std::vector<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > face_hole_loops;
face_hole_loops.resize(face_holes[i].size());
for (unsigned j = 0; j < face_holes[i].size(); ++j) {
face_hole_loops[j].swap(hole_loops[face_holes[i][j]]);
}
if (face_hole_loops.size()) {
f_loops.push_back(carve::triangulate::incorporateHolesIntoPolygon(
carve::mesh::MeshSet<3>::face_t::projection_mapping(face->project),
face_loops[i],
face_hole_loops));
} else {
f_loops.push_back(face_loops[i]);
}
}
#else
// use new 2d-only hole patching code.
for (size_t i = 0; i < face_loops.size(); ++i) {
if (!face_holes[i].size()) {
f_loops.push_back(face_loops[i]);
continue;
}
std::vector<std::vector<carve::geom2d::P2> > projected_poly;
projected_poly.resize(face_holes[i].size() + 1);
projected_poly[0].reserve(face_loops[i].size());
for (size_t j = 0; j < face_loops[i].size(); ++j) {
projected_poly[0].push_back(face->project(face_loops[i][j]->v));
}
for (size_t j = 0; j < face_holes[i].size(); ++j) {
projected_poly[j+1].reserve(hole_loops[face_holes[i][j]].size());
for (size_t k = 0; k < hole_loops[face_holes[i][j]].size(); ++k) {
projected_poly[j+1].push_back(face->project(hole_loops[face_holes[i][j]][k]->v));
}
}
std::vector<std::pair<size_t, size_t> > result = carve::triangulate::incorporateHolesIntoPolygon(projected_poly);
f_loops.push_back(std::vector<carve::mesh::MeshSet<3>::vertex_t *>());
std::vector<carve::mesh::MeshSet<3>::vertex_t *> &out = f_loops.back();
out.reserve(result.size());
for (size_t j = 0; j < result.size(); ++j) {
if (result[j].first == 0) {
out.push_back(face_loops[i][result[j].second]);
} else {
out.push_back(hole_loops[face_holes[i][result[j].first-1]][result[j].second]);
}
}
}
#endif
#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
dumpFacesAndHoles(f_loops.begin(), f_loops.end(), h_loops.begin(), h_loops.end(), "/tmp/post_merge.ply");
#endif
}
/**
* \brief Assemble the base loop for a face.
*
* The base loop is the original face loop, including vertices
* created by intersections crossing any of its edges.
*
* @param[in] face The face to process.
* @param[in] vmap
* @param[in] face_split_edges
* @param[in] divided_edges A mapping from edge pointer to sets of
* ordered vertices corrsponding to the intersection points
* on that edge.
* @param[out] base_loop A vector of the vertices of the base loop.
*/
static bool assembleBaseLoop(carve::mesh::MeshSet<3>::face_t *face,
const carve::csg::detail::Data &data,
std::vector<carve::mesh::MeshSet<3>::vertex_t *> &base_loop,
carve::csg::CSG::Hooks &hooks) {
base_loop.clear();
// XXX: assumes that face->edges is in the same order as
// face->vertices. (Which it is)
carve::mesh::MeshSet<3>::edge_t *e = face->edge;
size_t e_idx = 0;
bool face_edge_intersected = false;
do {
base_loop.push_back(carve::csg::map_vertex(data.vmap, e->vert));
carve::csg::detail::EVVMap::const_iterator ev = data.divided_edges.find(e);
if (ev != data.divided_edges.end()) {
const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &ev_vec = ((*ev).second);
for (size_t k = 0, ke = ev_vec.size(); k < ke;) {
base_loop.push_back(ev_vec[k++]);
}
if (ev_vec.size() && hooks.hasHook(carve::csg::CSG::Hooks::EDGE_DIVISION_HOOK)) {
carve::mesh::MeshSet<3>::vertex_t *v1 = e->vert;
carve::mesh::MeshSet<3>::vertex_t *v2;
for (size_t k = 0, ke = ev_vec.size(); k < ke;) {
v2 = ev_vec[k++];
hooks.edgeDivision(e, e_idx, v1, v2);
v1 = v2;
}
v2 = e->v2();
hooks.edgeDivision(e, e_idx, v1, v2);
}
face_edge_intersected = true;
}
e = e->next;
++e_idx;
} while (e != face->edge);
return face_edge_intersected;
}
// the crossing_data structure holds temporary information regarding
// paths, and their relationship to the loop of edges that forms the
// face perimeter.
struct crossing_data {
std::vector<carve::mesh::MeshSet<3>::vertex_t *> *path;
size_t edge_idx[2];
crossing_data(std::vector<carve::mesh::MeshSet<3>::vertex_t *> *p, size_t e1, size_t e2) : path(p) {
edge_idx[0] = e1; edge_idx[1] = e2;
}
bool operator<(const crossing_data &c) const {
// the sort order for paths is in order of increasing initial
// position on the edge loop, but decreasing final position.
return edge_idx[0] < c.edge_idx[0] || (edge_idx[0] == c.edge_idx[0] && edge_idx[1] > c.edge_idx[1]);
}
};
bool processCrossingEdges(carve::mesh::MeshSet<3>::face_t *face,
const carve::csg::VertexIntersections &vertex_intersections,
carve::csg::CSG::Hooks &hooks,
std::vector<carve::mesh::MeshSet<3>::vertex_t *> &base_loop,
std::vector<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &paths,
std::list<std::vector<carve::mesh::MeshSet<3>::vertex_t *> > &face_loops_out) {
const size_t N = base_loop.size();
std::vector<crossing_data> endpoint_indices;
endpoint_indices.reserve(paths.size());
for (size_t i = 0; i < paths.size(); ++i) {
endpoint_indices.push_back(crossing_data(&paths[i], N, N));
}
// Step 1:
// locate endpoints of paths on the base loop.
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < paths.size(); ++j) {
// test beginning of path.
if (paths[j].front() == base_loop[i]) {
if (endpoint_indices[j].edge_idx[0] == N) {
endpoint_indices[j].edge_idx[0] = i;
} else {
// there is a duplicated vertex in the face perimeter. The
// path might attach to either of the duplicate instances
// so we have to work out which is the right one to attach
// to. We assume it's the index currently being examined,
// if the path heads in a direction that's internal to the
// angle made by the prior and next edges of the face
// perimeter. Otherwise, leave it as the currently
// selected index (until another duplicate is found, if it
// exists, and is tested).
const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &p = *endpoint_indices[j].path;
const size_t pN = p.size();
carve::mesh::MeshSet<3>::vertex_t *a, *b, *c;
a = base_loop[(i+N-1)%N];
b = base_loop[i];
c = base_loop[(i+1)%N];
carve::mesh::MeshSet<3>::vertex_t *adj = (p[0] == base_loop[i]) ? p[1] : p[pN-2];
if (carve::geom2d::internalToAngle(face->project(c->v),
face->project(b->v),
face->project(a->v),
face->project(adj->v))) {
endpoint_indices[j].edge_idx[0] = i;
}
}
}
// test end of path.
if (paths[j].back() == base_loop[i]) {
if (endpoint_indices[j].edge_idx[1] == N) {
endpoint_indices[j].edge_idx[1] = i;
} else {
// Work out which of the duplicated vertices is the right
// one to attach to, as above.
const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &p = *endpoint_indices[j].path;
const size_t pN = p.size();
carve::mesh::MeshSet<3>::vertex_t *a, *b, *c;
a = base_loop[(i+N-1)%N];
b = base_loop[i];
c = base_loop[(i+1)%N];
carve::mesh::MeshSet<3>::vertex_t *adj = (p[0] == base_loop[i]) ? p[1] : p[pN-2];
if (carve::geom2d::internalToAngle(face->project(c->v),
face->project(b->v),
face->project(a->v),
face->project(adj->v))) {
endpoint_indices[j].edge_idx[1] = i;
}
}
}
}
}
#if defined(CARVE_DEBUG)
std::cerr << "### N: " << N << std::endl;
for (size_t i = 0; i < paths.size(); ++i) {
std::cerr << "### path: " << i << " endpoints: " << endpoint_indices[i].edge_idx[0] << " - " << endpoint_indices[i].edge_idx[1] << std::endl;
}
#endif
// Step 2:
// divide paths up into those that connect to the base loop in two
// places (cross), and those that do not (noncross).
std::vector<crossing_data> cross, noncross;
cross.reserve(endpoint_indices.size() + 1);
noncross.reserve(endpoint_indices.size());
for (size_t i = 0; i < endpoint_indices.size(); ++i) {
#if defined(CARVE_DEBUG)
std::cerr << "### orienting path: " << i << " endpoints: " << endpoint_indices[i].edge_idx[0] << " - " << endpoint_indices[i].edge_idx[1] << std::endl;
#endif
if (endpoint_indices[i].edge_idx[0] != N && endpoint_indices[i].edge_idx[1] != N) {
// Orient each path correctly. Paths should progress from
// smaller perimeter index to larger, but if the path starts
// and ends at the same perimeter index, then the decision
// needs to be made based upon area.
if (endpoint_indices[i].edge_idx[0] == endpoint_indices[i].edge_idx[1]) {
// The path forms a loop that starts and ends at the same
// vertex of the perimeter. In this case, we need to orient
// the path so that the constructed loop has the right
// signed area.
double area = carve::geom2d::signedArea(endpoint_indices[i].path->begin() + 1,
endpoint_indices[i].path->end(),
carve::mesh::MeshSet<3>::face_t::projection_mapping(face->project));
if (area < 0) {
// XXX: Create test case to check that this is the correct sign for the area.
std::reverse(endpoint_indices[i].path->begin(), endpoint_indices[i].path->end());
}
} else {
if (endpoint_indices[i].edge_idx[0] > endpoint_indices[i].edge_idx[1]) {
std::swap(endpoint_indices[i].edge_idx[0], endpoint_indices[i].edge_idx[1]);
std::reverse(endpoint_indices[i].path->begin(), endpoint_indices[i].path->end());
}
}
}
if (endpoint_indices[i].edge_idx[0] != N &&
endpoint_indices[i].edge_idx[1] != N &&
endpoint_indices[i].edge_idx[0] != endpoint_indices[i].edge_idx[1]) {
cross.push_back(endpoint_indices[i]);
} else {