Tech ENABLE_RAYCAST_PICKING - Refactoring to allow for easier update of raycasters transform

This commit is contained in:
enricoturri1966 2022-06-14 10:44:14 +02:00
parent 0a7dae6df6
commit 9ac5ab857b
6 changed files with 66 additions and 70 deletions

View File

@ -663,8 +663,8 @@ public:
void post_event(wxEvent &&event); void post_event(wxEvent &&event);
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
void add_raycaster_for_picking(SceneRaycaster::EType type, PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo) { std::shared_ptr<SceneRaycasterItem> add_raycaster_for_picking(SceneRaycaster::EType type, PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo) {
m_scene_raycaster.add_raycaster(type, id, raycaster, trafo); return m_scene_raycaster.add_raycaster(type, id, raycaster, trafo);
} }
void remove_raycasters_for_picking(SceneRaycaster::EType type, PickingId id) { void remove_raycasters_for_picking(SceneRaycaster::EType type, PickingId id) {
m_scene_raycaster.remove_raycasters(type, id); m_scene_raycaster.remove_raycasters(type, id);

View File

@ -54,17 +54,17 @@ float GLGizmoBase::Grabber::get_dragging_half_size(float size) const
} }
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
void GLGizmoBase::Grabber::register_raycasters_for_picking(int id) void GLGizmoBase::Grabber::register_raycasters_for_picking(PickingId id)
{ {
picking_id = id; picking_id = id;
assert(elements_registered_for_picking == false); // registration will happen on next call to render()
} }
void GLGizmoBase::Grabber::unregister_raycasters_for_picking() void GLGizmoBase::Grabber::unregister_raycasters_for_picking()
{ {
wxGetApp().plater()->canvas3D()->remove_raycasters_for_picking(SceneRaycaster::EType::Gizmo, picking_id); wxGetApp().plater()->canvas3D()->remove_raycasters_for_picking(SceneRaycaster::EType::Gizmo, picking_id);
picking_id = -1; picking_id = -1;
elements_registered_for_picking = false; raycasters = { nullptr };
} }
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING
@ -128,7 +128,7 @@ void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, boo
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
const Transform3d& view_matrix = camera.get_view_matrix(); const Transform3d& view_matrix = camera.get_view_matrix();
const Matrix3d view_matrix_no_offset = view_matrix.matrix().block(0, 0, 3, 3); const Matrix3d view_matrix_no_offset = view_matrix.matrix().block(0, 0, 3, 3);
std::vector<Transform3d> elements_matrices(7, Transform3d::Identity()); // 1 + count of EGrabberExtensions == 7 std::vector<Transform3d> elements_matrices(GRABBER_ELEMENTS_MAX_COUNT, Transform3d::Identity());
elements_matrices[0] = matrix * Geometry::assemble_transform(center, angles, 2.0 * half_size * Vec3d::Ones()); elements_matrices[0] = matrix * Geometry::assemble_transform(center, angles, 2.0 * half_size * Vec3d::Ones());
Transform3d view_model_matrix = view_matrix * elements_matrices[0]; Transform3d view_model_matrix = view_matrix * elements_matrices[0];
#else #else
@ -271,23 +271,28 @@ void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, boo
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL #endif // !ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
if (!elements_registered_for_picking) { if (raycasters[0] == nullptr) {
GLCanvas3D& canvas = *wxGetApp().plater()->canvas3D(); GLCanvas3D& canvas = *wxGetApp().plater()->canvas3D();
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cube.mesh_raycaster, elements_matrices[0]); raycasters[0] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cube.mesh_raycaster, elements_matrices[0]);
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosX)) != 0) if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosX)) != 0)
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[1]); raycasters[1] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[1]);
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegX)) != 0) if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegX)) != 0)
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[2]); raycasters[2] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[2]);
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosY)) != 0) if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosY)) != 0)
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[3]); raycasters[3] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[3]);
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegY)) != 0) if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegY)) != 0)
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[4]); raycasters[4] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[4]);
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosZ)) != 0) if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosZ)) != 0)
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[5]); raycasters[5] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[5]);
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegZ)) != 0) if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegZ)) != 0)
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[6]); raycasters[6] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[6]);
elements_registered_for_picking = true;
} }
else {
for (size_t i = 0; i < GRABBER_ELEMENTS_MAX_COUNT; ++i) {
if (raycasters[i] != nullptr)
raycasters[i]->set_transform(elements_matrices[i]);
}
}
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING
} }
@ -320,10 +325,10 @@ bool GLGizmoBase::update_items_state()
} }
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
void GLGizmoBase::register_grabbers_for_picking(bool use_group_id) void GLGizmoBase::register_grabbers_for_picking()
{ {
for (size_t i = 0; i < m_grabbers.size(); ++i) { for (size_t i = 0; i < m_grabbers.size(); ++i) {
m_grabbers[i].register_raycasters_for_picking(use_group_id ? m_group_id : i); m_grabbers[i].register_raycasters_for_picking((m_group_id >= 0) ? m_group_id : i);
} }
} }

