Snap in gizmo scale 3D when holding Shift pressed

This commit is contained in:
Enrico Turri 2018-11-14 12:57:12 +01:00
parent 62aa34b444
commit 4149b9ad28
4 changed files with 72 additions and 58 deletions

View file

@ -2550,14 +2550,14 @@ bool GLCanvas3D::Gizmos::grabber_contains_mouse() const
return (curr != nullptr) ? (curr->get_hover_id() != -1) : false;
}
void GLCanvas3D::Gizmos::update(const Linef3& mouse_ray, const Point* mouse_pos)
void GLCanvas3D::Gizmos::update(const Linef3& mouse_ray, bool shift_down, const Point* mouse_pos)
{
if (!m_enabled)
return;
GLGizmoBase* curr = _get_current();
if (curr != nullptr)
curr->update(mouse_ray, mouse_pos);
curr->update(GLGizmoBase::UpdateData(mouse_ray, mouse_pos, shift_down));
}
#if ENABLE_GIZMOS_RESET
@ -4313,7 +4313,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
m_canvas->CaptureMouse();
m_mouse.dragging = true;
m_gizmos.update(mouse_ray(pos), &pos);
m_gizmos.update(mouse_ray(pos), evt.ShiftDown(), &pos);
switch (m_gizmos.get_current_type())
{

View file

@ -602,7 +602,7 @@ private:
bool overlay_contains_mouse(const GLCanvas3D& canvas, const Vec2d& mouse_pos) const;
bool grabber_contains_mouse() const;
void update(const Linef3& mouse_ray, const Point* mouse_pos = nullptr);
void update(const Linef3& mouse_ray, bool shift_down, const Point* mouse_pos = nullptr);
#if ENABLE_GIZMOS_RESET
void process_double_click();
#endif // ENABLE_GIZMOS_RESET

View file

@ -201,10 +201,10 @@ void GLGizmoBase::stop_dragging()
on_stop_dragging();
}
void GLGizmoBase::update(const Linef3& mouse_ray, const Point* mouse_pos)
void GLGizmoBase::update(const UpdateData& data)
{
if (m_hover_id != -1)
on_update(mouse_ray, mouse_pos);
on_update(data);
}
float GLGizmoBase::picking_color_component(unsigned int id) const
@ -303,9 +303,9 @@ void GLGizmoRotate::on_start_dragging(const GLCanvas3D::Selection& selection)
m_snap_fine_out_radius = m_snap_fine_in_radius + m_radius * ScaleLongTooth;
}
void GLGizmoRotate::on_update(const Linef3& mouse_ray, const Point* mouse_position)
{
Vec2d mouse_pos = to_2d(mouse_position_in_local_plane(mouse_ray));
void GLGizmoRotate::on_update(const UpdateData& data)
{
Vec2d mouse_pos = to_2d(mouse_position_in_local_plane(data.mouse_ray));
Vec2d orig_dir = Vec2d::UnitX();
Vec2d new_dir = mouse_pos.normalized();
@ -650,6 +650,7 @@ const float GLGizmoScale3D::Offset = 5.0f;
GLGizmoScale3D::GLGizmoScale3D(GLCanvas3D& parent)
: GLGizmoBase(parent)
, m_scale(Vec3d::Ones())
, m_snap_step(0.05)
, m_starting_scale(Vec3d::Ones())
{
}
@ -702,16 +703,16 @@ void GLGizmoScale3D::on_start_dragging(const GLCanvas3D::Selection& selection)
}
}
void GLGizmoScale3D::on_update(const Linef3& mouse_ray, const Point* mouse_pos)
void GLGizmoScale3D::on_update(const UpdateData& data)
{
if ((m_hover_id == 0) || (m_hover_id == 1))
do_scale_x(mouse_ray);
do_scale_x(data);
else if ((m_hover_id == 2) || (m_hover_id == 3))
do_scale_y(mouse_ray);
do_scale_y(data);
else if ((m_hover_id == 4) || (m_hover_id == 5))
do_scale_z(mouse_ray);
do_scale_z(data);
else if (m_hover_id >= 6)
do_scale_uniform(mouse_ray);
do_scale_uniform(data);
}
#if ENABLE_GIZMOS_RESET
@ -940,39 +941,35 @@ void GLGizmoScale3D::render_grabbers_connection(unsigned int id_1, unsigned int
}
}
void GLGizmoScale3D::do_scale_x(const Linef3& mouse_ray)
void GLGizmoScale3D::do_scale_x(const UpdateData& data)
{
double ratio = calc_ratio(mouse_ray);
double ratio = calc_ratio(data);
if (ratio > 0.0)
m_scale(0) = m_starting_scale(0) * ratio;
}
void GLGizmoScale3D::do_scale_y(const Linef3& mouse_ray)
void GLGizmoScale3D::do_scale_y(const UpdateData& data)
{
double ratio = calc_ratio(mouse_ray);
double ratio = calc_ratio(data);
if (ratio > 0.0)
m_scale(1) = m_starting_scale(1) * ratio;
}
void GLGizmoScale3D::do_scale_z(const Linef3& mouse_ray)
void GLGizmoScale3D::do_scale_z(const UpdateData& data)
{
double ratio = calc_ratio(mouse_ray);
double ratio = calc_ratio(data);
if (ratio > 0.0)
m_scale(2) = m_starting_scale(2) * ratio;
}
void GLGizmoScale3D::do_scale_uniform(const Linef3& mouse_ray)
void GLGizmoScale3D::do_scale_uniform(const UpdateData& data)
{
double ratio = calc_ratio(mouse_ray);
double ratio = calc_ratio(data);
if (ratio > 0.0)
m_scale = m_starting_scale * ratio;
}
double GLGizmoScale3D::calc_ratio(const Linef3& mouse_ray) const
double GLGizmoScale3D::calc_ratio(const UpdateData& data) const
{
double ratio = 0.0;
@ -981,21 +978,24 @@ double GLGizmoScale3D::calc_ratio(const Linef3& mouse_ray) const
double len_starting_vec = starting_vec.norm();
if (len_starting_vec != 0.0)
{
Vec3d mouse_dir = mouse_ray.unit_vector();
Vec3d mouse_dir = data.mouse_ray.unit_vector();
// finds the intersection of the mouse ray with the plane parallel to the camera viewport and passing throught the starting position
// use ray-plane intersection see i.e. https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection algebric form
// in our case plane normal and ray direction are the same (orthogonal view)
// when moving to perspective camera the negative z unit axis of the camera needs to be transformed in world space and used as plane normal
Vec3d inters = mouse_ray.a + (m_starting_drag_position - mouse_ray.a).dot(mouse_dir) / mouse_dir.squaredNorm() * mouse_dir;
Vec3d inters = data.mouse_ray.a + (m_starting_drag_position - data.mouse_ray.a).dot(mouse_dir) / mouse_dir.squaredNorm() * mouse_dir;
// vector from the starting position to the found intersection
Vec3d inters_vec = inters - m_starting_drag_position;
// finds projection of the vector along the staring direction
double proj = inters_vec.dot(starting_vec.normalized());
return (len_starting_vec + proj) / len_starting_vec;
ratio = (len_starting_vec + proj) / len_starting_vec;
}
if (data.shift_down)
ratio = m_snap_step * (double)std::round(ratio / m_snap_step);
return ratio;
}
@ -1057,14 +1057,14 @@ void GLGizmoMove3D::on_stop_dragging()
m_displacement = Vec3d::Zero();
}
void GLGizmoMove3D::on_update(const Linef3& mouse_ray, const Point* mouse_pos)
void GLGizmoMove3D::on_update(const UpdateData& data)
{
if (m_hover_id == 0)
m_displacement(0) = calc_projection(mouse_ray);
m_displacement(0) = calc_projection(data);
else if (m_hover_id == 1)
m_displacement(1) = calc_projection(mouse_ray);
m_displacement(1) = calc_projection(data);
else if (m_hover_id == 2)
m_displacement(2) = calc_projection(mouse_ray);
m_displacement(2) = calc_projection(data);
}
void GLGizmoMove3D::on_render(const GLCanvas3D::Selection& selection) const
@ -1140,7 +1140,7 @@ void GLGizmoMove3D::on_render_for_picking(const GLCanvas3D::Selection& selection
render_grabbers_for_picking(selection.get_bounding_box());
}
double GLGizmoMove3D::calc_projection(const Linef3& mouse_ray) const
double GLGizmoMove3D::calc_projection(const UpdateData& data) const
{
double projection = 0.0;
@ -1148,12 +1148,12 @@ double GLGizmoMove3D::calc_projection(const Linef3& mouse_ray) const
double len_starting_vec = starting_vec.norm();
if (len_starting_vec != 0.0)
{
Vec3d mouse_dir = mouse_ray.unit_vector();
Vec3d mouse_dir = data.mouse_ray.unit_vector();
// finds the intersection of the mouse ray with the plane parallel to the camera viewport and passing throught the starting position
// use ray-plane intersection see i.e. https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection algebric form
// in our case plane normal and ray direction are the same (orthogonal view)
// when moving to perspective camera the negative z unit axis of the camera needs to be transformed in world space and used as plane normal
Vec3d inters = mouse_ray.a + (m_starting_drag_position - mouse_ray.a).dot(mouse_dir) / mouse_dir.squaredNorm() * mouse_dir;
Vec3d inters = data.mouse_ray.a + (m_starting_drag_position - data.mouse_ray.a).dot(mouse_dir) / mouse_dir.squaredNorm() * mouse_dir;
// vector from the starting position to the found intersection
Vec3d inters_vec = inters - m_starting_drag_position;
@ -1718,12 +1718,12 @@ void GLGizmoSlaSupports::delete_current_grabber(bool delete_all)
}
}
void GLGizmoSlaSupports::on_update(const Linef3& mouse_ray, const Point* mouse_pos)
void GLGizmoSlaSupports::on_update(const UpdateData& data)
{
if (m_hover_id != -1 && mouse_pos) {
if (m_hover_id != -1 && data.mouse_pos) {
Vec3f new_pos;
try {
new_pos = unproject_on_mesh(Vec2d((*mouse_pos)(0), (*mouse_pos)(1)));
new_pos = unproject_on_mesh(Vec2d((*data.mouse_pos)(0), (*data.mouse_pos)(1)));
m_grabbers[m_hover_id].center = new_pos.cast<double>();
m_model_object->sla_support_points[m_hover_id] = new_pos;
}

View file

@ -52,6 +52,17 @@ public:
Num_States
};
struct UpdateData
{
const Linef3 mouse_ray;
const Point* mouse_pos;
bool shift_down;
UpdateData(const Linef3& mouse_ray, const Point* mouse_pos = nullptr, bool shift_down = false)
: mouse_ray(mouse_ray), mouse_pos(mouse_pos), shift_down(shift_down)
{}
};
protected:
GLCanvas3D& m_parent;
@ -78,9 +89,7 @@ public:
void set_group_id(int id) { m_group_id = id; }
EState get_state() const { return m_state; }
void set_state(EState state) {
m_state = state; on_set_state();
}
void set_state(EState state) { m_state = state; on_set_state(); }
bool is_activable(const GLCanvas3D::Selection& selection) const { return on_is_activable(selection); }
@ -99,7 +108,7 @@ public:
void stop_dragging();
bool is_dragging() const { return m_dragging; }
void update(const Linef3& mouse_ray, const Point* mouse_pos);
void update(const UpdateData& data);
#if ENABLE_GIZMOS_RESET
void process_double_click() { on_process_double_click(); }
@ -118,7 +127,7 @@ protected:
virtual void on_disable_grabber(unsigned int id) {}
virtual void on_start_dragging(const GLCanvas3D::Selection& selection) {}
virtual void on_stop_dragging() {}
virtual void on_update(const Linef3& mouse_ray, const Point* mouse_pos) = 0;
virtual void on_update(const UpdateData& data) = 0;
#if ENABLE_GIZMOS_RESET
virtual void on_process_double_click() {}
#endif // ENABLE_GIZMOS_RESET
@ -175,7 +184,7 @@ protected:
virtual bool on_init();
virtual std::string on_get_name() const { return ""; }
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 UpdateData& data);
#if ENABLE_GIZMOS_RESET
virtual void on_process_double_click() { m_angle = 0.0; }
#endif // ENABLE_GIZMOS_RESET
@ -235,11 +244,11 @@ protected:
}
virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
virtual void on_stop_dragging();
virtual void on_update(const Linef3& mouse_ray, const Point* mouse_pos)
virtual void on_update(const UpdateData& data)
{
for (GLGizmoRotate& g : m_gizmos)
{
g.update(mouse_ray, mouse_pos);
g.update(data);
}
}
#if ENABLE_GIZMOS_RESET
@ -267,6 +276,8 @@ class GLGizmoScale3D : public GLGizmoBase
Vec3d m_scale;
double m_snap_step;
Vec3d m_starting_scale;
Vec3d m_starting_drag_position;
BoundingBoxf3 m_starting_box;
@ -274,6 +285,9 @@ class GLGizmoScale3D : public GLGizmoBase
public:
explicit GLGizmoScale3D(GLCanvas3D& parent);
double get_snap_step(double step) const { return m_snap_step; }
void set_snap_step(double step) { m_snap_step = step; }
const Vec3d& get_scale() const { return m_scale; }
void set_scale(const Vec3d& scale) { m_starting_scale = scale; m_scale = scale; }
@ -282,7 +296,7 @@ protected:
virtual std::string on_get_name() const;
virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const { return !selection.is_wipe_tower(); }
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 UpdateData& data);
#if ENABLE_GIZMOS_RESET
virtual void on_process_double_click();
#endif // ENABLE_GIZMOS_RESET
@ -292,12 +306,12 @@ protected:
private:
void render_grabbers_connection(unsigned int id_1, unsigned int id_2) const;
void do_scale_x(const Linef3& mouse_ray);
void do_scale_y(const Linef3& mouse_ray);
void do_scale_z(const Linef3& mouse_ray);
void do_scale_uniform(const Linef3& mouse_ray);
void do_scale_x(const UpdateData& data);
void do_scale_y(const UpdateData& data);
void do_scale_z(const UpdateData& data);
void do_scale_uniform(const UpdateData& data);
double calc_ratio(const Linef3& mouse_ray) const;
double calc_ratio(const UpdateData& data) const;
};
class GLGizmoMove3D : public GLGizmoBase
@ -319,12 +333,12 @@ protected:
virtual std::string on_get_name() const;
virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
virtual void on_stop_dragging();
virtual void on_update(const Linef3& mouse_ray, const Point* mouse_pos);
virtual void on_update(const UpdateData& data);
virtual void on_render(const GLCanvas3D::Selection& selection) const;
virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;
private:
double calc_projection(const Linef3& mouse_ray) const;
double calc_projection(const UpdateData& data) const;
};
class GLGizmoFlatten : public GLGizmoBase
@ -366,7 +380,7 @@ protected:
virtual std::string on_get_name() const;
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_update(const UpdateData& data) {}
virtual void on_render(const GLCanvas3D::Selection& selection) const;
virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;
virtual void on_set_state()
@ -404,7 +418,7 @@ public:
private:
bool on_init();
void on_update(const Linef3& mouse_ray, const Point* mouse_pos);
void on_update(const UpdateData& data);
virtual void on_render(const GLCanvas3D::Selection& selection) const;
virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;