attach vtk context to gcompose
This commit is contained in:
@@ -10,6 +10,7 @@ set(HEADERS
|
||||
Macros.h
|
||||
Mpl.h
|
||||
Object.h
|
||||
ObjectFactory.h
|
||||
ObjectsContext.h
|
||||
Options.h
|
||||
Serializable.h
|
||||
@@ -27,6 +28,7 @@ set(SOURCES
|
||||
Archives.cpp
|
||||
Debug.cpp
|
||||
Object.cpp
|
||||
ObjectFactory.cpp
|
||||
ObjectsContext.cpp
|
||||
Options.cpp
|
||||
Serializable.cpp
|
||||
|
||||
@@ -56,7 +56,8 @@ public:
|
||||
GenericMFPtr sloptr;
|
||||
std::string slostr;
|
||||
};
|
||||
|
||||
|
||||
std::string m_InstanceName;
|
||||
Vector<Signal> sigv;
|
||||
Vector<Slot> slov;
|
||||
};
|
||||
@@ -147,6 +148,15 @@ GenericMFPtr *Object::findSlotImpl(const char *name) const {
|
||||
|
||||
void Object::Updated() { ULIB_SIGNAL_EMIT(Object::Updated); }
|
||||
|
||||
const std::string& Object::GetInstanceName() const {
|
||||
return d->m_InstanceName;
|
||||
}
|
||||
|
||||
void Object::SetInstanceName(const std::string& name) {
|
||||
d->m_InstanceName = name;
|
||||
this->Updated();
|
||||
}
|
||||
|
||||
// std::ostream &
|
||||
// operator << (std::ostream &os, uLib::Object &ob)
|
||||
// {
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#define U_CORE_OBJECT_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// WARNING: COPILE ERROR if this goes after mpl/vector //
|
||||
// #include "Core/Vector.h"
|
||||
@@ -74,6 +75,10 @@ public:
|
||||
Object(const Object ©);
|
||||
~Object();
|
||||
|
||||
virtual const char * GetClassName() const { return "Object"; }
|
||||
|
||||
const std::string& GetInstanceName() const;
|
||||
void SetInstanceName(const std::string& name);
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// PARAMETERS //
|
||||
|
||||
|
||||
32
src/Core/ObjectFactory.cpp
Normal file
32
src/Core/ObjectFactory.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "ObjectFactory.h"
|
||||
|
||||
namespace uLib {
|
||||
|
||||
ObjectFactory& ObjectFactory::Instance() {
|
||||
static ObjectFactory instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void ObjectFactory::Register(const std::string& className, FactoryFunction func) {
|
||||
if (m_factoryMap.find(className) == m_factoryMap.end()) {
|
||||
m_factoryMap[className] = func;
|
||||
}
|
||||
}
|
||||
|
||||
Object* ObjectFactory::Create(const std::string& className) {
|
||||
auto it = m_factoryMap.find(className);
|
||||
if (it != m_factoryMap.end()) {
|
||||
return (it->second)();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> ObjectFactory::GetRegisteredClasses() const {
|
||||
std::vector<std::string> classes;
|
||||
for (auto const& [name, func] : m_factoryMap) {
|
||||
classes.push_back(name);
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
} // namespace uLib
|
||||
68
src/Core/ObjectFactory.h
Normal file
68
src/Core/ObjectFactory.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef U_CORE_OBJECTFACTORY_H
|
||||
#define U_CORE_OBJECTFACTORY_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "Core/Object.h"
|
||||
|
||||
namespace uLib {
|
||||
|
||||
/**
|
||||
* @brief Singleton factory for dynamic Object instantiation based on class name.
|
||||
*/
|
||||
class ObjectFactory {
|
||||
public:
|
||||
typedef std::function<Object*()> FactoryFunction;
|
||||
|
||||
/** @brief Get the singleton instance. */
|
||||
static ObjectFactory& Instance();
|
||||
|
||||
/** @brief Register a factory function for a given class name. */
|
||||
void Register(const std::string& className, FactoryFunction func);
|
||||
|
||||
/** @brief Create a new instance of the specified class. */
|
||||
Object* Create(const std::string& className);
|
||||
|
||||
/** @brief Get the names of all registered classes. */
|
||||
std::vector<std::string> GetRegisteredClasses() const;
|
||||
|
||||
private:
|
||||
ObjectFactory() = default;
|
||||
~ObjectFactory() = default;
|
||||
|
||||
// Prevent copy and assignment
|
||||
ObjectFactory(const ObjectFactory&) = delete;
|
||||
ObjectFactory& operator=(const ObjectFactory&) = delete;
|
||||
|
||||
std::map<std::string, FactoryFunction> m_factoryMap;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Helper class to statically register a factory function.
|
||||
*/
|
||||
template <typename T>
|
||||
class ObjectRegistrar {
|
||||
public:
|
||||
ObjectRegistrar(const std::string& className) {
|
||||
ObjectFactory::Instance().Register(className, []() -> Object* { return new T(); });
|
||||
}
|
||||
};
|
||||
|
||||
#define ULIB_REG_CONCAT_IMPL(a, b) a##b
|
||||
#define ULIB_REG_CONCAT(a, b) ULIB_REG_CONCAT_IMPL(a, b)
|
||||
|
||||
/**
|
||||
* @brief Macro to register a class to the factory.
|
||||
* Put this in the .cpp file of the class.
|
||||
*/
|
||||
#define ULIB_REGISTER_OBJECT(className) \
|
||||
static uLib::ObjectRegistrar<className> ULIB_REG_CONCAT(g_ObjectRegistrar_, __LINE__)(#className);
|
||||
|
||||
#define ULIB_REGISTER_OBJECT_NAME(className, registeredName) \
|
||||
static uLib::ObjectRegistrar<className> ULIB_REG_CONCAT(g_ObjectRegistrar_, __LINE__)(registeredName);
|
||||
|
||||
} // namespace uLib
|
||||
|
||||
#endif // U_CORE_OBJECTFACTORY_H
|
||||
@@ -10,6 +10,7 @@ ObjectsContext::~ObjectsContext() {}
|
||||
void ObjectsContext::AddObject(Object* obj) {
|
||||
if (obj && std::find(m_objects.begin(), m_objects.end(), obj) == m_objects.end()) {
|
||||
m_objects.push_back(obj);
|
||||
ULIB_SIGNAL_EMIT(ObjectsContext::ObjectAdded, obj);
|
||||
this->Updated(); // Signal that the context has been updated
|
||||
}
|
||||
}
|
||||
@@ -17,13 +18,18 @@ void ObjectsContext::AddObject(Object* obj) {
|
||||
void ObjectsContext::RemoveObject(Object* obj) {
|
||||
auto it = std::find(m_objects.begin(), m_objects.end(), obj);
|
||||
if (it != m_objects.end()) {
|
||||
Object* removedObj = *it;
|
||||
m_objects.erase(it);
|
||||
ULIB_SIGNAL_EMIT(ObjectsContext::ObjectRemoved, removedObj);
|
||||
this->Updated(); // Signal that the context has been updated
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectsContext::Clear() {
|
||||
if (!m_objects.empty()) {
|
||||
for (auto obj : m_objects) {
|
||||
ULIB_SIGNAL_EMIT(ObjectsContext::ObjectRemoved, obj);
|
||||
}
|
||||
m_objects.clear();
|
||||
this->Updated();
|
||||
}
|
||||
@@ -44,4 +50,12 @@ Object* ObjectsContext::GetObject(size_t index) const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ObjectsContext::ObjectAdded(Object* obj) {
|
||||
ULIB_SIGNAL_EMIT(ObjectsContext::ObjectAdded, obj);
|
||||
}
|
||||
|
||||
void ObjectsContext::ObjectRemoved(Object* obj) {
|
||||
ULIB_SIGNAL_EMIT(ObjectsContext::ObjectRemoved, obj);
|
||||
}
|
||||
|
||||
} // namespace uLib
|
||||
|
||||
@@ -14,6 +14,8 @@ public:
|
||||
ObjectsContext();
|
||||
virtual ~ObjectsContext();
|
||||
|
||||
virtual const char * GetClassName() const { return "ObjectsContext"; }
|
||||
|
||||
/**
|
||||
* @brief Adds an object to the context.
|
||||
* @param obj Pointer to the object to add.
|
||||
@@ -36,6 +38,12 @@ public:
|
||||
* @return Const reference to the vector of object pointers.
|
||||
*/
|
||||
const std::vector<Object*>& GetObjects() const;
|
||||
|
||||
signals:
|
||||
/** @brief Signal emitted when an object is added. */
|
||||
virtual void ObjectAdded(Object* obj);
|
||||
/** @brief Signal emitted when an object is removed. */
|
||||
virtual void ObjectRemoved(Object* obj);
|
||||
|
||||
/**
|
||||
* @brief Returns the number of objects in the context.
|
||||
|
||||
@@ -43,11 +43,23 @@ using namespace boost::placeholders;
|
||||
// Signals macro //
|
||||
|
||||
#define default(vlaue)
|
||||
#define slots
|
||||
#ifndef Q_MOC_RUN
|
||||
#ifndef signals
|
||||
#define signals /*virtual void init_signals();*/ public
|
||||
#endif
|
||||
#ifndef slots
|
||||
#define slots
|
||||
#endif
|
||||
#ifndef emit
|
||||
#define emit
|
||||
#endif
|
||||
#endif
|
||||
#ifndef SLOT
|
||||
#define SLOT(a) BOOST_STRINGIZE(a)
|
||||
#endif
|
||||
#ifndef SIGNAL
|
||||
#define SIGNAL(a) BOOST_STRINGIZE(a)
|
||||
#endif
|
||||
|
||||
#define _ULIB_DETAIL_SIGNAL_EMIT(_name, ...) \
|
||||
do { \
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "HEP/Detectors/DetectorChamber.h"
|
||||
#include "Core/ObjectFactory.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace uLib {
|
||||
@@ -43,4 +44,6 @@ MuonEvent DetectorChamber::ProjectMuonEvent(const MuonEvent &muon) const {
|
||||
return projectedMuon;
|
||||
}
|
||||
|
||||
ULIB_REGISTER_OBJECT(DetectorChamber)
|
||||
|
||||
} // namespace uLib
|
||||
@@ -45,6 +45,8 @@ class DetectorChamber : public ContainerBox {
|
||||
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "DetectorChamber"; }
|
||||
|
||||
DetectorChamber() : BaseClass() {
|
||||
m_ProjectionPlane.origin = HPoint3f(0, 0, 0);
|
||||
m_ProjectionPlane.direction = HVector3f(0, 0, 1);
|
||||
|
||||
@@ -26,6 +26,9 @@ namespace Geant {
|
||||
class EmitterPrimary : public G4VUserPrimaryGeneratorAction, public Object, public AffineTransform
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.EmitterPrimary"; }
|
||||
|
||||
EmitterPrimary();
|
||||
virtual ~EmitterPrimary();
|
||||
|
||||
@@ -44,6 +47,9 @@ class EmitterPrimary : public G4VUserPrimaryGeneratorAction, public Object, publ
|
||||
class SkyPlaneEmitterPrimary : public EmitterPrimary
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.SkyPlaneEmitterPrimary"; }
|
||||
|
||||
SkyPlaneEmitterPrimary();
|
||||
virtual ~SkyPlaneEmitterPrimary();
|
||||
|
||||
@@ -63,6 +69,9 @@ class SkyPlaneEmitterPrimary : public EmitterPrimary
|
||||
class CylinderEmitterPrimary : public EmitterPrimary
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.CylinderEmitterPrimary"; }
|
||||
|
||||
CylinderEmitterPrimary();
|
||||
virtual ~CylinderEmitterPrimary();
|
||||
|
||||
@@ -89,6 +98,9 @@ class CylinderEmitterPrimary : public EmitterPrimary
|
||||
class QuadMeshEmitterPrimary : public EmitterPrimary
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.QuadMeshEmitterPrimary"; }
|
||||
|
||||
QuadMeshEmitterPrimary();
|
||||
virtual ~QuadMeshEmitterPrimary();
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#ifndef U_GEANTEVENT_H
|
||||
#define U_GEANTEVENT_H
|
||||
|
||||
#include "Core/Object.h"
|
||||
#include "Core/Types.h"
|
||||
#include "Core/Vector.h"
|
||||
#include "Math/Dense.h"
|
||||
@@ -46,10 +47,12 @@ class SteppingAction;
|
||||
/// recording the change of momentum and direction at each step boundary.
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class GeantEvent {
|
||||
class GeantEvent : public Object {
|
||||
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.GeantEvent"; }
|
||||
|
||||
/// A single interaction step along the muon path.
|
||||
struct Delta {
|
||||
Scalarf m_Length; ///< step length through the solid
|
||||
|
||||
@@ -59,6 +59,8 @@ private:
|
||||
class Material : public Object {
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.Material"; }
|
||||
|
||||
uLibRefMacro(G4Data,G4Material *)
|
||||
private:
|
||||
G4Material *m_G4Data;
|
||||
|
||||
@@ -43,6 +43,9 @@ class EmitterPrimary;
|
||||
|
||||
class Scene : public Object {
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.Scene"; }
|
||||
|
||||
Scene();
|
||||
~Scene();
|
||||
|
||||
|
||||
@@ -43,6 +43,9 @@ namespace Geant {
|
||||
|
||||
class Solid : public Object {
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.Solid"; }
|
||||
|
||||
Solid();
|
||||
Solid(const char *name);
|
||||
virtual ~Solid();
|
||||
@@ -83,6 +86,9 @@ protected:
|
||||
class TessellatedSolid : public Solid {
|
||||
typedef Solid BaseClass;
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.TessellatedSolid"; }
|
||||
|
||||
TessellatedSolid(const char *name);
|
||||
void SetMesh(TriangleMesh &mesh);
|
||||
uLibGetMacro(Solid, G4TessellatedSolid *)
|
||||
@@ -104,6 +110,9 @@ class BoxSolid : public Solid {
|
||||
typedef Solid BaseClass;
|
||||
|
||||
public:
|
||||
|
||||
virtual const char* GetClassName() const override { return "Geant.BoxSolid"; }
|
||||
|
||||
BoxSolid(const char *name, ContainerBox *box);
|
||||
virtual G4VSolid* GetG4Solid() const override { return (G4VSolid*)m_Solid; }
|
||||
|
||||
|
||||
@@ -34,7 +34,8 @@ set(SOURCES VoxRaytracer.cpp
|
||||
QuadMesh.cpp
|
||||
Dense.cpp
|
||||
Structured2DGrid.cpp
|
||||
Structured4DGrid.cpp)
|
||||
Structured4DGrid.cpp
|
||||
MathRegistrations.cpp)
|
||||
|
||||
set(LIBRARIES ${PACKAGE_LIBPREFIX}Core
|
||||
Eigen3::Eigen
|
||||
|
||||
@@ -48,6 +48,9 @@ class ContainerBox : public AffineTransform, public Object {
|
||||
typedef AffineTransform BaseClass;
|
||||
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "ContainerBox"; }
|
||||
|
||||
/**
|
||||
* @brief Default constructor.
|
||||
* Initializes the local transformation with this instance as its parent.
|
||||
|
||||
@@ -45,6 +45,9 @@ class Cylinder : public AffineTransform, public Object {
|
||||
typedef AffineTransform BaseClass;
|
||||
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "Cylinder"; }
|
||||
|
||||
/**
|
||||
* @brief Default constructor.
|
||||
* Initializes with radius 1 and height 1.
|
||||
|
||||
@@ -28,15 +28,18 @@
|
||||
#ifndef U_GEOMETRY_H
|
||||
#define U_GEOMETRY_H
|
||||
|
||||
#include "Core/Object.h"
|
||||
#include "Math/Dense.h"
|
||||
#include "Math/Transform.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace uLib {
|
||||
|
||||
class Geometry : public AffineTransform {
|
||||
class Geometry : public AffineTransform, public Object {
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "Geometry"; }
|
||||
|
||||
virtual Vector3f ToLinear(const Vector3f& curved_space) const {
|
||||
return curved_space;
|
||||
}
|
||||
@@ -87,6 +90,8 @@ class SphericalGeometry : public Geometry {
|
||||
public:
|
||||
SphericalGeometry() {}
|
||||
|
||||
virtual const char * GetClassName() const { return "SphericalGeometry"; }
|
||||
|
||||
Vector3f ToLinear(const Vector3f& spherical) const {
|
||||
float r = spherical.x();
|
||||
float theta = spherical.y();
|
||||
@@ -109,6 +114,8 @@ class ToroidalGeometry : public Geometry {
|
||||
public:
|
||||
ToroidalGeometry(float Rtor) : m_Rtor(Rtor) {}
|
||||
|
||||
virtual const char * GetClassName() const { return "ToroidalGeometry"; }
|
||||
|
||||
Vector3f ToLinear(const Vector3f& toroidal) const {
|
||||
float r = toroidal.x();
|
||||
float theta = toroidal.y();
|
||||
|
||||
20
src/Math/MathRegistrations.cpp
Normal file
20
src/Math/MathRegistrations.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "Core/ObjectFactory.h"
|
||||
#include "Math/ContainerBox.h"
|
||||
#include "Math/Cylinder.h"
|
||||
#include "Math/Geometry.h"
|
||||
#include "Math/TriangleMesh.h"
|
||||
#include "Math/QuadMesh.h"
|
||||
#include "Math/VoxImage.h"
|
||||
#include "Math/StructuredData.h"
|
||||
|
||||
namespace uLib {
|
||||
|
||||
ULIB_REGISTER_OBJECT(ContainerBox)
|
||||
ULIB_REGISTER_OBJECT(Cylinder)
|
||||
ULIB_REGISTER_OBJECT(CylindricalGeometry)
|
||||
ULIB_REGISTER_OBJECT(SphericalGeometry)
|
||||
ULIB_REGISTER_OBJECT(TriangleMesh)
|
||||
ULIB_REGISTER_OBJECT(QuadMesh)
|
||||
ULIB_REGISTER_OBJECT_NAME(VoxImage<Voxel>, "VoxImage")
|
||||
|
||||
} // namespace uLib
|
||||
@@ -36,6 +36,8 @@ class Polydata : public Object {
|
||||
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "Polydata"; }
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
@@ -37,6 +37,9 @@ namespace uLib {
|
||||
class QuadMesh : public AffineTransform, public Object
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "QuadMesh"; }
|
||||
|
||||
void PrintSelf(std::ostream &o);
|
||||
|
||||
/** @brief Adds a point in global coordinates. Stored in local coordinates. */
|
||||
|
||||
@@ -40,6 +40,9 @@ namespace uLib {
|
||||
class TriangleMesh : public AffineTransform, public Object
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "TriangleMesh"; }
|
||||
|
||||
void PrintSelf(std::ostream &o);
|
||||
|
||||
/** @brief Adds a point in global coordinates. Stored in local coordinates. */
|
||||
|
||||
@@ -46,6 +46,9 @@ namespace Abstract {
|
||||
|
||||
class VoxImage : public uLib::StructuredGrid {
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "VoxImage"; }
|
||||
|
||||
typedef uLib::StructuredGrid BaseClass;
|
||||
|
||||
virtual float GetValue(const Vector3i &id) const = 0;
|
||||
|
||||
@@ -57,9 +57,12 @@ protected:
|
||||
} // namespace Abstract
|
||||
|
||||
template <typename VoxelT, typename AlgorithmT>
|
||||
class VoxImageFilter : public Abstract::VoxImageFilter {
|
||||
class VoxImageFilter : public Abstract::VoxImageFilter, public Object {
|
||||
|
||||
public:
|
||||
|
||||
virtual const char * GetClassName() const { return "VoxImageFilter"; }
|
||||
|
||||
VoxImageFilter(const Vector3i &size);
|
||||
|
||||
void Run();
|
||||
|
||||
@@ -5,6 +5,7 @@ set(HEADERS uLibVtkInterface.h
|
||||
vtkQViewport.h
|
||||
vtkViewport.h
|
||||
vtkPolydata.h
|
||||
vtkObjectsContext.h
|
||||
)
|
||||
|
||||
set(SOURCES uLibVtkInterface.cxx
|
||||
@@ -14,6 +15,7 @@ set(SOURCES uLibVtkInterface.cxx
|
||||
vtkQViewport.cpp
|
||||
vtkViewport.cpp
|
||||
vtkPolydata.cpp
|
||||
vtkObjectsContext.cpp
|
||||
)
|
||||
|
||||
## Pull in Math VTK wrappers (sets MATH_SOURCES / MATH_HEADERS)
|
||||
|
||||
@@ -103,6 +103,10 @@ private:
|
||||
class PuppetData *d;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Vtk
|
||||
} // namespace uLib
|
||||
|
||||
|
||||
150
src/Vtk/vtkObjectsContext.cpp
Normal file
150
src/Vtk/vtkObjectsContext.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "vtkObjectsContext.h"
|
||||
#include "vtkContainerBox.h"
|
||||
#include "HEP/Detectors/vtkDetectorChamber.h"
|
||||
|
||||
#include <vtkAssembly.h>
|
||||
#include <vtkPropCollection.h>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
namespace uLib {
|
||||
namespace Vtk {
|
||||
|
||||
vtkObjectsContext::vtkObjectsContext(uLib::ObjectsContext *context)
|
||||
: m_Context(context), m_Assembly(::vtkAssembly::New()) {
|
||||
this->SetProp(m_Assembly);
|
||||
if (m_Context) {
|
||||
Object::connect(m_Context, &uLib::ObjectsContext::ObjectAdded, this, &vtkObjectsContext::OnObjectAdded);
|
||||
Object::connect(m_Context, &uLib::ObjectsContext::ObjectRemoved, this, &vtkObjectsContext::OnObjectRemoved);
|
||||
this->Synchronize();
|
||||
}
|
||||
}
|
||||
|
||||
vtkObjectsContext::~vtkObjectsContext() {
|
||||
for (auto const& [obj, puppet] : m_Puppets) {
|
||||
delete puppet;
|
||||
}
|
||||
m_Assembly->Delete();
|
||||
}
|
||||
|
||||
void vtkObjectsContext::Synchronize() {
|
||||
if (!m_Context) return;
|
||||
|
||||
// 1. Identify objects to add and remove
|
||||
const auto& objects = m_Context->GetObjects();
|
||||
std::map<uLib::Object*, bool> currentObjects;
|
||||
for (auto obj : objects) currentObjects[obj] = true;
|
||||
|
||||
// Remove puppets for objects no longer in context
|
||||
for (auto it = m_Puppets.begin(); it != m_Puppets.end(); ) {
|
||||
if (currentObjects.find(it->first) == currentObjects.end()) {
|
||||
it->second->DisconnectRenderer(nullptr); // If we have a ref to a renderer we should disconnect but Puppet doesn't store it easily
|
||||
// Actually Puppet::DisconnectRenderer(vtkRenderer*) needs the renderer.
|
||||
// For now we just remove from assembly
|
||||
vtkPropCollection* props = it->second->GetProps();
|
||||
props->InitTraversal();
|
||||
while(vtkProp* prop = props->GetNextProp()) {
|
||||
if (vtkProp3D* p3d = vtkProp3D::SafeDownCast(prop))
|
||||
m_Assembly->RemovePart(p3d);
|
||||
}
|
||||
this->PuppetRemoved(it->second);
|
||||
delete it->second;
|
||||
it = m_Puppets.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
// Add puppets for new objects
|
||||
for (auto obj : objects) {
|
||||
if (m_Puppets.find(obj) == m_Puppets.end()) {
|
||||
Puppet* puppet = this->CreatePuppet(obj);
|
||||
if (puppet) {
|
||||
m_Puppets[obj] = puppet;
|
||||
vtkPropCollection* props = puppet->GetProps();
|
||||
props->InitTraversal();
|
||||
while(vtkProp* prop = props->GetNextProp()) {
|
||||
if (vtkProp3D* p3d = vtkProp3D::SafeDownCast(prop))
|
||||
m_Assembly->AddPart(p3d);
|
||||
}
|
||||
this->PuppetAdded(puppet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vtkObjectsContext::OnObjectAdded(uLib::Object* obj) {
|
||||
if (!obj) return;
|
||||
if (m_Puppets.find(obj) == m_Puppets.end()) {
|
||||
Puppet* puppet = this->CreatePuppet(obj);
|
||||
if (puppet) {
|
||||
m_Puppets[obj] = puppet;
|
||||
vtkPropCollection* props = puppet->GetProps();
|
||||
props->InitTraversal();
|
||||
while(vtkProp* prop = props->GetNextProp()) {
|
||||
if (vtkProp3D* p3d = vtkProp3D::SafeDownCast(prop))
|
||||
m_Assembly->AddPart(p3d);
|
||||
}
|
||||
this->PuppetAdded(puppet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vtkObjectsContext::OnObjectRemoved(uLib::Object* obj) {
|
||||
if (!obj) return;
|
||||
auto it = m_Puppets.find(obj);
|
||||
if (it != m_Puppets.end()) {
|
||||
// For now we just remove from assembly.
|
||||
// Puppet::DisconnectRenderer(vtkRenderer*) needs the renderer, but we don't have it here easily.
|
||||
vtkPropCollection* props = it->second->GetProps();
|
||||
props->InitTraversal();
|
||||
while(vtkProp* prop = props->GetNextProp()) {
|
||||
if (vtkProp3D* p3d = vtkProp3D::SafeDownCast(prop))
|
||||
m_Assembly->RemovePart(p3d);
|
||||
}
|
||||
this->PuppetRemoved(it->second);
|
||||
delete it->second;
|
||||
m_Puppets.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
Puppet* vtkObjectsContext::GetPuppet(uLib::Object* obj) {
|
||||
auto it = m_Puppets.find(obj);
|
||||
if (it != m_Puppets.end()) return it->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void vtkObjectsContext::Update() {
|
||||
for (auto const& [obj, puppet] : m_Puppets) {
|
||||
puppet->Update();
|
||||
}
|
||||
}
|
||||
|
||||
Puppet* vtkObjectsContext::CreatePuppet(uLib::Object* obj) {
|
||||
if (!obj) return nullptr;
|
||||
|
||||
const char* className = obj->GetClassName();
|
||||
if (std::strcmp(className, "ContainerBox") == 0) {
|
||||
return new vtkContainerBox(static_cast<uLib::ContainerBox*>(obj));
|
||||
} else if (std::strcmp(className, "DetectorChamber") == 0) {
|
||||
return new vtkDetectorChamber(static_cast<uLib::DetectorChamber*>(obj));
|
||||
}
|
||||
|
||||
// Fallback if we don't know the exact class but it might be a context itself
|
||||
if (auto subCtx = dynamic_cast<uLib::ObjectsContext*>(obj)) {
|
||||
return new vtkObjectsContext(subCtx);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void vtkObjectsContext::PuppetAdded(Puppet* puppet) {
|
||||
ULIB_SIGNAL_EMIT(vtkObjectsContext::PuppetAdded, puppet);
|
||||
}
|
||||
|
||||
void vtkObjectsContext::PuppetRemoved(Puppet* puppet) {
|
||||
ULIB_SIGNAL_EMIT(vtkObjectsContext::PuppetRemoved, puppet);
|
||||
}
|
||||
|
||||
} // namespace Vtk
|
||||
} // namespace uLib
|
||||
54
src/Vtk/vtkObjectsContext.h
Normal file
54
src/Vtk/vtkObjectsContext.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef U_VTKOBJECTSCONTEXT_H
|
||||
#define U_VTKOBJECTSCONTEXT_H
|
||||
|
||||
#include <map>
|
||||
#include "Core/ObjectsContext.h"
|
||||
#include "uLibVtkInterface.h"
|
||||
|
||||
class vtkAssembly;
|
||||
|
||||
namespace uLib {
|
||||
namespace Vtk {
|
||||
|
||||
/**
|
||||
* @brief vtkObjectsContext manages VTK representations (Puppets) for a collection of uLib::Objects.
|
||||
*/
|
||||
class vtkObjectsContext : public uLib::Object, public Puppet {
|
||||
public:
|
||||
virtual const char* GetClassName() const override { return "vtkObjectsContext"; }
|
||||
vtkObjectsContext(uLib::ObjectsContext *context);
|
||||
virtual ~vtkObjectsContext();
|
||||
|
||||
/** @brief Synchronizes the VTK puppets with the core ObjectsContext. */
|
||||
void Synchronize();
|
||||
|
||||
/** @brief Returns the puppet associated with a specific core object. */
|
||||
Puppet* GetPuppet(uLib::Object* obj);
|
||||
|
||||
/** @brief Updates all managed puppets. */
|
||||
virtual void Update() override;
|
||||
|
||||
public:
|
||||
virtual void PuppetAdded(Puppet* puppet);
|
||||
virtual void PuppetRemoved(Puppet* puppet);
|
||||
|
||||
public:
|
||||
/** @brief Slot called when an object is added to the core context. */
|
||||
void OnObjectAdded(uLib::Object* obj);
|
||||
/** @brief Slot called when an object is removed from the core context. */
|
||||
void OnObjectRemoved(uLib::Object* obj);
|
||||
|
||||
protected:
|
||||
/** @brief Factory method to create a puppet for a core object. */
|
||||
Puppet* CreatePuppet(uLib::Object* obj);
|
||||
|
||||
private:
|
||||
uLib::ObjectsContext *m_Context;
|
||||
std::map<uLib::Object*, Puppet*> m_Puppets;
|
||||
vtkAssembly *m_Assembly;
|
||||
};
|
||||
|
||||
} // namespace Vtk
|
||||
} // namespace uLib
|
||||
|
||||
#endif // U_VTKOBJECTSCONTEXT_H
|
||||
Reference in New Issue
Block a user