JeVois  1.3
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
StepRangeImpl.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 // This file has been modified / imported from the Neuromorphic Robotics Toolkit (NRT). Original copyright is:
19 
20 /* author Laurent Itti
21  // ////////////////////////////////////////////////////////////////////////
22  // The iLab Neuromorphic Robotics Toolkit (NRT) //
23  // Copyright 2010-2012 by the University of Southern California (USC) //
24  // and the iLab at USC. //
25  // //
26  // iLab - University of Southern California //
27  // Hedco Neurociences Building, Room HNB-10 //
28  // Los Angeles, Ca 90089-2520 - USA //
29  // //
30  // See http://ilab.usc.edu for information about this project. //
31  // ////////////////////////////////////////////////////////////////////////
32  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
33  // //
34  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
35  // redistribute it and/or modify it under the terms of the GNU General //
36  // Public License as published by the Free Software Foundation, either //
37  // version 3 of the License, or (at your option) any later version. //
38  // //
39  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
40  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
41  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
42  // PURPOSE. See the GNU General Public License for more details. //
43  // //
44  // You should have received a copy of the GNU General Public License //
45  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
46  // <http://www.gnu.org/licenses/>. //
47  // ////////////////////////////////////////////////////////////////////////
48 */
49 
50 #pragma once
51 
53 #include <jevois/Util/Utils.H>
54 
55 // ######################################################################
56 // Inline function implementations
57 // ######################################################################
58 template <typename T> inline
60  itsMin(0), itsStep(0), itsMax(0)
61 { }
62 
63 // ######################################################################
64 template <typename T> inline
65 jevois::StepRange<T>::StepRange(T const mini, T const stepi, T const maxi) :
66  itsMin(mini), itsStep(stepi), itsMax(maxi)
67 {
68  if (itsStep < 0) throw std::range_error("Invalid negative step value given to StepRange (must be >= 0)");
69  if (itsMax < itsMin) std::swap(itsMin, itsMax);
70 }
71 
72 // ######################################################################
73 template <typename T> template <typename U> inline
75  itsMin(jevois::clamped_convert<T, U>(other.min())),
76  itsStep(jevois::clamped_convert<T, U>(other.step())),
77  itsMax(jevois::clamped_convert<T, U>(other.max()))
78 { }
79 
80 // ######################################################################
81 template <typename T> inline
82 T const & jevois::StepRange<T>::min() const
83 { return itsMin; }
84 
85 // ######################################################################
86 template <typename T> inline
87 T const & jevois::StepRange<T>::step() const
88 { return itsStep; }
89 
90 // ######################################################################
91 template <typename T> inline
92 T const & jevois::StepRange<T>::max() const
93 { return itsMax; }
94 
95 // ######################################################################
96 template <typename T> inline
98 { return (itsMax == itsMin); }
99 
100 
101 // ######################################################################
102 template <typename T> inline
103 bool jevois::StepRange<T>::isValueValid(T const val) const
104 {
105  if (val < itsMin || val > itsMax) return false;
106  if (val == itsMax) return true;
107  if (itsStep == 0) return true;
108 
109  double n = (static_cast<double>(val) - static_cast<double>(itsMin)) / static_cast<double>(itsStep);
110 
111  if (n - int(n) > 1.0e-10) return false;
112 
113  return true;
114 }
115 
116 // ######################################################################
117 // Free function implementations
118 // ######################################################################
119 
120 // ######################################################################
121 template <typename T> inline
122 std::ostream & jevois::operator<<(std::ostream & out, jevois::StepRange<T> const & r)
123 { return out << '[' << r.min() << " ...(" << r.step() << ")... " << r.max() << ']'; }
124 
125 // ######################################################################
126 template <typename T> inline
127 std::istream & jevois::operator>>(std::istream & in, jevois::StepRange<T> & r)
128 {
129  char c; do { in >> c; } while (std::isspace(c));
130  if (c != '[') throw std::range_error("Error parsing StepRange: missing '['");
131 
132  T mi; in >> mi; if (in.fail()) throw std::range_error("Error parsing StepRange: bad min");
133 
134  do { in >> c; } while (std::isspace(c));
135  for (int i = 0; i < 3; ++i)
136  {
137  if (c != '.') throw std::range_error("Error parsing StepRange: missing first '...'");
138  in >> c;
139  }
140 
141  if (c != '(') throw std::range_error("Error parsing StepRange: missing '('");
142  T st; in >> st; if (in.fail()) throw std::range_error("Error parsing StepRange: bad step");
143  in >> c;
144  if (c != ')') throw std::range_error("Error parsing StepRange: missing '('");
145 
146  for (int i = 0; i < 3; ++i)
147  {
148  in >> c;
149  if (c != '.') throw std::range_error("Error parsing StepRange: missing second '...'");
150  }
151 
152  T ma; in >> ma; if (in.fail()) throw std::range_error("Error parsing StepRange: bad max");
153 
154  do { in >> c; } while (std::isspace(c));
155  if (c != ']') throw std::range_error("Error parsing StepRange: missing ']'");
156 
157  r = jevois::StepRange<T>(mi, st, ma);
158 
159  return in;
160 }
161 
162 // ######################################################################
163 template <class T> inline
164 void jevois::paramValToString(jevois::StepRange<T> const & val, std::string & result)
165 {
166  // Use paramValToString on T so we correctly handle unsigned char:
167  std::string mi; jevois::paramValToString(val.min(), mi);
168  std::string st; jevois::paramValToString(val.step(), st);
169  std::string ma; jevois::paramValToString(val.max(), ma);
170  result = mi + "..." + st + "..." + ma;
171 }
172 
173 // ######################################################################
174 template <class T> inline
175 void jevois::paramStringToVal(std::string const & valstring, jevois::StepRange<T> & result)
176 {
177  std::vector<std::string> splitVec = jevois::split(valstring, "\\.\\.\\.");
178 
179  if (splitVec.size() != 3) throw std::range_error("Error parsing StepRange from [" + valstring + "]: missing '...'");
180 
181  T mi = 0, st = 0, ma = 0;
182  try { jevois::paramStringToVal<T>(splitVec[0], mi); }
183  catch (...) { throw std::range_error("Error parsing StepRange from [" + valstring + "]: invalid min value"); }
184 
185  try { jevois::paramStringToVal<T>(splitVec[1], st); }
186  catch (...) { throw std::range_error("Error parsing StepRange from [" + valstring + "]: invalid step value"); }
187 
188  try { jevois::paramStringToVal<T>(splitVec[2], ma); }
189  catch (...) { throw std::range_error("Error parsing StepRange from [" + valstring + "]: invalid max value"); }
190 
191  result = jevois::StepRange<T>(mi, st, ma);
192 }
193 
194 // ######################################################################
195 template <typename T> inline
196 bool jevois::operator==(jevois::StepRange<T> const & range1, jevois::StepRange<T> const & range2)
197 { return (range1.min() == range2.min()) && (range1.step() == range2.step()) && (range1.max() == range2.max()); }
198 
199 // ######################################################################
200 template <typename T> inline
201 bool jevois::operator!=(jevois::StepRange<T> const & range1, jevois::StepRange<T> const & range2)
202 { return (range1.min() != range2.min()) || (range1.step() != range2.step()) || (range1.max() != range2.max()); }
203 
void paramStringToVal(std::string const &valstring, T &result)
Machine-readable conversion from string to T, for use in jevois::Parameter.
A generic range class with a step.
Definition: StepRange.H:69
dest_type clamped_convert(source_type source)
Clamped numerical conversion.
Definition: UtilsImpl.H:58
std::vector< std::string > split(std::string const &input, std::string const &regex="\\s+")
Split string into vector of tokens using a regex to specify what to split on; default regex splits by...
Definition: Utils.C:113
bool empty() const
Return whether min() == max()
Definition: StepRangeImpl.H:97
void paramValToString(T const &val, std::string &result)
Machine-readable conversion of T to a string, for use in jevois::Parameter.
T const & min() const
Return the minimum value.
Definition: StepRangeImpl.H:82
T const & max() const
Return the maximum value.
Definition: StepRangeImpl.H:92
bool isValueValid(T const val) const
Return true if a value is valid (i.e., it is min, min+step, min+step*2, ... or max) ...
T const & step() const
Return the step value.
Definition: StepRangeImpl.H:87
StepRange()
Default constructor, range is [0 .. (0) .. 0].
Definition: StepRangeImpl.H:59