Add mirror

- Keep the matricies as the source of truth
- Make sure instance matricies are right handed
- Volume matricies do not have to be right handed
- Add flags to "mark" mirror (MirrorAxes). These 3 flags
  are strictly "adding information" by recording the user
  intention, that is persisted in our 3mfs. This means
  we can tell "xy" mirror from rotation.
- Other places in slic3r do not have to be touched,
  as the matricies still carry the full story
  (other then places that explictly care about handedness - e.g.
   rendering, slicing alreadry handles that)
This commit is contained in:
Martin Šach 2026-11-03 15:59:28 +01:00 • committed by Martin Šach
parent 837c76b443
commit c9e8ec08d0
30 changed files with 551 additions and 28 deletions

1
.gitignore vendored
View file

@ -32,3 +32,4 @@ CMakeUserPresets.json
# Copied from the deps install prefix at configure time
/resources/matdb/
compile_commands.json

View file

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M13 3.00001C13.1719 3.17188 13.1719 3.45313 13 3.62501L3.62499 13C3.45312 13.1719 3.17187 13.1719 2.99999 13C2.82812 12.8281 2.82812 12.5469 2.99999 12.375L12.375 3.00001C12.5469 2.82813 12.8281 2.82813 13 3.00001ZM1.12499 6.12501L6.125 6.12501L6.125 1.125L1.12499 6.12501ZM5.5 0.500003C5.75195 0.24805 6.13281 0.171878 6.46484 0.308597C6.79687 0.445315 7.00976 0.767582 7.01172 1.125V6.12501C7.01172 6.61329 6.61523 7.00978 6.12695 7.00978L1.12694 7.00978C0.767569 7.00978 0.447256 6.79493 0.310537 6.4629C0.173818 6.13087 0.248038 5.75196 0.499991 5.50001L5.5 0.500003ZM14.875 9.87502L9.875 9.87501L9.875 14.875L14.875 9.87502ZM15.6914 9.53517C15.8281 9.8672 15.7539 10.2461 15.5 10.5L10.5 15.5C10.2481 15.752 9.86719 15.8281 9.53516 15.6914C9.20313 15.5547 8.99024 15.2324 8.98828 14.875L8.98828 9.87502C8.98828 9.38673 9.38477 8.99025 9.87305 8.99025L14.8731 8.99025C15.2324 8.99025 15.5527 9.20509 15.6895 9.53712L15.6914 9.53517Z" fill="#FFFFFF"/>
</svg>

After

Width:  |  Height:  |  Size: 1 KiB

View file

@ -0,0 +1,5 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(-26.6665 -5)">
<path d="M29.6665 8.00001C29.4946 8.17188 29.4946 8.45313 29.6665 8.62501L39.0415 18C39.2134 18.1719 39.4946 18.1719 39.6665 18C39.8384 17.8281 39.8384 17.5469 39.6665 17.375L30.2915 8.00001C30.1196 7.82813 29.8384 7.82813 29.6665 8.00001ZM41.5415 11.125L36.5415 11.125L36.5415 6.125L41.5415 11.125ZM37.1665 5.5C36.9146 5.24805 36.5337 5.17188 36.2017 5.3086C35.8696 5.44532 35.6567 5.76758 35.6548 6.125V11.125C35.6548 11.6133 36.0513 12.0098 36.5396 12.0098L41.5396 12.0098C41.8989 12.0098 42.2192 11.7949 42.356 11.4629C42.4927 11.1309 42.4185 10.752 42.1665 10.5L37.1665 5.5ZM27.7915 14.875L32.7915 14.875L32.7915 19.875L27.7915 14.875ZM26.9751 14.5352C26.8384 14.8672 26.9126 15.2461 27.1665 15.5L32.1665 20.5C32.4185 20.752 32.7993 20.8281 33.1313 20.6914C33.4634 20.5547 33.6763 20.2324 33.6782 19.875L33.6782 14.875C33.6782 14.3867 33.2817 13.9902 32.7935 13.9902L27.7934 13.9902C27.4341 13.9902 27.1138 14.2051 26.977 14.5371L26.9751 14.5352Z" fill="#FFFFFF"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,5 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(-26.6665 -5)">
<path d="M34.6665 5C34.9415 5 35.1665 5.225 35.1665 5.5V20.5C35.1665 20.775 34.9415 21 34.6665 21C34.3915 21 34.1665 20.775 34.1665 20.5V5.5C34.1665 5.225 34.3915 5 34.6665 5ZM27.6665 17L31.6665 13L27.6665 9V17ZM26.6665 9C26.6665 8.59687 26.9103 8.23125 27.2853 8.075C27.6603 7.91875 28.0884 8.00625 28.3759 8.29063L32.3759 12.2906C32.7665 12.6813 32.7665 13.3156 32.3759 13.7063L28.3759 17.7063C28.0884 17.9938 27.6603 18.0781 27.2853 17.9219C26.9103 17.7656 26.6665 17.4031 26.6665 17V9ZM41.6665 9L37.6665 13L41.6665 17V9ZM42.0478 8.075C42.4228 8.23125 42.6665 8.59375 42.6665 9V17C42.6665 17.4031 42.4228 17.7688 42.0478 17.925C41.6728 18.0813 41.2446 17.9938 40.9571 17.7094L36.9571 13.7094C36.5665 13.3188 36.5665 12.6844 36.9571 12.2937L40.9571 8.29375C41.2446 8.00625 41.6728 7.92188 42.0478 8.07812V8.075Z" fill="#FFFFFF"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 979 B

View file

@ -150,6 +150,7 @@ if(SLIC3R_BUILD_TESTS)
test/Slic3r/Biz/Algorithms/OptimizersTests.cpp
test/Slic3r/Biz/Algorithms/ZipArchiveTests.cpp
test/Slic3r/Biz/Algorithms/SlaPointsTests.cpp
test/Slic3r/Biz/Algorithms/MirrorAxesTests.cpp
)
target_include_directories(slic3r-biz-algorithms-tests PRIVATE test)

