Flatten gizmo should now work with multiple selection

This commit is contained in:
Lukas Matena 2018-11-02 15:20:26 +01:00
parent 385b0f261d
commit 622f4ee4f6
4 changed files with 95 additions and 56 deletions

View file

@ -1394,6 +1394,14 @@ int GLCanvas3D::Selection::get_instance_idx() const
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
{
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;
}
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)
{
if (!m_valid)
@ -2389,13 +2427,13 @@ void GLCanvas3D::Gizmos::set_rotation(const Vec3d& 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)
return Vec3d::Zero();
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)
@ -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)
{
height += (float)it->second->get_textures_size();
if (std::distance(it, m_gizmos.end()) > 1)
height += OverlayGapY;
if (it->first == SlaSupports && wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() != ptSLA)
continue;
height += (float)it->second->get_textures_size() + OverlayGapY;
}
return height;
return height - OverlayGapY;
}
GLGizmoBase* GLCanvas3D::Gizmos::_get_current() const
@ -3898,7 +3936,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
if (m_gizmos.get_current_type() == Gizmos::Flatten) {
// 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();
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_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);
}
}

View file

@ -496,6 +496,8 @@ public:
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
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 GLVolume* get_volume(unsigned int volume_idx) const;
@ -507,6 +509,7 @@ public:
void translate(const Vec3d& displacement);
void rotate(const Vec3d& rotation);
void flattening_rotate(const Vec3d& normal);
void scale(const Vec3d& scale);
void mirror(Axis axis);
@ -597,7 +600,7 @@ private:
Vec3d get_rotation() const;
void set_rotation(const Vec3d& rotation);
Vec3d get_flattening_rotation() const;
Vec3d get_flattening_normal() const;
void set_flattening_data(const ModelObject* model_object);

View file

@ -1175,38 +1175,41 @@ void GLGizmoFlatten::on_start_dragging(const GLCanvas3D::Selection& selection)
void GLGizmoFlatten::on_render(const GLCanvas3D::Selection& selection) const
{
// the dragged_offset is a vector measuring where was the object moved
// with the gizmo being on. This is reset in set_flattening_data and
// does not work correctly when there are multiple copies.
// The planes are rendered incorrectly when the object is being moved. We better won't render anything in that case.
// This indeed has a better solution (to be implemented when there is more time)
Vec3d dragged_offset(Vec3d::Zero());
if (m_starting_center == Vec3d::Zero())
m_starting_center = selection.get_bounding_box().center();
dragged_offset = selection.get_bounding_box().center() - m_starting_center;
if (dragged_offset.norm() > 0.001)
return;
::glEnable(GL_BLEND);
::glEnable(GL_DEPTH_TEST);
::glDisable(GL_CULL_FACE);
for (int i=0; i<(int)m_planes.size(); ++i) {
if (i == m_hover_id)
::glColor4f(0.9f, 0.9f, 0.9f, 0.75f);
else
::glColor4f(0.9f, 0.9f, 0.9f, 0.5f);
if (selection.is_from_single_object()) {
const std::set<int>& instances_list = selection.get_instance_idxs();
int instance_idx = selection.get_instance_idx();
if ((instance_idx != -1) && (m_model_object != nullptr))
{
if (!instances_list.empty() && m_model_object) {
for (const int instance_idx : instances_list) {
Transform3d m = m_model_object->instances[instance_idx]->get_matrix();
m.pretranslate(dragged_offset);
::glPushMatrix();
::glMultMatrixd(m.data());
::glBegin(GL_POLYGON);
for (const Vec3d& vertex : m_planes[i].vertices)
{
::glVertex3dv(vertex.data());
for (int i=0; i<(int)m_planes.size(); ++i) {
if (i == m_hover_id)
::glColor4f(0.9f, 0.9f, 0.9f, 0.75f);
else
::glColor4f(0.9f, 0.9f, 0.9f, 0.5f);
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);
::glDisable(GL_CULL_FACE);
for (unsigned int i = 0; i < m_planes.size(); ++i)
{
::glColor3f(1.0f, 1.0f, picking_color_component(i));
int instance_idx = selection.get_instance_idx();
if ((instance_idx != -1) && (m_model_object != nullptr))
{
::glPushMatrix();
::glMultMatrixd(m_model_object->instances[instance_idx]->get_matrix().data());
::glBegin(GL_POLYGON);
for (const Vec3d& vertex : m_planes[i].vertices)
{
::glVertex3dv(vertex.data());
if (selection.is_from_single_object()) {
const std::set<int>& instances_list = selection.get_instance_idxs();
if (!instances_list.empty() && m_model_object) {
for (const int instance_idx : instances_list) {
for (int i=0; i<(int)m_planes.size(); ++i) {
::glColor3f(1.0f, 1.0f, picking_color_component(i));
::glPushMatrix();
::glMultMatrixd(m_model_object->instances[instance_idx]->get_matrix().data());
::glBegin(GL_POLYGON);
for (const Vec3d& vertex : m_planes[i].vertices)
::glVertex3dv(vertex.data());
::glEnd();
::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)
{
m_starting_center = Vec3d::Zero();
bool object_changed = m_model_object != model_object;
m_model_object = model_object;
if (is_plane_update_necessary())
if (object_changed && is_plane_update_necessary())
update_planes();
}
@ -1456,20 +1459,14 @@ bool GLGizmoFlatten::is_plane_update_necessary() const
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
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());
Vec3d out = m_normal;
m_normal = Vec3d::Zero();
m_starting_center = Vec3d::Zero();
return angles;
return out;
}
GLGizmoSlaSupports::GLGizmoSlaSupports(GLCanvas3D& parent)
: GLGizmoBase(parent), m_starting_center(Vec3d::Zero())
{

View file

@ -348,6 +348,7 @@ private:
std::vector<PlaneData> m_planes;
mutable Vec3d m_starting_center;
const ModelObject* m_model_object = nullptr;
std::vector<const Transform3d*> instances_matrices;
void update_planes();
bool is_plane_update_necessary() const;
@ -356,12 +357,12 @@ public:
explicit GLGizmoFlatten(GLCanvas3D& parent);
void set_flattening_data(const ModelObject* model_object);
Vec3d get_flattening_rotation() const;
Vec3d get_flattening_normal() const;
protected:
virtual bool on_init();
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_update(const Linef3& mouse_ray, const Point* mouse_pos) {}
virtual void on_render(const GLCanvas3D::Selection& selection) const;