15 #ifndef VISIONTRANSFER_PARAMETER_H
16 #define VISIONTRANSFER_PARAMETER_H
28 #include <visiontransfer/common.h>
29 #include <visiontransfer/parametervalue.h>
30 #include <visiontransfer/conversionhelpers.h>
32 namespace visiontransfer {
47 class VT_EXPORT Parameter {
55 enum ParameterAccessMode {
61 enum ParameterInteractionHint {
62 INTERACTION_INVISIBLE = -1,
63 INTERACTION_INACTIVE = 0,
64 INTERACTION_ACTIVE = 1
68 Parameter(): uid(
"undefined"), name(
"undefined"), governorType(GOVERNOR_NONE), invokeGovernorOnInit(false), accessForConfig(ACCESS_NONE), accessForApi(ACCESS_NONE), interactionHint(INTERACTION_ACTIVE), isModified(false) {}
70 Parameter(
const std::string& uid);
72 std::string getUid()
const {
return uid; }
74 std::string getName()
const {
return name; }
76 std::string getModuleName()
const {
return modulename; }
78 std::string getCategoryName()
const {
return categoryname; }
80 std::string getDescription()
const {
return description; }
82 std::string getUnit()
const {
return unit; }
84 inline ParameterValue::ParameterType getType()
const {
return type; }
86 ParameterAccessMode getAccessForConfig()
const {
return accessForConfig; }
88 ParameterAccessMode getAccessForApi()
const {
return accessForApi; }
90 inline Parameter& setAccessForConfig(ParameterAccessMode mode) { accessForConfig = mode;
return *
this; }
92 inline Parameter& setAccessForApi(ParameterAccessMode mode) { accessForApi = mode;
return *
this; }
94 ParameterInteractionHint getInteractionHint()
const {
return interactionHint; }
96 inline Parameter& setInteractionHint(ParameterInteractionHint hint) { interactionHint = hint;
return *
this; }
98 bool getIsModified()
const {
return isModified; }
100 inline Parameter& setIsModified(
bool mod) { isModified = isCommand() ? false : mod;
return *
this; }
102 GovernorType getGovernorType()
const {
return governorType; }
104 std::string getGovernorString()
const {
return governorString; }
106 inline Parameter& setGovernor(GovernorType govType,
const std::string& govStr) { governorType = govType; governorString = govStr;
return *
this; }
108 bool getInvokeGovernorOnInit()
const {
return invokeGovernorOnInit; }
110 Parameter& setInvokeGovernorOnInit(
bool invoke) { invokeGovernorOnInit = invoke;
return *
this; }
116 bool isTensor()
const {
return type == ParameterValue::ParameterType::TYPE_TENSOR; }
118 bool isScalar()
const {
return type != ParameterValue::ParameterType::TYPE_TENSOR; }
120 bool isCommand()
const {
return currentValue.isCommand(); }
122 unsigned int getTensorDimension()
const {
123 return currentValue.isDefined() ? currentValue.getTensorDimension() : defaultValue.getTensorDimension(); }
125 std::vector<unsigned int> getTensorShape()
const {
126 return currentValue.isDefined() ? currentValue.getTensorShape() : defaultValue.getTensorShape();}
128 unsigned int getTensorNumElements()
const {
129 return currentValue.isDefined() ? currentValue.getTensorNumElements() : defaultValue.getTensorNumElements();}
131 std::vector<double> getTensorData()
const;
133 std::vector<double> getTensorDefaultData()
const;
135 std::vector<double>& getTensorDataReference();
137 std::vector<double>& getTensorDefaultDataReference();
139 inline Parameter& setTensorData(
const std::vector<double>& data) {
144 inline Parameter& setTensorDefaultData(
const std::vector<double>& data) {
148 #ifdef CV_MAJOR_VERSION
150 inline Parameter& setTensorFromCvSize(
const cv::Size& cvSize) {
151 if (currentValue.isDefined() && !(currentValue.isTensor())) {
152 throw std::runtime_error(
"Parameter::setTensorFromCvSize(): refused to overwrite existing non-tensor type");
154 if (isTensor() && (getTensorNumElements()!=0)) {
156 if (getTensorNumElements() != 2)
throw std::runtime_error(
"Parameter::setTensorFromSize(): refused to overwrite tensor with size != 2");
161 std::vector<double> data = { (double) cvSize.width, (
double) cvSize.height };
162 currentValue.setTensorData(data);
163 defaultValue.setTensorData(data);
167 inline void setCvSizeFromTensor(cv::Size& cvSize) {
168 if (getTensorNumElements() != 2)
throw std::runtime_error(
"Parameter::setCvSizeFromTensor(): refused to export Tensor of size!=2 to cv::Size");
169 auto val = getCurrentParameterValue();
170 cvSize = cv::Size((
int) val.tensorElementAt(0), (
int) val.tensorElementAt(1));
174 inline Parameter& setTensorFromCvMat(
const cv::Mat_<T>& cvMat) {
175 if (currentValue.isDefined() && !(currentValue.isTensor())) {
176 throw std::runtime_error(
"Parameter::setTensorFromCvMat(): refused to overwrite existing non-tensor type");
178 std::vector<unsigned int> dims = { (
unsigned int) cvMat.rows, (
unsigned int) cvMat.cols };
179 if (isTensor() && (getTensorNumElements()!=0)) {
181 if (getTensorNumElements() != dims[0]*dims[1])
throw std::runtime_error(
"Parameter::setTensorFromCvMat(): refused to overwrite tensor with cv::Mat of mismatching total size");
187 std::vector<double> data;
188 for (
unsigned int r=0; r<(
unsigned int) cvMat.rows; ++r)
189 for (
unsigned int c=0; c<(
unsigned int) cvMat.cols; ++c) {
190 data.push_back((
double) cvMat(r, c));
192 currentValue.setTensorData(data);
193 defaultValue.setTensorData(data);
198 inline void setCvMatFromTensor(cv::Mat_<T>& cvMat) {
199 if (getTensorDimension() != 2) {
200 std::ostringstream ss;
202 for (
unsigned int i=0; i<getTensorDimension(); ++i) {
203 ss << getTensorShape()[i] <<
", ";
206 ss <<
" " << getUid() <<
" " << ((int)getType());
207 throw std::runtime_error(std::string(
"Parameter::setCvMatFromTensor(): refused to export non-2D Tensor to cv::Mat, offending shape is: ")+ss.str());
209 auto& refData = getTensorDataReference();
210 cv::Mat_<T>(getTensorShape()[0], getTensorShape()[1], (T*)&refData[0]).copyTo(cvMat);
212 #endif // CV_MAJOR_VERSION
214 inline Parameter& setName(
const std::string& name) { this->name = name;
return *
this; }
216 inline Parameter& setModuleName(
const std::string& n) { this->modulename = n;
return *
this; }
218 inline Parameter& setCategoryName(
const std::string& n) { this->categoryname = n;
return *
this; }
220 inline Parameter& setDescription(
const std::string& d) { this->description = d;
return *
this; }
222 inline Parameter& setUnit(
const std::string& d) { this->unit = d;
return *
this; }
224 inline Parameter& setType(ParameterValue::ParameterType t) {
227 if (t==ParameterValue::ParameterType::TYPE_COMMAND) {
229 defaultValue.
setType(this->type);
230 currentValue.
setType(this->type);
231 defaultValue.setValue(
"");
232 currentValue.setValue(
"");
237 inline Parameter& setAsTensor(
const std::vector<unsigned int>& shape) {
238 setType(ParameterValue::TYPE_TENSOR);
239 defaultValue.setTensorShape(shape);
240 currentValue.setTensorShape(shape);
245 bool isValidNewValue(T t)
const {
246 if (validOptions.size()) {
248 for (
auto& o: validOptions) {
249 if (o.getValue<T>() == t)
return true;
253 if ((type==ParameterValue::ParameterType::TYPE_INT) || (type==ParameterValue::ParameterType::TYPE_DOUBLE)) {
254 if (minValue.isUndefined() || maxValue.isUndefined()) {
260 return val>=minValue.getValue<
double>() && val<=maxValue.getValue<
double>();
269 bool ensureValidDefault();
271 bool ensureValidCurrent();
273 template<
typename T> T enforceIncrement(T t);
277 defaultValue.
setType(getType());
278 defaultValue.setValue(enforceIncrement(t));
279 ensureValidDefault();
287 minValue.setValue(mn);
288 maxValue.setValue(mx);
289 ensureValidDefault();
290 ensureValidCurrent();
294 Parameter& unsetRange() {
295 minValue.setType(ParameterValue::ParameterType::TYPE_UNDEFINED);
296 maxValue.setType(ParameterValue::ParameterType::TYPE_UNDEFINED);
297 ensureValidDefault();
298 ensureValidCurrent();
303 Parameter& setIncrement(T t) {
304 incrementValue.setType(type);
305 incrementValue.setValue(t);
306 ensureValidDefault();
307 ensureValidCurrent();
313 currentValue.
setType(getType());
314 currentValue.setValue(enforceIncrement(t));
315 ensureValidCurrent();
324 Parameter& setOptions(
const std::vector<T>& opts,
const std::vector<std::string>& descriptions) {
325 if (opts.size() != descriptions.size())
throw std::runtime_error(
"Option list and description list of mismatched size");
326 validOptions.clear();
327 validOptionDescriptions.clear();
328 for (
unsigned int i=0; i<opts.size(); ++i) {
329 validOptions.push_back(
ParameterValue().setType(type).setValue(opts[i]));
330 validOptionDescriptions.push_back(descriptions[i]);
332 ensureValidDefault();
333 ensureValidCurrent();
338 Parameter& setOptions(std::initializer_list<T> opts, std::initializer_list<std::string> descriptions) {
339 std::vector<T> tmpOpts(opts);
340 std::vector<std::string> tmpComm(descriptions);
341 return setOptions(tmpOpts, tmpComm);
345 std::vector<T> getOptions()
const {
347 for (
auto& o: validOptions) {
348 vec.push_back(o.getValue<T>());
353 std::vector<std::string> getOptionDescriptions()
const {
354 return validOptionDescriptions;
357 inline ParameterValue getCurrentParameterValue() {
364 throw std::runtime_error(std::string(
"Tried getCurrent(), but no value set and no default defined for ") + getUid());
370 T getCurrent()
const {
372 return currentValue.getValue<T>();
375 return defaultValue.getValue<T>();
377 throw std::runtime_error(std::string(
"Tried getCurrent(), but no value set and no default defined for ") + getUid());
386 T getDefault()
const {
387 return defaultValue.getValue<T>();
392 return minValue.isDefined() ? (minValue.getValue<T>()) : (std::numeric_limits<T>::lowest());
397 return maxValue.isDefined() ? (maxValue.getValue<T>()) : ((std::numeric_limits<T>::max)());
401 T getIncrement()
const {
402 return incrementValue.isDefined() ? (incrementValue.getValue<T>()) : internal::ConversionHelpers::toStringIfStringExpected<T>(1);
405 bool hasOptions()
const {
406 return validOptions.size() > 0;
409 bool hasCurrent()
const {
410 if (type == ParameterValue::ParameterType::TYPE_TENSOR) {
412 return currentValue.isDefined() && (currentValue.getTensorCurrentDataSize() == currentValue.getTensorNumElements());
414 return currentValue.isDefined();
418 bool hasDefault()
const {
419 if (defaultValue.isTensor()) {
421 return defaultValue.isDefined() && (defaultValue.getTensorCurrentDataSize() == defaultValue.getTensorNumElements());
423 return defaultValue.isDefined();
427 bool hasRange()
const {
428 return maxValue.isDefined();
431 bool hasIncrement()
const {
432 return incrementValue.isDefined();
435 double at(
unsigned int x) {
return getCurrentParameterValue().tensorElementAt(x); }
437 double at(
unsigned int y,
unsigned int x) {
return getCurrentParameterValue().tensorElementAt(y, x); }
439 double at(
unsigned int z,
unsigned int y,
unsigned int x) {
return getCurrentParameterValue().tensorElementAt(z, y, x); }
445 std::string modulename;
446 std::string categoryname;
447 std::string description;
449 ParameterValue::ParameterType type;
456 std::vector<ParameterValue> validOptions;
457 std::vector<std::string> validOptionDescriptions;
460 GovernorType governorType;
461 std::string governorString;
464 bool invokeGovernorOnInit;
466 ParameterAccessMode accessForConfig;
467 ParameterAccessMode accessForApi;
469 ParameterInteractionHint interactionHint;