-
Notifications
You must be signed in to change notification settings - Fork 140
/
opennurbs_brep_extrude.cpp
1286 lines (1150 loc) · 38.7 KB
/
opennurbs_brep_extrude.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
void ON_BrepExtrudeHelper_ReserveSpace(
ON_Brep& brep,
int extruded_trim_count,
int cap_count
)
{
if ( extruded_trim_count >= 0 && cap_count >= 0 )
{
const int vertex_count0 = brep.m_V.Count();
const int trim_count0 = brep.m_T.Count();
const int loop_count0 = brep.m_L.Count();
const int edge_count0 = brep.m_E.Count();
const int face_count0 = brep.m_F.Count();
const int srf_count0 = brep.m_S.Count();
const int c2_count0 = brep.m_C2.Count();
const int c3_count0 = brep.m_C3.Count();
// the +1's are for open loops
brep.m_V.Reserve( vertex_count0 + extruded_trim_count + 1 );
brep.m_T.Reserve( trim_count0 + (4+cap_count)*extruded_trim_count );
brep.m_F.Reserve( face_count0 + extruded_trim_count + cap_count );
brep.m_E.Reserve( edge_count0 + 2*extruded_trim_count + 1 );
brep.m_L.Reserve( loop_count0 + extruded_trim_count + cap_count );
brep.m_S.Reserve( srf_count0 + extruded_trim_count + cap_count );
brep.m_C2.Reserve( c2_count0 + (4+cap_count)*extruded_trim_count );
brep.m_C3.Reserve( c3_count0 + 2*extruded_trim_count + 1 );
}
}
static
ON_SumSurface* ON_BrepExtrudeHelper_MakeSumSrf( const ON_Curve& path_curve,
const ON_BrepEdge& base_edge, bool bRev )
{
ON_SumSurface* sum_srf = 0;
// create side surface
if ( base_edge.ProxyCurve() )
{
ON_Curve* srf_path_curve = path_curve.DuplicateCurve();
ON_Curve* srf_base_curve = base_edge.DuplicateCurve();
if ( !bRev )
srf_base_curve->Reverse();
ON_3dPoint sum_basepoint = -ON_3dVector(srf_path_curve->PointAtStart());
sum_srf = new ON_SumSurface();
sum_srf->m_curve[0] = srf_base_curve;
sum_srf->m_curve[1] = srf_path_curve;
sum_srf->m_basepoint = sum_basepoint;
sum_srf->BoundingBox(); // fills in sum_srf->m_bbox
}
return sum_srf;
}
static
ON_NurbsSurface* ON_BrepExtrudeHelper_MakeConeSrf( const ON_3dPoint& apex_point,
const ON_BrepEdge& edge, bool bRev )
{
// The "s" parameter runs along the edge.
// The "t" parameter is the ruling parameter;
// t=0 is at the base_edge and t=max is at the apex.
// surface side location
// south base_edge
// east line from bRev?START:END of edge to apex
// north singular side at apex
// west line from bRev?END:START of edge to apex.
ON_NurbsSurface* cone_srf = new ON_NurbsSurface();
if ( cone_srf->CreateConeSurface( apex_point, edge ) )
{
if ( bRev )
cone_srf->Reverse(0);
// get a decent interval for the ruling parameter
double d = 0.0;
ON_Interval edom = edge.Domain();
ON_3dPoint pt;
int i, hint=0;
for ( i = 0; i <= 16; i++ )
{
if ( !edge.EvPoint( edom.ParameterAt(i/16.0), pt, 0, &hint ) )
continue;
if ( pt.DistanceTo(apex_point) > d )
d = pt.DistanceTo(apex_point);
}
if ( d > ON_SQRT_EPSILON )
cone_srf->SetDomain(1,0.0,d);
}
else
{
delete cone_srf;
cone_srf = 0;
}
return cone_srf;
}
static
bool ON_BrepExtrudeHelper_MakeSides(
ON_Brep& brep,
int loop_index,
const ON_Curve& path_curve,
bool bCap,
ON_SimpleArray<int>& side_face_index
)
{
int lti, ti, i, vid[4], eid[4];
bool bRev3d[4];
// indices of new faces appended to the side_face_index[] array
// (1 face index for each trim, -1 is used for singular trims)
// count number of new objects so we can grow arrays
// efficiently and use refs to dynamic array elements.
const int loop_trim_count = brep.m_L[loop_index].m_ti.Count();
if ( loop_trim_count == 0 )
return false;
// save input trim and edge counts for use below
const int trim_count0 = brep.m_T.Count();
const int edge_count0 = brep.m_E.Count();
ON_BrepExtrudeHelper_ReserveSpace( brep, loop_trim_count, bCap?1:0 );
side_face_index.Reserve( side_face_index.Count() + loop_trim_count); // index of new face above brep.m_L[loop_index].m_ti[lti]
int prev_face_index = -1;
int first_face_east_trim_index = -1;
for ( lti = 0; lti < loop_trim_count; lti++ )
{
ON_SumSurface* sum_srf = 0;
side_face_index.Append(-1);
ti = brep.m_L[loop_index].m_ti[lti];
if ( ti < 0 || ti >= trim_count0 )
continue;
for ( i = 0; i < 4; i++ )
{
vid[i] = -1;
eid[i] = -1;
}
bRev3d[0] = false;
bRev3d[1] = false;
bRev3d[2] = false;
bRev3d[3] = false;
// get side surface for new face
{
ON_BrepTrim& trim = brep.m_T[ti];
if ( trim.m_ei >= 0 && trim.m_ei < edge_count0 )
{
const ON_BrepEdge& base_edge = brep.m_E[trim.m_ei];
// 5 September, 2003 Dale Lear
// do not extrude seams - fixes rectangle slabe bug
if ( trim.m_type == ON_BrepTrim::seam )
{
prev_face_index = -1;
continue;
}
// connect new face to existing topology on trim
vid[0] = trim.m_vi[1];
vid[1] = trim.m_vi[0];
eid[0] = base_edge.m_edge_index;
bRev3d[0] = (trim.m_bRev3d?false:true);
sum_srf = ON_BrepExtrudeHelper_MakeSumSrf( path_curve, base_edge, trim.m_bRev3d );
}
}
if ( !sum_srf )
continue;
if ( prev_face_index >= 0 )
{
const ON_BrepTrim& prev_west_trim = brep.m_T[ brep.m_L[ brep.m_F[prev_face_index].m_li[0]].m_ti[3] ];
vid[2] = prev_west_trim.m_vi[0];
eid[1] = prev_west_trim.m_ei;
bRev3d[1] = (prev_west_trim.m_bRev3d?false:true);
}
if ( first_face_east_trim_index >= 0 && brep.m_T[first_face_east_trim_index].m_vi[0] == vid[0] )
{
const ON_BrepTrim& first_face_east_trim = brep.m_T[first_face_east_trim_index];
vid[3] = first_face_east_trim.m_vi[1];
eid[3] = first_face_east_trim.m_ei;
bRev3d[3] = (first_face_east_trim.m_bRev3d?false:true);
}
const ON_BrepFace* side_face = brep.NewFace(sum_srf,vid,eid,bRev3d);
if ( side_face )
{
*side_face_index.Last() = side_face->m_face_index;
prev_face_index = side_face->m_face_index;
if ( first_face_east_trim_index < 0 )
first_face_east_trim_index = brep.m_L[ side_face->m_li[0] ].m_ti[1];
}
}
return true;
}
static
bool ON_BrepExtrudeHelper_CheckPathCurve( const ON_Curve& path_curve, ON_3dVector& path_vector )
{
ON_Line path_line;
path_line.from = path_curve.PointAtStart();
path_line.to = path_curve.PointAtEnd();
path_vector = path_line.Direction();
return ( path_vector.IsZero() ? false : true );
}
static
bool ON_BrepExtrudeHelper_MakeTopLoop(
ON_Brep& brep,
ON_BrepFace& top_face,
int bottom_loop_index,
const ON_3dVector path_vector,
const int* side_face_index // array of brep.m_L[bottom_loop_index].m_ti.Count() face indices
)
{
bool rc = true;
int lti, top_trim_index, i;
if ( bottom_loop_index < 0 || bottom_loop_index >= brep.m_L.Count() )
return false;
ON_BrepLoop::TYPE loop_type = brep.m_L[bottom_loop_index].m_type;
if ( loop_type != ON_BrepLoop::inner )
loop_type = ON_BrepLoop::outer;
ON_BrepLoop& top_loop = brep.NewLoop( loop_type, top_face );
const ON_BrepLoop& bottom_loop = brep.m_L[bottom_loop_index];
const int loop_trim_count = bottom_loop.m_ti.Count();
brep.m_T.Reserve( brep.m_T.Count() + loop_trim_count );
// Set top_vertex_index[lti] = index of vertex above
// vertex brep.m_V[brep.m_T[bottom_loop.m_ti[lti]].m_vi[0]].
// Set top_vertex_index[lti] = index of edge above
// edge of brep.m_T[bottom_loop.m_ti[lti]].
// This informtion is needed for singular and seam trims.
ON_SimpleArray<int> top_vertex_index(loop_trim_count);
ON_SimpleArray<int> top_edge_index(loop_trim_count);
ON_SimpleArray<bool> top_trim_bRev3d(loop_trim_count);
for ( lti = 0; lti < loop_trim_count; lti++ )
{
top_vertex_index.Append(-1);
top_edge_index.Append(-1);
top_trim_bRev3d.Append(false);
}
// some (often all of) of the "top" vertices are already on
// the side faces
for ( lti = 0; lti < loop_trim_count; lti++ )
{
if ( side_face_index[lti] >= 0 )
{
const ON_BrepFace& side_face = brep.m_F[side_face_index[lti]];
const ON_BrepLoop& side_loop = brep.m_L[side_face.m_li[0]];
const ON_BrepTrim& side_north_trim = brep.m_T[side_loop.m_ti[2]];
top_vertex_index[lti] = side_north_trim.m_vi[0];
top_vertex_index[(lti+1)%loop_trim_count] = side_north_trim.m_vi[1];
top_edge_index[lti] = side_north_trim.m_ei;
}
else
{
// fix for RR 20423
int lti_prev = (lti+loop_trim_count-1)%loop_trim_count;
int lti_next = (lti+1)%loop_trim_count;
if ( side_face_index[lti_prev] < 0
&& side_face_index[lti_next] < 0
&& top_vertex_index[lti] < 0
&& top_vertex_index[lti_next] < 0
)
{
int bottom_ti_prev = bottom_loop.m_ti[lti_prev];
int bottom_ti = bottom_loop.m_ti[lti];
int bottom_ti_next = bottom_loop.m_ti[lti_next];
if ( bottom_ti >= 0 && bottom_ti < brep.m_T.Count()
&& bottom_ti_prev >= 0 && bottom_ti_prev < brep.m_T.Count()
&& bottom_ti_next >= 0 && bottom_ti_next < brep.m_T.Count()
)
{
const ON_BrepTrim& bottom_trim_prev = brep.m_T[bottom_ti_prev];
const ON_BrepTrim& bottom_trim = brep.m_T[bottom_ti];
const ON_BrepTrim& bottom_trim_next = brep.m_T[bottom_ti_next];
if ( ON_BrepTrim::seam == bottom_trim_prev.m_type
&& ON_BrepTrim::singular == bottom_trim.m_type
&& ON_BrepTrim::seam == bottom_trim_next.m_type
&& bottom_trim.m_vi[0] == bottom_trim.m_vi[1]
)
{
int vi = bottom_trim.m_vi[0];
if ( vi >= 0 && vi < brep.m_V.Count() )
{
ON_BrepVertex& top_vertex = brep.NewVertex(brep.m_V[vi].point+path_vector,0.0);
top_vertex_index[lti] = top_vertex.m_vertex_index;
top_vertex_index[lti_next] = top_vertex_index[lti];
}
}
}
}
}
}
// Fill in the missing "top" vertices that
// are associated with singular and trim edges by looking
// at their neighbors.
{
bool bKeepChecking = true;
while( bKeepChecking )
{
// set back to true if we make a change. This handles the
// (very rare) cases of multiple adjacent singular trims.
bKeepChecking = false;
for ( lti = 0; lti < loop_trim_count; lti++ )
{
if ( top_vertex_index[lti] == -1 )
{
for ( i = lti+1; i < loop_trim_count; i++ )
{
if ( ON_BrepTrim::singular != brep.m_T[bottom_loop.m_ti[i-1]].m_type )
break;
if ( top_vertex_index[i] >= 0 )
{
top_vertex_index[lti] = top_vertex_index[i];
bKeepChecking = true;
break;
}
}
}
if ( top_vertex_index[lti] == -1 )
{
for ( i = lti-1; i >= 0; i-- )
{
if ( ON_BrepTrim::singular != brep.m_T[bottom_loop.m_ti[i+1]].m_type )
break;
if ( top_vertex_index[i] >= 0 )
{
top_vertex_index[lti] = top_vertex_index[i];
bKeepChecking = true;
break;
}
}
}
}
}
}
// Fill in missing edges of "seam" trims.
for ( lti = 0; lti < loop_trim_count; lti++ )
{
if ( -1 != top_edge_index[lti] )
continue;
int bottom_ti = bottom_loop.m_ti[lti];
if ( bottom_ti < 0 || bottom_ti >= brep.m_T.Count() )
continue;
const ON_BrepTrim& bottom_trim = brep.m_T[bottom_ti];
if ( ON_BrepTrim::seam != bottom_trim.m_type )
continue;
if ( bottom_trim.m_ei < 0 )
continue;
if ( bottom_trim.m_ei >= brep.m_E.Count() )
continue;
// duplicate bottom edge curve
const ON_BrepEdge& bottom_edge = brep.m_E[bottom_trim.m_ei];
ON_Curve* top_c3 = bottom_edge.DuplicateCurve();
if ( 0 == top_c3 )
continue;
// move new edge curve to top location
top_c3->Translate(path_vector);
ON_3dPoint P0 = top_c3->PointAtStart();
ON_3dPoint P1 = top_c3->PointAtEnd();
int top_c3i = brep.AddEdgeCurve(top_c3);
top_c3 = 0;
// get vertices at start/end of the new edge
int e_vi0 = top_vertex_index[lti];
int e_vi1 = top_vertex_index[(lti+1)%loop_trim_count];
if ( bottom_trim.m_bRev3d )
{
// put points in trim order
ON_3dPoint tmp_P = P0; P0 = P1; P1 = tmp_P;
}
if ( e_vi0 < 0 )
{
e_vi0 = brep.NewVertex(P0).m_vertex_index;
top_vertex_index[lti] = e_vi0;
}
if ( e_vi1 < 0 )
{
e_vi1 = brep.NewVertex(P1).m_vertex_index;
top_vertex_index[(lti+1)%loop_trim_count] = e_vi1;
}
if ( bottom_trim.m_bRev3d )
{
// put edge vertex indices in edge order
int tmp_i = e_vi0; e_vi0 = e_vi1; e_vi1 = tmp_i;
}
ON_BrepEdge& top_edge = brep.NewEdge(brep.m_V[e_vi0],brep.m_V[e_vi1],top_c3i);
top_edge.m_tolerance = bottom_edge.m_tolerance;
top_edge_index[lti] = top_edge.m_edge_index;
top_trim_bRev3d[lti] = bottom_trim.m_bRev3d?true:false;
// find seam mate and set it's
// top_edge_index[] to top_edge.m_edge_index.
int mate_lti;
for( mate_lti = lti+1; mate_lti < loop_trim_count; mate_lti++ )
{
if ( top_edge_index[mate_lti] != -1 )
continue;
int bottom_mate_ti = bottom_loop.m_ti[mate_lti];
if ( bottom_mate_ti < 0 || bottom_mate_ti >= brep.m_T.Count() )
continue;
const ON_BrepTrim& bottom_mate_trim = brep.m_T[bottom_mate_ti];
if ( bottom_mate_trim.m_type != ON_BrepTrim::seam )
continue;
if ( bottom_mate_trim.m_ei != bottom_trim.m_ei )
continue;
top_edge_index[mate_lti] = top_edge.m_edge_index;
top_trim_bRev3d[mate_lti] = bottom_mate_trim.m_bRev3d?true:false;
break;
}
}
for ( lti = 0; lti < loop_trim_count; lti++ )
{
const ON_BrepTrim& bottom_trim = brep.m_T[ bottom_loop.m_ti[lti] ];
ON_Curve* top_c2 = bottom_trim.DuplicateCurve();
int top_c2i = (0!=top_c2) ? brep.AddTrimCurve(top_c2) : bottom_trim.m_c2i;
top_trim_index = -1;
if ( bottom_trim.m_type == ON_BrepTrim::singular && top_vertex_index[lti] >= 0 )
{
top_trim_index = brep.NewSingularTrim(brep.m_V[top_vertex_index[lti]], top_loop, bottom_trim.m_iso, top_c2i ).m_trim_index;
}
else if ( bottom_trim.m_type != ON_BrepTrim::singular && top_edge_index[lti] >= 0 && top_edge_index[lti] < brep.m_E.Count() )
{
ON_BrepEdge& top_edge = brep.m_E[top_edge_index[lti]];
top_trim_index = brep.NewTrim( top_edge, top_trim_bRev3d[lti], top_loop, top_c2i ).m_trim_index;
}
else
{
ON_ERROR("ON_BrepExtrudeHelper_MakeTopLoop ran into capping trouble.");
rc = false;
break;
}
ON_BrepTrim& top_trim = brep.m_T[top_trim_index];
top_trim.m_pline = bottom_trim.m_pline;
top_trim.m_pbox = bottom_trim.m_pbox;
top_trim.m_iso = bottom_trim.m_iso;
top_trim.m_type = bottom_trim.m_type;
top_trim.m_tolerance[0] = bottom_trim.m_tolerance[0];
top_trim.m_tolerance[1] = bottom_trim.m_tolerance[1];
top_trim.m__legacy_2d_tol = bottom_trim.m__legacy_2d_tol;
top_trim.m__legacy_3d_tol = bottom_trim.m__legacy_2d_tol;
top_trim.m__legacy_flags = bottom_trim.m__legacy_flags;
}
if (rc)
{
top_loop.m_pbox = bottom_loop.m_pbox;
}
return rc;
}
static
bool ON_BrepExtrudeHelper_CheckLoop( const ON_Brep& brep, int loop_index )
{
bool rc = false;
if ( loop_index >= 0 )
{
ON_BrepLoop::TYPE loop_type = brep.m_L[loop_index].m_type;
if ( loop_type == ON_BrepLoop::inner || loop_type == ON_BrepLoop::outer )
rc = true;
}
return rc;
}
static
bool ON_BrepExtrudeHelper_MakeCap(
ON_Brep& brep,
int bottom_loop_index,
const ON_3dVector path_vector,
const int* side_face_index
)
{
bool bCap = true;
// make cap
if ( !ON_BrepExtrudeHelper_CheckLoop( brep, bottom_loop_index ) )
return false;
brep.m_F.Reserve(brep.m_F.Count() + 1);
brep.m_L.Reserve(brep.m_L.Count() + 1);
const ON_BrepLoop& bottom_loop = brep.m_L[bottom_loop_index];
const ON_BrepFace& bottom_face = brep.m_F[bottom_loop.m_fi];
const ON_Surface* bottom_surface = bottom_face.SurfaceOf();
ON_Surface* top_surface = bottom_surface->Duplicate();
top_surface->Translate( path_vector );
int top_surface_index = brep.AddSurface( top_surface );
ON_BrepFace& top_face = brep.NewFace( top_surface_index );
bCap = ON_BrepExtrudeHelper_MakeTopLoop( brep, top_face, bottom_loop_index, path_vector, side_face_index );
if ( bCap )
{
ON_BrepLoop& top_loop = brep.m_L[brep.m_L.Count()-1];
if ( bottom_loop.m_type == ON_BrepLoop::inner )
{
// we capped an inner boundary
// top_loop.m_type = ON_BrepLoop::outer; // done in ON_BrepExtrudeHelper_MakeTopLoop
brep.FlipLoop(top_loop);
}
else if ( bottom_loop.m_type == ON_BrepLoop::outer )
{
// we capped an outer boundary
// top_loop.m_type = ON_BrepLoop::outer; // done in ON_BrepExtrudeHelper_MakeTopLoop
brep.FlipFace(top_face);
}
}
else
{
// delete partially made cap face
brep.DeleteFace( top_face, false );
delete brep.m_S[top_surface_index];
brep.m_S[top_surface_index] = 0;
}
return bCap;
}
int ON_BrepExtrudeFace(
ON_Brep& brep,
int face_index,
const ON_Curve& path_curve,
bool bCap
)
{
int rc = 0; // returns 1 for success with no cap, 2 for success with a cap
brep.DestroyMesh(ON::any_mesh);
brep.DestroyRegionTopology();
if ( face_index < 0 || face_index >= brep.m_F.Count() )
return false;
const int face_loop_count = brep.m_F[face_index].m_li.Count();
if ( face_loop_count < 1 )
return false;
if ( brep.m_F[face_index].m_li.Count() == 1 )
{
rc = ON_BrepExtrudeLoop( brep, brep.m_F[face_index].m_li[0], path_curve, bCap );
}
else
{
ON_3dVector path_vector;
ON_SimpleArray<int> side_face_index;
ON_SimpleArray<int> side_face_index_loop_mark;
int li, fli;
if ( !ON_BrepExtrudeHelper_CheckPathCurve( path_curve, path_vector ) )
return 0;
//const int trim_count0 = brep.m_T.Count();
const int loop_count0 = brep.m_L.Count();
const int face_count0 = brep.m_F.Count();
// count number of new objects so we can grow arrays
// efficiently and use refs to dynamic array elements.
int new_side_trim_count = 0;
for ( fli = 0; fli < face_loop_count; fli++ )
{
li = brep.m_F[face_index].m_li[fli];
if ( li < 0 || li >= loop_count0 )
return false;
if ( !ON_BrepExtrudeHelper_CheckLoop( brep, li ) )
continue;
new_side_trim_count += brep.m_L[li].m_ti.Count();
}
if ( new_side_trim_count == 0 )
return false;
ON_BrepExtrudeHelper_ReserveSpace( brep, new_side_trim_count, bCap?1:0 );
side_face_index.Reserve(new_side_trim_count);
side_face_index_loop_mark.Reserve(face_loop_count);
const ON_BrepFace& face = brep.m_F[face_index];
rc = true;
int outer_loop_index = -1;
int outer_fli = -1;
for ( fli = 0; fli < face_loop_count && rc; fli++ )
{
side_face_index_loop_mark.Append( side_face_index.Count() );
li = face.m_li[fli];
if ( !ON_BrepExtrudeHelper_CheckLoop( brep, li ) )
continue;
ON_BrepLoop& loop = brep.m_L[li];
if ( bCap && loop.m_type == ON_BrepLoop::outer )
{
if ( outer_loop_index >= 0 )
bCap = false;
else
{
outer_loop_index = li;
outer_fli = fli;
}
}
rc = ON_BrepExtrudeHelper_MakeSides( brep, li, path_curve, bCap, side_face_index );
}
if ( bCap && rc && outer_loop_index >= 0 )
{
const int face_count1 = brep.m_F.Count();
bCap = ON_BrepExtrudeHelper_MakeCap(
brep,
outer_loop_index,
path_vector,
side_face_index.Array() + side_face_index_loop_mark[outer_fli] );
if ( bCap && brep.m_F.Count() > face_count1)
{
// put inner bondaries on the cap
rc = 2;
ON_BrepFace& cap_face = brep.m_F[brep.m_F.Count()-1];
for ( fli = 0; fli < face_loop_count && rc; fli++ )
{
li = face.m_li[fli];
if ( li == outer_loop_index )
continue;
if ( !ON_BrepExtrudeHelper_CheckLoop( brep, li ) )
continue;
if ( ON_BrepExtrudeHelper_MakeTopLoop(
brep,
cap_face,
li,
path_vector,
side_face_index.Array() + side_face_index_loop_mark[fli] ) )
{
ON_BrepLoop& top_loop = brep.m_L[brep.m_L.Count()-1];
top_loop.m_type = brep.m_L[li].m_type;
}
}
}
}
if ( brep.m_F[face_index].m_bRev )
{
for ( int fi = face_count0; fi < brep.m_F.Count(); fi++ )
{
brep.FlipFace(brep.m_F[fi]);
}
}
}
return rc;
}
int ON_BrepExtrudeLoop(
ON_Brep& brep,
int loop_index,
const ON_Curve& path_curve,
bool bCap
)
{
ON_SimpleArray<int> side_face_index; // index of new face above brep.m_L[loop_index].m_ti[lti]
ON_3dVector path_vector;
brep.DestroyMesh(ON::any_mesh);
brep.DestroyRegionTopology();
const int face_count0 = brep.m_F.Count();
if ( loop_index < 0 || loop_index >= brep.m_L.Count() )
return false;
if ( !ON_BrepExtrudeHelper_CheckPathCurve(path_curve,path_vector) )
return false;
// can only cap closed loops ( for now, just test for inner and outer loops).
if ( brep.m_L[loop_index].m_type != ON_BrepLoop::outer && brep.m_L[loop_index].m_type != ON_BrepLoop::inner )
bCap = false;
// make sides
if ( !ON_BrepExtrudeHelper_MakeSides( brep, loop_index, path_curve, bCap, side_face_index ) )
return false;
// make cap
if ( bCap )
bCap = ON_BrepExtrudeHelper_MakeCap( brep, loop_index, path_vector, side_face_index.Array() );
const ON_BrepLoop& loop = brep.m_L[loop_index];
if ( loop.m_fi >= 0 && loop.m_fi < brep.m_F.Count() && brep.m_F[loop.m_fi].m_bRev )
{
for ( int fi = face_count0; fi < brep.m_F.Count(); fi++ )
{
brep.FlipFace( brep.m_F[fi] );
}
}
return (bCap?2:1);
}
int ON_BrepExtrudeEdge(
ON_Brep& brep,
int edge_index,
const ON_Curve& path_curve
)
{
ON_3dVector path_vector;
brep.DestroyMesh(ON::any_mesh);
brep.DestroyRegionTopology();
if ( edge_index < 0 && edge_index >= brep.m_E.Count() )
return false;
if ( !ON_BrepExtrudeHelper_CheckPathCurve(path_curve,path_vector) )
return false;
// make sides
bool bRev = false;
ON_SumSurface* sum_srf = ON_BrepExtrudeHelper_MakeSumSrf(
path_curve, brep.m_E[edge_index], bRev );
if ( !sum_srf )
return false;
int vid[4], eid[4];
bool bRev3d[4];
vid[0] = brep.m_E[edge_index].m_vi[bRev?0:1];
vid[1] = brep.m_E[edge_index].m_vi[bRev?1:0];
vid[2] = -1;
vid[3] = -1;
eid[0] = edge_index; // "south side edge"
eid[1] = -1;
eid[2] = -1;
eid[3] = -1;
bRev3d[0] = bRev ? false : true;
bRev3d[1] = false;
bRev3d[2] = false;
bRev3d[3] = false;
return brep.NewFace( sum_srf, vid, eid, bRev3d ) ? true : false;
}
bool ON_BrepExtrude(
ON_Brep& brep,
const ON_Curve& path_curve,
bool bCap
)
{
ON_Workspace ws;
const int vcount0 = brep.m_V.Count();
const int tcount0 = brep.m_T.Count();
const int lcount0 = brep.m_L.Count();
const int ecount0 = brep.m_E.Count();
const int fcount0 = brep.m_F.Count();
brep.DestroyMesh(ON::any_mesh);
brep.DestroyRegionTopology();
const ON_3dPoint PathStart = path_curve.PointAtStart();
ON_3dPoint P = path_curve.PointAtEnd();
if ( !PathStart.IsValid() || !P.IsValid() )
return false;
const ON_3dVector height = P - PathStart;
if ( !height.IsValid() || height.Length() <= ON_ZERO_TOLERANCE )
return false;
ON_Xform tr(ON_Xform::TranslationTransformation(height));
// count number of new sides
int side_count = 0;
int i, vi, ei, fi;
bool* bSideEdge = (bool*)ws.GetIntMemory(ecount0*sizeof(bSideEdge[0]));
for ( ei = 0; ei < ecount0; ei++ )
{
const ON_BrepEdge& e = brep.m_E[ei];
if ( 1 == e.m_ti.Count() )
{
side_count++;
bSideEdge[ei] = true;
}
else
{
bSideEdge[ei] = false;
}
}
brep.m_V.Reserve( 2*vcount0 );
i = 4*side_count + (bCap?tcount0:0);
brep.m_T.Reserve( tcount0 + i );
brep.m_C2.Reserve( brep.m_C2.Count() + i );
brep.m_L.Reserve( lcount0 + side_count + (bCap?lcount0:0) );
i = side_count + (bCap?ecount0:side_count);
if (side_count == 1)//NewFace(srf,vid,eid,bRev3d), down below, always reserves 4 edges.
i++;
brep.m_E.Reserve( ecount0 + i );
brep.m_C3.Reserve( brep.m_C3.Count() + i );
i = side_count + (bCap?fcount0:0);
brep.m_F.Reserve( fcount0 + i );
brep.m_S.Reserve( brep.m_S.Count() + i );
bool bOK = true;
// build top vertices
int* topvimap = ws.GetIntMemory(vcount0);
memset(topvimap,0,vcount0*sizeof(topvimap[0]));
if ( bCap )
{
for ( vi = 0; vi < vcount0; vi++ )
{
const ON_BrepVertex& bottomv = brep.m_V[vi];
ON_BrepVertex& topv = brep.NewVertex(bottomv.point+height,bottomv.m_tolerance);
topvimap[vi] = topv.m_vertex_index;
}
}
else
{
for ( ei = 0; ei < ecount0; ei++ )
{
if ( bSideEdge[ei] )
{
const ON_BrepEdge& bottome = brep.m_E[ei];
int bottomvi0 = bottome.m_vi[0];
if ( bottomvi0 < 0 || bottomvi0 >= vcount0 )
{
bOK = false;
break;
}
int bottomvi1 = bottome.m_vi[1];
if ( bottomvi1 < 0 || bottomvi1 >= vcount0 )
{
bOK = false;
break;
}
if ( !topvimap[bottomvi0] )
{
const ON_BrepVertex& bottomv = brep.m_V[bottomvi0];
ON_BrepVertex& topv = brep.NewVertex(bottomv.point+height,bottomv.m_tolerance);
topvimap[bottomvi0] = topv.m_vertex_index;
}
if ( !topvimap[bottomvi1] )
{
const ON_BrepVertex& bottomv = brep.m_V[bottomvi1];
ON_BrepVertex& topv = brep.NewVertex(bottomv.point+height,bottomv.m_tolerance);
topvimap[bottomvi1] = topv.m_vertex_index;
}
}
}
}
// build top edges
int* topeimap = ws.GetIntMemory(ecount0);
memset(topeimap,0,ecount0*sizeof(topeimap[0]));
if ( bOK ) for ( ei = 0; ei < ecount0; ei++ )
{
if ( bCap || bSideEdge[ei] )
{
const ON_BrepEdge& bottome = brep.m_E[ei];
ON_BrepVertex& topv0 = brep.m_V[topvimap[bottome.m_vi[0]]];
ON_BrepVertex& topv1 = brep.m_V[topvimap[bottome.m_vi[1]]];
ON_Curve* c3 = bottome.DuplicateCurve();
if ( !c3 )
{
bOK = false;
break;
}
c3->Transform(tr);
int c3i = brep.AddEdgeCurve(c3);
ON_BrepEdge& tope = brep.NewEdge(topv0,topv1,c3i,0,bottome.m_tolerance);
topeimap[ei] = tope.m_edge_index;
}
}
// build side edges
int* sideveimap = ws.GetIntMemory(vcount0);
memset(sideveimap,0,vcount0*sizeof(sideveimap[0]));
if ( bOK ) for ( vi = 0; vi < vcount0; vi++ )
{
ON_BrepVertex& bottomv = brep.m_V[vi];
for ( int vei = 0; vei < bottomv.m_ei.Count(); vei++ )
{
if ( bSideEdge[bottomv.m_ei[vei]] && topvimap[vi] )
{
ON_BrepVertex& topv = brep.m_V[topvimap[vi]];
ON_Curve* c3 = path_curve.DuplicateCurve();
if ( !c3 )
{
bOK = false;
}
else
{
ON_3dVector D = bottomv.point - PathStart;
c3->Translate(D);
int c3i = brep.AddEdgeCurve(c3);
const ON_BrepEdge& e = brep.NewEdge(bottomv,topv,c3i,0,0.0);
sideveimap[vi] = e.m_edge_index;
}
break;
}
}
}
if ( bOK && bCap )
{
// build top faces
for (fi = 0; fi < fcount0; fi++ )
{
const ON_BrepFace& bottomf = brep.m_F[fi];
ON_Surface* srf = bottomf.DuplicateSurface();
if ( !srf )
{
bOK = false;
break;
}
srf->Transform(tr);
int si = brep.AddSurface(srf);
ON_BrepFace& topf = brep.NewFace(si);
topf.m_bRev = !bottomf.m_bRev;
const int loop_count = bottomf.m_li.Count();
topf.m_li.Reserve(loop_count);
for ( int fli = 0; fli < loop_count; fli++ )
{
const ON_BrepLoop& bottoml = brep.m_L[bottomf.m_li[fli]];
ON_BrepLoop& topl = brep.NewLoop(bottoml.m_type,topf);
const int loop_trim_count = bottoml.m_ti.Count();
topl.m_ti.Reserve(loop_trim_count);
for ( int lti = 0; lti < loop_trim_count; lti++ )
{
const ON_BrepTrim& bottomt = brep.m_T[bottoml.m_ti[lti]];
ON_NurbsCurve* c2 = ON_NurbsCurve::New();
if ( !bottomt.GetNurbForm(*c2) )
{
delete c2;
bOK = false;
break;
}
int c2i = brep.AddTrimCurve(c2);
ON_BrepTrim* topt = 0;
if ( bottomt.m_ei >= 0 )
{
ON_BrepEdge& tope = brep.m_E[topeimap[bottomt.m_ei]];
topt = &brep.NewTrim(tope,bottomt.m_bRev3d,topl,c2i);
}
else
{
// singular trim
ON_BrepVertex& topv = brep.m_V[topvimap[bottomt.m_vi[0]]];
topt = &brep.NewSingularTrim(topv,topl,bottomt.m_iso,c2i);
}
topt->m_tolerance[0] = bottomt.m_tolerance[0];
topt->m_tolerance[1] = bottomt.m_tolerance[1];
topt->m_pbox = bottomt.m_pbox;
topt->m_type = bottomt.m_type;
topt->m_iso = bottomt.m_iso;
}
topl.m_pbox = bottoml.m_pbox;
}
}
}
// build sides
bool bRev3d[4] = {false,false,true,true};
int vid[4], eid[4];
if( bOK ) for ( ei = 0; ei < ecount0; ei++ )
{
if ( bSideEdge[ei] && topeimap[ei] )
{
ON_BrepEdge& bottome = brep.m_E[ei];
ON_BrepEdge& tope = brep.m_E[topeimap[ei]];
vid[0] = bottome.m_vi[0];
vid[1] = bottome.m_vi[1];
vid[2] = topvimap[vid[1]];
vid[3] = topvimap[vid[0]];