mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-01 09:22:58 -05:00
Split to objects gave each negative part, modifier and blocker to the one piece it overlapped most, so one reaching across two pieces no longer acted on the other. It also copied a painted modifier with the ID of the part it was painted on, which the split replaces, so the modifier lost its part: the painting tool could not find it and 3MF export dropped the link. When one part was split into its shells, the modifier kept the whole mesh on a single piece, and paint on the other shells was lost. Give every such volume to each piece it overlaps. Give a painted modifier to each piece of its part that holds some of its paint, point it at that piece's part, and move its paint onto the piece's mesh.
821 lines
37 KiB
C++
821 lines
37 KiB
C++
#include <catch2/catch_all.hpp>
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <map>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include "libslic3r/PrintConfig.hpp"
|
|
#include "libslic3r/TriangleMesh.hpp"
|
|
#include "libslic3r/Polygon.hpp"
|
|
#include "libslic3r/BoundingBox.hpp"
|
|
#include "libslic3r/Point.hpp"
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/catch_message.hpp>
|
|
#include <catch2/generators/catch_generators.hpp>
|
|
#include <catch2/generators/catch_generators_range.hpp>
|
|
#include <catch2/matchers/catch_matchers.hpp>
|
|
#include <catch2/matchers/catch_matchers_floating_point.hpp>
|
|
#include "libslic3r/MeshRepair.hpp"
|
|
#include "libslic3r/Model.hpp"
|
|
#include "libslic3r/ObjectID.hpp"
|
|
#include "libslic3r/TriangleSelector.hpp"
|
|
#include "libslic3r/Geometry.hpp"
|
|
#include "libslic3r/libslic3r.h"
|
|
|
|
using namespace Slic3r;
|
|
|
|
// convex_hull_2d does not clip geometry below the bed, so these cases avoid
|
|
// sinking transforms.
|
|
TEST_CASE("A part's 2D convex hull is its footprint projected onto the bed", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject* object = model.add_object();
|
|
// Keep the cube's raw coordinates ([0,20] on every axis): the default
|
|
// add_volume re-centers the geometry, which would move the footprint.
|
|
object->add_volume(make_cube(20, 20, 20), ModelVolumeType::MODEL_PART, false);
|
|
|
|
SECTION("identity transform yields the 20 mm square") {
|
|
const Polygon hull = object->convex_hull_2d(Geometry::Transformation{}.get_matrix());
|
|
const BoundingBox bb = hull.bounding_box();
|
|
CHECK(hull.size() == 4);
|
|
CHECK(bb.min.x() == scaled(0.));
|
|
CHECK(bb.min.y() == scaled(0.));
|
|
CHECK(bb.max.x() == scaled(20.));
|
|
CHECK(bb.max.y() == scaled(20.));
|
|
}
|
|
|
|
SECTION("scaling and offset move and grow the footprint") {
|
|
Geometry::Transformation t;
|
|
t.set_scaling_factor({2, 2, 2}); // cube now spans [0,40]
|
|
t.set_offset({10, 5, 0}); // then shift +10 in X, +5 in Y
|
|
|
|
const Polygon hull = object->convex_hull_2d(t.get_matrix());
|
|
const BoundingBox bb = hull.bounding_box();
|
|
CHECK(hull.size() == 4);
|
|
CHECK(bb.min.x() == scaled(10.));
|
|
CHECK(bb.min.y() == scaled(5.));
|
|
CHECK(bb.max.x() == scaled(50.));
|
|
CHECK(bb.max.y() == scaled(45.));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("An object's raw mesh keeps the triangles of each part on its own vertices", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
object->add_volume(make_cube(10, 10, 10), ModelVolumeType::MODEL_PART, false);
|
|
TriangleMesh second = make_cube(10, 10, 10);
|
|
second.translate(30, 0, 0);
|
|
object->add_volume(std::move(second), ModelVolumeType::MODEL_PART, false);
|
|
|
|
// Two separate cubes stay two closed components, one around each cube.
|
|
const std::vector<indexed_triangle_set> parts = its_split(object->raw_indexed_triangle_set());
|
|
REQUIRE(parts.size() == 2);
|
|
std::vector<double> min_x;
|
|
for (const indexed_triangle_set &part : parts) {
|
|
CHECK(part.indices.size() == 12);
|
|
min_x.push_back(bounding_box(part).min.x());
|
|
}
|
|
std::sort(min_x.begin(), min_x.end());
|
|
CHECK_THAT(min_x.front(), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
CHECK_THAT(min_x.back(), Catch::Matchers::WithinAbs(30., 1e-4));
|
|
}
|
|
|
|
TEST_CASE("An instance added from another's scale, rotation and mirror matches it", "[Model]")
|
|
{
|
|
// Add instance and Fill bed copy an instance this way.
|
|
const int case_idx = GENERATE(0, 1);
|
|
Transform3d trafo = Transform3d::Identity();
|
|
if (case_idx == 0)
|
|
trafo.linear() = Eigen::AngleAxisd(0.5 * PI, Vec3d::UnitZ()).toRotationMatrix() * Vec3d(-1., 1., 1.).asDiagonal();
|
|
else
|
|
trafo.linear() << 4.4408921e-16, 0.819152044, 0.573576436,
|
|
1.0, -4.4408921e-16, -1.11022302e-16,
|
|
-5.55111512e-17, -0.573576436, 0.819152044;
|
|
CAPTURE(case_idx);
|
|
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
object->add_volume(make_cube(10, 20, 30));
|
|
ModelInstance *original = object->add_instance();
|
|
original->set_transformation(Geometry::Transformation(trafo));
|
|
const ModelInstance *copy = object->add_instance(original->get_offset(), original->get_scaling_factor(),
|
|
original->get_rotation(), original->get_mirror());
|
|
CHECK(copy->get_matrix().linear().isApprox(original->get_matrix().linear(), 1e-9));
|
|
}
|
|
|
|
namespace {
|
|
|
|
// A flat triangle first, then a cube for each offset, all in one mesh.
|
|
TriangleMesh flat_triangle_then_cubes(const std::vector<double> &cube_offsets)
|
|
{
|
|
indexed_triangle_set its;
|
|
its.vertices = {{-20.f, 0.f, 0.f}, {-10.f, 0.f, 0.f}, {-15.f, 5.f, 0.f}};
|
|
its.indices = {{0, 1, 2}};
|
|
for (double offset : cube_offsets) {
|
|
TriangleMesh cube = make_cube(10, 10, 10);
|
|
cube.translate(float(offset), 0.f, 0.f);
|
|
its_merge(its, cube.its);
|
|
}
|
|
return TriangleMesh(its);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("Splitting a scaled object keeps each part in place in the assembly view", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
ModelInstance *instance = object->add_instance();
|
|
instance->set_scaling_factor(Vec3d::Constant(2.));
|
|
Geometry::Transformation assembly = instance->get_transformation();
|
|
assembly.set_rotation(Z, 0.5 * PI);
|
|
assembly.set_offset(Vec3d(40., 30., 10.));
|
|
instance->set_assemble_transformation(assembly);
|
|
|
|
std::vector<Transform3d> expected;
|
|
for (double x : {0., 20.}) {
|
|
ModelVolume *volume = object->add_volume(make_cube(10., 10., 10.));
|
|
volume->set_offset(Vec3d(x, 0., 0.));
|
|
expected.push_back(assembly.get_matrix() * volume->get_matrix());
|
|
}
|
|
|
|
ModelObjectPtrs parts;
|
|
object->split(&parts, false);
|
|
REQUIRE(parts.size() == expected.size());
|
|
for (size_t i = 0; i < parts.size(); ++i) {
|
|
const ModelObject &part = *parts[i];
|
|
const Transform3d actual = part.instances.front()->get_assemble_transformation().get_matrix() * part.volumes.front()->get_matrix();
|
|
CHECK(actual.isApprox(expected[i]));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Splitting a part drops pieces enclosing no volume and keeps the part itself", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
ModelVolume *volume = object->add_volume(flat_triangle_then_cubes({0., 30.}), ModelVolumeType::MODEL_PART, false);
|
|
REQUIRE(volume->is_splittable());
|
|
|
|
CHECK(volume->split(1, false) == 2);
|
|
REQUIRE(object->volumes.size() == 2);
|
|
CHECK(std::find(object->volumes.begin(), object->volumes.end(), volume) != object->volumes.end());
|
|
for (const ModelVolume *part : object->volumes) {
|
|
CHECK(part->mesh().facets_count() == 12);
|
|
CHECK_FALSE(part->is_splittable());
|
|
}
|
|
}
|
|
|
|
TEST_CASE("A part asked from several threads at once whether it can be split answers the same for all", "[Model]")
|
|
{
|
|
// The PrintObjects made from one ModelObject prepare their infill at the same time, and each asks the object's first
|
|
// part whether it can be split, which fills a cache in the part. Without the lock around the cache the answers
|
|
// still agree on most runs: ThreadSanitizer is what reports the race.
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
const ModelVolume *volume = object->add_volume(flat_triangle_then_cubes({0., 30.}), ModelVolumeType::MODEL_PART, false);
|
|
|
|
std::vector<char> answers(8, 0);
|
|
std::vector<std::thread> threads;
|
|
for (size_t i = 0; i < answers.size(); ++ i)
|
|
threads.emplace_back([volume, &answers, i] { answers[i] = volume->is_splittable() ? 1 : 0; });
|
|
for (std::thread &thread : threads)
|
|
thread.join();
|
|
|
|
CHECK(std::all_of(answers.begin(), answers.end(), [](char answer) { return answer == 1; }));
|
|
}
|
|
|
|
TEST_CASE("Splitting a part with a single solid piece keeps just that piece", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
ModelVolume *volume = object->add_volume(flat_triangle_then_cubes({0.}), ModelVolumeType::MODEL_PART, false);
|
|
|
|
CHECK(volume->split(1, false) == 1);
|
|
REQUIRE(object->volumes.size() == 1);
|
|
CHECK(object->volumes.front() == volume);
|
|
CHECK(volume->mesh().facets_count() == 12);
|
|
}
|
|
|
|
TEST_CASE("Splitting a part made only of pieces enclosing no volume leaves it whole", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
indexed_triangle_set its;
|
|
its.vertices = {{0.f, 0.f, 0.f}, {10.f, 0.f, 0.f}, {5.f, 5.f, 0.f}, {20.f, 0.f, 0.f}, {30.f, 0.f, 0.f}, {25.f, 5.f, 0.f}};
|
|
its.indices = {{0, 1, 2}, {3, 4, 5}};
|
|
ModelVolume *volume = object->add_volume(TriangleMesh(its), ModelVolumeType::MODEL_PART, false);
|
|
|
|
CHECK(volume->split(1, false) == 1);
|
|
REQUIRE(object->volumes.size() == 1);
|
|
CHECK(object->volumes.front() == volume);
|
|
CHECK(volume->mesh().facets_count() == 2);
|
|
}
|
|
|
|
TEST_CASE("Splitting an object gives each negative part and modifier to the pieces it overlaps", "[Model]")
|
|
{
|
|
// Two cubes 30 mm apart, in one part or in two.
|
|
const bool two_parts = GENERATE(false, true);
|
|
CAPTURE(two_parts);
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
TriangleMesh left = make_cube(10, 10, 10);
|
|
TriangleMesh right = make_cube(10, 10, 10);
|
|
right.translate(30.f, 0.f, 0.f);
|
|
if (two_parts) {
|
|
object->add_volume(std::move(left), ModelVolumeType::MODEL_PART, false);
|
|
object->add_volume(std::move(right), ModelVolumeType::MODEL_PART, false);
|
|
} else {
|
|
left.merge(right);
|
|
object->add_volume(std::move(left), ModelVolumeType::MODEL_PART, false);
|
|
}
|
|
TriangleMesh hole = make_cube(2, 2, 12);
|
|
hole.translate(4.f, 4.f, -1.f);
|
|
const ModelVolume *negative = object->add_volume(std::move(hole), ModelVolumeType::NEGATIVE_VOLUME, false);
|
|
TriangleMesh block = make_cube(4, 4, 4);
|
|
block.translate(33.f, 3.f, 3.f);
|
|
const ModelVolume *modifier = object->add_volume(std::move(block), ModelVolumeType::PARAMETER_MODIFIER, false);
|
|
TriangleMesh bar = make_cube(30, 2, 2);
|
|
bar.translate(5.f, 4.f, 4.f);
|
|
const ModelVolume *spanning = object->add_volume(std::move(bar), ModelVolumeType::PARAMETER_MODIFIER, false);
|
|
object->add_instance()->set_offset(Vec3d(100., 50., 0.));
|
|
|
|
auto world_bbox = [](const ModelObject &object, const ModelVolume &volume) {
|
|
return volume.mesh().bounding_box().transformed(object.instances.front()->get_matrix() * volume.get_matrix());
|
|
};
|
|
const BoundingBoxf3 negative_bbox = world_bbox(*object, *negative);
|
|
const BoundingBoxf3 modifier_bbox = world_bbox(*object, *modifier);
|
|
const BoundingBoxf3 spanning_bbox = world_bbox(*object, *spanning);
|
|
|
|
ModelObjectPtrs new_objects;
|
|
object->split(&new_objects, false);
|
|
REQUIRE(new_objects.size() == 2);
|
|
for (const ModelObject *piece : new_objects) {
|
|
REQUIRE(piece->volumes.size() == 3);
|
|
const bool is_left = world_bbox(*piece, *piece->volumes.front()).min.x() < 120.;
|
|
CAPTURE(is_left);
|
|
// Each stays where it was, and the bar across both pieces goes to both.
|
|
const ModelVolume &own = *piece->volumes[1];
|
|
const BoundingBoxf3 expected = is_left ? negative_bbox : modifier_bbox;
|
|
CHECK(own.type() == (is_left ? ModelVolumeType::NEGATIVE_VOLUME : ModelVolumeType::PARAMETER_MODIFIER));
|
|
CHECK((world_bbox(*piece, own).min - expected.min).norm() < 1e-4);
|
|
CHECK((world_bbox(*piece, own).max - expected.max).norm() < 1e-4);
|
|
const ModelVolume &bar_copy = *piece->volumes[2];
|
|
CHECK(bar_copy.type() == ModelVolumeType::PARAMETER_MODIFIER);
|
|
CHECK((world_bbox(*piece, bar_copy).min - spanning_bbox.min).norm() < 1e-4);
|
|
CHECK((world_bbox(*piece, bar_copy).max - spanning_bbox.max).norm() < 1e-4);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Splitting an object gives a painted modifier to the pieces of its part that hold its paint", "[Model]")
|
|
{
|
|
// Two cubes 30 mm apart, in one part or in two, with paint on the right cube only.
|
|
const bool two_parts = GENERATE(false, true);
|
|
CAPTURE(two_parts);
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
TriangleMesh left = make_cube(10, 10, 10);
|
|
TriangleMesh right = make_cube(10, 10, 10);
|
|
right.translate(30.f, 0.f, 0.f);
|
|
const ModelVolume *host = nullptr;
|
|
if (two_parts) {
|
|
object->add_volume(std::move(left), ModelVolumeType::MODEL_PART, false);
|
|
host = object->add_volume(std::move(right), ModelVolumeType::MODEL_PART, false);
|
|
} else {
|
|
left.merge(right);
|
|
host = object->add_volume(std::move(left), ModelVolumeType::MODEL_PART, false);
|
|
}
|
|
ModelVolume *painted_modifier = object->add_painted_modifier(*host);
|
|
const indexed_triangle_set &its = painted_modifier->mesh().its;
|
|
TriangleSelector selector(painted_modifier->mesh());
|
|
for (int facet_idx = 0; facet_idx < int(its.indices.size()); ++facet_idx)
|
|
if (its.vertices[its.indices[facet_idx][0]].x() > 20.f)
|
|
selector.set_facet(facet_idx, EnforcerBlockerType::ENFORCER);
|
|
painted_modifier->painted_modifier_facets.set(selector);
|
|
object->add_instance()->set_offset(Vec3d(100., 50., 0.));
|
|
|
|
ModelObjectPtrs new_objects;
|
|
object->split(&new_objects, false);
|
|
REQUIRE(new_objects.size() == 2);
|
|
for (const ModelObject *piece : new_objects) {
|
|
const BoundingBoxf3 part_bbox = piece->volumes.front()->mesh().bounding_box().transformed(
|
|
piece->instances.front()->get_matrix() * piece->volumes.front()->get_matrix());
|
|
const bool is_left = part_bbox.min.x() < 120.;
|
|
CAPTURE(is_left);
|
|
if (is_left) {
|
|
CHECK(piece->volumes.size() == 1);
|
|
continue;
|
|
}
|
|
REQUIRE(piece->volumes.size() == 2);
|
|
const ModelVolume © = *piece->volumes.back();
|
|
REQUIRE(copy.is_painted_modifier());
|
|
CHECK(piece->painted_modifier_host(copy) == piece->volumes.front());
|
|
CHECK(copy.mesh_ptr() == piece->volumes.front()->mesh_ptr());
|
|
// Every face of the right cube stays painted.
|
|
CHECK(copy.painted_modifier_facets.get_facets_strict(copy, EnforcerBlockerType::ENFORCER).indices.size() == 12);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Replacing a part's mesh updates whether it can be split", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
TriangleMesh two_cubes = make_cube(10, 10, 10);
|
|
TriangleMesh second = make_cube(10, 10, 10);
|
|
second.translate(30.f, 0.f, 0.f);
|
|
two_cubes.merge(second);
|
|
ModelVolume *volume = object->add_volume(std::move(two_cubes), ModelVolumeType::MODEL_PART, false);
|
|
REQUIRE(volume->is_splittable());
|
|
|
|
volume->set_mesh(make_cube(10, 10, 10));
|
|
CHECK_FALSE(volume->is_splittable());
|
|
}
|
|
|
|
TEST_CASE("Repairing a part replaces its mesh only when the repair changes it", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
TriangleMesh flipped = make_cube(10, 10, 10);
|
|
std::swap(flipped.its.indices.front()[1], flipped.its.indices.front()[2]);
|
|
ModelVolume *broken = object->add_volume(std::move(flipped), ModelVolumeType::MODEL_PART, false);
|
|
TriangleMesh cube = make_cube(10, 10, 10);
|
|
cube.translate(30.f, 0.f, 0.f);
|
|
ModelVolume *valid = object->add_volume(std::move(cube), ModelVolumeType::MODEL_PART, false);
|
|
const ObjectID broken_id = broken->id();
|
|
const ObjectID valid_id = valid->id();
|
|
|
|
MeshRepairStats stats;
|
|
REQUIRE(broken->repair(false, &stats));
|
|
CHECK(stats.modified);
|
|
CHECK(broken->id() != broken_id);
|
|
CHECK_THAT(its_volume(broken->mesh().its), Catch::Matchers::WithinRel(1000., 1e-6));
|
|
CHECK_FALSE(broken->get_convex_hull().empty());
|
|
|
|
REQUIRE(valid->repair(false, &stats));
|
|
CHECK_FALSE(stats.modified);
|
|
CHECK(valid->id() == valid_id);
|
|
}
|
|
|
|
// A cube whose raw mesh spans [0, size] on every axis, with one instance at plate_offset on the plate.
|
|
static ModelObject *add_cube(Model &model, double size, const Vec3d &plate_offset)
|
|
{
|
|
ModelObject *object = model.add_object();
|
|
object->add_volume(make_cube(size, size, size), ModelVolumeType::MODEL_PART, false);
|
|
object->add_instance()->set_offset(plate_offset);
|
|
return object;
|
|
}
|
|
|
|
TEST_CASE("An object placed in the assembly view goes 10 mm to the right of the others", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *placed = add_cube(model, 20., Vec3d::Zero());
|
|
placed->instances.front()->set_assemble_transformation(placed->instances.front()->get_transformation());
|
|
const Vec3d plate_offset(128., 128., 5.);
|
|
ModelObject *added = add_cube(model, 10., plate_offset);
|
|
|
|
model.set_assembly_pos(added);
|
|
|
|
const BoundingBoxf3 placed_box = placed->bounding_box_in_assembly_view();
|
|
const BoundingBoxf3 added_box = added->bounding_box_in_assembly_view();
|
|
CHECK(added->instances.front()->is_assemble_initialized());
|
|
CHECK_THAT(added_box.min.x(), Catch::Matchers::WithinAbs(placed_box.max.x() + 10., 1e-6));
|
|
CHECK_THAT(added_box.center().y(), Catch::Matchers::WithinAbs(placed_box.center().y(), 1e-6));
|
|
// The height comes from the plate, and the plate itself is left alone.
|
|
CHECK_THAT(added_box.min.z(), Catch::Matchers::WithinAbs(plate_offset.z(), 1e-6));
|
|
CHECK_THAT((added->instances.front()->get_offset() - plate_offset).norm(), Catch::Matchers::WithinAbs(0., 1e-6));
|
|
}
|
|
|
|
TEST_CASE("An object alone in the assembly view is centred on the origin and keeps its instance layout", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = add_cube(model, 10., {100., 100., 0.});
|
|
object->add_instance()->set_offset({130., 100., 0.});
|
|
|
|
model.set_assembly_pos(object);
|
|
|
|
const BoundingBoxf3 box = object->bounding_box_in_assembly_view();
|
|
CHECK_THAT(box.center().x(), Catch::Matchers::WithinAbs(0., 1e-6));
|
|
CHECK_THAT(box.center().y(), Catch::Matchers::WithinAbs(0., 1e-6));
|
|
const Vec3d spacing = object->instances[1]->get_assemble_offset() - object->instances[0]->get_assemble_offset();
|
|
CHECK_THAT((spacing - Vec3d(30., 0., 0.)).norm(), Catch::Matchers::WithinAbs(0., 1e-6));
|
|
}
|
|
|
|
TEST_CASE("Ignored objects do not push an object aside in the assembly view", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *ignored = add_cube(model, 20., Vec3d::Zero());
|
|
ignored->instances.front()->set_assemble_transformation(ignored->instances.front()->get_transformation());
|
|
ModelObject *object = add_cube(model, 10., {128., 128., 0.});
|
|
|
|
model.set_assembly_pos(object, {ignored});
|
|
|
|
const BoundingBoxf3 box = object->bounding_box_in_assembly_view();
|
|
CHECK_THAT(box.center().x(), Catch::Matchers::WithinAbs(0., 1e-6));
|
|
CHECK_THAT(box.center().y(), Catch::Matchers::WithinAbs(0., 1e-6));
|
|
}
|
|
namespace {
|
|
|
|
// A box of the given size whose lowest corner is at `corner` in the file's coordinates. The mesh keeps them, unless
|
|
// `as_reader` centers it and keeps the same place in the volume's offset, as the file readers do.
|
|
ModelVolume *add_box(ModelObject *object, const Vec3d &size, const Vec3d &corner, bool as_reader = false)
|
|
{
|
|
TriangleMesh box = make_cube(size.x(), size.y(), size.z());
|
|
box.translate(corner.cast<float>());
|
|
return object->add_volume(std::move(box), ModelVolumeType::MODEL_PART, as_reader);
|
|
}
|
|
|
|
// The vertices of every volume (or only of the model parts) of every instance of every object, in world coordinates,
|
|
// in a stable order.
|
|
std::vector<Vec3d> world_vertices(const Model &model, bool parts_only = false)
|
|
{
|
|
std::vector<Vec3d> points;
|
|
for (const ModelObject *object : model.objects)
|
|
for (const ModelInstance *instance : object->instances)
|
|
for (const ModelVolume *volume : object->volumes)
|
|
if (!parts_only || volume->is_model_part())
|
|
for (const Vec3f &p : volume->mesh().its.vertices)
|
|
points.push_back(instance->get_matrix() * volume->get_matrix() * p.cast<double>());
|
|
return points;
|
|
}
|
|
|
|
// The box around the given points.
|
|
BoundingBoxf3 points_box(const std::vector<Vec3d> &points)
|
|
{
|
|
BoundingBoxf3 bb;
|
|
for (const Vec3d &p : points)
|
|
bb.merge(p);
|
|
return bb;
|
|
}
|
|
|
|
// The larger distance between the matching corners of two boxes.
|
|
double box_distance(const BoundingBoxf3 &a, const BoundingBoxf3 &b) { return std::max((a.min - b.min).norm(), (a.max - b.max).norm()); }
|
|
|
|
// Largest distance between two lists of points of equal length.
|
|
double max_distance(const std::vector<Vec3d> &a, const std::vector<Vec3d> &b)
|
|
{
|
|
REQUIRE(a.size() == b.size());
|
|
double distance = 0.;
|
|
for (size_t i = 0; i < a.size(); ++i)
|
|
distance = std::max(distance, (a[i] - b[i]).norm());
|
|
return distance;
|
|
}
|
|
|
|
// The points turned by `angle` around the vertical axis through `center`.
|
|
std::vector<Vec3d> turned(const std::vector<Vec3d> &points, double angle, const Vec2d ¢er)
|
|
{
|
|
const Vec3d center_3d(center.x(), center.y(), 0.);
|
|
const Transform3d around = Geometry::translation_transform(center_3d) * Geometry::rotation_transform(angle * Vec3d::UnitZ()) *
|
|
Geometry::translation_transform(-center_3d);
|
|
std::vector<Vec3d> out;
|
|
for (const Vec3d &p : points)
|
|
out.push_back(around * p);
|
|
return out;
|
|
}
|
|
|
|
// The XY center of the box around the given points.
|
|
Vec2d layout_center(const std::vector<Vec3d> &points)
|
|
{
|
|
const BoundingBoxf3 bb = points_box(points);
|
|
return {bb.center().x(), bb.center().y()};
|
|
}
|
|
|
|
// The linear parts (rotation, scale, mirror) of the volumes and of the instances of an object.
|
|
std::vector<Matrix3d> linear_parts(const ModelObject &object)
|
|
{
|
|
std::vector<Matrix3d> parts;
|
|
for (const ModelVolume *volume : object.volumes)
|
|
parts.push_back(volume->get_matrix().linear());
|
|
for (const ModelInstance *instance : object.instances)
|
|
parts.push_back(instance->get_matrix().linear());
|
|
return parts;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("Centering a placed object keeps it where it stands", "[Model]")
|
|
{
|
|
const bool as_reader = GENERATE(false, true);
|
|
CAPTURE(as_reader);
|
|
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
// A part far from its object's origin, placed by its instance, as an AMF constellation leaves it, with a
|
|
// modifier reaching beyond it.
|
|
add_box(object, {20., 10., 5.}, {100., 50., 0.}, as_reader);
|
|
TriangleMesh modifier = make_cube(40., 10., 10.);
|
|
modifier.translate(90.f, 50.f, 0.f);
|
|
object->add_volume(std::move(modifier), ModelVolumeType::PARAMETER_MODIFIER, false);
|
|
object->add_instance()->set_offset({5., 7., 0.});
|
|
const std::vector<Vec3d> before = world_vertices(model);
|
|
|
|
model.rotate_and_center_objects(0.);
|
|
|
|
CHECK_THAT(max_distance(world_vertices(model), before), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
CHECK_THAT(object->full_raw_mesh_bounding_box().center().norm(), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
// The instance compensates the centering, so there is nothing for merging or export to undo.
|
|
CHECK_THAT(object->origin_translation.norm(), Catch::Matchers::WithinAbs(0., 1e-9));
|
|
}
|
|
|
|
TEST_CASE("Turning placed objects of different heights keeps them on the bed", "[Model]")
|
|
{
|
|
const double angle = GENERATE(0., 0.25 * PI);
|
|
const double raised = GENERATE(0., 1.);
|
|
CAPTURE(angle, raised);
|
|
|
|
Model model;
|
|
ModelObject *low = model.add_object();
|
|
ModelObject *flat = model.add_object();
|
|
add_box(low, {20., 10., 5.}, {0., 0., 0.});
|
|
add_box(flat, {30., 30., 2.}, {0., 0., raised});
|
|
low->add_instance()->set_offset({110., 110., 0.});
|
|
flat->add_instance()->set_offset({150., 125., 0.});
|
|
// Fill the bounding box caches first, so a stale cache would show.
|
|
REQUIRE_THAT(low->min_z(), Catch::Matchers::WithinAbs(0., 1e-6));
|
|
REQUIRE_THAT(flat->min_z(), Catch::Matchers::WithinAbs(raised, 1e-6));
|
|
|
|
model.rotate_and_center_objects(angle);
|
|
|
|
CHECK_THAT(low->min_z(), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
CHECK_THAT(flat->min_z(), Catch::Matchers::WithinAbs(raised, 1e-4));
|
|
CHECK(model.looks_like_multipart_object() == (raised != 0.));
|
|
}
|
|
|
|
TEST_CASE("Turning placed objects turns their whole layout around its center", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *bracket = model.add_object();
|
|
ModelObject *plate = model.add_object();
|
|
add_box(bracket, {20., 10., 5.}, {0., 0., 0.});
|
|
// A part at an offset inside its object turns with it.
|
|
add_box(bracket, {4., 4., 8.}, {25., 0., 0.});
|
|
add_box(plate, {30., 30., 2.}, {40., 40., 0.});
|
|
bracket->add_instance()->set_offset({110., 110., 0.});
|
|
bracket->add_instance()->set_offset({110., 140., 0.});
|
|
// A turned and scaled copy still takes the rotation in its volumes.
|
|
ModelInstance *plate_instance = plate->add_instance();
|
|
plate_instance->set_offset({150., 125., 0.});
|
|
plate_instance->set_rotation({0., 0., 0.3});
|
|
plate_instance->set_scaling_factor({1.5, 1.5, 1.5});
|
|
// A modifier reaching far to one side turns with its object but does not move the layout's center.
|
|
TriangleMesh modifier = make_cube(60., 5., 5.);
|
|
modifier.translate(200.f, 0.f, 0.f);
|
|
bracket->add_volume(std::move(modifier), ModelVolumeType::PARAMETER_MODIFIER, false);
|
|
// An object with nothing in it takes no part in the layout.
|
|
model.add_object()->add_instance();
|
|
// Fill the bounding box caches first, so a stale cache would show.
|
|
for (const ModelObject *object : model.objects) {
|
|
object->bounding_box_exact();
|
|
object->raw_mesh_bounding_box();
|
|
}
|
|
|
|
const double angle = 0.5 * PI;
|
|
const std::vector<Vec3d> before = world_vertices(model);
|
|
const Vec2d center = layout_center(world_vertices(model, true));
|
|
const std::vector<Matrix3d> plate_before = linear_parts(*plate);
|
|
// The modifier must reach outside the parts, or counting it would not move the center.
|
|
REQUIRE((layout_center(before) - center).norm() > 1.);
|
|
|
|
model.rotate_and_center_objects(angle);
|
|
|
|
CHECK_THAT(max_distance(world_vertices(model), turned(before, angle, center)), Catch::Matchers::WithinAbs(0., 1e-3));
|
|
// Instances that commute with the turn keep their own rotation and scale; the volumes take the turn.
|
|
const Matrix3d turn = Geometry::rotation_transform(angle * Vec3d::UnitZ()).linear();
|
|
const std::vector<Matrix3d> plate_after = linear_parts(*plate);
|
|
CHECK(plate_after[0].isApprox(turn * plate_before[0], 1e-9));
|
|
CHECK(plate_after[1].isApprox(plate_before[1], 1e-9));
|
|
// The caches follow the change.
|
|
for (const ModelObject *object : model.objects) {
|
|
if (object->volumes.empty())
|
|
continue;
|
|
BoundingBoxf3 raw;
|
|
for (const ModelVolume *volume : object->volumes)
|
|
if (volume->is_model_part())
|
|
raw.merge(volume->mesh().transformed_bounding_box(volume->get_matrix()));
|
|
CHECK_THAT(box_distance(object->raw_mesh_bounding_box(), raw), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
BoundingBoxf3 exact;
|
|
for (size_t i = 0; i < object->instances.size(); ++i)
|
|
exact.merge(object->instance_bounding_box(i));
|
|
CHECK_THAT(box_distance(object->bounding_box_exact(), exact), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Turning keeps mirrored, tilted and skewed instances in the turned layout", "[Model]")
|
|
{
|
|
const int case_idx = GENERATE(0, 1, 2, 3, 4);
|
|
CAPTURE(case_idx);
|
|
Transform3d linear = Transform3d::Identity();
|
|
switch (case_idx) {
|
|
case 0: linear.linear() = Vec3d(-1., 1., 1.).asDiagonal(); break; // mirror X
|
|
case 1: linear.linear() = Vec3d(-1., -1., 1.).asDiagonal(); break; // both horizontal mirrors
|
|
case 2: linear.linear() = Eigen::AngleAxisd(0.5, Vec3d::UnitX()).toRotationMatrix(); break; // tilt about X
|
|
case 3: linear.linear() << 1., 0.3, 0., 0., 1., 0., 0., 0., 1.; break; // skew in XY
|
|
default: linear.linear() = Vec3d(2., 1., 1.).asDiagonal(); break; // uneven XY scale
|
|
}
|
|
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
add_box(object, {20., 10., 5.}, {100., 0., 0.});
|
|
add_box(object, {4., 4., 8.}, {125., 0., 0.});
|
|
// One plain copy and one transformed copy of the same object: both must stay right.
|
|
object->add_instance()->set_offset({60., 60., 0.});
|
|
ModelInstance *transformed = object->add_instance();
|
|
transformed->set_transformation(Geometry::Transformation(Geometry::translation_transform({160., 90., 0.}) * linear));
|
|
|
|
const double angle = -0.25 * PI;
|
|
const std::vector<Vec3d> before = world_vertices(model);
|
|
const Vec2d center = layout_center(before);
|
|
const std::vector<Matrix3d> linear_before = linear_parts(*object);
|
|
|
|
model.rotate_and_center_objects(angle);
|
|
|
|
CHECK_THAT(max_distance(world_vertices(model), turned(before, angle, center)), Catch::Matchers::WithinAbs(0., 1e-3));
|
|
// Both horizontal mirrors commute with the turn, so the volumes take it; otherwise every instance of the object
|
|
// takes it and the shared volumes keep theirs.
|
|
const Matrix3d turn = Geometry::rotation_transform(angle * Vec3d::UnitZ()).linear();
|
|
const std::vector<Matrix3d> linear_after = linear_parts(*object);
|
|
const size_t volumes = object->volumes.size();
|
|
for (size_t i = 0; i < linear_after.size(); ++i) {
|
|
CAPTURE(i);
|
|
const bool turned_here = (i < volumes) == (case_idx == 1);
|
|
CHECK(linear_after[i].isApprox(turned_here ? Matrix3d(turn * linear_before[i]) : linear_before[i], 1e-9));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Merging turned objects into one keeps every part where it stands", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *bracket = model.add_object();
|
|
ModelObject *plate = model.add_object();
|
|
add_box(bracket, {20., 10., 5.}, {100., 50., 0.});
|
|
add_box(plate, {30., 30., 2.}, {-40., 10., 3.});
|
|
bracket->add_instance()->set_offset({10., 20., 0.});
|
|
plate->add_instance()->set_offset({60., 20., 0.});
|
|
|
|
model.rotate_and_center_objects(0.25 * PI);
|
|
const std::vector<Vec3d> standing = world_vertices(model);
|
|
model.convert_multipart_object(1);
|
|
|
|
// The merged object has no instance yet: its parts are in world coordinates.
|
|
REQUIRE(model.objects.size() == 1);
|
|
std::vector<Vec3d> merged;
|
|
for (const ModelVolume *volume : model.objects.front()->volumes)
|
|
for (const Vec3f &p : volume->mesh().its.vertices)
|
|
merged.push_back(volume->get_matrix() * p.cast<double>());
|
|
CHECK_THAT(max_distance(merged, standing), Catch::Matchers::WithinAbs(0., 1e-3));
|
|
}
|
|
|
|
TEST_CASE("Unplaced objects turn around the file origin and keep their height", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *bracket = model.add_object();
|
|
ModelObject *plate = model.add_object();
|
|
add_box(bracket, {20., 10., 5.}, {100., 50., 0.});
|
|
add_box(plate, {30., 30., 2.}, {-40., 10., 3.});
|
|
// Unplaced objects lie in the file's coordinates; origin_translation records how far they were centered.
|
|
auto file_points = [&model]() {
|
|
std::vector<Vec3d> points;
|
|
for (const ModelObject *object : model.objects)
|
|
for (const ModelVolume *volume : object->volumes)
|
|
for (const Vec3f &p : volume->mesh().its.vertices)
|
|
points.push_back(volume->get_matrix() * p.cast<double>() - object->origin_translation);
|
|
return points;
|
|
};
|
|
const double angle = 0.5 * PI;
|
|
const std::vector<Vec3d> before = file_points();
|
|
|
|
model.rotate_and_center_objects(angle);
|
|
|
|
CHECK_THAT(max_distance(file_points(), turned(before, angle, Vec2d::Zero())), Catch::Matchers::WithinAbs(0., 1e-3));
|
|
for (const ModelObject *object : model.objects) {
|
|
const BoundingBoxf3 bb = object->full_raw_mesh_bounding_box();
|
|
CHECK_THAT(bb.center().x(), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
CHECK_THAT(bb.center().y(), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
CHECK_THAT(object->origin_translation.z(), Catch::Matchers::WithinAbs(0., 1e-9));
|
|
}
|
|
CHECK_THAT(bracket->full_raw_mesh_bounding_box().min.z(), Catch::Matchers::WithinAbs(0., 1e-4));
|
|
CHECK_THAT(plate->full_raw_mesh_bounding_box().min.z(), Catch::Matchers::WithinAbs(3., 1e-4));
|
|
}
|
|
|
|
TEST_CASE("Unplaced objects scaled to millimetres still merge on a common base", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *tall = model.add_object();
|
|
ModelObject *flat = model.add_object();
|
|
add_box(tall, {2., 1., 0.5}, {0., 0., 0.});
|
|
add_box(flat, {3., 3., 0.2}, {5., 0., 0.});
|
|
|
|
model.rotate_and_center_objects(0.);
|
|
// The rest of the import as it runs for several files loaded as one object.
|
|
model.convert_from_imperial_units(false);
|
|
for (ModelObject *object : model.objects)
|
|
object->center_around_origin(false);
|
|
model.convert_multipart_object(1);
|
|
|
|
REQUIRE(model.objects.size() == 1);
|
|
REQUIRE(model.objects.front()->volumes.size() == 2);
|
|
std::vector<double> bases;
|
|
for (const ModelVolume *volume : model.objects.front()->volumes)
|
|
bases.push_back(volume->mesh().transformed_bounding_box(volume->get_matrix()).min.z());
|
|
CHECK_THAT(bases.front(), Catch::Matchers::WithinAbs(bases.back(), 1e-3));
|
|
}
|
|
|
|
TEST_CASE("Converting a part's units keeps its material", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
ModelVolume *volume = object->add_volume(make_cube(10, 10, 10), ModelVolumeType::MODEL_PART, false);
|
|
volume->set_material_id("mat_a");
|
|
|
|
ModelObjectPtrs converted;
|
|
object->convert_units(converted, ConversionType::CONV_FROM_INCH, {});
|
|
|
|
REQUIRE(converted.size() == 1);
|
|
REQUIRE(converted.front()->volumes.size() == 1);
|
|
CHECK(converted.front()->volumes.front()->material_id() == "mat_a");
|
|
|
|
// convert_units() hands its objects to the caller unowned; give them to a model so they are freed.
|
|
Model owner;
|
|
owner.objects = std::move(converted);
|
|
}
|
|
|
|
TEST_CASE("Converting a part's units keeps an id whose material is missing", "[Model]")
|
|
{
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
ModelVolume *volume = object->add_volume(make_cube(10, 10, 10), ModelVolumeType::MODEL_PART, false);
|
|
volume->set_material_id("mat_a");
|
|
model.delete_material("mat_a");
|
|
|
|
ModelObjectPtrs converted;
|
|
object->convert_units(converted, ConversionType::CONV_FROM_INCH, {});
|
|
|
|
REQUIRE(converted.size() == 1);
|
|
REQUIRE(converted.front()->volumes.size() == 1);
|
|
CHECK(converted.front()->volumes.front()->material_id() == "mat_a");
|
|
|
|
// convert_units() hands its objects to the caller unowned; give them to a model so they are freed.
|
|
Model owner;
|
|
owner.objects = std::move(converted);
|
|
}
|
|
|
|
TEST_CASE("Per-feature filament assignments follow mixed slots at each model scope", "[Model][FilamentMixer]")
|
|
{
|
|
const char *key = GENERATE("outer_wall_filament_id", "inner_wall_filament_id", "sparse_infill_filament_id",
|
|
"internal_solid_filament_id", "top_surface_filament_id", "bottom_surface_filament_id");
|
|
CAPTURE(key);
|
|
Model model;
|
|
ModelObject *object = model.add_object();
|
|
ModelVolume *part = object->add_volume(make_cube(10, 10, 10));
|
|
ModelVolume *painted_modifier = object->add_painted_modifier(*part);
|
|
auto &modifier = object->add_volume(make_cube(5, 5, 5), ModelVolumeType::PARAMETER_MODIFIER)->config;
|
|
auto &range = object->layer_config_ranges[{0., 5.}];
|
|
auto &material = model.add_material("shared")->config;
|
|
auto &inherited = object->layer_config_ranges[{5., 10.}];
|
|
auto &physical = model.add_object()->config;
|
|
physical.set(key, 2);
|
|
inherited.set("extruder", 0);
|
|
|
|
ModelConfig *scopes[] = {&object->config, &part->config, &modifier, &painted_modifier->config, &range, &material};
|
|
for (ModelConfig *config : scopes)
|
|
config->set(key, 3);
|
|
remap_model_filament_configs(model, {{2, 3}, {3, 4}}, true);
|
|
auto check_assignments = [&](int expected) {
|
|
for (const ModelConfig *config : scopes)
|
|
CHECK(config->opt_int(key) == expected);
|
|
CHECK(physical.opt_int(key) == 2);
|
|
CHECK_FALSE(inherited.has(key));
|
|
};
|
|
check_assignments(4);
|
|
remap_model_filament_configs(model, {{2, -1}, {3, 2}, {4, 3}}, false);
|
|
check_assignments(3);
|
|
remap_model_filament_slots(model, {{2, 4}});
|
|
check_assignments(5);
|
|
// Merge the selected slot into slot 4, then remove it without a replacement.
|
|
remap_model_filament_configs(model, {{4, 3}}, false);
|
|
check_assignments(4);
|
|
remap_model_filament_configs(model, {{3, -1}}, false);
|
|
check_assignments(0);
|
|
}
|
|
|
|
TEST_CASE("Imported vertex colors paint every face that touches another filament", "[Model][Regression]")
|
|
{
|
|
// Filament 1 everywhere but one corner of the cube, in filament 2.
|
|
const int corner = GENERATE(range(0, 8));
|
|
Model model;
|
|
model.add_object()->add_volume(make_cube(10., 10., 10.));
|
|
const ModelVolume &volume = *model.objects.front()->volumes.front();
|
|
const indexed_triangle_set &its = volume.mesh().its;
|
|
REQUIRE(its.vertices.size() == 8);
|
|
std::vector<unsigned char> vertex_filaments(its.vertices.size(), 1);
|
|
vertex_filaments[corner] = 2;
|
|
REQUIRE(Model::obj_import_vertex_color_deal(vertex_filaments, 1, &model));
|
|
for (size_t i = 0; i < its.indices.size(); ++i) {
|
|
const auto &face = its.indices[i];
|
|
const bool touches_corner = face[0] == corner || face[1] == corner || face[2] == corner;
|
|
CAPTURE(corner, i);
|
|
CHECK(volume.mmu_segmentation_facets.get_triangle_as_string(int(i)).empty() == ! touches_corner);
|
|
}
|
|
}
|