MeshLib
 
Loading...
Searching...
No Matches
MRObject.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRAffineXf3.h"
4#include "MRBox.h"
5#include "MRBitSet.h"
8#include "MRExpected.h"
9#include "MRSignal.h"
10#include <memory>
11#include <vector>
12#include <array>
13#include <future>
14#include <filesystem>
15
16namespace Json
17{
18class Value;
19}
20
21namespace MR
22{
23
35{
36public:
39 ObjectChildrenHolder & operator = ( const ObjectChildrenHolder & ) noexcept { return *this; }
43
44 // returns this Object as shared_ptr
45 // finds it among its parent's recognized children
46 [[nodiscard]] MRMESH_API std::shared_ptr<Object> getSharedPtr() const;
47
50 [[nodiscard]] MRMESH_API size_t heapBytes() const;
51
52protected:
54 std::vector< std::shared_ptr< Object > > children_;
55 std::vector< std::weak_ptr< Object > > bastards_;
56};
57
60{
61public:
62 Object() = default;
63 Object( Object && ) noexcept = default;
64 Object & operator = ( Object && ) noexcept = default;
65 virtual ~Object() = default;
66
67 // return name of subtype for serialization purposes
68 constexpr static const char* TypeName() noexcept { return "Object"; }
69 virtual const char* typeName() const { return TypeName(); }
70
71 template <typename T>
72 T * asType() { return dynamic_cast<T*>( this ); }
73 template <typename T>
74 const T * asType() const { return dynamic_cast<const T*>( this ); }
75
76 const std::string & name() const { return name_; }
77 virtual void setName( std::string name ) { name_ = std::move( name ); }
78
80 MRMESH_API std::shared_ptr<const Object> find( const std::string_view & name ) const;
81 std::shared_ptr<Object> find( const std::string_view & name ) { return std::const_pointer_cast<Object>( const_cast<const Object*>( this )->find( name ) ); }
82
84 template <typename T>
85 std::shared_ptr<const T> find() const;
86 template <typename T>
87 std::shared_ptr<T> find() { return std::const_pointer_cast<T>( const_cast<const Object*>( this )->find<T>() ); }
88
90 template <typename T>
91 std::shared_ptr<const T> find( const std::string_view & name ) const;
92 template <typename T>
93 std::shared_ptr<T> find( const std::string_view & name ) { return std::const_pointer_cast<T>( const_cast<const Object*>( this )->find<T>( name ) ); }
94
97 const AffineXf3f & xf( ViewportId id = {}, bool * isDef = nullptr ) const { return xf_.get( id, isDef ); }
98 MRMESH_API virtual void setXf( const AffineXf3f& xf, ViewportId id = {} );
100 MRMESH_API virtual void resetXf( ViewportId id = {} );
101
103 const ViewportProperty<AffineXf3f> & xfsForAllViewports() const { return xf_; }
105 virtual void setXfsForAllViewports( ViewportProperty<AffineXf3f> xf ) { xf_ = std::move( xf ); }
106
109 MRMESH_API AffineXf3f worldXf( ViewportId id = {}, bool * isDef = nullptr ) const;
110 MRMESH_API void setWorldXf( const AffineXf3f& xf, ViewportId id = {} );
111
113 MRMESH_API virtual void applyScale( float scaleFactor );
114
118 bool globalVisibility( ViewportMask viewportMask = ViewportMask::any() ) const { return !( globalVisibilityMask() & viewportMask ).empty(); }
121
123 bool isLocked() const { return locked_; }
124 virtual void setLocked( bool on ) { locked_ = on; }
125
128 [[nodiscard]] bool isParentLocked() const { return parentLocked_; }
129 virtual void setParentLocked( bool lock ) { parentLocked_ = lock; }
130
132 const Object * parent() const { return static_cast<const Object *>( parent_ ); }
133 Object * parent() { return static_cast<Object *>( parent_ ); }
134
136 MRMESH_API bool isAncestor( const Object* ancestor ) const;
137
142 [[nodiscard]] const Object* findCommonAncestor( const Object& other ) const
143 {
144 return const_cast<Object &>( *this ).findCommonAncestor( const_cast<Object &>( other ) );
145 }
146
151 const std::vector<std::shared_ptr<Object>>& children() { return children_; }
152 const std::vector<std::shared_ptr<const Object>>& children() const { return reinterpret_cast<const std::vector< std::shared_ptr< const Object > > &>( children_ ); }
157 MRMESH_API virtual bool addChild( std::shared_ptr<Object> child, bool recognizedChild = true );
161 MRMESH_API virtual bool addChildBefore( std::shared_ptr<Object> newChild, const std::shared_ptr<Object> & existingChild );
163 bool removeChild( const std::shared_ptr<Object>& child ) { return removeChild( child.get() ); }
164 MRMESH_API virtual bool removeChild( Object* child );
169
171 MRMESH_API virtual bool select( bool on );
172 virtual bool isSelected() const { return selected_; }
173
176 MRMESH_API virtual void setAncillary( bool ancillary );
177 bool isAncillary() const { return ancillary_; }
178
180 MRMESH_API void setVisible( bool on, ViewportMask viewportMask = ViewportMask::all() );
182 bool isVisible( ViewportMask viewportMask = ViewportMask::any() ) const { return !( visibilityMask() & viewportMask ).empty(); }
184 MRMESH_API virtual void setVisibilityMask( ViewportMask viewportMask );
186 virtual ViewportMask visibilityMask() const { return visibilityMask_; }
187
189 virtual bool getRedrawFlag( ViewportMask ) const { return needRedraw_; }
190 void resetRedrawFlag() const { needRedraw_ = false; }
191
193 MRMESH_API std::shared_ptr<Object> cloneTree() const;
195 MRMESH_API virtual std::shared_ptr<Object> clone() const;
198 MRMESH_API std::shared_ptr<Object> shallowCloneTree() const;
201 MRMESH_API virtual std::shared_ptr<Object> shallowClone() const;
202
204 MRMESH_API virtual std::vector<std::string> getInfoLines() const;
206 virtual std::string getClassName() const { return "Object"; }
207
212 MRMESH_API Expected<std::vector<std::future<VoidOrErrStr>>> serializeRecursive( const std::filesystem::path& path, Json::Value& root, int childId ) const;
213
217 MRMESH_API VoidOrErrStr deserializeRecursive( const std::filesystem::path& path, const Json::Value& root,
218 ProgressCallback progressCb = {}, int* objCounter = nullptr );
219
224
226 virtual Box3f getWorldBox( ViewportId = {} ) const { return {}; }
229
231 [[nodiscard]] virtual bool hasVisualRepresentation() const { return false; }
232
235 [[nodiscard]] virtual bool hasModel() const { return false; }
236
238 [[nodiscard]] MRMESH_API virtual size_t heapBytes() const;
239
243 using XfChangedSignal = Signal<void() >;
245protected:
246 struct ProtectedStruct{ explicit ProtectedStruct() = default; };
247public:
249 Object( ProtectedStruct, const Object& obj ) : Object( obj ) {}
250
251protected:
253 Object( const Object& obj ) = default;
254
260
263 MRMESH_API virtual Expected<std::future<VoidOrErrStr>> serializeModel_( const std::filesystem::path& path ) const;
264
267 MRMESH_API virtual void serializeFields_( Json::Value& root ) const;
268
270 MRMESH_API virtual VoidOrErrStr deserializeModel_( const std::filesystem::path& path, ProgressCallback progressCb = {} );
271
274 MRMESH_API virtual void deserializeFields_( const Json::Value& root );
275
276 std::string name_;
278 ViewportMask visibilityMask_ = ViewportMask::all(); // Prefer to not read directly. Use the getter, as it can be overridden.
279 bool locked_ = false;
280 bool parentLocked_ = false;
281 bool selected_{ false };
282 bool ancillary_{ false };
283 mutable bool needRedraw_{false};
284
286};
287
288template <typename T>
289std::shared_ptr<const T> Object::find() const
290{
291 for ( const auto & child : children_ )
292 if ( auto res = std::dynamic_pointer_cast<T>( child ) )
293 return res;
294 return {}; // not found
295}
296
297template <typename T>
298std::shared_ptr<const T> Object::find( const std::string_view & name ) const
299{
300 for ( const auto & child : children_ )
301 if ( child->name() == name )
302 if ( auto res = std::dynamic_pointer_cast<T>( child ) )
303 return res;
304 return {}; // not found
305}
306
308
309}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
#define MRMESH_CLASS
Definition MRMesh/MRMeshFwd.h:50
Definition MRObject.h:35
std::vector< std::shared_ptr< Object > > children_
Definition MRObject.h:54
std::vector< std::weak_ptr< Object > > bastards_
recognized ones
Definition MRObject.h:55
MRMESH_API size_t heapBytes() const
ObjectChildrenHolder & operator=(const ObjectChildrenHolder &) noexcept
Definition MRObject.h:39
MRMESH_API std::shared_ptr< Object > getSharedPtr() const
ObjectChildrenHolder(const ObjectChildrenHolder &) noexcept
Definition MRObject.h:38
ObjectChildrenHolder * parent_
Definition MRObject.h:53
MRMESH_API ObjectChildrenHolder(ObjectChildrenHolder &&) noexcept
named object in the data model
Definition MRObject.h:60
virtual MRMESH_API void setVisibilityMask(ViewportMask viewportMask)
specifies object visibility as bitmask of viewports
Object * parent()
Definition MRObject.h:133
virtual MRMESH_API void applyScale(float scaleFactor)
scale object size (all point positions)
virtual MRMESH_API std::shared_ptr< Object > shallowClone() const
virtual MRMESH_API void resetXf(ViewportId id={})
forgets specific transform in given viewport (or forgets all specific transforms for {}...
MRMESH_API ViewportMask globalVisibilityMask() const
returns all viewports where this object is visible together with all its parents
bool globalVisibility(ViewportMask viewportMask=ViewportMask::any()) const
returns true if this object is visible together with all its parents in any of given viewports
Definition MRObject.h:118
virtual MRMESH_API void removeAllChildren()
detaches all recognized children from this, keeping all unrecognized ones
T * asType()
Definition MRObject.h:72
MRMESH_API void setWorldXf(const AffineXf3f &xf, ViewportId id={})
virtual MRMESH_API bool removeChild(Object *child)
virtual MRMESH_API Expected< std::future< VoidOrErrStr > > serializeModel_(const std::filesystem::path &path) const
const std::vector< std::shared_ptr< const Object > > & children() const
Definition MRObject.h:152
virtual MRMESH_API void setXf(const AffineXf3f &xf, ViewportId id={})
std::shared_ptr< T > find()
Definition MRObject.h:87
virtual MRMESH_API void setAncillary(bool ancillary)
virtual bool hasModel() const
Definition MRObject.h:235
MRMESH_API void swap(Object &other)
std::string name_
Definition MRObject.h:276
bool isAncillary() const
Definition MRObject.h:177
virtual bool isSelected() const
Definition MRObject.h:172
MRMESH_API Box3f getWorldTreeBox(ViewportId={}) const
returns bounding box of this object and all children visible in given (or default) viewport in world ...
virtual const char * typeName() const
Definition MRObject.h:69
MRMESH_API Expected< std::vector< std::future< VoidOrErrStr > > > serializeRecursive(const std::filesystem::path &path, Json::Value &root, int childId) const
virtual bool getRedrawFlag(ViewportMask) const
this method virtual because others data model types could have dirty flags or something
Definition MRObject.h:189
virtual MRMESH_API void serializeFields_(Json::Value &root) const
MRMESH_API void setVisible(bool on, ViewportMask viewportMask=ViewportMask::all())
sets the object visible in the viewports specified by the mask (by default in all viewports)
MRMESH_API std::shared_ptr< Object > cloneTree() const
clones all tree of this object (except ancillary and unrecognized children)
virtual MRMESH_API void propagateWorldXfChangedSignal_()
ViewportProperty< AffineXf3f > xf_
Definition MRObject.h:277
const AffineXf3f & xf(ViewportId id={}, bool *isDef=nullptr) const
Definition MRObject.h:97
std::shared_ptr< Object > find(const std::string_view &name)
Definition MRObject.h:81
const T * asType() const
Definition MRObject.h:74
const Object * parent() const
returns parent object in the tree
Definition MRObject.h:132
const ViewportProperty< AffineXf3f > & xfsForAllViewports() const
returns xfs for all viewports, combined into a single object
Definition MRObject.h:103
virtual void setXfsForAllViewports(ViewportProperty< AffineXf3f > xf)
modifies xfs for all viewports at once
Definition MRObject.h:105
MRMESH_API void sortChildren()
sort recognized children by name
Object(ProtectedStruct, const Object &obj)
Definition MRObject.h:249
MRMESH_API bool isAncestor(const Object *ancestor) const
return true if given object is ancestor of this one, false otherwise
std::shared_ptr< T > find(const std::string_view &name)
Definition MRObject.h:93
virtual MRMESH_API bool detachFromParent()
virtual MRMESH_API void swapSignals_(Object &other)
XfChangedSignal worldXfChangedSignal
Definition MRObject.h:244
virtual MRMESH_API bool addChildBefore(std::shared_ptr< Object > newChild, const std::shared_ptr< Object > &existingChild)
virtual MRMESH_API void swapBase_(Object &other)
swaps whole object (signals too)
const std::vector< std::shared_ptr< Object > > & children()
an object can hold other sub-objects
Definition MRObject.h:151
virtual ViewportMask visibilityMask() const
gets object visibility as bitmask of viewports
Definition MRObject.h:186
virtual MRMESH_API bool select(bool on)
selects the object, returns true if value changed, otherwise returns false
bool isVisible(ViewportMask viewportMask=ViewportMask::any()) const
checks whether the object is visible in any of the viewports specified by the mask (by default in any...
Definition MRObject.h:182
void resetRedrawFlag() const
Definition MRObject.h:190
virtual bool hasVisualRepresentation() const
does the object have any visual representation (visible points, triangles, edges, etc....
Definition MRObject.h:231
MRMESH_API Object * findCommonAncestor(Object &other)
virtual MRMESH_API VoidOrErrStr deserializeModel_(const std::filesystem::path &path, ProgressCallback progressCb={})
Reads model from file.
virtual MRMESH_API bool addChild(std::shared_ptr< Object > child, bool recognizedChild=true)
bool removeChild(const std::shared_ptr< Object > &child)
returns false if it was not child of this
Definition MRObject.h:163
Object(Object &&) noexcept=default
MRMESH_API std::shared_ptr< const Object > find(const std::string_view &name) const
finds a direct child by name
MRMESH_API VoidOrErrStr deserializeRecursive(const std::filesystem::path &path, const Json::Value &root, ProgressCallback progressCb={}, int *objCounter=nullptr)
Object()=default
virtual void setLocked(bool on)
Definition MRObject.h:124
Object(const Object &obj)=default
user should not be able to call copy implicitly, use clone() function instead
MRMESH_API std::shared_ptr< Object > shallowCloneTree() const
virtual void setParentLocked(bool lock)
Definition MRObject.h:129
virtual void setName(std::string name)
Definition MRObject.h:77
virtual std::string getClassName() const
return human readable name of subclass
Definition MRObject.h:206
bool isLocked() const
object properties lock for UI
Definition MRObject.h:123
virtual MRMESH_API std::vector< std::string > getInfoLines() const
return several info lines that can better describe object in the UI
virtual MRMESH_API size_t heapBytes() const
returns the amount of memory this object occupies on heap
MRMESH_API AffineXf3f worldXf(ViewportId id={}, bool *isDef=nullptr) const
virtual MRMESH_API std::shared_ptr< Object > clone() const
clones current object only, without parent and/or children
bool isParentLocked() const
Definition MRObject.h:128
const std::string & name() const
Definition MRObject.h:76
virtual Box3f getWorldBox(ViewportId={}) const
returns bounding box of this object in world coordinates for default or specific viewport
Definition MRObject.h:226
const Object * findCommonAncestor(const Object &other) const
Definition MRObject.h:142
MRMESH_API void setGlobalVisibility(bool on, ViewportMask viewportMask=ViewportMask::any())
if true sets all predecessors visible, otherwise sets this object invisible
virtual MRMESH_API void deserializeFields_(const Json::Value &root)
Definition MRViewportId.h:16
stores mask of viewport unique identifiers
Definition MRViewportId.h:38
static ViewportMask any()
Definition MRViewportId.h:46
static ViewportMask all()
mask meaning all or any viewports
Definition MRViewportId.h:45
Definition MRViewportProperty.h:17
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:589
std::shared_ptr< const T > find() const
finds a direct child by type
Definition MRObject.h:289
Definition MRCameraOrientationPlugin.h:7
tl::expected< T, E > Expected
Definition MRExpected.h:49
Expected< void > VoidOrErrStr
return type for a void function that can produce an error string
Definition MRExpected.h:60
Definition MRObject.h:246