Skip to content

Commit a4b4f21

Browse files
feat: adapt distance computation depending of angle in CAD segmentation
1 parent b8cadee commit a4b4f21

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/diffCheck/segmentation/DFSegmentation.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "DFSegmentation.hh"
2+
#include <fstream>
3+
#include <chrono>
24

35
#include <cilantro/utilities/point_cloud.hpp>
46
#include <cilantro/core/nearest_neighbors.hpp>
@@ -234,6 +236,12 @@ namespace diffCheck::segmentation
234236
}
235237
else
236238
{
239+
std::string timestamp = std::to_string(
240+
std::chrono::duration_cast<std::chrono::milliseconds>(
241+
std::chrono::system_clock::now().time_since_epoch()
242+
).count()
243+
);
244+
std::ofstream logFile("C:\\Users\\localuser\\Desktop\\association_log_" + timestamp + ".txt", std::ios::app);
237245
for (std::shared_ptr<diffCheck::geometry::DFMesh> face : referenceMesh)
238246
{
239247
std::shared_ptr<geometry::DFPointCloud> correspondingSegment;
@@ -274,6 +282,8 @@ namespace diffCheck::segmentation
274282
// if the distance is smaller than the previous one, update the distance and the corresponding segment
275283
if (std::abs(sin(acos(faceNormal.dot(segmentNormal)))) < angleThreshold && currentDistance * (angleAssociationThreshold + std::abs(faceNormal.dot((faceCenter - segmentCenter) / (faceCenter - segmentCenter).norm()))) < faceDistance)
276284
{
285+
logFile << std::abs(sin(acos(faceNormal.dot(segmentNormal)))) << " < " << angleThreshold << " and " << currentDistance << " * (" << angleAssociationThreshold << " + " << std::abs(faceNormal.dot((faceCenter - segmentCenter) / (faceCenter - segmentCenter).norm())) << ") < " << faceDistance << std::endl;
286+
logFile << " considered face: Face normal: " << faceNormal.transpose() << ", Segment normal: " << segmentNormal.transpose() << ", Current distance: " << currentDistance << ", Face distance: " << faceDistance << std::endl;
277287
correspondingSegment = segment;
278288
faceDistance = currentDistance * (angleAssociationThreshold + std::abs(faceNormal.dot((faceCenter - segmentCenter) / (faceCenter - segmentCenter).norm())));
279289
}
@@ -324,6 +334,7 @@ namespace diffCheck::segmentation
324334
}
325335
faceSegments.push_back(facePoints);
326336
}
337+
logFile.close();
327338
}
328339
return faceSegments;
329340
}
@@ -334,7 +345,8 @@ namespace diffCheck::segmentation
334345
std::vector<std::vector<std::shared_ptr<geometry::DFPointCloud>>> &existingPointCloudSegments,
335346
std::vector<std::vector<std::shared_ptr<geometry::DFMesh>>> meshes,
336347
double angleThreshold,
337-
double associationThreshold)
348+
double associationThreshold,
349+
double angleAssociationThreshold)
338350
{
339351
if (unassociatedClusters.size() == 0)
340352
{
@@ -442,7 +454,7 @@ namespace diffCheck::segmentation
442454

443455
double currentDistance = (clusterCenter - faceCenter).norm() * std::abs(std::cos(clusterNormalToJunctionLineAngle))
444456
/ std::min(std::abs(clusterNormal.dot(faceNormal)), 0.05) ;
445-
if (std::abs(sin(acos(faceNormal.dot(clusterNormal)))) < angleThreshold && currentDistance < distance && std::abs(1 - std::sin(clusterNormalToJunctionLineAngle)) < associationThreshold)
457+
if (std::abs(sin(acos(faceNormal.dot(clusterNormal)))) < angleThreshold && currentDistance * (angleAssociationThreshold + std::abs(faceNormal.dot((faceCenter - clusterCenter) / (faceCenter - clusterCenter).norm()))) < distance)
446458
{
447459
goodMeshIndex = meshIndex;
448460
goodFaceIndex = faceIndex;

src/diffCheck/segmentation/DFSegmentation.hh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ namespace diffCheck::segmentation
4848
* @param unassociatedClusters the clusters from the normal-based segmentatinon that haven't been associated yet.
4949
* @param existingPointCloudSegments the already associated segments per mesh face.
5050
* @param meshes the mesh faces for all the model. This is used to associate the clusters to the mesh faces.
51-
* * @param angleThreshold the threshold to consider the a cluster as potential candidate for association. the value passed is the minimum sine of the angles. A value of 0 requires perfect alignment (angle = 0), while a value of 0.1 allows an angle of 5.7 degrees.
51+
* @param angleThreshold the threshold to consider the a cluster as potential candidate for association. the value passed is the minimum sine of the angles. A value of 0 requires perfect alignment (angle = 0), while a value of 0.1 allows an angle of 5.7 degrees.
5252
* @param associationThreshold the threshold to consider the points of a segment and a mesh face as associable. It is the ratio between the surface of the closest mesh triangle and the sum of the areas of the three triangles that form the rest of the pyramid described by the mesh triangle and the point we want to associate or not. The lower the number, the more strict the association will be and some poinnts on the mesh face might be wrongfully excluded.
53+
* @param angleAssociationThreshold a number to indicate how much distance in the plane of the face should be favored, compared to distance orthogonal to the face normal. If set to 0, any face in the same plane as the face will be considered as having a distance of 0. If set to a high value (e.g. 1000000), no difference will be made between distance in the plane of the face and orthogonal to it. Default is 0.5
54+
* @return void
5355
*/
5456
static void DFSegmentation::CleanUnassociatedClusters(
5557
bool isCylinder,
5658
std::vector<std::shared_ptr<geometry::DFPointCloud>> &unassociatedClusters,
5759
std::vector<std::vector<std::shared_ptr<geometry::DFPointCloud>>> &existingPointCloudSegments,
5860
std::vector<std::vector<std::shared_ptr<geometry::DFMesh>>> meshes,
5961
double angleThreshold = 0.1,
60-
double associationThreshold = 0.1);
62+
double associationThreshold = 0.1,
63+
double angleAssociationThreshold = 0.5);
6164
};
6265
} // namespace diffCheck::segmentation

