refactor: update transformation system, improve template readability, and reorganize VTK assembly management

This commit is contained in:
AndreaRigoni
2026-03-31 16:04:03 +00:00
parent 22d0041942
commit d4fd2d3914
30 changed files with 568 additions and 501 deletions

View File

@@ -26,15 +26,15 @@
#ifndef ULIBVTKINTERFACE_H
#define ULIBVTKINTERFACE_H
#include "Core/Monitor.h"
#include "Core/Object.h"
#include "Core/Property.h"
#include <boost/mpl/bool.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/type_traits/is_class.hpp>
#include <iomanip>
#include <ostream>
#include <vector>
#include <boost/type_traits/is_class.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/serialization/serialization.hpp>
#include "Core/Object.h"
#include "Core/Property.h"
#include "Core/Monitor.h"
// vtk classes forward declaration //
class vtkProp;
@@ -46,22 +46,27 @@ class vtkRendererCollection;
class vtkRenderWindowInteractor;
namespace uLib {
namespace Archive { class display_properties_archive; }
namespace Vtk { class Puppet; class Viewer; }
namespace Archive {
class display_properties_archive;
}
namespace Vtk {
class Puppet;
class Viewer;
} // namespace Vtk
} // namespace uLib
namespace uLib {
namespace Vtk {
class Puppet : public uLib::Object {
uLibTypeMacro(Puppet, uLib::Object)
uLibTypeMacro(Puppet, uLib::Object)
public:
Puppet();
public : Puppet();
virtual ~Puppet();
virtual vtkProp *GetProp();
virtual vtkProp3D *GetProxyProp();
virtual vtkPropCollection *GetParts();
@@ -87,10 +92,31 @@ public:
void SetSelected(bool selected = true);
bool IsSelected() const;
/**
* @brief Synchronizes the VTK representation with the internal state and properties.
*
* This method should be called whenever the underlying model or display properties
* are modified to ensure the visual representation in VTK is consistent.
*/
virtual void Update();
virtual void SyncFromVtk();
enum Representation { Points = 0, Wireframe = 1, Surface = 2, SurfaceWithEdges = 3, Volume = 4, Outline = 5, Slice = 6 };
/**
* @brief Synchronizes the internal state and properties from the VTK representation.
*
* This method should be called when the VTK representation has been modified
* (e.g., via a gizmo) and the changes need to be pushed back to the model.
*/
virtual void SyncFromVtk() {}
enum Representation {
Points = 0,
Wireframe = 1,
Surface = 2,
SurfaceWithEdges = 3,
Volume = 4,
Outline = 5,
Slice = 6
};
void SetRepresentation(Representation mode);
void SetRepresentation(const char *mode);
@@ -101,10 +127,15 @@ public:
vtkRendererCollection *GetRenderers() const;
const std::vector<uLib::PropertyBase*>& GetDisplayProperties() const { return m_DisplayProperties; }
void RegisterDisplayProperty(uLib::PropertyBase* prop) { m_DisplayProperties.push_back(prop); }
const std::vector<uLib::PropertyBase *> &GetDisplayProperties() const {
return m_DisplayProperties;
}
void RegisterDisplayProperty(uLib::PropertyBase *prop) {
m_DisplayProperties.push_back(prop);
}
virtual void serialize_display(uLib::Archive::display_properties_archive & ar, const unsigned int version = 0);
virtual void serialize_display(uLib::Archive::display_properties_archive &ar,
const unsigned int version = 0);
virtual void ConnectInteractor(class vtkRenderWindowInteractor *interactor);
@@ -116,15 +147,15 @@ protected:
void RemoveProp(vtkProp *prop);
void ApplyAppearance(vtkProp* prop);
void ApplyTransform(vtkProp3D* p3d);
void ApplyAppearance(vtkProp *prop);
void ApplyTransform(vtkProp3D *p3d);
std::vector<uLib::PropertyBase*> m_DisplayProperties;
std::vector<uLib::PropertyBase *> m_DisplayProperties;
mutable uLib::RecursiveMutex m_UpdateMutex;
private:
Puppet(const Puppet&) = delete;
Puppet& operator=(const Puppet&) = delete;
Puppet(const Puppet &) = delete;
Puppet &operator=(const Puppet &) = delete;
friend class PuppetData;
class PuppetData *pd;
@@ -133,104 +164,110 @@ private:
} // namespace Vtk
} // namespace uLib
// -------------------------------------------------------------------------- //
// DISPLAY PROPERTIES SERIALIZE
// DISPLAY PROPERTIES SERIALIZE
// -------------------------------------------------------------------------- //
namespace uLib {
namespace Archive {
/**
* @brief Specialized archive for registering display-only properties in Puppets.
* @brief Specialized archive for registering display-only properties in
* Puppets.
*/
class display_properties_archive : public boost::archive::detail::common_oarchive<display_properties_archive> {
class display_properties_archive
: public boost::archive::detail::common_oarchive<
display_properties_archive> {
public:
display_properties_archive(Vtk::Puppet* puppet) :
boost::archive::detail::common_oarchive<display_properties_archive>(boost::archive::no_header),
display_properties_archive(Vtk::Puppet *puppet)
: boost::archive::detail::common_oarchive<display_properties_archive>(
boost::archive::no_header),
m_Puppet(puppet) {}
std::string GetCurrentGroup() const {
std::string group;
for (const auto& g : m_GroupStack) {
if (!group.empty()) group += ".";
group += g;
}
return group;
std::string GetCurrentGroup() const {
std::string group;
for (const auto &g : m_GroupStack) {
if (!group.empty())
group += ".";
group += g;
}
return group;
}
template<class T>
void save_override(const boost::serialization::hrp<T> &t) {
if (m_Puppet) {
uLib::Property<T>* p = new uLib::Property<T>(m_Puppet, t.name(), &const_cast<boost::serialization::hrp<T>&>(t).value(), t.units() ? t.units() : "", GetCurrentGroup());
if (t.has_range()) p->SetRange(t.min_val(), t.max_val());
if (t.has_default()) p->SetDefault(t.default_val());
m_Puppet->RegisterDisplayProperty(p);
Vtk::Puppet* puppet = m_Puppet;
uLib::Object::connect(p, &uLib::PropertyBase::Updated, [puppet](){ puppet->Update(); });
}
template <class T> void save_override(const boost::serialization::hrp<T> &t) {
if (m_Puppet) {
uLib::Property<T> *p = new uLib::Property<T>(
m_Puppet, t.name(),
&const_cast<boost::serialization::hrp<T> &>(t).value(),
t.units() ? t.units() : "", GetCurrentGroup());
if (t.has_range())
p->SetRange(t.min_val(), t.max_val());
if (t.has_default())
p->SetDefault(t.default_val());
m_Puppet->RegisterDisplayProperty(p);
Vtk::Puppet *puppet = m_Puppet;
uLib::Object::connect(p, &uLib::PropertyBase::Updated,
[puppet]() { puppet->Update(); });
}
}
template<class T>
void save_override(const boost::serialization::hrp_enum<T> &t) {
if (m_Puppet) {
uLib::EnumProperty* p = new uLib::EnumProperty(m_Puppet, t.name(), (int*)&const_cast<boost::serialization::hrp_enum<T>&>(t).value(), t.labels(), t.units() ? t.units() : "", GetCurrentGroup());
m_Puppet->RegisterDisplayProperty(p);
Vtk::Puppet* puppet = m_Puppet;
uLib::Object::connect(p, &uLib::PropertyBase::Updated, [puppet](){ puppet->Update(); });
}
template <class T>
void save_override(const boost::serialization::hrp_enum<T> &t) {
if (m_Puppet) {
uLib::EnumProperty *p = new uLib::EnumProperty(
m_Puppet, t.name(),
(int *)&const_cast<boost::serialization::hrp_enum<T> &>(t).value(),
t.labels(), t.units() ? t.units() : "", GetCurrentGroup());
m_Puppet->RegisterDisplayProperty(p);
Vtk::Puppet *puppet = m_Puppet;
uLib::Object::connect(p, &uLib::PropertyBase::Updated,
[puppet]() { puppet->Update(); });
}
}
template<class T> void save_override(const boost::serialization::nvp<T> &t) {
if (t.name()) m_GroupStack.push_back(t.name());
this->save_helper(t.const_value(), typename boost::is_class<T>::type());
if (t.name()) m_GroupStack.pop_back();
}
template <class T> void save_override(const boost::serialization::nvp<T> &t) {
if (t.name())
m_GroupStack.push_back(t.name());
this->save_helper(t.const_value(), typename boost::is_class<T>::type());
if (t.name())
m_GroupStack.pop_back();
}
// Recursion for nested classes, ignore primitives
template<class T> void save_override(const T &t) {
this->save_helper(t, typename boost::is_class<T>::type());
}
// Recursion for nested classes, ignore primitives
template <class T> void save_override(const T &t) {
this->save_helper(t, typename boost::is_class<T>::type());
}
template<class T>
void save_helper(const T &t, boost::mpl::true_) {
boost::serialization::serialize_adl(*this, const_cast<T&>(t), 0);
}
template <class T> void save_helper(const T &t, boost::mpl::true_) {
boost::serialization::serialize_adl(*this, const_cast<T &>(t), 0);
}
template<class T>
void save_helper(const T &t, boost::mpl::false_) {}
template <class T> void save_helper(const T &t, boost::mpl::false_) {}
void save_override(const boost::archive::object_id_type & t) {}
void save_override(const boost::archive::object_reference_type & t) {}
void save_override(const boost::archive::version_type & t) {}
void save_override(const boost::archive::class_id_type & t) {}
void save_override(const boost::archive::class_id_optional_type & t) {}
void save_override(const boost::archive::class_id_reference_type & t) {}
void save_override(const boost::archive::class_name_type & t) {}
void save_override(const boost::archive::tracking_type & t) {}
void save_override(const boost::archive::object_id_type &t) {}
void save_override(const boost::archive::object_reference_type &t) {}
void save_override(const boost::archive::version_type &t) {}
void save_override(const boost::archive::class_id_type &t) {}
void save_override(const boost::archive::class_id_optional_type &t) {}
void save_override(const boost::archive::class_id_reference_type &t) {}
void save_override(const boost::archive::class_name_type &t) {}
void save_override(const boost::archive::tracking_type &t) {}
private:
Vtk::Puppet* m_Puppet;
std::vector<std::string> m_GroupStack;
Vtk::Puppet *m_Puppet;
std::vector<std::string> m_GroupStack;
};
} // namespace Archive
// This macro MUST be defined after both Puppet and display_properties_archive are fully defined.
#define ULIB_ACTIVATE_DISPLAY_PROPERTIES \
{ \
uLib::Archive::display_properties_archive dar(this); \
this->serialize_display(dar, 0); \
}
// This macro MUST be defined after both Puppet and display_properties_archive
// are fully defined.
#define ULIB_ACTIVATE_DISPLAY_PROPERTIES \
{ \
uLib::Archive::display_properties_archive dar(this); \
this->serialize_display(dar, 0); \
}
} // namespace uLib