Tech ENABLE_RAYCAST_PICKING - Small refactoring

This commit is contained in:
enricoturri1966 2022-06-21 08:26:39 +02:00
parent 37d36616ee
commit 255d8657dd
6 changed files with 24 additions and 33 deletions

View File

@ -659,10 +659,10 @@ public:
void post_event(wxEvent &&event); void post_event(wxEvent &&event);
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
std::shared_ptr<SceneRaycasterItem> 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, int id, const MeshRaycaster& raycaster, const Transform3d& trafo) {
return 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, int id) {
m_scene_raycaster.remove_raycasters(type, id); m_scene_raycaster.remove_raycasters(type, id);
} }
void remove_raycasters_for_picking(SceneRaycaster::EType type) { void remove_raycasters_for_picking(SceneRaycaster::EType type) {

View File

@ -54,7 +54,7 @@ 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(PickingId id) void GLGizmoBase::Grabber::register_raycasters_for_picking(int id)
{ {
picking_id = id; picking_id = id;
// registration will happen on next call to render() // registration will happen on next call to render()

View File

@ -72,7 +72,7 @@ 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
PickingId picking_id{ -1 }; int picking_id{ -1 };
std::array<std::shared_ptr<SceneRaycasterItem>, GRABBER_ELEMENTS_MAX_COUNT> raycasters = { nullptr }; std::array<std::shared_ptr<SceneRaycasterItem>, GRABBER_ELEMENTS_MAX_COUNT> raycasters = { nullptr };
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING
@ -90,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(PickingId id); void register_raycasters_for_picking(int id);
void unregister_raycasters_for_picking(); void unregister_raycasters_for_picking();
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING

View File

@ -40,7 +40,7 @@ private:
Vec3d normal; Vec3d normal;
float area; float area;
#if ENABLE_RAYCAST_PICKING #if ENABLE_RAYCAST_PICKING
PickingId picking_id{ -1 }; int picking_id{ -1 };
#endif // ENABLE_RAYCAST_PICKING #endif // ENABLE_RAYCAST_PICKING
}; };

View File

@ -33,7 +33,7 @@ SceneRaycaster::SceneRaycaster() {
#endif // ENABLE_RAYCAST_PICKING_DEBUG #endif // ENABLE_RAYCAST_PICKING_DEBUG
} }
std::shared_ptr<SceneRaycasterItem> SceneRaycaster::add_raycaster(EType type, PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo) std::shared_ptr<SceneRaycasterItem> SceneRaycaster::add_raycaster(EType type, int id, const MeshRaycaster& raycaster, const Transform3d& trafo)
{ {
switch (type) { switch (type) {
case EType::Bed: { return m_bed.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); } case EType::Bed: { return m_bed.emplace_back(std::make_shared<SceneRaycasterItem>(encode_id(type, id), raycaster, trafo)); }
@ -43,7 +43,7 @@ std::shared_ptr<SceneRaycasterItem> SceneRaycaster::add_raycaster(EType type, Pi
}; };
} }
void SceneRaycaster::remove_raycasters(EType type, PickingId id) void SceneRaycaster::remove_raycasters(EType type, int id)
{ {
std::vector<std::shared_ptr<SceneRaycasterItem>>* raycasters = get_raycasters(type); std::vector<std::shared_ptr<SceneRaycasterItem>>* raycasters = get_raycasters(type);
auto it = raycasters->begin(); auto it = raycasters->begin();
@ -186,13 +186,13 @@ std::vector<std::shared_ptr<SceneRaycasterItem>>* SceneRaycaster::get_raycasters
return ret; return ret;
} }
PickingId SceneRaycaster::base_id(EType type) int SceneRaycaster::base_id(EType type)
{ {
switch (type) switch (type)
{ {
case EType::Bed: { return PickingId(EPickingIdBase::Bed); } case EType::Bed: { return int(EIdBase::Bed); }
case EType::Volume: { return PickingId(EPickingIdBase::Volume); } case EType::Volume: { return int(EIdBase::Volume); }
case EType::Gizmo: { return PickingId(EPickingIdBase::Gizmo); } case EType::Gizmo: { return int(EIdBase::Gizmo); }
default: { break; } default: { break; }
}; };
@ -200,15 +200,8 @@ PickingId SceneRaycaster::base_id(EType type)
return -1; return -1;
} }
PickingId SceneRaycaster::encode_id(EType type, PickingId id) int SceneRaycaster::encode_id(EType type, int id) { return base_id(type) + id; }
{ int SceneRaycaster::decode_id(EType type, int id) { return id - base_id(type); }
return base_id(type) + id;
}
PickingId SceneRaycaster::decode_id(EType type, PickingId id)
{
return id - base_id(type);
}
} // namespace GUI } // namespace GUI
} // namespace Slic3r } // namespace Slic3r

