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