23 #include <visiontransfer/parameter.h>
25 namespace visiontransfer {
28 using namespace internal;
31 : uid(uid), name(uid), governorType(GOVERNOR_NONE), invokeGovernorOnInit(false), accessForConfig(ACCESS_NONE), accessForApi(ACCESS_NONE), interactionHint(INTERACTION_ACTIVE), isModified(false) {
35 std::string result = governorString;
36 std::set<char> subst{
'P',
'O',
'N',
'E',
'D'};
37 std::ostringstream ss;
42 int foundRightmost = -1;
44 for (
auto ch: subst) {
45 std::string lookfor =
"%";
47 foundAt = result.rfind(lookfor, where);
48 if (foundAt > foundRightmost) {
49 foundRightmost = foundAt;
53 if (foundRightmost >= 0) {
57 ss <<
"\"" <<
getUid() <<
"\"";
61 ss <<
"\"" << getCurrent<std::string>() <<
"\"";
65 if (first) first =
false;
81 ss << newVal.getTensorDimension();
83 auto sh = newVal.getTensorShape();
91 if (newVal.isScalar()) {
92 ss <<
"\"" << newVal.getValue<std::string>() <<
"\"";
95 for (
auto e: newVal.getTensorData()) {
96 if (first) first =
false;
103 result.replace(foundRightmost, 2, ss.str());
105 if (where == 0)
break;
106 where = (foundRightmost > 0) ? foundRightmost-1 : 0;
113 if(currentValue.getTensorShape() != from.getTensorShape()) {
114 throw std::runtime_error(
"Cannot assign tensors with unequal shape");
118 currentValue.setType(
getType());
121 case ParameterValue::ParameterType::TYPE_INT:
122 currentValue.setValue(from.getCurrent<
int>());
break;
123 case ParameterValue::ParameterType::TYPE_DOUBLE:
124 currentValue.setValue(from.getCurrent<
double>());
break;
125 case ParameterValue::ParameterType::TYPE_STRING:
126 case ParameterValue::ParameterType::TYPE_SAFESTRING:
127 case ParameterValue::ParameterType::TYPE_COMMAND:
128 currentValue.setValue(from.getCurrent<std::string>());
break;
130 case ParameterValue::ParameterType::TYPE_BOOL:
131 currentValue.setValue(from.getCurrent<
bool>());
break;
133 case ParameterValue::ParameterType::TYPE_TENSOR:
135 case ParameterValue::ParameterType::TYPE_UNDEFINED:
136 throw std::runtime_error(
"Cannot assign a value to an undefined parameter");
145 throw std::runtime_error(std::string(
"Cannot set current value from default - no default value set for ") +
getUid());
148 case ParameterValue::ParameterType::TYPE_INT:
149 currentValue.setType(
getType());
150 currentValue.setValue(getDefault<int>());
152 case ParameterValue::ParameterType::TYPE_DOUBLE:
153 currentValue.setType(
getType());
154 currentValue.setValue(getDefault<double>());
156 case ParameterValue::ParameterType::TYPE_STRING:
157 case ParameterValue::ParameterType::TYPE_SAFESTRING:
158 currentValue.setType(
getType());
159 currentValue.setValue(getDefault<std::string>());
161 case ParameterValue::ParameterType::TYPE_BOOL:
162 currentValue.setType(
getType());
163 currentValue.setValue(getDefault<bool>());
165 case ParameterValue::ParameterType::TYPE_TENSOR:
166 if (
hasCurrent() && (currentValue.getTensorNumElements() != defaultValue.getTensorNumElements())) {
167 throw std::runtime_error(std::string(
"Mismatching current and default tensor sizes for ") +
getUid());
169 currentValue.setType(
getType());
172 case ParameterValue::ParameterType::TYPE_COMMAND:
175 case ParameterValue::ParameterType::TYPE_UNDEFINED:
176 throw std::runtime_error(
"Cannot assign a value to an undefined parameter");
188 double inc = getIncrement<double>();
190 double min = getMin<double>();
191 return (
int) (min + inc * ((int) (val-min)/inc));
193 return (
int) (inc * ((int) val/inc));
203 double inc = getIncrement<double>();
205 double min = getMin<double>();
206 return min + inc * ((int) (val-min)/inc);
208 return inc * ((int) val/inc);
218 double inc = getIncrement<double>();
220 double min = getMin<double>();
237 throw std::runtime_error(
"Tried getTensorDataReference(), but no value set and no default defined");
249 throw std::runtime_error(
"Tried getTensorData(), but no value set and no default defined");
258 throw std::runtime_error(
"Tried getTensorDefaultDataReference(), but no value set and no default defined");
266 throw std::runtime_error(
"Tried getTensorDefaultData(), but no default defined");
274 std::string val = defaultValue.getValue<std::string>();
275 for (
auto& o: validOptions) {
276 if (val == o.getValue<std::string>()) {
281 defaultValue.setValue<std::string>(validOptions[0].getValue<std::string>());
284 if ((type==ParameterValue::ParameterType::TYPE_INT) || (type==ParameterValue::ParameterType::TYPE_DOUBLE)) {
287 double minVal = minValue.getValue<
double>();
288 double maxVal = maxValue.getValue<
double>();
289 double val = defaultValue.getValue<
double>();
292 defaultValue.setValue<
double>(minVal);
294 }
else if (val > maxVal) {
295 defaultValue.setValue<
double>(maxVal);
297 }
else if (val != incVal) {
299 defaultValue.setValue<
double>(incVal);
318 std::string val = currentValue.getValue<std::string>();
319 for (
auto& o: validOptions) {
320 if (val == o.getValue<std::string>()) {
326 std::string defVal = defaultValue.getValue<std::string>();
327 for (
auto& o: validOptions) {
328 if (defVal == o.getValue<std::string>()) {
329 currentValue.setValue<std::string>(defVal);
335 currentValue.setValue<std::string>(validOptions[0].getValue<std::string>());
338 if ((type==ParameterValue::ParameterType::TYPE_INT) || (type==ParameterValue::ParameterType::TYPE_DOUBLE)) {
341 double minVal = minValue.getValue<
double>();
342 double maxVal = maxValue.getValue<
double>();
343 double val = currentValue.getValue<
double>();
346 currentValue.setValue<
double>(minVal);
348 }
else if (val > maxVal) {
349 currentValue.setValue<
double>(maxVal);
351 }
else if (val != incVal) {
353 currentValue.setValue<
double>(incVal);