Flatten gizmo should now work with multiple selection
This commit is contained in:
parent
385b0f261d
commit
622f4ee4f6
4 changed files with 95 additions and 56 deletions
|
@ -1394,6 +1394,14 @@ int GLCanvas3D::Selection::get_instance_idx() const
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GLCanvas3D::Selection::InstanceIdxsList& GLCanvas3D::Selection::get_instance_idxs() const
|
||||||
|
{
|
||||||
|
if (m_cache.content.size() != 1)
|
||||||
|
throw std::runtime_error("get_instance_idxs() called for multiple object selection.");
|
||||||
|
|
||||||
|
return m_cache.content.begin()->second;
|
||||||
|
}
|
||||||
|
|
||||||
const GLVolume* GLCanvas3D::Selection::get_volume(unsigned int volume_idx) const
|
const GLVolume* GLCanvas3D::Selection::get_volume(unsigned int volume_idx) const
|
||||||
{
|
{
|
||||||
return (m_valid && (volume_idx < (unsigned int)m_volumes->size())) ? (*m_volumes)[volume_idx] : nullptr;
|
return (m_valid && (volume_idx < (unsigned int)m_volumes->size())) ? (*m_volumes)[volume_idx] : nullptr;
|
||||||
|
@ -1483,6 +1491,36 @@ void GLCanvas3D::Selection::rotate(const Vec3d& rotation)
|
||||||
m_bounding_box_dirty = true;
|
m_bounding_box_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GLCanvas3D::Selection::flattening_rotate(const Vec3d& normal)
|
||||||
|
{
|
||||||
|
// We get the normal in untransformed coordinates. We must transform it using the instance matrix, find out
|
||||||
|
// how to rotate the instance so it faces downwards and do the rotation. All that for all selected instances.
|
||||||
|
// The function assumes that is_from_single_object() holds.
|
||||||
|
|
||||||
|
if (!m_valid)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (unsigned int i : m_list)
|
||||||
|
{
|
||||||
|
Vec3d scaling_factor = m_cache.volumes_data[i].get_scaling_factor();
|
||||||
|
scaling_factor = Vec3d(1./scaling_factor(0), 1./scaling_factor(1), 1./scaling_factor(2));
|
||||||
|
|
||||||
|
Vec3d transformed_normal = Geometry::assemble_transform(Vec3d::Zero(), m_cache.volumes_data[i].get_rotation(), scaling_factor) * normal;
|
||||||
|
transformed_normal.normalize();
|
||||||
|
|
||||||
|
Vec3d axis = transformed_normal(2) > 0.999f ? Vec3d(1., 0., 0.) : Vec3d(transformed_normal.cross(Vec3d(0., 0., -1.)));
|
||||||
|
axis.normalize();
|
||||||
|
|
||||||
|
Transform3d extra_rotation = Transform3d::Identity();
|
||||||
|
extra_rotation.rotate(Eigen::AngleAxisd(acos(-transformed_normal(2)), axis));
|
||||||
|
|
||||||
|
Vec3d new_rotation = Geometry::extract_euler_angles(extra_rotation * m_cache.volumes_data[i].get_rotation_matrix() );
|
||||||
|
(*m_volumes)[i]->set_rotation(new_rotation);
|
||||||
|
}
|
||||||
|
m_bounding_box_dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void GLCanvas3D::Selection::scale(const Vec3d& scale)
|
void GLCanvas3D::Selection::scale(const Vec3d& scale)
|
||||||
{
|
{
|
||||||
if (!m_valid)
|
if (!m_valid)
|
||||||
|
@ -2389,13 +2427,13 @@ void GLCanvas3D::Gizmos::set_rotation(const Vec3d& rotation)
|
||||||
reinterpret_cast<GLGizmoRotate3D*>(it->second)->set_rotation(rotation);
|
reinterpret_cast<GLGizmoRotate3D*>(it->second)->set_rotation(rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3d GLCanvas3D::Gizmos::get_flattening_rotation() const
|
Vec3d GLCanvas3D::Gizmos::get_flattening_normal() const
|
||||||
{
|
{
|
||||||
if (!m_enabled)
|
if (!m_enabled)
|
||||||
return Vec3d::Zero();
|
return Vec3d::Zero();
|
||||||
|
|
||||||
GizmosMap::const_iterator it = m_gizmos.find(Flatten);
|
GizmosMap::const_iterator it = m_gizmos.find(Flatten);
|
||||||
return (it != m_gizmos.end()) ? reinterpret_cast<GLGizmoFlatten*>(it->second)->get_flattening_rotation() : Vec3d::Zero();
|
return (it != m_gizmos.end()) ? reinterpret_cast<GLGizmoFlatten*>(it->second)->get_flattening_normal() : Vec3d::Zero();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GLCanvas3D::Gizmos::set_flattening_data(const ModelObject* model_object)
|
void GLCanvas3D::Gizmos::set_flattening_data(const ModelObject* model_object)
|
||||||
|
@ -2520,12 +2558,12 @@ float GLCanvas3D::Gizmos::_get_total_overlay_height() const
|
||||||
|
|
||||||
for (GizmosMap::const_iterator it = m_gizmos.begin(); it != m_gizmos.end(); ++it)
|
for (GizmosMap::const_iterator it = m_gizmos.begin(); it != m_gizmos.end(); ++it)
|
||||||
{
|
{
|
||||||
height += (float)it->second->get_textures_size();
|
if (it->first == SlaSupports && wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() != ptSLA)
|
||||||
if (std::distance(it, m_gizmos.end()) > 1)
|
continue;
|
||||||
height += OverlayGapY;
|
height += (float)it->second->get_textures_size() + OverlayGapY;
|
||||||
}
|
}
|
||||||
|
|
||||||
return height;
|
return height - OverlayGapY;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLGizmoBase* GLCanvas3D::Gizmos::_get_current() const
|
GLGizmoBase* GLCanvas3D::Gizmos::_get_current() const
|
||||||
|
@ -3898,7 +3936,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
|
||||||
|
|
||||||
if (m_gizmos.get_current_type() == Gizmos::Flatten) {
|
if (m_gizmos.get_current_type() == Gizmos::Flatten) {
|
||||||
// Rotate the object so the normal points downward:
|
// Rotate the object so the normal points downward:
|
||||||
m_selection.rotate(m_gizmos.get_flattening_rotation());
|
m_selection.flattening_rotate(m_gizmos.get_flattening_normal());
|
||||||
_on_flatten();
|
_on_flatten();
|
||||||
wxGetApp().obj_manipul()->update_settings_value(m_selection);
|
wxGetApp().obj_manipul()->update_settings_value(m_selection);
|
||||||
}
|
}
|
||||||
|
@ -4977,7 +5015,7 @@ void GLCanvas3D::_update_gizmos_data()
|
||||||
{
|
{
|
||||||
m_gizmos.set_scale(Vec3d::Ones());
|
m_gizmos.set_scale(Vec3d::Ones());
|
||||||
m_gizmos.set_rotation(Vec3d::Zero());
|
m_gizmos.set_rotation(Vec3d::Zero());
|
||||||
m_gizmos.set_flattening_data(nullptr);
|
m_gizmos.set_flattening_data(m_selection.is_from_single_object() ? m_model->objects[m_selection.get_object_idx()] : nullptr);
|
||||||
m_gizmos.set_model_object_ptr(nullptr);
|
m_gizmos.set_model_object_ptr(nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -496,6 +496,8 @@ public:
|
||||||
int get_object_idx() const;
|
int get_object_idx() const;
|
||||||
// Returns the instance id if the selection is from a single object and from a single instance, otherwise is -1
|
// Returns the instance id if the selection is from a single object and from a single instance, otherwise is -1
|
||||||
int get_instance_idx() const;
|
int get_instance_idx() const;
|
||||||
|
// Returns the indices of selected instances if the selection is from a single object, throws otherwise!
|
||||||
|
const InstanceIdxsList& get_instance_idxs() const;
|
||||||
|
|
||||||
const IndicesList& get_volume_idxs() const { return m_list; }
|
const IndicesList& get_volume_idxs() const { return m_list; }
|
||||||
const GLVolume* get_volume(unsigned int volume_idx) const;
|
const GLVolume* get_volume(unsigned int volume_idx) const;
|
||||||
|
@ -507,6 +509,7 @@ public:
|
||||||
|
|
||||||
void translate(const Vec3d& displacement);
|
void translate(const Vec3d& displacement);
|
||||||
void rotate(const Vec3d& rotation);
|
void rotate(const Vec3d& rotation);
|
||||||
|
void flattening_rotate(const Vec3d& normal);
|
||||||
void scale(const Vec3d& scale);
|
void scale(const Vec3d& scale);
|
||||||
void mirror(Axis axis);
|
void mirror(Axis axis);
|
||||||
|
|
||||||
|
@ -597,7 +600,7 @@ private:
|
||||||
Vec3d get_rotation() const;
|
Vec3d get_rotation() const;
|
||||||
void set_rotation(const Vec3d& rotation);
|
void set_rotation(const Vec3d& rotation);
|
||||||
|
|
||||||
Vec3d get_flattening_rotation() const;
|
Vec3d get_flattening_normal() const;
|
||||||
|
|
||||||
void set_flattening_data(const ModelObject* model_object);
|
void set_flattening_data(const ModelObject* model_object);
|
||||||
|
|
||||||
|
|
|
@ -1175,38 +1175,41 @@ void GLGizmoFlatten::on_start_dragging(const GLCanvas3D::Selection& selection)
|
||||||
|
|
||||||
void GLGizmoFlatten::on_render(const GLCanvas3D::Selection& selection) const
|
void GLGizmoFlatten::on_render(const GLCanvas3D::Selection& selection) const
|
||||||
{
|
{
|
||||||
// the dragged_offset is a vector measuring where was the object moved
|
// The planes are rendered incorrectly when the object is being moved. We better won't render anything in that case.
|
||||||
// with the gizmo being on. This is reset in set_flattening_data and
|
// This indeed has a better solution (to be implemented when there is more time)
|
||||||
// does not work correctly when there are multiple copies.
|
|
||||||
Vec3d dragged_offset(Vec3d::Zero());
|
Vec3d dragged_offset(Vec3d::Zero());
|
||||||
if (m_starting_center == Vec3d::Zero())
|
if (m_starting_center == Vec3d::Zero())
|
||||||
m_starting_center = selection.get_bounding_box().center();
|
m_starting_center = selection.get_bounding_box().center();
|
||||||
dragged_offset = selection.get_bounding_box().center() - m_starting_center;
|
dragged_offset = selection.get_bounding_box().center() - m_starting_center;
|
||||||
|
if (dragged_offset.norm() > 0.001)
|
||||||
|
return;
|
||||||
|
|
||||||
::glEnable(GL_BLEND);
|
::glEnable(GL_BLEND);
|
||||||
::glEnable(GL_DEPTH_TEST);
|
::glEnable(GL_DEPTH_TEST);
|
||||||
::glDisable(GL_CULL_FACE);
|
::glDisable(GL_CULL_FACE);
|
||||||
|
|
||||||
for (int i=0; i<(int)m_planes.size(); ++i) {
|
if (selection.is_from_single_object()) {
|
||||||
if (i == m_hover_id)
|
const std::set<int>& instances_list = selection.get_instance_idxs();
|
||||||
::glColor4f(0.9f, 0.9f, 0.9f, 0.75f);
|
|
||||||
else
|
|
||||||
::glColor4f(0.9f, 0.9f, 0.9f, 0.5f);
|
|
||||||
|
|
||||||
int instance_idx = selection.get_instance_idx();
|
if (!instances_list.empty() && m_model_object) {
|
||||||
if ((instance_idx != -1) && (m_model_object != nullptr))
|
for (const int instance_idx : instances_list) {
|
||||||
{
|
|
||||||
Transform3d m = m_model_object->instances[instance_idx]->get_matrix();
|
Transform3d m = m_model_object->instances[instance_idx]->get_matrix();
|
||||||
m.pretranslate(dragged_offset);
|
for (int i=0; i<(int)m_planes.size(); ++i) {
|
||||||
::glPushMatrix();
|
if (i == m_hover_id)
|
||||||
::glMultMatrixd(m.data());
|
::glColor4f(0.9f, 0.9f, 0.9f, 0.75f);
|
||||||
::glBegin(GL_POLYGON);
|
else
|
||||||
for (const Vec3d& vertex : m_planes[i].vertices)
|
::glColor4f(0.9f, 0.9f, 0.9f, 0.5f);
|
||||||
{
|
|
||||||
::glVertex3dv(vertex.data());
|
m.pretranslate(dragged_offset);
|
||||||
|
::glPushMatrix();
|
||||||
|
::glMultMatrixd(m.data());
|
||||||
|
::glBegin(GL_POLYGON);
|
||||||
|
for (const Vec3d& vertex : m_planes[i].vertices)
|
||||||
|
::glVertex3dv(vertex.data());
|
||||||
|
::glEnd();
|
||||||
|
::glPopMatrix();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
::glEnd();
|
|
||||||
::glPopMatrix();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1218,22 +1221,21 @@ void GLGizmoFlatten::on_render_for_picking(const GLCanvas3D::Selection& selectio
|
||||||
{
|
{
|
||||||
::glEnable(GL_DEPTH_TEST);
|
::glEnable(GL_DEPTH_TEST);
|
||||||
::glDisable(GL_CULL_FACE);
|
::glDisable(GL_CULL_FACE);
|
||||||
|
if (selection.is_from_single_object()) {
|
||||||
for (unsigned int i = 0; i < m_planes.size(); ++i)
|
const std::set<int>& instances_list = selection.get_instance_idxs();
|
||||||
{
|
if (!instances_list.empty() && m_model_object) {
|
||||||
::glColor3f(1.0f, 1.0f, picking_color_component(i));
|
for (const int instance_idx : instances_list) {
|
||||||
int instance_idx = selection.get_instance_idx();
|
for (int i=0; i<(int)m_planes.size(); ++i) {
|
||||||
if ((instance_idx != -1) && (m_model_object != nullptr))
|
::glColor3f(1.0f, 1.0f, picking_color_component(i));
|
||||||
{
|
::glPushMatrix();
|
||||||
::glPushMatrix();
|
::glMultMatrixd(m_model_object->instances[instance_idx]->get_matrix().data());
|
||||||
::glMultMatrixd(m_model_object->instances[instance_idx]->get_matrix().data());
|
::glBegin(GL_POLYGON);
|
||||||
::glBegin(GL_POLYGON);
|
for (const Vec3d& vertex : m_planes[i].vertices)
|
||||||
for (const Vec3d& vertex : m_planes[i].vertices)
|
::glVertex3dv(vertex.data());
|
||||||
{
|
::glEnd();
|
||||||
::glVertex3dv(vertex.data());
|
::glPopMatrix();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
::glEnd();
|
|
||||||
::glPopMatrix();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1243,9 +1245,10 @@ void GLGizmoFlatten::on_render_for_picking(const GLCanvas3D::Selection& selectio
|
||||||
void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object)
|
void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object)
|
||||||
{
|
{
|
||||||
m_starting_center = Vec3d::Zero();
|
m_starting_center = Vec3d::Zero();
|
||||||
|
bool object_changed = m_model_object != model_object;
|
||||||
m_model_object = model_object;
|
m_model_object = model_object;
|
||||||
|
|
||||||
if (is_plane_update_necessary())
|
if (object_changed && is_plane_update_necessary())
|
||||||
update_planes();
|
update_planes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1456,20 +1459,14 @@ bool GLGizmoFlatten::is_plane_update_necessary() const
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3d GLGizmoFlatten::get_flattening_rotation() const
|
Vec3d GLGizmoFlatten::get_flattening_normal() const
|
||||||
{
|
{
|
||||||
// calculates the rotations in model space, taking in account the scaling factors
|
Vec3d out = m_normal;
|
||||||
Eigen::Matrix<double, 3, 3, Eigen::DontAlign> m = m_model_object->instances.front()->get_matrix(true, true).matrix().block(0, 0, 3, 3).inverse().transpose();
|
|
||||||
Eigen::Quaterniond q;
|
|
||||||
Vec3d angles = Geometry::extract_euler_angles(q.setFromTwoVectors(m * m_normal, -Vec3d::UnitZ()).toRotationMatrix());
|
|
||||||
m_normal = Vec3d::Zero();
|
m_normal = Vec3d::Zero();
|
||||||
m_starting_center = Vec3d::Zero();
|
m_starting_center = Vec3d::Zero();
|
||||||
return angles;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
GLGizmoSlaSupports::GLGizmoSlaSupports(GLCanvas3D& parent)
|
GLGizmoSlaSupports::GLGizmoSlaSupports(GLCanvas3D& parent)
|
||||||
: GLGizmoBase(parent), m_starting_center(Vec3d::Zero())
|
: GLGizmoBase(parent), m_starting_center(Vec3d::Zero())
|
||||||
{
|
{
|
||||||
|
|
|
@ -348,6 +348,7 @@ private:
|
||||||
std::vector<PlaneData> m_planes;
|
std::vector<PlaneData> m_planes;
|
||||||
mutable Vec3d m_starting_center;
|
mutable Vec3d m_starting_center;
|
||||||
const ModelObject* m_model_object = nullptr;
|
const ModelObject* m_model_object = nullptr;
|
||||||
|
std::vector<const Transform3d*> instances_matrices;
|
||||||
|
|
||||||
void update_planes();
|
void update_planes();
|
||||||
bool is_plane_update_necessary() const;
|
bool is_plane_update_necessary() const;
|
||||||
|
@ -356,12 +357,12 @@ public:
|
||||||
explicit GLGizmoFlatten(GLCanvas3D& parent);
|
explicit GLGizmoFlatten(GLCanvas3D& parent);
|
||||||
|
|
||||||
void set_flattening_data(const ModelObject* model_object);
|
void set_flattening_data(const ModelObject* model_object);
|
||||||
Vec3d get_flattening_rotation() const;
|
Vec3d get_flattening_normal() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool on_init();
|
virtual bool on_init();
|
||||||
virtual std::string on_get_name() const;
|
virtual std::string on_get_name() const;
|
||||||
virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const { return selection.is_single_full_instance(); }
|
virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const { return (selection.is_from_single_object() && !selection.is_wipe_tower() && !selection.is_modifier()); }
|
||||||
virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
|
virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
|
||||||
virtual void on_update(const Linef3& mouse_ray, const Point* mouse_pos) {}
|
virtual void on_update(const Linef3& mouse_ray, const Point* mouse_pos) {}
|
||||||
virtual void on_render(const GLCanvas3D::Selection& selection) const;
|
virtual void on_render(const GLCanvas3D::Selection& selection) const;
|
||||||
|
|
Loading…
Reference in a new issue