forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_subd_frommesh.cpp
2199 lines (1961 loc) · 70.4 KB
/
opennurbs_subd_frommesh.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"
class ON_MeshNGonEdge
{
public:
// In ON_Mesh to ON_SubD, there is a ON_MeshNGonEdge for each mesh ngon edge.
// So, for an interior edge, there will be 2 ON_MeshNGonEdge values, one
// for each attached ngon.
// Sorting ON_MeshNGonEdge by m_u.mesh_edge_id will group the these
// edges together.
static const ON_MeshNGonEdge Unset;
static const ON_MeshNGonEdge Create(
unsigned candidate_subd_face_id,
unsigned mesh_Vi,
unsigned mesh_Vj,
const unsigned int* mesh_point_id
);
const ON_MeshNGonEdge Reversed() const
{
if (IsSet())
{
ON_MeshNGonEdge e = *this;
e.m_mesh_Vi = m_mesh_Vj;
e.m_mesh_Vj = m_mesh_Vi;
if (2 == m_u_status)
{
e.m_u.subd_edgeptr = m_u.subd_edgeptr.Reversed();
}
return e;
}
return ON_MeshNGonEdge::Unset;
}
static int CompareMeshEdgeTopologyId(const void* lhs, const void* rhs);
/*
Returns:
True if a and b reference the same topological edge and the normals
at both ends of the edge are different
*/
static bool TagEdgeAsCrease(
const ON_MeshNGonEdge& a,
const ON_MeshNGonEdge& b,
const unsigned int* mesh_point_id
);
const ON_2udex PointIds(
const unsigned int* mesh_point_id
) const
{
return (nullptr != mesh_point_id) ? ON_2udex(mesh_point_id[m_mesh_Vi], mesh_point_id[m_mesh_Vj]) : ON_2udex::Zero;
}
unsigned int m_sud_face_id; // likely ON_SubDFace.m_id value (unless invalid input causes some to be skipped)
unsigned int m_mesh_Vi; // ON_Mesh vertex index
unsigned int m_mesh_Vj; // ON_Mesh vertex index
private:
unsigned char m_u_status; // 0 = u is unset, 1 = m_u.mesh_edge_topology_id is set, 2 = m_u.subd_edgeptr is set
union
{
ON_2udex mesh_edge_topology_id; // uniquely identifies mesh edge topologically - independent of vertex order
ON_SubDEdgePtr subd_edgeptr;
} m_u;
public:
const ON_2udex EdgeTopologyId() const
{
return 1 == m_u_status ? m_u.mesh_edge_topology_id : ON_2udex::Zero;
}
const ON_SubDEdgePtr EdgePtr() const
{
return 2 == m_u_status ? m_u.subd_edgeptr : ON_SubDEdgePtr::Null;
}
bool SetEdgePtr(ON_SubDEdgePtr eptr)
{
if (1 == m_u_status)
{
m_u_status = eptr.IsNull() ? 0 : 2;
m_u.subd_edgeptr = eptr;
return true;
}
return ON_SUBD_RETURN_ERROR(false);
}
bool IsSet() const
{
return 0 != m_u_status;
}
};
static ON_MeshNGonEdge Internal_CreateUnsetMeshNGonEdge()
{
ON_MeshNGonEdge unset;
memset(&unset, 0, sizeof(unset));
return unset;
}
const ON_MeshNGonEdge ON_MeshNGonEdge::Unset = Internal_CreateUnsetMeshNGonEdge();
const ON_MeshNGonEdge ON_MeshNGonEdge::Create(
unsigned candidate_sud_face_id,
unsigned mesh_Vi,
unsigned mesh_Vj,
const unsigned int* mesh_point_id
)
{
for(;;)
{
if (candidate_sud_face_id <= 0)
break;
if (mesh_Vi == mesh_Vj)
break;
ON_MeshNGonEdge e = ON_MeshNGonEdge::Unset;
const unsigned a = mesh_point_id[mesh_Vi];
const unsigned b = mesh_point_id[mesh_Vj];
if (a < b)
{
e.m_u.mesh_edge_topology_id.i = a;
e.m_u.mesh_edge_topology_id.j = b;
e.m_u_status = 1;
}
else if (a > b)
{
e.m_u.mesh_edge_topology_id.i = b;
e.m_u.mesh_edge_topology_id.j = a;
e.m_u_status = 1;
}
else
break;
e.m_mesh_Vi = mesh_Vi;
e.m_mesh_Vj = mesh_Vj;
e.m_sud_face_id = candidate_sud_face_id;
return e;
}
return ON_SUBD_RETURN_ERROR(ON_MeshNGonEdge::Unset);
}
int ON_MeshNGonEdge::CompareMeshEdgeTopologyId(const void* lhs, const void* rhs)
{
// compare location ids
const ON_2udex lhs_edge_topology_id = ((const ON_MeshNGonEdge*)lhs)->EdgeTopologyId();
const ON_2udex rhs_edge_topology_id = ((const ON_MeshNGonEdge*)rhs)->EdgeTopologyId();
if (lhs_edge_topology_id.i < rhs_edge_topology_id.i)
return -1;
if (lhs_edge_topology_id.i > rhs_edge_topology_id.i)
return 1;
if (lhs_edge_topology_id.j < rhs_edge_topology_id.j)
return -1;
if (lhs_edge_topology_id.j > rhs_edge_topology_id.j)
return 1;
return 0;
}
bool ON_MeshNGonEdge::TagEdgeAsCrease(
const ON_MeshNGonEdge& a,
const ON_MeshNGonEdge& b,
const unsigned int* mesh_point_id
)
{
if (1 != a.m_u_status || 1 != b.m_u_status
|| a.m_u.mesh_edge_topology_id.i != b.m_u.mesh_edge_topology_id.i
|| a.m_u.mesh_edge_topology_id.j != b.m_u.mesh_edge_topology_id.j
)
{
// a and b should currently have equal topology ids
return ON_SUBD_RETURN_ERROR(false);
}
const bool bFlipA = (a.m_u.mesh_edge_topology_id.i != mesh_point_id[a.m_mesh_Vi]) ? true : false;
const bool bFlipB = (b.m_u.mesh_edge_topology_id.i != mesh_point_id[b.m_mesh_Vi]) ? true : false;
if (bFlipA == bFlipB)
{
// a and b have the same direction
if (a.m_mesh_Vi != b.m_mesh_Vi && a.m_mesh_Vj != b.m_mesh_Vj)
return true; // ON_Mesh vertex indices are different at both ends
}
else
{
// a and b have opposite directions
if (a.m_mesh_Vi != b.m_mesh_Vj && a.m_mesh_Vj != b.m_mesh_Vi)
return true; // ON_Mesh vertex indices are different at both ends
}
return false;
}
static bool Internal_CandidateTagIsBetterCreaseEnd(
ON_SubDVertexTag current_tag,
const ON_SubDVertex* candidate
)
{
if (nullptr == candidate)
return false;
switch(current_tag)
{
case ON_SubDVertexTag::Unset:
if (ON_SubDVertexTag::Unset != candidate->m_vertex_tag )
return true;
break;
case ON_SubDVertexTag::Smooth:
if (candidate->IsDartOrCreaseOrCorner())
return true;
break;
case ON_SubDVertexTag::Dart:
if (candidate->IsCreaseOrCorner())
return true;
break;
case ON_SubDVertexTag::Crease:
if (candidate->IsCorner())
return true;
break;
case ON_SubDVertexTag::Corner:
break;
default:
break;
}
return false;
}
static bool Internal_CreateFromMesh_ValidateNonmanifoldVertexSector(
const ON_SubDVertex* v,
const ON_SubDEdge* e,
ON_SubDSectorIterator& sit
)
{
// e is non manifold edge
// v = nonmanifold corner vertex on e
// sit is a sector of v with e as a starting boundary
if (nullptr == v || v != sit.CenterVertex() || e != sit.CurrentEdge(0))
return false;
// k is used to protect against infinite looping if the topology
// around v is invalid.
const ON_SubDEdge* other_crease = nullptr;
const ON_SubDEdge* best_candidate_edge = nullptr;
const ON_SubDVertex* best_canditate_v1 = nullptr;
const ON_3dVector dir = -e->ControlNetDirectionFrom(v);
double best_dot = ON_DBL_QNAN;
for (unsigned short k = 0; k <= v->m_face_count; ++k)
{
const ON_SubDEdge* e1 = sit.CurrentEdge(1);
if (e1->IsCrease())
{
other_crease = e1;
break;
}
const ON_SubDVertex* v1 = e1->OtherEndVertex(v);
if (nullptr == v1)
{
ON_SUBD_ERROR("invalid SubD topology.");
return false; // invalid topology
}
const double d = dir * e1->ControlNetDirectionFrom(v);
if (
nullptr == best_candidate_edge
|| Internal_CandidateTagIsBetterCreaseEnd(best_canditate_v1->m_vertex_tag,v1)
|| (nullptr != best_candidate_edge && best_canditate_v1->m_vertex_tag == v1->m_vertex_tag && d > best_dot)
)
{
best_candidate_edge = e1;
best_dot = d;
best_canditate_v1 = v1;
}
if (nullptr == sit.NextFace(ON_SubDSectorIterator::StopAt::AnyCrease))
break;
}
if (nullptr == other_crease)
{
ON_SUBD_ERROR("bug in nonmanifold mesh to SubD code.");
return false;
}
if (other_crease != e)
return true; // this sector is a valid corner vertex sector.
if (nullptr == best_candidate_edge)
{
ON_SUBD_ERROR("bug in nonmanifold mesh to SubD code.");
return false;
}
// make best_candidate_edge a crease so corner sector is valid
const_cast<ON_SubDEdge*>(best_candidate_edge)->m_edge_tag = ON_SubDEdgeTag::Crease;
const ON_SubDVertexEdgeProperties best_ep = best_canditate_v1->EdgeProperties();
ON_SubDVertexTag vtag;
if ( 1 == best_ep.m_crease_edge_count && 2 == best_ep.m_min_edge_face_count && 2 == best_ep.m_max_edge_face_count)
vtag = ON_SubDVertexTag::Dart;
else if ( 2 == best_ep.m_crease_edge_count && best_ep.m_max_edge_face_count <= 2 )
vtag = ON_SubDVertexTag::Crease;
else
vtag = ON_SubDVertexTag::Corner;
if (false == Internal_CandidateTagIsBetterCreaseEnd(vtag, best_canditate_v1))
const_cast<ON_SubDVertex*>(best_canditate_v1)->m_vertex_tag = vtag;
return true;
}
static void Internal_CreateFromMesh_ValidateNonmanifoldVertex(
const ON_SubDVertex* v
)
{
if (
nullptr == v
|| ON_SubDVertexTag::Corner != v->m_vertex_tag
)
return;
for (unsigned short vei = 0; vei < v->m_edge_count; ++vei)
{
const ON_SubDEdge* e = v->Edge(vei);
if (
nullptr == e
|| ON_SubDEdgeTag::Crease != e->m_edge_tag
|| e->m_face_count <= 2
)
continue;
// e is non manifold - verify every attached face has a valid corner sector
for (unsigned short efi = 0; efi < e->m_face_count; ++efi)
{
const ON_SubDFace* f = e->Face(efi);
if (nullptr == f)
continue;
ON_SubDSectorIterator sit;
sit.Initialize(f, 0, v);
if (e != sit.CurrentEdge(0))
{
sit.Initialize(f, 1, v);
if (e != sit.CurrentEdge(0))
{
ON_SUBD_ERROR("bug in nonmanifold mesh to SubD code.");
continue;
}
}
Internal_CreateFromMesh_ValidateNonmanifoldVertexSector(v,e,sit);
// convert best_candidate to a crease to make this a valid corner sector;
}
}
return;
}
class ON_NgonBoundaryChecker
{
public:
/*
Parameters:
ngon - [in]
ngon to test
mesh [in]
mesh that ngon is a part of
bMustBeOriented - [in]
If true, the faces in the ngon must be compatibly oriented
*/
bool IsSimpleNgon(
const class ON_MeshNgon* ngon,
const class ON_Mesh* mesh,
bool bMustBeOriented
);
enum : unsigned int
{
HashTableSize = 256
};
private:
void Internal_Reset();
class ON_NgonBoundaryComponent* Internal_AddVertex(unsigned int vertex_index);
class ON_NgonBoundaryComponent* Internal_AddEdge(unsigned int vertex_index0, unsigned int vertex_index1, bool bMustBeOriented);
static unsigned int Internal_VertexHashIndex(unsigned int vertex_index);
static unsigned int Internal_EdgeHashIndex(unsigned int vertex_index0, unsigned int vertex_index1);
void Internal_InitialzeFixedSizePool();
void Internal_ReturnIsNotSimple();
// m_fsp manages the memory used for boundary components.
ON_FixedSizePool m_fsp;
class ON_NgonBoundaryComponent* m_hash_table[ON_NgonBoundaryChecker::HashTableSize] = {};
unsigned m_vertex_count = 0;
unsigned m_edge_count = 0;
bool m_bIsSimple = false;
bool m_bIsNotSimple = false;
};
ON_SubD* ON_SubD::CreateFromMesh(
const class ON_Mesh* level_zero_mesh,
const class ON_SubDFromMeshParameters* from_mesh_options,
ON_SubD* subd
)
{
ON_Mesh* local_copy = nullptr;
if (nullptr != level_zero_mesh)
{
// remove ngons with holes and other damaged ngons so the underlying faces get used.
ON_NgonBoundaryChecker bc;
const bool bMustBeOrientedNgon = false;
const unsigned ngon_count = level_zero_mesh->NgonUnsignedCount();
ON_SimpleArray<unsigned> ngons_with_holes(ngon_count);
for (unsigned ni = 0; ni < ngon_count; ++ni)
{
const class ON_MeshNgon* ngon = level_zero_mesh->Ngon(ni);
if ( nullptr == ngon)
continue;
if (ngon->m_Vcount < 3 || ngon->m_Fcount <= 1)
continue;
if ( false == bc.IsSimpleNgon(ngon, level_zero_mesh,bMustBeOrientedNgon) )
ngons_with_holes.Append(ni);
}
for (;;)
{
if (0 == ngons_with_holes.UnsignedCount())
break;
local_copy = new ON_Mesh(*level_zero_mesh);
if (nullptr == local_copy)
break;
if (ngon_count != local_copy->NgonUnsignedCount())
break;
const unsigned removed_count = local_copy->RemoveNgons(ngons_with_holes.UnsignedCount(), ngons_with_holes.Array());
if (removed_count > 0)
level_zero_mesh = local_copy;
break;
}
}
ON_SubD* subd_from_mesh = Internal_CreateFromMeshWithValidNgons(level_zero_mesh, from_mesh_options, subd);
if (nullptr != local_copy)
delete local_copy;
return subd_from_mesh;
}
static double Internal_FaceCornerAngleRadians(const ON_SubDVertex* v, const ON_SubDFace* f)
{
for (;;)
{
if (nullptr == f)
break;
if (false == f->IsConvex())
break;
// We could remove the f->IsConvex() test, but it might make make a bad situation worse.
// As of May 2020, nobody has complained about this approach.
// the code below assumes the face is convex
const unsigned fvi = f->VertexIndex(v);
const ON_SubDComponentPtrPair pair = f->VertexEdgePair(fvi);
if (false == pair.BothAreNotNull())
break;
const double a = ON_SubDSectorType::CornerSectorAngleRadiansFromEdges(pair.First().EdgePtr().Reversed(), pair.Second().EdgePtr());
if (false == (a > 0.0 && a < ON_PI))
break;
return a;
}
return ON_DBL_QNAN;
}
ON_SubD* ON_SubD::Internal_CreateFromMeshWithValidNgons(
const class ON_Mesh* mesh,
const class ON_SubDFromMeshParameters* from_mesh_options,
ON_SubD* subd
)
{
if (nullptr != subd)
{
ON_SubDimple* subdimple = subd->SubDimple(false);
if (nullptr != subdimple)
subdimple->Clear();
}
if (nullptr == mesh)
return nullptr;
ON_Workspace ws;
if (nullptr == from_mesh_options)
from_mesh_options = &ON_SubDFromMeshParameters::Smooth;
ON_3dPointListRef mesh_points(mesh);
const unsigned mesh_point_count = mesh_points.PointCount();
if (mesh_point_count < 3)
return nullptr;
const ON_MeshFaceList mesh_face_list(mesh);
const unsigned int mesh_face_count = mesh_face_list.FaceCount();
if ( mesh_face_count < 1 )
return nullptr;
const_cast<ON_Mesh*>(mesh)->NgonMap(true);
ON_MeshNgonIterator ngonit(mesh);
if (nullptr == ngonit.FirstNgon())
return nullptr;
////////////////////////////////////////////////////////////////////////////////////////
//
// Conceptually sort the vertices of mesh into groups that are at the same 3d location.
// For each group of mesh vertices at a particular spot, there will typically be
// one SubD vertex. However, mesh vertices that are not referenced by mesh faces
// are ignored so it is possible that some mesh vertices will not have a corresponding
// SubD vertex.
//
// mesh_point_count = mesh->MeshVertexCount();
// mesh_point_id[]
// mesh_point_id[] has mesh_point_count values.
// mesh_point_id[i] = mesh_point_id[j] if and only if mesh->m_V[i] and mesh->m_V[j] are coincident.
// Values in mesh_point_id[] run from 0 to mesh_point_id_count-1.
// There are mesh_point_id_count unique locations.
// mesh_point_map[] is a permutation of (0, ..., mesh_point_count-1).
// mesh_point_map[] sorts mesh->m_V[] into groups of vertices with the same mesh_id / location.
// 0 == mesh_point_id[mesh_point_map[0]] <= ... <= mesh_point_id[mesh_point_map[mesh_point_count-1]] = mesh_point_id_count-1.
// mesh_vertex_status[] mesh_point_id_count char values.
// mesh_vertex_status[mesh_vi] = 1 if that mesh vertex id/location has a corresponding SubD vertex. 0 if the mesh vertex will be ignored.
unsigned int* buffer1 = (unsigned int*)ws.GetIntMemory(mesh_point_count);
const unsigned int* mesh_point_id = mesh->GetVertexLocationIds(0, (unsigned int*)ws.GetIntMemory(mesh_point_count), buffer1);
if (nullptr == mesh_point_id)
return nullptr;
const unsigned int* mesh_point_map = buffer1;
// mesh_point_id_count = number of unique vertex locations in mesh->m_V[] array.
const unsigned mesh_point_id_count = mesh_point_id[mesh_point_map[mesh_point_count - 1]] + 1;
unsigned char* mesh_vertex_status = (unsigned char*)ws.GetMemory(mesh_point_id_count*sizeof(mesh_vertex_status[0]));
memset(mesh_vertex_status, 0, mesh_point_id_count*sizeof(mesh_vertex_status[0]));
ON_SubDFromMeshParameters::InteriorCreaseOption crease_test
= (nullptr != from_mesh_options)
? from_mesh_options->GetInteriorCreaseOption()
: ON_SubDFromMeshParameters::InteriorCreaseOption::None;
if (ON_SubDFromMeshParameters::InteriorCreaseOption::AtMeshDoubleEdge != crease_test)
{
crease_test = ON_SubDFromMeshParameters::InteriorCreaseOption::None;
}
////////////////////////////////////////////////////////////////////////////////////////
//
// Get mesh edge list
//
unsigned int subd_vertex_count = 0;
unsigned int mesh_edge_count = 0;
unsigned int max_subd_face_edge_count = 0;
ON_SimpleArray<ON_MeshNGonEdge> mesh_edges(4 * mesh->m_F.UnsignedCount());
unsigned int quad_vi[4];
ON_MeshNGonEdge quad_edges[4] = {};
bool bMergeColinearEdges = false;
unsigned int subd_face_count = 0;
unsigned int mesh_Vi;
unsigned int mesh_Vj;
for (const ON_MeshNgon* ngon = ngonit.FirstNgon(); nullptr != ngon; ngon = ngonit.NextNgon())
{
if (ngon->m_Vcount < 3 || ngon->m_Fcount < 1)
continue;
const int ngon_orientation = ngon->Orientation(mesh_face_list, false);
if (0 != ngon_orientation)
{
unsigned int ngon_edge_count = 0;
mesh_Vj = ngon->m_vi[0];
for (unsigned int nvi = 1; nvi <= ngon->m_Vcount; nvi++)
{
mesh_Vi = mesh_Vj;
mesh_Vj = ngon->m_vi[nvi % ngon->m_Vcount];
if (mesh_point_id[mesh_Vi] == mesh_point_id[mesh_Vj])
continue; // coincident vertex locations
ON_MeshNGonEdge& mesh_edge = mesh_edges.AppendNew();
mesh_edge = ON_MeshNGonEdge::Create(subd_face_count+1, mesh_Vi, mesh_Vj, mesh_point_id);
if (mesh_edge.IsSet())
{
ngon_edge_count++;
continue;
}
ngon_edge_count = 0;
break;
}
if (ngon_edge_count < 3)
{
mesh_edges.SetCount(mesh_edge_count);
continue;
}
if (ngon_orientation < 0)
{
// ngon and mesh have opposite orientations - mesh orientation wins
// reverese edges
unsigned int i0 = mesh_edge_count;
unsigned int i1 = mesh_edge_count + ngon_edge_count - 1;
while (i0 < i1)
{
const ON_MeshNGonEdge mesh_edge = mesh_edges[i0];
mesh_edges[i0] = mesh_edges[i1].Reversed();
mesh_edges[i1] = mesh_edge.Reversed();
i0++;
i1--;
}
// Flip middle edge if odd number of edges
if (i0 == i1)
mesh_edges[i0] = mesh_edges[i0].Reversed();
}
// the ngon created a single subd face
++subd_face_count;
if (ngon_edge_count >= 4)
bMergeColinearEdges = true;
if (mesh_edges.UnsignedCount() - mesh_edge_count > max_subd_face_edge_count)
max_subd_face_edge_count = mesh_edges.UnsignedCount() - mesh_edge_count;
}
else if ( ngon->m_Fcount >= 1 )
{
// This generally happens when the "ngon" has holes and it cannot be used as a subd control net polygon.
//
// Each tri or quad in the ngon will get added as a subd face.
for (unsigned int nfi = 0; nfi < ngon->m_Fcount; nfi++)
{
if ( nullptr == mesh_face_list.QuadFvi(ngon->m_fi[nfi],quad_vi))
continue;
unsigned int quad_edge_count = 0;
mesh_Vj = quad_vi[0];
for (unsigned int fvi = 1; fvi <= 4; fvi++)
{
mesh_Vi = mesh_Vj;
mesh_Vj = quad_vi[fvi % 4];
if (mesh_point_id[mesh_Vi] == mesh_point_id[mesh_Vj])
continue; // coincident vertex locations (always happens on a tri face and can happen on invalid faces)
quad_edges[quad_edge_count] = ON_MeshNGonEdge::Create(subd_face_count+1, mesh_Vi, mesh_Vj, mesh_point_id);
if (quad_edges[quad_edge_count].IsSet())
{
++quad_edge_count;
continue;
}
quad_edge_count = 0;
break;
}
if (quad_edge_count >= 3)
{
// each quad/triangle in the ON_Mesh ngon created a subd face
mesh_edges.Append(quad_edge_count,quad_edges);
++subd_face_count;
if( quad_edge_count > max_subd_face_edge_count)
max_subd_face_edge_count = quad_edge_count;
}
}
if ( mesh_edge_count == mesh_edges.UnsignedCount() )
continue;
}
for (/*empty init*/; mesh_edge_count < mesh_edges.UnsignedCount(); mesh_edge_count++)
{
const ON_MeshNGonEdge& mesh_edge = mesh_edges[mesh_edge_count];
if (0 == mesh_vertex_status[mesh_point_id[mesh_edge.m_mesh_Vi]])
{
mesh_vertex_status[mesh_point_id[mesh_edge.m_mesh_Vi]] = 1;
subd_vertex_count++;
}
if (0 == mesh_vertex_status[mesh_point_id[mesh_edge.m_mesh_Vj]])
{
mesh_vertex_status[mesh_point_id[mesh_edge.m_mesh_Vj]] = 1;
subd_vertex_count++;
}
}
}
if (subd_vertex_count < 3 || mesh_edge_count < 3 || subd_face_count < 1)
return nullptr;
std::unique_ptr<ON_SubD> uptr;
ON_SubD* new_subd = nullptr;
if (subd)
{
new_subd = subd;
}
else
{
uptr = std::make_unique<ON_SubD>();
new_subd = uptr.get();
}
// Make sure the subdimple is created before adding components.
if (nullptr == new_subd->SubDimple(true))
return nullptr;
bool bHasTaggedVertices = false;
bool bHasNonmanifoldCornerVertices = false;
//////////////////////////////////////////////////////////////////////
//
// create subd vertices
//
// subd_V[mesh_vi] = subd vertex that corresponds to mesh->m_V[].
// Note that when mesh-m_V[i] and mesh->m_V[j] are the same point,
// then subd_V[i] = subd_V[j].
// This is common and it happens when mesh_point_id_count < mesh_point_count
// (some distinct mesh points in the mesh->m_V[] array have the same location).
ON_SubDVertex** subd_V = (ON_SubDVertex**)ws.GetMemory(mesh_point_count * sizeof(subd_V[0]));
memset(subd_V, 0, mesh_point_count * sizeof(subd_V[0]));
for (unsigned int i = 0; i < mesh_point_count;/*empty iterator*/)
{
const unsigned int vid0 = mesh_point_id[mesh_point_map[i]];
if (0 == mesh_vertex_status[vid0])
{
// no edges reference this vertex.
++i;
continue;
}
unsigned int j;
for (j = i + 1; j < mesh_point_count; j++)
{
if (vid0 != mesh_point_id[mesh_point_map[j]])
break;
}
const ON_3dPoint P = mesh_points[mesh_point_map[i]];
ON_SubDVertex* subd_vertex = new_subd->AddVertex(ON_SubDVertexTag::Smooth, &P.x);
while (i < j)
subd_V[mesh_point_map[i++]] = subd_vertex;
}
////////////////////////////////////////////////////////////////////////
//
// If we are adding interior crease, set the mesh_edge_ref.m_mesh_Ni / m_mesh_Nj values used to detect creases.
//
if ( ON_SubDFromMeshParameters::InteriorCreaseOption::AtMeshDoubleEdge != crease_test )
{
crease_test = ON_SubDFromMeshParameters::InteriorCreaseOption::None;
}
//////////////////////////////////////////////////////////////////////
//
// Create SubD edges.
//
// An subd interior edge will have 2 consecutive element in mesh_edges[].
// A subd non-manifold edge with k faces with have k consecutive element in mesh_edges[].
// A subd boundary edge will have 1 element in mesh_edges[].
// A subd wire edge will have 1 element in mesh_edges[].
//
// mesh_edge_map[] is used to sort the sort mesh_edges[] into groups that correspond to the same SubD edge.
// The order of mesh_edges[] cannot be changed because the current order is needed to efficiently create the SubD faces.
unsigned int* mesh_edge_map = (unsigned int*)ws.GetMemory(mesh_edges.UnsignedCount() * sizeof(mesh_edge_map[0]));
ON_Sort(
ON::sort_algorithm::quick_sort,
mesh_edge_map, mesh_edges.Array(),
mesh_edges.UnsignedCount(),
sizeof(ON_MeshNGonEdge),
ON_MeshNGonEdge::CompareMeshEdgeTopologyId
);
for (unsigned int i = 0; i < mesh_edges.UnsignedCount(); /*empty iterator*/)
{
const ON_MeshNGonEdge& mesh_edge0 = mesh_edges[mesh_edge_map[i]];
const ON_2udex topology_id0 = mesh_edge0.EdgeTopologyId();
// get the group of mesh_edges[] that will use the same SubD edge.
// and determine if that edge should be tagged as a crease.
ON_SubDEdgeTag edge_tag = ON_SubDEdgeTag::Smooth;
unsigned j = i + 1;
for (/*empty init*/; j < mesh_edges.UnsignedCount(); ++j)
{
ON_MeshNGonEdge& mesh_edge1 = mesh_edges[mesh_edge_map[j]];
const ON_2udex topology_id1 = mesh_edge1.EdgeTopologyId();
if (topology_id0.i != topology_id1.i || topology_id0.j != topology_id1.j)
break;
if (ON_SubDFromMeshParameters::InteriorCreaseOption::None != crease_test)
{
if (ON_MeshNGonEdge::TagEdgeAsCrease(mesh_edge0, mesh_edge1, mesh_point_id))
edge_tag = ON_SubDEdgeTag::Crease;
}
}
if ( j-i != 2 )
edge_tag = ON_SubDEdgeTag::Crease; // wire, boundary, or non-manifold edge
// create the SubD edge.
ON_SubDVertex* v0[2] = { subd_V[mesh_edge0.m_mesh_Vi], subd_V[mesh_edge0.m_mesh_Vj] };
ON_SubDEdge* e
= (nullptr != v0[0] && nullptr != v0[1] && v0[0]->m_id != v0[1]->m_id)
? new_subd->AddEdgeWithSectorCoefficients(edge_tag, v0[0], ON_SubDSectorType::IgnoredSectorCoefficient, v0[1], ON_SubDSectorType::IgnoredSectorCoefficient)
: nullptr;
// Change the mesh_edges[].m_u from the topology id to an ON_SubDEdgePtr.
// We need the ON_SubDEdgePtr below to create SubD faces.
ON_SubDEdgePtr eptr = ON_SubDEdgePtr::Null;
for (/*empty init*/; i < j; ++i)
{
ON_MeshNGonEdge& mesh_edge = mesh_edges[mesh_edge_map[i]];
if (nullptr != e)
{
const ON_SubDVertex* v[2] = { subd_V[mesh_edge.m_mesh_Vi], subd_V[mesh_edge.m_mesh_Vj] };
if (v0[0] == v[0] && v0[1] == v[1])
eptr = ON_SubDEdgePtr::Create(e, 0);
else if (v0[0] == v[1] && v0[1] == v[0])
eptr = ON_SubDEdgePtr::Create(e, 1);
else
{
ON_SUBD_ERROR("There is a bug in the code above. This should not happen.");
eptr = ON_SubDEdgePtr::Null;
}
}
else
{
eptr = ON_SubDEdgePtr::Null;
}
mesh_edge.SetEdgePtr(eptr);
}
}
//////////////////////////////////////////////////////////////////////
//
// Create the SubD faces.
//
// mesh_edges[] is ordered in groups that form the boundary of each SubD face.
// All the edges that form a boundary of a face have the same m_sud_face_id value.
// If the input mesh is completely valid, this will also be ON_SubDFace m_id value.
// If the input mesh is damaged, some boundaries will not generate a corresponding SubD face.
//
const ON_SubDFromMeshParameters::TextureCoordinatesOption texture_coordinates_option
= nullptr != from_mesh_options
? from_mesh_options->GetTextureCoordinatesOption()
: ON_SubDFromMeshParameters::TextureCoordinatesOption::None;
const bool bAutomaticTextureCoordinates = ON_SubDFromMeshParameters::TextureCoordinatesOption::Automatic == texture_coordinates_option;
// At most one of bCopyMeshMappingTag, bCopyMeshTextureCoordinates, or bPackedTextureCoordinates is true.
const bool bCopyMeshMappingTag
= (bAutomaticTextureCoordinates || ON_SubDFromMeshParameters::TextureCoordinatesOption::CopyMapping == texture_coordinates_option)
&& mesh->m_Ttag.IsSet()
;
const bool bCopyMeshTextureCoordinates
= false == bCopyMeshMappingTag
&& (bAutomaticTextureCoordinates || ON_SubDFromMeshParameters::TextureCoordinatesOption::CopyCoordinates == texture_coordinates_option)
&& mesh->HasTextureCoordinates()
;
const bool bPackedTextureCoordinates
= false == bCopyMeshMappingTag
&& false == bCopyMeshTextureCoordinates
&& (bAutomaticTextureCoordinates || ON_SubDFromMeshParameters::TextureCoordinatesOption::Packed == texture_coordinates_option)
&& mesh->HasTextureCoordinates()
;
ON_SimpleArray< ON_SubDEdgePtr > EP(max_subd_face_edge_count);
ON_SimpleArray< ON_3dPoint > face_texture_points(bCopyMeshTextureCoordinates ? max_subd_face_edge_count : 0);
for (unsigned i = 0; i < mesh_edges.UnsignedCount(); /*empty iterator*/)
{
EP.SetCount(0);
face_texture_points.SetCount(0);
const unsigned candidate_sud_face_id = mesh_edges[i].m_sud_face_id;
unsigned j = i;
for (/*empty init*/; j < mesh_edges.UnsignedCount(); ++j)
{
const ON_MeshNGonEdge& mesh_edge = mesh_edges[j];
if (candidate_sud_face_id != mesh_edge.m_sud_face_id)
break;
const ON_SubDEdgePtr eptr = mesh_edge.EdgePtr();
if (eptr.IsNotNull())
{
EP.Append(eptr);
const ON_SubDVertex* debug_eptr_v[2] = { eptr.RelativeVertex(0), eptr.RelativeVertex(1) };
const ON_SubDVertex* debug_mesh_v[2] = { subd_V[mesh_edge.m_mesh_Vi], subd_V[mesh_edge.m_mesh_Vj] };
const bool bOK = debug_eptr_v[0] == debug_mesh_v[0] && debug_eptr_v[1] == debug_mesh_v[1];
if (false == bOK) ON_SUBD_ERROR("XXX");
if (bCopyMeshTextureCoordinates)
face_texture_points.Append(ON_3dPoint(mesh->m_T[mesh_edge.m_mesh_Vi]));
}
}
const unsigned edge_count = EP.UnsignedCount();
ON_SubDFace* f
= (edge_count >= 3 && j - i == edge_count)
? new_subd->AddFace(EP.Array(), EP.UnsignedCount())
: nullptr;
if (nullptr != f)
{
if (bCopyMeshTextureCoordinates)
new_subd->AddFaceTexturePoints(f, face_texture_points.Array(), face_texture_points.UnsignedCount() );
}
const unsigned actual_subd_face_id = (nullptr != f) ? f->m_id : 0;
for ( /*empty init*/; i < j; ++i)
mesh_edges[i].m_sud_face_id = actual_subd_face_id;
}
// Apply "ON_SubDEdgeTag::Crease" tag to boundary and non-manifold edges and their vertices.
unsigned int interior_crease_count = 0;
for (const ON_SubDEdge* edge = new_subd->FirstEdge(); nullptr != edge; edge = edge->m_next_edge)
{
// Note: edges are created before faces and we set the edge tag when the edges are created
// assuming that face creation will go as expected. If the mesh is damaged, the face may not
// be created. So, we need to check both edge->m_face_count and edge->m_edge_tag.
if (2 == edge->m_face_count && ON_SubDEdgeTag::Smooth == edge->m_edge_tag)
continue;
const_cast<ON_SubDEdge*>(edge)->m_edge_tag = ON_SubDEdgeTag::Crease;
bHasTaggedVertices = true;
const ON_SubDVertexTag vtag
= (1 == edge->m_face_count || 2 == edge->m_face_count)
? ON_SubDVertexTag::Crease
: ON_SubDVertexTag::Corner; // wire edge or non-manifold edge
// Depending on the number of creased edges, a vertex on an interior crease here that
// is tagged as ON_SubDVertexTag::Crease here may get changed
// to ON_SubDVertexTag::Dart or ON_SubDVertexTag::Corner below.
for (unsigned int j = 0; j < 2; j++)
{
const ON_SubDVertex* vertex = edge->m_vertex[j];
if (ON_SubDVertexTag::Smooth == vertex->m_vertex_tag)
{
const_cast<ON_SubDVertex*>(vertex)->m_vertex_tag = vtag;
if (ON_SubDVertexTag::Corner == vtag && edge->m_face_count > 2)
bHasNonmanifoldCornerVertices = true;
}
}
if ( 2 == edge->m_face_count )
interior_crease_count++;
}
if (bHasNonmanifoldCornerVertices)
{
// may need to crease more edges to get valid corners RH-49843
for (const ON_SubDVertex* v = new_subd->FirstVertex(); nullptr != v; v = v->m_next_vertex)
Internal_CreateFromMesh_ValidateNonmanifoldVertex(v);
}
if (interior_crease_count > 0)
{
// Any interior vertex that has exactly one creased edge must be tagged as a dart.
// Any interior vertex that has more than two creased edges must be tagged as a corner.
unsigned int k = 0; // k = number of interior creases we've tested
for (const ON_SubDEdge* edge = new_subd->FirstEdge(); nullptr != edge && k < interior_crease_count; edge = edge->m_next_edge)
{
if (2 != edge->m_face_count || ON_SubDEdgeTag::Crease != edge->m_edge_tag)
continue;
k++; // processing another interior crease.
if ( ON_SubDVertexTag::Crease != edge->m_vertex[0]->m_vertex_tag
&& ON_SubDVertexTag::Crease != edge->m_vertex[1]->m_vertex_tag)
continue;
for (unsigned int j = 0; j < 2; j++)
{
const ON_SubDVertex* vertex = edge->m_vertex[j];
if (ON_SubDVertexTag::Crease != vertex->m_vertex_tag)
continue; // this vertex has already been processed.
const ON_SubDVertexEdgeProperties ep = vertex->EdgeProperties();
if ( 0 == ep.m_null_edge_count && 0 == ep.m_unset_edge_count )