Skip to content

Commit 111c484

Browse files
UPDATE: adding some preventive checks to the methods in DFSegmentation
1 parent b688ec9 commit 111c484

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

src/diffCheck/segmentation/DFSegmentation.cc

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,37 +104,37 @@ namespace diffCheck::segmentation
104104
std::vector<std::shared_ptr<geometry::DFPointCloud>> segmentsRemainder;
105105

106106
// iterate through the mesh faces given as function argument
107+
if (referenceMesh.size() == 0)
108+
{
109+
DIFFCHECK_WARN("No mesh faces to associate with the clusters. Returning the first clusters untouched.");
110+
return clusters[0];
111+
}
107112
for (std::shared_ptr<diffCheck::geometry::DFMesh> face : referenceMesh)
108113
{
109114
std::shared_ptr<geometry::DFPointCloud> correspondingSegment;
110-
std::shared_ptr<open3d::geometry::TriangleMesh> o3dFace;
111-
o3dFace = face->Cvt2O3DTriangleMesh();
112115

113116
// Getting the center of the mesh face
114-
Eigen::Vector3d faceCenter;
115-
faceCenter = o3dFace->GetCenter();
117+
Eigen::Vector3d faceCenter = face->Cvt2O3DTriangleMesh()->GetCenter();
116118

117119
// Getting the normal of the mesh face
118-
119120
Eigen::Vector3d faceNormal = face->GetFirstNormal();
120121
faceNormal.normalize();
121122

122-
// iterate through the segments to find the closest ones compared to the face center taking the normals into account
123+
// iterate through the segments to find the closest one compared to the face center taking the normals into account
123124
Eigen::Vector3d segmentCenter;
124125
Eigen::Vector3d segmentNormal;
125126
double faceDistance = std::numeric_limits<double>::max();
127+
if (clusters.size() == 0)
128+
{
129+
DIFFCHECK_WARN("No clusters to associate with the mesh faces. Returning the first mesh face untouched.");
130+
return clusters[0];
131+
}
126132
for (auto segment : clusters)
127133
{
128-
for (auto point : segment->Points)
129-
{
130-
segmentCenter += point;
131-
}
134+
for (auto point : segment->Points){segmentCenter += point;}
132135
segmentCenter /= segment->GetNumPoints();
133136

134-
for (auto normal : segment->Normals)
135-
{
136-
segmentNormal += normal;
137-
}
137+
for (auto normal : segment->Normals){segmentNormal += normal;}
138138
segmentNormal.normalize();
139139

140140
double currentDistance = (faceCenter - segmentCenter).norm() / std::abs(segmentNormal.dot(faceNormal));
@@ -149,6 +149,11 @@ namespace diffCheck::segmentation
149149
// get the triangles of the face.
150150
std::vector<Eigen::Vector3i> faceTriangles = face->Faces;
151151

152+
if (correspondingSegment == nullptr)
153+
{
154+
DIFFCHECK_WARN("No segment found for the face. Skipping the face.");
155+
continue;
156+
}
152157
for (Eigen::Vector3d point : correspondingSegment->Points)
153158
{
154159
bool pointInFace = false;
@@ -165,6 +170,11 @@ namespace diffCheck::segmentation
165170
}
166171
}
167172
// removing points from the segment that are in the face
173+
if (unifiedPointCloud->GetNumPoints() == 0)
174+
{
175+
DIFFCHECK_WARN("No point was associated to this segment. Skipping the segment.");
176+
continue;
177+
}
168178
for(Eigen::Vector3d point : unifiedPointCloud->Points)
169179
{
170180
correspondingSegment->Points.erase(
@@ -174,6 +184,7 @@ namespace diffCheck::segmentation
174184
point),
175185
correspondingSegment->Points.end());
176186
}
187+
// removing the corresponding segment if it is empty after the point transfer
177188
if (correspondingSegment->GetNumPoints() == 0)
178189
{
179190
clusters.erase(
@@ -194,10 +205,21 @@ namespace diffCheck::segmentation
194205
double angleThreshold,
195206
double associationThreshold)
196207
{
208+
if (unassociatedClusters.size() == 0)
209+
{
210+
DIFFCHECK_WARN("No unassociated clusters. Nothing is done");
211+
return;
212+
}
197213
for (std::shared_ptr<geometry::DFPointCloud> cluster : unassociatedClusters)
198214
{
199215
Eigen::Vector3d clusterCenter;
200216
Eigen::Vector3d clusterNormal;
217+
218+
if (cluster->GetNumPoints() == 0)
219+
{
220+
DIFFCHECK_WARN("Empty cluster. Skipping the cluster.");
221+
continue;
222+
}
201223
for (Eigen::Vector3d point : cluster->Points)
202224
{
203225
clusterCenter += point;
@@ -213,8 +235,19 @@ namespace diffCheck::segmentation
213235
int meshIndex;
214236
int faceIndex ;
215237
double distance = std::numeric_limits<double>::max();
238+
239+
if (meshes.size() == 0)
240+
{
241+
DIFFCHECK_WARN("No meshes to associate with the clusters. Skipping the cluster.");
242+
continue;
243+
}
216244
for (std::vector<std::shared_ptr<geometry::DFMesh>> piece : meshes)
217245
{
246+
if (piece.size() == 0)
247+
{
248+
DIFFCHECK_WARN("Empty piece in the meshes vector. Skipping the mesh face vector.");
249+
continue;
250+
}
218251
for (std::shared_ptr<geometry::DFMesh> meshFace : piece)
219252
{
220253
Eigen::Vector3d faceCenter;
@@ -259,7 +292,7 @@ namespace diffCheck::segmentation
259292
}
260293
}
261294
std::vector<int> indicesToRemove;
262-
295+
263296
for (int i = 0; i < cluster->Points.size(); ++i)
264297
{
265298
if (std::find(completed_segment->Points.begin(), completed_segment->Points.end(), cluster->Points[i]) != completed_segment->Points.end())

0 commit comments

Comments
 (0)