MeshLib C++ Docs
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#if MR_COMPILING_C_BINDINGS
8#endif
9
10#include <unordered_map>
11#include <unordered_set>
12
13namespace MR
14{
15
19{
20 Point,
21 Line,
22 Plane,
23 Circle,
24 Sphere,
26 Cone,
27 _count,
28};
29
31template <FeaturesObjectKind X> struct ObjKindTraits
32{
33};
35{
37 static constexpr std::string_view name = "Point";
38};
40{
42 static constexpr std::string_view name = "Line";
43};
45{
47 static constexpr std::string_view name = "Plane";
48};
50{
52 static constexpr std::string_view name = "Circle";
53};
55{
57 static constexpr std::string_view name = "Sphere";
58};
60{
62 static constexpr std::string_view name = "Cylinder";
63};
65{
67 static constexpr std::string_view name = "Cone";
68};
69
71template <typename F>
72bool forEachObjectKind( F&& func )
73{
74 return[&]<int ...I>( std::integer_sequence<int, I...> )
75 {
76 return ( func( std::integral_constant<FeaturesObjectKind, FeaturesObjectKind( I )>{} ) || ... );
77 }( std::make_integer_sequence<int, int( FeaturesObjectKind::_count )>{} );
78}
79
81template <typename ...P>
82[[nodiscard]] std::shared_ptr<VisualObject> makeObjectFromEnum( FeaturesObjectKind kind, P&&... params )
83{
84 std::shared_ptr<VisualObject> ret;
85 [[maybe_unused]] bool ok = forEachObjectKind( [&] ( auto thisKind )
86 {
87 if ( thisKind.value == kind )
88 {
89 ret = std::make_shared<typename ObjKindTraits<thisKind.value>::type>( std::forward<P>( params )... );
90 return true;
91 }
92 return false;
93 } );
94 assert( ok && "This object type isn't added to `ObjKindTraits`." );
95 return ret;
96}
97
99template <typename ...P>
100[[nodiscard]] std::shared_ptr<VisualObject> makeObjectFromClassName( std::string className, P&&... params )
101{
102 std::shared_ptr<VisualObject> ret;
103 [[maybe_unused]] bool ok = forEachObjectKind( [&] ( auto thisKind )
104 {
105 if ( ObjKindTraits<thisKind.value>::name == className )
106 {
107 ret = std::make_shared<typename ObjKindTraits<thisKind.value>::type>( std::forward<P>( params )... );
108 return true;
109 }
110 return false;
111 } );
112 assert( ok && "This object type isn't added to `ObjKindTraits`." );
113 return ret;
114}
115
116template <typename T>
117concept HasGetNormalMethod = requires( T t )
118{
119 {
120 t.getNormal()
121 } -> std::convertible_to<Vector3f>;
122};
123
124template <typename T>
125concept HasGetDirectionMethod = requires( T t )
126{
127 {
128 t.getDirection()
129 } -> std::convertible_to<Vector3f>;
130};
131
132// Using forEachObjectKind the template collects a list of features for which the method ...->getNormal() is available
133MRMESH_API std::optional<Vector3f> getFeatureNormal( FeatureObject* feature );
134
135// Using forEachObjectKind the template collects a list of features for which the method ...->getDirection() is available
136MRMESH_API std::optional<Vector3f> getFeatureDirection( FeatureObject* feature );
137
138// Try to getNormal from specific feature using forEachObjectKind template. Returns nullopt is ...->getNormal() is not available for given feature type.
139MRMESH_API std::unordered_set<std::string> getFeaturesTypeWithNormals();
140
141// Try to getDirection from specific feature using forEachObjectKind template. Returns nullopt is ...->getDirection() is not available for given feature type.
142MRMESH_API std::unordered_set<std::string> getFeaturesTypeWithDirections();
143
144} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:80
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:13
Definition MRPlaneObject.h:12
Definition MRPointObject.h:13
Definition MRSphereObject.h:15
Definition MRFeatureHelpers.h:125
Definition MRFeatureHelpers.h:117
Definition MRCameraOrientationPlugin.h:8
MRMESH_API std::optional< Vector3f > getFeatureDirection(FeatureObject *feature)
FeaturesObjectKind
Definition MRFeatureHelpers.h:19
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:72
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:82
I
Definition MRMesh/MRMeshFwd.h:130
std::shared_ptr< VisualObject > makeObjectFromClassName(std::string className, P &&... params)
Allocates an object of type kind, passing params... to its constructor.
Definition MRFeatureHelpers.h:100
Definition MRLine.h:12
Various information about different types of objects.
Definition MRFeatureHelpers.h:32
Definition MRSphere.h:11