MeshLib
 
Loading...
Searching...
No Matches
MRSceneStateCheck.h
Go to the documentation of this file.
1#pragma once
2
4#include "MRMesh/MRObject.h"
5
6namespace MR
7{
8
9// special struct for disabling visual representation check
11
12// special struct for disabling model check
13struct NoModelCheck {};
14
15template<typename ObjectT>
16std::string getNObjectsLine( unsigned n )
17{
18 std::string typeName = ObjectT::TypeName();
19 if ( typeName.starts_with( "Object" ) && !typeName.ends_with( "Object" ) )
20 typeName = typeName.substr( 6 );
21 if ( typeName == "Points" )
22 typeName = "Point Cloud";
23 else if ( typeName == "Lines" )
24 typeName = "Polyline";
25 else if ( typeName == "Voxels" )
26 typeName = "Volume";
27
28 if ( n != 1 )
29 {
30 if ( typeName.ends_with( "s" ) || typeName.ends_with( "sh" ) )
31 typeName += "es";
32 else
33 typeName += "s";
34 }
35
36 switch ( n )
37 {
38 case 1:
39 return "one " + typeName;
40 case 2:
41 return "two " + typeName;
42 case 3:
43 return "three " + typeName;
44 case 4:
45 return "four " + typeName;
46 default:
47 return std::to_string( n ) + " " + typeName;
48 }
49}
50
51// check that given vector has exactly N objects if type ObjectT
52// returns error message if requirements are not satisfied
53template<typename ObjectT, bool visualRepresentationCheck, bool modelCheck>
54std::string sceneSelectedExactly( const std::vector<std::shared_ptr<const Object>>& objs, unsigned n )
55{
56 if ( objs.size() != n )
57 return "Select exactly " + getNObjectsLine<ObjectT>( n );
58 for ( const auto& obj : objs )
59 {
60 auto tObj = dynamic_cast<const ObjectT*>( obj.get() );
61 if ( !tObj )
62 return std::string( "Selected object(s) must have type: " ) + ObjectT::TypeName();
63
64 if constexpr ( modelCheck )
65 if ( !tObj->hasModel() )
66 return "Selected object(s) must have valid model";
67
68 if constexpr ( visualRepresentationCheck )
69 if ( !tObj->hasVisualRepresentation() )
70 return "Selected object(s) must have valid visual representation";
71 }
72 return "";
73}
74
75// checks that given vector has at least N objects if type ObjectT
76// returns error message if requirements are not satisfied
77template<typename ObjectT, bool visualRepresentationCheck, bool modelCheck>
78std::string sceneSelectedAtLeast( const std::vector<std::shared_ptr<const Object>>& objs, unsigned n )
79{
80 if ( objs.size() < n )
81 return "Select at least " + getNObjectsLine<ObjectT>( n );
82 unsigned i = 0;
83 for ( const auto& obj : objs )
84 {
85 auto tObj = dynamic_cast<const ObjectT*>( obj.get() );
86 if ( !tObj )
87 continue;
88
89 if constexpr ( modelCheck )
90 if ( !tObj->hasModel() )
91 continue;
92
93 if constexpr ( visualRepresentationCheck )
94 if ( !tObj->hasVisualRepresentation() )
95 continue;
96 ++i;
97 }
98 return ( i >= n ) ?
99 "" :
100 ( "Select at least " + getNObjectsLine<ObjectT>( n ) + " with valid model" );
101}
102
103// check that given vector has exactly N objects if type ObjectT
104template<unsigned N, typename ObjectT, typename = void>
106{
107public:
108 virtual ~SceneStateExactCheck() = default;
109 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
110 {
112 }
113};
114
115template<unsigned N, typename ObjectT>
117{
118public:
119 virtual ~SceneStateExactCheck() = default;
120 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
121 {
123 }
124};
125
126template<unsigned N, typename ObjectT>
127class SceneStateExactCheck<N, ObjectT, NoModelCheck> : virtual public ISceneStateCheck
128{
129public:
130 virtual ~SceneStateExactCheck() = default;
131 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
132 {
134 }
135};
136
137// checks that given vector has at least N objects if type ObjectT
138template<unsigned N, typename ObjectT, typename = void>
140{
141public:
142 virtual ~SceneStateAtLeastCheck() = default;
143 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
144 {
146 }
147};
148
149template<unsigned N, typename ObjectT>
151{
152public:
153 virtual ~SceneStateAtLeastCheck() = default;
154 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
155 {
157 }
158};
159
160template<unsigned N, typename ObjectT>
161class SceneStateAtLeastCheck<N, ObjectT, NoModelCheck> : virtual public ISceneStateCheck
162{
163public:
164 virtual ~SceneStateAtLeastCheck() = default;
165 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
166 {
168 }
169};
170
171// checks that at least one of argument checks is true
172template<typename ...Checks>
173class SceneStateOrCheck : public Checks...
174{
175public:
176 virtual ~SceneStateOrCheck() = default;
177 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>&objs ) const override
178 {
179 std::vector<std::string> checkRes;
180 checkRes.reserve( sizeof...( Checks ) );
181 ( checkRes.push_back( Checks::isAvailable( objs ) ), ... );
182 std::string combinedRes;
183 for ( int i = 0; i < checkRes.size(); ++i )
184 {
185 if ( checkRes[i].empty() )
186 return "";
187
188 if ( i != 0 )
189 checkRes[i].front() = ( char )tolower( checkRes[i].front() );
190
191 combinedRes += checkRes[i];
192 if ( i + 1 < checkRes.size() )
193 combinedRes += " or ";
194 }
195 return combinedRes;
196 }
197};
198
199// checks that all of argument checks are true
200template<typename ...Checks>
201class SceneStateAndCheck : public Checks...
202{
203public:
204 virtual ~SceneStateAndCheck() = default;
205 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
206 {
207 std::vector<std::string> checkRes;
208 checkRes.reserve( sizeof...( Checks ) );
209 ( checkRes.push_back( Checks::isAvailable( objs ) ), ... );
210 std::string combinedRes;
211 for ( int i = 0; i < checkRes.size(); ++i )
212 {
213 if ( checkRes[i].empty() )
214 continue;
215
216 if ( !combinedRes.empty() )
217 {
218 combinedRes += " and ";
219 checkRes[i].front() = ( char )tolower( checkRes[i].front() );
220 }
221 combinedRes += checkRes[i];
222 }
223 return combinedRes;
224 }
225};
226
227
228}
Definition MRISceneStateCheck.h:14
Definition MRSceneStateCheck.h:202
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:205
virtual ~SceneStateAndCheck()=default
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:165
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:154
Definition MRSceneStateCheck.h:140
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:143
virtual ~SceneStateAtLeastCheck()=default
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:131
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:120
Definition MRSceneStateCheck.h:106
virtual ~SceneStateExactCheck()=default
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:109
Definition MRSceneStateCheck.h:174
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:177
virtual ~SceneStateOrCheck()=default
Definition MRCameraOrientationPlugin.h:7
std::string getNObjectsLine(unsigned n)
Definition MRSceneStateCheck.h:16
std::string sceneSelectedExactly(const std::vector< std::shared_ptr< const Object > > &objs, unsigned n)
Definition MRSceneStateCheck.h:54
std::string sceneSelectedAtLeast(const std::vector< std::shared_ptr< const Object > > &objs, unsigned n)
Definition MRSceneStateCheck.h:78
Definition MRSceneStateCheck.h:13
Definition MRSceneStateCheck.h:10