Tech ENABLE_RAYCAST_PICKING - Refactoring to allow for easier update of raycasters transform
This commit is contained in:
parent
0a7dae6df6
commit
9ac5ab857b
@ -663,8 +663,8 @@ public:
|
||||
void post_event(wxEvent &&event);
|
||||
|
||||
#if ENABLE_RAYCAST_PICKING
|
||||
void add_raycaster_for_picking(SceneRaycaster::EType type, PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo) {
|
||||
m_scene_raycaster.add_raycaster(type, id, raycaster, trafo);
|
||||
std::shared_ptr<SceneRaycasterItem> add_raycaster_for_picking(SceneRaycaster::EType type, PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo) {
|
||||
return m_scene_raycaster.add_raycaster(type, id, raycaster, trafo);
|
||||
}
|
||||
void remove_raycasters_for_picking(SceneRaycaster::EType type, PickingId id) {
|
||||
m_scene_raycaster.remove_raycasters(type, id);
|
||||
|
@ -54,17 +54,17 @@ float GLGizmoBase::Grabber::get_dragging_half_size(float size) const
|
||||
}
|
||||
|
||||
#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;
|
||||
assert(elements_registered_for_picking == false);
|
||||
// registration will happen on next call to render()
|
||||
}
|
||||
|
||||
void GLGizmoBase::Grabber::unregister_raycasters_for_picking()
|
||||
{
|
||||
wxGetApp().plater()->canvas3D()->remove_raycasters_for_picking(SceneRaycaster::EType::Gizmo, picking_id);
|
||||
picking_id = -1;
|
||||
elements_registered_for_picking = false;
|
||||
raycasters = { nullptr };
|
||||
}
|
||||
#endif // ENABLE_RAYCAST_PICKING
|
||||
|
||||
@ -128,7 +128,7 @@ void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, boo
|
||||
#if ENABLE_RAYCAST_PICKING
|
||||
const Transform3d& view_matrix = camera.get_view_matrix();
|
||||
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());
|
||||
Transform3d view_model_matrix = view_matrix * elements_matrices[0];
|
||||
#else
|
||||
@ -271,23 +271,28 @@ void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, boo
|
||||
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
|
||||
|
||||
#if ENABLE_RAYCAST_PICKING
|
||||
if (!elements_registered_for_picking) {
|
||||
if (raycasters[0] == nullptr) {
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[6]);
|
||||
elements_registered_for_picking = true;
|
||||
raycasters[6] = canvas.add_raycaster_for_picking(SceneRaycaster::EType::Gizmo, picking_id, *s_cone.mesh_raycaster, elements_matrices[6]);
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@ -320,10 +325,10 @@ bool GLGizmoBase::update_items_state()
|
||||
}
|
||||
|
||||
#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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,6 +41,7 @@ public:
|
||||
// Starting value for ids to avoid clashing with ids used by GLVolumes
|
||||
// (254 is choosen to leave some space for forward compatibility)
|
||||
static const unsigned int BASE_ID = 255 * 255 * 254;
|
||||
static const unsigned int GRABBER_ELEMENTS_MAX_COUNT = 7;
|
||||
|
||||
enum class EGrabberExtension
|
||||
{
|
||||
@ -71,8 +72,8 @@ protected:
|
||||
EGrabberExtension extensions{ EGrabberExtension::None };
|
||||
#if ENABLE_RAYCAST_PICKING
|
||||
// the picking id shared by all the elements
|
||||
int picking_id{ -1 };
|
||||
bool elements_registered_for_picking{ false };
|
||||
PickingId picking_id{ -1 };
|
||||
std::array<std::shared_ptr<SceneRaycasterItem>, GRABBER_ELEMENTS_MAX_COUNT> raycasters = { nullptr };
|
||||
#endif // ENABLE_RAYCAST_PICKING
|
||||
|
||||
Grabber() = default;
|
||||
@ -89,7 +90,7 @@ protected:
|
||||
float get_dragging_half_size(float size) const;
|
||||
|
||||
#if ENABLE_RAYCAST_PICKING
|
||||
void register_raycasters_for_picking(int id);
|
||||
void register_raycasters_for_picking(PickingId id);
|
||||
void unregister_raycasters_for_picking();
|
||||
#endif // ENABLE_RAYCAST_PICKING
|
||||
|
||||
@ -207,7 +208,7 @@ public:
|
||||
virtual bool on_mouse(const wxMouseEvent &mouse_event) { return false; }
|
||||
|
||||
#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(); }
|
||||
#endif // ENABLE_RAYCAST_PICKING
|
||||
|
||||
@ -236,7 +237,7 @@ protected:
|
||||
virtual void on_render_input_window(float x, float y, float bottom_limit) {}
|
||||
|
||||
#if ENABLE_RAYCAST_PICKING
|
||||
void register_grabbers_for_picking(bool use_group_id = false);
|
||||
void register_grabbers_for_picking();
|
||||
void unregister_grabbers_for_picking();
|
||||
virtual void on_register_raycasters_for_picking() {}
|
||||
virtual void on_unregister_raycasters_for_picking() {}
|
||||
|
@ -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
|
||||
m_parent.set_raycaster_gizmos_on_top(true);
|
||||
for (GLGizmoRotate& g : m_gizmos) {
|
||||
g.register_raycasters_for_picking(true);
|
||||
g.register_raycasters_for_picking();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,31 +33,22 @@ SceneRaycaster::SceneRaycaster() {
|
||||
#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) {
|
||||
case EType::Bed: {
|
||||
m_bed.emplace_back(encode_id(type, id), raycaster, trafo);
|
||||
break;
|
||||
}
|
||||
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; }
|
||||
case EType::Bed: { return m_bed.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); }
|
||||
case EType::Volume: { return m_volumes.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); }
|
||||
case EType::Gizmo: { return m_gizmos.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); }
|
||||
default: { assert(false); return nullptr; }
|
||||
};
|
||||
}
|
||||
|
||||
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();
|
||||
while (it != raycasters->end()) {
|
||||
if (it->get_id() == encode_id(type, id))
|
||||
if ((*it)->get_id() == encode_id(type, id))
|
||||
it = raycasters->erase(it);
|
||||
else
|
||||
++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 (SceneRaycasterItem& item : *raycasters) {
|
||||
if (item.get_id() == encode_id(type, id)) {
|
||||
item.set_active(active);
|
||||
break;
|
||||
for (auto it = m_bed.begin(); it != m_bed.end(); ++it) {
|
||||
if (*it == item) {
|
||||
m_bed.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SceneRaycaster::set_raycaster_transform(EType type, PickingId id, const Transform3d& trafo)
|
||||
{
|
||||
std::vector<SceneRaycasterItem>* raycasters = get_raycasters(type);
|
||||
for (SceneRaycasterItem& item : *raycasters) {
|
||||
if (item.get_id() == encode_id(type, id)) {
|
||||
item.set_transform(trafo);
|
||||
break;
|
||||
for (auto it = m_volumes.begin(); it != m_volumes.end(); ++it) {
|
||||
if (*it == item) {
|
||||
m_volumes.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (auto it = m_gizmos.begin(); it != m_gizmos.end(); ++it) {
|
||||
if (*it == item) {
|
||||
m_gizmos.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -115,15 +106,15 @@ SceneRaycaster::HitResult SceneRaycaster::hit(const Vec2d& mouse_pos, const Came
|
||||
|
||||
auto test_raycasters = [&](EType type) {
|
||||
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 };
|
||||
for (const SceneRaycasterItem& item : *raycasters) {
|
||||
if (!item.is_active())
|
||||
for (std::shared_ptr<SceneRaycasterItem> item : *raycasters) {
|
||||
if (!item->is_active())
|
||||
continue;
|
||||
|
||||
current_hit.raycaster_id = item.get_id();
|
||||
const Transform3d& trafo = item.get_transform();
|
||||
if (item.get_raycaster()->closest_hit(mouse_pos, trafo, camera, current_hit.position, current_hit.normal, clip_plane)) {
|
||||
current_hit.raycaster_id = item->get_id();
|
||||
const Transform3d& trafo = item->get_transform();
|
||||
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>();
|
||||
if (is_closest(camera, current_hit.position)) {
|
||||
const Transform3d matrix = camera.get_view_matrix() * trafo;
|
||||
@ -182,14 +173,15 @@ void SceneRaycaster::render_hit(const Camera& camera)
|
||||
}
|
||||
#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)
|
||||
{
|
||||
case EType::Bed: { ret = &m_bed; break; }
|
||||
case EType::Volume: { ret = &m_volumes; break; }
|
||||
case EType::Gizmo: { ret = &m_gizmos; break; }
|
||||
default: { break; }
|
||||
}
|
||||
assert(ret != nullptr);
|
||||
return ret;
|
||||
|
@ -66,9 +66,9 @@ public:
|
||||
};
|
||||
|
||||
private:
|
||||
std::vector<SceneRaycasterItem> m_bed;
|
||||
std::vector<SceneRaycasterItem> m_volumes;
|
||||
std::vector<SceneRaycasterItem> m_gizmos;
|
||||
std::vector<std::shared_ptr<SceneRaycasterItem>> m_bed;
|
||||
std::vector<std::shared_ptr<SceneRaycasterItem>> m_volumes;
|
||||
std::vector<std::shared_ptr<SceneRaycasterItem>> m_gizmos;
|
||||
|
||||
// When set to true, if checking gizmos returns a valid hit,
|
||||
// the search is not performed on other types
|
||||
@ -83,12 +83,10 @@ private:
|
||||
public:
|
||||
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);
|
||||
|
||||
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 remove_raycaster(std::shared_ptr<SceneRaycasterItem> item);
|
||||
|
||||
void set_gizmos_on_top(bool value) { m_gizmos_on_top = value; }
|
||||
|
||||
@ -103,7 +101,7 @@ public:
|
||||
#endif // ENABLE_RAYCAST_PICKING_DEBUG
|
||||
|
||||
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 decode_id(EType type, PickingId id);
|
||||
|
Loading…
Reference in New Issue
Block a user