Slight refactoring

This commit is contained in:
Lukas Matena 2020-09-30 22:10:03 +02:00
parent 1ca8120398
commit 6744a40cd5

View File

@ -314,9 +314,15 @@ bool GLGizmoPainterBase::gizmo_event(SLAGizmoEventType action, const Vec2d& mous
} }
m_last_mouse_position = Vec2d::Zero(); // only actual hits should be saved m_last_mouse_position = Vec2d::Zero(); // only actual hits should be saved
// Precalculate transformations of individual meshes.
std::vector<Transform3d> trafo_matrices;
for (const ModelVolume* mv : mo->volumes) {
if (mv->is_model_part())
trafo_matrices.emplace_back(instance_trafo * mv->get_matrix());
}
// Now "click" into all the prepared points and spill paint around them. // Now "click" into all the prepared points and spill paint around them.
for (const Vec2d& mp : mouse_positions) { for (const Vec2d& mp : mouse_positions) {
std::vector<std::vector<std::pair<Vec3f, size_t>>> hit_positions_and_facet_ids;
bool clipped_mesh_was_hit = false; bool clipped_mesh_was_hit = false;
Vec3f normal = Vec3f::Zero(); Vec3f normal = Vec3f::Zero();
@ -327,9 +333,6 @@ bool GLGizmoPainterBase::gizmo_event(SLAGizmoEventType action, const Vec2d& mous
size_t closest_facet = 0; size_t closest_facet = 0;
int closest_hit_mesh_id = -1; int closest_hit_mesh_id = -1;
// Transformations of individual meshes
std::vector<Transform3d> trafo_matrices;
int mesh_id = -1; int mesh_id = -1;
// Cast a ray on all meshes, pick the closest hit and save it for the respective mesh // Cast a ray on all meshes, pick the closest hit and save it for the respective mesh
for (const ModelVolume* mv : mo->volumes) { for (const ModelVolume* mv : mo->volumes) {
@ -338,9 +341,6 @@ bool GLGizmoPainterBase::gizmo_event(SLAGizmoEventType action, const Vec2d& mous
++mesh_id; ++mesh_id;
trafo_matrices.push_back(instance_trafo * mv->get_matrix());
hit_positions_and_facet_ids.push_back(std::vector<std::pair<Vec3f, size_t>>());
if (m_c->raycaster()->raycasters()[mesh_id]->unproject_on_mesh( if (m_c->raycaster()->raycasters()[mesh_id]->unproject_on_mesh(
mp, mp,
trafo_matrices[mesh_id], trafo_matrices[mesh_id],
@ -366,18 +366,19 @@ bool GLGizmoPainterBase::gizmo_event(SLAGizmoEventType action, const Vec2d& mous
} }
} }
} }
mesh_id = closest_hit_mesh_id;
bool dragging_while_painting = (action == SLAGizmoEventType::Dragging && m_button_down != Button::None); bool dragging_while_painting = (action == SLAGizmoEventType::Dragging && m_button_down != Button::None);
// The mouse button click detection is enabled when there is a valid hit // The mouse button click detection is enabled when there is a valid hit
// or when the user clicks the clipping plane. Missing the object entirely // or when the user clicks the clipping plane. Missing the object entirely
// shall not capture the mouse. // shall not capture the mouse.
if (closest_hit_mesh_id != -1 || clipped_mesh_was_hit) { if (mesh_id != -1 || clipped_mesh_was_hit) {
if (m_button_down == Button::None) if (m_button_down == Button::None)
m_button_down = ((action == SLAGizmoEventType::LeftDown) ? Button::Left : Button::Right); m_button_down = ((action == SLAGizmoEventType::LeftDown) ? Button::Left : Button::Right);
} }
if (closest_hit_mesh_id == -1) { if (mesh_id == -1) {
// In case we have no valid hit, we can return. The event will // In case we have no valid hit, we can return. The event will
// be stopped in following two cases: // be stopped in following two cases:
// 1. clicking the clipping plane // 1. clicking the clipping plane
@ -386,16 +387,6 @@ bool GLGizmoPainterBase::gizmo_event(SLAGizmoEventType action, const Vec2d& mous
|| dragging_while_painting; || dragging_while_painting;
} }
// Find respective mesh id.
mesh_id = -1;
for (const ModelVolume* mv : mo->volumes) {
if (! mv->is_model_part())
continue;
++mesh_id;
if (mesh_id == closest_hit_mesh_id)
break;
}
const Transform3d& trafo_matrix = trafo_matrices[mesh_id]; const Transform3d& trafo_matrix = trafo_matrices[mesh_id];
// Calculate how far can a point be from the line (in mesh coords). // Calculate how far can a point be from the line (in mesh coords).