src/diffCheckBindings.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,6 @@ PYBIND11_MODULE(diffcheck_bindings, m) {
238238
py::arg("associated_clusters"),
239239
py::arg("reference_mesh"),
240240
py::arg("angle_threshold") = 0.1,
241-
py::arg("association_threshold") = 0.1);
241+
py::arg("association_threshold") = 0.1,
242+
py::arg("angle_association_threshold") = 0.5);
242243
}

src/gh/components/DF_CAD_segmentator/code.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def RunScript(self,
3030
i_angle_threshold = 0.1
3131
if i_association_threshold is None:
3232
i_association_threshold = 0.1
33-
33+
if i_angle_association_threshold is None:
34+
i_angle_association_threshold = 0.5
3435
o_face_clusters = []
3536
df_clusters = []
3637
# we make a deepcopy of the input clouds
@@ -66,7 +67,8 @@ def RunScript(self,
6667
associated_clusters=[df_asssociated_cluster_faces_per_beam[i]],
6768
reference_mesh=[df_b_mesh_faces],
6869
angle_threshold=i_angle_threshold,
69-
association_threshold=i_association_threshold
70+
association_threshold=i_association_threshold,
71+
angle_association_threshold=i_angle_association_threshold
7072
)
7173

7274
o_face_clusters[-1] = [df_cvt_bindings.cvt_dfcloud_2_rhcloud(cluster) for cluster in df_asssociated_cluster_faces_per_beam[i]]

0 commit comments

Comments
 (0)