View file

@ -24,6 +24,14 @@ void rotate(Domain::ModelObject& model_object, double angle, const Domain::Vec3d
void mirror(Domain::ModelObject& model_object, Domain::Axis axis);
/**
* Instances are expected to be right-handed; files from other software may carry left-handed
* instance matrices. When every instance of the object is left-handed, the X reflection is moved
* from the instances into the volumes (matrix and mirror flag), which leaves the geometry
* unchanged. Objects with mixed handedness are left alone.
*/
void normalize_instance_handedness(Domain::ModelObject& model_object);
Domain::ModelVolume* add_volume(Domain::ModelObject* model_object, const Domain::TriangleMesh& mesh);
Domain::ModelVolume* add_volume(Domain::ModelObject* model_object, Domain::TriangleMesh&& mesh, Domain::ModelVolumeType type);

View file

@ -83,6 +83,33 @@ void rotate(Domain::ModelObject& model_object, double angle, const Domain::Vec3d
model_object.invalidate_bounding_box();
}
void normalize_instance_handedness(Domain::ModelObject& model_object)
{
const bool all_left_handed = !model_object.instances.empty()
&& std::ranges::all_of(model_object.instances, [](const Domain::ModelInstance* instance) {
return instance->is_left_handed();
});
if (!all_left_handed) {
return;
}
Domain::MirrorAxes flip;
flip.flip(Domain::X);
const Domain::Transform3d reflection{flip.matrix()};
for (Domain::ModelInstance* instance : model_object.instances) {
instance->set_transformation(Domain::Transformation{instance->get_matrix() * reflection});
}
for (Domain::ModelVolume* volume : model_object.volumes) {
Domain::MirrorAxes axes{volume->mirror_axes()};
axes.flip(Domain::X);
volume->set_transformation(Domain::Transformation{reflection * volume->get_matrix()});
volume->set_mirror_axes(axes);
}
model_object.invalidate_bounding_box();
}
void mirror(Domain::ModelObject& model_object, Domain::Axis axis)
{
for (Domain::ModelVolume* v : model_object.volumes) {

View file

@ -0,0 +1,101 @@
#include "Slic3r/Biz/Algorithms/ModelObject.hpp"
#include "Slic3r/Biz/Algorithms/TriangleMesh.hpp"
#include "Slic3r/Domain/Model.hpp"
#include "Slic3r/Domain/ModelVolume.hpp"
#include <catch2/catch_test_macros.hpp>
using namespace Slic3r;
using Domain::MirrorAxes;
using Domain::Transform3d;
using Domain::Vec3d;
static Domain::ModelVolume& cube_volume(Domain::Model& model)
{
Domain::ModelObject& object = *model.add_object();
Biz::Algorithms::ModelObject::add_volume(&object, Biz::Algorithms::TriangleMesh::make_cube(10., 10., 10.));
object.add_instance();
return *object.volumes.front();
}
TEST_CASE("MirrorAxes matrix and parity", "[algorithms][mirror]")
{
MirrorAxes none;
CHECK_FALSE(none.any());
CHECK_FALSE(none.is_odd());
CHECK(none.matrix().matrix().isApprox(Transform3d::Identity().matrix()));
MirrorAxes xz{{true, false, true}};
CHECK(xz.any());
CHECK_FALSE(xz.is_odd());
CHECK(xz.matrix().linear().isApprox(Vec3d{-1., 1., -1.}.asDiagonal().toDenseMatrix()));
xz.flip(Domain::Y);
CHECK(xz.is_odd());
CHECK(xz.matrix().linear().determinant() < 0.);
}
TEST_CASE("Volume mirror axes are validated against the matrix", "[algorithms][mirror]")
{
Domain::Model model;
Domain::ModelVolume& volume = cube_volume(model);
SECTION("flags matching a left-handed matrix are kept")
{
volume.set_transformation(Transform3d(Eigen::Scaling(1., -1., 1.)));
volume.set_mirror_axes(MirrorAxes{{false, true, false}});
CHECK(volume.mirror_axes() == MirrorAxes{{false, true, false}});
CHECK(volume.right_handed_matrix().matrix().isApprox(Transform3d::Identity().matrix()));
}
SECTION("left-handed matrix without flags falls back to X")
{
volume.set_transformation(Transform3d(Eigen::Scaling(1., -1., 1.)));
CHECK(volume.mirror_axes() == MirrorAxes{{true, false, false}});
CHECK(volume.right_handed_matrix().linear().determinant() > 0.);
}
SECTION("right-handed matrix with odd flags reports none")
{
volume.set_mirror_axes(MirrorAxes{{true, false, false}});
CHECK_FALSE(volume.mirror_axes().any());
CHECK(volume.right_handed_matrix().matrix().isApprox(volume.get_matrix().matrix()));
}
SECTION("X then Y mirror is a right-handed matrix with two flags")
{
volume.set_transformation(Transform3d(Eigen::Scaling(-1., -1., 1.)));
volume.set_mirror_axes(MirrorAxes{{true, true, false}});
CHECK(volume.mirror_axes() == MirrorAxes{{true, true, false}});
CHECK(volume.right_handed_matrix().matrix().isApprox(Transform3d::Identity().matrix()));
}
}
TEST_CASE("Left-handed instances are normalized into the volumes", "[algorithms][mirror]")
{
Domain::Model model;
Domain::ModelVolume& volume = cube_volume(model);
Domain::ModelObject& object = *model.objects.front();
Domain::ModelInstance& instance = *object.instances.front();
const Transform3d instance_trafo{
Eigen::Translation3d(10., 20., 0.) * Eigen::AngleAxisd(0.4, Vec3d::UnitZ()) * Eigen::Scaling(1., -1., 1.)
};
const Transform3d volume_trafo{Eigen::Translation3d(1., 2., 3.) * Eigen::AngleAxisd(0.3, Vec3d::UnitX())};
instance.set_transformation(Domain::Transformation{instance_trafo});
volume.set_transformation(Domain::Transformation{volume_trafo});
REQUIRE(instance.is_left_handed());
Biz::Algorithms::ModelObject::normalize_instance_handedness(object);
CHECK_FALSE(instance.is_left_handed());
CHECK(volume.is_left_handed());
CHECK(volume.mirror_axes() == MirrorAxes{{true, false, false}});
CHECK((instance.get_matrix() * volume.get_matrix()).matrix().isApprox((instance_trafo * volume_trafo).matrix()));
SECTION("a right-handed instance is left alone")
{
Biz::Algorithms::ModelObject::normalize_instance_handedness(object);
CHECK(volume.mirror_axes() == MirrorAxes{{true, false, false}});
}
}

View file

@ -15,6 +15,7 @@
#include "Slic3r/Domain/SLA/DrainHole.hpp"
#include "Slic3r/Domain/SLA/SupportPoint.hpp"
#include <array>
#include <optional>
namespace Slic3r::Biz::Algorithms::ModelObject {
@ -51,6 +52,25 @@ enum class ModelVolumeType : int
SUPPORT_ENFORCER,
};
/**
* Which local axes of a volume the user mirrored. UI state only: the transformation matrix stays
* the single geometric truth, these flags make the per-axis indicator, the rotation / scale
* readouts and the revert actions well defined for left-handed matrices. See design/mirror.md.
*/
struct MirrorAxes
{
std::array<bool, 3> axes{false, false, false};
bool operator==(const MirrorAxes&) const = default;
bool is_odd() const { return (axes[0] + axes[1] + axes[2]) % 2 == 1; }
bool any() const { return axes[0] || axes[1] || axes[2]; }
void flip(Axis axis) { axes[axis] = !axes[axis]; }
Transform3d matrix() const;
template<class Archive> void serialize(Archive& ar) { ar(axes); }
};
/**
* An object STL, or a modifier volume, over which a different set of parameters shall be applied.
* ModelVolume instances are owned by a ModelObject.
@ -211,6 +231,13 @@ public:
void set_mirror(const Vec3d& mirror) override;
void set_mirror(Axis axis, double mirror) override;
/// Flags validated against the matrix: when their parity disagrees with the determinant
/// sign, a left-handed matrix reports X mirrored and a right-handed one reports none.
MirrorAxes mirror_axes() const;
void set_mirror_axes(const MirrorAxes& mirror_axes);
/// get_matrix() with the mirror of mirror_axes() removed; always right-handed.
Transform3d right_handed_matrix() const;
const Transform3d& get_matrix() const override;
Transform3d get_matrix_no_offset() const override;
@ -254,6 +281,7 @@ private:
// The convex hull of this model's mesh.
std::shared_ptr<const TriangleMesh> m_convex_hull;
Transformation m_transformation;
MirrorAxes m_mirror_axes;
// flag to optimize the checking if the volume is splittable
// -1 -> is unknown value (before first cheking)
@ -288,7 +316,7 @@ private:
: ObjectBase(other), name(other.name), source(other.source), cut_info(other.cut_info), volume_settings(other.volume_settings), supported_facets(other.supported_facets)
, seam_facets(other.seam_facets), mm_segmentation_facets(other.mm_segmentation_facets), fuzzy_skin_facets(other.fuzzy_skin_facets)
, sla_support_points(other.sla_support_points), sla_drain_holes(other.sla_drain_holes), text_configuration(other.text_configuration), emboss_shape(other.emboss_shape)
, object(object), m_mesh(other.m_mesh), m_type(other.m_type), m_convex_hull(other.m_convex_hull), m_transformation(other.m_transformation)
, object(object), m_mesh(other.m_mesh), m_type(other.m_type), m_convex_hull(other.m_convex_hull), m_transformation(other.m_transformation), m_mirror_axes(other.m_mirror_axes)
{
assert(this->id().valid());
assert(this->supported_facets.id().valid());
@ -310,7 +338,7 @@ private:
// It doesn't initialize m_convex_hull.
ModelVolume(ModelObject* object, const ModelVolume& other, TriangleMesh&& mesh)
: name(other.name), source(other.source), cut_info(other.cut_info), volume_settings(other.volume_settings), text_configuration(other.text_configuration), emboss_shape(other.emboss_shape)
, object(object), m_mesh(new TriangleMesh(std::move(mesh))), m_type(other.m_type), m_transformation(other.m_transformation)
, object(object), m_mesh(new TriangleMesh(std::move(mesh))), m_type(other.m_type), m_transformation(other.m_transformation), m_mirror_axes(other.m_mirror_axes)
{
assert(this->id().valid());
assert(this->supported_facets.id().valid());

View file

@ -142,6 +142,26 @@ void ModelVolume::set_mirror(const Vec3d& mirror) { m_transformation.set_mirror(
void ModelVolume::set_mirror(const Axis axis, double mirror) { m_transformation.set_mirror(axis, mirror); }
Transform3d MirrorAxes::matrix() const
{
return Transform3d(Eigen::Scaling(axes[0] ? -1. : 1., axes[1] ? -1. : 1., axes[2] ? -1. : 1.));
}
MirrorAxes ModelVolume::mirror_axes() const
{
const bool left_handed = m_transformation.is_left_handed();
if (m_mirror_axes.is_odd() == left_handed)
return m_mirror_axes;
return MirrorAxes{{left_handed, false, false}};
}
void ModelVolume::set_mirror_axes(const MirrorAxes& mirror_axes) { m_mirror_axes = mirror_axes; }
Transform3d ModelVolume::right_handed_matrix() const
{
return m_transformation.get_matrix() * mirror_axes().matrix();
}
const Transform3d& ModelVolume::get_matrix() const { return m_transformation.get_matrix(); }
Transform3d ModelVolume::get_matrix_no_offset() const { return m_transformation.get_matrix_no_offset(); }

View file

@ -122,6 +122,11 @@ enum class Icon : wchar_t
ReflectionX,
ReflectionY,
// Scale widget
MirrorX,
MirrorY,
MirrorZ,
// Gizmo Arrange
ArrangeTopLeft,
ArrangeTopRight,

View file

@ -176,6 +176,10 @@ static const std::unordered_map<Icon, const char*> ICON_FILENAMES = {
{Icon::ReflectionX, "reflection_x"},
{Icon::ReflectionY, "reflection_y"},
{Icon::MirrorX, "mirror_x"},
{Icon::MirrorY, "mirror_y"},
{Icon::MirrorZ, "mirror_z"},
{Icon::Layers, "layers"},
{Icon::WallsPerimeters, "walls_and_perimeters"},
{Icon::Infill, "infill"},

View file

@ -58,6 +58,7 @@ private:
TripleInput* m_relative_input{nullptr};
Yoga::Item* m_relative_input_item{nullptr};
Yoga::LayoutButton* m_lock{nullptr};
std::array<Yoga::LayoutButton*, 3> m_mirror_buttons{};
PlaceOnBedButton* m_place_on_bed_button{nullptr};
ReferenceFramePicker* m_reference_frame_picker;
@ -73,6 +74,7 @@ private:
void reload(std::optional<Domain::SelectionId> project_id = std::nullopt);
void apply_relative_scale(const Domain::Vec3d& scale);
void reset_scale();
void mirror(Domain::Axis axis);
Biz::Scene::SceneInteractor::ElementTransforms get_reset_scale_candidates() const;
};
} // namespace Slic3r::App::Plater

View file

@ -164,6 +164,7 @@ enum class Read3mfIssueType: unsigned short {
project_volume_bad_id,
project_volume_unknown_type,
project_volume_config_issue,
project_volume_mirror_issue,
project_source_unknown_property,
project_source_offset_issue,

View file

@ -18,6 +18,7 @@ enum class UndoSnapshotType
Scale,
SetScale,
RevertScale,
Mirror,
PlaceOnFace,
Arrange,
Cut,

View file

@ -553,6 +553,14 @@ public:
using ElementTransforms = std::map<Domain::ElementRef, Domain::SquareMatrix4d>;
void set_element_transforms(const ElementTransforms& transforms);
/**
* @brief Mirror the selection along @p axis of the current reference frame, through the
* selection centre. Volumes take the reflection and the matching mirror flag; a whole
* instance mirrored in the bed frame additionally has its own matrix conjugated by the
* reflection, which keeps it right-handed. See design/mirror.md.
*/
void mirror_selection(Domain::Axis axis);
/**
* @name Transforming selection
* @{

View file

@ -204,9 +204,15 @@ void PlaceOnFaceGizmo::recreate_planes_and_nodes()
Domain::Transform3f trafo = new_object->volumes.front()->get_transformation().get_matrix_no_offset().cast<float>();
Domain::Transform3f trafo_inv = new_object->volumes.front()->get_transformation().get_matrix().inverse().cast<float>();
const bool volume_left_handed = new_object->volumes.front()->get_transformation().is_left_handed();
for (PlaneData& plane : planes) {
for (Domain::Vec3f& pt : plane.its.vertices)
pt = trafo_inv * pt;
if (volume_left_handed) {
for (stl_triangle_vertex_indices& face : plane.its.indices)
std::swap(face[1], face[2]);
}
plane.normal = trafo.inverse().matrix().block(0,0,3,3).inverse().transpose() * plane.normal;
}

View file

@ -195,12 +195,15 @@ Domain::SquareMatrix4d remove_rotation(
}
Domain::SquareMatrix4d remove_rotation(
const Domain::Transform3d& volume_trafo,
const Domain::ModelVolume& volume,
const Domain::Transform3d& instance_trafo,
const Domain::Vec3d& center
)
{
const Domain::SquareMatrix3d inverse_rotation{volume_trafo.rotation().transpose()};
const Domain::Transform3d volume_trafo{volume.get_matrix()};
const Domain::SquareMatrix3d inverse_rotation{
volume.right_handed_matrix().rotation().transpose()
};
const Domain::Vec3d local_center{instance_trafo.inverse() * center};
Domain::SquareMatrix4d local{Domain::SquareMatrix4d::Identity()};
@ -254,7 +257,7 @@ Biz::Scene::SceneInteractor::ElementTransforms RotationDialog::get_reset_rotatio
const Domain::Transform3d volume_matrix{volume->get_matrix()};
const SquareMatrix4d no_rotation{
remove_rotation(volume_matrix, instance_matrix, bounding_box.center)
remove_rotation(*volume, instance_matrix, bounding_box.center)
};
if (!volume_matrix.matrix().isApprox(no_rotation)) {
result.insert_or_assign(ref, no_rotation);
@ -272,7 +275,7 @@ Biz::Scene::SceneInteractor::ElementTransforms RotationDialog::get_reset_rotatio
const Domain::ElementRef volume_ref{element.object_id, 0, volume->id().id};
const Domain::Transform3d volume_matrix{volume->get_matrix()};
const SquareMatrix4d no_rotation{remove_rotation(
volume_matrix,
*volume,
Domain::Transform3d{instance_no_rotation},
bounding_box.center
)};

View file

@ -5,6 +5,8 @@
#include "Slic3r/Biz/I18N/I18N.hpp"
#include "Slic3r/App/Plater/PlaterGizmosHelper.hpp"
#include "Slic3r/Math.hpp"
#include <array>
#include <tuple>
using namespace Slic3r::App::Yoga;
@ -49,7 +51,7 @@ static std::optional<Domain::Vec3d> get_volume_relative_scale(
const double max_angle_diff{Slic3r::deg2rad(0.001)};
if (!same_euler_angels(
(instance.get_matrix() * volume.get_matrix()).rotation(),
(instance.get_matrix() * volume.right_handed_matrix()).rotation(),
obb.rotation,
max_angle_diff
))
@ -57,7 +59,7 @@ static std::optional<Domain::Vec3d> get_volume_relative_scale(
return std::nullopt;
}
return get_relative_scale(volume.get_matrix().matrix().block(0, 0, 3, 3));
return get_relative_scale(volume.right_handed_matrix().matrix().block(0, 0, 3, 3));
}
static std::optional<Domain::Vec3d> get_current_absolute_scale(
@ -284,11 +286,94 @@ ScaleWidget::ScaleWidget(
apply_relative_scale(scale_by);
};
auto mirror_row{scaling_section->emplace_back<Item>()};
mirror_row->set_object_name("MirrorRow");
mirror_row->set_gap(5_fpx);
const std::array<std::tuple<Render::Icon, std::string, std::string>, 3> mirror_icons{
std::tuple{Render::Icon::MirrorX, _u8L("Mirror along X axis"), "MirrorXButton"},
std::tuple{Render::Icon::MirrorY, _u8L("Mirror along Y axis"), "MirrorYButton"},
std::tuple{Render::Icon::MirrorZ, _u8L("Mirror along Z axis"), "MirrorZButton"}
};
for (int i{}; i < 3; ++i) {
const auto& [icon, tooltip, object_name]{mirror_icons[i]};
m_mirror_buttons[i] = mirror_row->emplace_back<Yoga::LayoutButton>("", icon, tooltip);
m_mirror_buttons[i]->set_object_name(object_name);
m_mirror_buttons[i]->set_flex_grow(1);
m_mirror_buttons[i]->set_height(icon_size);
m_mirror_buttons[i]->set_content_padding(3_fpx);
m_mirror_buttons[i]->set_checkable(true);
m_mirror_buttons[i]->set_background_color_checked(Platform::Color::AccentSecondary);
m_mirror_buttons[i]->callbacks().action = [this, i]() {
mirror(static_cast<Domain::Axis>(i));
};
}
auto mirror_row_suffix{mirror_row->emplace_back<Item>()};
mirror_row_suffix->set_width(20_fpx);
m_place_on_bed_button = scaling_section->emplace_back<PlaceOnBedButton>(m_project_interactor);
m_reference_frame_picker = reference_frame_picker;
}
static std::optional<Domain::MirrorAxes> get_selection_mirror_axes(
const Biz::ProjectInteractor& project_interactor
)
{
const Domain::Project& project{
project_interactor.workbench().project(project_interactor.selected_project_id())
};
std::optional<Domain::MirrorAxes> common;
auto merge = [&common](const Domain::ModelVolume& volume) {
const Domain::MirrorAxes axes{volume.mirror_axes()};
if (!common) {
common = axes;
} else if (*common != axes) {
common = Domain::MirrorAxes{};
}
};
for (const Domain::ElementRef& element :
project_interactor.scene_interactor().object_selection().elements)
{
if (element.is_wipe_tower()) {
continue;
}
if (element.has_volume()) {
if (const Domain::ModelVolume* volume{
project.find_volume_by_id(element.object_id, element.volume_id)})
{
merge(*volume);
}
continue;
}
if (const Domain::ModelObject* object{project.find_object_by_id(element.object_id)}) {
for (const Domain::ModelVolume* volume : object->volumes) {
merge(*volume);
}
}
}
return common;
}
void ScaleWidget::mirror(const Domain::Axis axis)
{
const std::optional<Biz::Scene::SelectionExtents> selection_bounding_box{
m_project_interactor.scene_interactor().selection_bounding_box()
};
if (!selection_bounding_box) {
return;
}
const bool was_floating{selection_bounding_box->is_floating()};
m_project_interactor.scene_interactor().mirror_selection(axis);
if (!was_floating) {
Domain::SquareMatrix4d relative_transform_world{Domain::SquareMatrix4d::Identity()};
relative_transform_world.col(3).z() =
-m_project_interactor.scene_interactor().selection_bounding_box()->min_z();
m_project_interactor.scene_interactor().transform_selection(relative_transform_world);
}
m_project_interactor.undo_provider().take_snapshot(Biz::UndoSnapshotType::Mirror);
reload();
}
ScaleWidget::~ScaleWidget()
{
m_project_interactor.scene_interactor()
@ -374,6 +459,13 @@ void ScaleWidget::reload(std::optional<Domain::SelectionId> project_id)
} else {
m_revert_button->set_visible(true);
}
const Domain::MirrorAxes mirror_axes{
get_selection_mirror_axes(m_project_interactor).value_or(Domain::MirrorAxes{})
};
for (int i{}; i < 3; ++i) {
m_mirror_buttons[i]->set_checked(mirror_axes.axes[i]);
}
}
void ScaleWidget::apply_relative_scale(const Domain::Vec3d& scale_by)
@ -422,12 +514,15 @@ void ScaleWidget::apply_relative_scale(const Domain::Vec3d& scale_by)
}
static Domain::SquareMatrix4d remove_scale(
const Domain::Transform3d& volume_trafo,
const Domain::ModelVolume& volume,
const Domain::Transform3d& instance_trafo,
const Domain::Vec3d& center
)
{
const Domain::SquareMatrix3d rotation{volume_trafo.rotation()};
const Domain::Transform3d volume_trafo{volume.get_matrix()};
const Domain::SquareMatrix3d rotation{
(volume.right_handed_matrix().rotation() * volume.mirror_axes().matrix()).linear()
};
const Domain::Vec3d center_in_volume_coords{
volume_trafo.inverse() * instance_trafo.inverse() * center
@ -476,7 +571,7 @@ Biz::Scene::SceneInteractor::ElementTransforms ScaleWidget::get_reset_scale_cand
};
const Domain::Transform3d volume_matrix{volume->get_matrix()};
const SquareMatrix4d no_scale{
remove_scale(volume_matrix, instance_matrix, bounding_box.center)
remove_scale(*volume, instance_matrix, bounding_box.center)
};
if (!volume_matrix.matrix().isApprox(no_scale)) {
result.insert_or_assign(ref, no_scale);
@ -486,7 +581,7 @@ Biz::Scene::SceneInteractor::ElementTransforms ScaleWidget::get_reset_scale_cand
const Domain::ElementRef ref{element.object_id, 0, volume->id().id};
const Domain::Transform3d volume_matrix{volume->get_matrix()};
const SquareMatrix4d no_scale{
remove_scale(volume_matrix, instance_matrix, bounding_box.center)
remove_scale(*volume, instance_matrix, bounding_box.center)
};
if (!volume_matrix.matrix().isApprox(no_scale)) {
result.insert_or_assign(ref, no_scale);

View file

@ -237,10 +237,10 @@ GizmoWindowPtr SvgGizmo::release_ui_window()
const Domain::ModelVolume& volume = (ref.volume_id != 0) ?
*project.find_volume_by_id(ref.object_id, ref.volume_id) :
*project.find_object_by_id(ref.object_id)->volumes.front();
Domain::Transform3d to_world = instance.get_matrix() * volume.get_matrix();
Domain::Transform3d to_world = instance.get_matrix() * volume.right_handed_matrix();
const Domain::Transform3d& instance_tr = instance.get_matrix();
const Domain::Transform3d instance_tr_inv = instance_tr.inverse();
const Domain::Transform3d& volume_tr_inv = volume.get_matrix().inverse();
const Domain::Transform3d& volume_tr_inv = volume.right_handed_matrix().inverse();
Domain::Vec3d world_position = to_world.translation();
ProjectContext& proj_ctx = m_proj_ctxs->selected();

View file

@ -318,10 +318,10 @@ TextGizmo::TextGizmo(
const Domain::ModelVolume& volume = (ref.volume_id != 0) ?
*project.find_volume_by_id(ref.object_id, ref.volume_id) :
*project.find_object_by_id(ref.object_id)->volumes.front();
Domain::Transform3d to_world = instance.get_matrix() * volume.get_matrix();
Domain::Transform3d to_world = instance.get_matrix() * volume.right_handed_matrix();
const Domain::Transform3d& instance_tr = instance.get_matrix();
const Domain::Transform3d instance_tr_inv = instance_tr.inverse();
const Domain::Transform3d& volume_tr_inv = volume.get_matrix().inverse();
const Domain::Transform3d& volume_tr_inv = volume.right_handed_matrix().inverse();
Domain::Vec3d world_position = to_world.translation();
const Biz::Emboss::TextPresetManager::Preset& preset = m_preset_manager.get_preset();

View file

@ -37,7 +37,12 @@ void MeshRenderNodeComponent::render(const Node& node, const Camera& camera, con
material.set_uniform(UNIFORM_PROJECTION_VIEW_MODEL_MATRIX, pvm);
material.set_uniform(UNIFORM_VIEW_NORMAL_MATRIX, normal);
const bool left_handed = model.block<3, 3>(0, 0).determinant() < 0.f;
if (left_handed)
cmd_buffer.set_cull_face_mode(Render::CullFaceMode::Front);
cmd_buffer.bind_and_draw(*m_geometry, material);
if (left_handed)
cmd_buffer.set_cull_face_mode(Render::CullFaceMode::Back);
}
void MeshRenderNodeComponent::set_geometry(

View file

@ -4,6 +4,7 @@
#include <cereal/archives/adapters.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/cereal.hpp>
#include <cereal/types/array.hpp>
#include <cereal/types/optional.hpp>
#include <cereal/types/polymorphic.hpp>
#include <cereal/types/string.hpp>
@ -259,6 +260,7 @@ void load(Archive& ar, Slic3r::Domain::ModelVolume& model_volume)
ar(model_volume.m_type,
model_volume.m_transformation,
model_volume.m_mirror_axes,
model_volume.m_is_splittable,
model_volume.cut_info,
model_volume.volume_settings,
@ -289,6 +291,7 @@ void save(Archive& ar, const Slic3r::Domain::ModelVolume& model_volume)
ar(model_volume.m_type,
model_volume.m_transformation,
model_volume.m_mirror_axes,
model_volume.m_is_splittable,
model_volume.cut_info,
model_volume.volume_settings,

View file

@ -87,6 +87,8 @@ static std::string to_string(Biz::UndoSnapshotType type)
return _u8L("Set scale");
case Type::RevertScale:
return _u8L("Revert scale");
case Type::Mirror:
return _u8L("Mirror");
case Type::PlaceOnFace:
return _u8L("Place on face");
case Type::Arrange:

View file

@ -125,14 +125,14 @@ bool SurfaceDrag::on_drag_start(const App::Scene::GizmoEventContext& ctx, const
ASSERT(instance_ptr != nullptr);
m_drag = std::make_unique<Drag>();
m_drag->to_world = instance_ptr->get_matrix() * volume.get_matrix();
m_drag->to_world = instance_ptr->get_matrix() * volume.right_handed_matrix();
m_drag->instance = instance_ptr->get_matrix();
m_drag->instance_inv = instance_ptr->get_matrix().inverse();
m_drag->volume_inv = volume.get_matrix().inverse();
m_drag->volume_inv = volume.right_handed_matrix().inverse();
Domain::Transform3d dist_tr{ Eigen::Translation<double, 3>(Domain::Vec3d(0, 0, -distance.value_or(0.))) };
Domain::Vec3d surface_point = distance.has_value() ?
(Domain::Vec3d)(instance_ptr->get_matrix() * (volume.get_matrix() * dist_tr)).translation() :
(Domain::Vec3d)(instance_ptr->get_matrix() * (volume.right_handed_matrix() * dist_tr)).translation() :
(Domain::Vec3d)m_drag->to_world.translation();
// Settings for cross hair
@ -300,7 +300,7 @@ Domain::Transform3d world_tr(const Domain::Project& project, const Domain::Eleme
const Domain::ModelVolume& volume = (ref.volume_id != 0) ?
*project.find_volume_by_id(ref.object_id, ref.volume_id) :
*project.find_object_by_id(ref.object_id)->volumes.front();
return instance.get_matrix() * volume.get_matrix();
return instance.get_matrix() * volume.right_handed_matrix();
}
std::optional<float> calc_rotation(const Domain::Project& project, const Domain::ElementRef& ref)
@ -366,7 +366,7 @@ void transform_selection_relative(const Domain::Transform3d& tr, Biz::ProjectInt
const Domain::ModelVolume& volume = (el.volume_id != 0) ?
*project.find_volume_by_id(el.object_id, el.volume_id) :
*project.find_object_by_id(el.object_id)->volumes.front();
Domain::Transform3d volume_tr = volume.get_matrix();
Domain::Transform3d volume_tr = volume.right_handed_matrix();
Domain::Transform3d world_relative = instance_tr * volume_tr * tr * volume_tr.inverse() * instance_tr.inverse();
scene_interactor.transform_selection(world_relative.matrix());
}

View file

@ -680,17 +680,25 @@ static Loaded3MF load_legacy_project(const std::string& file_path, OptionalPrese
return loaded_3mf;
}
static Loaded3MF with_right_handed_instances(Loaded3MF loaded_3mf)
{
for (ModelObject* object : loaded_3mf.model.objects) {
Biz::Algorithms::ModelObject::normalize_instance_handedness(*object);
}
return loaded_3mf;
}
Loaded3MF
load_from_project(const boost::filesystem::path& project_file_path, OptionalPresetBundle bundle)
{
const std::string project_file_name = project_file_path.string();
try {
return load_3mf(project_file_name);
return with_right_handed_instances(load_3mf(project_file_name));
} catch (const Loaded3MFException& e) {
if (e.issue.type != Read3mfIssueType::legacy_loader_required)
throw;
}
return load_legacy_project(project_file_name, bundle);
return with_right_handed_instances(load_legacy_project(project_file_name, bundle));
}
static std::string skipped_painting_warning_text(const Read3mfIssues& issues)

View file

@ -1121,9 +1121,37 @@ constexpr std::string_view SOURCE = "source"; // more in
constexpr std::string_view CUT_INFO = "cutInfo"; // more in CutSerialization
constexpr std::string_view SLA_SUPPORT_POINTS = "slaSupportPoints"; // in mesh coordinates, more in SlaSupportPointsSerialization
constexpr std::string_view SLA_DRAIN_HOLES = "slaDrainHoles"; // in mesh coordinates, more in SlaDrainHolesSerialization
constexpr std::string_view MIRROR = "mirror"; // local axes the user mirrored, subset of "xyz"
NamesType VOLUME_NAMES{{ID, VOLUME_UUID, VOLUME_TYPE, VOLUME_SETTINGS, SOURCE,
TEXT_CONFIGURATION, SHAPE, CUT_INFO, SLA_SUPPORT_POINTS, SLA_DRAIN_HOLES}};
NamesType VOLUME_NAMES{{ID, VOLUME_UUID, VOLUME_TYPE, VOLUME_SETTINGS, SOURCE,
TEXT_CONFIGURATION, SHAPE, CUT_INFO, SLA_SUPPORT_POINTS, SLA_DRAIN_HOLES, MIRROR}};
constexpr std::string_view MIRROR_AXIS_NAMES = "xyz";
std::string mirror_axes_to_json(const Domain::MirrorAxes& mirror_axes)
{
std::string result;
for (size_t i = 0; i < 3; ++i)
if (mirror_axes.axes[i])
result += MIRROR_AXIS_NAMES[i];
return result;
}
bool mirror_axes_from_json(const json& volume_json, Domain::MirrorAxes& mirror_axes, Read3mfIssues& collected_issues)
{
std::string axes;
if (!from_json(volume_json, MIRROR, axes, collected_issues, RT::project_volume_mirror_issue))
return false;
for (const char axis : axes) {
const size_t index = MIRROR_AXIS_NAMES.find(axis);
if (index == std::string_view::npos) {
collected_issues.add_issue(Read3mfIssue(RT::project_volume_mirror_issue, std::string("unknown axis"), axes));
return false;
}
mirror_axes.axes[index] = true;
}
return true;
}
using VolumeTypeToName = boost::bimap<ModelVolumeType, std::string_view>;
const VolumeTypeToName volume_type_to_name = boost::assign::list_of<VolumeTypeToName::relation>
@ -1164,6 +1192,8 @@ json volumes_to_json(const ModelVolumePtrs &volumes, const VolumeToObjectid &v2i
add(volume_json, SLA_SUPPORT_POINTS, SlaSupportPointsSerialization::to_json(volume.sla_support_points));
if (!volume.sla_drain_holes.empty())
add(volume_json, SLA_DRAIN_HOLES, SlaDrainHolesSerialization::to_json(volume.sla_drain_holes));
if (volume.mirror_axes().any())
volume_json[MIRROR] = mirror_axes_to_json(volume.mirror_axes());
if (!volume_json.empty())
volumes_json.push_back(std::move(volume_json));
}
@ -1283,6 +1313,11 @@ void load_volume(const json &volume_json, const VolumeMap &volume_map, Read3mfIs
for (ModelVolume *mv : mvs)
mv->sla_drain_holes = holes;
}
if (Domain::MirrorAxes mirror_axes; mirror_axes_from_json(volume_json, mirror_axes, collected_issues)) {
for (ModelVolume *mv : mvs)
mv->set_mirror_axes(mirror_axes);
}
}
} // namespace VolumeSerialization

View file

@ -461,14 +461,14 @@ Domain::ElementRefs SceneInteractor::selected_volumes_with_shear() const
const Domain::ElementRef ref{element.object_id, 0, element.volume_id};
const Domain::ModelVolume* volume{m_workbench.project(m_selected_project_id)
.find_volume_by_id(ref.object_id, ref.volume_id)};
if (has_shear(volume->get_matrix().matrix())) {
if (has_shear(volume->right_handed_matrix().matrix())) {
result.push_back(ref);
}
} else {
ASSERT(element.has_instance());
for (const Domain::ModelVolume* volume : instance->get_object()->volumes) {
const Domain::ElementRef ref{element.object_id, 0, volume->id().id};
if (has_shear(volume->get_matrix().matrix())) {
if (has_shear(volume->right_handed_matrix().matrix())) {
result.push_back(ref);
}
}
@ -2870,6 +2870,123 @@ void SceneInteractor::set_element_transforms(const SceneInteractor::ElementTrans
}
}
static Transform3d reflection_through(const Vec3d& center, const Transform3d& reflection)
{
return Eigen::Translation3d(center) * reflection * Eigen::Translation3d(-center);
}
static Transform3d reflection_in_instance_frame(
const SelectionReferenceFrame frame,
const ModelInstance& instance,
const ModelVolume& volume,
const Vec3d& center,
const Transform3d& reflection
)
{
const Transform3d instance_inverse{instance.get_matrix().inverse()};
switch (frame) {
case SelectionReferenceFrame::Volume: {
const Transform3d volume_trafo{volume.get_matrix()};
return volume_trafo
* reflection_through(volume_trafo.inverse() * instance_inverse * center, reflection)
* volume_trafo.inverse();
}
case SelectionReferenceFrame::Instance:
return reflection_through(instance_inverse * center, reflection);
case SelectionReferenceFrame::Bed:
return instance_inverse * reflection_through(center, reflection) * instance.get_matrix();
}
UNREACHABLE();
return Transform3d::Identity();
}
void SceneInteractor::mirror_selection(const Domain::Axis axis)
{
const std::optional<SelectionExtents> extents{selection_bounding_box()};
if (!extents) {
return;
}
const Vec3d center{extents->oriented_bounding_box().center};
const SelectionReferenceFrame frame{object_selection_reference_frame()};
Project& project{m_workbench.project(m_selected_project_id)};
Domain::MirrorAxes flip;
flip.flip(axis);
const Transform3d reflection{flip.matrix()};
ElementTransforms transforms;
std::set<size_t> bed_mirrored_objects;
auto mirror_volume = [&](const ModelInstance& instance,
ModelVolume& volume,
const Transform3d& local_reflection) {
const ElementRef ref{instance.get_object()->id().id, 0, volume.id().id};
if (transforms.contains(ref)) {
return;
}
transforms[ref] = (local_reflection * volume.get_matrix()).matrix();
Domain::MirrorAxes axes{volume.mirror_axes()};
axes.flip(axis);
volume.set_mirror_axes(axes);
};
for (const ElementRef& element : object_selection().elements) {
if (element.is_wipe_tower()) {
continue;
}
ModelInstance* instance{
project.find_instance_by_id(element.object_id, element.instance_id)
};
if (!instance) {
continue;
}
if (element.has_volume()) {
ModelVolume* volume{project.find_volume_by_id(element.object_id, element.volume_id)};
ASSERT(volume);
mirror_volume(
*instance,
*volume,
reflection_in_instance_frame(frame, *instance, *volume, center, reflection)
);
continue;
}
if (frame == SelectionReferenceFrame::Bed) {
transforms[ElementRef{element.object_id, element.instance_id}] =
(reflection_through(center, reflection) * instance->get_matrix() * reflection)
.matrix();
bed_mirrored_objects.insert(element.object_id);
for (ModelVolume* volume : instance->get_object()->volumes) {
mirror_volume(*instance, *volume, reflection);
}
continue;
}
for (ModelVolume* volume : instance->get_object()->volumes) {
mirror_volume(
*instance,
*volume,
reflection_in_instance_frame(frame, *instance, *volume, center, reflection)
);
}
}
for (const size_t object_id : bed_mirrored_objects) {
const ModelObject* object{project.find_object_by_id(object_id)};
ASSERT(object);
for (const ModelInstance* other : object->instances) {
const ElementRef ref{object_id, other->id().id};
if (transforms.contains(ref)) {
continue;
}
const Vec3d pivot{Biz::Algorithms::BoundingBox::center(
Biz::Algorithms::ModelObject::instance_bounding_box(*object, *other)
)};
transforms[ref] =
(reflection_through(pivot, reflection) * other->get_matrix() * reflection).matrix();
}
}
set_element_transforms(transforms);
}
void SceneInteractor::transform_selection(
const Transform& relative_transform,
bool place_on_bed

View file

@ -199,7 +199,7 @@ static std::optional<Biz::Scene::OrientedBoundingBox> get_volume_obb(
project_id,
selection,
workbench,
(instance->get_matrix() * volume->get_matrix()).rotation()
(instance->get_matrix() * volume->right_handed_matrix()).rotation()
);
}

View file

@ -575,3 +575,32 @@ TEST_CASE("3MF failing in the legacy loader reports the reason", "[3mf]")
REQUIRE_FALSE(imported.has_value());
CHECK_THAT(imported.error(), Catch::Matchers::ContainsSubstring("at line 4"));
}
TEST_CASE("3MF round trip keeps the volume mirror flags and matrix", "[3mf]")
{
Project project;
project.model() = Test::generate_cubes(1, 1);
ModelVolume* volume = project.model().objects.front()->volumes.front();
const Domain::Transform3d mirrored{
Domain::Transform3d(Eigen::AngleAxisd(0.3, Vec3d::UnitZ())) * Eigen::Scaling(-1., -1., 1.)
};
volume->set_transformation(Domain::Transformation{mirrored});
volume->set_mirror_axes(Domain::MirrorAxes{{true, true, false}});
const fs::path temp_dir =
fs::temp_directory_path() / fs::unique_path("slic3r-3mf-test-%%%%-%%%%");
fs::create_directories(temp_dir);
const fs::path file_path = temp_dir / "mirror_round_trip.3mf";
store_3mf(file_path.string(), project);
const Loaded3MF loaded = load_3mf(file_path.string());
boost::system::error_code cleanup_error;
fs::remove_all(temp_dir, cleanup_error);
REQUIRE(loaded.model.objects.size() == 1);
REQUIRE(loaded.model.objects.front()->volumes.size() == 1);
const ModelVolume* loaded_volume = loaded.model.objects.front()->volumes.front();
CHECK(loaded_volume->mirror_axes() == Domain::MirrorAxes{{true, true, false}});
CHECK(loaded_volume->get_matrix().matrix().isApprox(mirrored.matrix()));
}