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 "MRConstChildren.h"
5#include "MRExpected.h"
7#include "MRSignal.h"
9
10#include <array>
11#include <filesystem>
12#include <future>
13#include <memory>
14#include <set>
15#include <vector>
16
17namespace Json
18{
19class Value;
20}
21
22namespace MR
23{
24
30
36{
37public:
40 ObjectChildrenHolder & operator = ( const ObjectChildrenHolder & ) noexcept { return *this; }
44
47 [[nodiscard]] MRMESH_API std::shared_ptr<Object> getSharedPtr() const;
48
51 [[nodiscard]] MRMESH_API size_t heapBytes() const;
52
53protected:
55 std::vector< std::shared_ptr< Object > > children_;
56 std::vector< std::weak_ptr< Object > > bastards_;
57};
58
61{
62public:
63 Object() = default;
64 Object( Object && ) noexcept = default;
65 Object & operator = ( Object && ) noexcept = default;
66 virtual ~Object() = default;
67
69 constexpr static const char* StaticTypeName() noexcept { return "Object"; }
70 virtual const char* typeName() const { return StaticTypeName(); }
71
73 constexpr static const char* StaticClassName() noexcept { return "Object"; }
74 virtual std::string className() const { return StaticClassName(); }
75
77 constexpr static const char* StaticClassNameInPlural() noexcept { return "Objects"; }
78 virtual std::string classNameInPlural() const { return StaticClassNameInPlural(); }
79
80 template <typename T>
81 T * asType() { return dynamic_cast<T*>( this ); }
82 template <typename T>
83 const T * asType() const { return dynamic_cast<const T*>( this ); }
84
85 const std::string & name() const { return name_; }
86 virtual void setName( std::string name ) { name_ = std::move( name ); }
87
89 MRMESH_API std::shared_ptr<const Object> find( const std::string_view & name ) const;
90 std::shared_ptr<Object> find( const std::string_view & name ) { return std::const_pointer_cast<Object>( const_cast<const Object*>( this )->find( name ) ); }
91
93 template <typename T>
94 std::shared_ptr<const T> find() const;
95 template <typename T>
96 std::shared_ptr<T> find() { return std::const_pointer_cast<T>( const_cast<const Object*>( this )->find<T>() ); }
97
99 template <typename T>
100 std::shared_ptr<const T> find( const std::string_view & name ) const;
101 template <typename T>
102 std::shared_ptr<T> find( const std::string_view & name ) { return std::const_pointer_cast<T>( const_cast<const Object*>( this )->find<T>( name ) ); }
103
106 const AffineXf3f & xf( ViewportId id = {}, bool * isDef = nullptr ) const { return xf_.get( id, isDef ); }
107 MRMESH_API virtual void setXf( const AffineXf3f& xf, ViewportId id = {} );
109 MRMESH_API virtual void resetXf( ViewportId id = {} );
110
115
118 MRMESH_API AffineXf3f worldXf( ViewportId id = {}, bool * isDef = nullptr ) const;
119 MRMESH_API void setWorldXf( const AffineXf3f& xf, ViewportId id = {} );
120
122 MRMESH_API virtual void applyScale( float scaleFactor );
123
127 bool globalVisibility( ViewportMask viewportMask = ViewportMask::any() ) const { return !( globalVisibilityMask() & viewportMask ).empty(); }
130
132 bool isLocked() const { return locked_; }
133 virtual void setLocked( bool on ) { locked_ = on; }
134
137 [[nodiscard]] bool isParentLocked() const { return parentLocked_; }
138 virtual void setParentLocked( bool lock ) { parentLocked_ = lock; }
139
141 const Object * parent() const { return static_cast<const Object *>( parent_ ); }
142 Object * parent() { return static_cast<Object *>( parent_ ); }
143
145 MRMESH_API bool isAncestor( const Object* ancestor ) const;
146
151 [[nodiscard]] const Object* findCommonAncestor( const Object& other ) const
152 {
153 return const_cast<Object &>( *this ).findCommonAncestor( const_cast<Object &>( other ) );
154 }
155
160 const std::vector<std::shared_ptr<Object>>& children() { return children_; }
161
164 [[nodiscard]] ConstChildren constChildren() const { return ConstChildren( children_ ); }
165
166 #ifdef __GNUC__
167 #pragma GCC diagnostic push
168 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
169 #endif
171 [[deprecated( "use constChildren() instead" )]]
172 const std::vector<std::shared_ptr<const Object>>& children() const { return reinterpret_cast<const std::vector< std::shared_ptr< const Object > > &>( children_ ); }
173 #ifdef __GNUC__
174 #pragma GCC diagnostic pop
175 #endif
176
181 MRMESH_API virtual bool addChild( std::shared_ptr<Object> child, bool recognizedChild = true );
185 MRMESH_API virtual bool addChildBefore( std::shared_ptr<Object> newChild, const std::shared_ptr<Object> & existingChild );
187 bool removeChild( const std::shared_ptr<Object>& child ) { return removeChild( child.get() ); }
188 MRMESH_API virtual bool removeChild( Object* child );
193
195 MRMESH_API virtual bool select( bool on );
196 virtual bool isSelected() const { return selected_; }
197
200 MRMESH_API virtual void setAncillary( bool ancillary );
201 bool isAncillary() const { return ancillary_; }
204
206 MRMESH_API void setVisible( bool on, ViewportMask viewportMask = ViewportMask::all() );
208 bool isVisible( ViewportMask viewportMask = ViewportMask::any() ) const { return !( visibilityMask() & viewportMask ).empty(); }
210 MRMESH_API virtual void setVisibilityMask( ViewportMask viewportMask );
212 virtual ViewportMask visibilityMask() const { return visibilityMask_; }
213
215 virtual bool getRedrawFlag( ViewportMask ) const { return needRedraw_; }
216 void resetRedrawFlag() const { needRedraw_ = false; }
217
219 MRMESH_API std::shared_ptr<Object> cloneTree() const;
221 MRMESH_API virtual std::shared_ptr<Object> clone() const;
224 MRMESH_API std::shared_ptr<Object> shallowCloneTree() const;
227 MRMESH_API virtual std::shared_ptr<Object> shallowClone() const;
228
230 MRMESH_API virtual std::vector<std::string> getInfoLines() const;
231
239 MRMESH_API MR_BIND_IGNORE Expected<std::vector<std::future<Expected<void>>>> serializeRecursive( const std::filesystem::path& path, Json::Value& root, int childId ) const;
240
244 MRMESH_API Expected<void> deserializeRecursive( const std::filesystem::path& path, const Json::Value& root,
245 ProgressCallback progressCb = {}, int* objCounter = nullptr );
246
251
253 MRMESH_API virtual Box3f getWorldBox( ViewportId = {} ) const;
256
258 [[nodiscard]] virtual bool hasVisualRepresentation() const { return false; }
259
262 [[nodiscard]] virtual bool hasModel() const { return false; }
263
266 const std::set<std::string>& tags() const { return tags_; }
270 MRMESH_API bool addTag( std::string tag );
273 MRMESH_API bool removeTag( const std::string& tag );
274
276 [[nodiscard]] MRMESH_API virtual size_t heapBytes() const;
277
279 MRMESH_API virtual bool sameModels( const Object& other ) const;
281 MRMESH_API virtual size_t getModelHash() const;
282
286 using XfChangedSignal = Signal<void()>;
288protected:
289 struct ProtectedStruct{ explicit ProtectedStruct() = default; };
290public:
292 Object( ProtectedStruct, const Object& obj ) : Object( obj ) {}
293
294protected:
296 Object( const Object& obj ) = default;
297
303
306 MRMESH_API virtual Expected<std::future<Expected<void>>> serializeModel_( const std::filesystem::path& path ) const;
307
310 MRMESH_API virtual void serializeFields_( Json::Value& root ) const;
311
313 MRMESH_API virtual Expected<void> deserializeModel_( const std::filesystem::path& path, ProgressCallback progressCb = {} );
316
319 MRMESH_API virtual void deserializeFields_( const Json::Value& root );
320
321 std::string name_;
324 bool locked_ = false;
325 bool parentLocked_ = false;
326 bool selected_{ false };
327 bool ancillary_{ false };
328 mutable bool needRedraw_{false};
329 std::set<std::string> tags_;
330
335
338
339private:
340 struct MapSharedObjects;
341 Expected<std::vector<std::future<Expected<void>>>> serializeRecursive_( const std::filesystem::path& path, Json::Value& root,
342 int childId, MapSharedObjects* mapSharedObjects ) const;
343
345 struct MapLinkToSharedObjectModel;
346 Expected<void> deserializeRecursive_( const std::filesystem::path& path, const Json::Value& root,
347 int* objCounter, MapLinkToSharedObjectModel& mapLinkToSharedObjectModel, const ProgressCallback& progressCb );
348};
349
350template <typename T>
351std::shared_ptr<const T> Object::find() const
352{
353 for ( const auto & child : children_ )
354 if ( auto res = std::dynamic_pointer_cast<T>( child ) )
355 return res;
356 return {};
357}
358
359template <typename T>
360std::shared_ptr<const T> Object::find( const std::string_view & name ) const
361{
362 for ( const auto & child : children_ )
363 if ( child->name() == name )
364 if ( auto res = std::dynamic_pointer_cast<T>( child ) )
365 return res;
366 return {};
367}
368
370
371}
#define MRMESH_API
Definition MRMeshFwd.h:85
#define MRMESH_CLASS
Definition MRMeshFwd.h:92
Definition MRConstChildren.h:35
std::shared_ptr< Object > getSharedPtr() const
size_t heapBytes() const
std::vector< std::shared_ptr< Object > > children_
Definition MRObject.h:55
std::vector< std::weak_ptr< Object > > bastards_
recognized ones
Definition MRObject.h:56
ObjectChildrenHolder & operator=(const ObjectChildrenHolder &) noexcept
Definition MRObject.h:40
ObjectChildrenHolder(const ObjectChildrenHolder &) noexcept
Definition MRObject.h:39
ObjectChildrenHolder * parent_
Definition MRObject.h:54
ObjectChildrenHolder(ObjectChildrenHolder &&) noexcept
named object in the data model
Definition MRObject.h:61
Object * parent()
Definition MRObject.h:142
virtual std::shared_ptr< Object > shallowClone() const
std::shared_ptr< const Object > find(const std::string_view &name) const
finds a direct child by name
bool selected_
Definition MRObject.h:326
virtual void setXf(const AffineXf3f &xf, ViewportId id={})
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:127
T * asType()
Definition MRObject.h:81
virtual Expected< void > deserializeModel_(const std::filesystem::path &path, ProgressCallback progressCb={})
Reads model from file.
bool isAncestor(const Object *ancestor) const
return true if given object is ancestor of this one, false otherwise
virtual void resetXf(ViewportId id={})
forgets specific transform in given viewport (or forgets all specific transforms for {}...
AffineXf3f worldXf(ViewportId id={}, bool *isDef=nullptr) const
void setGlobalVisibility(bool on, ViewportMask viewportMask=ViewportMask::any())
if true sets all predecessors visible, otherwise sets this object invisible
const std::vector< std::shared_ptr< const Object > > & children() const
Definition MRObject.h:172
bool parentLocked_
Definition MRObject.h:325
std::shared_ptr< T > find()
Definition MRObject.h:96
ViewportMask globalVisibilityMask() const
returns all viewports where this object is visible together with all its parents
virtual bool hasModel() const
Definition MRObject.h:262
virtual void swapSignals_(Object &other)
virtual void applyScale(float scaleFactor)
scale object size (all point positions)
virtual std::shared_ptr< Object > clone() const
clones current object only, without parent and/or children
std::string name_
Definition MRObject.h:321
bool isAncillary() const
Definition MRObject.h:201
virtual bool isSelected() const
Definition MRObject.h:196
virtual size_t getModelHash() const
return hash of model (or hash object pointer if object has no model)
std::shared_ptr< Object > shallowCloneTree() const
virtual const char * typeName() const
Definition MRObject.h:70
virtual bool removeChild(Object *child)
static constexpr const char * StaticClassNameInPlural() noexcept
return human readable name of subclass in plural form
Definition MRObject.h:77
virtual bool getRedrawFlag(ViewportMask) const
this method virtual because others data model types could have dirty flags or something
Definition MRObject.h:215
void sortChildren()
sort recognized children by name
virtual Expected< std::future< Expected< void > > > serializeModel_(const std::filesystem::path &path) const
void sendWorldXfChangedSignal_()
virtual size_t heapBytes() const
returns the amount of memory this object occupies on heap
bool addTag(std::string tag)
Expected< void > deserializeRecursive(const std::filesystem::path &path, const Json::Value &root, ProgressCallback progressCb={}, int *objCounter=nullptr)
ViewportProperty< AffineXf3f > xf_
Definition MRObject.h:322
const AffineXf3f & xf(ViewportId id={}, bool *isDef=nullptr) const
Definition MRObject.h:106
void setWorldXf(const AffineXf3f &xf, ViewportId id={})
std::shared_ptr< Object > find(const std::string_view &name)
Definition MRObject.h:90
const T * asType() const
Definition MRObject.h:83
bool isGlobalAncillary() const
returns true if the object or any of its ancestors are ancillary
const Object * parent() const
returns parent object in the tree
Definition MRObject.h:141
const ViewportProperty< AffineXf3f > & xfsForAllViewports() const
returns xfs for all viewports, combined into a single object
Definition MRObject.h:112
virtual bool select(bool on)
selects the object, returns true if value changed, otherwise returns false
virtual void setXfsForAllViewports(ViewportProperty< AffineXf3f > xf)
modifies xfs for all viewports at once
virtual std::string classNameInPlural() const
Definition MRObject.h:78
virtual bool addChild(std::shared_ptr< Object > child, bool recognizedChild=true)
bool ancillary_
Definition MRObject.h:327
Object(ProtectedStruct, const Object &obj)
Definition MRObject.h:292
virtual void removeAllChildren()
detaches all recognized children from this, keeping all unrecognized ones
void setVisible(bool on, ViewportMask viewportMask=ViewportMask::all())
sets the object visible in the viewports specified by the mask (by default in all viewports)
std::shared_ptr< T > find(const std::string_view &name)
Definition MRObject.h:102
virtual std::vector< std::string > getInfoLines() const
return several info lines that can better describe object in the UI
MR_BIND_IGNORE Expected< std::vector< std::future< Expected< void > > > > serializeRecursive(const std::filesystem::path &path, Json::Value &root, int childId) const
Object * findCommonAncestor(Object &other)
XfChangedSignal worldXfChangedSignal
Definition MRObject.h:287
const std::vector< std::shared_ptr< Object > > & children()
an object can hold other sub-objects
Definition MRObject.h:160
virtual void swapBase_(Object &other)
swaps whole object (signals too)
bool needRedraw_
Definition MRObject.h:328
virtual ViewportMask visibilityMask() const
gets object visibility as bitmask of viewports
Definition MRObject.h:212
bool locked_
Prefer to not read directly. Use the getter, as it can be overridden.
Definition MRObject.h:324
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:208
void resetRedrawFlag() const
Definition MRObject.h:216
virtual bool hasVisualRepresentation() const
does the object have any visual representation (visible points, triangles, edges, etc....
Definition MRObject.h:258
bool removeChild(const std::shared_ptr< Object > &child)
returns false if it was not child of this
Definition MRObject.h:187
static constexpr const char * StaticClassName() noexcept
return human readable name of subclass
Definition MRObject.h:73
virtual void onWorldXfChanged_()
Emits worldXfChangedSignal, but derived classes can add additional behavior to it.
bool removeTag(const std::string &tag)
static constexpr const char * StaticTypeName() noexcept
return name of subtype for serialization purposes
Definition MRObject.h:69
Object(Object &&) noexcept=default
virtual void setVisibilityMask(ViewportMask viewportMask)
specifies object visibility as bitmask of viewports
virtual bool sameModels(const Object &other) const
return true if model of current object equals to model (the same) of other
std::set< std::string > tags_
Definition MRObject.h:329
const std::set< std::string > & tags() const
Definition MRObject.h:266
Object()=default
virtual void setLocked(bool on)
Definition MRObject.h:133
Object(const Object &obj)=default
user should not be able to call copy implicitly, use clone() function instead
ConstChildren constChildren() const
Definition MRObject.h:164
virtual void setParentLocked(bool lock)
Definition MRObject.h:138
virtual bool detachFromParent()
virtual void deserializeFields_(const Json::Value &root)
virtual void setName(std::string name)
Definition MRObject.h:86
std::shared_ptr< Object > cloneTree() const
clones all tree of this object (except ancillary and unrecognized children)
Box3f getWorldTreeBox(ViewportId={}) const
returns bounding box of this object and all children visible in given (or default) viewport in world ...
bool isLocked() const
object properties lock for UI
Definition MRObject.h:132
Signal< void()> XfChangedSignal
Definition MRObject.h:286
virtual Expected< void > setSharedModel_(const Object &other)
shares model from other object
virtual bool addChildBefore(std::shared_ptr< Object > newChild, const std::shared_ptr< Object > &existingChild)
bool isParentLocked() const
Definition MRObject.h:137
const std::string & name() const
Definition MRObject.h:85
ViewportMask visibilityMask_
Definition MRObject.h:323
virtual Box3f getWorldBox(ViewportId={}) const
returns bounding box of this object in world coordinates for default or specific viewport
virtual void serializeFields_(Json::Value &root) const
const Object * findCommonAncestor(const Object &other) const
Definition MRObject.h:151
void swap(Object &other)
virtual void setAncillary(bool ancillary)
virtual std::string className() const
Definition MRObject.h:74
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 MRMeshFwd.h:759
std::shared_ptr< const T > find() const
finds a direct child by type
Definition MRObject.h:351
tl::expected< T, E > Expected
Definition MRExpected.h:31
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:13
@ other
Angle, normally float. Measure in radians.
Definition MRFeatureObject.h:27
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRObject.h:289
Definition MRSignal.h:28