Refactoring of GLGizmoMeasure to simplify code

This commit is contained in:
enricoturri1966 2022-08-30 15:38:29 +02:00
parent 4675ae2173
commit db1b2fbfc1
2 changed files with 167 additions and 169 deletions

View File

@ -55,8 +55,7 @@ GLGizmoMeasure::GLGizmoMeasure(GLCanvas3D& parent, const std::string& icon_filen
bool GLGizmoMeasure::on_mouse(const wxMouseEvent &mouse_event) bool GLGizmoMeasure::on_mouse(const wxMouseEvent &mouse_event)
{ {
m_mouse_pos_x = mouse_event.GetX(); m_mouse_pos = { double(mouse_event.GetX()), double(mouse_event.GetY()) };
m_mouse_pos_y = mouse_event.GetY();
if (mouse_event.Moving()) { if (mouse_event.Moving()) {
// only for sure // only for sure
@ -106,7 +105,7 @@ bool GLGizmoMeasure::gizmo_event(SLAGizmoEventType action, const Vec2d& mouse_po
{ {
if (action == SLAGizmoEventType::CtrlDown) { if (action == SLAGizmoEventType::CtrlDown) {
if (m_ctrl_kar_filter.is_first()) { if (m_ctrl_kar_filter.is_first()) {
if (!m_features.empty()) if (m_curr_feature.has_value())
m_mode = EMode::ExtendedSelection; m_mode = EMode::ExtendedSelection;
} }
@ -171,24 +170,20 @@ void GLGizmoMeasure::on_render()
Vec3f pos; Vec3f pos;
Vec3f normal; Vec3f normal;
size_t facet_idx; size_t facet_idx;
m_c->raycaster()->raycasters().front()->unproject_on_mesh(Vec2d(m_mouse_pos_x, m_mouse_pos_y), model_matrix, camera, pos, normal, nullptr, &facet_idx); m_c->raycaster()->raycasters().front()->unproject_on_mesh(m_mouse_pos, model_matrix, camera, pos, normal, nullptr, &facet_idx);
std::vector<Measure::SurfaceFeature> features; std::optional<Measure::SurfaceFeature> curr_feature;
if (m_mode == EMode::BasicSelection) { if (m_mode == EMode::BasicSelection)
std::optional<Measure::SurfaceFeature> feat = m_measuring->get_feature(facet_idx, pos.cast<double>()); curr_feature = m_measuring->get_feature(facet_idx, pos.cast<double>());
if (feat.has_value())
features.emplace_back(*feat);
}
if (m_mode == EMode::BasicSelection) { if (m_mode == EMode::BasicSelection) {
if (m_features != features) { if (m_curr_feature != curr_feature) {
GLGizmoMeasure::on_unregister_raycasters_for_picking(); GLGizmoMeasure::on_unregister_raycasters_for_picking();
m_features = features; m_curr_feature = curr_feature;
if (m_features.empty()) if (!m_curr_feature.has_value())
return; return;
for (const Measure::SurfaceFeature& feature : m_features) { switch (m_curr_feature->get_type()) {
switch (feature.get_type()) {
case Measure::SurfaceFeatureType::Point: case Measure::SurfaceFeatureType::Point:
{ {
m_raycasters.insert({ POINT_ID, m_parent.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, POINT_ID, *m_sphere.mesh_raycaster) }); m_raycasters.insert({ POINT_ID, m_parent.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, POINT_ID, *m_sphere.mesh_raycaster) });
@ -201,7 +196,10 @@ void GLGizmoMeasure::on_render()
} }
case Measure::SurfaceFeatureType::Circle: case Measure::SurfaceFeatureType::Circle:
{ {
const auto& [center, radius, n] = feature.get_circle(); const auto& [center, radius, n] = m_curr_feature->get_circle();
// TODO: check for changed inv_zoom
m_circle.reset(); m_circle.reset();
GLModel::Geometry circle_geometry = smooth_torus(64, 16, float(radius), 5.0f * inv_zoom); GLModel::Geometry circle_geometry = smooth_torus(64, 16, float(radius), 5.0f * inv_zoom);
m_circle.mesh_raycaster = std::make_unique<MeshRaycaster>(std::make_shared<const TriangleMesh>(std::move(circle_geometry.get_as_indexed_triangle_set()))); m_circle.mesh_raycaster = std::make_unique<MeshRaycaster>(std::make_shared<const TriangleMesh>(std::move(circle_geometry.get_as_indexed_triangle_set())));
@ -213,7 +211,10 @@ void GLGizmoMeasure::on_render()
} }
case Measure::SurfaceFeatureType::Plane: case Measure::SurfaceFeatureType::Plane:
{ {
const auto& [idx, normal, pt] = feature.get_plane(); const auto& [idx, normal, pt] = m_curr_feature->get_plane();
// TODO: check for changed idx
const std::vector<std::vector<int>> planes_triangles = m_measuring->get_planes_triangle_indices(); const std::vector<std::vector<int>> planes_triangles = m_measuring->get_planes_triangle_indices();
const std::vector<int>& triangle_indices = planes_triangles[idx]; const std::vector<int>& triangle_indices = planes_triangles[idx];
@ -243,7 +244,9 @@ void GLGizmoMeasure::on_render()
} }
} }
} }
}
if (!m_curr_feature.has_value())
return;
GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light"); GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light");
if (shader == nullptr) if (shader == nullptr)
@ -265,11 +268,10 @@ void GLGizmoMeasure::on_render()
case EMode::ExtendedSelection: { color = LOCK_COLOR; break; } case EMode::ExtendedSelection: { color = LOCK_COLOR; break; }
} }
for (const Measure::SurfaceFeature& feature : m_features) { switch (m_curr_feature->get_type()) {
switch (feature.get_type()) {
case Measure::SurfaceFeatureType::Point: case Measure::SurfaceFeatureType::Point:
{ {
const Vec3d& position = feature.get_point(); const Vec3d& position = m_curr_feature->get_point();
const Transform3d feature_matrix = model_matrix * Geometry::translation_transform(position) * Geometry::scale_transform(inv_zoom); const Transform3d feature_matrix = model_matrix * Geometry::translation_transform(position) * Geometry::scale_transform(inv_zoom);
const Transform3d view_model_matrix = view_matrix * feature_matrix; const Transform3d view_model_matrix = view_matrix * feature_matrix;
shader->set_uniform("view_model_matrix", view_model_matrix); shader->set_uniform("view_model_matrix", view_model_matrix);
@ -284,7 +286,7 @@ void GLGizmoMeasure::on_render()
} }
case Measure::SurfaceFeatureType::Circle: case Measure::SurfaceFeatureType::Circle:
{ {
const auto& [center, radius, n] = feature.get_circle(); const auto& [center, radius, n] = m_curr_feature->get_circle();
// render center // render center
const Transform3d center_matrix = model_matrix * Geometry::translation_transform(center) * Geometry::scale_transform(inv_zoom); const Transform3d center_matrix = model_matrix * Geometry::translation_transform(center) * Geometry::scale_transform(inv_zoom);
const Transform3d center_view_model_matrix = view_matrix * center_matrix; const Transform3d center_view_model_matrix = view_matrix * center_matrix;
@ -297,6 +299,7 @@ void GLGizmoMeasure::on_render()
if (it != m_raycasters.end() && it->second != nullptr) if (it != m_raycasters.end() && it->second != nullptr)
it->second->set_transform(center_matrix); it->second->set_transform(center_matrix);
// render circle
const Transform3d circle_matrix = model_matrix * Geometry::translation_transform(center); const Transform3d circle_matrix = model_matrix * Geometry::translation_transform(center);
const Transform3d circle_view_model_matrix = view_matrix * circle_matrix; const Transform3d circle_view_model_matrix = view_matrix * circle_matrix;
shader->set_uniform("view_model_matrix", circle_view_model_matrix); shader->set_uniform("view_model_matrix", circle_view_model_matrix);
@ -311,7 +314,7 @@ void GLGizmoMeasure::on_render()
} }
case Measure::SurfaceFeatureType::Edge: case Measure::SurfaceFeatureType::Edge:
{ {
const auto& [start, end] = feature.get_edge(); const auto& [start, end] = m_curr_feature->get_edge();
auto q = Eigen::Quaternion<double>::FromTwoVectors(Vec3d::UnitZ(), end - start); auto q = Eigen::Quaternion<double>::FromTwoVectors(Vec3d::UnitZ(), end - start);
const Transform3d feature_matrix = model_matrix * Geometry::translation_transform(start) * q * const Transform3d feature_matrix = model_matrix * Geometry::translation_transform(start) * q *
Geometry::scale_transform({ (double)inv_zoom, (double)inv_zoom, (end - start).norm() }); Geometry::scale_transform({ (double)inv_zoom, (double)inv_zoom, (end - start).norm() });
@ -328,7 +331,7 @@ void GLGizmoMeasure::on_render()
} }
case Measure::SurfaceFeatureType::Plane: case Measure::SurfaceFeatureType::Plane:
{ {
const auto& [idx, normal, pt] = feature.get_plane(); const auto& [idx, normal, pt] = m_curr_feature->get_plane();
assert(idx < m_plane_models_cache.size()); assert(idx < m_plane_models_cache.size());
const Transform3d view_model_matrix = view_matrix * model_matrix; const Transform3d view_model_matrix = view_matrix * model_matrix;
shader->set_uniform("view_model_matrix", view_model_matrix); shader->set_uniform("view_model_matrix", view_model_matrix);
@ -342,7 +345,7 @@ void GLGizmoMeasure::on_render()
break; break;
} }
} }
}
shader->stop_using(); shader->stop_using();
} }
} }
@ -426,7 +429,8 @@ void GLGizmoMeasure::update_if_needed()
void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit) void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit)
{ {
static Measure::SurfaceFeature last_feature = Measure::SurfaceFeature(Measure::SurfaceFeatureType::Undef, Vec3d::Zero(), Vec3d::Zero(), std::nullopt, 0.0); static std::optional<Measure::SurfaceFeature> last_feature;
static EMode last_mode = EMode::BasicSelection;
static float last_y = 0.0f; static float last_y = 0.0f;
static float last_h = 0.0f; static float last_h = 0.0f;
@ -446,9 +450,9 @@ void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit
last_y = y; last_y = y;
} }
if (m_features.empty()) if (!m_curr_feature.has_value())
m_imgui->text(_u8L("Select features to measure")); m_imgui->text(_u8L("Select features to measure"));
else {
auto add_row_to_table = [this](const wxString& label, const std::string& value) { auto add_row_to_table = [this](const wxString& label, const std::string& value) {
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0); ImGui::TableSetColumnIndex(0);
@ -458,19 +462,15 @@ void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit
}; };
const Transform3d volume_matrix = m_parent.get_selection().get_first_volume()->world_matrix(); const Transform3d volume_matrix = m_parent.get_selection().get_first_volume()->world_matrix();
Measure::SurfaceFeature curr_feature = Measure::SurfaceFeature(Measure::SurfaceFeatureType::Undef, Vec3d::Zero(), Vec3d::Zero(), std::nullopt, 0.0); const Measure::SurfaceFeatureType type = m_curr_feature->get_type();
for (size_t i = 0; i < m_features.size(); ++i) {
curr_feature = m_features[i];
const Measure::SurfaceFeatureType type = curr_feature.get_type();
if (type != Measure::SurfaceFeatureType::Undef) { if (type != Measure::SurfaceFeatureType::Undef) {
const std::string header = surface_feature_type_as_string(type) + std::string("##") + std::to_string(i); if (ImGui::CollapsingHeader(surface_feature_type_as_string(type).c_str(), ImGuiTreeNodeFlags_DefaultOpen)) {
if (ImGui::CollapsingHeader(header.c_str(), ImGuiTreeNodeFlags_DefaultOpen)) {
if (ImGui::BeginTable("Data", 2)) { if (ImGui::BeginTable("Data", 2)) {
switch (type) switch (type)
{ {
case Measure::SurfaceFeatureType::Point: case Measure::SurfaceFeatureType::Point:
{ {
const Vec3d position = volume_matrix * curr_feature.get_point(); const Vec3d position = volume_matrix * m_curr_feature->get_point();
char buf[1024]; char buf[1024];
sprintf(buf, "X: %.3f, Y: %.3f, Z: %.3f", position.x(), position.y(), position.z()); sprintf(buf, "X: %.3f, Y: %.3f, Z: %.3f", position.x(), position.y(), position.z());
add_row_to_table(_u8L("Position") + ":", std::string(buf)); add_row_to_table(_u8L("Position") + ":", std::string(buf));
@ -478,7 +478,7 @@ void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit
} }
case Measure::SurfaceFeatureType::Edge: case Measure::SurfaceFeatureType::Edge:
{ {
auto [from, to] = curr_feature.get_edge(); auto [from, to] = m_curr_feature->get_edge();
from = volume_matrix * from; from = volume_matrix * from;
to = volume_matrix * to; to = volume_matrix * to;
char buf[1024]; char buf[1024];
@ -490,7 +490,7 @@ void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit
} }
case Measure::SurfaceFeatureType::Circle: case Measure::SurfaceFeatureType::Circle:
{ {
auto [center, radius, normal] = curr_feature.get_circle(); auto [center, radius, normal] = m_curr_feature->get_circle();
center = volume_matrix * center; center = volume_matrix * center;
normal = volume_matrix.matrix().block(0, 0, 3, 3).inverse().transpose() * normal; normal = volume_matrix.matrix().block(0, 0, 3, 3).inverse().transpose() * normal;
char buf[1024]; char buf[1024];
@ -504,7 +504,7 @@ void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit
} }
case Measure::SurfaceFeatureType::Plane: case Measure::SurfaceFeatureType::Plane:
{ {
auto [idx, normal, origin] = curr_feature.get_plane(); auto [idx, normal, origin] = m_curr_feature->get_plane();
origin = volume_matrix * origin; origin = volume_matrix * origin;
normal = volume_matrix.matrix().block(0, 0, 3, 3).inverse().transpose() * normal; normal = volume_matrix.matrix().block(0, 0, 3, 3).inverse().transpose() * normal;
char buf[1024]; char buf[1024];
@ -521,13 +521,14 @@ void GLGizmoMeasure::on_render_input_window(float x, float y, float bottom_limit
} }
} }
if (last_feature != curr_feature) { if (last_feature != m_curr_feature || last_mode != m_mode) {
// the dialog may have changed its size, ask for an extra frame to render it properly // the dialog may have changed its size, ask for an extra frame to render it properly
last_feature = curr_feature; last_feature = m_curr_feature;
last_mode = m_mode;
m_imgui->set_requires_extra_frame(); m_imgui->set_requires_extra_frame();
} }
if (!m_features.empty()) { if (m_curr_feature.has_value() && m_mode == EMode::BasicSelection) {
static const std::string ctrl = static const std::string ctrl =
#ifdef __APPLE__ #ifdef __APPLE__
"" ""

View File

@ -41,7 +41,7 @@ class GLGizmoMeasure : public GLGizmoBase
std::vector<GLModel> m_plane_models_cache; std::vector<GLModel> m_plane_models_cache;
std::map<int, std::shared_ptr<SceneRaycasterItem>> m_raycasters; std::map<int, std::shared_ptr<SceneRaycasterItem>> m_raycasters;
std::vector<Measure::SurfaceFeature> m_features; std::optional<Measure::SurfaceFeature> m_curr_feature;
// This holds information to decide whether recalculation is necessary: // This holds information to decide whether recalculation is necessary:
std::vector<Transform3d> m_volumes_matrices; std::vector<Transform3d> m_volumes_matrices;
@ -50,13 +50,10 @@ class GLGizmoMeasure : public GLGizmoBase
Vec3d m_first_instance_mirror{ Vec3d::Ones() }; Vec3d m_first_instance_mirror{ Vec3d::Ones() };
bool m_mouse_left_down = false; // for detection left_up of this gizmo bool m_mouse_left_down = false; // for detection left_up of this gizmo
bool m_planes_valid = false;
const ModelObject* m_old_model_object = nullptr; const ModelObject* m_old_model_object = nullptr;
const ModelVolume* m_old_model_volume = nullptr; const ModelVolume* m_old_model_volume = nullptr;
std::vector<const Transform3d*> instances_matrices;
int m_mouse_pos_x; Vec2d m_mouse_pos{ Vec2d::Zero() };
int m_mouse_pos_y;
KeyAutoRepeatFilter m_ctrl_kar_filter; KeyAutoRepeatFilter m_ctrl_kar_filter;