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> 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) {}
72 std::string
getUid()
const {
return uid; }
74 std::string
getName()
const {
return name; }
82 std::string
getUnit()
const {
return unit; }
84 ParameterValue::ParameterType
getType()
const {
return type; }
106 inline Parameter&
setGovernor(GovernorType govType,
const std::string& govStr) { governorType = govType; governorString = govStr;
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(); }
123 return currentValue.isDefined() ? currentValue.getTensorDimension() : defaultValue.getTensorDimension(); }
126 return currentValue.isDefined() ? currentValue.getTensorShape() : defaultValue.getTensorShape();}
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();
148 #ifdef CV_MAJOR_VERSION 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);
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));
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);
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 227 if (t==ParameterValue::ParameterType::TYPE_COMMAND) {
229 defaultValue.
setType(this->type);
230 currentValue.
setType(this->type);
231 defaultValue.setValue(
"");
232 currentValue.setValue(
"");
238 setType(ParameterValue::TYPE_TENSOR);
239 defaultValue.setTensorShape(shape);
240 currentValue.setTensorShape(shape);
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 template<
typename T> T enforceIncrement(T t);
273 if (isValidNewValue(t)) {
274 defaultValue.
setType(this->type);
275 defaultValue.setValue(enforceIncrement(t));
284 minValue.setValue(mn);
285 maxValue.setValue(mx);
292 incrementValue.setValue(t);
298 if (isValidNewValue(t)) {
299 currentValue.
setType(this->type);
300 currentValue.setValue(enforceIncrement(t));
311 if (opts.size() != descriptions.size())
throw std::runtime_error(
"Option list and description list of mismatched size");
312 validOptions.clear();
313 for (
unsigned int i=0; i<opts.size(); ++i) {
314 validOptions.push_back(
ParameterValue().setType(type).setValue(opts[i]));
315 validOptionDescriptions.push_back(descriptions[i]);
322 std::vector<T> tmpOpts(opts);
323 std::vector<std::string> tmpComm(descriptions);
324 return setOptions(tmpOpts, tmpComm);
330 for (
auto& o: validOptions) {
331 vec.push_back(o.getValue<T>());
337 return validOptionDescriptions;
347 throw std::runtime_error(std::string(
"Tried getCurrent(), but no value set and no default defined for ") + getUid());
355 return currentValue.getValue<T>();
358 return defaultValue.getValue<T>();
360 throw std::runtime_error(std::string(
"Tried getCurrent(), but no value set and no default defined for ") + getUid());
370 return defaultValue.getValue<T>();
375 return minValue.isDefined() ? (minValue.getValue<T>()) : (std::numeric_limits<T>::lowest());
380 return maxValue.isDefined() ? (maxValue.getValue<T>()) : (std::numeric_limits<T>::max());
385 return incrementValue.isDefined() ? (incrementValue.getValue<T>()) : internal::ConversionHelpers::toStringIfStringExpected<T>(1);
389 return validOptions.size() > 0;
393 if (type == ParameterValue::ParameterType::TYPE_TENSOR) {
395 return currentValue.isDefined() && (currentValue.getTensorCurrentDataSize() == currentValue.getTensorNumElements());
397 return currentValue.isDefined();
402 if (defaultValue.isTensor()) {
404 return defaultValue.isDefined() && (defaultValue.getTensorCurrentDataSize() == defaultValue.getTensorNumElements());
406 return defaultValue.isDefined();
411 return maxValue.isDefined();
415 return incrementValue.isDefined();
418 double at(
unsigned int x) {
return getCurrentParameterValue().tensorElementAt(x); }
420 double at(
unsigned int y,
unsigned int x) {
return getCurrentParameterValue().tensorElementAt(y, x); }
422 double at(
unsigned int z,
unsigned int y,
unsigned int x) {
return getCurrentParameterValue().tensorElementAt(z, y, x); }
428 std::string modulename;
429 std::string categoryname;
430 std::string description;
432 ParameterValue::ParameterType type;
439 std::vector<ParameterValue> validOptions;
440 std::vector<std::string> validOptionDescriptions;
443 GovernorType governorType;
444 std::string governorString;
447 bool invokeGovernorOnInit;
449 ParameterAccessMode accessForConfig;
450 ParameterAccessMode accessForApi;
452 ParameterInteractionHint interactionHint;
Parameter & setType(ParameterValue::ParameterType t)
Parameter & setOptions(std::initializer_list< T > opts, std::initializer_list< std::string > descriptions)
Parameter & setIncrement(T t)
Parameter & setIsModified(bool mod)
bool isValidNewValue(T t) const
Parameter & setTensorFromCvMat(const cv::Mat_< T > &cvMat)
unsigned int getTensorDimension() const
std::string getCategoryName() const
Parameter & setModuleName(const std::string &n)
bool hasIncrement() const
std::string getUnit() const
std::string getDescription() const
ParameterValue getCurrentParameterValue()
double at(unsigned int x)
ParameterAccessMode getAccessForApi() const
ParameterInteractionHint getInteractionHint() const
static double anyToDouble(T val)
Converts any type to a double.
std::string getName() const
Parameter & setTensorDefaultData(const std::vector< double > &data)
Parameter & setOptions(const std::vector< T > &opts, const std::vector< std::string > &descriptions)
Parameter & setDescription(const std::string &d)
Parameter & setDefault(T t)
double at(unsigned int z, unsigned int y, unsigned int x)
double at(unsigned int y, unsigned int x)
std::vector< std::string > getOptionDescriptions() const
std::string getUid() const
Parameter & setInteractionHint(ParameterInteractionHint hint)
Parameter & setAccessForConfig(ParameterAccessMode mode)
Parameter & setAccessForApi(ParameterAccessMode mode)
std::string getModuleName() const
Parameter & setCurrent(T t)
Parameter & setGovernor(GovernorType govType, const std::string &govStr)
std::vector< unsigned int > getTensorShape() const
ParameterValue::ParameterType getType() const
std::string getGovernorString() const
GovernorType getGovernorType() const
bool getIsModified() const
Parameter & setAsTensor(const std::vector< unsigned int > &shape)
static std::string anyToString(T val)
Converts any type to a string.
void setCvSizeFromTensor(cv::Size &cvSize)
Parameter & setUnit(const std::string &d)
void setCvMatFromTensor(cv::Mat_< T > &cvMat)
Parameter & setTensorData(const std::vector< double > &data)
Parameter & setRange(T mn, T mx)
unsigned int getTensorNumElements() const
std::vector< T > getOptions() const
bool getInvokeGovernorOnInit() const
Parameter & setTensorFromCvSize(const cv::Size &cvSize)
ParameterAccessMode getAccessForConfig() const
Parameter & setName(const std::string &name)
Parameter & setCategoryName(const std::string &n)
Parameter & setInvokeGovernorOnInit(bool invoke)