View File

@ -41,6 +41,7 @@ public:
// Starting value for ids to avoid clashing with ids used by GLVolumes // Starting value for ids to avoid clashing with ids used by GLVolumes
// (254 is choosen to leave some space for forward compatibility) // (254 is choosen to leave some space for forward compatibility)
static const unsigned int BASE_ID = 255 * 255 * 254; static const unsigned int BASE_ID = 255 * 255 * 254;
static const unsigned int GRABBER_ELEMENTS_MAX_COUNT = 7;
enum class EGrabberExtension enum class EGrabberExtension
{ {
@ -71,8 +72,8 @@ protected:
EGrabberExtension extensions{ EGrabberExtension::None }; EGrabberExtension extensions{ EGrabberExtension::None };
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
// the picking id shared by all the elements // the picking id shared by all the elements
int picking_id{ -1 }; PickingId picking_id{ -1 };
bool elements_registered_for_picking{ false }; std::array<std::shared_ptr<SceneRaycasterItem>, GRABBER_ELEMENTS_MAX_COUNT> raycasters = { nullptr };
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING
Grabber() = default; Grabber() = default;
@ -89,7 +90,7 @@ protected:
float get_dragging_half_size(float size) const; float get_dragging_half_size(float size) const;
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
void register_raycasters_for_picking(int id); void register_raycasters_for_picking(PickingId id);
void unregister_raycasters_for_picking(); void unregister_raycasters_for_picking();
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING
@ -207,7 +208,7 @@ public:
virtual bool on_mouse(const wxMouseEvent &mouse_event) { return false; } virtual bool on_mouse(const wxMouseEvent &mouse_event) { return false; }
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
void register_raycasters_for_picking(bool use_group_id = false) { register_grabbers_for_picking(use_group_id); on_register_raycasters_for_picking(); } void register_raycasters_for_picking() { register_grabbers_for_picking(); on_register_raycasters_for_picking(); }
void unregister_raycasters_for_picking() { unregister_grabbers_for_picking(); on_unregister_raycasters_for_picking(); } void unregister_raycasters_for_picking() { unregister_grabbers_for_picking(); on_unregister_raycasters_for_picking(); }
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING
@ -236,7 +237,7 @@ protected:
virtual void on_render_input_window(float x, float y, float bottom_limit) {} virtual void on_render_input_window(float x, float y, float bottom_limit) {}
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
void register_grabbers_for_picking(bool use_group_id = false); void register_grabbers_for_picking();
void unregister_grabbers_for_picking(); void unregister_grabbers_for_picking();
virtual void on_register_raycasters_for_picking() {} virtual void on_register_raycasters_for_picking() {}
virtual void on_unregister_raycasters_for_picking() {} virtual void on_unregister_raycasters_for_picking() {}

View File

@ -888,7 +888,7 @@ void GLGizmoRotate3D::on_register_raycasters_for_picking()
// the gizmo grabbers are rendered on top of the scene, so the raytraced picker should take it into account // the gizmo grabbers are rendered on top of the scene, so the raytraced picker should take it into account
m_parent.set_raycaster_gizmos_on_top(true); m_parent.set_raycaster_gizmos_on_top(true);
for (GLGizmoRotate& g : m_gizmos) { for (GLGizmoRotate& g : m_gizmos) {
g.register_raycasters_for_picking(true); g.register_raycasters_for_picking();
} }
} }

