MeshLib Documentation
Loading...
Searching...
No Matches
MRVisualObject.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRObject.h"
4#include "MRMeshTexture.h"
5#include "MRVector.h"
6#include "MRColor.h"
7#include "MRPositionedText.h"
8#include "MRIRenderObject.h"
9#include "MRUniquePtr.h"
10
11namespace MR
12{
13
17
28
29// Note! Must use `MRMESH_CLASS` on this enum and all enums that extend this one,
30// otherwise you'll get silent wrong behavior on Mac.
31enum class MRMESH_CLASS VisualizeMaskType
32{
40 _count [[maybe_unused]],
41};
42
43// If a type derived from `VisualObject` wants to extend `VisualizeMaskType`, it must create a separate enum and specialize this to `true` for it.
44// NOTE! All those enums can start from 0, don't worry about collisions.
45template <typename T> struct IsVisualizeMaskEnum : std::false_type {};
46template <> struct IsVisualizeMaskEnum<VisualizeMaskType> : std::true_type {};
47
48// Wraps `IsVisualizeMaskEnum` and adds some sanity checks.
49template <typename T>
51 IsVisualizeMaskEnum<T>::value &&
52 std::is_same_v<std::underlying_type_t<T>, int> &&
53 std::is_same_v<T, std::remove_cvref_t<T>>;
54
55// Stores a `VisualizeMaskType` or any other enum that extends it (i.e. which specializes `IsVisualizeMaskEnum`).
56// To extract the value, do this:
57// if ( auto value = x.tryGet<MyEnum>() )
58// {
59// switch ( *value )
60// {
61// case MyEnum::foo: ...
62// case MyEnum::bar: ...
63// }
64// }
65// else // forward to the parent class
67{
68 std::type_index type_;
69 int value_ = 0;
70
71public:
72 template <AnyVisualizeMaskEnumType T>
73 AnyVisualizeMaskEnum( T value ) : type_( typeid(T) ), value_( decltype(value_)( value ) ) {}
74
75 template <AnyVisualizeMaskEnumType T>
76 [[nodiscard]] std::optional<T> tryGet() const
77 {
78 if ( type_ == typeid(T) )
79 return T( value_ );
80 else
81 return {};
82 }
83};
84
85using AllVisualizeProperties = std::vector<ViewportMask>;
86
111
115struct Dirty
116{
117 uint32_t f{DIRTY_ALL};
118 operator uint32_t&() { return f; }
119 operator uint32_t() const { return f; }
120
121 Dirty() noexcept = default;
122 Dirty( const Dirty& ) noexcept {}
123 Dirty( Dirty&& ) noexcept = default;
124 Dirty& operator =( const Dirty& ) noexcept { return *this; }
125 Dirty& operator =( Dirty&& ) noexcept = default;
126 Dirty& operator =( uint32_t b ) noexcept { f = b; return *this; }
127};
128
131{
132public:
134
136 VisualObject& operator = ( VisualObject&& ) = default;
137 virtual ~VisualObject() = default;
138
139 constexpr static const char* TypeName() noexcept { return "VisualObject"; }
140 virtual const char* typeName() const override { return TypeName(); }
141
143 virtual std::string getClassName() const override { return "Visual Object"; }
144
146 [[nodiscard]] MRMESH_API virtual bool supportsVisualizeProperty( AnyVisualizeMaskEnum type ) const;
148 MRMESH_API void setVisualizeProperty( bool value, AnyVisualizeMaskEnum type, ViewportMask viewportMask );
157
162 {
163 std::size_t counter = 0;
164 setAllVisualizeProperties_( properties, counter );
165 }
166
168 [[deprecated( "please use ObjectLabel mechanism instead" )]]
169 void showLabels( bool on ) { return setVisualizeProperty( on, VisualizeMaskType::Labels, ViewportMask::all() ); }
170 [[deprecated( "please use ObjectLabel mechanism instead" )]]
171 bool showLabels() const { return getVisualizeProperty( VisualizeMaskType::Labels, ViewportMask::any() ); }
172
174 void showName( bool on ) { return setVisualizeProperty( on, VisualizeMaskType::Name, ViewportMask::all() ); }
176 bool showName() const { return getVisualizeProperty( VisualizeMaskType::Name, ViewportMask::any() ); }
177
179 MRMESH_API const Color& getFrontColor( bool selected = true, ViewportId viewportId = {} ) const;
181 MRMESH_API virtual void setFrontColor( const Color& color, bool selected, ViewportId viewportId = {} );
182
184 MRMESH_API virtual const ViewportProperty<Color>& getFrontColorsForAllViewports( bool selected = true ) const;
186 MRMESH_API virtual void setFrontColorsForAllViewports( ViewportProperty<Color> val, bool selected = true );
187
192
194 MRMESH_API const Color& getBackColor( ViewportId viewportId = {} ) const;
196 MRMESH_API virtual void setBackColor( const Color& color, ViewportId viewportId = {} );
197
199 MRMESH_API const uint8_t& getGlobalAlpha( ViewportId viewportId = {} ) const;
201 MRMESH_API virtual void setGlobalAlpha( uint8_t alpha, ViewportId viewportId = {} );
202
207
208 [[deprecated( "please use ObjectLabel mechanism instead" )]]
209 MRMESH_API const Color& getLabelsColor( ViewportId viewportId = {} ) const;
210 [[deprecated( "please use ObjectLabel mechanism instead" )]]
211 MRMESH_API virtual void setLabelsColor( const Color& color, ViewportId viewportId = {} );
212
213 [[deprecated( "please use ObjectLabel mechanism instead" )]]
215
216 [[deprecated( "please use ObjectLabel mechanism instead" )]]
218
222 MRMESH_API virtual void setDirtyFlags( uint32_t mask, bool invalidateCaches = true );
224 MRMESH_API uint32_t getDirtyFlags() const { return dirty_; }
226 MRMESH_API void resetDirty() const;
227
232 MRMESH_API virtual Box3f getWorldBox( ViewportId = {} ) const override;
234 virtual bool getRedrawFlag( ViewportMask viewportMask ) const override
235 {
236 return Object::getRedrawFlag( viewportMask ) ||
237 ( isVisible( viewportMask ) &&
238 ( dirty_ & ( ~( DIRTY_CACHES ) ) ) );
239 }
240
242 bool isPickable( ViewportMask viewportMask = ViewportMask::any() ) const{return !(pickable_ & viewportMask).empty();}
244 MRMESH_API virtual void setPickable( bool on, ViewportMask viewportMask = ViewportMask::all() );
245
247 const VertColors& getVertsColorMap() const { return vertsColorMap_; }
249 virtual void setVertsColorMap( VertColors vertsColorMap ) { vertsColorMap_ = std::move( vertsColorMap ); dirty_ |= DIRTY_VERTS_COLORMAP; }
251 virtual void updateVertsColorMap( VertColors& vertsColorMap ) { std::swap( vertsColorMap_, vertsColorMap ); dirty_ |= DIRTY_VERTS_COLORMAP; }
252
254 ColoringType getColoringType() const { return coloringType_; }
255
257 MRMESH_API virtual void setColoringType( ColoringType coloringType );
258
260 MRMESH_API virtual void copyColors( const VisualObject & src, const VertMap & thisToSrc, const FaceMap& thisToSrcFaces = {} );
261
263 float getShininess() const { return shininess_; }
265 virtual void setShininess( float shininess ) { shininess_ = shininess; needRedraw_ = true; }
266
268 float getSpecularStrength() const { return specularStrength_; }
270 virtual void setSpecularStrength( float specularStrength ) { specularStrength_ = specularStrength; needRedraw_ = true; }
271
273 float getAmbientStrength() const { return ambientStrength_; }
275 virtual void setAmbientStrength( float ambientStrength ) { ambientStrength_ = ambientStrength; needRedraw_ = true; }
276
277 [[deprecated( "please use ObjectLabel mechanism instead" )]]
278 const std::vector<PositionedText>& getLabels() const { return labels_; }
279 [[deprecated( "please use ObjectLabel mechanism instead" )]]
280 virtual void setLabels( std::vector<PositionedText> labels ) { labels_ = std::move( labels ); needRedraw_ = true; }
281
284 MRMESH_API virtual std::shared_ptr<Object> clone() const override;
287 MRMESH_API virtual std::shared_ptr<Object> shallowClone() const override;
288
291 MRMESH_API virtual bool render( const ModelRenderParams& ) const;
293 MRMESH_API virtual void renderForPicker( const ModelBaseRenderParams&, unsigned ) const;
295 MRMESH_API virtual void renderUi( const UiRenderParams& params ) const;
296
299
301 [[nodiscard]] MRMESH_API virtual size_t heapBytes() const override;
303 MRMESH_API virtual std::vector<std::string> getInfoLines() const override;
304
307 [[nodiscard]] MRMESH_API bool useDefaultScenePropertiesOnDeserialization() const { return useDefaultScenePropertiesOnDeserialization_; }
310 MRMESH_API void setUseDefaultScenePropertiesOnDeserialization( bool useDefaultScenePropertiesOnDeserialization )
311 { useDefaultScenePropertiesOnDeserialization_ = useDefaultScenePropertiesOnDeserialization; }
312
313protected:
314 VisualObject( const VisualObject& obj ) = default;
315
317 MRMESH_API virtual void swapBase_( Object& other ) override;
318
321 virtual void setupRenderObject_() const {}
322
324 mutable Dirty dirty_;
325
332 ViewportMask cropLabels_ = ViewportMask::all();
333 ViewportMask pickable_ = ViewportMask::all();
335 ViewportMask depthTest_ = ViewportMask::all();
336
337 ViewportProperty<Color> labelsColor_ = {};
338
339 float shininess_{35.0f};
340 float specularStrength_{ 0.5f }; // reflection intensity
341 float ambientStrength_{ 0.1f }; //non - directional light intensity
342
344 ColoringType coloringType_{ColoringType::SolidColor};
345 VertColors vertsColorMap_;
349 ViewportProperty<uint8_t> globalAlpha_{ 255 };
350
351 std::vector<PositionedText> labels_;
352
353 bool useDefaultScenePropertiesOnDeserialization_{ false };
354
356
357 MRMESH_API virtual void serializeFields_( Json::Value& root ) const override;
358
359 MRMESH_API void deserializeFields_( const Json::Value& root ) override;
360
361 virtual Box3f computeBoundingBox_() const { return Box3f(); }
362
364 MRMESH_API void boundingBoxToInfoLines_( std::vector<std::string> & res ) const;
365
366 MRMESH_API virtual void setAllVisualizeProperties_( const AllVisualizeProperties& properties, std::size_t& pos );
367
368 // Derived classes should use this to implement `setAllVisualizeProperties()`.
369 template <AnyVisualizeMaskEnumType T>
370 void setAllVisualizePropertiesForEnum( const AllVisualizeProperties& properties, std::size_t& pos )
371 {
372 for ( int i = 0; i < int( T::_count ); i++ )
373 setVisualizePropertyMask( T( i ), properties[pos++] );
374 }
375 // Derived classes should use this to implement `getAllVisualizeProperties()`.
376 template <AnyVisualizeMaskEnumType T>
378 {
379 properties.reserve( properties.size() + std::size_t( T::_count ) );
380 for ( int i = 0; i < int( ( T::_count ) ); i++ )
381 properties.push_back( getVisualizePropertyMask( T( i ) ) );
382 }
383
384private:
385 mutable Box3f boundingBoxCache_;
386
388 void setDefaultColors_();
389
391 void setDefaultSceneProperties_();
392};
393
395
396} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
#define MRMESH_CLASS
Definition MRMesh/MRMeshFwd.h:50
InvertedNormals
Definition MRVisualObject.h:34
Visibility
Definition MRVisualObject.h:33
ClippedByPlane
Definition MRVisualObject.h:38
CropLabelsByViewportRect
Definition MRVisualObject.h:37
Labels
Definition MRVisualObject.h:36
Name
Definition MRVisualObject.h:35
DepthTest
Definition MRVisualObject.h:39
Definition MRVisualObject.h:67
AnyVisualizeMaskEnum(T value)
Definition MRVisualObject.h:73
std::optional< T > tryGet() const
Definition MRVisualObject.h:76
named object in the data model
Definition MRObject.h:60
Definition MRViewportId.h:16
stores mask of viewport unique identifiers
Definition MRViewportId.h:38
Definition MRViewportProperty.h:17
Visual Object.
Definition MRVisualObject.h:131
MRMESH_API bool useDefaultScenePropertiesOnDeserialization() const
Definition MRVisualObject.h:307
virtual MRMESH_API const ViewportProperty< Color > & getFrontColorsForAllViewports(bool selected=true) const
returns color of object when it is selected/not-selected (depending on argument) in all viewports
virtual bool getRedrawFlag(ViewportMask viewportMask) const override
returns true if the object must be redrawn (due to dirty flags) in one of specified viewports
Definition MRVisualObject.h:234
virtual MRMESH_API void setFrontColor(const Color &color, bool selected, ViewportId viewportId={})
sets color of object when it is selected/not-selected (depending on argument) in given viewport
MRMESH_API ViewportMask & getVisualizePropertyMask_(AnyVisualizeMaskEnum type)
ViewportMask showLabels_
Definition MRVisualObject.h:330
MRMESH_API void setUseDefaultScenePropertiesOnDeserialization(bool useDefaultScenePropertiesOnDeserialization)
Definition MRVisualObject.h:310
VisualObject(ProtectedStruct, const VisualObject &obj)
this ctor is public only for std::make_shared used inside clone()
Definition MRVisualObject.h:298
virtual MRMESH_API bool supportsVisualizeProperty(AnyVisualizeMaskEnum type) const
Returns true if this class supports the property type. Otherwise passing it to the functions below is...
virtual MRMESH_API void renderForPicker(const ModelBaseRenderParams &, unsigned) const
draws this object for picking
float getShininess() const
returns the current shininess visual value
Definition MRVisualObject.h:263
virtual MRMESH_API size_t heapBytes() const override
returns the amount of memory this object occupies on heap
MRMESH_API VisualObject()
virtual MRMESH_API std::shared_ptr< Object > clone() const override
MRMESH_API void setVisualizeProperty(bool value, AnyVisualizeMaskEnum type, ViewportMask viewportMask)
set visual property in all viewports specified by the mask
MRMESH_API const ViewportProperty< Color > & getLabelsColorsForAllViewports() const
MRMESH_API void resetDirty() const
resets all dirty flags (except for cache flags that will be reset automatically on cache update)
virtual MRMESH_API Box3f getWorldBox(ViewportId={}) const override
virtual ~VisualObject()=default
MRMESH_API bool getVisualizeProperty(AnyVisualizeMaskEnum type, ViewportMask viewportMask) const
returns true if the property is set at least in one viewport specified by the mask
virtual MRMESH_API void setDirtyFlags(uint32_t mask, bool invalidateCaches=true)
virtual MRMESH_API const ViewportProperty< uint8_t > & getGlobalAlphaForAllViewports() const
returns global transparency alpha of object in all viewports
MRMESH_API const Color & getLabelsColor(ViewportId viewportId={}) const
void getAllVisualizePropertiesForEnum(AllVisualizeProperties &properties) const
Definition MRVisualObject.h:377
virtual void setSpecularStrength(float specularStrength)
sets intensity of reflections
Definition MRVisualObject.h:270
virtual MRMESH_API void setBackColor(const Color &color, ViewportId viewportId={})
sets backward color of object in given viewport
static constexpr const char * TypeName() noexcept
Definition MRVisualObject.h:139
virtual void updateVertsColorMap(VertColors &vertsColorMap)
swaps per-vertex colors of the object with given argument
Definition MRVisualObject.h:251
virtual Box3f computeBoundingBox_() const
Definition MRVisualObject.h:361
virtual MRMESH_API bool render(const ModelRenderParams &) const
bool showLabels() const
Definition MRVisualObject.h:171
ViewportProperty< Color > unselectedColor_
Definition MRVisualObject.h:347
virtual MRMESH_API void setLabelsColorsForAllViewports(ViewportProperty< Color > val)
virtual MRMESH_API void setAllVisualizeProperties_(const AllVisualizeProperties &properties, std::size_t &pos)
ViewportMask showName_
Definition MRVisualObject.h:331
MRMESH_API uint32_t getDirtyFlags() const
returns current dirty flags for the object
Definition MRVisualObject.h:224
MRMESH_API void deserializeFields_(const Json::Value &root) override
virtual MRMESH_API void setBackColorsForAllViewports(ViewportProperty< Color > val)
sets backward color of object in all viewports
virtual MRMESH_API void setLabelsColor(const Color &color, ViewportId viewportId={})
virtual const char * typeName() const override
Definition MRVisualObject.h:140
bool showName() const
returns whether object name is shown in any viewport
Definition MRVisualObject.h:176
MRMESH_API const Color & getFrontColor(bool selected=true, ViewportId viewportId={}) const
returns color of object when it is selected/not-selected (depending on argument) in given viewport
virtual MRMESH_API const ViewportProperty< Color > & getBackColorsForAllViewports() const
returns backward color of object in all viewports
float getSpecularStrength() const
returns intensity of reflections
Definition MRVisualObject.h:268
virtual MRMESH_API std::vector< std::string > getInfoLines() const override
return several info lines that can better describe the object in the UI
virtual MRMESH_API void swapBase_(Object &other) override
swaps this object with other
VertColors vertsColorMap_
Definition MRVisualObject.h:345
virtual MRMESH_API void renderUi(const UiRenderParams &params) const
draws this object for 2d UI
VisualObject(const VisualObject &obj)=default
virtual std::string getClassName() const override
return human readable name of subclass
Definition MRVisualObject.h:143
Dirty dirty_
Definition MRVisualObject.h:324
virtual MRMESH_API void setGlobalAlphaForAllViewports(ViewportProperty< uint8_t > val)
sets global transparency alpha of object in all viewports
void showLabels(bool on)
shows/hides labels
Definition MRVisualObject.h:169
virtual void setupRenderObject_() const
Definition MRVisualObject.h:321
virtual MRMESH_API void setColoringType(ColoringType coloringType)
sets coloring mode of the object with given argument
MRMESH_API Box3f getBoundingBox() const
returns cached bounding box of this object in local coordinates
virtual void setAmbientStrength(float ambientStrength)
sets intensity of non-directional light
Definition MRVisualObject.h:275
bool isPickable(ViewportMask viewportMask=ViewportMask::any()) const
whether the object can be picked (by mouse) in any of given viewports
Definition MRVisualObject.h:242
virtual MRMESH_API const ViewportMask & getVisualizePropertyMask(AnyVisualizeMaskEnum type) const
returns mask of viewports where given property is set
VisualObject(VisualObject &&)=default
virtual MRMESH_API void copyColors(const VisualObject &src, const VertMap &thisToSrc, const FaceMap &thisToSrcFaces={})
copies point colors from given source object
virtual MRMESH_API void setGlobalAlpha(uint8_t alpha, ViewportId viewportId={})
sets global transparency alpha of object in given viewport
void setAllVisualizeProperties(const AllVisualizeProperties &properties)
set all visualize properties masks
Definition MRVisualObject.h:161
void showName(bool on)
shows/hides object name in all viewports
Definition MRVisualObject.h:174
void setAllVisualizePropertiesForEnum(const AllVisualizeProperties &properties, std::size_t &pos)
Definition MRVisualObject.h:370
virtual MRMESH_API void serializeFields_(Json::Value &root) const override
std::vector< PositionedText > labels_
Definition MRVisualObject.h:351
UniquePtr< IRenderObject > renderObj_
Definition MRVisualObject.h:323
virtual MRMESH_API AllVisualizeProperties getAllVisualizeProperties() const
get all visualize properties masks
ViewportMask clipByPlane_
Definition MRVisualObject.h:329
virtual void setLabels(std::vector< PositionedText > labels)
Definition MRVisualObject.h:280
ViewportProperty< Color > selectedColor_
Definition MRVisualObject.h:346
virtual MRMESH_API void setFrontColorsForAllViewports(ViewportProperty< Color > val, bool selected=true)
sets color of object when it is selected/not-selected (depending on argument) in all viewports
virtual MRMESH_API void setVisualizePropertyMask(AnyVisualizeMaskEnum type, ViewportMask viewportMask)
set visual property mask
ColoringType getColoringType() const
returns the current coloring mode of the object
Definition MRVisualObject.h:254
virtual void setVertsColorMap(VertColors vertsColorMap)
sets per-vertex colors of the object
Definition MRVisualObject.h:249
const VertColors & getVertsColorMap() const
returns per-vertex colors of the object
Definition MRVisualObject.h:247
virtual MRMESH_API void setPickable(bool on, ViewportMask viewportMask=ViewportMask::all())
sets the object as can/cannot be picked (by mouse) in all of given viewports
virtual void setShininess(float shininess)
sets shininess visual value of the object with given argument
Definition MRVisualObject.h:265
virtual MRMESH_API std::shared_ptr< Object > shallowClone() const override
ViewportProperty< Color > backFacesColor_
Definition MRVisualObject.h:348
const std::vector< PositionedText > & getLabels() const
Definition MRVisualObject.h:278
MRMESH_API const Color & getBackColor(ViewportId viewportId={}) const
returns backward color of object in given viewport
MRMESH_API void toggleVisualizeProperty(AnyVisualizeMaskEnum type, ViewportMask viewportMask)
toggle visual property in all viewports specified by the mask
ViewportMask invertNormals_
invert mesh normals
Definition MRVisualObject.h:334
MRMESH_API void boundingBoxToInfoLines_(std::vector< std::string > &res) const
adds information about bounding box in res
float getAmbientStrength() const
returns intensity of non-directional light
Definition MRVisualObject.h:273
MRMESH_API const uint8_t & getGlobalAlpha(ViewportId viewportId={}) const
returns global transparency alpha of object in given viewport
Definition MRVisualObject.h:50
DirtyFlags
Definition MRVisualObject.h:88
std::vector< ViewportMask > AllVisualizeProperties
Definition MRVisualObject.h:85
ColoringType
Definition MRVisualObject.h:21
@ DIRTY_PRIMITIVE_COLORMAP
Definition MRVisualObject.h:101
@ DIRTY_POSITION
Definition MRVisualObject.h:90
@ DIRTY_TEXTURE
Definition MRVisualObject.h:97
@ DIRTY_NONE
Definition MRVisualObject.h:89
@ DIRTY_SELECTION
Definition MRVisualObject.h:96
@ DIRTY_FACES_RENDER_NORMAL
gl normals
Definition MRVisualObject.h:93
@ DIRTY_MESH
Definition MRVisualObject.h:104
@ DIRTY_FACES_COLORMAP
Definition MRVisualObject.h:102
@ DIRTY_TEXTURE_PER_FACE
Definition MRVisualObject.h:103
@ DIRTY_FACE
Definition MRVisualObject.h:99
@ DIRTY_EDGES_SELECTION
Definition MRVisualObject.h:107
@ DIRTY_VERTS_COLORMAP
Definition MRVisualObject.h:100
@ DIRTY_RENDER_NORMALS
Definition MRVisualObject.h:95
@ DIRTY_BORDER_LINES
Definition MRVisualObject.h:106
@ DIRTY_ALL
Definition MRVisualObject.h:109
@ DIRTY_VERTS_RENDER_NORMAL
Definition MRVisualObject.h:92
@ DIRTY_CACHES
Definition MRVisualObject.h:108
@ DIRTY_UV
Definition MRVisualObject.h:91
@ DIRTY_PRIMITIVES
Definition MRVisualObject.h:98
@ DIRTY_BOUNDING_BOX
Definition MRVisualObject.h:105
@ DIRTY_CORNERS_RENDER_NORMAL
gl normals
Definition MRVisualObject.h:94
@ LinesColorMap
Use different color (taken from faces colormap) for each line (primitive for object lines)
@ VertsColorMap
Use different color (taken from verts colormap) for each vert.
@ SolidColor
Use simple color for whole mesh.
@ FacesColorMap
Use different color (taken from faces colormap) for each face (primitive for object mesh)
@ PrimitivesColorMap
Use different color (taken from faces colormap) for each ptimitive.
Definition MRCameraOrientationPlugin.h:8
Definition MRColor.h:9
Definition MRVisualObject.h:116
Dirty() noexcept=default
uint32_t f
Definition MRVisualObject.h:117
Dirty & operator=(const Dirty &) noexcept
Definition MRVisualObject.h:124
Dirty(Dirty &&) noexcept=default
Definition MRVisualObject.h:45
Definition MRIRenderObject.h:43
Mesh rendering parameters for primary rendering (as opposed to the picker).
Definition MRIRenderObject.h:51
Definition MRObject.h:246
Definition MRIRenderObject.h:107
Definition MRUniquePtr.h:12