mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-11 09:51:06 +00:00
* Fix decomposition of mirrored transformations computeRotationScaling() may place the mirror on any axis, giving unstable rotation/scale for mirrored matrices. Decompose them by flipping each local axis and choosing the one needing the least rotation (lowest axis on ties). Merge the extract helpers into a single extract_rotation_scale() and simplify contains_skew() to check that the scale is diagonal. set_scaling_factor() now preserves the mirror sign on the scaled axis. Selection::scale_and_translate() uses Transformation's rotation and scale instead of calling computeRotationScaling() directly, so scaling a mirrored instance adds no skew. Add tests for mirrored decomposition, scaling, and skew detection. * Use Eigen polar decomposition for instance scaling Selection::scale_and_translate now splits the instance matrix with computeRotationScaling instead of get_rotation_matrix(). Adds a Model test checking that an instance created from another's offset, scale, rotation and mirror reproduces its matrix (as Add instance and Fill bed do). * tidy
100 lines
4.1 KiB
C++
100 lines
4.1 KiB
C++
#include <catch2/catch_all.hpp>
|
|
#include <algorithm>
|
|
#include <utility>
|
|
#include <vector>
|
|
#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/matchers/catch_matchers.hpp>
|
|
#include <catch2/matchers/catch_matchers_floating_point.hpp>
|
|
#include "libslic3r/Model.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));
|
|
}
|