MeshLib C++ Docs
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.
18enum class FeaturePropertyKind
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.
35struct FeatureObjectSharedProperty
36{
37 std::string propertyName;
38 FeaturePropertyKind kind;
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,
48 FeaturePropertyKind kind,
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
78struct FeatureObjectProjectPointResult {
79 Vector3f point;
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:
95 constexpr static const char* StaticTypeName() noexcept { return "FeatureObject"; }
96 virtual const char* typeName() const override { return StaticTypeName(); }
97
98 constexpr static const char* StaticClassName() noexcept { return "Feature"; }
99 virtual std::string className() const override { return StaticClassName(); }
100
101 constexpr static const char* StaticClassNameInPlural() noexcept { return "Features"; }
102 virtual std::string classNameInPlural() const override { return StaticClassNameInPlural(); }
103
105 virtual const std::vector<FeatureObjectSharedProperty>& getAllSharedProperties() const = 0;
106
107 [[nodiscard]] MRMESH_API bool supportsVisualizeProperty( AnyVisualizeMaskEnum type ) const override;
108 MRMESH_API AllVisualizeProperties getAllVisualizeProperties() const override;
109 MRMESH_API const ViewportMask& getVisualizePropertyMask( AnyVisualizeMaskEnum type ) const override;
110
111 MRMESH_API void serializeFields_( Json::Value& root ) const override;
112 MRMESH_API void deserializeFields_( const Json::Value& root ) override;
113
114
115 // Since a point on an abstract feature is difficult to uniquely parameterize,
116 // the projection function simultaneously returns the normal to the surface at the projection point.
117 [[nodiscard]] virtual FeatureObjectProjectPointResult projectPoint( const Vector3f& point, ViewportId id = {} ) const = 0;
118 [[nodiscard]] MRMESH_API std::optional<Vector3f> getNormal( const Vector3f& point ) const;
119
120 MRMESH_API void setXf( const AffineXf3f& xf, ViewportId id = {} ) override;
121 MRMESH_API void resetXf( ViewportId id = {} ) override;
122
123 // Returns point considered as base for the feature
124 [[nodiscard]] MRMESH_API virtual Vector3f getBasePoint( ViewportId id = {} ) const;
125
126 // The cached orthonormalized rotation matrix.
127 // `isDef` receives false if matrix is overridden for this specific viewport.
128 [[nodiscard]] Matrix3f getRotationMatrix( ViewportId id = {}, bool* isDef = nullptr ) const { return r_.get( id, isDef ); }
129 // The cached scale and shear matrix. The main diagnoal stores the scale, and some other elements store the shearing.
130 // `isDef` receives false if matrix is overridden for this specific viewport.
131 [[nodiscard]] Matrix3f getScaleShearMatrix( ViewportId id = {}, bool* isDef = nullptr ) const { return s_.get( id, isDef ); }
132
133 // This color is used for subfeatures.
134 // `isDef` receives false if matrix is overridden for this specific viewport.
135 MRMESH_API const Color& getDecorationsColor( bool selected, ViewportId viewportId = {}, bool* isDef = nullptr ) const;
136 MRMESH_API virtual void setDecorationsColor( const Color& color, bool selected, ViewportId viewportId = {} );
137 MRMESH_API virtual const ViewportProperty<Color>& getDecorationsColorForAllViewports( bool selected ) const;
138 MRMESH_API virtual void setDecorationsColorForAllViewports( ViewportProperty<Color> val, bool selected );
139
140 // Point size and line width, for primary rendering rather than subfeatures.
141 [[nodiscard]] MRMESH_API virtual float getPointSize() const;
142 [[nodiscard]] MRMESH_API virtual float getLineWidth() const;
143 MRMESH_API virtual void setPointSize( float pointSize );
144 MRMESH_API virtual void setLineWidth( float lineWidth );
145
146 // Point size and line width, for subfeatures rather than primary rendering.
147 [[nodiscard]] MRMESH_API virtual float getSubfeaturePointSize() const;
148 [[nodiscard]] MRMESH_API virtual float getSubfeatureLineWidth() const;
149 MRMESH_API virtual void setSubfeaturePointSize( float pointSize );
150 MRMESH_API virtual void setSubfeatureLineWidth( float lineWidth );
151
152 // Per-component alpha multipliers. The global alpha is multiplied by thise.
153 [[nodiscard]] MRMESH_API virtual float getMainFeatureAlpha() const;
154 [[nodiscard]] MRMESH_API virtual float getSubfeatureAlphaPoints() const;
155 [[nodiscard]] MRMESH_API virtual float getSubfeatureAlphaLines() const;
156 [[nodiscard]] MRMESH_API virtual float getSubfeatureAlphaMesh() const;
157 MRMESH_API virtual void setMainFeatureAlpha( float alpha );
158 MRMESH_API virtual void setSubfeatureAlphaPoints( float alpha );
159 MRMESH_API virtual void setSubfeatureAlphaLines( float alpha );
160 MRMESH_API virtual void setSubfeatureAlphaMesh( float alpha );
161
162protected:
163 // `numDimensions` is 0 for points, 1 for lines, 2 for surface meshes. We don't use 3 at the moment.
164 MRMESH_API FeatureObject( int numDimensions );
165
166 MRMESH_API void setAllVisualizeProperties_( const AllVisualizeProperties& properties, std::size_t& pos ) override;
167
168 ViewportMask subfeatureVisibility_ = ViewportMask::all();
169 ViewportMask detailsOnNameTag_ = ViewportMask::all();
170
171 // Decomposition of the transformation matrix xf.A into a rotation and scaling matrix.Updated automatically in the setXf() method
172 // This cache need for fast calculation of feature properties w/o expensive transformation matrix QR decomposition.
173 ViewportProperty<Matrix3f> r_; // rotation
174 ViewportProperty<Matrix3f> s_; // scale
175
176 // This is used for subfeatures. The index is for `isSelected()`.
177 std::array<ViewportProperty<Color>, 2> decorationsColor_;
178
179 // Those apply only to some features:
180
181 // Point size and line width, for primary rendering rather than subfeatures.
182 float pointSize_ = 10;
183 float lineWidth_ = 2;
184
185 // Point size and line width, for subfeatures rather than primary rendering.
186 float subPointSize_ = 6;
187 float subLineWidth_ = 1;
188
189 // Per-component alpha multipliers. The global alpha is multiplied by thise.
190 float mainFeatureAlpha_ = 1;
191 float subAlphaPoints_ = 1;
192 float subAlphaLines_ = 1;
193 float subAlphaMesh_ = 0.5f;
194};
195
196}
_count
Definition MRFeatureObject.h:87
DetailsOnNameTag
Definition MRFeatureObject.h:86
Subfeatures
Definition MRFeatureObject.h:85
#define MRMESH_API
Definition MRMeshFwd.h:80
#define MRMESH_CLASS
Definition MRMeshFwd.h:87
angle
Definition MRObjectDimensionsEnum.h:13
unsafe FeatureObjectSharedProperty(MR._ByValue_FeatureObjectSharedProperty _other)
Definition MRFeatureObject.h:93
Definition MRVisualObject.h:119
std::string name(const T &primitive)
Definition MRFeatures.h:309
Definition MRCameraOrientationPlugin.h:8
ImVec2 position(const ViewportRectangle &rect)
Definition MRViewport.h:24
AllVisualizeProperties
FeaturesPropertyTypesVariant
Definition MRVisualObject.h:32