Cut: Sanitize Z when switching selections

This commit is contained in:
Vojtech Kral 2018-11-29 12:21:42 +01:00
parent ef1d43c711
commit be9ba936e9
2 changed files with 21 additions and 6 deletions

View File

@ -2001,6 +2001,7 @@ const std::array<float, 3> GLGizmoCut::GrabberColor = { 1.0, 0.5, 0.0 };
GLGizmoCut::GLGizmoCut(GLCanvas3D& parent)
: GLGizmoBase(parent)
, m_cut_z(0.0)
, m_max_z(0.0)
#if !ENABLE_IMGUI
, m_panel(nullptr)
#endif // not ENABLE_IMGUI
@ -2087,7 +2088,7 @@ void GLGizmoCut::on_start_dragging(const GLCanvas3D::Selection& selection)
const BoundingBoxf3& box = selection.get_bounding_box();
m_start_z = m_cut_z;
m_max_z = box.size()(2);
update_max_z(selection);
m_drag_pos = m_grabbers[m_hover_id].center;
m_drag_center = box.center();
m_drag_center(2) = m_cut_z;
@ -2096,9 +2097,7 @@ void GLGizmoCut::on_start_dragging(const GLCanvas3D::Selection& selection)
void GLGizmoCut::on_update(const UpdateData& data)
{
if (m_hover_id != -1) {
// Clamp the plane to the object's bounding box
const double new_z = m_start_z + calc_projection(data.mouse_ray);
m_cut_z = std::max(0.0, std::min(m_max_z, new_z));
set_cut_z(m_start_z + calc_projection(data.mouse_ray));
}
}
@ -2108,6 +2107,8 @@ void GLGizmoCut::on_render(const GLCanvas3D::Selection& selection) const
set_tooltip("Z: " + format(m_cut_z, 2));
}
update_max_z(selection);
const BoundingBoxf3& box = selection.get_bounding_box();
Vec3d plane_center = box.center();
plane_center(2) = m_cut_z;
@ -2182,6 +2183,18 @@ void GLGizmoCut::on_render_input_window(float x, float y, const GLCanvas3D::Sele
}
#endif // ENABLE_IMGUI
void GLGizmoCut::update_max_z(const GLCanvas3D::Selection& selection) const
{
m_max_z = selection.get_bounding_box().size()(2);
set_cut_z(m_cut_z);
}
void GLGizmoCut::set_cut_z(double cut_z) const
{
// Clamp the plane to the object's bounding box
m_cut_z = std::max(0.0, std::min(m_max_z, cut_z));
}
void GLGizmoCut::perform_cut(const GLCanvas3D::Selection& selection)
{
const auto instance_idx = selection.get_instance_idx();

View File

@ -519,9 +519,9 @@ class GLGizmoCut : public GLGizmoBase
static const double Margin;
static const std::array<float, 3> GrabberColor;
double m_cut_z;
mutable double m_cut_z;
double m_start_z;
double m_max_z;
mutable double m_max_z;
Vec3d m_drag_pos;
Vec3d m_drag_center;
bool m_keep_upper;
@ -554,6 +554,8 @@ protected:
virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection);
#endif // ENABLE_IMGUI
private:
void update_max_z(const GLCanvas3D::Selection& selection) const;
void set_cut_z(double cut_z) const;
void perform_cut(const GLCanvas3D::Selection& selection);
double calc_projection(const Linef3& mouse_ray) const;
};