JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
RangeImpl.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 <class T> inline
60  itsMin(0), itsMax(0)
61 { }
62 
63 // ######################################################################
64 template <class T> inline
65 jevois::Range<T>::Range(T const mini, T const maxi) :
66  itsMin(mini), itsMax(maxi)
67 {
68  if (itsMax < itsMin) std::swap(itsMin, itsMax);
69 }
70 
71 // ######################################################################
72 template <class T> template <class U> inline
74  itsMin(jevois::clamped_convert<T, U>(other.min())),
75  itsMax(jevois::clamped_convert<T, U>(other.max()))
76 { }
77 
78 // ######################################################################
79 template <class T> inline
80 T const & jevois::Range<T>::min() const
81 { return itsMin; }
82 
83 // ######################################################################
84 template <class T> inline
85 T const & jevois::Range<T>::max() const
86 { return itsMax; }
87 
88 // ######################################################################
89 template <class T> inline
91 { return (itsMax == itsMin); }
92 
93 
94 // ######################################################################
95 template <class T> inline
96 void jevois::Range<T>::extend(T const val)
97 {
98  if (itsMax < val) itsMax = val;
99  if (val < itsMin) itsMin = val;
100 }
101 
102 // ######################################################################
103 template <class T> inline
104 bool jevois::Range<T>::contains(T const & val) const
105 { return (val >= itsMin && val <= itsMax); }
106 
107 // ######################################################################
108 // Free function implementations
109 // ######################################################################
110 template <class T> inline
111 jevois::Range<T> jevois::merge(jevois::Range<T> const & r1, jevois::Range<T> const & r2)
112 {
113  return jevois::Range<T>(std::min(r1.min(), r2.min()), std::max(r1.max(), r2.max()));
114 }
115 
116 // ######################################################################
117 template <class T> inline
118 std::ostream & jevois::operator<<(std::ostream & out, jevois::Range<T> const & r)
119 { return out << '[' << r.min() << " ... " << r.max() << ']'; }
120 
121 // ######################################################################
122 template <class T> inline
123 std::istream & jevois::operator>>(std::istream & in, jevois::Range<T> & r)
124 {
125  char c; do { in >> c; } while (std::isspace(c));
126  if (c != '[') throw std::range_error("Error parsing Range: missing '['");
127  T mi; in >> mi; if (in.fail()) throw std::range_error("Error parsing Range: bad min");
128  do { in >> c; } while (std::isspace(c));
129  for (int i = 0; i < 3; ++i)
130  {
131  if (c != '.') throw std::range_error("Error parsing Range: missing '...'");
132  in >> c;
133  }
134  T ma; in >> ma; if (in.fail()) throw std::range_error("Error parsing Range: bad max");
135  do { in >> c; } while (std::isspace(c));
136  if (c != ']') throw std::range_error("Error parsing Range: missing ']'");
137 
138  r = jevois::Range<T>(mi, ma);
139 
140  return in;
141 }
142 
143 // ######################################################################
144 template <class T> inline
145 void jevois::paramValToString(jevois::Range<T> const & val, std::string & result)
146 {
147  // Use paramValToString on T so we correctly handle unsigned char:
148  std::string mi; jevois::paramValToString(val.min(), mi);
149  std::string ma; jevois::paramValToString(val.max(), ma);
150  result = mi + "..." + ma;
151 }
152 
153 // ######################################################################
154 template <class T> inline
155 void jevois::paramStringToVal(std::string const & valstring, jevois::Range<T> & result)
156 {
157  std::vector<std::string> splitVec = jevois::split(valstring, "\\.\\.\\.");
158 
159  if (splitVec.size() != 2) throw std::range_error("Error parsing Range from [" + valstring + "]: missing '...'");
160 
161  T mi = 0, ma = 0;
162  try { jevois::paramStringToVal<T>(splitVec[0], mi); }
163  catch (...) { throw std::range_error("Error parsing Range from [" + valstring + "]: invalid min value"); }
164 
165  try { jevois::paramStringToVal<T>(splitVec[1], ma); }
166  catch (...) { throw std::range_error("Error parsing Range from [" + valstring + "]: invalid max value"); }
167 
168  result = jevois::Range<T>(mi, ma);
169 }
170 
171 // ######################################################################
172 template <class T> inline
173 bool jevois::operator==(jevois::Range<T> const & range1, jevois::Range<T> const & range2)
174 { return (range1.min() == range2.min()) && (range1.max() == range2.max()); }
175 
176 // ######################################################################
177 template <class T> inline
178 bool jevois::operator!=(jevois::Range<T> const & range1, jevois::Range<T> const & range2)
179 { return (range1.min() != range2.min()) || (range1.max() != range2.max()); }
180 
181 // ######################################################################
182 template <class T> inline
183 jevois::Range<T> jevois::operator+(jevois::Range<T> const & range, T const & scalar)
184 { return jevois::Range<T>(range.min() + scalar, range.max() + scalar); }
185 
186 // ######################################################################
187 template <class T> inline
188 jevois::Range<T> jevois::operator+(T const & scalar, jevois::Range<T> const & range)
189 { return jevois::Range<T>(scalar + range.min(), scalar + range.max()); }
190 
191 // ######################################################################
192 template <class T> inline
193 jevois::Range<T> jevois::operator-(jevois::Range<T> const & range, T const & scalar)
194 { return jevois::Range<T>(range.min() - scalar, range.max() - scalar); }
195 
196 // ######################################################################
197 template <class T> inline
198 jevois::Range<T> jevois::operator-(T const & scalar, jevois::Range<T> const & range)
199 { return jevois::Range<T>(scalar - range.min(), scalar - range.max()); }
200 
201 // ######################################################################
202 template <class T> inline
203 jevois::Range<T> jevois::operator/(jevois::Range<T> const & range, T const & scalar)
204 { return jevois::Range<T>(range.min() / scalar, range.max() / scalar); }
205 
206 // ######################################################################
207 template <class T> inline
208 jevois::Range<T> jevois::operator/(T const & scalar, jevois::Range<T> const & range)
209 { return jevois::Range<T>(scalar / range.min(), scalar / range.max()); }
210 
211 // ######################################################################
212 template <class T> inline
213 jevois::Range<T> jevois::operator*(jevois::Range<T> const & range, T const & scalar)
214 { return jevois::Range<T>(range.min() * scalar, range.max() * scalar); }
215 
216 // ######################################################################
217 template <class T> inline
218 jevois::Range<T> jevois::operator*(T const & scalar, jevois::Range<T> const & range)
219 { return jevois::Range<T>(scalar * range.min(), scalar * range.max()); }
220 
221 // ######################################################################
222 template <class T> inline
223 jevois::Range<T> & jevois::operator+=(jevois::Range<T> & range, T const & scalar)
224 { return range = jevois::Range<T>(range.min() + scalar, range.max() + scalar); }
225 
226 // ######################################################################
227 template <class T> inline
228 jevois::Range<T> & jevois::operator-=(jevois::Range<T> & range, T const & scalar)
229 { return range = jevois::Range<T>(range.min() - scalar, range.max() - scalar); }
230 
231 // ######################################################################
232 template <class T> inline
233 jevois::Range<T> & jevois::operator*=(jevois::Range<T> & range, T const & scalar)
234 { return range = jevois::Range<T>(range.min() * scalar, range.max() * scalar); }
235 
236 // ######################################################################
237 template <class T> inline
238 jevois::Range<T> & jevois::operator/=(jevois::Range<T> & range, T const & scalar)
239 { return range = jevois::Range<T>(range.min() / scalar, range.max() / scalar); }
240 
void paramStringToVal(std::string const &valstring, T &result)
Machine-readable conversion from string to T, for use in jevois::Parameter.
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:63
bool empty() const
Return whether min() == max()
Definition: RangeImpl.H:90
T const & max() const
Return the maximum value.
Definition: RangeImpl.H:85
void paramValToString(T const &val, std::string &result)
Machine-readable conversion of T to a string, for use in jevois::Parameter.
bool contains(T const &val) const
Return true if val is within [min ... max].
Definition: RangeImpl.H:104
Range()
Default constructor, range is [0 .. 0].
Definition: RangeImpl.H:59
A generic range class.
Definition: Range.H:80
void extend(T const val)
Extend the range, if needed, so that it includes val.
Definition: RangeImpl.H:96
T const & min() const
Return the minimum value.
Definition: RangeImpl.H:80