MeshLib C++ Docs
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 switch ( n )
19 {
20 case 1:
21 return std::string( "one " ) + ObjectT::ClassName();
22 case 2:
23 return std::string( "two " ) + ObjectT::ClassNameInPlural();
24 case 3:
25 return std::string( "three " ) + ObjectT::ClassNameInPlural();
26 case 4:
27 return std::string( "four " ) + ObjectT::ClassNameInPlural();
28 default:
29 return std::to_string( n ) + " " + ObjectT::ClassNameInPlural();
30 }
31}
32
33// check that given vector has exactly N objects if type ObjectT
34// returns error message if requirements are not satisfied
35template<typename ObjectT, bool visualRepresentationCheck, bool modelCheck>
36std::string sceneSelectedExactly( const std::vector<std::shared_ptr<const Object>>& objs, unsigned n )
37{
38 if ( objs.size() != n )
39 return "Select exactly " + getNObjectsLine<ObjectT>( n );
40 for ( const auto& obj : objs )
41 {
42 auto tObj = dynamic_cast<const ObjectT*>( obj.get() );
43 if ( !tObj )
44 return std::string( "Selected object(s) must be " ) + ObjectT::ClassName();
45
46 if constexpr ( modelCheck )
47 if ( !tObj->hasModel() )
48 return "Selected object(s) must have valid model";
49
50 if constexpr ( visualRepresentationCheck )
51 if ( !tObj->hasVisualRepresentation() )
52 return "Selected object(s) must have valid visual representation";
53 }
54 return "";
55}
56
57// checks that given vector has at least N objects if type ObjectT
58// returns error message if requirements are not satisfied
59template<typename ObjectT, bool visualRepresentationCheck, bool modelCheck>
60std::string sceneSelectedAtLeast( const std::vector<std::shared_ptr<const Object>>& objs, unsigned n )
61{
62 if ( objs.size() < n )
63 return "Select at least " + getNObjectsLine<ObjectT>( n );
64 unsigned i = 0;
65 for ( const auto& obj : objs )
66 {
67 auto tObj = dynamic_cast<const ObjectT*>( obj.get() );
68 if ( !tObj )
69 continue;
70
71 if constexpr ( modelCheck )
72 if ( !tObj->hasModel() )
73 continue;
74
75 if constexpr ( visualRepresentationCheck )
76 if ( !tObj->hasVisualRepresentation() )
77 continue;
78 ++i;
79 }
80 return ( i >= n ) ?
81 "" :
82 ( "Select at least " + getNObjectsLine<ObjectT>( n ) + " with valid model" );
83}
84
85// check that given vector has exactly N objects if type ObjectT
86template<unsigned N, typename ObjectT, typename = void>
88{
89public:
90 virtual ~SceneStateExactCheck() = default;
91 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
92 {
94 }
95};
96
97template<unsigned N, typename ObjectT>
99{
100public:
101 virtual ~SceneStateExactCheck() = default;
102 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
103 {
105 }
106};
107
108template<unsigned N, typename ObjectT>
109class SceneStateExactCheck<N, ObjectT, NoModelCheck> : virtual public ISceneStateCheck
110{
111public:
112 virtual ~SceneStateExactCheck() = default;
113 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
114 {
116 }
117};
118
119// checks that given vector has at least N objects if type ObjectT
120template<unsigned N, typename ObjectT, typename = void>
122{
123public:
124 virtual ~SceneStateAtLeastCheck() = default;
125 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
126 {
128 }
129};
130
131template<unsigned N, typename ObjectT>
133{
134public:
135 virtual ~SceneStateAtLeastCheck() = default;
136 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
137 {
139 }
140};
141
142template<unsigned N, typename ObjectT>
143class SceneStateAtLeastCheck<N, ObjectT, NoModelCheck> : virtual public ISceneStateCheck
144{
145public:
146 virtual ~SceneStateAtLeastCheck() = default;
147 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
148 {
150 }
151};
152
153// checks that at least one of argument checks is true
154template<typename ...Checks>
155class SceneStateOrCheck : public Checks...
156{
157public:
158 virtual ~SceneStateOrCheck() = default;
159 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>&objs ) const override
160 {
161 std::vector<std::string> checkRes;
162 checkRes.reserve( sizeof...( Checks ) );
163 ( checkRes.push_back( Checks::isAvailable( objs ) ), ... );
164 std::string combinedRes;
165 for ( int i = 0; i < checkRes.size(); ++i )
166 {
167 if ( checkRes[i].empty() )
168 return "";
169
170 if ( i != 0 )
171 checkRes[i].front() = ( char )tolower( checkRes[i].front() );
172
173 combinedRes += checkRes[i];
174 if ( i + 1 < checkRes.size() )
175 combinedRes += " or ";
176 }
177 return combinedRes;
178 }
179};
180
181// checks that all of argument checks are true
182template<typename ...Checks>
183class SceneStateAndCheck : public Checks...
184{
185public:
186 virtual ~SceneStateAndCheck() = default;
187 virtual std::string isAvailable( const std::vector<std::shared_ptr<const Object>>& objs ) const override
188 {
189 std::vector<std::string> checkRes;
190 checkRes.reserve( sizeof...( Checks ) );
191 ( checkRes.push_back( Checks::isAvailable( objs ) ), ... );
192 std::string combinedRes;
193 for ( int i = 0; i < checkRes.size(); ++i )
194 {
195 if ( checkRes[i].empty() )
196 continue;
197
198 if ( !combinedRes.empty() )
199 {
200 combinedRes += " and ";
201 checkRes[i].front() = ( char )tolower( checkRes[i].front() );
202 }
203 combinedRes += checkRes[i];
204 }
205 return combinedRes;
206 }
207};
208
209
210}
Definition MRISceneStateCheck.h:14
Definition MRSceneStateCheck.h:184
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:187
virtual ~SceneStateAndCheck()=default
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:147
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:136
Definition MRSceneStateCheck.h:122
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:125
virtual ~SceneStateAtLeastCheck()=default
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:113
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:102
Definition MRSceneStateCheck.h:88
virtual ~SceneStateExactCheck()=default
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:91
Definition MRSceneStateCheck.h:156
virtual std::string isAvailable(const std::vector< std::shared_ptr< const Object > > &objs) const override
Definition MRSceneStateCheck.h:159
virtual ~SceneStateOrCheck()=default
Definition MRCameraOrientationPlugin.h:8
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:36
std::string sceneSelectedAtLeast(const std::vector< std::shared_ptr< const Object > > &objs, unsigned n)
Definition MRSceneStateCheck.h:60
Definition MRSceneStateCheck.h:13
Definition MRSceneStateCheck.h:10