Skip to content

Commit a22ecf4

Browse files
UPDATE: association criterion now with projection, and no unwanted deletions in DFSegmentation methods
1 parent 2385589 commit a22ecf4

File tree

2 files changed

+63
-51
lines changed

2 files changed

+63
-51
lines changed

src/diffCheck/segmentation/DFSegmentation.cc

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -119,35 +119,41 @@ namespace diffCheck::segmentation
119119
// Getting the normal of the mesh face
120120
Eigen::Vector3d faceNormal = face->GetFirstNormal();
121121
faceNormal.normalize();
122-
123-
// iterate through the segments to find the closest one compared to the face center taking the normals into account
124-
Eigen::Vector3d segmentCenter;
125-
Eigen::Vector3d segmentNormal;
122+
126123
double faceDistance = std::numeric_limits<double>::max();
127124
if (clusters.size() == 0)
128125
{
129126
DIFFCHECK_WARN("No clusters to associate with the mesh faces. Returning the first mesh face untouched.");
130127
return clusters[0];
131128
}
132129
for (auto segment : clusters)
133-
{
130+
{
131+
Eigen::Vector3d segmentCenter;
132+
Eigen::Vector3d segmentNormal;
133+
134134
for (auto point : segment->Points){segmentCenter += point;}
135-
segmentCenter /= segment->GetNumPoints();
135+
if (segment->GetNumPoints() > 0)
136+
{
137+
segmentCenter /= segment->GetNumPoints();
138+
}
139+
else
140+
{
141+
DIFFCHECK_WARN("Empty segment. Skipping the segment.");
142+
continue;
143+
}
136144

137145
for (auto normal : segment->Normals){segmentNormal += normal;}
138146
segmentNormal.normalize();
139147

140-
double currentDistance = (faceCenter - segmentCenter).norm() / std::abs(segmentNormal.dot(faceNormal));
148+
double currentDistance = (faceCenter - segmentCenter).norm();
141149
// if the distance is smaller than the previous one, update the distance and the corresponding segment
142150
if (std::abs(sin(acos(faceNormal.dot(segmentNormal)))) < angleThreshold && currentDistance < faceDistance)
143151
{
144152
correspondingSegment = segment;
145153
faceDistance = currentDistance;
146154
}
155+
147156
}
148-
149-
// get the triangles of the face.
150-
std::vector<Eigen::Vector3i> faceTriangles = face->Faces;
151157

152158
if (correspondingSegment == nullptr)
153159
{
@@ -185,15 +191,15 @@ namespace diffCheck::segmentation
185191
correspondingSegment->Points.end());
186192
}
187193
// removing the corresponding segment if it is empty after the point transfer
188-
if (correspondingSegment->GetNumPoints() == 0)
189-
{
190-
clusters.erase(
191-
std::remove(
192-
clusters.begin(),
193-
clusters.end(),
194-
correspondingSegment),
195-
clusters.end());
196-
}
194+
// if (correspondingSegment->GetNumPoints() == 0)
195+
// {
196+
// clusters.erase(
197+
// std::remove(
198+
// clusters.begin(),
199+
// clusters.end(),
200+
// correspondingSegment),
201+
// clusters.end());
202+
// }
197203
}
198204
return unifiedPointCloud;
199205
}
@@ -269,7 +275,7 @@ namespace diffCheck::segmentation
269275

270276
double clusterNormalToJunctionLineAngle = std::abs(std::acos(clusterNormal.dot((clusterCenter - faceCenter).normalized())));
271277

272-
double currentDistance = (clusterCenter - faceCenter).norm() * std::pow(std::cos(clusterNormalToJunctionLineAngle), 2) / std::pow(clusterNormal.dot(faceNormal), 2);
278+
double currentDistance = (clusterCenter - faceCenter).norm() * std::abs(std::cos(clusterNormalToJunctionLineAngle)) / std::abs(clusterNormal.dot(faceNormal));
273279
if (std::abs(sin(acos(faceNormal.dot(clusterNormal)))) < angleThreshold && currentDistance < distance)
274280
{
275281
distance = currentDistance;
@@ -294,22 +300,22 @@ namespace diffCheck::segmentation
294300
completed_segment->Normals.push_back(cluster->Normals[std::distance(cluster->Points.begin(), std::find(cluster->Points.begin(), cluster->Points.end(), point))]);
295301
}
296302
}
297-
std::vector<int> indicesToRemove;
303+
// std::vector<int> indicesToRemove;
298304

