forked from Nathaniell1/STL_CUT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstlcut.cpp
1373 lines (1275 loc) · 38.5 KB
/
stlcut.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
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
#include "stlcut.h"
using namespace p2t;
using namespace std;
double eps = 1e-25;
double minEps = numeric_limits<double>::max();
char removedAxis = 'z';
#ifndef SEGV
#define SEGV
jmp_buf buf;
sigset_t signal_set;
int numOfSegv=0;
#endif
stl_plane::stl_plane(float x, float y, float z,float d)
{
this->x = x;
this->y = y;
this->z = z;
this->d = -1*d;
}
bool operator == (const stl_vertex& a, const stl_vertex& b)
{
if(a.x == b.x && a.y == b.y && a.z == b.z)
return true;
return false;
}
/*
*Compares two vertices for equality (with tolerance).
* Comparision is made in 2D, 2 out of 3 points are selected based on removed axis.
*@param [in] a First vertex
*@param [in] b Second vertex
*/
bool vertexEqual (stl_vertex a, stl_vertex b)
{
float tolerance = eps;
double tol1 = ABS(a.x-b.x);
double tol2 = ABS(a.y-b.y);
double tol3 = ABS(a.z-b.z);
if(removedAxis == 'x') { tol1 = tol3; a.x = a.z; b.x = b.z; }
if(removedAxis == 'y') { tol2 = tol3; a.y = a.z; b.y = b.z; }
double tmp = tol1>tol2 ? tol1:tol2;
if(tmp < minEps) minEps = tmp;
return (ABS((float)(a.x-b.x))<tolerance && ABS((float)(a.y-b.y))<tolerance );
}
/*
*Checks for duplicit vertices in border. Duplicits are erased.
*/
void Mesh::checkDuplicity()
{
for (unsigned int i = 0; i < border.size()-2; i+=2)
{
for (unsigned int j = i+2; j < border.size(); j+=2)
{
stl_vertex tmp1 = border[j];
stl_vertex tmp2 = border[j+1];
stl_vertex tmp3 = border[i];
stl_vertex tmp4 = border[i+1];
if( (tmp1 == tmp3 && tmp2 == tmp4 ) || (tmp2 == tmp3 && tmp1 == tmp4) )
{
border.erase(border.begin()+j,border.begin()+(j+2));
j-=2;
}
}
}
}
/*
* Function to help handle segmentation faults of poly2tri.
*/
void segv_handler(int s)
{
switch(s)
{
case SIGSEGV:
cerr<<"\nSegmentation fault signal caught! Attempting recovery.."<<endl<<endl;
numOfSegv++;
longjmp(buf, numOfSegv);
break;
}
}
/*
*Function to make segfault recovery possible.
*Sets function to call in case on segfault and unmasks segfault signal.
*/
void segvInit()
{
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGSEGV);
sigprocmask(SIG_UNBLOCK, &signal_set, NULL); //clearnig segfault signal
signal(SIGSEGV, segv_handler);
}
/*
* Comparator of two poly2tri points.
*/
bool comparatorStruct::operator() (const p2t::Point* lhs, const p2t::Point* rhs) const
{
if(lhs->x < rhs->x) return true;
else
{
if(lhs->x != rhs->x) return false;
if(lhs->y < rhs->y) return true;
}
return false;
}
/*
* Comparator of two poly2tri points.
*/
bool comparatorStruct::operator() (const p2t::Point lhs, const p2t::Point rhs) const
{
if(lhs.x < rhs.x) return true;
else
{
if(lhs.x != rhs.x) return false;
if(lhs.y < rhs.y) return true;
}
return false;
}
bool comparatorStruct::operator()(const stl_facet & a, const stl_facet & b) const
{
vector<stl_vertex> v1,v2;
for (int i = 0; i < 3; ++i)
{
v1.push_back(a.vertex[i]);
v2.push_back(b.vertex[i]);
}
sort(v1.begin(),v1.end(),comparatorStruct());
sort(v2.begin(),v2.end(),comparatorStruct());
if(v1 == v2)
return true;
return false;
}
/*
* Compares vertexes based on removed axis. No tolerance used.
* This comparator serves for sorting purpouses.
*/
bool comparatorStruct::operator() (const stl_vertex& lhs, const stl_vertex& rhs) const
{
if(removedAxis == 'z')
{
if(lhs.x < rhs.x) return true;
else
{
if(lhs.x != rhs.x) return false;
if(lhs.y < rhs.y) return true;
else
{
if(lhs.y != rhs.y) return false;
if(lhs.z < rhs.z) return true;
}
}
return false;
}
if(removedAxis == 'x')
{
if(lhs.y < rhs.y) return true;
else
{
if(lhs.y != rhs.y) return false;
if(lhs.z < rhs.z) return true;
else
{
if(lhs.z != rhs.z) return false;
if(lhs.x < rhs.x) return true;
}
}
return false;
}
if(removedAxis == 'y')
{
if(lhs.x < rhs.x) return true;
else
{
if(lhs.x != rhs.x) return false;
if(lhs.z < rhs.z) return true;
else
{
if(lhs.z != rhs.z) return false;
if(lhs.y < rhs.y) return true;
}
}
return false;
}
}
/*
* Returns missing coordinate when reverting points from 2d back to 3d. Missing coordinate is first calculated, and then its real value is found in original vertices.
* Original 3D point is returned.
*/
stl_vertex Mesh::getMissingCoordinate(const p2t::Point* a)
{
stl_vertex b;
if(removedAxis == 'x')
{
b.y = a->y;
b.z = a->x;
b.x = (plane.y*b.y + plane.z*b.z + plane.d) / ((-1.0) * plane.x);
}
if(removedAxis == 'y')
{
b.x = a->x;
b.z = a->y;
b.y = (plane.x*b.x+plane.z*b.z+plane.d) / ((-1.0) * plane.y);
}
if(removedAxis == 'z')
{
b.x = a->x;
b.y = a->y;
b.z = (plane.x*b.x + plane.y*b.y + plane.d) / ((-1.0) * plane.z);
}
set<stl_vertex>::iterator itlow,itup;
itlow = originalVertices.lower_bound (b);
itup = originalVertices.upper_bound (b);
if(itlow != itup)
b = *itlow;
else
{
if(removedAxis == 'z' && b.x == itlow->x && b.y == itlow->y || removedAxis == 'x' && b.z == itlow->z && b.y == itlow->y || removedAxis == 'y' && b.x == itlow->x && b.z == itlow->z )
b = *itlow;
else
{
if(itlow-- != originalVertices.begin())
if(removedAxis == 'z' && b.x == itlow->x && b.y == itlow->y || removedAxis == 'x' && b.z == itlow->z && b.y == itlow->y || removedAxis == 'y' && b.x == itlow->x && b.z == itlow->z )
b = *itlow;
}
}
return b;
}
/*
*Swap coordinates based on the one we ignore and push it to polylines
*/
void Mesh::pushBackToPolylines(vector<p2t::Point*> &vec,stl_vertex vert)
{
if(removedAxis == 'x')
{
vert.x = vert.z;
}
if(removedAxis == 'y')
{
vert.y = vert.z;
}
if(vec.size() == 0 || !(vec.back()->x == vert.x && vec.back()->y == vert.y))
vec.push_back(new p2t::Point(vert.x,vert.y));
}
/*
* Closes the mesh file.
*/
void Mesh::close()
{
stl_close(&meshFile);
}
/*
* Self-explenatory
*/
double Mesh::calculatePolygonArea(vector<p2t::Point*> polygon)
{
int n = polygon.size();
int j = 0;
double area = 0.0;
//shoelace algorithm to calculate polygon area
for (int i = 0; i < n; ++i)
{
j = (i+1)%n;
area += polygon[i]->x * polygon[j]->y;
area -= polygon[j]->x * polygon[i]->y;
}
area = abs(area) / 2.0;
return area;
}
/*
*Calculates if 3 point are in counter clockwise order. Used in finding non-simple polygons.
*/
bool Mesh::ccw(p2t::Point* a, p2t::Point* b, p2t::Point* c)
{
return ( (c->y - a->y) * (b->x - a->x) > (b->y - a->y) * (c->x - a->x) );
}
/*
* Calculates if two edges intersect.
*/
bool Mesh::edgesIntersect (p2t::Point* a, p2t::Point* b, p2t::Point* c, p2t::Point* d)
{
return ( ccw(a, c, d) != ccw(b, c, d) && ccw(a, b, c) != ccw(a, b, d) );
}
/*
* Tests if given vertex is inside given polygon .
* Used to find holes in polygons.
*/
bool Mesh::vertexInPolygon( const vector<Point* >& polygon, const double &testx, const double &testy)
{
int i, j;
bool in = false;
//vector from point to the right(to infinity) vs edges
for (i = 0, j = polygon.size()-1; i < polygon.size(); j = i++)
{
if ( ((polygon.at(i)->y > testy) != (polygon.at(j)->y > testy)) &&
(testx < (polygon.at(j)->x - polygon.at(i)->x) * (testy - polygon.at(i)->y) / (polygon.at(j)->y - polygon.at(i)->y) + polygon.at(i)->x) )
{
in = !in;
}
}
return in;
}
/*
* Sorts polylines (polygons) in descending order based on their area and removes polygons with area of 0;
*/
void Mesh::sortPolylines()
{
vector<double> polygonArea;
polygonArea.resize(polylines.size());
for (unsigned int i = 0; i < polylines.size(); ++i)
{
polygonArea[i] = calculatePolygonArea(polylines[i]);
}
vector<int> areaOrder;
for (unsigned int i = 0; i < polylines.size(); ++i)
{
areaOrder.push_back(i);
}
sort(areaOrder.begin(), areaOrder.end(), [&polygonArea](const int &a, const int &b)->bool{return polygonArea[a] < polygonArea[b];});
//AreaOrdernow contains sorted indexes of polygons based on their area
vector<vector <p2t::Point*> > tmpPolylines;
tmpPolylines.resize(polylines.size());
for (unsigned int i = 0; i < polylines.size(); ++i)
{
tmpPolylines[i] = polylines[areaOrder[i]];
}
vector<int> indexes;
for (unsigned int i = 0; i < tmpPolylines.size(); ++i)
{
if(polygonArea[areaOrder[i]] == 0)
{
indexes.push_back(i);
}
}
sort(indexes.begin(),indexes.end());
for (int i = indexes.size()-1; i >= 0; --i)
{
for (unsigned int n = 0; n < tmpPolylines[indexes[i]].size(); ++n)
{
delete tmpPolylines[indexes[i]][n];
}
//erasing wrong polygons with area = 0
tmpPolylines.erase(tmpPolylines.begin()+indexes[i]);
}
polylines = tmpPolylines;
}
/*
* This method determines if polygons in polylines are polygons or holes and distributes them in the designated container.
* When this method returns polygonsWithHoles contains those polygons and holes.
*/
void Mesh::findHoles()
{
vector<p2t::Point*> tmpPolygon = polylines.back();
polylines.pop_back();
pair<vector<p2t::Point*>,int > tmpPair = make_pair(tmpPolygon,-1);
vector<pair<vector<p2t::Point*> , int>> tmpVecPair;
tmpVecPair.push_back(tmpPair);
polygonsWithHoles.push_back(tmpVecPair);
int in;
int out;
int holeIn = -1; //-1 for polygon
bool placeFound = false;
int pos = 0;
while(polylines.size() > 0)
{
tmpPolygon = polylines.back();
polylines.pop_back();
pos = 0;
placeFound = false;
holeIn = -1;
for(unsigned int k = 0;k < polygonsWithHoles.size();k++)
{
in = 0;
out = 0;
//test 3 points and based on majority decide if this polygons is inside biggerPolygon
for(int j = 0;j < 3;j+= 1)
{
vector<p2t::Point*> biggerPolygon = polygonsWithHoles[k][pos].first;
double a = tmpPolygon[j]->x;
double b = tmpPolygon[j]->y;
if(vertexInPolygon(biggerPolygon, a, b))
in++;
else
out++;
}
if(in > out || placeFound)
{
if(in > out)
{
if(holeIn != -1)
holeIn = -1; //swaping between polygon
else
holeIn = pos; //and hole (pos = in which polygon is this one as a hole)
}
placeFound = true;
//as long as we didnt get to the end of the vector, continue - basicaly
if(pos < polygonsWithHoles[k].size()-1) // we dont want to test it vs itself
{
pos++;
k--; // we need to stay at the same index,
}
else //we get to the end, make a pair and push it to the end
{
tmpPair = make_pair(tmpPolygon, holeIn);
polygonsWithHoles[k].push_back(tmpPair);
break;
}
}
}
if(!placeFound) // if we get here and point wasnt in
{ // we found a new polygon
tmpPair = make_pair(tmpPolygon,-1);
tmpVecPair.erase(tmpVecPair.begin(), tmpVecPair.end());
tmpVecPair.push_back(tmpPair);
polygonsWithHoles.push_back(tmpVecPair);
}
}
}
/*
*This method tryes to fix non-simple polygons, but in very basic way.
*Its designed to remove falsely made intersecting edges due to floating points calcuation errors.
*Thats the reason why it always removes first point of second edge.. because this points is usualy close to the second point of first edge which causes intersection.
*/
void Mesh::removeNonsimplePolygonPoints(vector<p2t::Point*> & p)
{
bool pointRemoved;
do
{
pointRemoved = false;
for (int m = 0; m < p.size()-1; m+=1)
{
for (int j = m+2; j < p.size()-1; j+=1) //I assume that edges which share one point wont intersect - to prevent false intersection results
{
if(edgesIntersect(p[m], p[m+1], p[j], p[j+1] ))
{
p.erase(p.begin()+j,p.begin()+j+1);
j--;
pointRemoved = true;
continue;
}
}
}
for (int k = 1; k < p.size()-2; k+=1)
{
if(edgesIntersect(p[k], p[k+1], p[0], p[ p.size()-1] ))
{
p.erase(p.begin()+p.size()-1,p.begin()+p.size());
k--;
pointRemoved=true;
continue;
}
}
}while(pointRemoved == true);
}
/*
*Tests every polygon if its non-simple, if not, some points are removed in order to (try to) make it a simple polygon.
*/
void Mesh::repairIfNonsimplePolygon()
{
for (int i = 0; i < polygonsWithHoles.size(); ++i)
{
for (int j = 0; j < polygonsWithHoles[i].size(); ++j)
{
removeNonsimplePolygonPoints(polygonsWithHoles[i][j].first );
}
}
}
/*
* Searches through newly triagulated polygon and tryes to find triangles with points that did not exist before triangulation
* (poly2tri sometime make them when provided with nonsimple polygon). If those tringles are found, they are deleted.
* Its one of the many just-in-case-something-went-wrong methods. Can help to make succesful cut through model with small errors.
*/
void Mesh::checkPoly2triResult( vector<p2t::Triangle*>& triangles )
{
set<p2t::Point,comparatorStruct> originalBorderPoints;
for (std::set<stl_vertex>::iterator i = originalVertices.begin(); i != originalVertices.end(); ++i)
{
double x = (*i).x;
double y = (*i).y;
if(removedAxis == 'x')
x = (*i).z;
if(removedAxis == 'y')
y = (*i).z;
p2t::Point p = Point(x,y);
originalBorderPoints.insert(p);
}
for (int i = 0; i < triangles.size(); ++i)
{
for(int j = 0; j<3; j++)
{
p2t::Point* tmp = triangles[i]->GetPoint(j);
p2t::Point p = Point(tmp->x,tmp->y);
if( originalBorderPoints.count(p) == 0)// test if 2D point is in the original border created by cut
{
// if its not, erase the triangle
triangles.erase(triangles.begin()+i,triangles.begin()+i+1);
i--;
break;
}
}
}
}
/*
* Clean up method for polygonsWithHoles.
*/
void Mesh::deletePolygonsWithHoles()
{
for (int i = 0; i < polygonsWithHoles.size(); ++i)
{
for (int j = 0; j < polygonsWithHoles[i].size(); ++j)
{
for (int k = 0; k < polygonsWithHoles[i][j].first.size(); ++k)
{
delete polygonsWithHoles[i][j].first[k];
}
}
polygonsWithHoles[i].clear();
}
polygonsWithHoles.clear();
}
/*
* Triangulates all identified polygons with its holes.
*/
void Mesh::triangulateCut(int topOrBot)
{
map<int, p2t::CDT*> polygons;
repairIfNonsimplePolygon();
for (unsigned int i = 0; i < polygonsWithHoles.size(); ++i)
{
for (unsigned int j = 0; j < polygonsWithHoles[i].size(); ++j)
{
if(polygonsWithHoles[i][j].second == -1) // we found polygon, not a hole
{
p2t::CDT* tmp = new p2t::CDT(polygonsWithHoles[i][j].first);
polygons.insert ( pair<int,p2t::CDT*>(j,tmp) );
}
else // we found a hole
{
int holeIn = polygonsWithHoles[i][j].second;
map<int, p2t::CDT*>::iterator it;
it = polygons.find(holeIn);
it->second->AddHole(polygonsWithHoles[i][j].first);
}
}
//all polygons processed and ready to triangulation
if(polygons.size() > 0)
for (map<int, p2t::CDT*>::iterator k = polygons.begin(); k != polygons.end(); ++k)
{
k->second->Triangulate();
vector<p2t::Triangle*> triangles = k->second->GetTriangles();
checkPoly2triResult(triangles);
createFacets(triangles, topOrBot);
}
for (map<int, p2t::CDT*>::iterator k = polygons.begin(); k != polygons.end(); ++k)
{
delete (*k).second;
}
polygons.erase(polygons.begin(),polygons.end());
}
deletePolygonsWithHoles();
}
/*
* Makes facets in 3D from triangles in 2D provided by poly2tri triangulation.
*/
void Mesh::createFacets(vector<p2t::Triangle*> &triangles, int side)
{
// for each triangle, create facet
for (vector<p2t::Triangle*>::iterator i = triangles.begin(); i != triangles.end(); i++)
{
stl_vertex vertex;
stl_facet facet;
for (size_t j = 0; j < 3; j++)
{
p2t::Point* p = (*i)->GetPoint(j);
facet.vertex[j] = getMissingCoordinate(p);
}
float test_norm[3];
test_norm[0] = plane.x;
test_norm[1] = plane.y;
test_norm[2] = plane.z;
stl_normalize_vector(test_norm);
facet.normal.x = test_norm[0];
facet.normal.y = test_norm[1];
facet.normal.z = test_norm[2];
if(side == 0 || side == -1)
botFacets.push_back(facet);
//reverse normals
facet.normal.x *= -1.0;
facet.normal.y *= -1.0;
facet.normal.z *= -1.0;
vertex = facet.vertex[1];
facet.vertex[1] = facet.vertex[2];
facet.vertex[2] = vertex;
if(side == 0 || side == 1)
topFacets.push_back(facet);
}
}
/*
* Insert stl_vertex to the front of polylines. Used to clear up code. Used during find for continious edges after the cut.
*/
void Mesh::pushFrontToPolylines(vector<p2t::Point*> &vec,stl_vertex vert)
{
if(removedAxis == 'x')
{
vert.x = vert.z;
}
if(removedAxis == 'y')
{
vert.y = vert.z;
}
if(vec.size() == 0 || !(vec.front()->x == vert.x && vec.front()->y == vert.y))
vec.insert(vec.begin(),new p2t::Point(vert.x,vert.y));
}
/*
* Pops from border into the two provided stl_vertex variables.
*/
void Mesh::popTo(stl_vertex& a, stl_vertex& b)
{
a = border.back();
border.pop_back();
b = border.back();
border.pop_back();
}
/*
* Creates facet. Used to clean up code. Creates new triangles during cut.
*/
stl_facet Mesh::createFacet(stl_facet facet, int s, int i, stl_vertex intersect)
{
stl_facet tmp_facet = facet;
tmp_facet.vertex[0] = facet.vertex[s];
tmp_facet.vertex[1] = facet.vertex[(s+i)%3];
tmp_facet.vertex[2] = intersect;
return tmp_facet;
}
/*
* Creates facet. Used to clean up code. Creates new triangles during cut.
*/
stl_facet Mesh::createFacet(stl_facet facet,int s, int i, stl_vertex intersect1, stl_vertex intersect2)
{
stl_facet tmp_facet = facet;
switch(i)
{
case 0:
{
tmp_facet.vertex[0] = facet.vertex[s];
tmp_facet.vertex[1] = intersect1;
tmp_facet.vertex[2] = intersect2;
return tmp_facet;
}
case 1:
{
tmp_facet.vertex[0] = intersect1;
tmp_facet.vertex[1] = facet.vertex[(s+1)%3];
tmp_facet.vertex[2] = intersect2;
return tmp_facet;
}
case 2:
{
tmp_facet.vertex[0] = intersect2;
tmp_facet.vertex[1] = facet.vertex[(s+1)%3];
tmp_facet.vertex[2] = facet.vertex[(s+2)%3];
return tmp_facet;
}
}
}
/*
* Normalizes plane normal and sets up plane variable.
*/
void Mesh::setPlane(stl_plane plane)
{
this->plane = plane;
float norm[3];
norm[0] = plane.x;
norm[1] = plane.y;
norm[2] = plane.z;
stl_normalize_vector(norm);
this->plane.x = norm[0];
this->plane.y = norm[1];
this->plane.z = norm[2];
}
/*
* Sets removed axis based on the plane normal vector.
*/
void Mesh::setRemovedAxis()
{
removedAxis = 'z';
if(ABS(plane.x) >= ABS(plane.y) && ABS(plane.x) >= ABS(plane.z) )
removedAxis = 'x';
if(ABS(plane.y) >= ABS(plane.x) && ABS(plane.y) >= ABS(plane.z) )
removedAxis = 'y';
}
void Mesh::setVertexFromFacet(stl_vertex& a, stl_vertex& b,const int &s,const stl_facet & facet)
{
a = facet.vertex[(s+1)%3];
b = facet.vertex[(s+2)%3];
}
/*
* Calculates if two facets have equal edge. Facets provided have both at least one edge "on" the cuting plane.
* Used during prorocessing facets that have whole edge on the plane. In case of comparing facets that have all 3 points on, the stl_vertexes provided in
* the touple are irelevant.
*@param [in] facet1 Its a tuple. stl_facet its facet. stl_position is "above/below" if 2 points are "on" and one is not, or "on" when three points are on the cuting plane. stl_vertices represent edge on the cuting plane.
*@param [in] facet2
*/
bool Mesh::haveEqualEdges(tuple<stl_facet,stl_position,stl_vertex,stl_vertex>& facet1, tuple<stl_facet,stl_position,stl_vertex,stl_vertex>& facet2)
{
//ifs facet is "on" , all 3 points were on and we have to try to match all 3 edges of facet1 to 3 edges of facet2
if( (get<1>(facet1) == on && get<1>(facet2)!= on))
{
pair<stl_vertex,stl_vertex> a,b;
for (int i = 0; i < 3; ++i)
{
a = make_pair(get<0>(facet1).vertex[i] , get<0>(facet1).vertex[(i+1)%3]);
b = make_pair(get<0>(facet1).vertex[(i+1)%3] , get<0>(facet1).vertex[i]);
if(a == (make_pair(get<2>(facet2),get<3>(facet2))) || b == (make_pair(get<2>(facet2),get<3>(facet2))))
return true;
}
}
if( (get<1>(facet2) == on && get<1>(facet1)!= on))
{
pair<stl_vertex,stl_vertex> a,b;
for (int i = 0; i < 3; ++i)
{
a = make_pair(get<0>(facet2).vertex[i] , get<0>(facet2).vertex[(i+1)%3]);
b = make_pair(get<0>(facet2).vertex[(i+1)%3] , get<0>(facet2).vertex[i]);
if(a == make_pair(get<2>(facet1),get<3>(facet1)) || b == make_pair(get<2>(facet1),get<3>(facet1)))
return true;
}
}
// tests if its a same edge
if( ((get<2>(facet1) == get<2>(facet2)) && (get<3>(facet1) == get<3>(facet2))) || ((get<3>(facet1) == get<2>(facet2) && get<2>(facet1) == get<3>(facet2))))
return true;
return false;
}
/*
* Inserts and puhes back vertex x and y. Used to clean up code from reocurring parts.
*/
void Mesh::insertTo(stl_vertex x, stl_vertex y, vector<stl_vertex>& a, set<stl_vertex,comparatorStruct> & b)
{
a.push_back(x);
a.push_back(y);
b.insert(x);
b.insert(y);
}
/*
* After the cut, this method identifies polylines/polygons made during cut.
* In case of edges from original mode on cutting plane, this method recursively calls itself via processOnBorder.
*@param [in] firstCall Signalise if this method is called from cut method or processOnBorder method via recursion.
*/
bool Mesh::createBorderPolylines(bool firstCall)
{
if(firstCall == true && processOnFacets() == true)
if(processOnBorder() == true)
return false; // this means everything worked well, but we dont want to triangulate again, because processOnBorder already did that.
if(border.size() == 0 )
{
topFacets.clear();
botFacets.clear();
if(!silent) cerr<<"Nothing to cut"<<endl;
return false;
}
checkDuplicity();
stl_vertex cont,end,tmp1,tmp2;
popTo(cont,end);
polylines.resize(20);
numOfPolylines = 0;
eps = 1e-24;
minEps = numeric_limits<double>::max();
bool found = true;
pushBackToPolylines(polylines[numOfPolylines],end);
pushBackToPolylines(polylines[numOfPolylines],cont);
while(border.size()!=0)
{
if(!found)
eps = minEps*1.005;
if(eps > 0.25) // ignoring edges, if it gets to this, there was probably a problem with mesh
{
eps = 1e-24;
minEps = numeric_limits<double>::max();
popTo(cont,end);
if(!polylines[numOfPolylines].empty())
numOfPolylines++;
pushBackToPolylines(polylines[numOfPolylines],end);
pushBackToPolylines(polylines[numOfPolylines],cont);// if we didnt found it with 0.1 tolerance.. we will triangulate what we found
}
for (int i = border.size()-1;i >= 0; i-= 2)
{
found = false;
tmp1 = border[i];
tmp2 = border[i-1];
if(vertexEqual(tmp1, cont) || vertexEqual(tmp2, cont) || vertexEqual(tmp1, end) || vertexEqual(tmp2, end))// ve found next vertex in polyline
{
found = true;
if(vertexEqual(tmp1,cont))
{
pushBackToPolylines(polylines[numOfPolylines], tmp2);
cont = tmp2;
border.erase(border.begin()+(i-1), border.begin()+i+1);
}
else if(vertexEqual(tmp2,cont))
{
pushBackToPolylines(polylines[numOfPolylines], tmp1);
cont = tmp1;
border.erase(border.begin()+(i-1), border.begin()+i+1);
}
else if(vertexEqual(tmp1, end))
{
pushFrontToPolylines(polylines[numOfPolylines], tmp2);
end = tmp2;
border.erase(border.begin()+(i-1), border.begin()+i+1);
}
else if(vertexEqual(tmp2, end))
{
pushFrontToPolylines(polylines[numOfPolylines], tmp1);
end = tmp1;
border.erase(border.begin()+(i-1), border.begin()+i+1);
}
if(vertexEqual(cont,end)) //after we found next point, we have to check if another point is an end
{
if(!polylines[numOfPolylines].empty())
{
delete polylines[numOfPolylines][polylines[numOfPolylines].size()-1];
polylines[numOfPolylines].pop_back(); // delete last one, we dont want it twice
}
if(border.size() > 0)
{
numOfPolylines++;
if(numOfPolylines+5 > polylines.size())
polylines.resize(numOfPolylines*10);
popTo(end, cont);
pushBackToPolylines(polylines[numOfPolylines], end); //start of new polyline
pushBackToPolylines(polylines[numOfPolylines], cont);
}
minEps = numeric_limits<double>::max();
break;
}
else
{
minEps = numeric_limits<double>::max();
eps = 1e-24;
break;
}
}
}
}
polylines.resize(numOfPolylines+1);
sortPolylines();
if (polylines.size() == 0)
return false;
return true;
}
/*
* Called if processOnFacets returns true. This method is called in special case that there are edges that were exactly on the cuting plane.
* In situation like this, its necessary to triangulate twice with addition of top-related and bot-related edges to the border.
* Returns false if cut was made through side of a model (and every triangle in on one side of the plane or on the plane).
* Returns true otherwise.
*/
bool Mesh::processOnBorder()
{
if (!(topFacets.size()!=0 && botFacets.size() != 0 ))
return false;
vector<stl_vertex> borderBackUp = border;
border.insert(border.end(), botBorder.begin(), botBorder.end());
if(createBorderPolylines(false))
{
findHoles();
triangulateCut(-1);
}
border = borderBackUp;
border.insert(border.end(), topBorder.begin(), topBorder.end());
if(createBorderPolylines(false))
{
findHoles();
triangulateCut(1);
}
return true;
}
/*
* Sorts facets with edge on the cutting plane to the corresponding containers.
* Returns true if there are facet pairs with one facet completely on the cutting plane and second with two points on and one below or above.
*/
bool Mesh::processOnFacets()
{
for (int i = 0; i < facetsOnPlane.size(); ++i)
{
for (int j = i+1; j < facetsOnPlane.size(); ++j)
{
if( haveEqualEdges(facetsOnPlane[i],facetsOnPlane[j]) )
{
auto k = get<1>(facetsOnPlane[i]); auto l = get<1>(facetsOnPlane[j]);
if( (k == below && l == above) || (k == above && l == below) )
insertTo(get<2> (facetsOnPlane[i]), get<3> (facetsOnPlane[i]), border, originalVertices);
if((k == on && l == below) || (l == on && k == below))
{
int pos = (k == on) ? pos=j:pos=i;
insertTo(get<2>(facetsOnPlane[pos]), get<3>(facetsOnPlane[pos]), botBorder, originalVertices);
}
if((k == on && l == above) || (l == on && k == above))
{
int pos = (k == on) ? pos=j:pos=i;
insertTo(get<2>(facetsOnPlane[pos]), get<3>(facetsOnPlane[pos]), topBorder, originalVertices);
}
}
}
}
if( (botBorder.size() != 0 || topBorder.size() != 0))
return true;
return false;
}
/*
* Pushes facet to bot or top container accoring to its position to cuting plane.
*/
void Mesh::pushAboveBelow(const int aboves,stl_vertex& a,stl_vertex& b,const stl_facet &facet, const stl_position* pos)
{
if(aboves == 1)
for (int s = 0; s < 3; ++s)
{
if(pos[s] == above)
{
setVertexFromFacet(a,b,s,facet);
stl_vertex intersect1 = intersection(facet.vertex[s],a);
stl_vertex intersect2 = intersection(facet.vertex[s],b);
topFacets.push_back( createFacet(facet,s,0,intersect1,intersect2) ); // facet with above vertex of triangle and intersecting vertices added
botFacets.push_back( createFacet(facet,s,1,intersect1,intersect2) ); // facet with intersecting vertices and below vertex 1
botFacets.push_back( createFacet(facet,s,2,intersect1,intersect2) ); // facet with below vertices and intersecting vertex 2
border.push_back(intersect1);
border.push_back(intersect2);
break;
}
}
if(aboves == 2)
for (int s = 0; s < 3; ++s)
{
if(pos[s] == below)
{