View File

@ -33,31 +33,22 @@ SceneRaycaster::SceneRaycaster() {
#endif // ENABLE_RAYCAST_PICKING_DEBUG #endif // ENABLE_RAYCAST_PICKING_DEBUG
} }
void SceneRaycaster::add_raycaster(EType type, PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo) std::shared_ptr<SceneRaycasterItem> SceneRaycaster::add_raycaster(EType type, PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo)
{ {
switch (type) { switch (type) {
case EType::Bed: { case EType::Bed: { return m_bed.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); }
m_bed.emplace_back(encode_id(type, id), raycaster, trafo); case EType::Volume: { return m_volumes.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); }
break; case EType::Gizmo: { return m_gizmos.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); }
} default: { assert(false); return nullptr; }
case EType::Volume: {
m_volumes.emplace_back(encode_id(type, id), raycaster, trafo);
break;
}
case EType::Gizmo: {
m_gizmos.emplace_back(encode_id(type, id), raycaster, trafo);
break;
}
default: { break; }
}; };
} }
void SceneRaycaster::remove_raycasters(EType type, PickingId id) void SceneRaycaster::remove_raycasters(EType type, PickingId id)
{ {
std::vector<SceneRaycasterItem>* raycasters = get_raycasters(type); std::vector<std::shared_ptr<SceneRaycasterItem>>* raycasters = get_raycasters(type);
auto it = raycasters->begin(); auto it = raycasters->begin();
while (it != raycasters->end()) { while (it != raycasters->end()) {
if (it->get_id() == encode_id(type, id)) if ((*it)->get_id() == encode_id(type, id))
it = raycasters->erase(it); it = raycasters->erase(it);
else else
++it; ++it;
@ -74,24 +65,24 @@ void SceneRaycaster::remove_raycasters(EType type)
}; };
} }
void SceneRaycaster::set_raycaster_active_state(EType type, PickingId id, bool active) void SceneRaycaster::remove_raycaster(std::shared_ptr<SceneRaycasterItem> item)
{ {
std::vector<SceneRaycasterItem>* raycasters = get_raycasters(type); for (auto it = m_bed.begin(); it != m_bed.end(); ++it) {
for (SceneRaycasterItem& item : *raycasters) { if (*it == item) {
if (item.get_id() == encode_id(type, id)) { m_bed.erase(it);
item.set_active(active); return;
break;
} }
} }
} for (auto it = m_volumes.begin(); it != m_volumes.end(); ++it) {
if (*it == item) {
void SceneRaycaster::set_raycaster_transform(EType type, PickingId id, const Transform3d& trafo) m_volumes.erase(it);
{ return;
std::vector<SceneRaycasterItem>* raycasters = get_raycasters(type); }
for (SceneRaycasterItem& item : *raycasters) { }
if (item.get_id() == encode_id(type, id)) { for (auto it = m_gizmos.begin(); it != m_gizmos.end(); ++it) {
item.set_transform(trafo); if (*it == item) {
break; m_gizmos.erase(it);
return;
} }
} }
} }
@ -115,15 +106,15 @@ SceneRaycaster::HitResult SceneRaycaster::hit(const Vec2d& mouse_pos, const Came
auto test_raycasters = [&](EType type) { auto test_raycasters = [&](EType type) {
const ClippingPlane* clip_plane = (clipping_plane != nullptr && type == EType::Volume) ? clipping_plane : nullptr; const ClippingPlane* clip_plane = (clipping_plane != nullptr && type == EType::Volume) ? clipping_plane : nullptr;
const std::vector<SceneRaycasterItem>* raycasters = get_raycasters(type); std::vector<std::shared_ptr<SceneRaycasterItem>>* raycasters = get_raycasters(type);
HitResult current_hit = { type }; HitResult current_hit = { type };
for (const SceneRaycasterItem& item : *raycasters) { for (std::shared_ptr<SceneRaycasterItem> item : *raycasters) {
if (!item.is_active()) if (!item->is_active())
continue; continue;
current_hit.raycaster_id = item.get_id(); current_hit.raycaster_id = item->get_id();
const Transform3d& trafo = item.get_transform(); const Transform3d& trafo = item->get_transform();
if (item.get_raycaster()->closest_hit(mouse_pos, trafo, camera, current_hit.position, current_hit.normal, clip_plane)) { if (item->get_raycaster()->closest_hit(mouse_pos, trafo, camera, current_hit.position, current_hit.normal, clip_plane)) {
current_hit.position = (trafo * current_hit.position.cast<double>()).cast<float>(); current_hit.position = (trafo * current_hit.position.cast<double>()).cast<float>();
if (is_closest(camera, current_hit.position)) { if (is_closest(camera, current_hit.position)) {
const Transform3d matrix = camera.get_view_matrix() * trafo; const Transform3d matrix = camera.get_view_matrix() * trafo;
@ -182,14 +173,15 @@ void SceneRaycaster::render_hit(const Camera& camera)
} }
#endif // ENABLE_RAYCAST_PICKING_DEBUG #endif // ENABLE_RAYCAST_PICKING_DEBUG
std::vector<SceneRaycasterItem>* SceneRaycaster::get_raycasters(EType type) std::vector<std::shared_ptr<SceneRaycasterItem>>* SceneRaycaster::get_raycasters(EType type)
{ {
std::vector<SceneRaycasterItem>* ret = nullptr; std::vector<std::shared_ptr<SceneRaycasterItem>>* ret = nullptr;
switch (type) switch (type)
{ {
case EType::Bed: { ret = &m_bed; break; } case EType::Bed: { ret = &m_bed; break; }
case EType::Volume: { ret = &m_volumes; break; } case EType::Volume: { ret = &m_volumes; break; }
case EType::Gizmo: { ret = &m_gizmos; break; } case EType::Gizmo: { ret = &m_gizmos; break; }
default: { break; }
} }
assert(ret != nullptr); assert(ret != nullptr);
return ret; return ret;

View File

@ -66,9 +66,9 @@ public:
}; };
private: private:
std::vector<SceneRaycasterItem> m_bed; std::vector<std::shared_ptr<SceneRaycasterItem>> m_bed;
std::vector<SceneRaycasterItem> m_volumes; std::vector<std::shared_ptr<SceneRaycasterItem>> m_volumes;
std::vector<SceneRaycasterItem> m_gizmos; std::vector<std::shared_ptr<SceneRaycasterItem>> m_gizmos;
// When set to true, if checking gizmos returns a valid hit, // When set to true, if checking gizmos returns a valid hit,
// the search is not performed on other types // the search is not performed on other types
@ -83,12 +83,10 @@ private:
public: public:
SceneRaycaster(); SceneRaycaster();
void add_raycaster(EType type, PickingId picking_id, const MeshRaycaster& raycaster, const Transform3d& trafo); std::shared_ptr<SceneRaycasterItem> add_raycaster(EType type, PickingId picking_id, const MeshRaycaster& raycaster, const Transform3d& trafo);
void remove_raycasters(EType type, PickingId id); void remove_raycasters(EType type, PickingId id);
void remove_raycasters(EType type); void remove_raycasters(EType type);
void remove_raycaster(std::shared_ptr<SceneRaycasterItem> item);
void set_raycaster_active_state(EType type, PickingId picking_id, bool active);
void set_raycaster_transform(EType type, PickingId picking_id, const Transform3d& trafo);
void set_gizmos_on_top(bool value) { m_gizmos_on_top = value; } void set_gizmos_on_top(bool value) { m_gizmos_on_top = value; }
@ -103,7 +101,7 @@ public:
#endif // ENABLE_RAYCAST_PICKING_DEBUG #endif // ENABLE_RAYCAST_PICKING_DEBUG
private: private:
std::vector<SceneRaycasterItem>* get_raycasters(EType type); std::vector<std::shared_ptr<SceneRaycasterItem>>* get_raycasters(EType type);
static PickingId encode_id(EType type, PickingId id); static PickingId encode_id(EType type, PickingId id);
static PickingId decode_id(EType type, PickingId id); static PickingId decode_id(EType type, PickingId id);