@@ -154,6 +154,11 @@ Point_2 getUVCoordinates(const Point_3& barycentric_coordinates, const Triangle_
154
154
template <typename PointsContainer>
155
155
void makeMeshFromPointsCloud (const PointsContainer& points_cloud, PolygonMesh& output_mesh, const coord_t points_grid_resolution)
156
156
{
157
+ if (points_cloud.empty ())
158
+ {
159
+ return ;
160
+ }
161
+
157
162
const double alpha = points_grid_resolution * 2.0 ;
158
163
const double offset = alpha / 50.0 ;
159
164
@@ -587,10 +592,11 @@ std::size_t hash_value(VoxelGrid::LocalCoordinates const& position)
587
592
return boost::hash<uint64_t >()(position.key );
588
593
}
589
594
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)
591
596
{
592
597
const auto faces = mesh.faces ();
593
598
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;
594
600
595
601
run_multiple_producers_ordered_consumer (
596
602
0 ,
@@ -618,12 +624,27 @@ void makeInitialVoxelSpaceFromTexture(const PolygonMesh& mesh, const std::shared
618
624
if (extruder_nr.has_value ())
619
625
{
620
626
voxel_space.setOrUpdateOccupation (traversed_voxel, extruder_nr.value ());
627
+ found_extruders.insert (extruder_nr.value ());
621
628
}
622
629
}
623
630
624
631
return true ;
625
632
},
626
633
[](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 ;
627
648
}
628
649
629
650
void registerModifiedMesh (MeshGroup* meshgroup, const PolygonMesh& output_mesh)
@@ -724,7 +745,11 @@ void makeModifierMeshVoxelSpace(const PolygonMesh& mesh, const std::shared_ptr<T
724
745
spdlog::info (" Fill original voxels based on texture data" );
725
746
double resolution = settings.get <double >(" multi_material_paint_resolution" ) * 1000.0 ;
726
747
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
+ }
728
753
729
754
VoxelGrid low_res_texture_data (bounding_box, std::max (1000.0 , resolution));
730
755
makeInitialVoxelSpaceFromTexture (mesh, texture_data_provider, low_res_texture_data);
@@ -886,6 +911,11 @@ void makeModifierMeshVoxelSpace(const PolygonMesh& mesh, const std::shared_ptr<T
886
911
887
912
void splitMesh (Mesh& mesh, MeshGroup* meshgroup)
888
913
{
914
+ if (mesh.texture_ == nullptr || mesh.texture_data_mapping_ == nullptr || ! mesh.texture_data_mapping_ ->contains (" extruder" ))
915
+ {
916
+ return ;
917
+ }
918
+
889
919
spdlog::stopwatch timer;
890
920
891
921
PolygonMesh converted_mesh;
@@ -894,6 +924,7 @@ void splitMesh(Mesh& mesh, MeshGroup* meshgroup)
894
924
converted_mesh.add_vertex (Point_3 (vertex.p_ .x_ , vertex.p_ .y_ , vertex.p_ .z_ ));
895
925
}
896
926
927
+ bool has_uvs = false ;
897
928
auto uv_coords = converted_mesh.add_property_map <CGAL::SM_Face_index, std::array<Point_2, 3 >>(" f:uv_coords" ).first ;
898
929
for (const MeshFace& face : mesh.faces_ )
899
930
{
@@ -913,6 +944,7 @@ void splitMesh(Mesh& mesh, MeshGroup* meshgroup)
913
944
{
914
945
const auto & uv = face.uv_coordinates_ [j].value ();
915
946
face_uvs[j] = Point_2 (uv.x_ , uv.y_ );
947
+ has_uvs = true ;
916
948
}
917
949
else
918
950
{
@@ -923,10 +955,20 @@ void splitMesh(Mesh& mesh, MeshGroup* meshgroup)
923
955
uv_coords[face_index] = face_uvs;
924
956
}
925
957
958
+ if (! has_uvs)
959
+ {
960
+ return ;
961
+ }
962
+
926
963
const auto texture_data_provider = std::make_shared<TextureDataProvider>(nullptr , mesh.texture_ , mesh.texture_data_mapping_ );
927
964
928
965
PolygonMesh output_mesh;
929
966
makeModifierMeshVoxelSpace (converted_mesh, texture_data_provider, output_mesh);
967
+ if (output_mesh.faces ().empty ())
968
+ {
969
+ return ;
970
+ }
971
+
930
972
registerModifiedMesh (meshgroup, output_mesh);
931
973
932
974
spdlog::info (" Multi-material mesh splitting took {} seconds" , timer.elapsed ().count ());
0 commit comments