@@ -663,35 +663,36 @@ void Manifold::Impl::SetNormals(int normalIdx, float minSharpAngle) {
663
663
for (int tri = 0 ; tri < numTri; ++tri) {
664
664
for (const int i : {0 , 1 , 2 }) {
665
665
if (meshRelation_.triProperties [tri][i] >= 0 ) continue ;
666
- int startEdge = 3 * tri + i;
667
- const int vert = halfedge_[startEdge].startVert ;
666
+ int current = 3 * tri + i;
667
+ const int startEdge = current;
668
+ const int vert = halfedge_[current].startVert ;
668
669
669
670
if (vertNumSharp[vert] < 2 ) {
670
671
const glm::vec3 normal = vertFlatFace[vert] >= 0
671
672
? faceNormal_[vertFlatFace[vert]]
672
673
: vertNormal_[vert];
673
674
int lastProp = -1 ;
674
- ForVert (startEdge, [&](int current) {
675
+ do {
676
+ current = NextHalfedge (halfedge_[current].pairedHalfedge );
675
677
const int thisTri = current / 3 ;
676
678
const int j = current - 3 * thisTri;
677
679
const int prop = oldTriProp[thisTri][j];
678
680
meshRelation_.triProperties [thisTri][j] = prop;
679
- if (prop == lastProp) return ;
681
+ if (prop == lastProp) continue ;
680
682
lastProp = prop;
681
683
auto start = oldProperties.begin () + prop * oldNumProp;
682
684
std::copy (start, start + oldNumProp,
683
685
meshRelation_.properties .begin () + prop * numProp);
684
686
for (const int i : {0 , 1 , 2 })
685
687
meshRelation_.properties [prop * numProp + normalIdx + i] =
686
688
normal [i];
687
- });
689
+ } while (current != startEdge );
688
690
} else {
689
691
const glm::vec3 centerPos = vertPos_[vert];
690
692
// Length degree
691
693
std::vector<int > group;
692
694
// Length number of normals
693
695
std::vector<glm::vec3> normals;
694
- int current = startEdge;
695
696
int prevFace = halfedge_[current].face ;
696
697
697
698
do {
@@ -712,38 +713,34 @@ void Manifold::Impl::SetNormals(int normalIdx, float minSharpAngle) {
712
713
} while (current != startEdge);
713
714
714
715
const int endEdge = current;
716
+ glm::vec3 prevEdgeVec =
717
+ glm::normalize (vertPos_[halfedge_[current].endVert ] - centerPos);
715
718
716
- struct FaceEdge {
717
- int face;
718
- glm::vec3 edgeVec;
719
- };
720
-
721
- ForVert<FaceEdge>(
722
- endEdge,
723
- [this , centerPos](int current) {
724
- return FaceEdge (
725
- {halfedge_[current].face ,
726
- glm::normalize (vertPos_[halfedge_[current].endVert ] -
727
- centerPos)});
728
- },
729
- [this , &triIsFlatFace, &normals, &group, minSharpAngle](
730
- int current, const FaceEdge& here, const FaceEdge& next) {
731
- const float dihedral = glm::degrees (glm::acos (
732
- glm::dot (faceNormal_[here.face ], faceNormal_[next.face ])));
733
- if (dihedral > minSharpAngle ||
734
- triIsFlatFace[here.face ] != triIsFlatFace[next.face ] ||
735
- (triIsFlatFace[here.face ] && triIsFlatFace[next.face ] &&
736
- !meshRelation_.triRef [here.face ].SameFace (
737
- meshRelation_.triRef [next.face ]))) {
738
- normals.push_back (glm::vec3 (0 ));
739
- }
740
- group.push_back (normals.size () - 1 );
741
- float dot = glm::dot (here.edgeVec , next.edgeVec );
742
- const float phi =
743
- dot >= 1 ? 0
744
- : (dot <= -1 ? glm::pi <float >() : glm::acos (dot));
745
- normals.back () += faceNormal_[next.face ] * phi;
746
- });
719
+ do {
720
+ current = NextHalfedge (halfedge_[current].pairedHalfedge );
721
+ const int face = halfedge_[current].face ;
722
+
723
+ const float dihedral = glm::degrees (
724
+ glm::acos (glm::dot (faceNormal_[face], faceNormal_[prevFace])));
725
+ if (dihedral > minSharpAngle ||
726
+ triIsFlatFace[face] != triIsFlatFace[prevFace] ||
727
+ (triIsFlatFace[face] && triIsFlatFace[prevFace] &&
728
+ !meshRelation_.triRef [face].SameFace (
729
+ meshRelation_.triRef [prevFace]))) {
730
+ normals.push_back (glm::vec3 (0 ));
731
+ }
732
+ group.push_back (normals.size () - 1 );
733
+
734
+ const glm::vec3 edgeVec =
735
+ glm::normalize (vertPos_[halfedge_[current].endVert ] - centerPos);
736
+ float dot = glm::dot (prevEdgeVec, edgeVec);
737
+ const float phi =
738
+ dot >= 1 ? 0 : (dot <= -1 ? glm::pi <float >() : glm::acos (dot));
739
+ normals.back () += faceNormal_[face] * phi;
740
+
741
+ prevFace = face;
742
+ prevEdgeVec = edgeVec;
743
+ } while (current != endEdge);
747
744
748
745
for (auto & normal : normals) {
749
746
normal = glm::normalize (normal );
@@ -753,9 +750,10 @@ void Manifold::Impl::SetNormals(int normalIdx, float minSharpAngle) {
753
750
int lastProp = -1 ;
754
751
int newProp = -1 ;
755
752
int idx = 0 ;
756
- ForVert (endEdge, [&](int current1) {
757
- const int thisTri = current1 / 3 ;
758
- const int j = current1 - 3 * thisTri;
753
+ do {
754
+ current = NextHalfedge (halfedge_[current].pairedHalfedge );
755
+ const int thisTri = current / 3 ;
756
+ const int j = current - 3 * thisTri;
759
757
const int prop = oldTriProp[thisTri][j];
760
758
auto start = oldProperties.begin () + prop * oldNumProp;
761
759
@@ -782,7 +780,7 @@ void Manifold::Impl::SetNormals(int normalIdx, float minSharpAngle) {
782
780
783
781
meshRelation_.triProperties [thisTri][j] = newProp;
784
782
++idx;
785
- });
783
+ } while (current != endEdge );
786
784
}
787
785
}
788
786
}
@@ -857,9 +855,11 @@ void Manifold::Impl::CreateTangents(int normalIdx) {
857
855
}
858
856
});
859
857
} else { // Sharpen vertex uniformly
860
- ForVert (first, [this ](int current) {
858
+ int current = first;
859
+ do {
861
860
halfedgeTangent_[current] = glm::vec4 (0 );
862
- });
861
+ current = NextHalfedge (halfedge_[current].pairedHalfedge );
862
+ } while (current != first);
863
863
}
864
864
}
865
865
}
@@ -947,17 +947,18 @@ void Manifold::Impl::CreateTangents(std::vector<Smoothness> sharpenedEdges) {
947
947
tangent[second] = CircularTangent (
948
948
-newTangent, vertPos_[halfedge_[second].endVert ] - pos);
949
949
950
- float smoothness =
951
- (vert[0 ].second .smoothness + vert[1 ].first .smoothness ) / 2 ;
952
- ForVert (
953
- first, [&tangent, &smoothness, &vert, first, second](int current) {
954
- if (current == second) {
955
- smoothness =
956
- (vert[1 ].second .smoothness + vert[0 ].first .smoothness ) / 2 ;
957
- } else if (current != first) {
958
- tangent[current] = smoothness * tangent[current];
959
- }
960
- });
950
+ auto SmoothHalf = [&](int first, int last, float smoothness) {
951
+ int current = NextHalfedge (halfedge_[first].pairedHalfedge );
952
+ while (current != last) {
953
+ tangent[current] = smoothness * tangent[current];
954
+ current = NextHalfedge (halfedge_[current].pairedHalfedge );
955
+ }
956
+ };
957
+
958
+ SmoothHalf (first, second,
959
+ (vert[0 ].second .smoothness + vert[1 ].first .smoothness ) / 2 );
960
+ SmoothHalf (second, first,
961
+ (vert[1 ].second .smoothness + vert[0 ].first .smoothness ) / 2 );
961
962
} else { // Sharpen vertex uniformly
962
963
float smoothness = 0 ;
963
964
for (const Pair& pair : vert) {
@@ -966,9 +967,12 @@ void Manifold::Impl::CreateTangents(std::vector<Smoothness> sharpenedEdges) {
966
967
}
967
968
smoothness /= 2 * vert.size ();
968
969
969
- ForVert (vert[0 ].first .halfedge , [&tangent, smoothness](int current) {
970
+ const int start = vert[0 ].first .halfedge ;
971
+ int current = start;
972
+ do {
970
973
tangent[current] = smoothness * tangent[current];
971
- });
974
+ current = NextHalfedge (halfedge_[current].pairedHalfedge );
975
+ } while (current != start);
972
976
}
973
977
}
974
978
}
0 commit comments