MeshLib C++ Docs
Loading...
Searching...
No Matches
MRObject.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRAffineXf3.h"
4#include "MRBitSet.h"
5#include "MRBox.h"
6#include "MRExpected.h"
8#include "MRSignal.h"
10
11#include <array>
12#include <filesystem>
13#include <future>
14#include <memory>
15#include <unordered_set>
16#include <vector>
17
18namespace Json
19{
20class Value;
21}
22
23namespace MR
24{
25
37{
38public:
41 ObjectChildrenHolder & operator = ( const ObjectChildrenHolder & ) noexcept { return *this; }
45
46 // returns this Object as shared_ptr
47 // finds it among its parent's recognized children
48 [[nodiscard]] MRMESH_API std::shared_ptr<Object> getSharedPtr() const;
49
52 [[nodiscard]] MRMESH_API size_t heapBytes() const;
53
54protected:
56 std::vector< std::shared_ptr< Object > > children_;
57 std::vector< std::weak_ptr< Object > > bastards_;
58};
59
62{
63public:
64 Object() = default;
65 Object( Object && ) noexcept = default;
66 Object & operator = ( Object && ) noexcept = default;
67 virtual ~Object() = default;
68
69 // return name of subtype for serialization purposes
70 constexpr static const char* TypeName() noexcept { return "Object"; }
71 virtual const char* typeName() const { return TypeName(); }
72
73 template <typename T>
74 T * asType() { return dynamic_cast<T*>( this ); }
75 template <typename T>
76 const T * asType() const { return dynamic_cast<const T*>( this ); }
77
78 const std::string & name() const { return name_; }
79 virtual void setName( std::string name ) { name_ = std::move( name ); }
80
82 MRMESH_API std::shared_ptr<const Object> find( const std::string_view & name ) const;
83 std::shared_ptr<Object> find( const std::string_view & name ) { return std::const_pointer_cast<Object>( const_cast<const Object*>( this )->find( name ) ); }
84
86 template <typename T>
87 std::shared_ptr<const T> find() const;
88 template <typename T>
89 std::shared_ptr<T> find() { return std::const_pointer_cast<T>( const_cast<const Object*>( this )->find<T>() ); }
90
92 template <typename T>
93 std::shared_ptr<const T> find( const std::string_view & name ) const;
94 template <typename T>
95 std::shared_ptr<T> find( const std::string_view & name ) { return std::const_pointer_cast<T>( const_cast<const Object*>( this )->find<T>( name ) ); }
96
99 const AffineXf3f & xf( ViewportId id = {}, bool * isDef = nullptr ) const { return xf_.get( id, isDef ); }
100 MRMESH_API virtual void setXf( const AffineXf3f& xf, ViewportId id = {} );
102 MRMESH_API virtual void resetXf( ViewportId id = {} );
103
105 const ViewportProperty<AffineXf3f> & xfsForAllViewports() const { return xf_; }
107 virtual void setXfsForAllViewports( ViewportProperty<AffineXf3f> xf ) { xf_ = std::move( xf ); }
108
111 MRMESH_API AffineXf3f worldXf( ViewportId id = {}, bool * isDef = nullptr ) const;
112 MRMESH_API void setWorldXf( const AffineXf3f& xf, ViewportId id = {} );
113
115 MRMESH_API virtual void applyScale( float scaleFactor );
116
120 bool globalVisibility( ViewportMask viewportMask = ViewportMask::any() ) const { return !( globalVisibilityMask() & viewportMask ).empty(); }
123
125 bool isLocked() const { return locked_; }
126 virtual void setLocked( bool on ) { locked_ = on; }
127
130 [[nodiscard]] bool isParentLocked() const { return parentLocked_; }
131 virtual void setParentLocked( bool lock ) { parentLocked_ = lock; }
132
134 const Object * parent() const { return static_cast<const Object *>( parent_ ); }
135 Object * parent() { return static_cast<Object *>( parent_ ); }
136
138 MRMESH_API bool isAncestor( const Object* ancestor ) const;
139
144 [[nodiscard]] const Object* findCommonAncestor( const Object& other ) const
145 {
146 return const_cast<Object &>( *this ).findCommonAncestor( const_cast<Object &>( other ) );
147 }
148
153 const std::vector<std::shared_ptr<Object>>& children() { return children_; }
154
155 #ifdef __GNUC__
156 #pragma GCC diagnostic push
157 #pragma GCC diagnostic ignored "-Wstrict-aliasing" // Fingers crossed.
158 #endif
159 const std::vector<std::shared_ptr<const Object>>& children() const { return reinterpret_cast<const std::vector< std::shared_ptr< const Object > > &>( children_ ); }
160 #ifdef __GNUC__
161 #pragma GCC diagnostic pop
162 #endif
163
168 MRMESH_API virtual bool addChild( std::shared_ptr<Object> child, bool recognizedChild = true );
172 MRMESH_API virtual bool addChildBefore( std::shared_ptr<Object> newChild, const std::shared_ptr<Object> & existingChild );
174 bool removeChild( const std::shared_ptr<Object>& child ) { return removeChild( child.get() ); }
175 MRMESH_API virtual bool removeChild( Object* child );
180
182 MRMESH_API virtual bool select( bool on );
183 virtual bool isSelected() const { return selected_; }
184
187 MRMESH_API virtual void setAncillary( bool ancillary );
188 bool isAncillary() const { return ancillary_; }
189
191 MRMESH_API void setVisible( bool on, ViewportMask viewportMask = ViewportMask::all() );
193 bool isVisible( ViewportMask viewportMask = ViewportMask::any() ) const { return !( visibilityMask() & viewportMask ).empty(); }
195 MRMESH_API virtual void setVisibilityMask( ViewportMask viewportMask );
197 virtual ViewportMask visibilityMask() const { return visibilityMask_; }
198
200 virtual bool getRedrawFlag( ViewportMask ) const { return needRedraw_; }
201 void resetRedrawFlag() const { needRedraw_ = false; }
202
204 MRMESH_API std::shared_ptr<Object> cloneTree() const;
206 MRMESH_API virtual std::shared_ptr<Object> clone() const;
209 MRMESH_API std::shared_ptr<Object> shallowCloneTree() const;
212 MRMESH_API virtual std::shared_ptr<Object> shallowClone() const;
213
215 MRMESH_API virtual std::vector<std::string> getInfoLines() const;
216
218 virtual std::string getClassName() const { return "Object"; }
219
221 virtual std::string getClassNameInPlural() const { return "Objects"; }
222
227 // This would be automatically skipped in the bindings anyway because of the `Json::Value` parameter.
228 // But skipping it here prevents the vector-of-futures type from being registered, which is helpful.
229 // TODO: figure out how to automate this (add a flag to the parser to outright reject functions based on their parameter and return types).
230 MRMESH_API MR_BIND_IGNORE Expected<std::vector<std::future<Expected<void>>>> serializeRecursive( const std::filesystem::path& path, Json::Value& root, int childId ) const;
231
235 MRMESH_API Expected<void> deserializeRecursive( const std::filesystem::path& path, const Json::Value& root,
236 ProgressCallback progressCb = {}, int* objCounter = nullptr );
237
242
244 virtual Box3f getWorldBox( ViewportId = {} ) const { return {}; }
247
249 [[nodiscard]] virtual bool hasVisualRepresentation() const { return false; }
250
253 [[nodiscard]] virtual bool hasModel() const { return false; }
254
257 const std::unordered_set<std::string>& tags() const { return tags_; }
260 MRMESH_API bool addTag( std::string tag );
263 MRMESH_API bool removeTag( const std::string& tag );
264
266 [[nodiscard]] MRMESH_API virtual size_t heapBytes() const;
267
271 using XfChangedSignal = Signal<void()>;
273protected:
274 struct ProtectedStruct{ explicit ProtectedStruct() = default; };
275public:
277 Object( ProtectedStruct, const Object& obj ) : Object( obj ) {}
278
279protected:
281 Object( const Object& obj ) = default;
282
288
291 MRMESH_API virtual Expected<std::future<Expected<void>>> serializeModel_( const std::filesystem::path& path ) const;
292
295 MRMESH_API virtual void serializeFields_( Json::Value& root ) const;
296
298 MRMESH_API virtual Expected<void> deserializeModel_( const std::filesystem::path& path, ProgressCallback progressCb = {} );
299
302 MRMESH_API virtual void deserializeFields_( const Json::Value& root );
303
304 std::string name_;
306 ViewportMask visibilityMask_ = ViewportMask::all(); // Prefer to not read directly. Use the getter, as it can be overridden.
307 bool locked_ = false;
308 bool parentLocked_ = false;
309 bool selected_{ false };
310 bool ancillary_{ false };
311 mutable bool needRedraw_{false};
312 std::unordered_set<std::string> tags_;
313
314 // This calls `onWorldXfChanged_()` for all children recursively, which in turn emits `worldXfChangedSignal`.
315 // This isn't virtual because it wouldn't be very useful, because it doesn't call itself on the children
316 // (it doesn't use a true recursion, instead imitiating one, presumably to save stack space, though this is unlikely to be an issue).
318 // Emits `worldXfChangedSignal`, but derived classes can add additional behavior to it.
320};
321
322template <typename T>
323std::shared_ptr<const T> Object::find() const
324{
325 for ( const auto & child : children_ )
326 if ( auto res = std::dynamic_pointer_cast<T>( child ) )
327 return res;
328 return {}; // not found
329}
330
331template <typename T>
332std::shared_ptr<const T> Object::find( const std::string_view & name ) const
333{
334 for ( const auto & child : children_ )
335 if ( child->name() == name )
336 if ( auto res = std::dynamic_pointer_cast<T>( child ) )
337 return res;
338 return {}; // not found
339}
340
342
343}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:80
#define MRMESH_CLASS
Definition MRMesh/MRMeshFwd.h:84
Definition MRObject.h:37
std::vector< std::shared_ptr< Object > > children_
Definition MRObject.h:56
std::vector< std::weak_ptr< Object > > bastards_
recognized ones
Definition MRObject.h:57
MRMESH_API size_t heapBytes() const
ObjectChildrenHolder & operator=(const ObjectChildrenHolder &) noexcept
Definition MRObject.h:41
MRMESH_API std::shared_ptr< Object > getSharedPtr() const
ObjectChildrenHolder(const ObjectChildrenHolder &) noexcept
Definition MRObject.h:40
ObjectChildrenHolder * parent_
Definition MRObject.h:55
MRMESH_API ObjectChildrenHolder(ObjectChildrenHolder &&) noexcept
named object in the data model
Definition MRObject.h:62
virtual MRMESH_API void setVisibilityMask(ViewportMask viewportMask)
specifies object visibility as bitmask of viewports
Object * parent()
Definition MRObject.h:135
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:120
virtual MRMESH_API void removeAllChildren()
detaches all recognized children from this, keeping all unrecognized ones
MRMESH_API bool addTag(std::string tag)
virtual std::string getClassNameInPlural() const
return human readable name of subclass in plural form
Definition MRObject.h:221
T * asType()
Definition MRObject.h:74
MRMESH_API void setWorldXf(const AffineXf3f &xf, ViewportId id={})
virtual MRMESH_API bool removeChild(Object *child)
const std::vector< std::shared_ptr< const Object > > & children() const
Definition MRObject.h:159
virtual MRMESH_API void setXf(const AffineXf3f &xf, ViewportId id={})
std::shared_ptr< T > find()
Definition MRObject.h:89
virtual MRMESH_API void setAncillary(bool ancillary)
virtual bool hasModel() const
Definition MRObject.h:253
MRMESH_API void swap(Object &other)
std::string name_
Definition MRObject.h:304
bool isAncillary() const
Definition MRObject.h:188
virtual bool isSelected() const
Definition MRObject.h:183
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:71
virtual bool getRedrawFlag(ViewportMask) const
this method virtual because others data model types could have dirty flags or something
Definition MRObject.h:200
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)
ViewportProperty< AffineXf3f > xf_
Definition MRObject.h:305
const AffineXf3f & xf(ViewportId id={}, bool *isDef=nullptr) const
Definition MRObject.h:99
std::shared_ptr< Object > find(const std::string_view &name)
Definition MRObject.h:83
const T * asType() const
Definition MRObject.h:76
const Object * parent() const
returns parent object in the tree
Definition MRObject.h:134
virtual MRMESH_API Expected< void > deserializeModel_(const std::filesystem::path &path, ProgressCallback progressCb={})
Reads model from file.
const ViewportProperty< AffineXf3f > & xfsForAllViewports() const
returns xfs for all viewports, combined into a single object
Definition MRObject.h:105
virtual void setXfsForAllViewports(ViewportProperty< AffineXf3f > xf)
modifies xfs for all viewports at once
Definition MRObject.h:107
MRMESH_API void sortChildren()
sort recognized children by name
Object(ProtectedStruct, const Object &obj)
Definition MRObject.h:277
virtual MRMESH_API Expected< std::future< Expected< void > > > serializeModel_(const std::filesystem::path &path) const
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:95
virtual MRMESH_API bool detachFromParent()
virtual MRMESH_API void swapSignals_(Object &other)
XfChangedSignal worldXfChangedSignal
Definition MRObject.h:272
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:153
virtual ViewportMask visibilityMask() const
gets object visibility as bitmask of viewports
Definition MRObject.h:197
MRMESH_API MR_BIND_IGNORE Expected< std::vector< std::future< Expected< void > > > > serializeRecursive(const std::filesystem::path &path, Json::Value &root, int childId) const
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:193
void resetRedrawFlag() const
Definition MRObject.h:201
virtual bool hasVisualRepresentation() const
does the object have any visual representation (visible points, triangles, edges, etc....
Definition MRObject.h:249
MRMESH_API Object * findCommonAncestor(Object &other)
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:174
MRMESH_API void sendWorldXfChangedSignal_()
MRMESH_API bool removeTag(const std::string &tag)
Object(Object &&) noexcept=default
MRMESH_API std::shared_ptr< const Object > find(const std::string_view &name) const
finds a direct child by name
const std::unordered_set< std::string > & tags() const
Definition MRObject.h:257
Object()=default
virtual void setLocked(bool on)
Definition MRObject.h:126
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:131
virtual void setName(std::string name)
Definition MRObject.h:79
virtual std::string getClassName() const
return human readable name of subclass
Definition MRObject.h:218
bool isLocked() const
object properties lock for UI
Definition MRObject.h:125
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
virtual MRMESH_API void onWorldXfChanged_()
MRMESH_API Expected< void > deserializeRecursive(const std::filesystem::path &path, const Json::Value &root, ProgressCallback progressCb={}, int *objCounter=nullptr)
bool isParentLocked() const
Definition MRObject.h:130
const std::string & name() const
Definition MRObject.h:78
virtual Box3f getWorldBox(ViewportId={}) const
returns bounding box of this object in world coordinates for default or specific viewport
Definition MRObject.h:244
const Object * findCommonAncestor(const Object &other) const
Definition MRObject.h:144
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)
std::unordered_set< std::string > tags_
Definition MRObject.h:312
Definition MRViewportId.h:16
stores mask of viewport unique identifiers
Definition MRViewportId.h:42
static ViewportMask any()
Definition MRViewportId.h:50
static ViewportMask all()
mask meaning all or any viewports
Definition MRViewportId.h:49
Definition MRViewportProperty.h:17
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:728
std::shared_ptr< const T > find() const
finds a direct child by type
Definition MRObject.h:323
Definition MRCameraOrientationPlugin.h:8
tl::expected< T, E > Expected
Definition MRExpected.h:25
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:10
Definition MRObject.h:274