MeshLib
 
Loading...
Searching...
No Matches
MRFeatureHelpers.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRFeatureObject.h"
5
6#include <unordered_map>
7#include <unordered_set>
8
9namespace MR
10{
11
15{
16 Point,
17 Line,
18 Plane,
19 Circle,
20 Sphere,
22 Cone,
23 _count,
24};
25
27template <FeaturesObjectKind X> struct ObjKindTraits
28{
29};
31{
33 static constexpr std::string_view name = "Point";
34};
36{
38 static constexpr std::string_view name = "Line";
39};
41{
43 static constexpr std::string_view name = "Plane";
44};
46{
48 static constexpr std::string_view name = "Circle";
49};
51{
53 static constexpr std::string_view name = "Sphere";
54};
56{
58 static constexpr std::string_view name = "Cylinder";
59};
61{
63 static constexpr std::string_view name = "Cone";
64};
65
67template <typename F>
68bool forEachObjectKind( F&& func )
69{
70 return[&]<int ...I>( std::integer_sequence<int, I...> )
71 {
72 return ( func( std::integral_constant<FeaturesObjectKind, FeaturesObjectKind( I )>{} ) || ... );
73 }( std::make_integer_sequence<int, int( FeaturesObjectKind::_count )>{} );
74}
75
77template <typename ...P>
78[[nodiscard]] std::shared_ptr<VisualObject> makeObjectFromEnum( FeaturesObjectKind kind, P&&... params )
79{
80 std::shared_ptr<VisualObject> ret;
81 [[maybe_unused]] bool ok = forEachObjectKind( [&] ( auto thisKind )
82 {
83 if ( thisKind.value == kind )
84 {
85 ret = std::make_shared<typename ObjKindTraits<thisKind.value>::type>( std::forward<P>( params )... );
86 return true;
87 }
88 return false;
89 } );
90 assert( ok && "This object type isn't added to `ObjKindTraits`." );
91 return ret;
92}
93
95template <typename ...P>
96[[nodiscard]] std::shared_ptr<VisualObject> makeObjectFromClassName( std::string className, P&&... params )
97{
98 std::shared_ptr<VisualObject> ret;
99 [[maybe_unused]] bool ok = forEachObjectKind( [&] ( auto thisKind )
100 {
101 if ( ObjKindTraits<thisKind.value>::name == className )
102 {
103 ret = std::make_shared<typename ObjKindTraits<thisKind.value>::type>( std::forward<P>( params )... );
104 return true;
105 }
106 return false;
107 } );
108 assert( ok && "This object type isn't added to `ObjKindTraits`." );
109 return ret;
110}
111
112template <typename T>
113concept HasGetNormalMethod = requires( T t )
114{
115 {
116 t.getNormal()
117 } -> std::convertible_to<Vector3f>;
118};
119
120template <typename T>
121concept HasGetDirectionMethod = requires( T t )
122{
123 {
124 t.getDirection()
125 } -> std::convertible_to<Vector3f>;
126};
127
128// Using forEachObjectKind the template collects a list of features for which the method ...->getNormal() is available
129MRMESH_API std::optional<Vector3f> getFeatureNormal( FeatureObject* feature );
130
131// Using forEachObjectKind the template collects a list of features for which the method ...->getDirection() is available
132MRMESH_API std::optional<Vector3f> getFeatureDirection( FeatureObject* feature );
133
134// Try to getNormal from specific feature using forEachObjectKind template. Returns nullopt is ...->getNormal() is not available for given feature type.
135MRMESH_API std::unordered_set<std::string> getFeaturesTypeWithNormals();
136
137// Try to getDirection from specific feature using forEachObjectKind template. Returns nullopt is ...->getDirection() is not available for given feature type.
138MRMESH_API std::unordered_set<std::string> getFeaturesTypeWithDirections();
139
140} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRCircleObject.h:17
Definition MRConeObject.h:19
Definition MRCylinderObject.h:18
An interface class which allows feature objects to share setters and getters on their main properties...
Definition MRFeatureObject.h:93
Definition MRLineObject.h:12
Definition MRPlaneObject.h:12
Definition MRPointObject.h:13
Definition MRSphereObject.h:15
Definition MRFeatureHelpers.h:121
Definition MRFeatureHelpers.h:113
Definition MRCameraOrientationPlugin.h:7
MRMESH_API std::optional< Vector3f > getFeatureDirection(FeatureObject *feature)
FeaturesObjectKind
Definition MRFeatureHelpers.h:15
MRMESH_API std::optional< Vector3f > getFeatureNormal(FeatureObject *feature)
MRMESH_API std::unordered_set< std::string > getFeaturesTypeWithDirections()
bool forEachObjectKind(F &&func)
Calls func, which is ( auto kind ) -> bool, for each known object kind. If it returns true,...
Definition MRFeatureHelpers.h:68
MRMESH_API std::unordered_set< std::string > getFeaturesTypeWithNormals()
std::shared_ptr< VisualObject > makeObjectFromEnum(FeaturesObjectKind kind, P &&... params)
Allocates an object of type kind, passing params... to its constructor.
Definition MRFeatureHelpers.h:78
I
Definition MRMesh/MRMeshFwd.h:88
std::shared_ptr< VisualObject > makeObjectFromClassName(std::string className, P &&... params)
Allocates an object of type kind, passing params... to its constructor.
Definition MRFeatureHelpers.h:96
Definition MRLine.h:12
Various information about different types of objects.
Definition MRFeatureHelpers.h:28
Definition MRSphere.h:9