From 1026a9c81760410f9547a96eb13184fb82e155b8 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 24 Sep 2018 10:19:40 +0200 Subject: [PATCH 1/7] Tweaks to flatten gizmo --- xs/src/slic3r/GUI/GLGizmo.cpp | 66 ++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index 1aad6d147..d0a052e16 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -1365,12 +1365,16 @@ void GLGizmoFlatten::update_planes() TriangleMesh ch; for (const ModelVolume* vol : m_model_object->volumes) ch.merge(vol->get_convex_hull()); + ch = ch.convex_hull_3d(); #if !ENABLE_MODELINSTANCE_3D_ROTATION ch.scale(m_model_object->instances.front()->scaling_factor); ch.rotate_z(m_model_object->instances.front()->rotation); #endif // !ENABLE_MODELINSTANCE_3D_ROTATION + const Vec3d& bb_size = ch.bounding_box().size(); + double min_bb_face_area = std::min(bb_size(0) * bb_size(1), std::min(bb_size(0) * bb_size(2), bb_size(1) * bb_size(2))); + m_planes.clear(); // Now we'll go through all the facets and append Points of facets sharing the same normal: @@ -1399,7 +1403,7 @@ void GLGizmoFlatten::update_planes() if (std::abs(this_normal(0) - (*normal_ptr)(0)) < 0.001 && std::abs(this_normal(1) - (*normal_ptr)(1)) < 0.001 && std::abs(this_normal(2) - (*normal_ptr)(2)) < 0.001) { stl_vertex* first_vertex = ch.stl.facet_start[facet_idx].vertex; for (int j=0; j<3; ++j) - m_planes.back().vertices.emplace_back(first_vertex[j](0), first_vertex[j](1), first_vertex[j](2)); + m_planes.back().vertices.emplace_back((double)first_vertex[j](0), (double)first_vertex[j](1), (double)first_vertex[j](2)); facet_visited[facet_idx] = true; for (int j = 0; j < 3; ++ j) { @@ -1413,49 +1417,69 @@ void GLGizmoFlatten::update_planes() // if this is a just a very small triangle, remove it to speed up further calculations (it would be rejected anyway): if (m_planes.back().vertices.size() == 3 && - (m_planes.back().vertices[0] - m_planes.back().vertices[1]).norm() < 1.f - || (m_planes.back().vertices[0] - m_planes.back().vertices[2]).norm() < 1.f) - m_planes.pop_back(); + ((m_planes.back().vertices[0] - m_planes.back().vertices[1]).norm() < 1.0 + || (m_planes.back().vertices[0] - m_planes.back().vertices[2]).norm() < 1.0 + || (m_planes.back().vertices[1] - m_planes.back().vertices[2]).norm() < 1.0)) + m_planes.pop_back(); } + const float minimal_area = 0.01f * (float)min_bb_face_area; + // Now we'll go through all the polygons, transform the points into xy plane to process them: for (unsigned int polygon_id=0; polygon_id < m_planes.size(); ++polygon_id) { Pointf3s& polygon = m_planes[polygon_id].vertices; const Vec3d& normal = m_planes[polygon_id].normal; // We are going to rotate about z and y to flatten the plane - float angle_z = 0.f; - float angle_y = 0.f; - if (std::abs(normal(1)) > 0.001) - angle_z = -atan2(normal(1), normal(0)); // angle to rotate so that normal ends up in xz-plane - if (std::abs(normal(0)*cos(angle_z) - normal(1)*sin(angle_z)) > 0.001) - angle_y = -atan2(normal(0)*cos(angle_z) - normal(1)*sin(angle_z), normal(2)); // angle to rotate to make normal point upwards - else { - // In case it already was in z-direction, we must ensure it is not the wrong way: - angle_y = normal(2) > 0.f ? 0.f : -PI; - } - - // Rotate all points to the xy plane: + Eigen::Quaterniond q; Transform3d m = Transform3d::Identity(); - m.rotate(Eigen::AngleAxisd((double)angle_y, Vec3d::UnitY())); - m.rotate(Eigen::AngleAxisd((double)angle_z, Vec3d::UnitZ())); + m.matrix().block(0, 0, 3, 3) = q.setFromTwoVectors(normal, Vec3d::UnitZ()).toRotationMatrix(); polygon = transform(polygon, m); polygon = Slic3r::Geometry::convex_hull(polygon); // To remove the inner points // We will calculate area of the polygon and discard ones that are too small // The limit is more forgiving in case the normal is in the direction of the coordinate axes - const float minimal_area = (std::abs(normal(0)) > 0.999f || std::abs(normal(1)) > 0.999f || std::abs(normal(2)) > 0.999f) ? 1.f : 20.f; + float area_threshold = (std::abs(normal(0)) > 0.999f || std::abs(normal(1)) > 0.999f || std::abs(normal(2)) > 0.999f) ? minimal_area : 10.0f * minimal_area; float& area = m_planes[polygon_id].area; area = 0.f; for (unsigned int i = 0; i < polygon.size(); i++) // Shoelace formula area += polygon[i](0)*polygon[i + 1 < polygon.size() ? i + 1 : 0](1) - polygon[i + 1 < polygon.size() ? i + 1 : 0](0)*polygon[i](1); - area = std::abs(area / 2.f); - if (area < minimal_area) { + area = 0.5f * std::abs(area); + if (area < area_threshold) { m_planes.erase(m_planes.begin()+(polygon_id--)); continue; } + // We check the inner angles and discard polygon with angles smaller than the following threshold + const double angle_threshold = ::cos(10.0 * (double)PI / 180.0); + bool discard = false; + + std::cout << std::endl << "polygon: " << polygon_id << " - " << polygon.size() << "(" << area << "/" << 100.0 * area / min_bb_face_area << ")" << std::endl; + + for (unsigned int i = 0; i < polygon.size(); ++i) + { + const Vec3d& prec = polygon[(i == 0) ? polygon.size() - 1 : i - 1]; + const Vec3d& curr = polygon[i]; + const Vec3d& next = polygon[(i == polygon.size() - 1) ? 0 : i + 1]; + + if ((prec - curr).normalized().dot((next - curr).normalized()) > angle_threshold) + { + discard = true; + break; + } + + std::cout << ::acos((prec - curr).normalized().dot((next - curr).normalized())) * 180.0 / PI << std::endl; + } + + if (discard) + { +// m_planes.erase(m_planes.begin() + polygon_id); +// --polygon_id; + m_planes.erase(m_planes.begin() + (polygon_id--)); + continue; + } + // We will shrink the polygon a little bit so it does not touch the object edges: Vec3d centroid = std::accumulate(polygon.begin(), polygon.end(), Vec3d(0.0, 0.0, 0.0)); centroid /= (double)polygon.size(); From 270341300fb2af96ea3e3db9662fe48aeaf76ea8 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 24 Sep 2018 10:28:52 +0200 Subject: [PATCH 2/7] Code cleanup --- xs/src/slic3r/GUI/GLGizmo.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index d0a052e16..599333898 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -1438,7 +1438,7 @@ void GLGizmoFlatten::update_planes() polygon = Slic3r::Geometry::convex_hull(polygon); // To remove the inner points - // We will calculate area of the polygon and discard ones that are too small + // We will calculate area of the polygons and discard ones that are too small // The limit is more forgiving in case the normal is in the direction of the coordinate axes float area_threshold = (std::abs(normal(0)) > 0.999f || std::abs(normal(1)) > 0.999f || std::abs(normal(2)) > 0.999f) ? minimal_area : 10.0f * minimal_area; float& area = m_planes[polygon_id].area; @@ -1451,12 +1451,10 @@ void GLGizmoFlatten::update_planes() continue; } - // We check the inner angles and discard polygon with angles smaller than the following threshold + // We check the inner angles and discard polygons with angles smaller than the following threshold const double angle_threshold = ::cos(10.0 * (double)PI / 180.0); bool discard = false; - std::cout << std::endl << "polygon: " << polygon_id << " - " << polygon.size() << "(" << area << "/" << 100.0 * area / min_bb_face_area << ")" << std::endl; - for (unsigned int i = 0; i < polygon.size(); ++i) { const Vec3d& prec = polygon[(i == 0) ? polygon.size() - 1 : i - 1]; @@ -1468,14 +1466,10 @@ void GLGizmoFlatten::update_planes() discard = true; break; } - - std::cout << ::acos((prec - curr).normalized().dot((next - curr).normalized())) * 180.0 / PI << std::endl; } if (discard) { -// m_planes.erase(m_planes.begin() + polygon_id); -// --polygon_id; m_planes.erase(m_planes.begin() + (polygon_id--)); continue; } From e3d44b07fec13e1a9ead40bb954f48914cac6ad1 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 24 Sep 2018 15:21:18 +0200 Subject: [PATCH 3/7] Fixed arrange for objects with 3D rotations --- lib/Slic3r/Model.pm | 2 +- xs/src/libslic3r/ModelArrange.hpp | 3 +-- xs/xsp/Model.xsp | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 22a76100d..6ec3fc1bf 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -122,7 +122,7 @@ sub add_instance { my $new_instance = $self->_add_instance; - $new_instance->set_rotation($args{rotation}) + $new_instance->set_rotations($args{rotation}) if defined $args{rotation}; $new_instance->set_scaling_factor($args{scaling_factor}) if defined $args{scaling_factor}; diff --git a/xs/src/libslic3r/ModelArrange.hpp b/xs/src/libslic3r/ModelArrange.hpp index 9f983ee9f..b203193ef 100644 --- a/xs/src/libslic3r/ModelArrange.hpp +++ b/xs/src/libslic3r/ModelArrange.hpp @@ -687,8 +687,7 @@ void applyResult( // write the transformation data into the model instance #if ENABLE_MODELINSTANCE_3D_ROTATION - // CHECK_ME -> Is the following correct ? - inst_ptr->set_rotation(Vec3d(0.0, 0.0, rot)); + inst_ptr->set_rotation(Z, rot); #else inst_ptr->rotation = rot; #endif // ENABLE_MODELINSTANCE_3D_ROTATION diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index 892ecd861..d5192cf8e 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -365,8 +365,8 @@ ModelMaterial::attributes() %code%{ RETVAL = THIS->get_object(); %}; #if ENABLE_MODELINSTANCE_3D_ROTATION - double rotation() - %code%{ RETVAL = THIS->get_rotation(Z); %}; + Vec3d* rotation() + %code%{ RETVAL = new Vec3d(THIS->get_rotation(X), THIS->get_rotation(Y), THIS->get_rotation(Z)); %}; #else double rotation() %code%{ RETVAL = THIS->rotation; %}; From 0e1843a871ede5670c07727c64a042fb8c3ec0b5 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 24 Sep 2018 15:54:09 +0200 Subject: [PATCH 4/7] 1st installment of ModelInstance 3D scale components --- lib/Slic3r/GUI/Plater.pm | 51 +++++++++++++++ lib/Slic3r/Model.pm | 2 +- xs/src/callback.cpp | 20 ++++++ xs/src/callback.hpp | 1 + xs/src/libslic3r/Format/3mf.cpp | 6 ++ xs/src/libslic3r/Format/AMF.cpp | 85 +++++++++++++++++++++++++ xs/src/libslic3r/Format/PRUS.cpp | 17 +++++ xs/src/libslic3r/Model.cpp | 21 ++++++ xs/src/libslic3r/Model.hpp | 19 ++++++ xs/src/libslic3r/ModelArrange.hpp | 14 ++++ xs/src/libslic3r/MultiPoint.cpp | 11 ++++ xs/src/libslic3r/MultiPoint.hpp | 3 + xs/src/libslic3r/Print.cpp | 9 ++- xs/src/libslic3r/Technologies.hpp | 3 +- xs/src/slic3r/GUI/3DScene.cpp | 55 ++++++++++++++++ xs/src/slic3r/GUI/3DScene.hpp | 11 ++++ xs/src/slic3r/GUI/GLCanvas3D.cpp | 82 +++++++++++++++++++++++- xs/src/slic3r/GUI/GLCanvas3D.hpp | 21 ++++++ xs/src/slic3r/GUI/GLCanvas3DManager.cpp | 18 ++++++ xs/src/slic3r/GUI/GLCanvas3DManager.hpp | 8 +++ xs/src/slic3r/GUI/GLGizmo.cpp | 33 +++++++++- xs/src/slic3r/GUI/GLGizmo.hpp | 13 +++- xs/src/slic3r/GUI/GUI_ObjectParts.cpp | 32 ++++++++++ xs/src/slic3r/GUI/GUI_ObjectParts.hpp | 4 ++ xs/xsp/GUI_3DScene.xsp | 14 ++++ xs/xsp/Model.xsp | 14 +++- 26 files changed, 558 insertions(+), 9 deletions(-) diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 525cc7931..c7d4260d2 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -144,6 +144,40 @@ sub new { $self->schedule_background_process; }; + # callback to react to gizmo scale + my $on_gizmo_scale_3D = sub { + my ($scale_x, $scale_y, $scale_z) = @_; + + my ($obj_idx, $object) = $self->selected_object; + return if !defined $obj_idx; + + my $model_object = $self->{model}->objects->[$obj_idx]; + my $model_instance = $model_object->instances->[0]; + + $self->stop_background_process; + + #FIXME Scale the layer height profile? +# my $variation = $scale / $model_instance->scaling_factor; +# foreach my $range (@{ $model_object->layer_height_ranges }) { +# $range->[0] *= $variation; +# $range->[1] *= $variation; +# } + + my $scale = Slic3r::Pointf3->new($scale_x, $scale_y, $scale_z); + foreach my $inst (@{ $model_object->instances }) { + $inst->set_scaling_factors($scale); + } + Slic3r::GUI::_3DScene::update_gizmos_data($self->{canvas3D}) if ($self->{canvas3D}); + + #update print and start background processing + $self->{print}->add_model_object($model_object, $obj_idx); + + $self->selection_changed(1); # refresh info (size, volume etc.) + $self->update; + $self->schedule_background_process; + + }; + # callback to react to gizmo rotate my $on_gizmo_rotate = sub { my ($angle) = @_; @@ -223,6 +257,21 @@ sub new { } }; + # callback to update object's geometry info while using gizmos + my $on_update_geometry_3D_info = sub { + my ($size_x, $size_y, $size_z, $scale_x, $scale_y, $scale_z) = @_; + + my ($obj_idx, $object) = $self->selected_object; + + if ((defined $obj_idx) && ($self->{object_info_size})) { # have we already loaded the info pane? + $self->{object_info_size}->SetLabel(sprintf("%.2f x %.2f x %.2f", $size_x, $size_y, $size_z)); + my $model_object = $self->{model}->objects->[$obj_idx]; + if (my $stats = $model_object->mesh_stats) { + $self->{object_info_volume}->SetLabel(sprintf('%.2f', $stats->{volume} * $scale_x * $scale_y * $scale_z)); + } + } + }; + # callbacks for toolbar my $on_action_add = sub { $self->add; @@ -312,11 +361,13 @@ sub new { Slic3r::GUI::_3DScene::register_on_instance_moved_callback($self->{canvas3D}, $on_instances_moved); Slic3r::GUI::_3DScene::register_on_enable_action_buttons_callback($self->{canvas3D}, $enable_action_buttons); Slic3r::GUI::_3DScene::register_on_gizmo_scale_uniformly_callback($self->{canvas3D}, $on_gizmo_scale_uniformly); + Slic3r::GUI::_3DScene::register_on_gizmo_scale_3D_callback($self->{canvas3D}, $on_gizmo_scale_3D); Slic3r::GUI::_3DScene::register_on_gizmo_rotate_callback($self->{canvas3D}, $on_gizmo_rotate); Slic3r::GUI::_3DScene::register_on_gizmo_rotate_3D_callback($self->{canvas3D}, $on_gizmo_rotate_3D); Slic3r::GUI::_3DScene::register_on_gizmo_flatten_callback($self->{canvas3D}, $on_gizmo_flatten); Slic3r::GUI::_3DScene::register_on_gizmo_flatten_3D_callback($self->{canvas3D}, $on_gizmo_flatten_3D); Slic3r::GUI::_3DScene::register_on_update_geometry_info_callback($self->{canvas3D}, $on_update_geometry_info); + Slic3r::GUI::_3DScene::register_on_update_geometry_3D_info_callback($self->{canvas3D}, $on_update_geometry_3D_info); Slic3r::GUI::_3DScene::register_action_add_callback($self->{canvas3D}, $on_action_add); Slic3r::GUI::_3DScene::register_action_delete_callback($self->{canvas3D}, $on_action_delete); Slic3r::GUI::_3DScene::register_action_deleteall_callback($self->{canvas3D}, $on_action_deleteall); diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 6ec3fc1bf..ec31f6c8a 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -124,7 +124,7 @@ sub add_instance { $new_instance->set_rotations($args{rotation}) if defined $args{rotation}; - $new_instance->set_scaling_factor($args{scaling_factor}) + $new_instance->set_scaling_factors($args{scaling_factor}) if defined $args{scaling_factor}; $new_instance->set_offset($args{offset}) if defined $args{offset}; diff --git a/xs/src/callback.cpp b/xs/src/callback.cpp index 05b4f9e50..3d2a8e903 100644 --- a/xs/src/callback.cpp +++ b/xs/src/callback.cpp @@ -149,6 +149,26 @@ void PerlCallback::call(double a, double b, double c, double d) const LEAVE; } +void PerlCallback::call(double a, double b, double c, double d, double e, double f) const +{ + if (!m_callback) + return; + dSP; + ENTER; + SAVETMPS; + PUSHMARK(SP); + XPUSHs(sv_2mortal(newSVnv(a))); + XPUSHs(sv_2mortal(newSVnv(b))); + XPUSHs(sv_2mortal(newSVnv(c))); + XPUSHs(sv_2mortal(newSVnv(d))); + XPUSHs(sv_2mortal(newSVnv(e))); + XPUSHs(sv_2mortal(newSVnv(f))); + PUTBACK; + perl_call_sv(SvRV((SV*)m_callback), G_DISCARD); + FREETMPS; + LEAVE; +} + void PerlCallback::call(bool b) const { call(b ? 1 : 0); diff --git a/xs/src/callback.hpp b/xs/src/callback.hpp index 9530829f8..ee7a5eadf 100644 --- a/xs/src/callback.hpp +++ b/xs/src/callback.hpp @@ -22,6 +22,7 @@ public: void call(double a, double b) const; void call(double a, double b, double c) const; void call(double a, double b, double c, double d) const; + void call(double a, double b, double c, double d, double e, double f) const; void call(bool b) const; private: void *m_callback; diff --git a/xs/src/libslic3r/Format/3mf.cpp b/xs/src/libslic3r/Format/3mf.cpp index ec864d995..79863f074 100644 --- a/xs/src/libslic3r/Format/3mf.cpp +++ b/xs/src/libslic3r/Format/3mf.cpp @@ -1270,9 +1270,11 @@ namespace Slic3r { if ((sx == 0.0) || (sy == 0.0) || (sz == 0.0)) return; +#if !ENABLE_MODELINSTANCE_3D_SCALE // non-uniform scale value, return if ((std::abs(sx - sy) > 0.00001) || (std::abs(sx - sz) > 0.00001)) return; +#endif // !ENABLE_MODELINSTANCE_3D_SCALE double inv_sx = 1.0 / sx; double inv_sy = 1.0 / sy; @@ -1303,7 +1305,11 @@ namespace Slic3r { instance.offset(0) = offset_x; instance.offset(1) = offset_y; #endif // ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_SCALE + instance.set_scaling_factor(Vec3d(sx, sy, sz)); +#else instance.scaling_factor = sx; +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION instance.set_rotation(rotation); #else diff --git a/xs/src/libslic3r/Format/AMF.cpp b/xs/src/libslic3r/Format/AMF.cpp index 6d3e06407..1f2298d9b 100644 --- a/xs/src/libslic3r/Format/AMF.cpp +++ b/xs/src/libslic3r/Format/AMF.cpp @@ -31,8 +31,14 @@ // 1 : Introduction of amf versioning. No other change in data saved into amf files. #if ENABLE_MODELINSTANCE_3D_OFFSET #if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE // 2 : Added z component of offset // Added x and y components of rotation +// Added x, y and z components of scale +#else +// 2 : Added z component of offset +// Added x and y components of rotation +#endif // ENABLE_MODELINSTANCE_3D_SCALE #else // 2 : Added z component of offset. #endif // ENABLE_MODELINSTANCE_3D_ROTATION @@ -138,13 +144,22 @@ struct AMFParserContext #endif // ENABLE_MODELINSTANCE_3D_ROTATION NODE_TYPE_RZ, // amf/constellation/instance/rz NODE_TYPE_SCALE, // amf/constellation/instance/scale +#if ENABLE_MODELINSTANCE_3D_SCALE + NODE_TYPE_SCALEX, // amf/constellation/instance/scalex + NODE_TYPE_SCALEY, // amf/constellation/instance/scaley + NODE_TYPE_SCALEZ, // amf/constellation/instance/scalez +#endif // ENABLE_MODELINSTANCE_3D_SCALE NODE_TYPE_METADATA, // anywhere under amf/*/metadata }; struct Instance { #if ENABLE_MODELINSTANCE_3D_OFFSET #if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rx_set(false), ry_set(false), rz_set(false), scalex_set(false), scaley_set(false), scalez_set(false) {} +#else Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rx_set(false), ry_set(false), rz_set(false), scale_set(false) {} +#endif // ENABLE_MODELINSTANCE_3D_SCALE #else Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rz_set(false), scale_set(false) {} #endif // ENABLE_MODELINSTANCE_3D_ROTATION @@ -173,9 +188,19 @@ struct AMFParserContext // Rotation around the Z axis. float rz; bool rz_set; +#if ENABLE_MODELINSTANCE_3D_SCALE + // Scaling factors + float scalex; + bool scalex_set; + float scaley; + bool scaley_set; + float scalez; + bool scalez_set; +#else // Scaling factor float scale; bool scale_set; +#endif // ENABLE_MODELINSTANCE_3D_SCALE }; struct Object { @@ -304,6 +329,14 @@ void AMFParserContext::startElement(const char *name, const char **atts) #endif // ENABLE_MODELINSTANCE_3D_ROTATION else if (strcmp(name, "rz") == 0) node_type_new = NODE_TYPE_RZ; +#if ENABLE_MODELINSTANCE_3D_SCALE + else if (strcmp(name, "scalex") == 0) + node_type_new = NODE_TYPE_SCALEX; + else if (strcmp(name, "scaley") == 0) + node_type_new = NODE_TYPE_SCALEY; + else if (strcmp(name, "scalez") == 0) + node_type_new = NODE_TYPE_SCALEZ; +#endif // ENABLE_MODELINSTANCE_3D_SCALE else if (strcmp(name, "scale") == 0) node_type_new = NODE_TYPE_SCALE; } @@ -371,7 +404,14 @@ void AMFParserContext::characters(const XML_Char *s, int len) m_path.back() == NODE_TYPE_RY || #endif // ENABLE_MODELINSTANCE_3D_ROTATION m_path.back() == NODE_TYPE_RZ || +#if ENABLE_MODELINSTANCE_3D_SCALE + m_path.back() == NODE_TYPE_SCALEX || + m_path.back() == NODE_TYPE_SCALEY || + m_path.back() == NODE_TYPE_SCALEZ || m_path.back() == NODE_TYPE_SCALE) +#else + m_path.back() == NODE_TYPE_SCALE) +#endif // ENABLE_MODELINSTANCE_3D_SCALE #else if (m_path.back() == NODE_TYPE_DELTAX || m_path.back() == NODE_TYPE_DELTAY || m_path.back() == NODE_TYPE_RZ || m_path.back() == NODE_TYPE_SCALE) #endif // ENABLE_MODELINSTANCE_3D_OFFSET @@ -444,10 +484,39 @@ void AMFParserContext::endElement(const char * /* name */) break; case NODE_TYPE_SCALE: assert(m_instance); +#if ENABLE_MODELINSTANCE_3D_SCALE + m_instance->scalex = float(atof(m_value[0].c_str())); + m_instance->scalex_set = true; + m_instance->scaley = float(atof(m_value[0].c_str())); + m_instance->scaley_set = true; + m_instance->scalez = float(atof(m_value[0].c_str())); + m_instance->scalez_set = true; +#else m_instance->scale = float(atof(m_value[0].c_str())); m_instance->scale_set = true; +#endif // ENABLE_MODELINSTANCE_3D_SCALE m_value[0].clear(); break; +#if ENABLE_MODELINSTANCE_3D_SCALE + case NODE_TYPE_SCALEX: + assert(m_instance); + m_instance->scalex = float(atof(m_value[0].c_str())); + m_instance->scalex_set = true; + m_value[0].clear(); + break; + case NODE_TYPE_SCALEY: + assert(m_instance); + m_instance->scaley = float(atof(m_value[0].c_str())); + m_instance->scaley_set = true; + m_value[0].clear(); + break; + case NODE_TYPE_SCALEZ: + assert(m_instance); + m_instance->scalez = float(atof(m_value[0].c_str())); + m_instance->scalez_set = true; + m_value[0].clear(); + break; +#endif // ENABLE_MODELINSTANCE_3D_SCALE // Object vertices: case NODE_TYPE_VERTEX: @@ -596,7 +665,11 @@ void AMFParserContext::endDocument() #else mi->rotation = instance.rz_set ? instance.rz : 0.f; #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + mi->set_scaling_factor(Vec3d(instance.scalex_set ? (double)instance.scalex : 1.0, instance.scaley_set ? (double)instance.scaley : 1.0, instance.scalez_set ? (double)instance.scalez : 1.0)); +#else mi->scaling_factor = instance.scale_set ? instance.scale : 1.f; +#endif // ENABLE_MODELINSTANCE_3D_SCALE } } } @@ -904,7 +977,13 @@ bool store_amf(const char *path, Model *model, Print* print, bool export_print_c " %lf\n" #endif // ENABLE_MODELINSTANCE_3D_ROTATION " %lf\n" +#if ENABLE_MODELINSTANCE_3D_SCALE + " %lf\n" + " %lf\n" + " %lf\n" +#else " %lf\n" +#endif // ENABLE_MODELINSTANCE_3D_SCALE " \n", object_id, #if ENABLE_MODELINSTANCE_3D_OFFSET @@ -922,7 +1001,13 @@ bool store_amf(const char *path, Model *model, Print* print, bool export_print_c #else instance->rotation, #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + instance->get_scaling_factor(X), + instance->get_scaling_factor(Y), + instance->get_scaling_factor(Z)); +#else instance->scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE //FIXME missing instance->scaling_factor instances.append(buf); diff --git a/xs/src/libslic3r/Format/PRUS.cpp b/xs/src/libslic3r/Format/PRUS.cpp index 27095acef..385e70b5d 100644 --- a/xs/src/libslic3r/Format/PRUS.cpp +++ b/xs/src/libslic3r/Format/PRUS.cpp @@ -169,7 +169,11 @@ bool load_prus(const char *path, Model *model) #else double instance_rotation = 0.; #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + Vec3d instance_scaling_factor = Vec3d::Ones(); +#else double instance_scaling_factor = 1.f; +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_OFFSET Vec3d instance_offset = Vec3d::Zero(); #else @@ -197,10 +201,14 @@ bool load_prus(const char *path, Model *model) "[%f, %f, %f]", scale, scale+1, scale+2) == 3 && sscanf(zero_xml+strlen(zero_tag), "[%f, %f, %f]", zero, zero+1, zero+2) == 3) { +#if ENABLE_MODELINSTANCE_3D_SCALE + instance_scaling_factor = Vec3d((double)scale[0], (double)scale[1], (double)scale[2]); +#else if (scale[0] == scale[1] && scale[1] == scale[2]) { instance_scaling_factor = scale[0]; scale[0] = scale[1] = scale[2] = 1.; } +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION instance_rotation = Vec3d(-(double)rotation[0], -(double)rotation[1], -(double)rotation[2]); #else @@ -225,7 +233,12 @@ bool load_prus(const char *path, Model *model) instance_offset(0) = position[0] - zero[0]; instance_offset(1) = position[1] - zero[1]; #endif // ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_SCALE + // CHECK_ME -> Is the following correct ? + trafo[2][3] = position[2] / instance_scaling_factor(2); +#else trafo[2][3] = position[2] / instance_scaling_factor; +#endif // ENABLE_MODELINSTANCE_3D_SCALE trafo_set = true; } const char *group_tag = ""; @@ -379,7 +392,11 @@ bool load_prus(const char *path, Model *model) #else instance->rotation = instance_rotation; #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + instance->set_scaling_factor(instance_scaling_factor); +#else instance->scaling_factor = instance_scaling_factor; +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_OFFSET instance->set_offset(instance_offset); #else diff --git a/xs/src/libslic3r/Model.cpp b/xs/src/libslic3r/Model.cpp index 65743da8d..0f8c3665d 100644 --- a/xs/src/libslic3r/Model.cpp +++ b/xs/src/libslic3r/Model.cpp @@ -1090,11 +1090,23 @@ BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mes BoundingBoxf3 bbox = copy.bounding_box(); if (!empty(bbox)) { +#if ENABLE_MODELINSTANCE_3D_SCALE + // Scale the bounding box along the three axes. + for (unsigned int i = 0; i < 3; ++i) + { + if (std::abs(this->m_scaling_factor(i) - 1.0) > EPSILON) + { + bbox.min(i) *= this->m_scaling_factor(i); + bbox.max(i) *= this->m_scaling_factor(i); + } + } +#else // Scale the bounding box uniformly. if (std::abs(this->scaling_factor - 1.) > EPSILON) { bbox.min *= this->scaling_factor; bbox.max *= this->scaling_factor; } +#endif // ENABLE_MODELINSTANCE_3D_SCALE // Translate the bounding box. if (! dont_translate) { #if ENABLE_MODELINSTANCE_3D_OFFSET @@ -1127,7 +1139,12 @@ void ModelInstance::transform_polygon(Polygon* polygon) const #else polygon->rotate(this->rotation); // rotate around polygon origin #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + // CHECK_ME -> Is the following correct ? + polygon->scale(this->m_scaling_factor(0), this->m_scaling_factor(1)); // scale around polygon origin +#else polygon->scale(this->scaling_factor); // scale around polygon origin +#endif // ENABLE_MODELINSTANCE_3D_SCALE } Transform3d ModelInstance::world_matrix(bool dont_translate, bool dont_rotate, bool dont_scale) const @@ -1153,7 +1170,11 @@ Transform3d ModelInstance::world_matrix(bool dont_translate, bool dont_rotate, b #endif // ENABLE_MODELINSTANCE_3D_ROTATION if (!dont_scale) +#if ENABLE_MODELINSTANCE_3D_SCALE + m.scale(m_scaling_factor); +#else m.scale(scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE return m; } diff --git a/xs/src/libslic3r/Model.hpp b/xs/src/libslic3r/Model.hpp index ba315e3d6..e3dc0b522 100644 --- a/xs/src/libslic3r/Model.hpp +++ b/xs/src/libslic3r/Model.hpp @@ -249,6 +249,9 @@ private: #if ENABLE_MODELINSTANCE_3D_ROTATION Vec3d m_rotation; // Rotation around the three axes, in radians around mesh center point #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + Vec3d m_scaling_factor; // Scaling factors along the three axes +#endif // ENABLE_MODELINSTANCE_3D_SCALE public: #endif // ENABLE_MODELINSTANCE_3D_OFFSET @@ -256,7 +259,9 @@ public: #if !ENABLE_MODELINSTANCE_3D_ROTATION double rotation; // Rotation around the Z axis, in radians around mesh center point #endif // !ENABLE_MODELINSTANCE_3D_ROTATION +#if !ENABLE_MODELINSTANCE_3D_SCALE double scaling_factor; +#endif // !ENABLE_MODELINSTANCE_3D_SCALE #if !ENABLE_MODELINSTANCE_3D_OFFSET Vec2d offset; // in unscaled coordinates #endif // !ENABLE_MODELINSTANCE_3D_OFFSET @@ -282,6 +287,14 @@ public: void set_rotation(Axis axis, double rotation); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + Vec3d get_scaling_factor() const { return m_scaling_factor; } + double get_scaling_factor(Axis axis) const { return m_scaling_factor(axis); } + + void set_scaling_factor(const Vec3d& scaling_factor) { m_scaling_factor = scaling_factor; } + void set_scaling_factor(Axis axis, double scaling_factor) { m_scaling_factor(axis) = scaling_factor; } +#endif // ENABLE_MODELINSTANCE_3D_SCALE + // To be called on an external mesh void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const; // Calculate a bounding box of a transformed mesh. To be called on an external mesh. @@ -303,9 +316,15 @@ private: #if ENABLE_MODELINSTANCE_3D_OFFSET #if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + ModelInstance(ModelObject *object) : m_rotation(Vec3d::Zero()), m_scaling_factor(Vec3d::Ones()), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {} + ModelInstance(ModelObject *object, const ModelInstance &other) : + m_rotation(other.m_rotation), m_scaling_factor(other.m_scaling_factor), m_offset(other.m_offset), object(object), print_volume_state(PVS_Inside) {} +#else ModelInstance(ModelObject *object) : m_rotation(Vec3d::Zero()), scaling_factor(1), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {} ModelInstance(ModelObject *object, const ModelInstance &other) : m_rotation(other.m_rotation), scaling_factor(other.scaling_factor), m_offset(other.m_offset), object(object), print_volume_state(PVS_Inside) {} +#endif // ENABLE_MODELINSTANCE_3D_SCALE #else ModelInstance(ModelObject *object) : rotation(0), scaling_factor(1), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {} ModelInstance(ModelObject *object, const ModelInstance &other) : diff --git a/xs/src/libslic3r/ModelArrange.hpp b/xs/src/libslic3r/ModelArrange.hpp index b203193ef..7e2de2bf1 100644 --- a/xs/src/libslic3r/ModelArrange.hpp +++ b/xs/src/libslic3r/ModelArrange.hpp @@ -29,7 +29,12 @@ std::string toString(const Model& model, bool holes = true) { if(!objinst) continue; Slic3r::TriangleMesh tmpmesh = rmesh; +#if ENABLE_MODELINSTANCE_3D_SCALE + // CHECK_ME -> Is the following correct ? + tmpmesh.scale(objinst->get_scaling_factor()); +#else tmpmesh.scale(objinst->scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE objinst->transform_mesh(&tmpmesh); ExPolygons expolys = tmpmesh.horizontal_projection(); for(auto& expoly_complex : expolys) { @@ -87,7 +92,11 @@ void toSVG(SVG& svg, const Model& model) { if(!objinst) continue; Slic3r::TriangleMesh tmpmesh = rmesh; +#if ENABLE_MODELINSTANCE_3D_SCALE + tmpmesh.scale(objinst->get_scaling_factor()); +#else tmpmesh.scale(objinst->scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE objinst->transform_mesh(&tmpmesh); ExPolygons expolys = tmpmesh.horizontal_projection(); svg.draw(expolys); @@ -513,7 +522,12 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model) { Slic3r::TriangleMesh tmpmesh = rmesh; ClipperLib::PolygonImpl pn; +#if ENABLE_MODELINSTANCE_3D_SCALE + // CHECK_ME -> is the following correct ? + tmpmesh.scale(objinst->get_scaling_factor()); +#else tmpmesh.scale(objinst->scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE // TODO export the exact 2D projection auto p = tmpmesh.convex_hull(); diff --git a/xs/src/libslic3r/MultiPoint.cpp b/xs/src/libslic3r/MultiPoint.cpp index f44897a04..48b2840c8 100644 --- a/xs/src/libslic3r/MultiPoint.cpp +++ b/xs/src/libslic3r/MultiPoint.cpp @@ -14,6 +14,17 @@ void MultiPoint::scale(double factor) pt *= factor; } +#if ENABLE_MODELINSTANCE_3D_SCALE +void MultiPoint::scale(double factor_x, double factor_y) +{ + for (Point &pt : points) + { + pt(0) *= factor_x; + pt(1) *= factor_y; + } +} +#endif // ENABLE_MODELINSTANCE_3D_SCALE + void MultiPoint::translate(double x, double y) { Vector v(x, y); diff --git a/xs/src/libslic3r/MultiPoint.hpp b/xs/src/libslic3r/MultiPoint.hpp index 03b89df51..c96aa3814 100644 --- a/xs/src/libslic3r/MultiPoint.hpp +++ b/xs/src/libslic3r/MultiPoint.hpp @@ -26,6 +26,9 @@ public: MultiPoint& operator=(const MultiPoint &other) { points = other.points; return *this; } MultiPoint& operator=(MultiPoint &&other) { points = std::move(other.points); return *this; } void scale(double factor); +#if ENABLE_MODELINSTANCE_3D_SCALE + void scale(double factor_x, double factor_y); +#endif // ENABLE_MODELINSTANCE_3D_SCALE void translate(double x, double y); void translate(const Point &vector); void rotate(double angle) { this->rotate(cos(angle), sin(angle)); } diff --git a/xs/src/libslic3r/Print.cpp b/xs/src/libslic3r/Print.cpp index cdc12d2d1..1a9b13049 100644 --- a/xs/src/libslic3r/Print.cpp +++ b/xs/src/libslic3r/Print.cpp @@ -431,7 +431,14 @@ void Print::add_model_object(ModelObject* model_object, int idx) std::vector v_scale; for (const PrintObject *object : m_objects) { const ModelObject &mobj = *object->model_object(); - v_scale.push_back(boost::lexical_cast(mobj.instances[0]->scaling_factor*100) + "%"); +#if ENABLE_MODELINSTANCE_3D_SCALE + // CHECK_ME -> Is the following correct ? + v_scale.push_back("x:" + boost::lexical_cast(mobj.instances[0]->get_scaling_factor(X) * 100) + + "% y:" + boost::lexical_cast(mobj.instances[0]->get_scaling_factor(Y) * 100) + + "% z:" + boost::lexical_cast(mobj.instances[0]->get_scaling_factor(Z) * 100) + "%"); +#else + v_scale.push_back(boost::lexical_cast(mobj.instances[0]->scaling_factor * 100) + "%"); +#endif // ENABLE_MODELINSTANCE_3D_SCALE if (input_file.empty()) input_file = mobj.input_file; } diff --git a/xs/src/libslic3r/Technologies.hpp b/xs/src/libslic3r/Technologies.hpp index 25e6b85e7..bf0c8b370 100644 --- a/xs/src/libslic3r/Technologies.hpp +++ b/xs/src/libslic3r/Technologies.hpp @@ -10,7 +10,8 @@ #define ENABLE_GIZMOS_RESET (1 && ENABLE_1_42_0) // Add x and y rotation components to model instances' offset #define ENABLE_MODELINSTANCE_3D_ROTATION (1 && ENABLE_MODELINSTANCE_3D_OFFSET) - +// Add scaling factors for all the three axes to model instances +#define ENABLE_MODELINSTANCE_3D_SCALE (1 && ENABLE_MODELINSTANCE_3D_ROTATION) #endif // _technologies_h_ diff --git a/xs/src/slic3r/GUI/3DScene.cpp b/xs/src/slic3r/GUI/3DScene.cpp index 5b830b30d..2863796ae 100644 --- a/xs/src/slic3r/GUI/3DScene.cpp +++ b/xs/src/slic3r/GUI/3DScene.cpp @@ -201,7 +201,11 @@ GLVolume::GLVolume(float r, float g, float b, float a) #else , m_rotation(0.0) #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + , m_scaling_factor(Vec3d::Ones()) +#else , m_scaling_factor(1.0) +#endif // ENABLE_MODELINSTANCE_3D_SCALE , m_world_matrix(Transform3f::Identity()) , m_world_matrix_dirty(true) , m_transformed_bounding_box_dirty(true) @@ -309,6 +313,18 @@ void GLVolume::set_offset(const Vec3d& offset) } } +#if ENABLE_MODELINSTANCE_3D_SCALE +void GLVolume::set_scaling_factor(const Vec3d& scaling_factor) +{ + if (m_scaling_factor != scaling_factor) + { + m_scaling_factor = scaling_factor; + m_world_matrix_dirty = true; + m_transformed_bounding_box_dirty = true; + m_transformed_convex_hull_bounding_box_dirty = true; + } +} +#else void GLVolume::set_scaling_factor(double factor) { if (m_scaling_factor != factor) @@ -319,6 +335,7 @@ void GLVolume::set_scaling_factor(double factor) m_transformed_convex_hull_bounding_box_dirty = true; } } +#endif // ENABLE_MODELINSTANCE_3D_SCALE void GLVolume::set_convex_hull(const TriangleMesh& convex_hull) { @@ -356,7 +373,11 @@ const Transform3f& GLVolume::world_matrix() const #else m_world_matrix.rotate(Eigen::AngleAxisf((float)m_rotation, Vec3f::UnitZ())); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + m_world_matrix.scale(m_scaling_factor.cast()); +#else m_world_matrix.scale((float)m_scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE m_world_matrix_dirty = false; } return m_world_matrix; @@ -438,7 +459,11 @@ void GLVolume::render() const #else ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); +#else ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE if (this->indexed_vertex_array.indexed()) this->indexed_vertex_array.render(this->tverts_range, this->qverts_range); else @@ -570,7 +595,11 @@ void GLVolume::render_VBOs(int color_id, int detection_id, int worldmatrix_id) c #else ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); +#else ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE if (n_triangles > 0) { @@ -621,7 +650,11 @@ void GLVolume::render_legacy() const #else ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); +#else ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE if (n_triangles > 0) ::glDrawElements(GL_TRIANGLES, n_triangles, GL_UNSIGNED_INT, indexed_vertex_array.triangle_indices.data() + tverts_range.first); @@ -749,7 +782,11 @@ std::vector GLVolumeCollection::load_object( #else v.set_rotation(instance->rotation); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + v.set_scaling_factor(instance->get_scaling_factor()); +#else v.set_scaling_factor(instance->scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE } } @@ -2112,7 +2149,16 @@ void _3DScene::register_on_enable_action_buttons_callback(wxGLCanvas* canvas, vo void _3DScene::register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback) { +#if !ENABLE_MODELINSTANCE_3D_SCALE s_canvas_mgr.register_on_gizmo_scale_uniformly_callback(canvas, callback); +#endif // !ENABLE_MODELINSTANCE_3D_SCALE +} + +void _3DScene::register_on_gizmo_scale_3D_callback(wxGLCanvas* canvas, void* callback) +{ +#if ENABLE_MODELINSTANCE_3D_SCALE + s_canvas_mgr.register_on_gizmo_scale_3D_callback(canvas, callback); +#endif // ENABLE_MODELINSTANCE_3D_SCALE } void _3DScene::register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback) @@ -2145,7 +2191,16 @@ void _3DScene::register_on_gizmo_flatten_3D_callback(wxGLCanvas* canvas, void* c void _3DScene::register_on_update_geometry_info_callback(wxGLCanvas* canvas, void* callback) { +#if !ENABLE_MODELINSTANCE_3D_SCALE s_canvas_mgr.register_on_update_geometry_info_callback(canvas, callback); +#endif // !ENABLE_MODELINSTANCE_3D_SCALE +} + +void _3DScene::register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback) +{ +#if ENABLE_MODELINSTANCE_3D_SCALE + s_canvas_mgr.register_on_update_geometry_3D_info_callback(canvas, callback); +#endif // ENABLE_MODELINSTANCE_3D_SCALE } void _3DScene::register_action_add_callback(wxGLCanvas* canvas, void* callback) diff --git a/xs/src/slic3r/GUI/3DScene.hpp b/xs/src/slic3r/GUI/3DScene.hpp index 8fc1661fc..f9ff9f1ea 100644 --- a/xs/src/slic3r/GUI/3DScene.hpp +++ b/xs/src/slic3r/GUI/3DScene.hpp @@ -263,8 +263,13 @@ private: // Rotation around Z axis of the volume to be rendered. double m_rotation; #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + // Scale factor along the three axes of the volume to be rendered. + Vec3d m_scaling_factor; +#else // Scale factor of the volume to be rendered. double m_scaling_factor; +#endif // ENABLE_MODELINSTANCE_3D_SCALE // World matrix of the volume to be rendered. mutable Transform3f m_world_matrix; // Whether or not is needed to recalculate the world matrix. @@ -343,7 +348,11 @@ public: const Vec3d& get_offset() const; void set_offset(const Vec3d& offset); +#if ENABLE_MODELINSTANCE_3D_SCALE + void set_scaling_factor(const Vec3d& scaling_factor); +#else void set_scaling_factor(double factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE void set_convex_hull(const TriangleMesh& convex_hull); @@ -567,11 +576,13 @@ public: static void register_on_wipe_tower_moved_callback(wxGLCanvas* canvas, void* callback); static void register_on_enable_action_buttons_callback(wxGLCanvas* canvas, void* callback); static void register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback); + static void register_on_gizmo_scale_3D_callback(wxGLCanvas* canvas, void* callback); static void register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback); static void register_on_gizmo_rotate_3D_callback(wxGLCanvas* canvas, void* callback); static void register_on_gizmo_flatten_callback(wxGLCanvas* canvas, void* callback); static void register_on_gizmo_flatten_3D_callback(wxGLCanvas* canvas, void* callback); static void register_on_update_geometry_info_callback(wxGLCanvas* canvas, void* callback); + static void register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback); static void register_action_add_callback(wxGLCanvas* canvas, void* callback); static void register_action_delete_callback(wxGLCanvas* canvas, void* callback); diff --git a/xs/src/slic3r/GUI/GLCanvas3D.cpp b/xs/src/slic3r/GUI/GLCanvas3D.cpp index 658e1731a..e64c7ac10 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.cpp @@ -1159,6 +1159,7 @@ bool GLCanvas3D::Gizmos::init(GLCanvas3D& parent) if (!gizmo->init()) return false; +#if !ENABLE_MODELINSTANCE_3D_SCALE // temporary disable x grabbers gizmo->disable_grabber(0); gizmo->disable_grabber(1); @@ -1168,6 +1169,7 @@ bool GLCanvas3D::Gizmos::init(GLCanvas3D& parent) // temporary disable z grabbers gizmo->disable_grabber(4); gizmo->disable_grabber(5); +#endif // !ENABLE_MODELINSTANCE_3D_SCALE m_gizmos.insert(GizmosMap::value_type(Scale, gizmo)); @@ -1419,6 +1421,26 @@ void GLCanvas3D::Gizmos::set_position(const Vec3d& position) reinterpret_cast(it->second)->set_position(position); } +#if ENABLE_MODELINSTANCE_3D_SCALE +Vec3d GLCanvas3D::Gizmos::get_scale() const +{ + if (!m_enabled) + return Vec3d::Ones(); + + GizmosMap::const_iterator it = m_gizmos.find(Scale); + return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_scale() : Vec3d::Ones(); +} + +void GLCanvas3D::Gizmos::set_scale(const Vec3d& scale) +{ + if (!m_enabled) + return; + + GizmosMap::const_iterator it = m_gizmos.find(Scale); + if (it != m_gizmos.end()) + reinterpret_cast(it->second)->set_scale(scale); +} +#else float GLCanvas3D::Gizmos::get_scale() const { if (!m_enabled) @@ -1437,6 +1459,7 @@ void GLCanvas3D::Gizmos::set_scale(float scale) if (it != m_gizmos.end()) reinterpret_cast(it->second)->set_scale(scale); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION Vec3d GLCanvas3D::Gizmos::get_rotation() const @@ -2431,7 +2454,11 @@ void GLCanvas3D::update_gizmos_data() #else m_gizmos.set_position(Vec3d(model_instance->offset(0), model_instance->offset(1), 0.0)); #endif // ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_SCALE + m_gizmos.set_scale(model_instance->get_scaling_factor()); +#else m_gizmos.set_scale(model_instance->scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION m_gizmos.set_rotation(model_instance->get_rotation()); #else @@ -2444,7 +2471,11 @@ void GLCanvas3D::update_gizmos_data() else { m_gizmos.set_position(Vec3d::Zero()); +#if ENABLE_MODELINSTANCE_3D_SCALE + m_gizmos.set_scale(Vec3d::Ones()); +#else m_gizmos.set_scale(1.0f); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION m_gizmos.set_rotation(Vec3d::Zero()); #else @@ -2806,11 +2837,19 @@ void GLCanvas3D::register_on_enable_action_buttons_callback(void* callback) m_on_enable_action_buttons_callback.register_callback(callback); } +#if ENABLE_MODELINSTANCE_3D_SCALE +void GLCanvas3D::register_on_gizmo_scale_3D_callback(void* callback) +{ + if (callback != nullptr) + m_on_gizmo_scale_3D_callback.register_callback(callback); +} +#else void GLCanvas3D::register_on_gizmo_scale_uniformly_callback(void* callback) { if (callback != nullptr) m_on_gizmo_scale_uniformly_callback.register_callback(callback); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION void GLCanvas3D::register_on_gizmo_rotate_3D_callback(void* callback) @@ -2838,11 +2877,19 @@ void GLCanvas3D::register_on_gizmo_flatten_callback(void* callback) } #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE +void GLCanvas3D::register_on_update_geometry_3D_info_callback(void* callback) +{ + if (callback != nullptr) + m_on_update_geometry_3D_info_callback.register_callback(callback); +} +#else void GLCanvas3D::register_on_update_geometry_info_callback(void* callback) { if (callback != nullptr) m_on_update_geometry_info_callback.register_callback(callback); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE void GLCanvas3D::register_action_add_callback(void* callback) { @@ -3123,7 +3170,12 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) { case Gizmos::Scale: { +#if ENABLE_MODELINSTANCE_3D_SCALE + const Vec3d& scale = m_gizmos.get_scale(); + m_on_gizmo_scale_3D_callback.call(scale(0), scale(1), scale(2)); +#else m_on_gizmo_scale_uniformly_callback.call((double)m_gizmos.get_scale()); +#endif // ENABLE_MODELINSTANCE_3D_SCALE update_scale_values(); m_dirty = true; break; @@ -3371,6 +3423,15 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) } case Gizmos::Scale: { +#if ENABLE_MODELINSTANCE_3D_SCALE + // Apply new temporary scale factors + const Vec3d& scale = m_gizmos.get_scale(); + for (GLVolume* v : volumes) + { + v->set_scaling_factor(scale); + } + update_scale_values(scale); +#else // Apply new temporary scale factor float scale_factor = m_gizmos.get_scale(); for (GLVolume* v : volumes) @@ -3378,13 +3439,14 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) v->set_scaling_factor((double)scale_factor); } update_scale_values((double)scale_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE break; } case Gizmos::Rotate: { #if ENABLE_MODELINSTANCE_3D_ROTATION // Apply new temporary rotation - Vec3d rotation = m_gizmos.get_rotation(); + const Vec3d& rotation = m_gizmos.get_rotation(); for (GLVolume* v : volumes) { v->set_rotation(rotation); @@ -3413,7 +3475,12 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) bb.merge(volume->transformed_bounding_box()); } const Vec3d& size = bb.size(); +#if ENABLE_MODELINSTANCE_3D_SCALE + const Vec3d& scale = m_gizmos.get_scale(); + m_on_update_geometry_3D_info_callback.call(size(0), size(1), size(2), scale(0), scale(1), scale(2)); +#else m_on_update_geometry_info_callback.call(size(0), size(1), size(2), m_gizmos.get_scale()); +#endif // ENABLE_MODELINSTANCE_3D_SCALE } m_dirty = true; @@ -3539,7 +3606,12 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) } case Gizmos::Scale: { +#if ENABLE_MODELINSTANCE_3D_SCALE + const Vec3d& scale = m_gizmos.get_scale(); + m_on_gizmo_scale_3D_callback.call(scale(0), scale(1), scale(2)); +#else m_on_gizmo_scale_uniformly_callback.call((double)m_gizmos.get_scale()); +#endif // ENABLE_MODELINSTANCE_3D_SCALE break; } case Gizmos::Rotate: @@ -3995,7 +4067,11 @@ void GLCanvas3D::_deregister_callbacks() m_on_instance_moved_callback.deregister_callback(); m_on_wipe_tower_moved_callback.deregister_callback(); m_on_enable_action_buttons_callback.deregister_callback(); +#if ENABLE_MODELINSTANCE_3D_SCALE + m_on_gizmo_scale_3D_callback.deregister_callback(); +#else m_on_gizmo_scale_uniformly_callback.deregister_callback(); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION m_on_gizmo_rotate_3D_callback.deregister_callback(); m_on_gizmo_flatten_3D_callback.deregister_callback(); @@ -4003,7 +4079,11 @@ void GLCanvas3D::_deregister_callbacks() m_on_gizmo_rotate_callback.deregister_callback(); m_on_gizmo_flatten_callback.deregister_callback(); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + m_on_update_geometry_3D_info_callback.deregister_callback(); +#else m_on_update_geometry_info_callback.deregister_callback(); +#endif // ENABLE_MODELINSTANCE_3D_SCALE m_action_add_callback.deregister_callback(); m_action_delete_callback.deregister_callback(); diff --git a/xs/src/slic3r/GUI/GLCanvas3D.hpp b/xs/src/slic3r/GUI/GLCanvas3D.hpp index 712ffbefc..4458ca8b8 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.hpp @@ -384,8 +384,13 @@ class GLCanvas3D Vec3d get_position() const; void set_position(const Vec3d& position); +#if ENABLE_MODELINSTANCE_3D_SCALE + Vec3d get_scale() const; + void set_scale(const Vec3d& scale); +#else float get_scale() const; void set_scale(float scale); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION Vec3d get_rotation() const; @@ -515,7 +520,11 @@ class GLCanvas3D PerlCallback m_on_instance_moved_callback; PerlCallback m_on_wipe_tower_moved_callback; PerlCallback m_on_enable_action_buttons_callback; +#if ENABLE_MODELINSTANCE_3D_SCALE + PerlCallback m_on_gizmo_scale_3D_callback; +#else PerlCallback m_on_gizmo_scale_uniformly_callback; +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION PerlCallback m_on_gizmo_rotate_3D_callback; PerlCallback m_on_gizmo_flatten_3D_callback; @@ -523,7 +532,11 @@ class GLCanvas3D PerlCallback m_on_gizmo_rotate_callback; PerlCallback m_on_gizmo_flatten_callback; #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + PerlCallback m_on_update_geometry_3D_info_callback; +#else PerlCallback m_on_update_geometry_info_callback; +#endif // ENABLE_MODELINSTANCE_3D_SCALE PerlCallback m_action_add_callback; PerlCallback m_action_delete_callback; @@ -645,7 +658,11 @@ public: void register_on_instance_moved_callback(void* callback); void register_on_wipe_tower_moved_callback(void* callback); void register_on_enable_action_buttons_callback(void* callback); +#if ENABLE_MODELINSTANCE_3D_SCALE + void register_on_gizmo_scale_3D_callback(void* callback); +#else void register_on_gizmo_scale_uniformly_callback(void* callback); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION void register_on_gizmo_rotate_3D_callback(void* callback); void register_on_gizmo_flatten_3D_callback(void* callback); @@ -653,7 +670,11 @@ public: void register_on_gizmo_rotate_callback(void* callback); void register_on_gizmo_flatten_callback(void* callback); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + void register_on_update_geometry_3D_info_callback(void* callback); +#else void register_on_update_geometry_info_callback(void* callback); +#endif // ENABLE_MODELINSTANCE_3D_SCALE void register_action_add_callback(void* callback); void register_action_delete_callback(void* callback); diff --git a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp index 1ac588f6c..35c36dd6b 100644 --- a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp @@ -692,12 +692,21 @@ void GLCanvas3DManager::register_on_enable_action_buttons_callback(wxGLCanvas* c it->second->register_on_enable_action_buttons_callback(callback); } +#if ENABLE_MODELINSTANCE_3D_SCALE +void GLCanvas3DManager::register_on_gizmo_scale_3D_callback(wxGLCanvas* canvas, void* callback) +{ + CanvasesMap::iterator it = _get_canvas(canvas); + if (it != m_canvases.end()) + it->second->register_on_gizmo_scale_3D_callback(callback); +} +#else void GLCanvas3DManager::register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback) { CanvasesMap::iterator it = _get_canvas(canvas); if (it != m_canvases.end()) it->second->register_on_gizmo_scale_uniformly_callback(callback); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION void GLCanvas3DManager::register_on_gizmo_rotate_3D_callback(wxGLCanvas* canvas, void* callback) @@ -729,12 +738,21 @@ void GLCanvas3DManager::register_on_gizmo_flatten_callback(wxGLCanvas* canvas, v } #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE +void GLCanvas3DManager::register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback) +{ + CanvasesMap::iterator it = _get_canvas(canvas); + if (it != m_canvases.end()) + it->second->register_on_update_geometry_3D_info_callback(callback); +} +#else void GLCanvas3DManager::register_on_update_geometry_info_callback(wxGLCanvas* canvas, void* callback) { CanvasesMap::iterator it = _get_canvas(canvas); if (it != m_canvases.end()) it->second->register_on_update_geometry_info_callback(callback); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE void GLCanvas3DManager::register_action_add_callback(wxGLCanvas* canvas, void* callback) { diff --git a/xs/src/slic3r/GUI/GLCanvas3DManager.hpp b/xs/src/slic3r/GUI/GLCanvas3DManager.hpp index 2331ec3f5..05b26ebff 100644 --- a/xs/src/slic3r/GUI/GLCanvas3DManager.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3DManager.hpp @@ -162,7 +162,11 @@ public: void register_on_instance_moved_callback(wxGLCanvas* canvas, void* callback); void register_on_wipe_tower_moved_callback(wxGLCanvas* canvas, void* callback); void register_on_enable_action_buttons_callback(wxGLCanvas* canvas, void* callback); +#if ENABLE_MODELINSTANCE_3D_SCALE + void register_on_gizmo_scale_3D_callback(wxGLCanvas* canvas, void* callback); +#else void register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_ROTATION void register_on_gizmo_rotate_3D_callback(wxGLCanvas* canvas, void* callback); void register_on_gizmo_flatten_3D_callback(wxGLCanvas* canvas, void* callback); @@ -170,7 +174,11 @@ public: void register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback); void register_on_gizmo_flatten_callback(wxGLCanvas* canvas, void* callback); #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + void register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback); +#else void register_on_update_geometry_info_callback(wxGLCanvas* canvas, void* callback); +#endif // ENABLE_MODELINSTANCE_3D_SCALE void register_action_add_callback(wxGLCanvas* canvas, void* callback); void register_action_delete_callback(wxGLCanvas* canvas, void* callback); diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index 599333898..5c6f6b00c 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -987,8 +987,11 @@ void GLGizmoScale3D::do_scale_y(const Linef3& mouse_ray) double ratio = calc_ratio(2, mouse_ray, m_starting_box.center()); if (ratio > 0.0) - m_scale(0) = m_starting_scale(1) * ratio; // << this is temporary -// m_scale(1) = m_starting_scale(1) * ratio; +#if ENABLE_MODELINSTANCE_3D_SCALE + m_scale(1) = m_starting_scale(1) * ratio; +#else + m_scale(0) = m_starting_scale(1) * ratio; +#endif // ENABLE_MODELINSTANCE_3D_SCALE } void GLGizmoScale3D::do_scale_z(const Linef3& mouse_ray) @@ -996,8 +999,11 @@ void GLGizmoScale3D::do_scale_z(const Linef3& mouse_ray) double ratio = calc_ratio(1, mouse_ray, m_starting_box.center()); if (ratio > 0.0) +#if ENABLE_MODELINSTANCE_3D_SCALE + m_scale(2) = m_starting_scale(2) * ratio; +#else m_scale(0) = m_starting_scale(2) * ratio; // << this is temporary -// m_scale(2) = m_starting_scale(2) * ratio; +#endif // ENABLE_MODELINSTANCE_3D_SCALE } void GLGizmoScale3D::do_scale_uniform(const Linef3& mouse_ray) @@ -1276,7 +1282,11 @@ void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const ::glRotated(inst.rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glRotated(inst.rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); ::glRotated(inst.rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); +#if ENABLE_MODELINSTANCE_3D_SCALE + ::glScaled(inst.scaling_factor(0), inst.scaling_factor(1), inst.scaling_factor(2)); +#else ::glScaled(inst.scaling_factor, inst.scaling_factor, inst.scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #else ::glTranslated(offset(0), offset(1), offset(2)); #endif // ENABLE_MODELINSTANCE_3D_ROTATION @@ -1317,7 +1327,11 @@ void GLGizmoFlatten::on_render_for_picking(const BoundingBoxf3& box) const ::glRotated(inst.rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glRotated(inst.rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); ::glRotated(inst.rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); +#if ENABLE_MODELINSTANCE_3D_SCALE + ::glScaled(inst.scaling_factor(0), inst.scaling_factor(1), inst.scaling_factor(2)); +#else ::glScaled(inst.scaling_factor, inst.scaling_factor, inst.scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #else ::glTranslated(offset(0), offset(1), offset(2)); #endif // ENABLE_MODELINSTANCE_3D_ROTATION @@ -1347,7 +1361,11 @@ void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object) for (const auto* instance : m_model_object->instances) #if ENABLE_MODELINSTANCE_3D_OFFSET #if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + m_instances.emplace_back(instance->get_offset(), instance->get_rotation(), instance->get_scaling_factor()); +#else m_instances.emplace_back(instance->get_offset(), instance->get_rotation(), instance->scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE #else m_instances_positions.emplace_back(instance->get_offset()); #endif // ENABLE_MODELINSTANCE_3D_ROTATION @@ -1576,11 +1594,20 @@ bool GLGizmoFlatten::is_plane_update_necessary() const #if ENABLE_MODELINSTANCE_3D_ROTATION Vec3d GLGizmoFlatten::get_flattening_rotation() const { +#if ENABLE_MODELINSTANCE_3D_SCALE + // calculates the rotations in model space, taking in account the scaling factors + Eigen::Matrix m = m_model_object->instances.front()->world_matrix(true, true).matrix().block(0, 0, 3, 3).inverse().transpose(); + Eigen::Quaterniond q; + Vec3d angles = q.setFromTwoVectors(m * m_normal, -Vec3d::UnitZ()).toRotationMatrix().eulerAngles(2, 1, 0); + m_normal = Vec3d::Zero(); + return Vec3d(angles(2), angles(1), angles(0)); +#else // calculates the rotations in model space Eigen::Quaterniond q; Vec3d angles = q.setFromTwoVectors(m_normal, -Vec3d::UnitZ()).toRotationMatrix().eulerAngles(2, 1, 0); m_normal = Vec3d::Zero(); return Vec3d(angles(2), angles(1), angles(0)); +#endif // ENABLE_MODELINSTANCE_3D_SCALE } #else Vec3d GLGizmoFlatten::get_flattening_normal() const { diff --git a/xs/src/slic3r/GUI/GLGizmo.hpp b/xs/src/slic3r/GUI/GLGizmo.hpp index 30f17a3f4..4f5c8b471 100644 --- a/xs/src/slic3r/GUI/GLGizmo.hpp +++ b/xs/src/slic3r/GUI/GLGizmo.hpp @@ -275,6 +275,10 @@ class GLGizmoScale3D : public GLGizmoBase public: explicit GLGizmoScale3D(GLCanvas3D& parent); +#if ENABLE_MODELINSTANCE_3D_SCALE + const Vec3d& get_scale() const { return m_scale; } + void set_scale(const Vec3d& scale) { m_starting_scale = scale; } +#else double get_scale_x() const { return m_scale(0); } void set_scale_x(double scale) { m_starting_scale(0) = scale; } @@ -285,6 +289,7 @@ public: void set_scale_z(double scale) { m_starting_scale(2) = scale; } void set_scale(double scale) { m_starting_scale = scale * Vec3d::Ones(); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE protected: virtual bool on_init(); @@ -365,10 +370,16 @@ private: struct InstanceData { Vec3d position; - Vec3d rotation; + Vec3d rotation; +#if ENABLE_MODELINSTANCE_3D_SCALE + Vec3d scaling_factor; + + InstanceData(const Vec3d& position, const Vec3d& rotation, const Vec3d& scaling_factor) : position(position), rotation(rotation), scaling_factor(scaling_factor) {} +#else double scaling_factor; InstanceData(const Vec3d& position, const Vec3d& rotation, double scaling_factor) : position(position), rotation(rotation), scaling_factor(scaling_factor) {} +#endif // ENABLE_MODELINSTANCE_3D_SCALE }; std::vector m_instances; #else diff --git a/xs/src/slic3r/GUI/GUI_ObjectParts.cpp b/xs/src/slic3r/GUI/GUI_ObjectParts.cpp index ec99c82a6..4d120cd00 100644 --- a/xs/src/slic3r/GUI/GUI_ObjectParts.cpp +++ b/xs/src/slic3r/GUI/GUI_ObjectParts.cpp @@ -1750,6 +1750,18 @@ void update_scale_values() auto instance = (*m_objects)[m_selected_object_id]->instances.front(); auto size = (*m_objects)[m_selected_object_id]->instance_bounding_box(0).size(); +#if ENABLE_MODELINSTANCE_3D_SCALE + if (g_is_percent_scale) { + og->set_value("scale_x", int(instance->get_scaling_factor(X) * 100)); + og->set_value("scale_y", int(instance->get_scaling_factor(Y) * 100)); + og->set_value("scale_z", int(instance->get_scaling_factor(Z) * 100)); + } + else { + og->set_value("scale_x", int(instance->get_scaling_factor(X) * size(0) + 0.5)); + og->set_value("scale_y", int(instance->get_scaling_factor(Y) * size(1) + 0.5)); + og->set_value("scale_z", int(instance->get_scaling_factor(Z) * size(2) + 0.5)); + } +#else if (g_is_percent_scale) { auto scale = instance->scaling_factor * 100.0; og->set_value("scale_x", int(scale)); @@ -1761,6 +1773,7 @@ void update_scale_values() og->set_value("scale_y", int(instance->scaling_factor * size(1) + 0.5)); og->set_value("scale_z", int(instance->scaling_factor * size(2) + 0.5)); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE } void update_position_values() @@ -1788,6 +1801,24 @@ void update_position_values(const Vec3d& position) og->set_value("position_z", int(position(2))); } +#if ENABLE_MODELINSTANCE_3D_SCALE +void update_scale_values(const Vec3d& scaling_factor) +{ + auto og = get_optgroup(ogFrequentlyObjectSettings); + + // this is temporary + // to be able to update the values as size + // we need to store somewhere the original size + // or have it passed as parameter + if (!g_is_percent_scale) + og->set_value("scale_unit", _("%")); + + auto scale = scaling_factor * 100.0; + og->set_value("scale_x", int(scale(0))); + og->set_value("scale_y", int(scale(1))); + og->set_value("scale_z", int(scale(2))); +} +#else void update_scale_values(double scaling_factor) { auto og = get_optgroup(ogFrequentlyObjectSettings); @@ -1804,6 +1835,7 @@ void update_scale_values(double scaling_factor) og->set_value("scale_y", int(scale)); og->set_value("scale_z", int(scale)); } +#endif // ENABLE_MODELINSTANCE_3D_SCALE void update_rotation_values() { diff --git a/xs/src/slic3r/GUI/GUI_ObjectParts.hpp b/xs/src/slic3r/GUI/GUI_ObjectParts.hpp index fdeb7a629..7e3bbbafe 100644 --- a/xs/src/slic3r/GUI/GUI_ObjectParts.hpp +++ b/xs/src/slic3r/GUI/GUI_ObjectParts.hpp @@ -119,7 +119,11 @@ void update_position_values(); void update_position_values(const Vec3d& position); // update scale values after scale unit changing or "gizmos" void update_scale_values(); +#if ENABLE_MODELINSTANCE_3D_SCALE +void update_scale_values(const Vec3d& scaling_factor); +#else void update_scale_values(double scaling_factor); +#endif // ENABLE_MODELINSTANCE_3D_SCALE // update rotation values object selection changing void update_rotation_values(); // update rotation value after "gizmos" diff --git a/xs/xsp/GUI_3DScene.xsp b/xs/xsp/GUI_3DScene.xsp index 0a3d11ed6..dc252d8bd 100644 --- a/xs/xsp/GUI_3DScene.xsp +++ b/xs/xsp/GUI_3DScene.xsp @@ -644,6 +644,13 @@ register_on_gizmo_scale_uniformly_callback(canvas, callback) CODE: _3DScene::register_on_gizmo_scale_uniformly_callback((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), (void*)callback); +void +register_on_gizmo_scale_3D_callback(canvas, callback) + SV *canvas; + SV *callback; + CODE: + _3DScene::register_on_gizmo_scale_3D_callback((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), (void*)callback); + void register_on_gizmo_rotate_callback(canvas, callback) SV *canvas; @@ -679,6 +686,13 @@ register_on_update_geometry_info_callback(canvas, callback) CODE: _3DScene::register_on_update_geometry_info_callback((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), (void*)callback); +void +register_on_update_geometry_3D_info_callback(canvas, callback) + SV *canvas; + SV *callback; + CODE: + _3DScene::register_on_update_geometry_3D_info_callback((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), (void*)callback); + void register_action_add_callback(canvas, callback) SV *canvas; diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index d5192cf8e..b92b55935 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -371,8 +371,13 @@ ModelMaterial::attributes() double rotation() %code%{ RETVAL = THIS->rotation; %}; #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + Vec3d* scaling_factor() + %code%{ RETVAL = new Vec3d(THIS->get_scaling_factor(X), THIS->get_scaling_factor(Y), THIS->get_scaling_factor(Z)); %}; +#else double scaling_factor() %code%{ RETVAL = THIS->scaling_factor; %}; +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_OFFSET Vec2d* offset() %code%{ RETVAL = new Vec2d(THIS->get_offset(X), THIS->get_offset(Y)); %}; @@ -387,13 +392,20 @@ ModelMaterial::attributes() void set_rotations(Vec3d *rotation) %code%{ THIS->set_rotation(*rotation); THIS->get_object()->invalidate_bounding_box(); %}; - #else void set_rotation(double val) %code%{ THIS->rotation = val; THIS->get_object()->invalidate_bounding_box(); %}; #endif // ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_SCALE + void set_scaling_factor(double val) + %code%{ THIS->set_scaling_factor(X, val); THIS->set_scaling_factor(Y, val); THIS->set_scaling_factor(Z, val); THIS->get_object()->invalidate_bounding_box(); %}; + + void set_scaling_factors(Vec3d *scale) + %code%{ THIS->set_scaling_factor(*scale); THIS->get_object()->invalidate_bounding_box(); %}; +#else void set_scaling_factor(double val) %code%{ THIS->scaling_factor = val; THIS->get_object()->invalidate_bounding_box(); %}; +#endif // ENABLE_MODELINSTANCE_3D_SCALE #if ENABLE_MODELINSTANCE_3D_OFFSET void set_offset(Vec2d *offset) %code%{ From 197600d452da7241da46bfd1f232e80c5f658a3c Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 25 Sep 2018 09:21:38 +0200 Subject: [PATCH 5/7] Test.pm modified to use ModelInstance full 3D transform --- lib/Slic3r/Test.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Slic3r/Test.pm b/lib/Slic3r/Test.pm index c0137f9e4..b767ca593 100644 --- a/lib/Slic3r/Test.pm +++ b/lib/Slic3r/Test.pm @@ -158,8 +158,12 @@ sub model { $object->add_volume(mesh => $mesh, material_id => $model_name); $object->add_instance( offset => Slic3r::Pointf->new(0,0), - rotation => $params{rotation} // 0, - scaling_factor => $params{scale} // 1, + # 3D full transform + rotation => Slic3r::Pointf3->new(0, 0, $params{rotation} // 0), + scaling_factor => Slic3r::Pointf3->new($params{scale} // 1, $params{scale} // 1, $params{scale} // 1), + # old transform +# rotation => $params{rotation} // 0, +# scaling_factor => $params{scale} // 1, ); return $model; } From a651f5f5d79eb0ec843e6f39e5649269c9e34626 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 25 Sep 2018 10:42:11 +0200 Subject: [PATCH 6/7] ModelInstance full 3D transform code moved into a single technology --- xs/src/libslic3r/Format/3mf.cpp | 28 ++-- xs/src/libslic3r/Format/AMF.cpp | 119 ++++++----------- xs/src/libslic3r/Format/PRUS.cpp | 50 +++---- xs/src/libslic3r/Model.cpp | 82 ++++++------ xs/src/libslic3r/Model.hpp | 40 +----- xs/src/libslic3r/ModelArrange.hpp | 36 +++--- xs/src/libslic3r/MultiPoint.cpp | 4 +- xs/src/libslic3r/MultiPoint.hpp | 4 +- xs/src/libslic3r/Print.cpp | 4 +- xs/src/libslic3r/PrintObject.cpp | 4 +- xs/src/libslic3r/Technologies.hpp | 9 +- xs/src/slic3r/GUI/3DScene.cpp | 99 ++++++-------- xs/src/slic3r/GUI/3DScene.hpp | 25 ++-- xs/src/slic3r/GUI/GLCanvas3D.cpp | 165 ++++++++++-------------- xs/src/slic3r/GUI/GLCanvas3D.hpp | 53 +++----- xs/src/slic3r/GUI/GLCanvas3DManager.cpp | 36 +++--- xs/src/slic3r/GUI/GLCanvas3DManager.hpp | 16 +-- xs/src/slic3r/GUI/GLGizmo.cpp | 89 ++++--------- xs/src/slic3r/GUI/GLGizmo.hpp | 30 ++--- xs/src/slic3r/GUI/GUI_ObjectParts.cpp | 20 +-- xs/src/slic3r/GUI/GUI_ObjectParts.hpp | 8 +- xs/xsp/Model.xsp | 52 ++++---- 22 files changed, 369 insertions(+), 604 deletions(-) diff --git a/xs/src/libslic3r/Format/3mf.cpp b/xs/src/libslic3r/Format/3mf.cpp index 79863f074..4c21a4e8f 100644 --- a/xs/src/libslic3r/Format/3mf.cpp +++ b/xs/src/libslic3r/Format/3mf.cpp @@ -1253,13 +1253,13 @@ namespace Slic3r { // we extract from the given matrix only the values currently used // translation -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d offset(transform(0, 3), transform(1, 3), transform(2, 3)); #else double offset_x = transform(0, 3); double offset_y = transform(1, 3); double offset_z = transform(2, 3); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // scale double sx = ::sqrt(sqr(transform(0, 0)) + sqr(transform(1, 0)) + sqr(transform(2, 0))); @@ -1270,11 +1270,11 @@ namespace Slic3r { if ((sx == 0.0) || (sy == 0.0) || (sz == 0.0)) return; -#if !ENABLE_MODELINSTANCE_3D_SCALE +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // non-uniform scale value, return if ((std::abs(sx - sy) > 0.00001) || (std::abs(sx - sz) > 0.00001)) return; -#endif // !ENABLE_MODELINSTANCE_3D_SCALE +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM double inv_sx = 1.0 / sx; double inv_sy = 1.0 / sy; @@ -1285,9 +1285,13 @@ namespace Slic3r { transform(1, 0) * inv_sx, transform(1, 1) * inv_sy, transform(1, 2) * inv_sz, transform(2, 0) * inv_sx, transform(2, 1) * inv_sy, transform(2, 2) * inv_sz; -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d angles = m3x3.eulerAngles(2, 1, 0); Vec3d rotation(angles(2), angles(1), angles(0)); + + instance.set_offset(offset); + instance.set_scaling_factor(Vec3d(sx, sy, sz)); + instance.set_rotation(rotation); #else Eigen::AngleAxisd rotation; rotation.fromRotationMatrix(m3x3); @@ -1297,24 +1301,12 @@ namespace Slic3r { return; double angle_z = (rotation.axis() == Vec3d::UnitZ()) ? rotation.angle() : -rotation.angle(); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_OFFSET - instance.set_offset(offset); -#else instance.offset(0) = offset_x; instance.offset(1) = offset_y; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_SCALE - instance.set_scaling_factor(Vec3d(sx, sy, sz)); -#else instance.scaling_factor = sx; -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION - instance.set_rotation(rotation); -#else instance.rotation = angle_z; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } bool _3MF_Importer::_handle_start_config(const char** attributes, unsigned int num_attributes) diff --git a/xs/src/libslic3r/Format/AMF.cpp b/xs/src/libslic3r/Format/AMF.cpp index 1f2298d9b..2b7cc5c74 100644 --- a/xs/src/libslic3r/Format/AMF.cpp +++ b/xs/src/libslic3r/Format/AMF.cpp @@ -29,23 +29,14 @@ // VERSION NUMBERS // 0 : .amf, .amf.xml and .zip.amf files saved by older slic3r. No version definition in them. // 1 : Introduction of amf versioning. No other change in data saved into amf files. -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // 2 : Added z component of offset // Added x and y components of rotation // Added x, y and z components of scale -#else -// 2 : Added z component of offset -// Added x and y components of rotation -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#else -// 2 : Added z component of offset. -#endif // ENABLE_MODELINSTANCE_3D_ROTATION const unsigned int VERSION_AMF = 2; #else const unsigned int VERSION_AMF = 1; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const char* SLIC3RPE_AMF_VERSION = "slic3rpe_amf_version"; const char* SLIC3R_CONFIG_TYPE = "slic3rpe_config"; @@ -135,60 +126,48 @@ struct AMFParserContext NODE_TYPE_INSTANCE, // amf/constellation/instance NODE_TYPE_DELTAX, // amf/constellation/instance/deltax NODE_TYPE_DELTAY, // amf/constellation/instance/deltay -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM NODE_TYPE_DELTAZ, // amf/constellation/instance/deltaz -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION NODE_TYPE_RX, // amf/constellation/instance/rx NODE_TYPE_RY, // amf/constellation/instance/ry -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM NODE_TYPE_RZ, // amf/constellation/instance/rz NODE_TYPE_SCALE, // amf/constellation/instance/scale -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM NODE_TYPE_SCALEX, // amf/constellation/instance/scalex NODE_TYPE_SCALEY, // amf/constellation/instance/scaley NODE_TYPE_SCALEZ, // amf/constellation/instance/scalez -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM NODE_TYPE_METADATA, // anywhere under amf/*/metadata }; struct Instance { -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rx_set(false), ry_set(false), rz_set(false), scalex_set(false), scaley_set(false), scalez_set(false) {} -#else - Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rx_set(false), ry_set(false), rz_set(false), scale_set(false) {} -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#else - Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rz_set(false), scale_set(false) {} -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else Instance() : deltax_set(false), deltay_set(false), rz_set(false), scale_set(false) {} -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Shift in the X axis. float deltax; bool deltax_set; // Shift in the Y axis. float deltay; bool deltay_set; -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Shift in the Z axis. float deltaz; bool deltaz_set; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION // Rotation around the X axis. float rx; bool rx_set; // Rotation around the Y axis. float ry; bool ry_set; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Rotation around the Z axis. float rz; bool rz_set; -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Scaling factors float scalex; bool scalex_set; @@ -200,7 +179,7 @@ struct AMFParserContext // Scaling factor float scale; bool scale_set; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM }; struct Object { @@ -317,26 +296,24 @@ void AMFParserContext::startElement(const char *name, const char **atts) node_type_new = NODE_TYPE_DELTAX; else if (strcmp(name, "deltay") == 0) node_type_new = NODE_TYPE_DELTAY; -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM else if (strcmp(name, "deltaz") == 0) node_type_new = NODE_TYPE_DELTAZ; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION else if (strcmp(name, "rx") == 0) node_type_new = NODE_TYPE_RX; else if (strcmp(name, "ry") == 0) node_type_new = NODE_TYPE_RY; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM else if (strcmp(name, "rz") == 0) node_type_new = NODE_TYPE_RZ; -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM else if (strcmp(name, "scalex") == 0) node_type_new = NODE_TYPE_SCALEX; else if (strcmp(name, "scaley") == 0) node_type_new = NODE_TYPE_SCALEY; else if (strcmp(name, "scalez") == 0) node_type_new = NODE_TYPE_SCALEZ; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM else if (strcmp(name, "scale") == 0) node_type_new = NODE_TYPE_SCALE; } @@ -395,26 +372,20 @@ void AMFParserContext::characters(const XML_Char *s, int len) { switch (m_path.size()) { case 4: -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (m_path.back() == NODE_TYPE_DELTAX || m_path.back() == NODE_TYPE_DELTAY || m_path.back() == NODE_TYPE_DELTAZ || -#if ENABLE_MODELINSTANCE_3D_ROTATION m_path.back() == NODE_TYPE_RX || m_path.back() == NODE_TYPE_RY || -#endif // ENABLE_MODELINSTANCE_3D_ROTATION m_path.back() == NODE_TYPE_RZ || -#if ENABLE_MODELINSTANCE_3D_SCALE m_path.back() == NODE_TYPE_SCALEX || m_path.back() == NODE_TYPE_SCALEY || m_path.back() == NODE_TYPE_SCALEZ || m_path.back() == NODE_TYPE_SCALE) -#else - m_path.back() == NODE_TYPE_SCALE) -#endif // ENABLE_MODELINSTANCE_3D_SCALE #else if (m_path.back() == NODE_TYPE_DELTAX || m_path.back() == NODE_TYPE_DELTAY || m_path.back() == NODE_TYPE_RZ || m_path.back() == NODE_TYPE_SCALE) -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_value[0].append(s, len); break; case 6: @@ -454,15 +425,13 @@ void AMFParserContext::endElement(const char * /* name */) m_instance->deltay_set = true; m_value[0].clear(); break; -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM case NODE_TYPE_DELTAZ: assert(m_instance); m_instance->deltaz = float(atof(m_value[0].c_str())); m_instance->deltaz_set = true; m_value[0].clear(); break; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION case NODE_TYPE_RX: assert(m_instance); m_instance->rx = float(atof(m_value[0].c_str())); @@ -475,7 +444,7 @@ void AMFParserContext::endElement(const char * /* name */) m_instance->ry_set = true; m_value[0].clear(); break; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM case NODE_TYPE_RZ: assert(m_instance); m_instance->rz = float(atof(m_value[0].c_str())); @@ -484,7 +453,7 @@ void AMFParserContext::endElement(const char * /* name */) break; case NODE_TYPE_SCALE: assert(m_instance); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_instance->scalex = float(atof(m_value[0].c_str())); m_instance->scalex_set = true; m_instance->scaley = float(atof(m_value[0].c_str())); @@ -494,10 +463,10 @@ void AMFParserContext::endElement(const char * /* name */) #else m_instance->scale = float(atof(m_value[0].c_str())); m_instance->scale_set = true; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_value[0].clear(); break; -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM case NODE_TYPE_SCALEX: assert(m_instance); m_instance->scalex = float(atof(m_value[0].c_str())); @@ -516,7 +485,7 @@ void AMFParserContext::endElement(const char * /* name */) m_instance->scalez_set = true; m_value[0].clear(); break; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Object vertices: case NODE_TYPE_VERTEX: @@ -654,22 +623,16 @@ void AMFParserContext::endDocument() for (const Instance &instance : object.second.instances) if (instance.deltax_set && instance.deltay_set) { ModelInstance *mi = m_model.objects[object.second.idx]->add_instance(); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM mi->set_offset(Vec3d(instance.deltax_set ? (double)instance.deltax : 0.0, instance.deltay_set ? (double)instance.deltay : 0.0, instance.deltaz_set ? (double)instance.deltaz : 0.0)); + mi->set_rotation(Vec3d(instance.rx_set ? (double)instance.rx : 0.0, instance.ry_set ? (double)instance.ry : 0.0, instance.rz_set ? (double)instance.rz : 0.0)); + mi->set_scaling_factor(Vec3d(instance.scalex_set ? (double)instance.scalex : 1.0, instance.scaley_set ? (double)instance.scaley : 1.0, instance.scalez_set ? (double)instance.scalez : 1.0)); #else mi->offset(0) = instance.deltax; mi->offset(1) = instance.deltay; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION - mi->set_rotation(Vec3d(instance.rx_set ? (double)instance.rx : 0.0, instance.ry_set ? (double)instance.ry : 0.0, instance.rz_set ? (double)instance.rz : 0.0)); -#else mi->rotation = instance.rz_set ? instance.rz : 0.f; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE - mi->set_scaling_factor(Vec3d(instance.scalex_set ? (double)instance.scalex : 1.0, instance.scaley_set ? (double)instance.scaley : 1.0, instance.scalez_set ? (double)instance.scalez : 1.0)); -#else mi->scaling_factor = instance.scale_set ? instance.scale : 1.f; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } } } @@ -969,45 +932,37 @@ bool store_amf(const char *path, Model *model, Print* print, bool export_print_c " \n" " %lf\n" " %lf\n" -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM " %lf\n" -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION " %lf\n" " %lf\n" -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM " %lf\n" -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM " %lf\n" " %lf\n" " %lf\n" #else " %lf\n" -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM " \n", object_id, -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM instance->get_offset(X), instance->get_offset(Y), instance->get_offset(Z), -#else - instance->offset(0), - instance->offset(1), -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION instance->get_rotation(X), instance->get_rotation(Y), instance->get_rotation(Z), -#else - instance->rotation, -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE instance->get_scaling_factor(X), instance->get_scaling_factor(Y), instance->get_scaling_factor(Z)); #else + instance->offset(0), + instance->offset(1), + instance->rotation, instance->scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM //FIXME missing instance->scaling_factor instances.append(buf); diff --git a/xs/src/libslic3r/Format/PRUS.cpp b/xs/src/libslic3r/Format/PRUS.cpp index 385e70b5d..5b2758331 100644 --- a/xs/src/libslic3r/Format/PRUS.cpp +++ b/xs/src/libslic3r/Format/PRUS.cpp @@ -164,21 +164,15 @@ bool load_prus(const char *path, Model *model) const char *zero_tag = ""; const char *zero_xml = strstr(scene_xml_data.data(), zero_tag); float trafo[3][4] = { 0 }; -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d instance_rotation = Vec3d::Zero(); -#else - double instance_rotation = 0.; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE Vec3d instance_scaling_factor = Vec3d::Ones(); -#else - double instance_scaling_factor = 1.f; -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_OFFSET Vec3d instance_offset = Vec3d::Zero(); #else + double instance_rotation = 0.; + double instance_scaling_factor = 1.f; Vec2d instance_offset(0., 0.); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM bool trafo_set = false; unsigned int group_id = (unsigned int)-1; unsigned int extruder_id = (unsigned int)-1; @@ -201,22 +195,19 @@ bool load_prus(const char *path, Model *model) "[%f, %f, %f]", scale, scale+1, scale+2) == 3 && sscanf(zero_xml+strlen(zero_tag), "[%f, %f, %f]", zero, zero+1, zero+2) == 3) { -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM instance_scaling_factor = Vec3d((double)scale[0], (double)scale[1], (double)scale[2]); + instance_rotation = Vec3d(-(double)rotation[0], -(double)rotation[1], -(double)rotation[2]); #else if (scale[0] == scale[1] && scale[1] == scale[2]) { instance_scaling_factor = scale[0]; scale[0] = scale[1] = scale[2] = 1.; } -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION - instance_rotation = Vec3d(-(double)rotation[0], -(double)rotation[1], -(double)rotation[2]); -#else if (rotation[0] == 0. && rotation[1] == 0.) { - instance_rotation = - rotation[2]; + instance_rotation = -rotation[2]; rotation[2] = 0.; } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Eigen::Matrix3f mat_rot, mat_scale, mat_trafo; mat_rot = Eigen::AngleAxisf(-rotation[2], Eigen::Vector3f::UnitZ()) * Eigen::AngleAxisf(-rotation[1], Eigen::Vector3f::UnitY()) * @@ -227,18 +218,15 @@ bool load_prus(const char *path, Model *model) for (size_t c = 0; c < 3; ++ c) trafo[r][c] += mat_trafo(r, c); } -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM instance_offset = Vec3d((double)(position[0] - zero[0]), (double)(position[1] - zero[1]), (double)(position[2] - zero[2])); -#else - instance_offset(0) = position[0] - zero[0]; - instance_offset(1) = position[1] - zero[1]; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_SCALE // CHECK_ME -> Is the following correct ? trafo[2][3] = position[2] / instance_scaling_factor(2); #else + instance_offset(0) = position[0] - zero[0]; + instance_offset(1) = position[1] - zero[1]; trafo[2][3] = position[2] / instance_scaling_factor; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM trafo_set = true; } const char *group_tag = ""; @@ -387,21 +375,15 @@ bool load_prus(const char *path, Model *model) model_object = model->add_object(name_utf8.data(), path, std::move(mesh)); volume = model_object->volumes.front(); ModelInstance *instance = model_object->add_instance(); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM instance->set_rotation(instance_rotation); -#else - instance->rotation = instance_rotation; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE instance->set_scaling_factor(instance_scaling_factor); -#else - instance->scaling_factor = instance_scaling_factor; -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_OFFSET instance->set_offset(instance_offset); #else + instance->rotation = instance_rotation; + instance->scaling_factor = instance_scaling_factor; instance->offset = instance_offset; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ++num_models; if (group_id != (size_t)-1) group_to_model_object[group_id] = model_object; diff --git a/xs/src/libslic3r/Model.cpp b/xs/src/libslic3r/Model.cpp index 0f8c3665d..bd96aa3b9 100644 --- a/xs/src/libslic3r/Model.cpp +++ b/xs/src/libslic3r/Model.cpp @@ -242,19 +242,19 @@ void Model::center_instances_around_point(const Vec2d &point) for (size_t i = 0; i < o->instances.size(); ++ i) bb.merge(o->instance_bounding_box(i, false)); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec2d shift2 = point - to_2d(bb.center()); Vec3d shift3 = Vec3d(shift2(0), shift2(1), 0.0); #else Vec2d shift = point - 0.5 * to_2d(bb.size()) - to_2d(bb.min); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (ModelObject *o : this->objects) { for (ModelInstance *i : o->instances) -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM i->set_offset(i->get_offset() + shift3); #else i->offset += shift; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM o->invalidate_bounding_box(); } } @@ -320,12 +320,12 @@ bool Model::arrange_objects(coordf_t dist, const BoundingBoxf* bb) size_t idx = 0; for (ModelObject *o : this->objects) { for (ModelInstance *i : o->instances) { -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec2d offset_xy = positions[idx] - instance_centers[idx]; i->set_offset(Vec3d(offset_xy(0), offset_xy(1), i->get_offset(Z))); #else i->offset = positions[idx] - instance_centers[idx]; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ++idx; } o->invalidate_bounding_box(); @@ -350,11 +350,11 @@ void Model::duplicate(size_t copies_num, coordf_t dist, const BoundingBoxf* bb) for (const ModelInstance *i : instances) { for (const Vec2d &pos : positions) { ModelInstance *instance = o->add_instance(*i); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM instance->set_offset(instance->get_offset() + Vec3d(pos(0), pos(1), 0.0)); #else instance->offset += pos; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } } o->invalidate_bounding_box(); @@ -384,21 +384,21 @@ void Model::duplicate_objects_grid(size_t x, size_t y, coordf_t dist) ModelObject* object = this->objects.front(); object->clear_instances(); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d ext_size = object->bounding_box().size() + dist * Vec3d::Ones(); #else Vec3d size = object->bounding_box().size(); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (size_t x_copy = 1; x_copy <= x; ++x_copy) { for (size_t y_copy = 1; y_copy <= y; ++y_copy) { ModelInstance* instance = object->add_instance(); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM instance->set_offset(Vec3d(ext_size(0) * (double)(x_copy - 1), ext_size(1) * (double)(y_copy - 1), 0.0)); #else instance->offset(0) = (size(0) + dist) * (x_copy - 1); instance->offset(1) = (size(1) + dist) * (y_copy - 1); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } } } @@ -704,21 +704,21 @@ void ModelObject::center_around_origin() this->translate(shift); this->origin_translation += shift; -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // set z to zero, translation in z has already been done within the mesh shift(2) = 0.0; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (!this->instances.empty()) { for (ModelInstance *i : this->instances) { // apply rotation and scaling to vector as well before translating instance, // in order to leave final position unaltered -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM i->set_offset(i->get_offset() + i->transform_vector(-shift, true)); #else Vec3d i_shift = i->world_matrix(true) * shift; i->offset -= to_2d(i_shift); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } this->invalidate_bounding_box(); } @@ -1054,7 +1054,7 @@ size_t ModelVolume::split(unsigned int max_extruders) return idx; } -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void ModelInstance::set_rotation(const Vec3d& rotation) { set_rotation(X, rotation(0)); @@ -1075,7 +1075,7 @@ void ModelInstance::set_rotation(Axis axis, double rotation) } m_rotation(axis) = rotation; } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void ModelInstance::transform_mesh(TriangleMesh* mesh, bool dont_translate) const { @@ -1090,7 +1090,7 @@ BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mes BoundingBoxf3 bbox = copy.bounding_box(); if (!empty(bbox)) { -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Scale the bounding box along the three axes. for (unsigned int i = 0; i < 3; ++i) { @@ -1100,23 +1100,25 @@ BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mes bbox.max(i) *= this->m_scaling_factor(i); } } + + // Translate the bounding box. + if (! dont_translate) { + bbox.min += this->m_offset; + bbox.max += this->m_offset; + } #else // Scale the bounding box uniformly. if (std::abs(this->scaling_factor - 1.) > EPSILON) { bbox.min *= this->scaling_factor; bbox.max *= this->scaling_factor; } -#endif // ENABLE_MODELINSTANCE_3D_SCALE + // Translate the bounding box. - if (! dont_translate) { -#if ENABLE_MODELINSTANCE_3D_OFFSET - bbox.min += this->m_offset; - bbox.max += this->m_offset; -#else + if (!dont_translate) { Eigen::Map(bbox.min.data()) += this->offset; Eigen::Map(bbox.max.data()) += this->offset; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET } +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } return bbox; } @@ -1133,48 +1135,44 @@ Vec3d ModelInstance::transform_vector(const Vec3d& v, bool dont_translate) const void ModelInstance::transform_polygon(Polygon* polygon) const { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // CHECK_ME -> Is the following correct or it should take in account all three rotations ? polygon->rotate(this->m_rotation(2)); // rotate around polygon origin -#else - polygon->rotate(this->rotation); // rotate around polygon origin -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE // CHECK_ME -> Is the following correct ? polygon->scale(this->m_scaling_factor(0), this->m_scaling_factor(1)); // scale around polygon origin #else + polygon->rotate(this->rotation); // rotate around polygon origin polygon->scale(this->scaling_factor); // scale around polygon origin -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } Transform3d ModelInstance::world_matrix(bool dont_translate, bool dont_rotate, bool dont_scale) const { Transform3d m = Transform3d::Identity(); +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (!dont_translate) -#if ENABLE_MODELINSTANCE_3D_OFFSET m.translate(m_offset); -#else - m.translate(Vec3d(offset(0), offset(1), 0.0)); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET if (!dont_rotate) -#if ENABLE_MODELINSTANCE_3D_ROTATION { m.rotate(Eigen::AngleAxisd(m_rotation(2), Vec3d::UnitZ())); m.rotate(Eigen::AngleAxisd(m_rotation(1), Vec3d::UnitY())); m.rotate(Eigen::AngleAxisd(m_rotation(0), Vec3d::UnitX())); } -#else - m.rotate(Eigen::AngleAxisd(rotation, Vec3d::UnitZ())); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION if (!dont_scale) -#if ENABLE_MODELINSTANCE_3D_SCALE m.scale(m_scaling_factor); #else + if (!dont_translate) + m.translate(Vec3d(offset(0), offset(1), 0.0)); + + if (!dont_rotate) + m.rotate(Eigen::AngleAxisd(rotation, Vec3d::UnitZ())); + + if (!dont_scale) m.scale(scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM return m; } diff --git a/xs/src/libslic3r/Model.hpp b/xs/src/libslic3r/Model.hpp index e3dc0b522..7a186df5d 100644 --- a/xs/src/libslic3r/Model.hpp +++ b/xs/src/libslic3r/Model.hpp @@ -243,57 +243,43 @@ public: friend class ModelObject; -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM private: Vec3d m_offset; // in unscaled coordinates -#if ENABLE_MODELINSTANCE_3D_ROTATION Vec3d m_rotation; // Rotation around the three axes, in radians around mesh center point -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE Vec3d m_scaling_factor; // Scaling factors along the three axes -#endif // ENABLE_MODELINSTANCE_3D_SCALE public: -#endif // ENABLE_MODELINSTANCE_3D_OFFSET - -#if !ENABLE_MODELINSTANCE_3D_ROTATION +#else double rotation; // Rotation around the Z axis, in radians around mesh center point -#endif // !ENABLE_MODELINSTANCE_3D_ROTATION -#if !ENABLE_MODELINSTANCE_3D_SCALE double scaling_factor; -#endif // !ENABLE_MODELINSTANCE_3D_SCALE -#if !ENABLE_MODELINSTANCE_3D_OFFSET Vec2d offset; // in unscaled coordinates -#endif // !ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // flag showing the position of this instance with respect to the print volume (set by Print::validate() using ModelObject::check_instances_print_volume_state()) EPrintVolumeState print_volume_state; ModelObject* get_object() const { return this->object; } -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& get_offset() const { return m_offset; } double get_offset(Axis axis) const { return m_offset(axis); } void set_offset(const Vec3d& offset) { m_offset = offset; } void set_offset(Axis axis, double offset) { m_offset(axis) = offset; } -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION const Vec3d& get_rotation() const { return m_rotation; } double get_rotation(Axis axis) const { return m_rotation(axis); } void set_rotation(const Vec3d& rotation); void set_rotation(Axis axis, double rotation); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE Vec3d get_scaling_factor() const { return m_scaling_factor; } double get_scaling_factor(Axis axis) const { return m_scaling_factor(axis); } void set_scaling_factor(const Vec3d& scaling_factor) { m_scaling_factor = scaling_factor; } void set_scaling_factor(Axis axis, double scaling_factor) { m_scaling_factor(axis) = scaling_factor; } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // To be called on an external mesh void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const; @@ -314,27 +300,15 @@ private: // Parent object, owning this instance. ModelObject* object; -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ModelInstance(ModelObject *object) : m_rotation(Vec3d::Zero()), m_scaling_factor(Vec3d::Ones()), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {} ModelInstance(ModelObject *object, const ModelInstance &other) : m_rotation(other.m_rotation), m_scaling_factor(other.m_scaling_factor), m_offset(other.m_offset), object(object), print_volume_state(PVS_Inside) {} -#else - ModelInstance(ModelObject *object) : m_rotation(Vec3d::Zero()), scaling_factor(1), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {} - ModelInstance(ModelObject *object, const ModelInstance &other) : - m_rotation(other.m_rotation), scaling_factor(other.scaling_factor), m_offset(other.m_offset), object(object), print_volume_state(PVS_Inside) {} -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#else - ModelInstance(ModelObject *object) : rotation(0), scaling_factor(1), m_offset(Vec3d::Zero()), object(object), print_volume_state(PVS_Inside) {} - ModelInstance(ModelObject *object, const ModelInstance &other) : - rotation(other.rotation), scaling_factor(other.scaling_factor), m_offset(other.m_offset), object(object), print_volume_state(PVS_Inside) {} -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else ModelInstance(ModelObject *object) : rotation(0), scaling_factor(1), offset(Vec2d::Zero()), object(object), print_volume_state(PVS_Inside) {} ModelInstance(ModelObject *object, const ModelInstance &other) : rotation(other.rotation), scaling_factor(other.scaling_factor), offset(other.offset), object(object), print_volume_state(PVS_Inside) {} -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM }; diff --git a/xs/src/libslic3r/ModelArrange.hpp b/xs/src/libslic3r/ModelArrange.hpp index 7e2de2bf1..f8beb668a 100644 --- a/xs/src/libslic3r/ModelArrange.hpp +++ b/xs/src/libslic3r/ModelArrange.hpp @@ -29,12 +29,12 @@ std::string toString(const Model& model, bool holes = true) { if(!objinst) continue; Slic3r::TriangleMesh tmpmesh = rmesh; -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // CHECK_ME -> Is the following correct ? tmpmesh.scale(objinst->get_scaling_factor()); #else tmpmesh.scale(objinst->scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM objinst->transform_mesh(&tmpmesh); ExPolygons expolys = tmpmesh.horizontal_projection(); for(auto& expoly_complex : expolys) { @@ -92,11 +92,11 @@ void toSVG(SVG& svg, const Model& model) { if(!objinst) continue; Slic3r::TriangleMesh tmpmesh = rmesh; -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM tmpmesh.scale(objinst->get_scaling_factor()); #else tmpmesh.scale(objinst->scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM objinst->transform_mesh(&tmpmesh); ExPolygons expolys = tmpmesh.horizontal_projection(); svg.draw(expolys); @@ -522,12 +522,12 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model) { Slic3r::TriangleMesh tmpmesh = rmesh; ClipperLib::PolygonImpl pn; -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // CHECK_ME -> is the following correct ? tmpmesh.scale(objinst->get_scaling_factor()); #else tmpmesh.scale(objinst->scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // TODO export the exact 2D projection auto p = tmpmesh.convex_hull(); @@ -541,20 +541,20 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model) { // Invalid geometries would throw exceptions when arranging if(item.vertexCount() > 3) { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // CHECK_ME -> is the following correct or it should take in account all three rotations ? item.rotation(objinst->get_rotation(Z)); #else item.rotation(objinst->rotation); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM item.translation({ -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ClipperLib::cInt(objinst->get_offset(X)/SCALING_FACTOR), ClipperLib::cInt(objinst->get_offset(Y)/SCALING_FACTOR) #else ClipperLib::cInt(objinst->offset(0)/SCALING_FACTOR), ClipperLib::cInt(objinst->offset(1)/SCALING_FACTOR) -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM }); ret.emplace_back(objinst, item); } @@ -691,25 +691,21 @@ void applyResult( // appropriately auto off = item.translation(); Radians rot = item.rotation(); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d foff(off.X*SCALING_FACTOR + batch_offset, off.Y*SCALING_FACTOR, 0.0); -#else - Vec2d foff(off.X*SCALING_FACTOR + batch_offset, off.Y*SCALING_FACTOR); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET // write the transformation data into the model instance -#if ENABLE_MODELINSTANCE_3D_ROTATION inst_ptr->set_rotation(Z, rot); -#else - inst_ptr->rotation = rot; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_OFFSET inst_ptr->set_offset(foff); #else + Vec2d foff(off.X*SCALING_FACTOR + batch_offset, off.Y*SCALING_FACTOR); + + // write the transformation data into the model instance + inst_ptr->rotation = rot; inst_ptr->offset = foff; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } } diff --git a/xs/src/libslic3r/MultiPoint.cpp b/xs/src/libslic3r/MultiPoint.cpp index 48b2840c8..45b354d63 100644 --- a/xs/src/libslic3r/MultiPoint.cpp +++ b/xs/src/libslic3r/MultiPoint.cpp @@ -14,7 +14,7 @@ void MultiPoint::scale(double factor) pt *= factor; } -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void MultiPoint::scale(double factor_x, double factor_y) { for (Point &pt : points) @@ -23,7 +23,7 @@ void MultiPoint::scale(double factor_x, double factor_y) pt(1) *= factor_y; } } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void MultiPoint::translate(double x, double y) { diff --git a/xs/src/libslic3r/MultiPoint.hpp b/xs/src/libslic3r/MultiPoint.hpp index c96aa3814..288b3137e 100644 --- a/xs/src/libslic3r/MultiPoint.hpp +++ b/xs/src/libslic3r/MultiPoint.hpp @@ -26,9 +26,9 @@ public: MultiPoint& operator=(const MultiPoint &other) { points = other.points; return *this; } MultiPoint& operator=(MultiPoint &&other) { points = std::move(other.points); return *this; } void scale(double factor); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void scale(double factor_x, double factor_y); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void translate(double x, double y); void translate(const Point &vector); void rotate(double angle) { this->rotate(cos(angle), sin(angle)); } diff --git a/xs/src/libslic3r/Print.cpp b/xs/src/libslic3r/Print.cpp index 1a9b13049..37b55f87d 100644 --- a/xs/src/libslic3r/Print.cpp +++ b/xs/src/libslic3r/Print.cpp @@ -431,14 +431,14 @@ void Print::add_model_object(ModelObject* model_object, int idx) std::vector v_scale; for (const PrintObject *object : m_objects) { const ModelObject &mobj = *object->model_object(); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // CHECK_ME -> Is the following correct ? v_scale.push_back("x:" + boost::lexical_cast(mobj.instances[0]->get_scaling_factor(X) * 100) + "% y:" + boost::lexical_cast(mobj.instances[0]->get_scaling_factor(Y) * 100) + "% z:" + boost::lexical_cast(mobj.instances[0]->get_scaling_factor(Z) * 100) + "%"); #else v_scale.push_back(boost::lexical_cast(mobj.instances[0]->scaling_factor * 100) + "%"); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (input_file.empty()) input_file = mobj.input_file; } diff --git a/xs/src/libslic3r/PrintObject.cpp b/xs/src/libslic3r/PrintObject.cpp index ef2364dc4..4e72871fc 100644 --- a/xs/src/libslic3r/PrintObject.cpp +++ b/xs/src/libslic3r/PrintObject.cpp @@ -113,7 +113,7 @@ bool PrintObject::reload_model_instances() copies.reserve(m_model_object->instances.size()); for (const ModelInstance *mi : m_model_object->instances) { -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (mi->is_printable()) { const Vec3d& offset = mi->get_offset(); @@ -122,7 +122,7 @@ bool PrintObject::reload_model_instances() #else if (mi->is_printable()) copies.emplace_back(Point::new_scale(mi->offset(0), mi->offset(1))); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } return this->set_copies(copies); } diff --git a/xs/src/libslic3r/Technologies.hpp b/xs/src/libslic3r/Technologies.hpp index bf0c8b370..14e2a8bde 100644 --- a/xs/src/libslic3r/Technologies.hpp +++ b/xs/src/libslic3r/Technologies.hpp @@ -5,13 +5,12 @@ #define ENABLE_1_42_0 1 // Add z coordinate to model instances' offset -#define ENABLE_MODELINSTANCE_3D_OFFSET (1 && ENABLE_1_42_0) +// Add x and y rotation components to model instances' offset +// Add scaling factors for all the three axes to model instances +#define ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM (1 && ENABLE_1_42_0) // Add double click on gizmo grabbers to reset transformation components to their default value #define ENABLE_GIZMOS_RESET (1 && ENABLE_1_42_0) -// Add x and y rotation components to model instances' offset -#define ENABLE_MODELINSTANCE_3D_ROTATION (1 && ENABLE_MODELINSTANCE_3D_OFFSET) -// Add scaling factors for all the three axes to model instances -#define ENABLE_MODELINSTANCE_3D_SCALE (1 && ENABLE_MODELINSTANCE_3D_ROTATION) + #endif // _technologies_h_ diff --git a/xs/src/slic3r/GUI/3DScene.cpp b/xs/src/slic3r/GUI/3DScene.cpp index 2863796ae..02a35029e 100644 --- a/xs/src/slic3r/GUI/3DScene.cpp +++ b/xs/src/slic3r/GUI/3DScene.cpp @@ -196,16 +196,13 @@ const float GLVolume::SELECTED_OUTSIDE_COLOR[4] = { 0.19f, 0.58f, 1.0f, 1.0f }; GLVolume::GLVolume(float r, float g, float b, float a) : m_offset(Vec3d::Zero()) -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM , m_rotation(Vec3d::Zero()) -#else - , m_rotation(0.0) -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE , m_scaling_factor(Vec3d::Ones()) #else + , m_rotation(0.0) , m_scaling_factor(1.0) -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM , m_world_matrix(Transform3f::Identity()) , m_world_matrix_dirty(true) , m_transformed_bounding_box_dirty(true) @@ -263,7 +260,7 @@ void GLVolume::set_render_color() set_render_color(color, 4); } -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& GLVolume::get_rotation() const { return m_rotation; @@ -295,7 +292,7 @@ void GLVolume::set_rotation(double rotation) m_transformed_convex_hull_bounding_box_dirty = true; } } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& GLVolume::get_offset() const { @@ -313,7 +310,7 @@ void GLVolume::set_offset(const Vec3d& offset) } } -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void GLVolume::set_scaling_factor(const Vec3d& scaling_factor) { if (m_scaling_factor != scaling_factor) @@ -335,7 +332,7 @@ void GLVolume::set_scaling_factor(double factor) m_transformed_convex_hull_bounding_box_dirty = true; } } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void GLVolume::set_convex_hull(const TriangleMesh& convex_hull) { @@ -366,18 +363,15 @@ const Transform3f& GLVolume::world_matrix() const { m_world_matrix = Transform3f::Identity(); m_world_matrix.translate(m_offset.cast()); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_world_matrix.rotate(Eigen::AngleAxisf((float)m_rotation(2), Vec3f::UnitZ())); m_world_matrix.rotate(Eigen::AngleAxisf((float)m_rotation(1), Vec3f::UnitY())); m_world_matrix.rotate(Eigen::AngleAxisf((float)m_rotation(0), Vec3f::UnitX())); -#else - m_world_matrix.rotate(Eigen::AngleAxisf((float)m_rotation, Vec3f::UnitZ())); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE m_world_matrix.scale(m_scaling_factor.cast()); #else + m_world_matrix.rotate(Eigen::AngleAxisf((float)m_rotation, Vec3f::UnitZ())); m_world_matrix.scale((float)m_scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_world_matrix_dirty = false; } return m_world_matrix; @@ -452,18 +446,15 @@ void GLVolume::render() const ::glCullFace(GL_BACK); ::glPushMatrix(); ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glRotated(m_rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glRotated(m_rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); ::glRotated(m_rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); -#else - ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); #else + ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (this->indexed_vertex_array.indexed()) this->indexed_vertex_array.render(this->tverts_range, this->qverts_range); else @@ -588,18 +579,15 @@ void GLVolume::render_VBOs(int color_id, int detection_id, int worldmatrix_id) c ::glPushMatrix(); ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glRotated(m_rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glRotated(m_rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); ::glRotated(m_rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); -#else - ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); #else + ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (n_triangles > 0) { @@ -643,18 +631,15 @@ void GLVolume::render_legacy() const ::glPushMatrix(); ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glRotated(m_rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glRotated(m_rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); ::glRotated(m_rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); -#else - ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); #else + ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (n_triangles > 0) ::glDrawElements(GL_TRIANGLES, n_triangles, GL_UNSIGNED_INT, indexed_vertex_array.triangle_indices.data() + tverts_range.first); @@ -772,21 +757,15 @@ std::vector GLVolumeCollection::load_object( } v.is_modifier = ! model_volume->is_model_part(); v.shader_outside_printer_detection_enabled = model_volume->is_model_part(); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM v.set_offset(instance->get_offset()); -#else - v.set_offset(Vec3d(instance->offset(0), instance->offset(1), 0.0)); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION v.set_rotation(instance->get_rotation()); -#else - v.set_rotation(instance->rotation); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE v.set_scaling_factor(instance->get_scaling_factor()); #else + v.set_offset(Vec3d(instance->offset(0), instance->offset(1), 0.0)); + v.set_rotation(instance->rotation); v.set_scaling_factor(instance->scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } } @@ -2149,58 +2128,58 @@ void _3DScene::register_on_enable_action_buttons_callback(wxGLCanvas* canvas, vo void _3DScene::register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback) { -#if !ENABLE_MODELINSTANCE_3D_SCALE +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_gizmo_scale_uniformly_callback(canvas, callback); -#endif // !ENABLE_MODELINSTANCE_3D_SCALE +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_on_gizmo_scale_3D_callback(wxGLCanvas* canvas, void* callback) { -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_gizmo_scale_3D_callback(canvas, callback); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback) { -#if !ENABLE_MODELINSTANCE_3D_ROTATION +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_gizmo_rotate_callback(canvas, callback); -#endif // !ENABLE_MODELINSTANCE_3D_ROTATION +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_on_gizmo_rotate_3D_callback(wxGLCanvas* canvas, void* callback) { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_gizmo_rotate_3D_callback(canvas, callback); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_on_gizmo_flatten_callback(wxGLCanvas* canvas, void* callback) { -#if !ENABLE_MODELINSTANCE_3D_ROTATION +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_gizmo_flatten_callback(canvas, callback); -#endif // !ENABLE_MODELINSTANCE_3D_ROTATION +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_on_gizmo_flatten_3D_callback(wxGLCanvas* canvas, void* callback) { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_gizmo_flatten_3D_callback(canvas, callback); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_on_update_geometry_info_callback(wxGLCanvas* canvas, void* callback) { -#if !ENABLE_MODELINSTANCE_3D_SCALE +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_update_geometry_info_callback(canvas, callback); -#endif // !ENABLE_MODELINSTANCE_3D_SCALE +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback) { -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM s_canvas_mgr.register_on_update_geometry_3D_info_callback(canvas, callback); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void _3DScene::register_action_add_callback(wxGLCanvas* canvas, void* callback) diff --git a/xs/src/slic3r/GUI/3DScene.hpp b/xs/src/slic3r/GUI/3DScene.hpp index f9ff9f1ea..484d6028d 100644 --- a/xs/src/slic3r/GUI/3DScene.hpp +++ b/xs/src/slic3r/GUI/3DScene.hpp @@ -256,20 +256,17 @@ public: private: // Offset of the volume to be rendered. Vec3d m_offset; -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Rotation around three axes of the volume to be rendered. Vec3d m_rotation; -#else - // Rotation around Z axis of the volume to be rendered. - double m_rotation; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE // Scale factor along the three axes of the volume to be rendered. Vec3d m_scaling_factor; #else + // Rotation around Z axis of the volume to be rendered. + double m_rotation; // Scale factor of the volume to be rendered. double m_scaling_factor; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // World matrix of the volume to be rendered. mutable Transform3f m_world_matrix; // Whether or not is needed to recalculate the world matrix. @@ -337,23 +334,21 @@ public: // Sets render color in dependence of current state void set_render_color(); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& get_rotation() const; void set_rotation(const Vec3d& rotation); + + void set_scaling_factor(const Vec3d& scaling_factor); #else double get_rotation() const; void set_rotation(double rotation); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION + + void set_scaling_factor(double factor); +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& get_offset() const; void set_offset(const Vec3d& offset); -#if ENABLE_MODELINSTANCE_3D_SCALE - void set_scaling_factor(const Vec3d& scaling_factor); -#else - void set_scaling_factor(double factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE - void set_convex_hull(const TriangleMesh& convex_hull); void set_select_group_id(const std::string& select_by); diff --git a/xs/src/slic3r/GUI/GLCanvas3D.cpp b/xs/src/slic3r/GUI/GLCanvas3D.cpp index e64c7ac10..bd9f58739 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.cpp @@ -1145,10 +1145,10 @@ bool GLCanvas3D::Gizmos::init(GLCanvas3D& parent) if (!gizmo->init()) return false; -#if !ENABLE_MODELINSTANCE_3D_OFFSET +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // temporary disable z grabber gizmo->disable_grabber(2); -#endif // !ENABLE_MODELINSTANCE_3D_OFFSET +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_gizmos.insert(GizmosMap::value_type(Move, gizmo)); @@ -1159,7 +1159,7 @@ bool GLCanvas3D::Gizmos::init(GLCanvas3D& parent) if (!gizmo->init()) return false; -#if !ENABLE_MODELINSTANCE_3D_SCALE +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // temporary disable x grabbers gizmo->disable_grabber(0); gizmo->disable_grabber(1); @@ -1169,7 +1169,7 @@ bool GLCanvas3D::Gizmos::init(GLCanvas3D& parent) // temporary disable z grabbers gizmo->disable_grabber(4); gizmo->disable_grabber(5); -#endif // !ENABLE_MODELINSTANCE_3D_SCALE +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_gizmos.insert(GizmosMap::value_type(Scale, gizmo)); @@ -1186,11 +1186,11 @@ bool GLCanvas3D::Gizmos::init(GLCanvas3D& parent) return false; } -#if !ENABLE_MODELINSTANCE_3D_ROTATION +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // temporary disable x and y grabbers gizmo->disable_grabber(0); gizmo->disable_grabber(1); -#endif // !ENABLE_MODELINSTANCE_3D_ROTATION +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_gizmos.insert(GizmosMap::value_type(Rotate, gizmo)); @@ -1421,7 +1421,7 @@ void GLCanvas3D::Gizmos::set_position(const Vec3d& position) reinterpret_cast(it->second)->set_position(position); } -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d GLCanvas3D::Gizmos::get_scale() const { if (!m_enabled) @@ -1440,28 +1440,7 @@ void GLCanvas3D::Gizmos::set_scale(const Vec3d& scale) if (it != m_gizmos.end()) reinterpret_cast(it->second)->set_scale(scale); } -#else -float GLCanvas3D::Gizmos::get_scale() const -{ - if (!m_enabled) - return 1.0f; - GizmosMap::const_iterator it = m_gizmos.find(Scale); - return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_scale_x() : 1.0f; -} - -void GLCanvas3D::Gizmos::set_scale(float scale) -{ - if (!m_enabled) - return; - - GizmosMap::const_iterator it = m_gizmos.find(Scale); - if (it != m_gizmos.end()) - reinterpret_cast(it->second)->set_scale(scale); -} -#endif // ENABLE_MODELINSTANCE_3D_SCALE - -#if ENABLE_MODELINSTANCE_3D_ROTATION Vec3d GLCanvas3D::Gizmos::get_rotation() const { if (!m_enabled) @@ -1490,6 +1469,25 @@ Vec3d GLCanvas3D::Gizmos::get_flattening_rotation() const return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_flattening_rotation() : Vec3d::Zero(); } #else +float GLCanvas3D::Gizmos::get_scale() const +{ + if (!m_enabled) + return 1.0f; + + GizmosMap::const_iterator it = m_gizmos.find(Scale); + return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_scale_x() : 1.0f; +} + +void GLCanvas3D::Gizmos::set_scale(float scale) +{ + if (!m_enabled) + return; + + GizmosMap::const_iterator it = m_gizmos.find(Scale); + if (it != m_gizmos.end()) + reinterpret_cast(it->second)->set_scale(scale); +} + float GLCanvas3D::Gizmos::get_angle_z() const { if (!m_enabled) @@ -1517,7 +1515,7 @@ Vec3d GLCanvas3D::Gizmos::get_flattening_normal() const GizmosMap::const_iterator it = m_gizmos.find(Flatten); return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_flattening_normal() : Vec3d::Zero(); } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void GLCanvas3D::Gizmos::set_flattening_data(const ModelObject* model_object) { @@ -2449,21 +2447,15 @@ void GLCanvas3D::update_gizmos_data() ModelInstance* model_instance = model_object->instances[0]; if (model_instance != nullptr) { -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_gizmos.set_position(model_instance->get_offset()); -#else - m_gizmos.set_position(Vec3d(model_instance->offset(0), model_instance->offset(1), 0.0)); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_SCALE m_gizmos.set_scale(model_instance->get_scaling_factor()); -#else - m_gizmos.set_scale(model_instance->scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION m_gizmos.set_rotation(model_instance->get_rotation()); #else + m_gizmos.set_position(Vec3d(model_instance->offset(0), model_instance->offset(1), 0.0)); + m_gizmos.set_scale(model_instance->scaling_factor); m_gizmos.set_angle_z(model_instance->rotation); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_gizmos.set_flattening_data(model_object); } } @@ -2471,16 +2463,13 @@ void GLCanvas3D::update_gizmos_data() else { m_gizmos.set_position(Vec3d::Zero()); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_gizmos.set_scale(Vec3d::Ones()); -#else - m_gizmos.set_scale(1.0f); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION m_gizmos.set_rotation(Vec3d::Zero()); #else + m_gizmos.set_scale(1.0f); m_gizmos.set_angle_z(0.0f); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_gizmos.set_flattening_data(nullptr); } } @@ -2837,21 +2826,13 @@ void GLCanvas3D::register_on_enable_action_buttons_callback(void* callback) m_on_enable_action_buttons_callback.register_callback(callback); } -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void GLCanvas3D::register_on_gizmo_scale_3D_callback(void* callback) { if (callback != nullptr) m_on_gizmo_scale_3D_callback.register_callback(callback); } -#else -void GLCanvas3D::register_on_gizmo_scale_uniformly_callback(void* callback) -{ - if (callback != nullptr) - m_on_gizmo_scale_uniformly_callback.register_callback(callback); -} -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION void GLCanvas3D::register_on_gizmo_rotate_3D_callback(void* callback) { if (callback != nullptr) @@ -2863,7 +2844,19 @@ void GLCanvas3D::register_on_gizmo_flatten_3D_callback(void* callback) if (callback != nullptr) m_on_gizmo_flatten_3D_callback.register_callback(callback); } + +void GLCanvas3D::register_on_update_geometry_3D_info_callback(void* callback) +{ + if (callback != nullptr) + m_on_update_geometry_3D_info_callback.register_callback(callback); +} #else +void GLCanvas3D::register_on_gizmo_scale_uniformly_callback(void* callback) +{ + if (callback != nullptr) + m_on_gizmo_scale_uniformly_callback.register_callback(callback); +} + void GLCanvas3D::register_on_gizmo_rotate_callback(void* callback) { if (callback != nullptr) @@ -2875,21 +2868,13 @@ void GLCanvas3D::register_on_gizmo_flatten_callback(void* callback) if (callback != nullptr) m_on_gizmo_flatten_callback.register_callback(callback); } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE -void GLCanvas3D::register_on_update_geometry_3D_info_callback(void* callback) -{ - if (callback != nullptr) - m_on_update_geometry_3D_info_callback.register_callback(callback); -} -#else void GLCanvas3D::register_on_update_geometry_info_callback(void* callback) { if (callback != nullptr) m_on_update_geometry_info_callback.register_callback(callback); } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void GLCanvas3D::register_action_add_callback(void* callback) { @@ -3162,32 +3147,30 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) #if ENABLE_GIZMOS_RESET else if (evt.LeftDClick() && m_gizmos.grabber_contains_mouse()) { -#if ENABLE_GIZMOS_RESET m_mouse.ignore_up_event = true; -#endif // ENABLE_GIZMOS_RESET m_gizmos.process_double_click(); switch (m_gizmos.get_current_type()) { case Gizmos::Scale: { -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& scale = m_gizmos.get_scale(); m_on_gizmo_scale_3D_callback.call(scale(0), scale(1), scale(2)); #else m_on_gizmo_scale_uniformly_callback.call((double)m_gizmos.get_scale()); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM update_scale_values(); m_dirty = true; break; } case Gizmos::Rotate: { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& rotation = m_gizmos.get_rotation(); m_on_gizmo_rotate_3D_callback.call(rotation(0), rotation(1), rotation(2)); #else m_on_gizmo_rotate_callback.call((double)m_gizmos.get_angle_z()); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM update_rotation_values(); m_dirty = true; break; @@ -3239,7 +3222,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) m_mouse.drag.gizmo_volume_idx = _get_first_selected_volume_id(selected_object_idx); if (m_gizmos.get_current_type() == Gizmos::Flatten) { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Rotate the object so the normal points downward: const Vec3d& rotation = m_gizmos.get_flattening_rotation(); m_on_gizmo_flatten_3D_callback.call(rotation(0), rotation(1), rotation(2)); @@ -3251,7 +3234,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) float angle = acos(clamp(-1.0, 1.0, -normal(2))); m_on_gizmo_flatten_callback.call(angle, (float)axis(0), (float)axis(1), (float)axis(2)); } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } m_dirty = true; @@ -3423,7 +3406,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) } case Gizmos::Scale: { -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Apply new temporary scale factors const Vec3d& scale = m_gizmos.get_scale(); for (GLVolume* v : volumes) @@ -3439,12 +3422,12 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) v->set_scaling_factor((double)scale_factor); } update_scale_values((double)scale_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM break; } case Gizmos::Rotate: { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // Apply new temporary rotation const Vec3d& rotation = m_gizmos.get_rotation(); for (GLVolume* v : volumes) @@ -3460,7 +3443,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) v->set_rotation((double)angle_z); } update_rotation_value((double)angle_z, Z); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM break; } default: @@ -3475,12 +3458,12 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) bb.merge(volume->transformed_bounding_box()); } const Vec3d& size = bb.size(); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& scale = m_gizmos.get_scale(); m_on_update_geometry_3D_info_callback.call(size(0), size(1), size(2), scale(0), scale(1), scale(2)); #else m_on_update_geometry_info_callback.call(size(0), size(1), size(2), m_gizmos.get_scale()); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } m_dirty = true; @@ -3606,22 +3589,22 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) } case Gizmos::Scale: { -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& scale = m_gizmos.get_scale(); m_on_gizmo_scale_3D_callback.call(scale(0), scale(1), scale(2)); #else m_on_gizmo_scale_uniformly_callback.call((double)m_gizmos.get_scale()); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM break; } case Gizmos::Rotate: { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& rotation = m_gizmos.get_rotation(); m_on_gizmo_rotate_3D_callback.call(rotation(0), rotation(1), rotation(2)); #else m_on_gizmo_rotate_callback.call((double)m_gizmos.get_angle_z()); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM break; } default: @@ -4067,23 +4050,17 @@ void GLCanvas3D::_deregister_callbacks() m_on_instance_moved_callback.deregister_callback(); m_on_wipe_tower_moved_callback.deregister_callback(); m_on_enable_action_buttons_callback.deregister_callback(); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_on_gizmo_scale_3D_callback.deregister_callback(); -#else - m_on_gizmo_scale_uniformly_callback.deregister_callback(); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION m_on_gizmo_rotate_3D_callback.deregister_callback(); m_on_gizmo_flatten_3D_callback.deregister_callback(); -#else - m_on_gizmo_rotate_callback.deregister_callback(); - m_on_gizmo_flatten_callback.deregister_callback(); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE m_on_update_geometry_3D_info_callback.deregister_callback(); #else + m_on_gizmo_scale_uniformly_callback.deregister_callback(); + m_on_gizmo_rotate_callback.deregister_callback(); + m_on_gizmo_flatten_callback.deregister_callback(); m_on_update_geometry_info_callback.deregister_callback(); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_action_add_callback.deregister_callback(); m_action_delete_callback.deregister_callback(); @@ -5590,12 +5567,12 @@ void GLCanvas3D::_on_move(const std::vector& volume_idxs) ModelObject* model_object = m_model->objects[obj_idx]; if (model_object != nullptr) { -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM model_object->instances[instance_idx]->set_offset(volume->get_offset()); #else const Vec3d& offset = volume->get_offset(); model_object->instances[instance_idx]->offset = Vec2d(offset(0), offset(1)); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM model_object->invalidate_bounding_box(); update_position_values(); object_moved = true; diff --git a/xs/src/slic3r/GUI/GLCanvas3D.hpp b/xs/src/slic3r/GUI/GLCanvas3D.hpp index 4458ca8b8..451190161 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.hpp @@ -384,27 +384,24 @@ class GLCanvas3D Vec3d get_position() const; void set_position(const Vec3d& position); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d get_scale() const; void set_scale(const Vec3d& scale); + + Vec3d get_rotation() const; + void set_rotation(const Vec3d& rotation); + + Vec3d get_flattening_rotation() const; #else float get_scale() const; void set_scale(float scale); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION - Vec3d get_rotation() const; - void set_rotation(const Vec3d& rotation); -#else float get_angle_z() const; void set_angle_z(float angle_z); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_ROTATION - Vec3d get_flattening_rotation() const; -#else Vec3d get_flattening_normal() const; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM + void set_flattening_data(const ModelObject* model_object); void render_current_gizmo(const BoundingBoxf3& box) const; @@ -520,23 +517,17 @@ class GLCanvas3D PerlCallback m_on_instance_moved_callback; PerlCallback m_on_wipe_tower_moved_callback; PerlCallback m_on_enable_action_buttons_callback; -#if ENABLE_MODELINSTANCE_3D_SCALE - PerlCallback m_on_gizmo_scale_3D_callback; -#else - PerlCallback m_on_gizmo_scale_uniformly_callback; -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM PerlCallback m_on_gizmo_rotate_3D_callback; PerlCallback m_on_gizmo_flatten_3D_callback; -#else - PerlCallback m_on_gizmo_rotate_callback; - PerlCallback m_on_gizmo_flatten_callback; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE + PerlCallback m_on_gizmo_scale_3D_callback; PerlCallback m_on_update_geometry_3D_info_callback; #else + PerlCallback m_on_gizmo_scale_uniformly_callback; + PerlCallback m_on_gizmo_rotate_callback; + PerlCallback m_on_gizmo_flatten_callback; PerlCallback m_on_update_geometry_info_callback; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM PerlCallback m_action_add_callback; PerlCallback m_action_delete_callback; @@ -658,23 +649,17 @@ public: void register_on_instance_moved_callback(void* callback); void register_on_wipe_tower_moved_callback(void* callback); void register_on_enable_action_buttons_callback(void* callback); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void register_on_gizmo_scale_3D_callback(void* callback); -#else - void register_on_gizmo_scale_uniformly_callback(void* callback); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION void register_on_gizmo_rotate_3D_callback(void* callback); void register_on_gizmo_flatten_3D_callback(void* callback); -#else - void register_on_gizmo_rotate_callback(void* callback); - void register_on_gizmo_flatten_callback(void* callback); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE void register_on_update_geometry_3D_info_callback(void* callback); #else + void register_on_gizmo_scale_uniformly_callback(void* callback); + void register_on_gizmo_rotate_callback(void* callback); + void register_on_gizmo_flatten_callback(void* callback); void register_on_update_geometry_info_callback(void* callback); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void register_action_add_callback(void* callback); void register_action_delete_callback(void* callback); diff --git a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp index 35c36dd6b..1fef7ffe9 100644 --- a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp @@ -692,23 +692,14 @@ void GLCanvas3DManager::register_on_enable_action_buttons_callback(wxGLCanvas* c it->second->register_on_enable_action_buttons_callback(callback); } -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void GLCanvas3DManager::register_on_gizmo_scale_3D_callback(wxGLCanvas* canvas, void* callback) { CanvasesMap::iterator it = _get_canvas(canvas); if (it != m_canvases.end()) it->second->register_on_gizmo_scale_3D_callback(callback); } -#else -void GLCanvas3DManager::register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback) -{ - CanvasesMap::iterator it = _get_canvas(canvas); - if (it != m_canvases.end()) - it->second->register_on_gizmo_scale_uniformly_callback(callback); -} -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION void GLCanvas3DManager::register_on_gizmo_rotate_3D_callback(wxGLCanvas* canvas, void* callback) { CanvasesMap::iterator it = _get_canvas(canvas); @@ -722,7 +713,21 @@ void GLCanvas3DManager::register_on_gizmo_flatten_3D_callback(wxGLCanvas* canvas if (it != m_canvases.end()) it->second->register_on_gizmo_flatten_3D_callback(callback); } + +void GLCanvas3DManager::register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback) +{ + CanvasesMap::iterator it = _get_canvas(canvas); + if (it != m_canvases.end()) + it->second->register_on_update_geometry_3D_info_callback(callback); +} #else +void GLCanvas3DManager::register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback) +{ + CanvasesMap::iterator it = _get_canvas(canvas); + if (it != m_canvases.end()) + it->second->register_on_gizmo_scale_uniformly_callback(callback); +} + void GLCanvas3DManager::register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback) { CanvasesMap::iterator it = _get_canvas(canvas); @@ -736,23 +741,14 @@ void GLCanvas3DManager::register_on_gizmo_flatten_callback(wxGLCanvas* canvas, v if (it != m_canvases.end()) it->second->register_on_gizmo_flatten_callback(callback); } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE -void GLCanvas3DManager::register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback) -{ - CanvasesMap::iterator it = _get_canvas(canvas); - if (it != m_canvases.end()) - it->second->register_on_update_geometry_3D_info_callback(callback); -} -#else void GLCanvas3DManager::register_on_update_geometry_info_callback(wxGLCanvas* canvas, void* callback) { CanvasesMap::iterator it = _get_canvas(canvas); if (it != m_canvases.end()) it->second->register_on_update_geometry_info_callback(callback); } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void GLCanvas3DManager::register_action_add_callback(wxGLCanvas* canvas, void* callback) { diff --git a/xs/src/slic3r/GUI/GLCanvas3DManager.hpp b/xs/src/slic3r/GUI/GLCanvas3DManager.hpp index 05b26ebff..1c376cb29 100644 --- a/xs/src/slic3r/GUI/GLCanvas3DManager.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3DManager.hpp @@ -162,23 +162,17 @@ public: void register_on_instance_moved_callback(wxGLCanvas* canvas, void* callback); void register_on_wipe_tower_moved_callback(wxGLCanvas* canvas, void* callback); void register_on_enable_action_buttons_callback(wxGLCanvas* canvas, void* callback); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void register_on_gizmo_scale_3D_callback(wxGLCanvas* canvas, void* callback); -#else - void register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_ROTATION void register_on_gizmo_rotate_3D_callback(wxGLCanvas* canvas, void* callback); void register_on_gizmo_flatten_3D_callback(wxGLCanvas* canvas, void* callback); -#else - void register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback); - void register_on_gizmo_flatten_callback(wxGLCanvas* canvas, void* callback); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE void register_on_update_geometry_3D_info_callback(wxGLCanvas* canvas, void* callback); #else + void register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback); + void register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback); + void register_on_gizmo_flatten_callback(wxGLCanvas* canvas, void* callback); void register_on_update_geometry_info_callback(wxGLCanvas* canvas, void* callback); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void register_action_add_callback(wxGLCanvas* canvas, void* callback); void register_action_delete_callback(wxGLCanvas* canvas, void* callback); diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index 5c6f6b00c..d1bd89c4d 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -987,11 +987,11 @@ void GLGizmoScale3D::do_scale_y(const Linef3& mouse_ray) double ratio = calc_ratio(2, mouse_ray, m_starting_box.center()); if (ratio > 0.0) -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_scale(1) = m_starting_scale(1) * ratio; #else m_scale(0) = m_starting_scale(1) * ratio; -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void GLGizmoScale3D::do_scale_z(const Linef3& mouse_ray) @@ -999,11 +999,11 @@ void GLGizmoScale3D::do_scale_z(const Linef3& mouse_ray) double ratio = calc_ratio(1, mouse_ray, m_starting_box.center()); if (ratio > 0.0) -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_scale(2) = m_starting_scale(2) * ratio; #else m_scale(0) = m_starting_scale(2) * ratio; // << this is temporary -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void GLGizmoScale3D::do_scale_uniform(const Linef3& mouse_ray) @@ -1263,36 +1263,23 @@ void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const else ::glColor4f(0.9f, 0.9f, 0.9f, 0.5f); -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (const InstanceData& inst : m_instances) { Vec3d position = inst.position + dragged_offset; -#else - for (Vec3d offset : m_instances_positions) { - offset += dragged_offset; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else for (Vec2d offset : m_instances_positions) { offset += to_2d(dragged_offset); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glPushMatrix(); -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glTranslated(position(0), position(1), position(2)); ::glRotated(inst.rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glRotated(inst.rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); ::glRotated(inst.rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); -#if ENABLE_MODELINSTANCE_3D_SCALE ::glScaled(inst.scaling_factor(0), inst.scaling_factor(1), inst.scaling_factor(2)); -#else - ::glScaled(inst.scaling_factor, inst.scaling_factor, inst.scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#else - ::glTranslated(offset(0), offset(1), offset(2)); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else ::glTranslatef((GLfloat)offset(0), (GLfloat)offset(1), 0.0f); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glBegin(GL_POLYGON); for (const Vec3d& vertex : m_planes[i].vertices) ::glVertex3f((GLfloat)vertex(0), (GLfloat)vertex(1), (GLfloat)vertex(2)); @@ -1311,33 +1298,21 @@ void GLGizmoFlatten::on_render_for_picking(const BoundingBoxf3& box) const for (unsigned int i = 0; i < m_planes.size(); ++i) { ::glColor3f(1.0f, 1.0f, picking_color_component(i)); -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (const InstanceData& inst : m_instances) { -#else - for (const Vec3d& offset : m_instances_positions) { -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else for (const Vec2d& offset : m_instances_positions) { -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glPushMatrix(); -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glTranslated(inst.position(0), inst.position(1), inst.position(2)); ::glRotated(inst.rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glRotated(inst.rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); ::glRotated(inst.rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); -#if ENABLE_MODELINSTANCE_3D_SCALE ::glScaled(inst.scaling_factor(0), inst.scaling_factor(1), inst.scaling_factor(2)); -#else - ::glScaled(inst.scaling_factor, inst.scaling_factor, inst.scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#else - ::glTranslated(offset(0), offset(1), offset(2)); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else ::glTranslatef((GLfloat)offset(0), (GLfloat)offset(1), 0.0f); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glBegin(GL_POLYGON); for (const Vec3d& vertex : m_planes[i].vertices) ::glVertex3f((GLfloat)vertex(0), (GLfloat)vertex(1), (GLfloat)vertex(2)); @@ -1353,25 +1328,17 @@ void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object) // ...and save the updated positions of the object instances: if (m_model_object && !m_model_object->instances.empty()) { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_instances.clear(); #else m_instances_positions.clear(); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (const auto* instance : m_model_object->instances) -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_instances.emplace_back(instance->get_offset(), instance->get_rotation(), instance->get_scaling_factor()); -#else - m_instances.emplace_back(instance->get_offset(), instance->get_rotation(), instance->scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#else - m_instances_positions.emplace_back(instance->get_offset()); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else m_instances_positions.emplace_back(instance->offset); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } if (is_plane_update_necessary()) @@ -1385,10 +1352,10 @@ void GLGizmoFlatten::update_planes() ch.merge(vol->get_convex_hull()); ch = ch.convex_hull_3d(); -#if !ENABLE_MODELINSTANCE_3D_ROTATION +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ch.scale(m_model_object->instances.front()->scaling_factor); ch.rotate_z(m_model_object->instances.front()->rotation); -#endif // !ENABLE_MODELINSTANCE_3D_ROTATION +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& bb_size = ch.bounding_box().size(); double min_bb_face_area = std::min(bb_size(0) * bb_size(1), std::min(bb_size(0) * bb_size(2), bb_size(1) * bb_size(2))); @@ -1554,10 +1521,10 @@ void GLGizmoFlatten::update_planes() m_source_data.bounding_boxes.clear(); for (const auto& vol : m_model_object->volumes) m_source_data.bounding_boxes.push_back(vol->get_convex_hull().bounding_box()); -#if !ENABLE_MODELINSTANCE_3D_ROTATION +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM m_source_data.scaling_factor = m_model_object->instances.front()->scaling_factor; m_source_data.rotation = m_model_object->instances.front()->rotation; -#endif // !ENABLE_MODELINSTANCE_3D_ROTATION +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const float* first_vertex = m_model_object->volumes.front()->get_convex_hull().first_vertex(); m_source_data.mesh_first_point = Vec3d((double)first_vertex[0], (double)first_vertex[1], (double)first_vertex[2]); } @@ -1569,13 +1536,13 @@ bool GLGizmoFlatten::is_plane_update_necessary() const if (m_state != On || !m_model_object || m_model_object->instances.empty()) return false; -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (m_model_object->volumes.size() != m_source_data.bounding_boxes.size()) #else if (m_model_object->volumes.size() != m_source_data.bounding_boxes.size() || m_model_object->instances.front()->scaling_factor != m_source_data.scaling_factor || m_model_object->instances.front()->rotation != m_source_data.rotation) -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM return true; // now compare the bounding boxes: @@ -1591,23 +1558,15 @@ bool GLGizmoFlatten::is_plane_update_necessary() const return false; } -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d GLGizmoFlatten::get_flattening_rotation() const { -#if ENABLE_MODELINSTANCE_3D_SCALE // calculates the rotations in model space, taking in account the scaling factors Eigen::Matrix m = m_model_object->instances.front()->world_matrix(true, true).matrix().block(0, 0, 3, 3).inverse().transpose(); Eigen::Quaterniond q; Vec3d angles = q.setFromTwoVectors(m * m_normal, -Vec3d::UnitZ()).toRotationMatrix().eulerAngles(2, 1, 0); m_normal = Vec3d::Zero(); return Vec3d(angles(2), angles(1), angles(0)); -#else - // calculates the rotations in model space - Eigen::Quaterniond q; - Vec3d angles = q.setFromTwoVectors(m_normal, -Vec3d::UnitZ()).toRotationMatrix().eulerAngles(2, 1, 0); - m_normal = Vec3d::Zero(); - return Vec3d(angles(2), angles(1), angles(0)); -#endif // ENABLE_MODELINSTANCE_3D_SCALE } #else Vec3d GLGizmoFlatten::get_flattening_normal() const { @@ -1615,7 +1574,7 @@ Vec3d GLGizmoFlatten::get_flattening_normal() const { m_normal = Vec3d::Zero(); return normal.normalized(); } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } // namespace GUI } // namespace Slic3r diff --git a/xs/src/slic3r/GUI/GLGizmo.hpp b/xs/src/slic3r/GUI/GLGizmo.hpp index 4f5c8b471..412987a22 100644 --- a/xs/src/slic3r/GUI/GLGizmo.hpp +++ b/xs/src/slic3r/GUI/GLGizmo.hpp @@ -192,7 +192,7 @@ class GLGizmoRotate3D : public GLGizmoBase public: explicit GLGizmoRotate3D(GLCanvas3D& parent); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d get_rotation() const { return Vec3d(m_gizmos[X].get_angle(), m_gizmos[Y].get_angle(), m_gizmos[Z].get_angle()); } void set_rotation(const Vec3d& rotation) { m_gizmos[X].set_angle(rotation(0)); m_gizmos[Y].set_angle(rotation(1)); m_gizmos[Z].set_angle(rotation(2)); } #else @@ -204,7 +204,7 @@ public: double get_angle_z() const { return m_gizmos[Z].get_angle(); } void set_angle_z(double angle) { m_gizmos[Z].set_angle(angle); } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM protected: virtual bool on_init(); @@ -275,7 +275,7 @@ class GLGizmoScale3D : public GLGizmoBase public: explicit GLGizmoScale3D(GLCanvas3D& parent); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM const Vec3d& get_scale() const { return m_scale; } void set_scale(const Vec3d& scale) { m_starting_scale = scale; } #else @@ -289,7 +289,7 @@ public: void set_scale_z(double scale) { m_starting_scale(2) = scale; } void set_scale(double scale) { m_starting_scale = scale * Vec3d::Ones(); } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM protected: virtual bool on_init(); @@ -354,10 +354,10 @@ private: }; struct SourceDataSummary { std::vector bounding_boxes; // bounding boxes of convex hulls of individual volumes -#if !ENABLE_MODELINSTANCE_3D_ROTATION +#if !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM float scaling_factor; float rotation; -#endif // !ENABLE_MODELINSTANCE_3D_ROTATION +#endif // !ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d mesh_first_point; }; @@ -365,29 +365,19 @@ private: SourceDataSummary m_source_data; std::vector m_planes; -#if ENABLE_MODELINSTANCE_3D_OFFSET -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM struct InstanceData { Vec3d position; Vec3d rotation; -#if ENABLE_MODELINSTANCE_3D_SCALE Vec3d scaling_factor; InstanceData(const Vec3d& position, const Vec3d& rotation, const Vec3d& scaling_factor) : position(position), rotation(rotation), scaling_factor(scaling_factor) {} -#else - double scaling_factor; - - InstanceData(const Vec3d& position, const Vec3d& rotation, double scaling_factor) : position(position), rotation(rotation), scaling_factor(scaling_factor) {} -#endif // ENABLE_MODELINSTANCE_3D_SCALE }; std::vector m_instances; -#else - Pointf3s m_instances_positions; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION #else std::vector m_instances_positions; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d m_starting_center; const ModelObject* m_model_object = nullptr; @@ -398,11 +388,11 @@ public: explicit GLGizmoFlatten(GLCanvas3D& parent); void set_flattening_data(const ModelObject* model_object); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d get_flattening_rotation() const; #else Vec3d get_flattening_normal() const; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM protected: virtual bool on_init(); diff --git a/xs/src/slic3r/GUI/GUI_ObjectParts.cpp b/xs/src/slic3r/GUI/GUI_ObjectParts.cpp index 4d120cd00..949c77171 100644 --- a/xs/src/slic3r/GUI/GUI_ObjectParts.cpp +++ b/xs/src/slic3r/GUI/GUI_ObjectParts.cpp @@ -1750,7 +1750,7 @@ void update_scale_values() auto instance = (*m_objects)[m_selected_object_id]->instances.front(); auto size = (*m_objects)[m_selected_object_id]->instance_bounding_box(0).size(); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM if (g_is_percent_scale) { og->set_value("scale_x", int(instance->get_scaling_factor(X) * 100)); og->set_value("scale_y", int(instance->get_scaling_factor(Y) * 100)); @@ -1773,7 +1773,7 @@ void update_scale_values() og->set_value("scale_y", int(instance->scaling_factor * size(1) + 0.5)); og->set_value("scale_z", int(instance->scaling_factor * size(2) + 0.5)); } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void update_position_values() @@ -1781,7 +1781,7 @@ void update_position_values() auto og = get_optgroup(ogFrequentlyObjectSettings); auto instance = (*m_objects)[m_selected_object_id]->instances.front(); -#if ENABLE_MODELINSTANCE_3D_OFFSET +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM og->set_value("position_x", int(instance->get_offset(X))); og->set_value("position_y", int(instance->get_offset(Y))); og->set_value("position_z", int(instance->get_offset(Z))); @@ -1789,7 +1789,7 @@ void update_position_values() og->set_value("position_x", int(instance->offset(0))); og->set_value("position_y", int(instance->offset(1))); og->set_value("position_z", 0); -#endif // ENABLE_MODELINSTANCE_3D_OFFSET +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void update_position_values(const Vec3d& position) @@ -1801,7 +1801,7 @@ void update_position_values(const Vec3d& position) og->set_value("position_z", int(position(2))); } -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void update_scale_values(const Vec3d& scaling_factor) { auto og = get_optgroup(ogFrequentlyObjectSettings); @@ -1835,11 +1835,11 @@ void update_scale_values(double scaling_factor) og->set_value("scale_y", int(scale)); og->set_value("scale_z", int(scale)); } -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void update_rotation_values() { -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM update_rotation_value((*m_objects)[m_selected_object_id]->instances.front()->get_rotation()); #else auto og = get_optgroup(ogFrequentlyObjectSettings); @@ -1847,7 +1847,7 @@ void update_rotation_values() og->set_value("rotation_x", 0); og->set_value("rotation_y", 0); og->set_value("rotation_z", int(Geometry::rad2deg(instance->rotation))); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM } void update_rotation_value(double angle, Axis axis) @@ -1877,7 +1877,7 @@ void update_rotation_value(double angle, Axis axis) og->set_value(axis_str, round_nearest(int(Geometry::rad2deg(angle)), 0)); } -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void update_rotation_value(const Vec3d& rotation) { auto og = get_optgroup(ogFrequentlyObjectSettings); @@ -1885,7 +1885,7 @@ void update_rotation_value(const Vec3d& rotation) og->set_value("rotation_y", int(round_nearest(Geometry::rad2deg(rotation(1)), 0))); og->set_value("rotation_z", int(round_nearest(Geometry::rad2deg(rotation(2)), 0))); } -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void set_uniform_scaling(const bool uniform_scale) { diff --git a/xs/src/slic3r/GUI/GUI_ObjectParts.hpp b/xs/src/slic3r/GUI/GUI_ObjectParts.hpp index 7e3bbbafe..8189238f0 100644 --- a/xs/src/slic3r/GUI/GUI_ObjectParts.hpp +++ b/xs/src/slic3r/GUI/GUI_ObjectParts.hpp @@ -119,18 +119,18 @@ void update_position_values(); void update_position_values(const Vec3d& position); // update scale values after scale unit changing or "gizmos" void update_scale_values(); -#if ENABLE_MODELINSTANCE_3D_SCALE +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void update_scale_values(const Vec3d& scaling_factor); #else void update_scale_values(double scaling_factor); -#endif // ENABLE_MODELINSTANCE_3D_SCALE +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM // update rotation values object selection changing void update_rotation_values(); // update rotation value after "gizmos" void update_rotation_value(double angle, Axis axis); -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void update_rotation_value(const Vec3d& rotation); -#endif // ENABLE_MODELINSTANCE_3D_ROTATION +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM void set_uniform_scaling(const bool uniform_scale); void on_begin_drag(wxDataViewEvent &event); diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index b92b55935..58e69693b 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -364,59 +364,53 @@ ModelMaterial::attributes() Ref object() %code%{ RETVAL = THIS->get_object(); %}; -#if ENABLE_MODELINSTANCE_3D_ROTATION +#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM Vec3d* rotation() %code%{ RETVAL = new Vec3d(THIS->get_rotation(X), THIS->get_rotation(Y), THIS->get_rotation(Z)); %}; -#else - double rotation() - %code%{ RETVAL = THIS->rotation; %}; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE + Vec3d* scaling_factor() %code%{ RETVAL = new Vec3d(THIS->get_scaling_factor(X), THIS->get_scaling_factor(Y), THIS->get_scaling_factor(Z)); %}; -#else - double scaling_factor() - %code%{ RETVAL = THIS->scaling_factor; %}; -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_OFFSET + Vec2d* offset() %code%{ RETVAL = new Vec2d(THIS->get_offset(X), THIS->get_offset(Y)); %}; -#else - Ref offset() - %code%{ RETVAL = &THIS->offset; %}; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET - -#if ENABLE_MODELINSTANCE_3D_ROTATION + void set_rotation(double val) %code%{ THIS->set_rotation(Z, val); THIS->get_object()->invalidate_bounding_box(); %}; void set_rotations(Vec3d *rotation) %code%{ THIS->set_rotation(*rotation); THIS->get_object()->invalidate_bounding_box(); %}; -#else - void set_rotation(double val) - %code%{ THIS->rotation = val; THIS->get_object()->invalidate_bounding_box(); %}; -#endif // ENABLE_MODELINSTANCE_3D_ROTATION -#if ENABLE_MODELINSTANCE_3D_SCALE + void set_scaling_factor(double val) %code%{ THIS->set_scaling_factor(X, val); THIS->set_scaling_factor(Y, val); THIS->set_scaling_factor(Z, val); THIS->get_object()->invalidate_bounding_box(); %}; void set_scaling_factors(Vec3d *scale) %code%{ THIS->set_scaling_factor(*scale); THIS->get_object()->invalidate_bounding_box(); %}; -#else - void set_scaling_factor(double val) - %code%{ THIS->scaling_factor = val; THIS->get_object()->invalidate_bounding_box(); %}; -#endif // ENABLE_MODELINSTANCE_3D_SCALE -#if ENABLE_MODELINSTANCE_3D_OFFSET + void set_offset(Vec2d *offset) %code%{ THIS->set_offset(X, (*offset)(0)); THIS->set_offset(Y, (*offset)(1)); %}; #else + double rotation() + %code%{ RETVAL = THIS->rotation; %}; + + double scaling_factor() + %code%{ RETVAL = THIS->scaling_factor; %}; + + Ref offset() + %code%{ RETVAL = &THIS->offset; %}; + + void set_rotation(double val) + %code%{ THIS->rotation = val; THIS->get_object()->invalidate_bounding_box(); %}; + + void set_scaling_factor(double val) + %code%{ THIS->scaling_factor = val; THIS->get_object()->invalidate_bounding_box(); %}; + void set_offset(Vec2d *offset) %code%{ THIS->offset = *offset; %}; -#endif // ENABLE_MODELINSTANCE_3D_OFFSET - +#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM + void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const; void transform_polygon(Polygon* polygon) const; }; From 51cf964b517c3f82c670e5015d7d4bb9d786432a Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 25 Sep 2018 12:15:51 +0200 Subject: [PATCH 7/7] Reduced count of opengl draw calls for full 3D transform --- xs/src/slic3r/GUI/3DScene.cpp | 24 +++++++++--------------- xs/src/slic3r/GUI/GLGizmo.cpp | 33 ++++++++------------------------- xs/src/slic3r/GUI/GLGizmo.hpp | 7 ++----- 3 files changed, 19 insertions(+), 45 deletions(-) diff --git a/xs/src/slic3r/GUI/3DScene.cpp b/xs/src/slic3r/GUI/3DScene.cpp index 02a35029e..3eb174694 100644 --- a/xs/src/slic3r/GUI/3DScene.cpp +++ b/xs/src/slic3r/GUI/3DScene.cpp @@ -445,13 +445,11 @@ void GLVolume::render() const ::glCullFace(GL_BACK); ::glPushMatrix(); - ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); + #if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM - ::glRotated(m_rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); - ::glRotated(m_rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); - ::glRotated(m_rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); - ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); + ::glMultMatrixf(world_matrix().data()); #else + ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); #endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM @@ -578,13 +576,11 @@ void GLVolume::render_VBOs(int color_id, int detection_id, int worldmatrix_id) c ::glNormalPointer(GL_FLOAT, 6 * sizeof(float), nullptr); ::glPushMatrix(); - ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); + #if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM - ::glRotated(m_rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); - ::glRotated(m_rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); - ::glRotated(m_rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); - ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); + ::glMultMatrixf(world_matrix().data()); #else + ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); #endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM @@ -630,13 +626,11 @@ void GLVolume::render_legacy() const ::glNormalPointer(GL_FLOAT, 6 * sizeof(float), indexed_vertex_array.vertices_and_normals_interleaved.data()); ::glPushMatrix(); - ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); + #if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM - ::glRotated(m_rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); - ::glRotated(m_rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); - ::glRotated(m_rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); - ::glScaled(m_scaling_factor(0), m_scaling_factor(1), m_scaling_factor(2)); + ::glMultMatrixf(world_matrix().data()); #else + ::glTranslated(m_offset(0), m_offset(1), m_offset(2)); ::glRotated(m_rotation * 180.0 / (double)PI, 0.0, 0.0, 1.0); ::glScaled(m_scaling_factor, m_scaling_factor, m_scaling_factor); #endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index d1bd89c4d..55729ac87 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -769,13 +769,7 @@ void GLGizmoScale3D::on_update(const Linef3& mouse_ray) #if ENABLE_GIZMOS_RESET void GLGizmoScale3D::on_process_double_click() { - if ((m_hover_id == 0) || (m_hover_id == 1)) - m_scale(0) = 1.0; - else if ((m_hover_id == 2) || (m_hover_id == 3)) - m_scale(1) = 1.0; - else if ((m_hover_id == 4) || (m_hover_id == 5)) - m_scale(2) = 1.0; - else if (m_hover_id >= 6) + if (m_hover_id >= 6) m_scale = Vec3d::Ones(); } #endif // ENABLE_GIZMOS_RESET @@ -1265,19 +1259,14 @@ void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const #if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (const InstanceData& inst : m_instances) { - Vec3d position = inst.position + dragged_offset; + Transform3d m = inst.matrix; + m.pretranslate(dragged_offset); + ::glPushMatrix(); + ::glMultMatrixd(m.data()); #else for (Vec2d offset : m_instances_positions) { offset += to_2d(dragged_offset); -#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glPushMatrix(); -#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM - ::glTranslated(position(0), position(1), position(2)); - ::glRotated(inst.rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); - ::glRotated(inst.rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); - ::glRotated(inst.rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); - ::glScaled(inst.scaling_factor(0), inst.scaling_factor(1), inst.scaling_factor(2)); -#else ::glTranslatef((GLfloat)offset(0), (GLfloat)offset(1), 0.0f); #endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glBegin(GL_POLYGON); @@ -1300,17 +1289,11 @@ void GLGizmoFlatten::on_render_for_picking(const BoundingBoxf3& box) const ::glColor3f(1.0f, 1.0f, picking_color_component(i)); #if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (const InstanceData& inst : m_instances) { + ::glPushMatrix(); + ::glMultMatrixd(inst.matrix.data()); #else for (const Vec2d& offset : m_instances_positions) { -#endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glPushMatrix(); -#if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM - ::glTranslated(inst.position(0), inst.position(1), inst.position(2)); - ::glRotated(inst.rotation(2) * 180.0 / (double)PI, 0.0, 0.0, 1.0); - ::glRotated(inst.rotation(1) * 180.0 / (double)PI, 0.0, 1.0, 0.0); - ::glRotated(inst.rotation(0) * 180.0 / (double)PI, 1.0, 0.0, 0.0); - ::glScaled(inst.scaling_factor(0), inst.scaling_factor(1), inst.scaling_factor(2)); -#else ::glTranslatef((GLfloat)offset(0), (GLfloat)offset(1), 0.0f); #endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM ::glBegin(GL_POLYGON); @@ -1335,7 +1318,7 @@ void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object) #endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM for (const auto* instance : m_model_object->instances) #if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM - m_instances.emplace_back(instance->get_offset(), instance->get_rotation(), instance->get_scaling_factor()); + m_instances.emplace_back(instance->world_matrix()); #else m_instances_positions.emplace_back(instance->offset); #endif // ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM diff --git a/xs/src/slic3r/GUI/GLGizmo.hpp b/xs/src/slic3r/GUI/GLGizmo.hpp index 412987a22..ac85caa18 100644 --- a/xs/src/slic3r/GUI/GLGizmo.hpp +++ b/xs/src/slic3r/GUI/GLGizmo.hpp @@ -368,11 +368,8 @@ private: #if ENABLE_MODELINSTANCE_3D_FULL_TRANSFORM struct InstanceData { - Vec3d position; - Vec3d rotation; - Vec3d scaling_factor; - - InstanceData(const Vec3d& position, const Vec3d& rotation, const Vec3d& scaling_factor) : position(position), rotation(rotation), scaling_factor(scaling_factor) {} + Transform3d matrix; + InstanceData(const Transform3d& matrix) : matrix(matrix) {} }; std::vector m_instances; #else