forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_subd_archive.cpp
2400 lines (2053 loc) · 64.8 KB
/
opennurbs_subd_archive.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"
static bool Internal_WriteDouble3(
const double x[3],
ON_BinaryArchive& archive
)
{
for (;;)
{
if (!archive.WriteDouble(3, x))
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_ReadDouble3(
ON_BinaryArchive& archive,
double x[3]
)
{
for (;;)
{
if (!archive.ReadDouble(3, x))
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
enum : unsigned char
{
ON_SubDComponentArchiveAnonymousChunkMark = 254U,
ON_SubDComponentArchiveAdditionEndMark = 255U
};
static bool Internal_ReadComponentAdditionSize(ON_BinaryArchive& archive, unsigned char valid_sz, unsigned char* sz)
{
if (archive.Archive3dmVersion() < 70)
{
return ON_SUBD_RETURN_ERROR(false);
}
if (0 == valid_sz)
return ON_SUBD_RETURN_ERROR(false);
if (false == archive.ReadChar(sz))
return ON_SUBD_RETURN_ERROR(false);
if ( 0 != *sz && valid_sz != *sz && ON_SubDComponentArchiveAdditionEndMark != *sz )
return ON_SUBD_RETURN_ERROR(false);
return true;
}
static bool Internal_WriteComponentAdditionSize(bool bHaveAddition, ON_BinaryArchive& archive, unsigned char sz)
{
if (archive.Archive3dmVersion() < 70)
{
return ON_SUBD_RETURN_ERROR(false);
}
if (0 == sz)
return ON_SUBD_RETURN_ERROR(false);
if (false == bHaveAddition)
sz = 0;
if (false == archive.WriteChar(sz))
return ON_SUBD_RETURN_ERROR(false);
return true;
}
static bool Internal_FinishReadingComponentAdditions(ON_BinaryArchive& archive)
{
if (archive.Archive3dmVersion() < 70)
{
return ON_SUBD_RETURN_ERROR(false);
}
unsigned char sz = 1;
if ( false == archive.ReadChar(&sz))
return ON_SUBD_RETURN_ERROR(false);
for (;;)
{
if (ON_SubDComponentArchiveAdditionEndMark == sz)
return true;
if (ON_SubDComponentArchiveAnonymousChunkMark == sz)
{
// skip an addition a future version added as an anonymous chunk
int v = 0;
if (false == archive.BeginRead3dmAnonymousChunk(&v))
break;
if (false == archive.EndRead3dmChunk())
break;
}
else if ( sz > 0 )
{
// skip an addition a future version added as a fixed number of bytes
// use archive.ReadByte(sz,buffer) instead of archive.SeekForward(sz) so CRC is properly calculated.
char buffer[256];
if (false == archive.ReadByte(sz,buffer))
break;
}
sz = 0;
if (false == archive.ReadChar(&sz))
break;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_FinishWritingComponentAdditions(ON_BinaryArchive& archive)
{
if (archive.Archive3dmVersion() < 70)
return ON_SUBD_RETURN_ERROR(false);
const unsigned char sz = ON_SubDComponentArchiveAdditionEndMark;
return archive.WriteChar(sz);
}
static bool Internal_WriteArchiveIdAndFlags(
unsigned int archive_id,
ON__UINT_PTR ptr_flags,
ON_BinaryArchive& archive
)
{
if (!archive.WriteInt(archive_id))
return ON_SUBD_RETURN_ERROR(false);
unsigned char flags = (unsigned char)ON_SUBD_COMPONENT_FLAGS(ptr_flags);
if (!archive.WriteChar(flags))
return ON_SUBD_RETURN_ERROR(false);
return true;
}
static bool Internal_ReadArchiveIdAndFlagsIntoComponentPtr(
ON_BinaryArchive& archive,
ON__UINT_PTR& element_ptr
)
{
element_ptr = 0;
unsigned int archive_id = 0;
if (!archive.ReadInt(&archive_id))
return ON_SUBD_RETURN_ERROR(false);
unsigned char flags = 0;
if (!archive.ReadChar(&flags))
return ON_SUBD_RETURN_ERROR(false);
element_ptr = archive_id;
element_ptr *= (ON_SUBD_COMPONENT_FLAGS_MASK + 1);
element_ptr += (flags & ON_SUBD_COMPONENT_FLAGS_MASK);
return true;
}
static bool Internal_WritesSymmetrySetNext(
const ON_SubDComponentBase& c,
ON_BinaryArchive& archive
)
{
const ON_SubDComponentPtr symmetry_set_next = ON_SubDArchiveIdMap::SymmetrySetNextForExperts(c);
const ON_SubDComponentBase* next_c = symmetry_set_next.ComponentBase();
const unsigned archive_id = (nullptr != next_c) ? next_c->ArchiveId() : 0;
return Internal_WriteArchiveIdAndFlags(archive_id, symmetry_set_next.m_ptr, archive);
}
static bool Internal_ReadSymmetrySetNext(
ON_BinaryArchive& archive,
const ON_SubDComponentBase& c
)
{
return Internal_ReadArchiveIdAndFlagsIntoComponentPtr(archive, ON_SubDArchiveIdMap::SymmetrySetNextForExperts(c).m_ptr);
}
static bool WriteBase(
const ON_SubDComponentBase* base,
ON_BinaryArchive& archive
)
{
for (;;)
{
unsigned int archive_id = base->ArchiveId();
unsigned int id = base->m_id;
unsigned short level = (unsigned short)base->SubdivisionLevel();
if (!archive.WriteInt(archive_id))
break;
if (!archive.WriteInt(id))
break;
if (!archive.WriteShort(level))
break;
if (archive.Archive3dmVersion() < 70)
{
// version 6 3dm files
double P[3];
const bool bHaveP = base->GetSavedSubdivisionPoint(P);
unsigned char cP = bHaveP ? 4U : 0U;
if (!archive.WriteChar(cP))
break;
if (0 != cP)
{
if (!Internal_WriteDouble3(P, archive))
break;
}
unsigned char deprecated_and_never_used_zero = 0U;
if (!archive.WriteChar(deprecated_and_never_used_zero))
break;
return true;
}
// version 7 3dm files and later
// never used displacement
if ( false == Internal_WriteComponentAdditionSize(false,archive,24) )
break;
// 4 byte group id addition
const bool bWriteGroupId = base->m_group_id > 0;
if (false == Internal_WriteComponentAdditionSize(bWriteGroupId, archive, 4))
break;
if (bWriteGroupId)
{
if (!archive.WriteInt(base->m_group_id))
break;
}
// 5 byte symmetry set next addition Dec 2020 Rhino 7.2 and later
// 5 bytes = unsigned archive id + char flags
const bool bWriteSymmetrySetNext = base->InSymmetrySet();
if (false == Internal_WriteComponentAdditionSize(bWriteSymmetrySetNext, archive, 5))
break;
if (bWriteSymmetrySetNext)
{
if (!Internal_WritesSymmetrySetNext(*base,archive))
break;
}
return Internal_FinishWritingComponentAdditions(archive);
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool ReadBase(
ON_BinaryArchive& archive,
ON_SubDComponentBase& base
)
{
unsigned int archive_id = 0;
unsigned int id = 0;
unsigned short level = 0;
for (;;)
{
if (!archive.ReadInt(&archive_id))
break;
if (!archive.ReadInt(&id))
break;
if (!archive.ReadShort(&level))
break;
base.m_id = id;
base.SetArchiveId(archive_id);
base.SetSubdivisionLevel(level);
if (archive.Archive3dmVersion() < 70)
{
unsigned char cP = 0U;
unsigned char deprecated_and_never_used_char = 0U;
double P[3];
if (!archive.ReadChar(&cP))
break;
if (0 != cP)
{
if (!Internal_ReadDouble3(archive, P))
break;
}
if (!archive.ReadChar(&deprecated_and_never_used_char))
break;
if (0 != deprecated_and_never_used_char)
{
double deprecated_and_never_used_V[3];
if (!Internal_ReadDouble3(archive, deprecated_and_never_used_V))
break;
}
if (4 == cP)
base.SetSavedSubdivisionPoint(P);
return true;
}
// read additions
unsigned char sz;
// 24 byte displacement addition
// This addition was a deprecated prototype that was never used in commercial Rhino.
sz = 0;
if (false == Internal_ReadComponentAdditionSize(archive, 24, &sz))
break;
if (ON_SubDComponentArchiveAdditionEndMark == sz)
return true; // end of additions
if (0 != sz)
{
double deprecated_and_never_used_V[3] = {};
if (!archive.ReadDouble(3, deprecated_and_never_used_V))
break;
}
// 4 byte group id addition
sz = 0;
if (false == Internal_ReadComponentAdditionSize(archive, 4, &sz))
break;
if (ON_SubDComponentArchiveAdditionEndMark == sz)
return true; // end of additions
if (0 != sz)
{
if (!archive.ReadInt(&base.m_group_id))
break;
}
// 5 bytes symmetry set next addition Dec 2020 Rhino 7.2 and later
// 5 bytes = unsigned archive id + char flags
sz = 0;
if (false == Internal_ReadComponentAdditionSize(archive, 5, &sz))
break;
if (ON_SubDComponentArchiveAdditionEndMark == sz)
return true; // end of additions
if (0 != sz)
{
if (!Internal_ReadSymmetrySetNext(archive,base))
break;
}
return Internal_FinishReadingComponentAdditions(archive);
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_WriteSavedLimitPointList(
unsigned int vertex_face_count,
bool bHaveLimitPoint,
const ON_SubDSectorSurfacePoint& limit_point,
ON_BinaryArchive& archive
)
{
unsigned int limit_point_count = 0;
const ON_SubDSectorSurfacePoint* p;
if (bHaveLimitPoint)
{
for (p = &limit_point; nullptr != p && limit_point_count <= vertex_face_count; p = p->m_next_sector_limit_point)
{
if (!ON_IsValid(p->m_limitP[0]))
break;
if (limit_point_count > 0 && nullptr == p->m_sector_face)
break;
limit_point_count++;
}
if (limit_point_count > vertex_face_count || nullptr != p)
limit_point_count = 0;
if (limit_point_count > vertex_face_count)
limit_point_count = 0;
}
if (0 == limit_point_count)
bHaveLimitPoint = false;
for (;;)
{
unsigned char c = bHaveLimitPoint ? 4 : 0;
if (!archive.WriteChar(c))
break;
if (0 == c)
return true;
if (!archive.WriteInt(limit_point_count))
break;
p = &limit_point;
for (unsigned int i = 0; i < limit_point_count; i++, p = p->m_next_sector_limit_point )
{
if (!Internal_WriteDouble3(limit_point.m_limitP, archive))
break;
if (!Internal_WriteDouble3(limit_point.m_limitT1, archive))
break;
if (!Internal_WriteDouble3(limit_point.m_limitT2, archive))
break;
if (!Internal_WriteDouble3(limit_point.m_limitN, archive))
break;
if (!Internal_WriteArchiveIdAndFlags(limit_point.m_sector_face ? limit_point.m_sector_face->ArchiveId() : 0, 0, archive))
break;
}
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_ReadSavedLimitPointList(
ON_BinaryArchive& archive,
unsigned int vertex_face_count,
ON_SimpleArray< ON_SubDSectorSurfacePoint > limit_points
)
{
limit_points.SetCount(0);
for (;;)
{
unsigned char c = 0;
if (!archive.ReadChar(&c))
break;
if ( 0 == c)
return true;
unsigned int limit_point_count = 0;
if (!archive.ReadInt(&limit_point_count))
break;
if ( 0 == limit_point_count )
break;
if (limit_point_count > vertex_face_count)
break;
limit_points.Reserve(limit_point_count);
for ( unsigned int i = 0; i < limit_point_count; i++ )
{
ON_SubDSectorSurfacePoint limit_point = ON_SubDSectorSurfacePoint::Unset;
if (!Internal_ReadDouble3(archive,limit_point.m_limitP))
break;
if (!Internal_ReadDouble3(archive,limit_point.m_limitT1))
break;
if (!Internal_ReadDouble3(archive,limit_point.m_limitT2))
break;
if (!Internal_ReadDouble3(archive,limit_point.m_limitN))
break;
ON_SubDFacePtr fptr = ON_SubDFacePtr::Null;
if (!Internal_ReadArchiveIdAndFlagsIntoComponentPtr(archive,fptr.m_ptr))
break;
limit_points.Append(limit_point);
}
if (limit_point_count != limit_points.UnsignedCount() )
break;
if (4 != c)
limit_points.SetCount(0);
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_WriteVertexList(
unsigned short vertex_count,
const ON_SubDVertex*const* vertex,
ON_BinaryArchive& archive
)
{
for (;;)
{
ON_SubDArchiveIdMap::ValidateArrayCounts(vertex_count,vertex_count,vertex,0,nullptr);
if (!archive.WriteShort(vertex_count))
break;
if ( 0 == vertex_count )
return true;
const ON__UINT_PTR ptr_flags = 0; // for future use
unsigned short i = 0;
for (i = 0; i < vertex_count; i++)
{
const ON_SubDVertex* v = vertex[i];
if (!Internal_WriteArchiveIdAndFlags((nullptr != v) ? v->ArchiveId() : 0, ptr_flags, archive))
break;
}
if ( i < vertex_count )
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_ReadVertexList(
ON_BinaryArchive& archive,
unsigned short& vertex_count,
unsigned short vertex_capacity,
ON_SubDVertex* vertex[]
)
{
for (;;)
{
unsigned short archive_vertex_count = 0;
if (!archive.ReadShort(&archive_vertex_count))
break;
if (archive_vertex_count != vertex_count)
{
ON_ERROR("Archive vertex count != expected vertex count.");
if ( archive_vertex_count < vertex_count)
vertex_count = archive_vertex_count;
}
ON_SubDArchiveIdMap::ValidateArrayCounts(vertex_count,vertex_capacity,vertex,0,nullptr);
unsigned short i = 0;
for (i = 0; i < vertex_count; i++)
{
ON__UINT_PTR vptr = 0;
if (!Internal_ReadArchiveIdAndFlagsIntoComponentPtr(archive,vptr))
break;
vertex[i] = (ON_SubDVertex*)vptr;
}
if ( i < vertex_count )
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_WriteEdgePtrList(
unsigned short edge_count,
unsigned short edgeN_capacity,
const ON_SubDEdgePtr* edgeN,
unsigned short edgeX_capacity,
const ON_SubDEdgePtr* edgeX,
ON_BinaryArchive& archive
)
{
for (;;)
{
ON_SubDArchiveIdMap::ValidateArrayCounts(edge_count,edgeN_capacity,edgeN,edgeX_capacity,edgeX);
if (!archive.WriteShort(edge_count))
break;
if ( 0 == edge_count )
return true;
const ON_SubDEdgePtr* eptr = edgeN;
unsigned short i = 0;
for (i = 0; i < edge_count; i++, eptr++)
{
if ( i == edgeN_capacity)
eptr = edgeX;
const ON_SubDEdge* edge = ON_SUBD_EDGE_POINTER(eptr->m_ptr);
if (!Internal_WriteArchiveIdAndFlags((nullptr != edge) ? edge->ArchiveId() : 0,eptr->m_ptr,archive))
break;
}
if ( i < edge_count )
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_ReadEdgePtrList(
ON_BinaryArchive& archive,
unsigned short& edge_count,
unsigned short edgeN_capacity,
ON_SubDEdgePtr* edgeN,
unsigned short edgeX_capacity,
ON_SubDEdgePtr* edgeX
)
{
for (;;)
{
unsigned short archive_edge_count = 0;
if (!archive.ReadShort(&archive_edge_count))
break;
if (archive_edge_count != edge_count)
{
ON_ERROR("Archive edge count != expected edge count.");
if ( archive_edge_count < edge_count)
edge_count = archive_edge_count;
}
ON_SubDArchiveIdMap::ValidateArrayCounts(edge_count,edgeN_capacity,edgeN,edgeX_capacity,edgeX);
ON_SubDEdgePtr* eptr = edgeN;
unsigned short i = 0;
for (i = 0; i < edge_count; i++, eptr++)
{
if ( i == edgeN_capacity)
eptr = edgeX;
if (!Internal_ReadArchiveIdAndFlagsIntoComponentPtr(archive,eptr->m_ptr))
break;
}
if ( i < edge_count )
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_WriteFacePtrList(
unsigned short face_count,
size_t faceN_capacity,
const ON_SubDFacePtr* faceN,
unsigned short faceX_capacity,
const ON_SubDFacePtr* faceX,
ON_BinaryArchive& archive
)
{
for (;;)
{
ON_SubDArchiveIdMap::ValidateArrayCounts(face_count,faceN_capacity,faceN,faceX_capacity,faceX);
if (!archive.WriteShort(face_count))
break;
if ( 0 == face_count )
return true;
const ON_SubDFacePtr* fptr = faceN;
unsigned short i = 0;
for (i = 0; i < face_count; i++, fptr++)
{
if ( i == faceN_capacity)
fptr = faceX;
const ON_SubDFace* face = ON_SUBD_FACE_POINTER(fptr->m_ptr);
if (!Internal_WriteArchiveIdAndFlags((nullptr != face) ? face->ArchiveId() : 0,fptr->m_ptr,archive))
break;
}
if ( i < face_count )
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
static bool Internal_ReadFacePtrList(
ON_BinaryArchive& archive,
unsigned short& face_count,
unsigned short faceN_capacity,
ON_SubDFacePtr* faceN,
unsigned short faceX_capacity,
ON_SubDFacePtr* faceX
)
{
for (;;)
{
unsigned short archive_face_count = 0;
if (!archive.ReadShort(&archive_face_count))
break;
if (archive_face_count != face_count)
{
ON_ERROR("Archive face count != expected face count.");
if ( archive_face_count < face_count)
face_count = archive_face_count;
}
ON_SubDArchiveIdMap::ValidateArrayCounts(face_count,faceN_capacity,faceN,faceX_capacity,faceX);
ON_SubDFacePtr* fptr = faceN;
unsigned short i = 0;
for (i = 0; i < face_count; i++, fptr++)
{
if ( i == faceN_capacity)
fptr = faceX;
if (!Internal_ReadArchiveIdAndFlagsIntoComponentPtr(archive,fptr->m_ptr))
break;
}
if ( i < face_count )
break;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
ON_SubDComponentPtr& ON_SubDArchiveIdMap::SymmetrySetNextForExperts(const ON_SubDComponentBase& c)
{
return const_cast<ON_SubDComponentPtr&>(c.m_symmetry_set_next);
}
bool ON_SubDVertex::Write(
ON_BinaryArchive& archive
) const
{
for (;;)
{
if (!WriteBase(this,archive))
break;
if (!archive.WriteChar((unsigned char)m_vertex_tag))
break;
if (!Internal_WriteDouble3(m_P,archive))
break;
if (!archive.WriteShort(m_edge_count))
break;
if (!archive.WriteShort(m_face_count))
break;
if (!Internal_WriteSavedLimitPointList(m_face_count, this->SurfacePointIsSet(), m_limit_point, archive))
break;
if (!Internal_WriteEdgePtrList(m_edge_count,m_edge_capacity,m_edges,0,nullptr, archive))
break;
if (!Internal_WriteFacePtrList(m_face_count,m_face_capacity,(const ON_SubDFacePtr*)m_faces,0,nullptr, archive))
break;
if (archive.Archive3dmVersion() < 70)
{
// mark end with a 0 byte
if (!archive.WriteChar((unsigned char)0U))
break;
return true;
}
return Internal_FinishWritingComponentAdditions(archive);
}
return ON_SUBD_RETURN_ERROR(false);
}
bool ON_SubDVertex::Read(
class ON_BinaryArchive& archive,
class ON_SubD& subd,
class ON_SubDVertex*& vertex
)
{
vertex = nullptr;
for (;;)
{
ON_SubDimple* subdimple = const_cast<ON_SubDimple*>(subd.SubDimple());
if ( nullptr == subdimple)
break;
ON_SubDComponentBase base = ON_SubDComponentBase::Unset;
unsigned char vertex_tag = 0;
//unsigned char vertex_edge_order = 0;
//unsigned char vertex_facet_type = 0;
double P[3];
unsigned short edge_count = 0;
unsigned short face_count = 0;
ON_SimpleArray<ON_SubDSectorSurfacePoint> limit_points;
if (!ReadBase(archive,base))
break;
if (!archive.ReadChar(&vertex_tag))
break;
if (!Internal_ReadDouble3(archive,P))
break;
if (!archive.ReadShort(&edge_count))
break;
if (!archive.ReadShort(&face_count))
break;
if (!Internal_ReadSavedLimitPointList(archive, face_count, limit_points))
break;
ON_SubDVertex* v = subdimple->AllocateVertex(
base.m_id, // serialization must preserve ON_SubDVertex.m_id
ON_SubD::VertexTagFromUnsigned(vertex_tag),
base.SubdivisionLevel(),
P,
edge_count,
face_count
);
if ( nullptr == v )
break;
v->ON_SubDComponentBase::operator=(base);
if (!Internal_ReadEdgePtrList(archive,edge_count,v->m_edge_capacity,v->m_edges,0,nullptr))
break;
v->m_edge_count = edge_count;
if (!Internal_ReadFacePtrList(archive,face_count,v->m_face_capacity,(ON_SubDFacePtr*)v->m_faces,0,nullptr))
break;
v->m_face_count = face_count;
for (unsigned int i = 0; i < limit_points.UnsignedCount(); i++)
{
ON_SubDSectorSurfacePoint limit_point = limit_points[i];
limit_point.m_next_sector_limit_point = (const ON_SubDSectorSurfacePoint*)1U; // skips checks
if (false == v->SetSavedSurfacePoint( true, limit_point))
{
v->ClearSavedSurfacePoints();
break;
}
}
vertex = v;
if (archive.Archive3dmVersion() < 70)
{
unsigned char sz = 1;
if (!archive.ReadChar(&sz) || 0 != sz)
break;
return true;
}
// read additions
return Internal_FinishReadingComponentAdditions(archive);
}
return ON_SUBD_RETURN_ERROR(false);
}
bool ON_SubDEdge::Write(
ON_BinaryArchive& archive
) const
{
for (;;)
{
if (!WriteBase(this,archive))
break;
if (!archive.WriteChar((unsigned char)m_edge_tag))
break;
if (!archive.WriteShort(m_face_count))
break;
if (!archive.WriteDouble(2,m_sector_coefficient))
break;
if (!archive.WriteDouble(this->m_sharpness[0]))
break;
if (!Internal_WriteVertexList(2, m_vertex, archive))
break;
if (!Internal_WriteFacePtrList(m_face_count,sizeof(m_face2)/sizeof(m_face2[0]),m_face2,m_facex_capacity,m_facex, archive))
break;
if (archive.Archive3dmVersion() < 70)
{
// mark end with a 0 byte
if (!archive.WriteChar((unsigned char)0U))
break;
return true;
}
if (archive.Archive3dmVersion() >= 80)
{
// 2nd sharpness added Jan 2023 to v8 files.
if (!archive.WriteChar((unsigned char)8U))
break;
if (!archive.WriteDouble(this->m_sharpness[1]))
break;
}
return Internal_FinishWritingComponentAdditions(archive);
}
return ON_SUBD_RETURN_ERROR(false);
}
bool ON_SubDEdge::Read(
class ON_BinaryArchive& archive,
class ON_SubD& subd,
class ON_SubDEdge*& edge
)
{
edge = nullptr;
for (;;)
{
ON_SubDimple* subdimple = const_cast<ON_SubDimple*>(subd.SubDimple());
if ( nullptr == subdimple)
break;
ON_SubDComponentBase base = ON_SubDComponentBase::Unset;
unsigned char edge_tag = 0;
unsigned short face_count = 0;
double sector_coefficient[2] = { 0 };
double sharpness0 = 0.0;
if (!ReadBase(archive,base))
break;
if (!archive.ReadChar(&edge_tag))
break;
if (!archive.ReadShort(&face_count))
break;
if (!archive.ReadDouble(2,sector_coefficient))
break;
if (!archive.ReadDouble(&sharpness0))
break;
ON_SubDVertex* v[2] = { 0 };
unsigned short vertex_count = 2;
if (!Internal_ReadVertexList(archive, vertex_count, 2, v))
break;
ON_SubDEdge* e = subdimple->AllocateEdge(
base.m_id, // serialization must preserve ON_SubDEdge.m_id
ON_SubD::EdgeTagFromUnsigned(edge_tag),
base.SubdivisionLevel(),
face_count
);
if ( nullptr == e )
break;
e->ON_SubDComponentBase::operator=(base);
for ( unsigned short evi = 0; evi < 2 && evi < vertex_count; evi++ )
e->m_vertex[evi] = v[evi];
e->m_sector_coefficient[0] = sector_coefficient[0];
e->m_sector_coefficient[1] = sector_coefficient[1];
if (!Internal_ReadFacePtrList(archive,face_count,sizeof(e->m_face2)/sizeof(e->m_face2[0]),e->m_face2,e->m_facex_capacity,e->m_facex))
break;
e->m_face_count = face_count;
edge = e;
if (archive.Archive3dmVersion() < 70)
{
unsigned char sz;
if (false == archive.ReadChar(&sz) || 0 != sz)
break;
return true;
}
if (archive.Archive3dmVersion() >= 80)
{
unsigned char sz;
if (false == archive.ReadChar(&sz))
break;
if (ON_SubDComponentArchiveAdditionEndMark == sz)
return true;
if (8 != sz)
break; // error
// 2nd sharpness added Jan 2023 to v8 files.
double sharpness1 = 0.0;
if (false == archive.ReadDouble(&sharpness1))
break;
if (e->IsSmooth())
e->SetSharpnessForExperts(ON_SubDEdgeSharpness::FromInterval(sharpness0,sharpness1));
}
return Internal_FinishReadingComponentAdditions(archive);
}
return ON_SUBD_RETURN_ERROR(false);
}
bool ON_SubDFace::Write(
ON_BinaryArchive& archive
) const
{
for (;;)
{
if (!WriteBase(this,archive))
break;
if (!archive.WriteInt(m_level_zero_face_id))
break;
// OBSOLETE parent face id
const int obsolete_parent_face_id = 0;
if (!archive.WriteInt(obsolete_parent_face_id))
break;
if (!archive.WriteShort(m_edge_count))
break;
if (!Internal_WriteEdgePtrList(m_edge_count,sizeof(m_edge4)/sizeof(m_edge4[0]),m_edge4,m_edgex_capacity,m_edgex, archive))
break;
if (archive.Archive3dmVersion() < 70)
{
unsigned char sz = 0;
if (!archive.WriteChar(sz))
break;
return true;
}