Skip to content

Commit 08b9d23

Browse files
committed
Add early-outs
CURA-12587 This avoids some crashes and long operations that turn out to be useless
1 parent 4562f0f commit 08b9d23

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/MeshMaterialSplitter.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ Point_2 getUVCoordinates(const Point_3& barycentric_coordinates, const Triangle_
154154
template<typename PointsContainer>
155155
void makeMeshFromPointsCloud(const PointsContainer& points_cloud, PolygonMesh& output_mesh, const coord_t points_grid_resolution)
156156
{
157+
if (points_cloud.empty())
158+
{
159+
return;
160+
}
161+
157162
const double alpha = points_grid_resolution * 2.0;
158163
const double offset = alpha / 50.0;
159164

@@ -587,10 +592,11 @@ std::size_t hash_value(VoxelGrid::LocalCoordinates const& position)
587592
return boost::hash<uint64_t>()(position.key);
588593
}
589594

590-
void makeInitialVoxelSpaceFromTexture(const PolygonMesh& mesh, const std::shared_ptr<TextureDataProvider>& texture_data_provider, VoxelGrid& voxel_space)
595+
bool makeInitialVoxelSpaceFromTexture(const PolygonMesh& mesh, const std::shared_ptr<TextureDataProvider>& texture_data_provider, VoxelGrid& voxel_space)
591596
{
592597
const auto faces = mesh.faces();
593598
const auto uv_coords = mesh.property_map<CGAL::SM_Face_index, std::array<Point_2, 3>>("f:uv_coords").value();
599+
boost::concurrent_flat_set<uint8_t> found_extruders;
594600

595601
run_multiple_producers_ordered_consumer(
596602
0,
@@ -618,12 +624,27 @@ void makeInitialVoxelSpaceFromTexture(const PolygonMesh& mesh, const std::shared
618624
if (extruder_nr.has_value())
619625
{
620626
voxel_space.setOrUpdateOccupation(traversed_voxel, extruder_nr.value());
627+
found_extruders.insert(extruder_nr.value());
621628
}
622629
}
623630

624631
return true;
625632
},
626633
[](bool result) {});
634+
635+
if (found_extruders.size() == 1)
636+
{
637+
// We have found only one extruder in the texture, so return true only if this extruder is not 0, otherwise the whole splitting is useless
638+
bool is_non_zero = true;
639+
found_extruders.visit_all(
640+
[&is_non_zero](const uint8_t extruder_nr)
641+
{
642+
is_non_zero = extruder_nr != 0;
643+
});
644+
return is_non_zero;
645+
}
646+
647+
return true;
627648
}
628649

629650
void registerModifiedMesh(MeshGroup* meshgroup, const PolygonMesh& output_mesh)
@@ -724,7 +745,11 @@ void makeModifierMeshVoxelSpace(const PolygonMesh& mesh, const std::shared_ptr<T
724745
spdlog::info("Fill original voxels based on texture data");
725746
double resolution = settings.get<double>("multi_material_paint_resolution") * 1000.0;
726747
VoxelGrid voxel_space(bounding_box, resolution);
727-
makeInitialVoxelSpaceFromTexture(mesh, texture_data_provider, voxel_space);
748+
if (! makeInitialVoxelSpaceFromTexture(mesh, texture_data_provider, voxel_space))
749+
{
750+
// Texture is filled with 0s, don't bother doing anything
751+
return;
752+
}
728753

729754
VoxelGrid low_res_texture_data(bounding_box, std::max(1000.0, resolution));
730755
makeInitialVoxelSpaceFromTexture(mesh, texture_data_provider, low_res_texture_data);
@@ -886,6 +911,11 @@ void makeModifierMeshVoxelSpace(const PolygonMesh& mesh, const std::shared_ptr<T
886911

887912
void splitMesh(Mesh& mesh, MeshGroup* meshgroup)
888913
{
914+
if (mesh.texture_ == nullptr || mesh.texture_data_mapping_ == nullptr || ! mesh.texture_data_mapping_->contains("extruder"))
915+
{
916+
return;
917+
}
918+
889919
spdlog::stopwatch timer;
890920

891921
PolygonMesh converted_mesh;
@@ -894,6 +924,7 @@ void splitMesh(Mesh& mesh, MeshGroup* meshgroup)
894924
converted_mesh.add_vertex(Point_3(vertex.p_.x_, vertex.p_.y_, vertex.p_.z_));
895925
}
896926

927+
bool has_uvs = false;
897928
auto uv_coords = converted_mesh.add_property_map<CGAL::SM_Face_index, std::array<Point_2, 3>>("f:uv_coords").first;
898929
for (const MeshFace& face : mesh.faces_)
899930
{
@@ -913,6 +944,7 @@ void splitMesh(Mesh& mesh, MeshGroup* meshgroup)
913944
{
914945
const auto& uv = face.uv_coordinates_[j].value();
915946
face_uvs[j] = Point_2(uv.x_, uv.y_);
947+
has_uvs = true;
916948
}
917949
else
918950
{
@@ -923,10 +955,20 @@ void splitMesh(Mesh& mesh, MeshGroup* meshgroup)
923955
uv_coords[face_index] = face_uvs;
924956
}
925957

958+
if (! has_uvs)
959+
{
960+
return;
961+
}
962+
926963
const auto texture_data_provider = std::make_shared<TextureDataProvider>(nullptr, mesh.texture_, mesh.texture_data_mapping_);
927964

928965
PolygonMesh output_mesh;
929966
makeModifierMeshVoxelSpace(converted_mesh, texture_data_provider, output_mesh);
967+
if (output_mesh.faces().empty())
968+
{
969+
return;
970+
}
971+
930972
registerModifiedMesh(meshgroup, output_mesh);
931973

932974
spdlog::info("Multi-material mesh splitting took {} seconds", timer.elapsed().count());

0 commit comments

Comments
 (0)