-
Notifications
You must be signed in to change notification settings - Fork 140
/
opennurbs_brep_tools.cpp
4049 lines (3575 loc) · 106 KB
/
opennurbs_brep_tools.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
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
static
const ON_BrepEdge* FindLinearEdge( const ON_Brep& brep, int vi0, int vi1 )
{
// searchs for a linear edge connecting the vertices
// brep.m_V[vi0] and brep.m_V[vi1].
if ( vi0 < 0 || vi0 >= brep.m_V.Count() )
return nullptr;
if ( vi1 < 0 || vi1 >= brep.m_V.Count() )
return nullptr;
if ( vi0 == vi1 )
return nullptr;
const ON_BrepVertex& v0 = brep.m_V[vi0];
//const ON_BrepVertex& v1 = brep.m_V[vi1];
int vei;
for ( vei = 0; vei < v0.m_ei.Count(); vei++ )
{
const ON_BrepEdge* edge = brep.Edge( v0.m_ei[vei] );
if ( !edge )
continue;
if ( edge->m_vi[0] != vi0 && edge->m_vi[1] != vi0 )
continue;
if ( edge->m_vi[0] != vi1 && edge->m_vi[1] != vi1 )
continue;
if ( !edge->IsLinear() )
continue;
return edge;
}
return nullptr;
}
static
void SynchFaceOrientation( ON_Brep& brep, int fi )
{
const ON_BrepFace* face = brep.Face(fi);
if ( face )
{
int flip = -1, fli, lti;
for ( fli = 0; fli < face->m_li.Count(); fli++ )
{
const ON_BrepLoop* loop = brep.Loop( face->m_li[fli] );
if ( !loop )
continue;
for ( lti = 0; lti < loop->m_ti.Count(); lti++ )
{
const ON_BrepTrim* trim = brep.Trim( loop->m_ti[lti] );
if ( !trim )
continue;
const ON_BrepEdge* edge = brep.Edge( trim->m_ei );
if ( !edge )
continue;
if ( edge->m_ti.Count() != 2 )
continue;
const ON_BrepTrim* trim0 = brep.Trim( edge->m_ti[0] );
const ON_BrepTrim* trim1 = brep.Trim( edge->m_ti[1] );
if ( !trim0 || !trim1 )
continue;
bool bRev0 = trim0->m_bRev3d ? true : false;
bool bRev1 = trim1->m_bRev3d ? true : false;
if ( bRev0 == bRev1 )
{
if ( flip == -1 )
flip = 1;
else if (flip != 1 )
return;
}
else
{
if ( flip == -1 )
flip = 0;
else if (flip != 0 )
return;
}
}
}
if ( flip == 1 )
brep.FlipFace(brep.m_F[fi]);
}
}
ON_BrepFace* ON_Brep::NewRuledFace(
const ON_BrepEdge& edgeA,
bool bRevEdgeA,
const ON_BrepEdge& edgeB,
bool bRevEdgeB
)
{
if ( edgeA.m_edge_index == edgeB.m_edge_index )
return nullptr;
if ( Edge( edgeA.m_edge_index) != &edgeA )
return nullptr;
if ( Edge( edgeB.m_edge_index) != &edgeB )
return nullptr;
ON_NurbsCurve cA, cB;
if ( !edgeA.GetNurbForm( cA ) )
return nullptr;
if ( bRevEdgeA )
cA.Reverse();
if ( !edgeB.GetNurbForm( cB ) )
return nullptr;
if ( bRevEdgeB )
cB.Reverse();
ON_NurbsSurface* srf = ON_NurbsSurface::New();
if ( !srf->CreateRuledSurface( cA, cB ) )
{
delete srf;
return nullptr;
}
// corner vertices (sw,se,ne,nw)
int vid[4] = {-1,-1,-1,-1};
vid[0] = edgeA.m_vi[bRevEdgeA?1:0];
vid[1] = edgeA.m_vi[bRevEdgeA?0:1];
vid[2] = edgeB.m_vi[bRevEdgeB?0:1];
vid[3] = edgeB.m_vi[bRevEdgeB?1:0];
if ( vid[1] == vid[2] )
{
// make sure surface has a singular east side
srf->CollapseSide( 1 );
}
if ( vid[1] == vid[2] )
{
// make sure surface has a singular west side
srf->CollapseSide( 3 );
}
// side edges (s,e,n,w)
int eid[4] = {-1,-1,-1,-1};
bool bRev3d[4] = {false,false,false,false};
// south side
eid[0] = edgeA.m_edge_index;
bRev3d[0] = bRevEdgeA;
// east side
const ON_BrepEdge* east_edge = FindLinearEdge( *this, vid[1], vid[2] );
if ( east_edge )
{
eid[1] = east_edge->m_edge_index;
bRev3d[1] = (east_edge->m_vi[0] == vid[2]);
}
// north side
eid[2] = edgeB.m_edge_index;
bRev3d[2] = !bRevEdgeB;
// west side
const ON_BrepEdge* west_edge = FindLinearEdge( *this, vid[3], vid[0] );
if ( west_edge )
{
eid[3] = west_edge->m_edge_index;
bRev3d[3] = (west_edge->m_vi[0] == vid[0]);
}
ON_BrepFace* face = NewFace( srf, vid, eid, bRev3d );
if ( face )
SynchFaceOrientation( *this, face->m_face_index );
return face;
}
ON_BrepFace* ON_Brep::NewConeFace(
const ON_BrepVertex& vertex,
const ON_BrepEdge& edge,
bool bRevEdge
)
{
if ( Edge( edge.m_edge_index) != &edge )
return nullptr;
if ( Vertex( vertex.m_vertex_index) != &vertex )
return nullptr;
if ( edge.m_vi[0] == vertex.m_vertex_index )
return nullptr;
if ( edge.m_vi[1] == vertex.m_vertex_index )
return nullptr;
ON_NurbsCurve c;
if ( !edge.GetNurbForm( c ) )
return nullptr;
if ( bRevEdge )
c.Reverse();
ON_NurbsSurface* srf = ON_NurbsSurface::New();
if ( !srf->CreateConeSurface( vertex.point, c ) )
{
delete srf;
return nullptr;
}
// corner vertices (sw,se,ne,nw)
int vid[4] = {-1,-1,-1,-1};
vid[0] = edge.m_vi[bRevEdge?1:0];
vid[1] = edge.m_vi[bRevEdge?0:1];
vid[2] = vertex.m_vertex_index;
vid[3] = vertex.m_vertex_index;
// side edges (s,e,n,w)
int eid[4] = {-1,-1,-1,-1};
bool bRev3d[4] = {false,false,false,false};
// south side
eid[0] = edge.m_edge_index;
bRev3d[0] = bRevEdge;
// east side
const ON_BrepEdge* east_edge = FindLinearEdge( *this, vid[1], vid[2] );
if ( east_edge )
{
eid[1] = east_edge->m_edge_index;
bRev3d[1] = (east_edge->m_vi[0] == vid[2]);
}
// west side
const ON_BrepEdge* west_edge = FindLinearEdge( *this, vid[3], vid[0] );
if ( west_edge )
{
eid[3] = west_edge->m_edge_index;
bRev3d[3] = (west_edge->m_vi[0] == vid[0]);
}
ON_BrepFace* face = NewFace( srf, vid, eid, bRev3d );
if ( face )
SynchFaceOrientation( *this, face->m_face_index );
return face;
}
bool ON_Brep::SetTrimBoundingBoxes( bool bLazy )
{
bool rc = true;
int fi, face_count = m_F.Count();
for ( fi = 0; fi < face_count; fi++ )
{
if ( !SetTrimBoundingBoxes( m_F[fi], bLazy ) )
rc = false;
}
return rc;
}
bool ON_Brep::SetTrimBoundingBoxes( ON_BrepFace& face, bool bLazy )
{
bool rc = true;
int li, fli, loop_count = m_L.Count(), fl_count = face.m_li.Count();
for ( fli = 0; fli < fl_count; fli++ )
{
li = face.m_li[fli];
if ( li >= 0 && li < loop_count )
{
if ( !SetTrimBoundingBoxes( m_L[li], bLazy ) )
rc = false;
}
}
return rc;
}
bool ON_Brep::SetTrimBoundingBoxes( ON_BrepLoop& loop, bool bLazy )
{
// TL_Brep overrides this function and computes much tighter
// bounding boxes that take trim.m_t[] into account.
bool rc = true;
int ti, lti, trim_count = m_T.Count(), lt_count = loop.m_ti.Count();
bool bSetLoopBox = true;
if ( bLazy && loop.m_pbox.IsValid() )
bSetLoopBox = false;
else
loop.m_pbox.Destroy();
for ( lti = 0; lti < lt_count; lti++ )
{
ti = loop.m_ti[lti];
if ( ti >= 0 && ti < trim_count )
{
if ( !SetTrimBoundingBox( m_T[ti], bLazy ) )
rc = false;
else if ( bSetLoopBox )
loop.m_pbox.Union( m_T[ti].m_pbox );
}
}
return (rc && loop.m_pbox.IsValid()) ? true : false;
}
bool ON_Brep::SetTrimBoundingBox( ON_BrepTrim& trim, bool bLazy )
{
// TL_Brep overrides this function and computes much
// tighter bounding boxes that take trim.m_t[] into account.
bool rc = true;
if ( !trim.m_pbox.IsValid() || !bLazy )
{
trim.m_pbox.Destroy();
if ( trim.ProxyCurve() )
{
trim.m_pbox = trim.BoundingBox();
trim.m_pbox.m_min.z = 0.0;
trim.m_pbox.m_max.z = 0.0;
}
}
return (rc && trim.m_pbox.IsValid()) ? true : false;
}
void ON_Brep::SetTolerancesBoxesAndFlags(
bool bLazy,
bool bSetVertexTolerances,
bool bSetEdgeTolerances,
bool bSetTrimTolerances,
bool bSetTrimIsoFlags,
bool bSetTrimTypeFlags,
bool bSetLoopTypeFlags,
bool bSetTrimBoxes
)
{
int ei, ti, li;
const int trim_count = m_T.Count();
const int loop_count = m_L.Count();
const int edge_count = m_E.Count();
if ( bSetVertexTolerances )
SetVertexTolerances(bLazy);
if ( bSetEdgeTolerances )
{
for ( ei = 0; ei < edge_count; ei++ )
SetEdgeTolerance(m_E[ei],bLazy);
}
if ( bSetTrimTolerances )
{
for ( ti = 0; ti < trim_count; ti++ )
SetTrimTolerance(m_T[ti],bLazy);
}
if ( bSetTrimIsoFlags )
SetTrimIsoFlags();
if ( bSetTrimTypeFlags )
SetTrimTypeFlags(bLazy);
if ( bSetTrimTypeFlags )
SetTrimTypeFlags(bLazy);
if ( bSetLoopTypeFlags )
{
for ( li = 0; li < loop_count; li++ )
{
ON_BrepLoop& loop = m_L[li];
if ( loop.m_type == ON_BrepLoop::unknown || !bLazy )
{
loop.m_type = ComputeLoopType( loop );
}
}
}
if ( bSetTrimBoxes )
SetTrimBoundingBoxes(bLazy);
}
static
bool CheckForMatchingVertexIndices( int i, int j, int corner_vi[4] )
{
if (corner_vi[i] == corner_vi[j])
return true;
bool rc = false;
if ( corner_vi[i] >= 0 || corner_vi[j] >= 0 )
{
if ( corner_vi[i] == -1 )
{
corner_vi[i] = corner_vi[j];
rc = true;
}
else if ( corner_vi[j] == -1 )
{
corner_vi[j] = corner_vi[i];
rc = true;
}
}
return rc;
}
ON_BrepFace* ON_Brep::NewFace(
ON_Surface* pSurface,
int vid[4],
int eid[4],
bool bRev3d[4]
)
{
m_bbox.Destroy();
m_is_solid = 0;
bool bAddedSurface = false;
ON_BrepFace* pFace = nullptr;
if ( !pSurface )
return nullptr;
int si;
for ( si = 0; si < m_S.Count(); si++ )
{
if ( pSurface == m_S[si] )
break;
}
if ( si >= m_S.Count() )
{
si = AddSurface(pSurface);
bAddedSurface = (si >= 0);
}
int face_index = NewFace(si).m_face_index;
if ( NewOuterLoop( face_index, vid, eid, bRev3d ) )
{
pFace = &m_F[face_index];
}
else
{
// failed
if ( bAddedSurface )
{
m_S[si] = 0;
if ( m_S.Count() == si+1 )
m_S.SetCount(si);
}
DeleteFace( m_F[face_index], false );
if ( m_F.Count() == face_index+1 )
{
m_F.SetCount(face_index);
}
}
return pFace;
}
ON_BrepLoop* ON_Brep::NewOuterLoop(
int face_index,
int vid[4],
int eid[4],
bool boolRev3d[4]
)
{
// 2 Sept 2020 S. Baer (RH-59952)
// Destroy cached bounding box on breps when messing around with loops
m_bbox.Destroy();
m_is_solid = 0;
if ( face_index < 0 || face_index >= m_F.Count() )
return nullptr;
ON_BrepFace& face = m_F[face_index];
const ON_Surface* pSurface = face.SurfaceOf();
if (!pSurface)
return nullptr;
double u[2], v[2];
if (!pSurface->GetDomain(0, &u[0], &u[1]))
return 0;
if (!pSurface->GetDomain(1, &v[0], &v[1]))
return 0;
ON_3dPoint srf_P[2][2];
if ( !pSurface->EvPoint(u[0],v[0],srf_P[0][0] ) )
return 0;
if ( !pSurface->EvPoint(u[1],v[0],srf_P[1][0] ) )
return 0;
if ( !pSurface->EvPoint(u[0],v[1],srf_P[0][1] ) )
return 0;
if ( !pSurface->EvPoint(u[1],v[1],srf_P[1][1] ) )
return 0;
bool bIsSingular[4]; // south, east, north, west
bool bIsClosed[2]; // u direction, v direction
int i, eti;
ON_Curve* c3[4] = {nullptr,nullptr,nullptr,nullptr};
int bRev3d[4] = { 0 };
for ( i = 0; i < 4; i++ )
{
if ( boolRev3d[i] )
bRev3d[i] = 1; // do this so we can use 1-bRev3d[i] as an array index
}
// check specified edge indices
for ( i = 0; i < 4; i++ )
{
if ( eid[i] != -1 )
{
if ( eid[i] < 0 || eid[i] >= m_E.Count() )
{
ON_ERROR("Bad edge index passed to ON_BrepNewFace.");
return 0;
}
const int* edge_vi = m_E[eid[i]].m_vi;
int vi0 = edge_vi[bRev3d[i]];
int vi1 = edge_vi[1-bRev3d[i]];
if ( vi0 < 0 || vi1 < 0 )
{
ON_ERROR("ON_Brep::NewFace(ON_Surface*,...) error: Bad edge vertex informtion.");
return 0;
}
if ( vid[i] == -1 )
vid[i] = vi0;
else if ( vid[i] != vi0 )
{
ON_ERROR("ON_Brep::NewFace(ON_Surface*,...) error: Edge and vertex informtion do not match.");
return 0;
}
if ( vid[(i+1)%4] == -1 )
vid[(i+1)%4] = vi1;
else if ( vid[(i+1)%4] != vi1 )
{
ON_ERROR("ON_Brep::NewFace(ON_Surface*,...) error: Edge and vertex informtion do not match.");
return 0;
}
}
}
// check specified vertex indices
for ( i = 0; i < 4; i++ )
{
if ( vid[i] != -1 )
{
if ( vid[i] < 0 || vid[i] >= m_V.Count() )
{
ON_ERROR("Bad vertex index passed to ON_Brep::NewFace.");
return 0;
}
}
}
for ( i = 0; i < 4; i++ )
bIsSingular[i] = pSurface->IsSingular(i);
for ( i = 0; i < 2; i++ )
bIsClosed[i] = pSurface->IsClosed(i);
for (i = 0; i < 2; i++ )
{
if ( bIsClosed[i] )
{
int j = i?0:1;
int k = j+2;
if ( eid[j] == -1 && eid[k] != -1)
{
eid[j] = eid[k];
bRev3d[j] = 1-bRev3d[k];
}
else if ( eid[k] == -1 && eid[j] != -1)
{
eid[k] = eid[j];
bRev3d[k] = 1-bRev3d[j];
}
else if ( eid[k] != -1 || eid[j] != -1)
{
if ( eid[j] != eid[k] || bRev3d[j] != 1-bRev3d[k] )
{
ON_ERROR("Bad edge information passed to ON_Brep::NewFace.");
return 0;
}
}
}
}
// if surface has singularities or is closed, make sure vertex and edge information is correct
for ( i = 0; i < 4; i++ )
{
if ( bIsSingular[i] )
{
if ( eid[i] != -1 || bRev3d[i] )
{
ON_ERROR("Bad edge information passed to ON_Brep::NewFace.");
return 0;
}
}
if ( bIsSingular[i] || bIsClosed[i%2] )
{
if ( !CheckForMatchingVertexIndices(i,(i+1)%4,vid) )
{
ON_ERROR("Bad vertex indices passed to ON_Brep::NewFace.");
return 0;
}
}
}
m_C3.Reserve( m_C3.Count() + 4 );
// create missing 3d curves
bool bEdgeIsClosed[4]; // true if 3d edge is closed or edge is singular.
for ( i = 0; i < 4; i++ )
{
bEdgeIsClosed[i] = false;
if ( eid[i] != -1 )
{
const ON_BrepEdge& edge = m_E[eid[i]];
bEdgeIsClosed[i] = (edge.m_vi[0] == edge.m_vi[1]);
continue;
}
if ( bIsSingular[i] )
{
bEdgeIsClosed[i] = true;
continue;
}
if ( i >= 2 && bIsClosed[(i==2)?1:0] )
{
bEdgeIsClosed[i] = bEdgeIsClosed[i-2];
continue;
}
switch(i)
{
case 0: // south side
c3[i] = pSurface->IsoCurve(i%2, v[0]);
break;
case 1: // east side
c3[i] = pSurface->IsoCurve(i%2, u[1]);
break;
case 2: // north side
c3[i] = pSurface->IsoCurve(i%2, v[1]);
break;
case 3: // west side
c3[i] = pSurface->IsoCurve(i%2, u[0]);
break;
}
if ( !c3[i] )
{
ON_ERROR("ON_Brep::NewLoop unable to make 3d edge curve.");
return 0;
}
if ( pSurface->IsClosed(i%2) )
bEdgeIsClosed[i] = true;
else
bEdgeIsClosed[i] = c3[i]->IsClosed()?true:false;
if ( (i <= 1 && bRev3d[i]) || (i >= 2 && !bRev3d[i]) )
{
c3[i]->Reverse();
}
}
if ( m_V.Capacity() < 2 )
m_V.Reserve(4);
// create missing vertices
if ( vid[0] == -1 )
{
if ( vid[1] >= 0 && bEdgeIsClosed[0] )
vid[0] = vid[1];
else if ( vid[3] >= 0 && bEdgeIsClosed[3] )
vid[0] = vid[3];
else
vid[0] = NewVertex( srf_P[0][0],0.0).m_vertex_index;
}
if ( vid[1] == -1 )
{
if ( bEdgeIsClosed[0] )
vid[1] = vid[0];
else if ( vid[2] >= 0 && bEdgeIsClosed[1] )
vid[1] = vid[2];
else
vid[1] = NewVertex(srf_P[1][0],0.0).m_vertex_index;
}
if ( vid[2] == -1 )
{
if ( bEdgeIsClosed[1] )
vid[2] = vid[1];
else if (vid[3] >= 0 && bEdgeIsClosed[2] )
vid[2] = vid[3];
else
vid[2] = NewVertex( srf_P[1][1],0.0).m_vertex_index;
}
if ( vid[3] == -1 )
{
if ( bEdgeIsClosed[2] )
vid[3] = vid[2];
else if ( bEdgeIsClosed[3] )
vid[3] = vid[0];
else
vid[3] = NewVertex( srf_P[0][1],0.0).m_vertex_index;
}
if ( m_E.Capacity() < 4 )
m_E.Reserve(4);
// create missing edges
for ( i = 0; i < 4; i++ )
{
if ( c3[i] )
{
int i0, i1;
if ( bRev3d[i] )
{
i0 = (i+1)%4;
i1 = i;
}
else
{
i0 = i;
i1 = (i+1)%4;
}
ON_BrepEdge& edge = NewEdge( m_V[vid[i0]], m_V[vid[i1]], AddEdgeCurve( c3[i] ) );
edge.m_tolerance = 0.0;
eid[i] = edge.m_edge_index;
if ( i == 0 && bIsClosed[1] )
{
eid[2] = eid[0];
bRev3d[2] = 1-bRev3d[0];
}
else if ( i == 1 && bIsClosed[0] )
{
eid[3] = eid[1];
bRev3d[3] = 1-bRev3d[1];
}
}
}
m_T.Reserve( m_T.Count() + 4 );
m_C2.Reserve( m_C2.Count() + 4 );
ON_BrepLoop& loop = NewLoop( ON_BrepLoop::outer, face );
loop.m_pbox.m_min.x = u[0];
loop.m_pbox.m_min.y = v[0];
loop.m_pbox.m_min.z = 0.0;
loop.m_pbox.m_max.x = u[1];
loop.m_pbox.m_max.y = v[1];
loop.m_pbox.m_max.z = 0.0;
ON_3dPoint corners[4];
corners[0].Set(u[0],v[0],0.0);
corners[1].Set(u[1],v[0],0.0);
corners[2].Set(u[1],v[1],0.0);
corners[3].Set(u[0],v[1],0.0);
ON_Surface::ISO srf_iso[4] = {ON_Surface::S_iso,ON_Surface::E_iso,ON_Surface::N_iso,ON_Surface::W_iso};
for ( i = 0; i < 4; i++ )
{
ON_NurbsCurve* c2 = new ON_NurbsCurve( 2, 0, 2, 2 );
c2->SetCV(0,corners[i]);
c2->SetCV(1,corners[(i+1)%4]);
c2->m_knot[0] = 0.0;
c2->m_knot[1] = 1.0;
if ( i%2 )
c2->SetDomain(v[0],v[1]);
else
c2->SetDomain(u[0],u[1]);
int c2i = AddTrimCurve( c2 );
if ( bIsSingular[i] )
NewSingularTrim( m_V[vid[i]],loop,srf_iso[i],c2i);
else
{
ON_BrepTrim& trim = NewTrim( m_E[eid[i]], bRev3d[i], loop, c2i);
trim.m_iso = srf_iso[i];
if ( bIsClosed[(i+1)%2] )
trim.m_type = ON_BrepTrim::seam;
else {
trim.m_type = ON_BrepTrim::boundary;
const ON_BrepEdge& edge = m_E[eid[i]];
if ( edge.m_ti.Count() > 1 )
{
for ( eti = 0; eti < edge.m_ti.Count(); eti++ )
{
m_T[edge.m_ti[eti]].m_type = ON_BrepTrim::mated;
}
}
}
trim.m_tolerance[0] = 0.0;
trim.m_tolerance[1] = 0.0;
trim.m__legacy_2d_tol = 0.0;
trim.m__legacy_3d_tol = 0.0;
trim.m__legacy_flags_Set(-1,1);
}
}
for ( i = 0; i < 4; i++ )
{
boolRev3d[i] = bRev3d[i] ? true : false;
}
return &m_L[loop.m_loop_index];
}
ON_Brep* ON_BrepBox( const ON_3dPoint* box_corners, ON_Brep* pBrep )
{
ON_Brep* brep = 0;
int vi, ei, fi, si, c2i;
if (box_corners)
{
if ( pBrep ) {
pBrep->Destroy();
brep = pBrep;
}
else
brep = new ON_Brep();
brep->m_C2.Reserve(24);
brep->m_C3.Reserve(12);
brep->m_S.Reserve(6);
brep->m_V.Reserve(8);
brep->m_E.Reserve(12);
brep->m_L.Reserve(6);
brep->m_T.Reserve(24);
brep->m_F.Reserve(6);
for ( vi = 0; vi < 8; vi++ )
{
brep->NewVertex( box_corners[vi], 0.0 );
}
for ( ei = 0; ei < 4; ei++ )
{
ON_BrepVertex& v0 = brep->m_V[ei];
ON_BrepVertex& v1 = brep->m_V[(ei+1)%4];
brep->m_C3.Append( new ON_LineCurve( v0.point, v1.point ) );
brep->NewEdge( v0, v1, ei, nullptr, 0.0 );
}
for ( ei = 4; ei < 8; ei++ )
{
ON_BrepVertex& v0 = brep->m_V[ei];
ON_BrepVertex& v1 = brep->m_V[ei==7?4:(ei+1)];
brep->m_C3.Append( new ON_LineCurve( v0.point, v1.point ) );
brep->NewEdge( v0, v1, ei, nullptr, 0.0 );
}
for ( ei = 8; ei < 12; ei++ )
{
ON_BrepVertex& v0 = brep->m_V[ei-8];
ON_BrepVertex& v1 = brep->m_V[ei-4];
brep->m_C3.Append( new ON_LineCurve( v0.point, v1.point ) );
brep->NewEdge( v0, v1, ei, nullptr, 0.0 );
}
/*
// v7_______e6_____v6
// |\ |\
// | e7 | e5
// | \ ______e4_____\
// e11 v4 | v5
// | | e10 |
// | | | |
// 3---|---e2-----2 e9
// \ e8 \ |
// e3 | e1 |
// \ | \ |
// \v0_____e0_____\v1
*/
struct {
int e[4], bRev[4];
} f[6] = {
{{0, 9, 4, 8}, {false, false, true, true}},
{{1,10, 5, 9}, {false, false, true, true}},
{{2,11, 6,10}, {false, false, true, true}},
{{3, 8, 7,11}, {false, false, true, true}},
{{3, 2, 1, 0}, {true, true, true, true}},
{{4, 5, 6, 7}, {false, false, false, false}}
};
for ( fi = 0; fi < 6; fi++ )
{
ON_BrepEdge& e0 = brep->m_E[f[fi].e[0]];
ON_BrepEdge& e1 = brep->m_E[f[fi].e[1]];
ON_BrepEdge& e2 = brep->m_E[f[fi].e[2]];
ON_BrepEdge& e3 = brep->m_E[f[fi].e[3]];
ON_BrepVertex& v0 = brep->m_V[e0.m_vi[f[fi].bRev[0]?1:0]];
ON_BrepVertex& v1 = brep->m_V[e1.m_vi[f[fi].bRev[1]?1:0]];
ON_BrepVertex& v2 = brep->m_V[e2.m_vi[f[fi].bRev[2]?1:0]];
ON_BrepVertex& v3 = brep->m_V[e3.m_vi[f[fi].bRev[3]?1:0]];
si = brep->AddSurface( ON_NurbsSurfaceQuadrilateral( v0.point, v1.point, v2.point, v3.point ) );
ON_Interval s = brep->m_S[si]->Domain(0);
ON_Interval t = brep->m_S[si]->Domain(1);
ON_2dPoint p0(s[0],t[0]);
ON_2dPoint p1(s[1],t[0]);
ON_2dPoint p2(s[1],t[1]);
ON_2dPoint p3(s[0],t[1]);
ON_BrepFace& face = brep->NewFace( si );
ON_BrepLoop& loop = brep->NewLoop( ON_BrepLoop::outer, face );
loop.m_pbox.m_min.x = s[0];
loop.m_pbox.m_min.y = t[0];
loop.m_pbox.m_min.z = 0.0;
loop.m_pbox.m_max.x = s[1];
loop.m_pbox.m_max.y = t[1];
loop.m_pbox.m_max.z = 0.0;
// south side of surface
c2i = brep->AddTrimCurve( new ON_LineCurve( p0, p1 ) );
ON_BrepTrim& trim0 = brep->NewTrim( e0, f[fi].bRev[0], loop, c2i );
trim0.m_tolerance[0] = 0.0;
trim0.m_tolerance[1] = 0.0;
trim0.m_type = (trim0.m_vi[0] != trim0.m_vi[1]) ? ON_BrepTrim::mated : ON_BrepTrim::singular;
trim0.m_iso = ON_Surface::S_iso;
// east side of surface
c2i = brep->AddTrimCurve( new ON_LineCurve( p1, p2 ) );
ON_BrepTrim& trim1 = brep->NewTrim( e1, f[fi].bRev[1], loop, c2i );
trim1.m_tolerance[0] = 0.0;
trim1.m_tolerance[1] = 0.0;
trim1.m_type = (trim1.m_vi[0] != trim1.m_vi[1]) ? ON_BrepTrim::mated : ON_BrepTrim::singular;
trim1.m_iso = ON_Surface::E_iso;
// north side of surface
c2i = brep->AddTrimCurve( new ON_LineCurve( p2, p3 ) );
ON_BrepTrim& trim2 = brep->NewTrim( e2, f[fi].bRev[2], loop, c2i );
trim2.m_tolerance[0] = 0.0;
trim2.m_tolerance[1] = 0.0;
trim2.m_type = (trim2.m_vi[0] != trim2.m_vi[1]) ? ON_BrepTrim::mated : ON_BrepTrim::singular;
trim2.m_iso = ON_Surface::N_iso;
// west side of surface
c2i = brep->AddTrimCurve( new ON_LineCurve( p3, p0 ) );
ON_BrepTrim& trim3 = brep->NewTrim( e3, f[fi].bRev[3], loop, c2i );
trim3.m_tolerance[0] = 0.0;
trim3.m_tolerance[1] = 0.0;
trim3.m_type = (trim3.m_vi[0] != trim3.m_vi[1]) ? ON_BrepTrim::mated : ON_BrepTrim::singular;
trim3.m_iso = ON_Surface::W_iso;
}
if ( !brep->IsValid() ) {
if ( pBrep )
pBrep->Destroy();
else
delete brep;
brep = 0;
}
}
else
brep = 0;
return brep;
}
ON_Brep* ON_BrepWedge( const ON_3dPoint* corners, ON_Brep* pBrep )
{
ON_Brep* brep = 0;
int vi, ei, ti, fi, si, c2i;
if(corners)
{
// use the one passed in or make a new one
if( pBrep )
{
pBrep->Destroy();
brep = pBrep;
}
else
brep = new ON_Brep();
brep->m_C2.Reserve(18);
brep->m_C3.Reserve(9);
brep->m_S.Reserve(5);
brep->m_V.Reserve(6);
brep->m_E.Reserve(9);
brep->m_L.Reserve(5);
brep->m_T.Reserve(18);
brep->m_F.Reserve(5);
// vertices
for ( vi = 0; vi < 6; vi++ )
{
brep->NewVertex( corners[vi], 0.0 );
}
// 3d edges around bottom e0 - e2
for ( ei = 0; ei < 3; ei++ )
{
ON_BrepVertex& v0 = brep->m_V[ei];
ON_BrepVertex& v1 = brep->m_V[(ei+1)%3];
brep->m_C3.Append( new ON_LineCurve( v0.point, v1.point ) );
brep->NewEdge( v0, v1, ei, nullptr, 0.0 );
}
// 3d edges around top e3 - e5
for ( ei = 3; ei < 6; ei++ )
{
ON_BrepVertex& v0 = brep->m_V[ei];
ON_BrepVertex& v1 = brep->m_V[ei==5?3:(ei+1)];
brep->m_C3.Append( new ON_LineCurve( v0.point, v1.point ) );
brep->NewEdge( v0, v1, ei, nullptr, 0.0 );
}
// 3d vertical edges e6 - e8
for ( ei = 6; ei < 9; ei++ )
{
ON_BrepVertex& v0 = brep->m_V[ei-6];
ON_BrepVertex& v1 = brep->m_V[ei-3];
brep->m_C3.Append( new ON_LineCurve( v0.point, v1.point ) );
brep->NewEdge( v0, v1, ei, nullptr, 0.0 );
}
/*
//