-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathopennurbs_subd_fragment.cpp
4487 lines (3934 loc) · 129 KB
/
opennurbs_subd_fragment.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
#include "opennurbs_subd_data.h"
/////////////////////////////////////////////////////////////////////////////////////////
//
// ON_SubDMeshFragment
//
void ON_SubDMeshFragment::Internal_Set3dPointArrayToNan(double* a, size_t a_count, size_t a_stride)
{
if (nullptr == a || a_count <= 0 || a_stride < 3)
return;
double *a2 = a + a_count * a_stride;
if (3 == a_stride)
{
while (a < a2)
{
*a++ = ON_DBL_QNAN;
*a++ = ON_DBL_QNAN;
*a++ = ON_DBL_QNAN;
}
}
else
{
a_stride -= 3;
while (a < a2)
{
*a++ = ON_DBL_QNAN;
*a++ = ON_DBL_QNAN;
*a++ = ON_DBL_QNAN;
a += a_stride;
}
}
}
unsigned ON_SubDMeshFragment::VertexCapacity() const
{
return (m_vertex_capacity_etc & ON_SubDMeshFragment::ValueMask);
}
void ON_SubDMeshFragment::Internal_LayoutArrays(
size_t vertex_capacity,
double* PNTKC_array
)
{
const bool bManagedArray = (vertex_capacity >= 4 && nullptr == PNTKC_array);
if (bManagedArray)
{
const size_t double_capacity = ON_SubDMeshFragment::DoublesPerVertex * vertex_capacity;
// PNTKC_array will be deleted in ON_SubDMeshFragment::DeleteManagedArrays().
PNTKC_array = new(std::nothrow) double[double_capacity];
}
this->SetVertexCount(0);
if (nullptr == PNTKC_array || vertex_capacity < 4 || vertex_capacity > ((size_t)ON_SubDMeshFragment::MaximumVertexCount) )
{
PNTKC_array = nullptr;
vertex_capacity = 0;
}
if (bManagedArray)
{
unsigned short etc = (m_vertex_capacity_etc & ON_SubDMeshFragment::EtcMask);
etc |= ON_SubDMeshFragment::EtcManagedArraysBit;
m_vertex_capacity_etc = (unsigned short)vertex_capacity;
m_vertex_capacity_etc |= etc;
}
else
{
this->SetUnmanagedVertexCapacityForExperts(vertex_capacity);
}
// If needed, bInterlaced can become a parameter in this private function.
// bInterlaced - [in]
// True if the points, normals, textures, colors, and curvatures should be interlaced.
// False if the points, normals, textures, colors, and curvatures should be in
// contiguous memory.
const bool bInterlaced = false;
// stride_PNT = number of double elements to get from m_P[i] to m_P[i+1] (dittof for m_N[] and m_T[])
const size_t stride_PNT
= (nullptr != PNTKC_array)
? (bInterlaced ? ON_SubDMeshFragment::DoublesPerVertex : 3)
: 0;
// offset_PNT = offset between start of m_P, m_N, m_T, m_K
const size_t offset_PNT
= (nullptr != PNTKC_array)
? 3 * (bInterlaced ? 1 : vertex_capacity)
: 0;
// stride_K = number of ON_SubDSurfaceCurvature elements to get from m_K[i] to m_K[i+1]
const size_t stride_K
= (nullptr != PNTKC_array)
? (bInterlaced ? (ON_SubDMeshFragment::DoublesPerVertex * sizeof(double)) / sizeof(m_K[0]) : 1)
: 0;
// offset_K = offset between m_K and m_C
const size_t offset_K
= (nullptr != PNTKC_array)
? (bInterlaced ? 1 : vertex_capacity)
: 0;
// stride_C = number of ON_Color elements to get from m_C[i] to m_C[i+1]
const size_t stride_C
= (nullptr != PNTKC_array)
? (bInterlaced ? (ON_SubDMeshFragment::DoublesPerVertex * sizeof(double)) / sizeof(m_C[0]) : 1)
: 0;
m_P = PNTKC_array;
m_P_stride = stride_PNT;
m_N = m_P + offset_PNT;
m_N_stride = stride_PNT;
m_T = m_N + offset_PNT;
m_T_stride = stride_PNT;
m_K = (ON_SurfaceCurvature*)(m_T + offset_PNT);
m_K_stride = stride_K;
m_C = (ON_Color*)(m_K + offset_K);
m_C_stride = stride_C;
}
bool ON_SubDMeshFragment::ManagedArrays() const
{
const size_t vertex_capacity = (ON_SubDMeshFragment::ValueMask & m_vertex_capacity_etc);
return (
0 != (ON_SubDMeshFragment::EtcManagedArraysBit & m_vertex_capacity_etc)
&& vertex_capacity > 0
&& nullptr != m_P
&& (ON__UINT_PTR)m_N > (ON__UINT_PTR)m_P
&& (ON__UINT_PTR)m_T > (ON__UINT_PTR)m_N
&& (ON__UINT_PTR)m_K > (ON__UINT_PTR)m_T
&& (ON__UINT_PTR)m_C > (ON__UINT_PTR)m_K
) ? true : false;
}
bool ON_SubDMeshFragment::DeleteManagedArrays()
{
if (ManagedArrays())
{
double* managed_array = m_P;
m_vertex_count_etc &= ON_SubDMeshFragment::EtcControlNetQuadBit;
m_vertex_capacity_etc = 0;
m_P = nullptr;
m_N = nullptr;
m_T = nullptr;
m_C = nullptr;
m_K = nullptr;
m_P_stride = 0;
m_N_stride = 0;
m_T_stride = 0;
m_C_stride = 0;
m_K_stride = 0;
if (nullptr != managed_array)
{
// Allocated in ON_SubDMeshFragment::ReserveManagedVertexCapacity()
delete[] managed_array;
}
return true;
}
return false;
}
bool ON_SubDMeshFragment::UnmanagedArrays() const
{
const size_t vertex_capacity = (ON_SubDMeshFragment::ValueMask & m_vertex_capacity_etc);
return (
0 == (ON_SubDMeshFragment::EtcManagedArraysBit & m_vertex_capacity_etc)
&& vertex_capacity > 0
&& nullptr != m_P
) ? true : false;
}
bool ON_SubDMeshFragment::SetUnmanagedVertexCapacityForExperts(size_t vertex_capacity)
{
// Do not check for a non-null m_P.
// This function is used in expert situations when m_P is null.
if (vertex_capacity < 0 || vertex_capacity > (size_t)ON_SubDMeshFragment::MaximumVertexCount)
return ON_SUBD_RETURN_ERROR(false);
if (ManagedArrays())
{
// attempting to convert internally managed memory to externally managed memory
return ON_SUBD_RETURN_ERROR(false);
}
unsigned short etc = m_vertex_capacity_etc &= ON_SubDMeshFragment::EtcMask;
etc &= ~ON_SubDMeshFragment::EtcManagedArraysBit;
m_vertex_capacity_etc = ((unsigned short)vertex_capacity) | etc;
return true;
}
bool ON_SubDMeshFragment::ReserveManagedVertexCapacity(size_t vertex_capacity)
{
if (vertex_capacity < 0 || vertex_capacity >(size_t)ON_SubDMeshFragment::MaximumVertexCount)
return ON_SUBD_RETURN_ERROR(false);
if (vertex_capacity > (size_t)(ON_SubDMeshFragment::ValueMask))
return ON_SUBD_RETURN_ERROR(false); // too big
if (UnmanagedArrays())
{
// attempting to convert externally managed memory to internally managed memory.
return ON_SUBD_RETURN_ERROR(false);
}
const size_t current_capacity = (size_t)(ON_SubDMeshFragment::ValueMask & m_vertex_capacity_etc);
if (ManagedArrays())
{
if (current_capacity >= vertex_capacity)
return true;
DeleteManagedArrays();
}
Internal_LayoutArrays( vertex_capacity, nullptr );
return (this->VertexCapacity() >= vertex_capacity);
}
unsigned ON_SubDMeshFragment::VertexCount() const
{
return (m_vertex_count_etc & ON_SubDMeshFragment::ValueMask);
}
bool ON_SubDMeshFragment::SetVertexCount(size_t vertex_count)
{
if (0 == vertex_count)
{
Clear();
}
else
{
// Do not check for non-null m_P. This function is called in situation where m_P is nullptr.
if (vertex_count < 0 || vertex_count > VertexCapacity())
return ON_SUBD_RETURN_ERROR(false);
const unsigned short etc = m_vertex_count_etc & ON_SubDMeshFragment::EtcMask;
m_vertex_count_etc = ((unsigned short)vertex_count) | etc;
}
return true;
}
void ON_SubDMeshFragment::Clear() ON_NOEXCEPT
{
m_face = nullptr;
m_face_vertex_index[0] = 0;
m_face_vertex_index[1] = 0;
m_face_vertex_index[2] = 0;
m_face_vertex_index[3] = 0;
m_face_fragment_count = 0;
m_face_fragment_index = 0;
// This line sets vertex count = 0
m_vertex_count_etc &= ON_SubDMeshFragment::EtcMask;
// Do NOT change vertex capacity.
// Do NOT delete managed arrays.
// The point is to be able to clear and reuse a fragment.
ClearTextureCoordinates();
ClearCurvatures();
ClearColors();
ClearControlNetQuad();
ClearPackRect();
m_grid = ON_SubDMeshFragmentGrid::Empty;
m_surface_bbox = ON_BoundingBox::NanBoundingBox;
}
unsigned int ON_SubDMeshFragment::PointCount() const
{
return (nullptr != m_P && m_P_stride >= 3) ? VertexCount() : 0U;
}
unsigned int ON_SubDMeshFragment::NormalCount() const
{
return (nullptr != m_N && m_N_stride >= 3) ? VertexCount() : 0U;
}
unsigned int ON_SubDMeshFragment::CurvatureCount() const
{
return (CurvaturesExistForExperts() && nullptr != m_K && m_K_stride >= 1) ? VertexCount() : 0U;
}
unsigned int ON_SubDMeshFragment::ColorCount() const
{
return (ColorsExistForExperts() && nullptr != m_C && m_C_stride >= 1) ? VertexCount() : 0U;
}
unsigned int ON_SubDMeshFragment::PointCapacity() const
{
return (nullptr != m_P && m_P_stride >= 3) ? VertexCapacity() : 0U;
}
unsigned int ON_SubDMeshFragment::NormalCapacity() const
{
return (nullptr != m_N && m_N_stride >= 3) ? VertexCapacity() : 0U;
}
unsigned int ON_SubDMeshFragment::CurvatureCapacity() const
{
return (nullptr != m_K && m_K_stride >= 1) ? VertexCapacity() : 0U;
}
unsigned int ON_SubDMeshFragment::ColorCapacity() const
{
return (nullptr != m_C && m_C_stride >= 1) ? VertexCapacity() : 0U;
}
const double* ON_SubDMeshFragment::PointArray(ON_SubDComponentLocation subd_appearance)const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? &m_ctrlnetP[0][0] : m_P;
}
size_t ON_SubDMeshFragment::PointArrayStride(ON_SubDComponentLocation subd_appearance)const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 3 : m_P_stride;
}
unsigned ON_SubDMeshFragment::PointArrayCount(ON_SubDComponentLocation subd_appearance) const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 4U : PointCount();
}
const double* ON_SubDMeshFragment::NormalArray(ON_SubDComponentLocation subd_appearance)const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? m_ctrlnetN : m_N;
}
size_t ON_SubDMeshFragment::NormalArrayStride(ON_SubDComponentLocation subd_appearance)const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 0 : m_N_stride;
}
unsigned ON_SubDMeshFragment::NormalArrayCount(ON_SubDComponentLocation subd_appearance) const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 4U : NormalCount();
}
const ON_SurfaceCurvature* ON_SubDMeshFragment::CurvatureArray(ON_SubDComponentLocation subd_appearance)const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? &m_ctrlnetK[0] : m_K;
}
unsigned ON_SubDMeshFragment::CurvatureArrayCount(ON_SubDComponentLocation subd_appearance) const
{
if (false == CurvaturesExistForExperts())
return 0U;
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 4U : CurvatureCount();
}
size_t ON_SubDMeshFragment::CurvatureArrayStride(ON_SubDComponentLocation subd_appearance) const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 1 : m_K_stride;
}
const ON_Color* ON_SubDMeshFragment::ColorArray(ON_SubDComponentLocation subd_appearance) const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? &m_ctrlnetC[0] : m_C;
}
size_t ON_SubDMeshFragment::ColorArrayStride(ON_SubDComponentLocation subd_appearance) const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 1 : m_C_stride;
}
unsigned ON_SubDMeshFragment::ColorArrayCount(ON_SubDComponentLocation subd_appearance) const
{
if (false == ColorsExistForExperts())
return 0U;
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? 4U : ColorCount();
}
bool ON_SubDMeshFragment::ColorsExistForExperts() const
{
return (0 != (m_vertex_capacity_etc & ON_SubDMeshFragment::EtcColorsExistBit));
}
const ON_Color ON_SubDMeshFragment::VertexColor(
unsigned grid2dex_i,
unsigned grid2dex_j
) const
{
return VertexColor(m_grid.PointIndexFromGrid2dex(grid2dex_i, grid2dex_j));
}
const ON_Color ON_SubDMeshFragment::VertexColor(
ON_2udex grid2dex
) const
{
return VertexColor(m_grid.PointIndexFromGrid2dex(grid2dex.i, grid2dex.j));
}
const ON_Color ON_SubDMeshFragment::VertexColor(
unsigned grid_point_index
) const
{
return
(grid_point_index < this->ColorCount())
? m_C[grid_point_index * m_C_stride]
: ON_Color::UnsetColor;
}
void ON_SubDMeshFragment::ClearColors() const
{
m_vertex_capacity_etc &= ~ON_SubDMeshFragment::EtcColorsExistBit;
this->m_ctrlnetC[0] = ON_Color::UnsetColor;
this->m_ctrlnetC[1] = ON_Color::UnsetColor;
this->m_ctrlnetC[2] = ON_Color::UnsetColor;
this->m_ctrlnetC[3] = ON_Color::UnsetColor;
}
void ON_SubDMeshFragment::SetColorsExistForExperts(bool bColorsExist) const
{
if (bColorsExist)
m_vertex_capacity_etc |= ON_SubDMeshFragment::EtcColorsExistBit;
else
this->ClearColors();
}
bool ON_SubDMeshFragment::CurvaturesExistForExperts() const
{
return (0 != (m_vertex_capacity_etc & ON_SubDMeshFragment::EtcCurvaturesExistBit));
}
const ON_SurfaceCurvature ON_SubDMeshFragment::VertexCurvature(
unsigned grid2dex_i,
unsigned grid2dex_j
) const
{
return VertexCurvature(m_grid.PointIndexFromGrid2dex(grid2dex_i, grid2dex_j));
}
const ON_SurfaceCurvature ON_SubDMeshFragment::VertexCurvature(
ON_2udex grid2dex
) const
{
return VertexCurvature(m_grid.PointIndexFromGrid2dex(grid2dex.i, grid2dex.j));
}
const ON_SurfaceCurvature ON_SubDMeshFragment::VertexCurvature(
unsigned grid_point_index
) const
{
return
(grid_point_index < this->CurvatureCount())
? m_K[grid_point_index * m_K_stride]
: ON_SurfaceCurvature::Nan;
}
void ON_SubDMeshFragment::ClearCurvatures() const
{
m_vertex_capacity_etc &= ~ON_SubDMeshFragment::EtcCurvaturesExistBit;
this->m_ctrlnetK[0] = ON_SurfaceCurvature::Nan;
this->m_ctrlnetK[1] = ON_SurfaceCurvature::Nan;
this->m_ctrlnetK[2] = ON_SurfaceCurvature::Nan;
this->m_ctrlnetK[3] = ON_SurfaceCurvature::Nan;
}
void ON_SubDMeshFragment::SetCurvaturesExistForExperts(bool bSetCurvaturesExist) const
{
if (bSetCurvaturesExist)
m_vertex_capacity_etc |= ON_SubDMeshFragment::EtcCurvaturesExistBit;
else
ClearCurvatures();
}
bool ON_SubDMeshFragment::SetColors(ON_Color color) const
{
if (ON_Color::UnsetColor == color)
{
ClearColors();
}
else
{
m_ctrlnetC[0] = color;
m_ctrlnetC[1] = color;
m_ctrlnetC[2] = color;
m_ctrlnetC[3] = color;
const size_t capacity = ColorCapacity();
if (capacity > 0)
{
const size_t stride = m_C_stride;
ON_Color* c = m_C;
ON_Color* c1 = c + capacity * stride;
while (c < c1)
{
*c = color;
c += stride;
}
}
this->SetColorsExistForExperts(true);
}
return ColorsExistForExperts();
}
bool ON_SubDMeshFragment::SetColorsFromCallback(
const ON_MappingTag& fragment_colors_mapping_tag,
const ON_SubD& subd,
ON__UINT_PTR callback_context,
const ON_Color(*color_callback)(
ON__UINT_PTR callback_context,
const ON_MappingTag& mapping_tag,
const ON_SubD& subd,
ON_SubDComponentPtr cptr,
const ON_3dPoint& P,
const ON_3dVector& N,
const ON_3dPoint& T,
const ON_SurfaceCurvature& K
)
) const
{
ClearColors();
for (;;)
{
if (nullptr == color_callback)
{
// removing vertex colors.
break;
}
const ON_SubDComponentLocation subd_appearance = ON_SubDComponentLocation::Surface;
const unsigned count = PointArrayCount(subd_appearance);
if (count <= 0)
break;
if (count != ColorCapacity())
break;
const double* P = PointArray(subd_appearance);
const size_t Pstride = PointArrayStride(subd_appearance);
if (nullptr == P || Pstride < 3)
break;
// Note that ColorsExist() is currently false. We are setting colors now.
ON_Color* C = m_C;
if (nullptr == C)
break;
const size_t Cstride = this->m_C_stride;
if (Cstride <= 0)
break;
const double nan3[3] = { ON_DBL_QNAN, ON_DBL_QNAN, ON_DBL_QNAN };
const double* N = NormalArray(subd_appearance);
const size_t Nstride = (nullptr != N) ? NormalArrayStride(subd_appearance) : 0;
if (nullptr == N)
N = nan3;
const double* T = TextureCoordinateArray(subd_appearance);
const size_t Tstride = (nullptr != T) ? TextureCoordinateArrayStride(subd_appearance) : 0;
if (nullptr == T)
T = nan3;
const ON_SurfaceCurvature* K = CurvatureArray(subd_appearance);
const size_t Kstride = (nullptr != K) ? CurvatureArrayStride(subd_appearance) : 0;
if (nullptr == K)
K = &ON_SurfaceCurvature::Nan;
bool bColorsExist = false;
const ON_SubDComponentPtr cptr = ON_SubDComponentPtr::Create(m_face);
for (const double* P1 = P + Pstride * count; P < P1; P += Pstride, C += Cstride)
{
// Never give the callback access to fragment array pointers.
const ON_3dPoint callbackP(P);
const ON_3dVector callbackN(N);
const ON_3dPoint callbackT(T);
const ON_SurfaceCurvature callbackK(*K);
const ON_Color callbackC = color_callback(callback_context, fragment_colors_mapping_tag, subd, cptr, callbackP, callbackN, callbackT, callbackK);
if (callbackC != ON_Color::UnsetColor)
bColorsExist = true;
*C = callbackC;
N += Nstride;
T += Tstride;
K += Kstride;
}
if (bColorsExist)
{
m_ctrlnetC[0] = CornerColor(0);
m_ctrlnetC[1] = CornerColor(1);
m_ctrlnetC[2] = CornerColor(2);
m_ctrlnetC[3] = CornerColor(3);
}
SetColorsExistForExperts(bColorsExist);
break;
}
return ColorsExistForExperts();
}
bool ON_SubD::SetFragmentColorsFromCallback(
bool bLazySet,
ON_SHA1_Hash fragment_colors_settings_hash,
ON_MappingTag fragment_colors_mapping_tag,
ON__UINT_PTR callback_context,
const ON_Color(*color_callback)(
ON__UINT_PTR callback_context,
const ON_MappingTag& mapping_tag,
const ON_SubD& subd,
ON_SubDComponentPtr cptr,
const ON_3dPoint& P,
const ON_3dVector& N,
const ON_3dPoint& T,
const ON_SurfaceCurvature& K)
) const
{
if (bLazySet
&& fragment_colors_settings_hash == FragmentColorsSettingsHash()
&& fragment_colors_mapping_tag == ColorsMappingTag()
&& this->HasFragmentColors()
)
{
// This subd has fragments with per vertex colors.
// The settings used to create those colors exactly
// match the settings that color_callback() will use
// to assign colors. The caller said to be lazy, so
// assume the existing colors are correct and return.
return true;
}
bool bFragmentVetexColorsSet = false;
const ON_SubDimple* subdimple = this->SubDimple();
if (nullptr != subdimple)
{
ON_SubDMeshFragmentIterator fragit(*this);
for (const ON_SubDMeshFragment* frag = fragit.FirstFragment(); nullptr != frag; frag = fragit.NextFragment())
{
const bool b = frag->SetColorsFromCallback(
fragment_colors_mapping_tag,
*this,
callback_context,
color_callback
);
if (b)
{
bFragmentVetexColorsSet = true;
frag->SetColorsExistForExperts(true);
}
else
frag->SetColorsExistForExperts(false);
}
if (bFragmentVetexColorsSet)
{
subdimple->Internal_SetFragmentColorsSettingsHash(fragment_colors_settings_hash);
SetColorsMappingTag(fragment_colors_mapping_tag);
ChangeRenderContentSerialNumber();
}
else
{
subdimple->Internal_SetFragmentColorsSettingsHash(ON_SHA1_Hash::EmptyContentHash);
this->SetColorsMappingTag(ON_MappingTag::Unset);
}
}
return bFragmentVetexColorsSet;
}
bool ON_SubD::HasFragmentColors() const
{
bool bHasColors = false;
const ON_SubDimple* subdimple = this->SubDimple();
if (nullptr != subdimple)
{
ON_SubDMeshFragmentIterator fragit(*this);
for (const ON_SubDMeshFragment* frag = fragit.FirstFragment(); nullptr != frag; frag = fragit.NextFragment())
{
if (0 == frag->ColorCount())
return false;
// NOTE WELL:
// All fragments need to be tested as lazy updates of the
// surface mesh cache result in the update fragments having
// no colors while the preexisting fragments have colors.
bHasColors = true; // fragments with colors exist
}
}
return bHasColors;
}
bool ON_SubD::HasFragmentColors(
ON_MappingTag color_mapping_tag
) const
{
return
this->ColorsMappingTag() == color_mapping_tag
&& this->HasFragmentColors();
}
bool ON_SubD::HasFragmentColors(
ON_SHA1_Hash color_settings_hash
) const
{
return
this->FragmentColorsSettingsHash() == color_settings_hash
&& this->HasFragmentColors();
}
bool ON_SubD::HasFragmentColors(
ON_SHA1_Hash color_settings_hash,
ON_MappingTag color_mapping_tag
) const
{
return
this->FragmentColorsSettingsHash() == color_settings_hash
&& this->ColorsMappingTag() == color_mapping_tag
&& this->HasFragmentColors();
}
bool ON_SubD::HasFragmentTextureCoordinates() const
{
const ON_SubDimple* subdimple = this->SubDimple();
if (nullptr != subdimple)
{
ON_SubDMeshFragmentIterator fragit(*this);
for (const ON_SubDMeshFragment* frag = fragit.FirstFragment(); nullptr != frag; frag = fragit.NextFragment())
{
if (frag->ColorCount() > 0)
return true;
}
}
return false;
}
bool ON_SubD::HasFragmentTextureCoordinates(
ON_MappingTag texture_mapping_tag
) const
{
return
this->TextureMappingTag(true) == texture_mapping_tag
&& this->HasFragmentTextureCoordinates();
}
bool ON_SubD::HasFragmentTextureCoordinates(
ON_SHA1_Hash texture_settings_hash
) const
{
return
this->TextureSettingsHash() == texture_settings_hash
&& this->HasFragmentTextureCoordinates();
}
bool ON_SubD::HasFragmentTextureCoordinates(
ON_SHA1_Hash texture_settings_hash,
ON_MappingTag texture_mapping_tag
) const
{
return
this->TextureSettingsHash() == texture_settings_hash
&& this->TextureMappingTag(true) == texture_mapping_tag
&& this->HasFragmentTextureCoordinates();
}
void ON_SubD::ClearFragmentColors(
bool bClearFragmentColorsMappingTag
)
{
const ON_SubDimple* subdimple = this->SubDimple();
if (nullptr != subdimple)
{
bool bFragmentsChanged = false;
ON_SubDMeshFragmentIterator fragit(*this);
for (const ON_SubDMeshFragment* frag = fragit.FirstFragment(); nullptr != frag; frag = fragit.NextFragment())
{
if (false == bFragmentsChanged && frag->ColorCount() > 0)
bFragmentsChanged = true;
frag->SetColorsExistForExperts(false);
}
if (bClearFragmentColorsMappingTag)
{
subdimple->Internal_SetFragmentColorsSettingsHash(ON_SHA1_Hash::EmptyContentHash);
this->SetColorsMappingTag(ON_MappingTag::Unset);
}
if (bFragmentsChanged)
this->ChangeRenderContentSerialNumber();
}
}
const ON_SHA1_Hash ON_SubD::FragmentColorsSettingsHash() const
{
const ON_SubDimple* subdimple = this->SubDimple();
return (nullptr != subdimple) ? subdimple->FragmentColorsSettingsHash() : ON_SHA1_Hash::EmptyContentHash;
}
void ON_SubD::SetFragmentColorsMappingTag(const ON_MappingTag& colors_mapping_tag) const
{
return SetColorsMappingTag(colors_mapping_tag);
}
void ON_SubD::SetColorsMappingTag(const ON_MappingTag& colors_mapping_tag) const
{
const ON_SubDimple* dimple = SubDimple();
if (nullptr != dimple)
dimple->SetColorsMappingTag(colors_mapping_tag);
}
const ON_MappingTag ON_SubD::FragmentColorsMappingTag() const
{
return ColorsMappingTag();
}
const ON_MappingTag ON_SubD::ColorsMappingTag() const
{
const ON_SubDimple* dimple = SubDimple();
return (nullptr != dimple) ? dimple->ColorsMappingTag() : ON_MappingTag::Unset;
}
const ON_3dPoint ON_SubDMeshFragment::ControlNetQuadPoint(
bool bGridOrder,
unsigned point_index
) const
{
if (point_index >= 4 || 0 == (ON_SubDMeshFragment::EtcControlNetQuadBit & m_vertex_count_etc))
return ON_3dPoint::NanPoint;
if (false == bGridOrder)
{
if (2 == point_index)
point_index = 3;
else if (3 == point_index)
point_index = 2;
}
return ON_3dPoint(m_ctrlnetP[point_index]);
}
const ON_SubDMeshFragment ON_SubDMeshFragment::ControlNetQuadFragmentForExperts() const
{
ON_SubDMeshFragment q(*this);
for (;;)
{
if ((m_vertex_count_etc & ON_SubDMeshFragment::ValueMask) < 4)
break;
if ( 0 == (m_vertex_count_etc & ON_SubDMeshFragment::EtcControlNetQuadBit) )
break;
q.m_vertex_count_etc = 4;
q.m_vertex_count_etc |= ON_SubDMeshFragment::EtcControlNetQuadBit;
q.m_vertex_count_etc |= (ON_SubDMeshFragment::EtcTextureCoordinatesExistBit & m_vertex_count_etc);
q.m_vertex_capacity_etc = 4;
q.m_vertex_capacity_etc |= (ON_SubDMeshFragment::EtcColorsExistBit & m_vertex_capacity_etc);
q.m_vertex_capacity_etc |= (ON_SubDMeshFragment::EtcCurvaturesExistBit & m_vertex_capacity_etc);
// 4 quad corner points in grid order with stride = 3
q.m_P = const_cast<double*>(&m_ctrlnetP[0][0]);
q.m_P_stride = 3;
// 4 identical normals with stride = 0
q.m_N = const_cast<double*>(&m_ctrlnetN[0]);
q.m_N_stride = 0;
// 4 quad corner texture coordinates in grid order with stride = 3
q.m_T = const_cast<double*>(&m_ctrlnetT[0][0]);
q.m_T_stride = 3;
q.m_C = const_cast<ON_Color*>(&m_ctrlnetC[0]);
q.m_C_stride = 1;
q.m_K = const_cast<ON_SurfaceCurvature*>(&m_ctrlnetK[0]);
q.m_K_stride = 1;
// The grid is a single quad
q.m_grid = ON_SubDMeshFragmentGrid::OneQuadGrid;
// both bounding boxes are equal.
q.m_surface_bbox = ControlNetQuadBoundingBox();
return q;
}
memset(&q, 0, sizeof(q));
return ON_SUBD_RETURN_ERROR(q);
}
bool ON_SubDMeshFragment::GetControlNetQuad(
bool bGridOrder,
ON_3dPoint quad_points[4],
ON_3dVector& quad_normal
) const
{
const bool rc = 0 != (ON_SubDMeshFragment::EtcControlNetQuadBit & m_vertex_count_etc) ? true : false;
if (nullptr != quad_points)
{
if (rc)
{
int i;
quad_points[0] = ON_3dPoint(m_ctrlnetP[0]);
quad_points[1] = ON_3dPoint(m_ctrlnetP[1]);
i = bGridOrder ? 2 : 3;
quad_points[i] = ON_3dPoint(m_ctrlnetP[2]);
i = bGridOrder ? 3 : 2;
quad_points[i] = ON_3dPoint(m_ctrlnetP[3]);
quad_normal = ON_3dVector(m_ctrlnetN);
}
else
{
for (int i = 0; i < 4; i++)
quad_points[i] = ON_3dPoint::NanPoint;
quad_normal = ON_3dVector::ZeroVector;
}
}
return rc;
}
const ON_SubDMeshFragmentGrid& ON_SubDMeshFragment::Grid(ON_SubDComponentLocation subd_appearance) const
{
return (ON_SubDComponentLocation::ControlNet == subd_appearance) ? ON_SubDMeshFragmentGrid::OneQuadGrid : m_grid;
}
void ON_SubDMeshFragment::SetControlNetQuad(bool bGridOrder, const ON_3dPoint quad_points[4], ON_3dVector quad_normal )
{
if (nullptr != quad_points && quad_points[0].IsValid() && quad_points[1].IsValid() && quad_points[2].IsValid() && quad_points[3].IsValid() && quad_normal.IsNotZero())
{
int i;
m_ctrlnetP[0][0] = quad_points[0].x;
m_ctrlnetP[0][1] = quad_points[0].y;
m_ctrlnetP[0][2] = quad_points[0].z;
m_ctrlnetP[1][0] = quad_points[1].x;
m_ctrlnetP[1][1] = quad_points[1].y;
m_ctrlnetP[1][2] = quad_points[1].z;
i = bGridOrder ? 2 : 3;
m_ctrlnetP[i][0] = quad_points[2].x;
m_ctrlnetP[i][1] = quad_points[2].y;
m_ctrlnetP[i][2] = quad_points[2].z;
i = bGridOrder ? 3 : 2;
m_ctrlnetP[i][0] = quad_points[3].x;
m_ctrlnetP[i][1] = quad_points[3].y;
m_ctrlnetP[i][2] = quad_points[3].z;
m_ctrlnetN[0] = quad_normal.x;
m_ctrlnetN[1] = quad_normal.y;
m_ctrlnetN[2] = quad_normal.z;
m_vertex_count_etc |= ON_SubDMeshFragment::EtcControlNetQuadBit;
}
else
{
ClearControlNetQuad();
}
}
void ON_SubDMeshFragment::UnsetControlNetQuad()
{
this->ClearControlNetQuad();
}
void ON_SubDMeshFragment::ClearControlNetQuad()
{
for (int i = 0; i < 4; ++i)
{
m_ctrlnetP[i][0] = m_ctrlnetP[i][1] = m_ctrlnetP[i][2] = ON_DBL_QNAN;
m_ctrlnetT[i][0] = m_ctrlnetT[i][1] = m_ctrlnetT[i][2] = ON_DBL_QNAN;
m_ctrlnetK[i] = ON_SurfaceCurvature::Nan;
m_ctrlnetC[i] = ON_UNSET_COLOR;
}
m_ctrlnetN[0] = m_ctrlnetN[1] = m_ctrlnetN[2] = ON_DBL_QNAN;
m_vertex_count_etc &= ~ON_SubDMeshFragment::EtcControlNetQuadBit;
}
const ON_BoundingBox ON_SubDMeshFragment::SurfaceBoundingBox() const
{
return m_surface_bbox;
}
void ON_SubDMeshFragment::ClearSurfaceBoundingBox()