Cut gizmo: cut by line does not rely on mesh raycasters

This commit is contained in:
Lukas Matena 2022-05-16 11:44:30 +02:00
parent 496481e972
commit ef26b1abeb
3 changed files with 28 additions and 40 deletions

View File

@ -1391,49 +1391,37 @@ bool GLGizmoCut3D::process_cut_line(SLAGizmoEventType action, const Vec2d& mouse
const Camera& camera = wxGetApp().plater()->get_camera(); const Camera& camera = wxGetApp().plater()->get_camera();
int mesh_id = -1; Vec3d pt;
for (const ModelVolume* mv : mo->volumes) { Vec3d dir;
++mesh_id; MeshRaycaster::line_from_mouse_pos(mouse_position, Transform3d::Identity(), camera, pt, dir);
if (!mv->is_model_part()) dir.normalize();
continue; pt += dir; // Move the pt along dir so it is not clipped.
const Transform3d trafo = inst_trafo * mv->get_matrix(); if (action == SLAGizmoEventType::LeftDown && !cut_line_processing()) {
const MeshRaycaster* raycaster = m_c->raycaster()->raycasters()[mesh_id]; m_line_beg = pt;
m_line_end = pt;
return true;
}
Vec3d point; if (cut_line_processing()) {
Vec3d direction; m_line_end = pt;
if (raycaster->unproject_on_mesh(mouse_position, trafo, camera, point, direction)) if (action == SLAGizmoEventType::LeftDown) {
{ Vec3d point = m_line_end;
point += mi->get_offset(); Vec3d line_dir = m_line_end - m_line_beg;
point[Z] += sla_shift; Plater::TakeSnapshot snapshot(wxGetApp().plater(), _L("Cut by line"), UndoRedo::SnapshotType::GizmoAction);
if (action == SLAGizmoEventType::LeftDown && !cut_line_processing()) { Vec3d cross_dir = line_dir.cross(dir).normalized();
m_line_beg = point; Eigen::Quaterniond q;
return true; Transform3d m = Transform3d::Identity();
} m.matrix().block(0, 0, 3, 3) = q.setFromTwoVectors(Vec3d::UnitZ(), cross_dir).toRotationMatrix();
if (action == SLAGizmoEventType::Moving && cut_line_processing()) {
m_line_end = point;
return true;
}
if (action == SLAGizmoEventType::LeftDown && cut_line_processing()) {
Vec3f camera_dir = camera.get_dir_forward().cast<float>();
Vec3f line_dir = (m_line_end - m_line_beg).cast<float>();
Plater::TakeSnapshot snapshot(wxGetApp().plater(), _L("Cut by line"), UndoRedo::SnapshotType::GizmoAction); m_rotation_gizmo.set_rotation(Geometry::Transformation(m).get_rotation());
Vec3f cross_dir = line_dir.cross(camera_dir).normalized(); set_center(m_plane_center + cross_dir * (cross_dir.dot(pt - m_plane_center)));
Eigen::Quaterniond q;
Transform3d m = Transform3d::Identity();
m.matrix().block(0, 0, 3, 3) = q.setFromTwoVectors(Vec3d::UnitZ(), cross_dir.cast<double>()).toRotationMatrix();
m_rotation_gizmo.set_rotation(Geometry::Transformation(m).get_rotation()); m_line_end = m_line_beg = Vec3d::Zero();
set_center(Vec3d(0.5 * (point[X] + m_line_beg[X]), 0.5 * (point[Y] + m_line_beg[Y]), 0.5 * (point[Z] + m_line_beg[Z])));
m_line_end = m_line_beg = Vec3d::Zero();
return true;
}
} }
return true;
} }
return false; return false;
} }
@ -1461,7 +1449,7 @@ bool GLGizmoCut3D::gizmo_event(SLAGizmoEventType action, const Vec2d& mouse_posi
if (unproject_on_cut_plane(mouse_position.cast<double>(), pos_and_normal)) { if (unproject_on_cut_plane(mouse_position.cast<double>(), pos_and_normal)) {
const Vec3d& hit = pos_and_normal.first; const Vec3d& hit = pos_and_normal.first;
// The clipping plane was clicked, hit containts coordinates of the hit in world coords. // The clipping plane was clicked, hit containts coordinates of the hit in world coords.
std::cout << hit.x() << "\t" << hit.y() << "\t" << hit.z() << std::endl; //std::cout << hit.x() << "\t" << hit.y() << "\t" << hit.z() << std::endl;
Plater::TakeSnapshot snapshot(wxGetApp().plater(), _L("Add connector"), UndoRedo::SnapshotType::GizmoAction); Plater::TakeSnapshot snapshot(wxGetApp().plater(), _L("Add connector"), UndoRedo::SnapshotType::GizmoAction);
connectors.emplace_back(hit, m_rotation_gizmo.get_rotation(), float(m_connector_size * 0.5), float(m_connector_depth_ratio)); connectors.emplace_back(hit, m_rotation_gizmo.get_rotation(), float(m_connector_size * 0.5), float(m_connector_depth_ratio));

View File

@ -324,7 +324,7 @@ Vec3f MeshRaycaster::get_triangle_normal(size_t facet_idx) const
} }
void MeshRaycaster::line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera, void MeshRaycaster::line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera,
Vec3d& point, Vec3d& direction) const Vec3d& point, Vec3d& direction)
{ {
Matrix4d modelview = camera.get_view_matrix().matrix(); Matrix4d modelview = camera.get_view_matrix().matrix();
Matrix4d projection= camera.get_projection_matrix().matrix(); Matrix4d projection= camera.get_projection_matrix().matrix();

View File

@ -138,8 +138,8 @@ public:
{ {
} }
void line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera, static void line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera,
Vec3d& point, Vec3d& direction) const; Vec3d& point, Vec3d& direction);
// Given a mouse position, this returns true in case it is on the mesh. // Given a mouse position, this returns true in case it is on the mesh.
bool unproject_on_mesh( bool unproject_on_mesh(