299-
for (int i = 0; i < cluster->Points.size(); ++i)
300-
{
301-
if (std::find(completed_segment->Points.begin(), completed_segment->Points.end(), cluster->Points[i]) != completed_segment->Points.end())
302-
{
303-
indicesToRemove.push_back(i);
304-
}
305-
}
306-
for (auto it = indicesToRemove.rbegin(); it != indicesToRemove.rend(); ++it)
307-
{
308-
std::swap(cluster->Points[*it], cluster->Points.back());
309-
cluster->Points.pop_back();
310-
std::swap(cluster->Normals[*it], cluster->Normals.back());
311-
cluster->Normals.pop_back();
312-
}
305+
// for (int i = 0; i < cluster->Points.size(); ++i)
306+
// {
307+
// if (std::find(completed_segment->Points.begin(), completed_segment->Points.end(), cluster->Points[i]) != completed_segment->Points.end())
308+
// {
309+
// indicesToRemove.push_back(i);
310+
// }
311+
// }
312+
// for (auto it = indicesToRemove.rbegin(); it != indicesToRemove.rend(); ++it)
313+
// {
314+
// std::swap(cluster->Points[*it], cluster->Points.back());
315+
// cluster->Points.pop_back();
316+
// std::swap(cluster->Normals[*it], cluster->Normals.back());
317+
// cluster->Normals.pop_back();
318+
// }
313319
}
314320
};
315321

@@ -331,18 +337,22 @@ namespace diffCheck::segmentation
331337
Eigen::Vector3d v1 = face->Vertices[triangle[1]];
332338
Eigen::Vector3d v2 = face->Vertices[triangle[2]];
333339
Eigen::Vector3d n = (v1 - v0).cross(v2 - v0);
334-
double referenceTriangleArea = n.norm()*0.5;
335-
Eigen::Vector3d n1 = (v1 - v0).cross(point - v0);
340+
double normOfNormal = n.norm();
341+
n.normalize();
342+
343+
Eigen::Vector3d projectedPoint = point - n * (n.dot(point - v0)) ;
344+
345+
double referenceTriangleArea = normOfNormal*0.5;
346+
Eigen::Vector3d n1 = (v1 - v0).cross(projectedPoint - v0);
336347
double area1 = n1.norm()*0.5;
337-
Eigen::Vector3d n2 = (v2 - v1).cross(point - v1);
348+
Eigen::Vector3d n2 = (v2 - v1).cross(projectedPoint - v1);
338349
double area2 = n2.norm()*0.5;
339-
Eigen::Vector3d n3 = (v0 - v2).cross(point - v2);
350+
Eigen::Vector3d n3 = (v0 - v2).cross(projectedPoint - v2);
340351
double area3 = n3.norm()*0.5;
341-
double res = ( area1 + area2 + area3 - referenceTriangleArea) / referenceTriangleArea;
342-
if (res < associationThreshold)
352+
double res = (area1 + area2 + area3 - referenceTriangleArea) / referenceTriangleArea;
353+
if (std::abs(res) < associationThreshold)
343354
{
344355
return true;
345-
break;
346356
}
347357
}
348358
return false;

src/gh/components/DF_CAD_segmentator/code.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ def RunScript(self,
4343

4444
df_beams = i_assembly.beams
4545
df_beams_meshes = []
46+
rh_beams_meshes = []
4647

4748
for df_b in df_beams:
4849
rh_b_mesh_faces = [df_b_f.to_mesh() for df_b_f in df_b.side_faces]
4950
df_b_mesh_faces = [df_cvt_bindings.cvt_rhmesh_2_dfmesh(rh_b_mesh_face) for rh_b_mesh_face in rh_b_mesh_faces]
5051
df_beams_meshes.append(df_b_mesh_faces)
52+
rh_beams_meshes.append(rh_b_mesh_faces)
5153

5254
df_asssociated_cluster = dfb_segmentation.DFSegmentation.associate_clusters(
5355
reference_mesh=df_b_mesh_faces,
@@ -68,21 +70,21 @@ def RunScript(self,
6870
df_clusters.append(df_asssociated_cluster)
6971

7072
# FIXME: the refiner is crashing the script
71-
# dfb_segmentation.DFSegmentation.clean_unassociated_clusters(
72-
# unassociated_clusters=df_clouds,
73-
# associated_clusters=df_clusters,
74-
# reference_mesh=df_beams_meshes,
75-
# angle_threshold=i_angle_threshold,
76-
# association_threshold=i_association_threshold
77-
# )
73+
dfb_segmentation.DFSegmentation.clean_unassociated_clusters(
74+
unassociated_clusters=df_clouds,
75+
associated_clusters=df_clusters,
76+
reference_mesh=df_beams_meshes,
77+
angle_threshold=i_angle_threshold,
78+
association_threshold=i_association_threshold
79+
)
7880

7981
o_clusters = [df_cvt_bindings.cvt_dfcloud_2_rhcloud(cluster) for cluster in df_clusters]
8082

81-
return o_clusters
83+
return o_clusters, rh_beams_meshes
8284

8385
if __name__ == "__main__":
8486
com = DFCADSegmentator()
85-
o_clusters = com.RunScript(
87+
o_clusters, rh_beams_meshes = com.RunScript(
8688
i_clouds,
8789
i_assembly,
8890
i_angle_threshold,

0 commit comments

Comments
 (0)