iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DimsImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_GEOMETRY_DETAILS_DIMSIMPL_H
37 #define INCLUDE_NRT_CORE_GEOMETRY_DETAILS_DIMSIMPL_H
38 
42 
43 #include <boost/lexical_cast.hpp>
44 
45 #include <iostream>
46 
47 // ######################################################################
48 // Inline function implementations
49 // ######################################################################
50 template <class T> inline
52  itsWidth(0), itsHeight(0)
53 { }
54 
55 // ######################################################################
56 template <class T> inline
57 nrt::Dims<T>::Dims(T const width, T const height) :
58  itsWidth(width), itsHeight(height)
59 { }
60 
61 // ######################################################################
62 template <class T> template <class U> inline
64  itsWidth(nrt::clamped_convert<T,U>(other.width())),
65  itsHeight(nrt::clamped_convert<T,U>(other.height()))
66 { }
67 
68 // ######################################################################
69 template <class T> inline
71 { return itsWidth; }
72 
73 // ######################################################################
74 template <class T> inline
76 { return itsHeight; }
77 
78 // ######################################################################
79 template <class T> inline
81 { return itsWidth * itsHeight; }
82 
83 // ######################################################################
84 template <class T> inline
85 bool nrt::Dims<T>::empty() const
86 { return (itsHeight == 0 || itsWidth == 0); }
87 
88 // ######################################################################
89 template <class T> template <class U> inline
90 bool nrt::Dims<T>::contains(Point2D<U> const & p) const
91 {
92  return (p.x() >= 0 && nrt::clamped_convert<T>(p.x()) < itsWidth &&
93  p.y() >= 0 && nrt::clamped_convert<T>(p.y()) < itsHeight);
94 }
95 
96 // ######################################################################
97 // Free function implementations
98 // ######################################################################
99 
100 template <class T> inline
101 std::ostream & nrt::operator<<(std::ostream & out, nrt::Dims<T> const & d)
102 { return out << d.width() << "x" << d.height(); }
103 
104 // ######################################################################
105 template <class T> inline
106 std::istream & nrt::operator>>(std::istream & in, nrt::Dims<T> & d)
107 {
108  std::string stringRep; in >> stringRep;
109  std::vector<std::string> splitVec = nrt::splitString(stringRep, 'x');
110 
111  if (splitVec.size() != 2)
112  throw nrt::exception::Exception(std::string("Could Not Parse Dims From [" + stringRep + ']').c_str());
113 
114  T w = 0, h = 0;
115  try { w = boost::lexical_cast<T>(splitVec[0]); h = boost::lexical_cast<T>(splitVec[1]); }
116  catch (boost::bad_lexical_cast & e)
117  { throw nrt::exception::Exception(std::string("Could Not Parse Dims From [" + stringRep + "]: "+e.what()).c_str()); }
118 
119  d = nrt::Dims<T>(w, h);
120 
121  return in;
122 }
123 
124 // ######################################################################
125 template <class T> inline
126 void nrt::paramValToString(nrt::Dims<T> const & val, std::string & result)
127 { std::stringstream out; out << val.width() << "x" << val.height(); result = out.str(); }
128 
129 // ######################################################################
130 template <class T> inline
131 void nrt::paramStringToVal(std::string const & valstring, nrt::Dims<T> & result)
132 {
133  std::vector<std::string> splitVec = nrt::splitString(valstring, 'x');
134 
135  if (splitVec.size() != 2)
136  throw nrt::exception::Exception(std::string("Could Not Parse Dims From ["+valstring+']').c_str());
137 
138  T w = 0, h = 0;
139  try { w = boost::lexical_cast<T>(splitVec[0]); h = boost::lexical_cast<T>(splitVec[1]); }
140  catch (boost::bad_lexical_cast & e)
141  { throw nrt::exception::Exception(std::string("Could Not Parse Dims From ["+valstring+"]: " + e.what()).c_str()); }
142 
143  result = nrt::Dims<T>(w, h);
144 }
145 
146 // ######################################################################
147 // Dims<T> == Dims<T>
148 template <class T> inline
149 bool nrt::operator==(nrt::Dims<T> const & dims1, nrt::Dims<T> const & dims2)
150 { return (dims1.width() == dims2.width()) && (dims1.height() == dims2.height()); }
151 
152 // ######################################################################
153 // Dims<T> != Dims<T>
154 template <class T> inline
155 bool nrt::operator!=(nrt::Dims<T> const & dims1, nrt::Dims<T> const & dims2)
156 { return (dims1.width() != dims2.width()) || (dims1.height() != dims2.height()); }
157 
158 // ######################################################################
159 // Dims<T> + Dims<T>
160 template <class T> inline
161 nrt::Dims<T> nrt::operator+(nrt::Dims<T> const & dims1, nrt::Dims<T> const & dims2)
162 { return nrt::Dims<T>(dims1.width() + dims2.width(), dims1.height() + dims2.height()); }
163 
164 // ######################################################################
165 // Dims<T> + T
166 template <class T> inline
167 nrt::Dims<T> nrt::operator+(nrt::Dims<T> const & dims, T const scalar)
168 { return nrt::Dims<T>(dims.width() + scalar, dims.height() + scalar); }
169 
170 // ######################################################################
171 // T + Dims<T>
172 template <class T> inline
173 nrt::Dims<T> nrt::operator+(T const scalar, nrt::Dims<T> const & dims)
174 { return nrt::Dims<T>(scalar + dims.width(), scalar + dims.height()); }
175 
176 // ######################################################################
177 // Dims<T> - Dims<T>
178 template <class T> inline
179 nrt::Dims<T> nrt::operator-(nrt::Dims<T> const & dims1, nrt::Dims<T> const & dims2)
180  { return nrt::Dims<T>(dims1.width() - dims2.width(), dims1.height() - dims2.height()); }
181 
182 // ######################################################################
183 // Dims<T> - T
184 template <class T> inline
185 nrt::Dims<T> nrt::operator-(nrt::Dims<T> const & dims, T const scalar)
186 { return nrt::Dims<T>(dims.width() - scalar, dims.height() - scalar); }
187 
188 // ######################################################################
189 // T - Dims<T>
190 template <class T> inline
191 nrt::Dims<T> nrt::operator-(T const scalar, nrt::Dims<T> const & dims)
192 { return nrt::Dims<T>(scalar - dims.width(), scalar - dims.height()); }
193 
194 // ######################################################################
195 // Dims<T> / Dims<T>
196 template <class T> inline
197 nrt::Dims<T> nrt::operator/(nrt::Dims<T> const & dims1, nrt::Dims<T> const & dims2)
198 { return nrt::Dims<T>(dims1.width() / dims2.width(), dims1.height() / dims2.height()); }
199 
200 // ######################################################################
201 // Dims<T> / T
202 template <class T> inline
203 nrt::Dims<T> nrt::operator/(nrt::Dims<T> const & dims, double const scalar)
204 { return nrt::Dims<T>(nrt::clamped_convert<T>(dims.width() / scalar),
205  nrt::clamped_convert<T>(dims.height() / scalar)); }
206 
207 // ######################################################################
208 // Dims<T> * T
209 template <class T> inline
210 nrt::Dims<T> nrt::operator*(nrt::Dims<T> const & dims, double const scalar)
211 { return nrt::Dims<T>(nrt::clamped_convert<T>(dims.width() * scalar),
212  nrt::clamped_convert<T>(dims.height() * scalar)); }
213 
214 // ######################################################################
215 // T * Dims<T>
216 template <class T> inline
217 nrt::Dims<T> nrt::operator*(double const scalar, nrt::Dims<T> const & dims)
218 { return nrt::Dims<T>(nrt::clamped_convert<T>(dims.width() * scalar),
219  nrt::clamped_convert<T>(dims.height() * scalar)); }
220 
221 // ######################################################################
222 // Dims<T> += T
223 template <class T> inline
224 nrt::Dims<T> & nrt::operator+=(nrt::Dims<T> & dims, T const scalar)
225 { return dims = nrt::Dims<T>(dims.width() + scalar, dims.height() + scalar); }
226 
227 // ######################################################################
228 // Dims<T> -= T
229 template <class T> inline
230 nrt::Dims<T> & nrt::operator-=(nrt::Dims<T> & dims, T const scalar)
231 { return dims = nrt::Dims<T>(dims.width() - scalar, dims.height() - scalar); }
232 
233 // ######################################################################
234 // Dims<T> *= T
235 template <class T> inline
236 nrt::Dims<T> & nrt::operator*=(nrt::Dims<T> & dims, double const scalar)
237 { return dims = nrt::Dims<T>(nrt::clamped_convert<T>(dims.width() * scalar),
238  nrt::clamped_convert<T>(dims.height() * scalar)); }
239 
240 // ######################################################################
241 // Dims<T> /= T
242 template <class T> inline
243 nrt::Dims<T> & nrt::operator/=(nrt::Dims<T> & dims, double const scalar)
244 { return dims = nrt::Dims<T>(nrt::clamped_convert<T>(dims.width() / scalar),
245  nrt::clamped_convert<T>(dims.height() / scalar)); }
246 
247 // ######################################################################
248 // Dims<T> += Dims<T>
249 template <class T> inline
250 nrt::Dims<T> & nrt::operator+=(nrt::Dims<T> & dims1, nrt::Dims<T> const & dims2)
251 { return dims1 = nrt::Dims<T>(dims1.width() + dims2.width(), dims1.height() + dims2.height()); }
252 
253 // ######################################################################
254 // Dims<T> -= Dims<T>
255 template <class T> inline
256 nrt::Dims<T> & nrt::operator-=(nrt::Dims<T> & dims1, nrt::Dims<T> const & dims2)
257 { return dims1 = nrt::Dims<T>(dims1.width() - dims2.width(), dims1.height() - dims2.height()); }
258 
259 
260 #endif // INCLUDE_NRT_CORE_GEOMETRY_DETAILS_DIMSIMPL_H