/*////////////////////////////////////////////////////////////////////////////// // CMT Cosmic Muon Tomography project ////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova All rights reserved Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > ------------------------------------------------------------------ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. //////////////////////////////////////////////////////////////////////////////*/ #ifndef ULIBVTKINTERFACE_H #define ULIBVTKINTERFACE_H #include "Core/Monitor.h" #include "Core/Object.h" #include "Core/Property.h" #include #include #include #include #include #include // vtk classes forward declaration // class vtkProp; class vtkProp3D; class vtkPolyData; class vtkPropCollection; class vtkRenderer; class vtkRendererCollection; class vtkRenderWindowInteractor; namespace uLib { 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) public : Puppet(); virtual ~Puppet(); virtual vtkProp *GetProp(); virtual vtkProp3D *GetProxyProp(); virtual vtkPropCollection *GetParts(); virtual vtkPropCollection *GetProps(); virtual uLib::Object *GetContent() const { return nullptr; } void ConnectRenderer(vtkRenderer *renderer); void DisconnectRenderer(vtkRenderer *renderer); void ConnectViewer(Viewer *viewer); void DisonnectViewer(Viewer *viewer); void SetColor(double r, double g, double b); void SetOpacity(double alpha); void SetSelectable(bool selectable = true); bool IsSelectable() const; 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(); /** * @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); virtual void PrintSelf(std::ostream &o) const; void ShowBoundingBox(bool show); void ShowScaleMeasures(bool show); vtkRendererCollection *GetRenderers() const; const std::vector &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 ConnectInteractor(class vtkRenderWindowInteractor *interactor); void AddToViewer(class Viewport &viewer); void RemoveFromViewer(class Viewport &viewer); protected: void SetProp(vtkProp *prop); void RemoveProp(vtkProp *prop); void ApplyAppearance(vtkProp *prop); void ApplyTransform(vtkProp3D *p3d); std::vector m_DisplayProperties; mutable uLib::RecursiveMutex m_UpdateMutex; private: Puppet(const Puppet &) = delete; Puppet &operator=(const Puppet &) = delete; friend class PuppetData; class PuppetData *pd; }; } // namespace Vtk } // namespace uLib // -------------------------------------------------------------------------- // // DISPLAY PROPERTIES SERIALIZE // -------------------------------------------------------------------------- // namespace uLib { namespace Archive { /** * @brief Specialized archive for registering display-only properties in * Puppets. */ 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( 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; } template void save_override(const boost::serialization::hrp &t) { if (m_Puppet) { uLib::Property *p = new uLib::Property( m_Puppet, t.name(), &const_cast &>(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 void save_override(const boost::serialization::hrp_enum &t) { if (m_Puppet) { uLib::EnumProperty *p = new uLib::EnumProperty( m_Puppet, t.name(), (int *)&const_cast &>(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 void save_override(const boost::serialization::nvp &t) { if (t.name()) m_GroupStack.push_back(t.name()); this->save_helper(t.const_value(), typename boost::is_class::type()); if (t.name()) m_GroupStack.pop_back(); } // Recursion for nested classes, ignore primitives template void save_override(const T &t) { this->save_helper(t, typename boost::is_class::type()); } template void save_helper(const T &t, boost::mpl::true_) { boost::serialization::serialize_adl(*this, const_cast(t), 0); } template 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) {} private: Vtk::Puppet *m_Puppet; std::vector 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); \ } } // namespace uLib #endif // ULIBVTKINTERFACE_H