gVirtualXRay  2.0.10
VirtualX-RayImagingLibraryonGPU
Vec2.inl
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2014, Dr Franck P. Vidal (franck.p.vidal@fpvidal.net),
4 http://www.fpvidal.net/
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation and/or
15 other materials provided with the distribution.
16 
17 3. Neither the name of the Bangor University nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 */
33 
34 
62 //******************************************************************************
63 // namespace
64 //******************************************************************************
65 namespace gVirtualXRay {
66 
67 
68 //-----------------------------------------------------------
69 template<typename T> Vec2<T>::Vec2(const T& aX, const T& aY):
70 //-----------------------------------------------------------
71  m_x(aX),
72  m_y(aY)
73 //-----------------------------------------------------------
74 {}
75 
76 
77 //------------------------------------------------------
78 template<typename T> void Vec2<T>::setX(const T& aValue)
79 //------------------------------------------------------
80 {
81  m_x = aValue;
82 }
83 
84 
85 //------------------------------------------------------
86 template<typename T> void Vec2<T>::setY(const T& aValue)
87 //------------------------------------------------------
88 {
89  m_y = aValue;
90 }
91 
92 
93 //-------------------------------------------
94  template<typename T> T Vec2<T>::getX() const
95 //-------------------------------------------
96 {
97  return (m_x);
98 }
99 
100 
101 //-------------------------------------------
102  template<typename T> T Vec2<T>::getY() const
103 //-------------------------------------------
104 {
105  return (m_y);
106 }
107 
108 
109 //--------------------------------------------------
110 template<typename T> double Vec2<T>::length() const
111 //--------------------------------------------------
112 {
113  return (sqrt(m_x * m_x + m_y * m_y));
114 }
115 
116 
117 //---------------------------------------------------
118 template<typename T> Vec2<T> Vec2<T>::normal() const
119 //---------------------------------------------------
120 {
121  return (Vec2(m_x, m_y) / length());
122 }
123 
124 
125 //---------------------------------------------
126 template<typename T> void Vec2<T>::normalize()
127 //---------------------------------------------
128 {
129  double vector_length(length());
130  m_x /= vector_length;
131  m_y /= vector_length;
132 }
133 
134 
135 //---------------------------------------------
136 template<typename T> void Vec2<T>::normalise()
137 //---------------------------------------------
138 {
139  return (normalize());
140 }
141 
142 
143 //------------------------------------------------------------------------
144 template<typename T> double Vec2<T>::dotProduct(const Vec2& aVector) const
145 //------------------------------------------------------------------------
146 {
147  return (m_x * aVector.m_x + m_y * aVector.m_y);
148 }
149 
150 
151 //----------------------------------------------------------------------
152 template<typename T> double Vec2<T>::distance(const Vec2& aVector) const
153 //----------------------------------------------------------------------
154 {
155  return (sqrt(
156  pow(m_x - aVector.m_x, 2.0) +
157  pow(m_y - aVector.m_y, 2.0))
158  );
159 }
160 
161 
162 
163 //-----------------------------------------------------------------------------
164 template<typename T> const Vec2<T> Vec2<T>::operator+(const Vec2& aValue) const
165 //-----------------------------------------------------------------------------
166 {
167  return (Vec2(m_x + aValue.m_x, m_y + aValue.m_y));
168 }
169 
170 
171 //--------------------------------------------------------------------
172  template<typename T> Vec2<T>& Vec2<T>::operator+=(const Vec2& aValue)
173 //--------------------------------------------------------------------
174 {
175  m_x += aValue.m_x;
176  m_y += aValue.m_y;
177 
178  return (*this);
179 }
180 
181 
182 //-----------------------------------------------------------------------------
183 template<typename T> const Vec2<T> Vec2<T>::operator-(const Vec2& aValue) const
184 //-----------------------------------------------------------------------------
185 {
186  return (Vec2(m_x - aValue.m_x, m_y - aValue.m_y));
187 }
188 
189 
190 //-------------------------------------------------------------------
191 template<typename T> Vec2<T>& Vec2<T>::operator-=(const Vec2& aValue)
192 //-------------------------------------------------------------------
193 {
194  m_x -= aValue.m_x;
195  m_y -= aValue.m_y;
196 
197  return (*this);
198 }
199 
200 
201 //-------------------------------------------------------------------------------
202 template<typename T> const Vec2<T> Vec2<T>::operator*(const double& aValue) const
203 //-------------------------------------------------------------------------------
204 {
205  return (Vec2(m_x * aValue, m_y * aValue));
206 }
207 
208 
209 //---------------------------------------------------------------------
210 template<typename T> Vec2<T>& Vec2<T>::operator*=(const double& aValue)
211 //---------------------------------------------------------------------
212 {
213  m_x *= aValue;
214  m_y *= aValue;
215 
216  return (*this);
217 }
218 
219 
220 //------------------------------------------------------------------------------
221 template<typename T> const Vec2<T> Vec2<T>::operator*(const float& aValue) const
222 //------------------------------------------------------------------------------
223 {
224  return (Vec2(m_x * aValue, m_y * aValue));
225 }
226 
227 
228 //--------------------------------------------------------------------
229 template<typename T> Vec2<T>& Vec2<T>::operator*=(const float& aValue)
230 //--------------------------------------------------------------------
231 {
232  m_x *= aValue;
233  m_y *= aValue;
234 
235  return (*this);
236 }
237 
238 
239 //-------------------------------------------------------------------------------
240 template<typename T> const Vec2<T> Vec2<T>::operator/(const double& aValue) const
241 //-------------------------------------------------------------------------------
242 {
243  return (Vec2(m_x / aValue, m_y / aValue));
244 }
245 
246 
247 //---------------------------------------------------------------------
248 template<typename T> Vec2<T>& Vec2<T>::operator/=(const double& aValue)
249 //---------------------------------------------------------------------
250 {
251  m_x /= aValue;
252  m_y /= aValue;
253 
254  return (*this);
255 }
256 
257 
258 //------------------------------------------------------------------------------
259 template<typename T> const Vec2<T> Vec2<T>::operator/(const float& aValue) const
260 //------------------------------------------------------------------------------
261 {
262  return (Vec2(m_x / aValue, m_y / aValue));
263 }
264 
265 
266 //--------------------------------------------------------------------
267 template<typename T> Vec2<T>& Vec2<T>::operator/=(const float& aValue)
268 //--------------------------------------------------------------------
269 {
270  m_x /= aValue;
271  m_y /= aValue;
272 
273  return (*this);
274 }
275 
276 
277 //-----------------------------------------------------------
278 template<typename T> const Vec2<T> Vec2<T>::operator-() const
279 //-----------------------------------------------------------
280 {
281  return (Vec2(-m_x, -m_y));
282 }
283 
284 
285 //---------------------------------------------------------
286 template<typename T> T& Vec2<T>::operator()(unsigned int i)
287 //---------------------------------------------------------
288 {
289  if (i == 0)
290  {
291  return (m_x);
292  }
293  else if (i == 1)
294  {
295  return (m_y);
296  }
297  else
298  {
299  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
300  }
301 }
302 
303 
304 //---------------------------------------------------------------------
305 template<typename T> const T& Vec2<T>::operator()(unsigned int i) const
306 //---------------------------------------------------------------------
307 {
308  if (i == 0)
309  {
310  return (m_x);
311  }
312  else if (i == 1)
313  {
314  return (m_y);
315  }
316  else
317  {
318  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
319  }
320 }
321 
322 
323 //---------------------------------------------------------
324 template<typename T> T& Vec2<T>::operator[](unsigned int i)
325 //---------------------------------------------------------
326 {
327  if (i == 0)
328  {
329  return (m_x);
330  }
331  else if (i == 1)
332  {
333  return (m_y);
334  }
335  else
336  {
337  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
338  }
339 }
340 
341 
342 //---------------------------------------------------------------------
343 template<typename T> const T& Vec2<T>::operator[](unsigned int i) const
344 //---------------------------------------------------------------------
345 {
346  if (i == 0)
347  {
348  return (m_x);
349  }
350  else if (i == 1)
351  {
352  return (m_y);
353  }
354  else
355  {
356  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
357  }
358 }
359 
360 
361 //--------------------------------------------------------------------------
362 template <typename T> std::ostream& operator<<(std::ostream& anOutputStream,
363  const Vec2<T>& aVector)
364 //--------------------------------------------------------------------------
365 {
366  anOutputStream << aVector.getX() << ", " << aVector.getY();
367  return (anOutputStream);
368 }
369 
370 
371 } // namespace gVirtualXRay
Vec2 is a template class to handle a 2D vector.
Definition: Vec2.h:83
T m_x
the position along the x-axis
Definition: Vec2.h:349
Image< T > operator+(const T &aValue, const Image< T > &anImage)
Definition: Image.inl:5137
Image< T > operator/(const T &aValue, const Image< T > &anImage)
Definition: Image.inl:5113
T getX() const
Accessor on the position along the x-axis.
Definition: Vec2.inl:94
T m_y
the position along the y-axi
Definition: Vec2.h:350
std::ostream & operator<<(std::ostream &anOutputSream, const gVirtualXRay::AtomicElement &anElement)
operator <<
Image< T > operator-(const T &aValue, const Image< T > &anImage)
Definition: Image.inl:5146
Vec2(const T &aX=0.0f, const T &aY=0.0f)
Default Constructor.
Definition: Vec2.inl:69
T getY() const
Accessor on the position along the y-axis.
Definition: Vec2.inl:102
Image< T > operator*(const T &aValue, const Image< T > &anImage)
Definition: Image.inl:5104