MeshLib
 
Loading...
Searching...
No Matches
MRFeatureObject.h
Go to the documentation of this file.
1#pragma once
3#include "MRMeshFwd.h"
4
5#include "MRVector3.h"
6
7#include <cassert>
8#include <variant>
9
10namespace MR
11{
12
13using FeaturesPropertyTypesVariant = std::variant<float, Vector3f>;
14
15class FeatureObject;
16
17// Classifies `FeatureObjectSharedProperty`, mostly for informational purposes.
19{
20 position, // Position, normally Vector3f.
21 linearDimension, // Length or size.
22 direction, // Direction, normally Vector3f.
23 angle, // Angle, normally float. Measure in radians.
24 other,
25};
26
27// FeatureObjectSharedProperty struct is designed to represent a shared property of a feature object, enabling the use of generalized getter and setter methods for property manipulation.
28// propertyName: A string representing the name of the property.
29// getter : A std::function encapsulating a method with no parameters that returns a FeaturesPropertyTypesVariant.This allows for a generic way to retrieve the value of the property.
30// setter : A std::function encapsulating a method that takes a FeaturesPropertyTypesVariant as a parameter and returns void.This function sets the value of the property.
31// The templated constructor of this struct takes the property name, pointers to the getter and setter member functions, and a pointer to the object( obj ).
32// The constructor initializes the propertyName and uses lambdas to adapt the member function pointers into std::function objects that conform to the expected
33// getter and setter signatures.The getter lambda invokes the getter method on the object, and the setter lambda ensures the correct variant type is passed before
34// invoking the setter method.
36{
37 std::string propertyName;
39 // due to getAllSharedProperties in FeatureObject returns static vector, we need externaly setup object to invoke setter ad getter.
40 std::function<FeaturesPropertyTypesVariant( const FeatureObject* objectToInvoke, ViewportId id )> getter;
41 // NOTE: `id` should usually be `{}`, not the current viewport ID, to set the property for all viewports.
42 // Passing a non-zero ID would only modify the active viewport, and per-viewport properties aren't usually used.
43 std::function<void( const FeaturesPropertyTypesVariant&, FeatureObject* objectToInvoke, ViewportId id )> setter;
44
45 template <typename T, typename C, typename SetterFunc>
47 std::string name,
49 T( C::* m_getter )( ViewportId ) const,
50 SetterFunc m_setter
51 )
52 : propertyName( std::move( name ) ),
53 kind( kind ),
54 getter( [m_getter] ( const FeatureObject* objectToInvoke, ViewportId id ) -> FeaturesPropertyTypesVariant
55 {
56 return std::invoke( m_getter, dynamic_cast< const C* >( objectToInvoke ), id );
57 } )
58 {
59 if constexpr ( ( std::is_same_v<SetterFunc, void ( C::* )( const T&, ViewportId )> )
60 || ( std::is_same_v<SetterFunc, void ( C::* )( T, ViewportId )> ) )
61 {
62 setter = [m_setter] ( const FeaturesPropertyTypesVariant& v, FeatureObject* objectToInvoke, ViewportId id )
63 {
64 assert( std::holds_alternative<T>( v ) );
65 if ( std::holds_alternative<T>( v ) )
66 {
67 std::invoke( m_setter, dynamic_cast< C* > ( objectToInvoke ), std::get<T>( v ), id );
68 }
69 };
70 }
71 else
72 {
73 static_assert( dependent_false<T>, "Setter function signature unsupported" );
74 }
75 }
76};
77
80 std::optional<Vector3f> normal;
81};
82
83enum class MRMESH_CLASS FeatureVisualizePropertyType
84{
86 DetailsOnNameTag, // If true, show additional details on the name tag, such as point coordinates. Not all features use this.
87 _count [[maybe_unused]],
88};
89template <> struct IsVisualizeMaskEnum<FeatureVisualizePropertyType> : std::true_type {};
90
93{
94public:
96 virtual const std::vector<FeatureObjectSharedProperty>& getAllSharedProperties() const = 0;
97
98 [[nodiscard]] MRMESH_API bool supportsVisualizeProperty( AnyVisualizeMaskEnum type ) const override;
101
102 MRMESH_API void serializeFields_( Json::Value& root ) const override;
103 MRMESH_API void deserializeFields_( const Json::Value& root ) override;
104
105
106 // Since a point on an abstract feature is difficult to uniquely parameterize,
107 // the projection function simultaneously returns the normal to the surface at the projection point.
108 [[nodiscard]] virtual FeatureObjectProjectPointResult projectPoint( const Vector3f& point, ViewportId id = {} ) const = 0;
109 [[nodiscard]] MRMESH_API std::optional<Vector3f> getNormal( const Vector3f& point ) const;
110
111 MRMESH_API void setXf( const AffineXf3f& xf, ViewportId id = {} ) override;
112 MRMESH_API void resetXf( ViewportId id = {} ) override;
113
114 // Returns point considered as base for the feature
115 [[nodiscard]] MRMESH_API virtual Vector3f getBasePoint( ViewportId id = {} ) const;
116
117 // The cached orthonormalized rotation matrix.
118 // `isDef` receives false if matrix is overridden for this specific viewport.
119 [[nodiscard]] Matrix3f getRotationMatrix( ViewportId id = {}, bool* isDef = nullptr ) const { return r_.get( id, isDef ); }
120 // The cached scale and shear matrix. The main diagnoal stores the scale, and some other elements store the shearing.
121 // `isDef` receives false if matrix is overridden for this specific viewport.
122 [[nodiscard]] Matrix3f getScaleShearMatrix( ViewportId id = {}, bool* isDef = nullptr ) const { return s_.get( id, isDef ); }
123
124 // This color is used for subfeatures.
125 // `isDef` receives false if matrix is overridden for this specific viewport.
126 MRMESH_API const Color& getDecorationsColor( bool selected, ViewportId viewportId = {}, bool* isDef = nullptr ) const;
127 MRMESH_API virtual void setDecorationsColor( const Color& color, bool selected, ViewportId viewportId = {} );
130
131 // Point size and line width, for primary rendering rather than subfeatures.
132 [[nodiscard]] MRMESH_API virtual float getPointSize() const;
133 [[nodiscard]] MRMESH_API virtual float getLineWidth() const;
134 MRMESH_API virtual void setPointSize( float pointSize );
135 MRMESH_API virtual void setLineWidth( float lineWidth );
136
137 // Point size and line width, for subfeatures rather than primary rendering.
138 [[nodiscard]] MRMESH_API virtual float getSubfeaturePointSize() const;
139 [[nodiscard]] MRMESH_API virtual float getSubfeatureLineWidth() const;
140 MRMESH_API virtual void setSubfeaturePointSize( float pointSize );
141 MRMESH_API virtual void setSubfeatureLineWidth( float lineWidth );
142
143 // Per-component alpha multipliers. The global alpha is multiplied by thise.
144 [[nodiscard]] MRMESH_API virtual float getMainFeatureAlpha() const;
145 [[nodiscard]] MRMESH_API virtual float getSubfeatureAlphaPoints() const;
146 [[nodiscard]] MRMESH_API virtual float getSubfeatureAlphaLines() const;
147 [[nodiscard]] MRMESH_API virtual float getSubfeatureAlphaMesh() const;
148 MRMESH_API virtual void setMainFeatureAlpha( float alpha );
149 MRMESH_API virtual void setSubfeatureAlphaPoints( float alpha );
150 MRMESH_API virtual void setSubfeatureAlphaLines( float alpha );
151 MRMESH_API virtual void setSubfeatureAlphaMesh( float alpha );
152
153protected:
154 // `numDimensions` is 0 for points, 1 for lines, 2 for surface meshes. We don't use 3 at the moment.
155 MRMESH_API FeatureObject( int numDimensions );
156
157 MRMESH_API void setAllVisualizeProperties_( const AllVisualizeProperties& properties, std::size_t& pos ) override;
158
159 ViewportMask subfeatureVisibility_ = ViewportMask::all();
160 ViewportMask detailsOnNameTag_ = ViewportMask::all();
161
162 // Decomposition of the transformation matrix xf.A into a rotation and scaling matrix.Updated automatically in the setXf() method
163 // This cache need for fast calculation of feature properties w/o expensive transformation matrix QR decomposition.
166
167 // This is used for subfeatures. The index is for `isSelected()`.
168 std::array<ViewportProperty<Color>, 2> decorationsColor_;
169
170 // Those apply only to some features:
171
172 // Point size and line width, for primary rendering rather than subfeatures.
173 float pointSize_ = 10;
174 float lineWidth_ = 2;
175
176 // Point size and line width, for subfeatures rather than primary rendering.
177 float subPointSize_ = 6;
178 float subLineWidth_ = 1;
179
180 // Per-component alpha multipliers. The global alpha is multiplied by thise.
181 float mainFeatureAlpha_ = 1;
182 float subAlphaPoints_ = 1;
183 float subAlphaLines_ = 1;
184 float subAlphaMesh_ = 0.5f;
185};
186
187}
DetailsOnNameTag
Definition MRFeatureObject.h:86
Subfeatures
Definition MRFeatureObject.h:85
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
#define MRMESH_CLASS
Definition MRMesh/MRMeshFwd.h:50
Definition MRVisualObject.h:67
An interface class which allows feature objects to share setters and getters on their main properties...
Definition MRFeatureObject.h:93
MRMESH_API AllVisualizeProperties getAllVisualizeProperties() const override
get all visualize properties masks
virtual MRMESH_API float getMainFeatureAlpha() const
MRMESH_API const ViewportMask & getVisualizePropertyMask(AnyVisualizeMaskEnum type) const override
returns mask of viewports where given property is set
virtual MRMESH_API float getPointSize() const
std::array< ViewportProperty< Color >, 2 > decorationsColor_
Definition MRFeatureObject.h:168
virtual MRMESH_API void setLineWidth(float lineWidth)
MRMESH_API void setXf(const AffineXf3f &xf, ViewportId id={}) override
virtual MRMESH_API void setSubfeatureAlphaLines(float alpha)
virtual MRMESH_API float getSubfeatureAlphaMesh() const
virtual MRMESH_API float getSubfeaturePointSize() const
virtual MRMESH_API const ViewportProperty< Color > & getDecorationsColorForAllViewports(bool selected) const
virtual MRMESH_API void setDecorationsColor(const Color &color, bool selected, ViewportId viewportId={})
virtual MRMESH_API float getLineWidth() const
Matrix3f getScaleShearMatrix(ViewportId id={}, bool *isDef=nullptr) const
Definition MRFeatureObject.h:122
MRMESH_API FeatureObject(int numDimensions)
virtual const std::vector< FeatureObjectSharedProperty > & getAllSharedProperties() const =0
Create and generate list of bounded getters and setters for the main properties of feature object,...
virtual MRMESH_API void setPointSize(float pointSize)
virtual MRMESH_API Vector3f getBasePoint(ViewportId id={}) const
Matrix3f getRotationMatrix(ViewportId id={}, bool *isDef=nullptr) const
Definition MRFeatureObject.h:119
virtual MRMESH_API void setSubfeatureAlphaPoints(float alpha)
MRMESH_API bool supportsVisualizeProperty(AnyVisualizeMaskEnum type) const override
Returns true if this class supports the property type. Otherwise passing it to the functions below is...
ViewportProperty< Matrix3f > s_
Definition MRFeatureObject.h:165
MRMESH_API void resetXf(ViewportId id={}) override
forgets specific transform in given viewport (or forgets all specific transforms for {}...
virtual MRMESH_API float getSubfeatureLineWidth() const
virtual MRMESH_API void setDecorationsColorForAllViewports(ViewportProperty< Color > val, bool selected)
virtual MRMESH_API float getSubfeatureAlphaLines() const
MRMESH_API void serializeFields_(Json::Value &root) const override
ViewportProperty< Matrix3f > r_
Definition MRFeatureObject.h:164
virtual MRMESH_API void setSubfeaturePointSize(float pointSize)
virtual FeatureObjectProjectPointResult projectPoint(const Vector3f &point, ViewportId id={}) const =0
virtual MRMESH_API void setSubfeatureAlphaMesh(float alpha)
MRMESH_API std::optional< Vector3f > getNormal(const Vector3f &point) const
virtual MRMESH_API void setMainFeatureAlpha(float alpha)
MRMESH_API void deserializeFields_(const Json::Value &root) override
MRMESH_API void setAllVisualizeProperties_(const AllVisualizeProperties &properties, std::size_t &pos) override
virtual MRMESH_API void setSubfeatureLineWidth(float lineWidth)
MRMESH_API const Color & getDecorationsColor(bool selected, ViewportId viewportId={}, bool *isDef=nullptr) const
virtual MRMESH_API float getSubfeatureAlphaPoints() const
Definition MRViewportId.h:16
stores mask of viewport unique identifiers
Definition MRViewportId.h:38
Definition MRViewportProperty.h:17
Visual Object.
Definition MRVisualObject.h:131
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
std::vector< ViewportMask > AllVisualizeProperties
Definition MRVisualObject.h:85
Definition MRCameraOrientationPlugin.h:7
FeaturePropertyKind
Definition MRFeatureObject.h:19
std::variant< float, Vector3f > FeaturesPropertyTypesVariant
Definition MRFeatureObject.h:13
constexpr bool dependent_false
Definition MRMesh/MRMeshFwd.h:619
Definition MRColor.h:9
Definition MRFeatureObject.h:78
std::optional< Vector3f > normal
Definition MRFeatureObject.h:80
Vector3f point
Definition MRFeatureObject.h:79
Definition MRFeatureObject.h:36
std::function< FeaturesPropertyTypesVariant(const FeatureObject *objectToInvoke, ViewportId id)> getter
Definition MRFeatureObject.h:40
std::function< void(const FeaturesPropertyTypesVariant &, FeatureObject *objectToInvoke, ViewportId id)> setter
Definition MRFeatureObject.h:43
std::string propertyName
Definition MRFeatureObject.h:37
FeatureObjectSharedProperty(std::string name, FeaturePropertyKind kind, T(C::*m_getter)(ViewportId) const, SetterFunc m_setter)
Definition MRFeatureObject.h:46
FeaturePropertyKind kind
Definition MRFeatureObject.h:38
Definition MRVisualObject.h:45