View File

@ -15,21 +15,19 @@ namespace GUI {
struct Camera; struct Camera;
using PickingId = int;
class SceneRaycasterItem class SceneRaycasterItem
{ {
PickingId m_id{ -1 }; int m_id{ -1 };
bool m_active{ true }; bool m_active{ true };
const MeshRaycaster* m_raycaster; const MeshRaycaster* m_raycaster;
Transform3d m_trafo; Transform3d m_trafo;
public: public:
SceneRaycasterItem(PickingId id, const MeshRaycaster& raycaster, const Transform3d& trafo) SceneRaycasterItem(int id, const MeshRaycaster& raycaster, const Transform3d& trafo)
: m_id(id), m_raycaster(&raycaster), m_trafo(trafo) : m_id(id), m_raycaster(&raycaster), m_trafo(trafo)
{} {}
PickingId get_id() const { return m_id; } int get_id() const { return m_id; }
bool is_active() const { return m_active; } bool is_active() const { return m_active; }
void set_active(bool active) { m_active = active; } void set_active(bool active) { m_active = active; }
const MeshRaycaster* get_raycaster() const { return m_raycaster; } const MeshRaycaster* get_raycaster() const { return m_raycaster; }
@ -48,7 +46,7 @@ public:
Gizmo Gizmo
}; };
enum class EPickingIdBase enum class EIdBase
{ {
Bed = 0, Bed = 0,
Volume = 1000, Volume = 1000,
@ -58,7 +56,7 @@ public:
struct HitResult struct HitResult
{ {
EType type{ EType::None }; EType type{ EType::None };
PickingId raycaster_id{ -1 }; int raycaster_id{ -1 };
Vec3f position{ Vec3f::Zero() }; Vec3f position{ Vec3f::Zero() };
Vec3f normal{ Vec3f::Zero() }; Vec3f normal{ Vec3f::Zero() };
@ -83,8 +81,8 @@ private:
public: public:
SceneRaycaster(); SceneRaycaster();
std::shared_ptr<SceneRaycasterItem> add_raycaster(EType type, PickingId picking_id, const MeshRaycaster& raycaster, const Transform3d& trafo); std::shared_ptr<SceneRaycasterItem> add_raycaster(EType type, int picking_id, const MeshRaycaster& raycaster, const Transform3d& trafo);
void remove_raycasters(EType type, PickingId id); void remove_raycasters(EType type, int id);
void remove_raycasters(EType type); void remove_raycasters(EType type);
void remove_raycaster(std::shared_ptr<SceneRaycasterItem> item); void remove_raycaster(std::shared_ptr<SceneRaycasterItem> item);
@ -103,9 +101,9 @@ public:
#endif // ENABLE_RAYCAST_PICKING_DEBUG #endif // ENABLE_RAYCAST_PICKING_DEBUG
private: private:
static PickingId encode_id(EType type, PickingId id); static int encode_id(EType type, int id);
static PickingId decode_id(EType type, PickingId id); static int decode_id(EType type, int id);
static PickingId base_id(EType type); static int base_id(EType type);
}; };
} // namespace GUI } // namespace GUI