PD-536: Preserve the painting when splitting a volume to parts and to objects.

This commit is contained in:
Lukáš Hejl 2026-11-05 13:28:30 +01:00
parent 264712491d
commit e57bffc0bd
4 changed files with 97 additions and 28 deletions

View file

@ -5,6 +5,12 @@
#include "Slic3r/Domain/ModelVolume.hpp"
#include "Slic3r/Domain/Types.hpp"
#include <vector>
namespace Slic3r::Biz::Algorithms::TriangleMesh {
struct SourceFacet;
} // namespace Slic3r::Biz::Algorithms::TriangleMesh
namespace Slic3r::Biz::Algorithms::ModelVolume {
Domain::ModelVolume* construct_ptr(Domain::ModelObject* object, const Domain::TriangleMesh& mesh, Domain::ModelVolumeType type = Domain::ModelVolumeType::MODEL_PART);
@ -36,6 +42,18 @@ size_t split(Domain::ModelVolume* volume, unsigned int max_extruders);
bool is_splittable(const Domain::ModelVolume& model_volume);
/**
* @brief Remaps the painting of source_volume onto derived_volume, whose mesh was derived from source_mesh. The derived_volume may be the source_volume itself.
*
* @param source_mesh Mesh painted by source_volume, in the coordinate system of the mesh of derived_volume. It may differ from the current mesh of source_volume.
*/
void remap_facets_annotations(
const Domain::ModelVolume& source_volume,
const Domain::TriangleMesh& source_mesh,
const std::vector<TriangleMesh::SourceFacet>& source_facets,
Domain::ModelVolume& derived_volume
);
void calculate_convex_hull(Domain::ModelVolume& model_volume);
// Returns the bbox of the given ModelVolume transformed by the given transformation

View file

@ -10,6 +10,7 @@
#include "Slic3r/Domain/Model.hpp"
#include "Slic3r/Exception.hpp"
#include <algorithm>
#include <ranges>
#include <iostream>
@ -541,23 +542,23 @@ void split(Domain::ModelObject* object, Domain::ModelObjectPtrs* new_objects)
if (volume->text_configuration.has_value())
volume->text_configuration.reset();
std::vector<Domain::TriangleMesh> meshes =
Slic3r::Biz::Algorithms::TriangleMesh::split(volume->mesh());
std::sort(
meshes.begin(),
meshes.end(),
Slic3r::Biz::Algorithms::TriangleMesh::is_front_up_left
std::vector<TriangleMesh::TriangleMeshWithSourceFacets> components =
TriangleMesh::split_to_components(volume->mesh());
std::ranges::sort(
components,
TriangleMesh::is_front_up_left,
&TriangleMesh::TriangleMeshWithSourceFacets::mesh
);
size_t counter = 1;
for (Domain::TriangleMesh& mesh : meshes) {
for (auto& [mesh, source_facets] : components) {
// FIXME: crashes if not satisfied
if (mesh.facets_count() < 3 || mesh.has_zero_volume())
continue;
// XXX: this seems to be the only real usage of m_model, maybe refactor this so that it's not needed?
Domain::ModelObject* new_object = object->get_model()->add_object();
if (meshes.size() == 1) {
if (components.size() == 1) {
new_object->name = volume->name;
new_object->object_settings = object->object_settings.overrides.empty() ?
create_object_settings_from_volume_settings<Domain::ObjectSettings>(
@ -571,7 +572,7 @@ void split(Domain::ModelObject* object, Domain::ModelObjectPtrs* new_objects)
object->object_settings_sla;
} else {
new_object->name =
object->name + (meshes.size() > 1 ? "_" + std::to_string(counter++) : "");
object->name + (components.size() > 1 ? "_" + std::to_string(counter++) : "");
new_object->object_settings = object->object_settings;
new_object->object_settings_sla = object->object_settings_sla;
}
@ -581,7 +582,8 @@ void split(Domain::ModelObject* object, Domain::ModelObjectPtrs* new_objects)
new_object->add_instance(*model_instance);
Domain::ModelVolume* new_vol =
Algorithms::ModelObject::add_volume(new_object, *volume, std::move(mesh));
ModelObject::add_volume(new_object, *volume, std::move(mesh));
ModelVolume::remap_facets_annotations(*volume, volume->mesh(), source_facets, *new_vol);
// Invalidate extruder value in volume's config,
// otherwise there will no way to change extruder for object after splitting,

View file

@ -5,8 +5,10 @@
#include "Slic3r/Biz/Algorithms/ModelObject.hpp"
#include "Slic3r/Biz/Algorithms/SlaPoints.hpp"
#include "Slic3r/Biz/Algorithms/TriangleMesh.hpp"
#include "Slic3r/Biz/Algorithms/TriangleSelector.hpp"
#include "Slic3r/Domain/TriangleMesh.hpp"
#include <algorithm>
#include <boost/filesystem/path.hpp>
using namespace Slic3r::Biz::Algorithms;
@ -115,27 +117,51 @@ void mirror(Domain::ModelVolume& model_volume, const Domain::Axis axis)
model_volume.set_mirror(mirror);
}
void remap_facets_annotations(
const Domain::ModelVolume& source_volume,
const Domain::TriangleMesh& source_mesh,
const std::vector<TriangleMesh::SourceFacet>& source_facets,
Domain::ModelVolume& derived_volume
)
{
for (const Domain::FacetsAnnotationKind kind : Domain::ModelVolume::facets_annotation_kinds) {
if (const Domain::FacetsAnnotation& source_annotation =
source_volume.facets_annotation(kind);
!source_annotation.empty())
{
derived_volume.facets_annotation(kind).set_data(
TriangleSelector::remap_to_derived_mesh(
source_annotation.get_data(),
source_mesh,
derived_volume.mesh(),
source_facets
)
);
}
}
}
// Split this volume, append the result to the object owning this volume.
// Return the number of volumes created from this one.
// This is useful to assign different materials to different volumes of an object.
size_t split(Domain::ModelVolume* volume, unsigned int max_extruders)
{
std::vector<Domain::TriangleMesh> meshes = TriangleMesh::split(volume->mesh());
if (meshes.size() <= 1)
std::vector<TriangleMesh::TriangleMeshWithSourceFacets> components =
TriangleMesh::split_to_components(volume->mesh());
if (components.size() <= 1) {
return 1;
}
std::sort(
meshes.begin(),
meshes.end(),
Slic3r::Biz::Algorithms::TriangleMesh::is_front_up_left
std::ranges::sort(
components,
TriangleMesh::is_front_up_left,
&TriangleMesh::TriangleMeshWithSourceFacets::mesh
);
// splited volume should not be text object
if (volume->text_configuration.has_value())
volume->text_configuration.reset();
volume->reset_extra_facets();
// The SLA support points and drain holes go to the parts they sit on.
const Domain::SLA::SupportPoints sla_support_points = std::move(volume->sla_support_points);
const Domain::SLA::DrainHoles sla_drain_holes = std::move(volume->sla_drain_holes);
@ -152,7 +178,10 @@ size_t split(Domain::ModelVolume* volume, unsigned int max_extruders)
int extruder_counter = 0;
const Domain::Vec3d offset = volume->get_offset();
for (Domain::TriangleMesh& mesh : meshes) {
const std::shared_ptr<const Domain::TriangleMesh> source_mesh = volume->mesh_ptr();
const std::vector<TriangleMesh::SourceFacet>* kept_component_source_facets = nullptr;
for (auto& [mesh, source_facets] : components) {
if (mesh.empty() || mesh.has_zero_volume())
// Repair may have removed unconnected triangles, thus emptying the mesh.
continue;
@ -164,9 +193,13 @@ size_t split(Domain::ModelVolume* volume, unsigned int max_extruders)
volume->set_new_unique_id();
// reset the source to disable reload from disk
volume->source = Domain::ModelVolume::Source();
kept_component_source_facets = &source_facets;
} else {
Domain::ModelVolume* split_volume =
ModelObject::insert_volume(object, (++ivolume), *volume, std::move(mesh));
remap_facets_annotations(*volume, *source_mesh, source_facets, *split_volume);
}
else
Algorithms::ModelObject::insert_volume(object, (++ivolume), *volume, std::move(mesh));
object->volumes[ivolume]->set_offset(Domain::Vec3d::Zero());
Algorithms::ModelVolume::translate(*object->volumes[ivolume], offset);
@ -179,6 +212,12 @@ size_t split(Domain::ModelVolume* volume, unsigned int max_extruders)
extruder_counter = 0;
}
// The painting of this volume is the source for all the components, so it is remapped onto the component kept in this volume
// only after the other components took it over.
if (kept_component_source_facets != nullptr) {
remap_facets_annotations(*volume, *source_mesh, *kept_component_source_facets, *volume);
}
// discard volumes for which the convex hull was not generated or is degenerate
size_t i = 0;
while (i < object->volumes.size()) {

View file

@ -74,7 +74,7 @@ TEST_CASE("With limited instances temporarily removes instances and objects", "[
});
}
TEST_CASE("Splitting a painted volume clears facet annotations", "[model-volume][split]")
TEST_CASE("Splitting a painted volume keeps the painting of its parts", "[model-volume][split]")
{
TriangleMesh disconnected_mesh = make_cube(10, 10, 10);
TriangleMesh second_cube = make_cube(10, 10, 10);
@ -102,11 +102,21 @@ TEST_CASE("Splitting a painted volume clears facet annotations", "[model-volume]
REQUIRE(Algorithms::ModelVolume::split(painted_volume, 2) == 2);
REQUIRE(model_object->volumes.size() == 2);
for (const ModelVolume* split_volume : model_object->volumes) {
CHECK(split_volume->mesh().facets_count() == 12);
CHECK(split_volume->supported_facets.empty());
CHECK(split_volume->seam_facets.empty());
CHECK(split_volume->mm_segmentation_facets.empty());
CHECK(split_volume->fuzzy_skin_facets.empty());
// The painted facet is the last one of the second cube, which becomes the second part.
const ModelVolume& first_part = *model_object->volumes.front();
const ModelVolume& second_part = *model_object->volumes.back();
CHECK(first_part.mesh().facets_count() == 12);
CHECK(second_part.mesh().facets_count() == 12);
CHECK_FALSE(first_part.is_painted());
const Slic3r::Domain::its_triangle painted_facet_vertices =
Slic3r::Domain::its_triangle_vertices(disconnected_mesh.its, painted_facet_index);
for (const Slic3r::Domain::FacetsAnnotationKind kind : ModelVolume::facets_annotation_kinds) {
TriangleSelector part_selector{second_part.mesh()};
part_selector.deserialize(second_part.facets_annotation(kind).get_data());
const indexed_triangle_set painted_facets =
part_selector.get_facets(TriangleStateType::ENFORCER);
REQUIRE(painted_facets.indices.size() == 1);
CHECK((Slic3r::Domain::its_triangle_vertices(painted_facets, 0) == painted_facet_vertices));
}
}