gVirtualXRay  2.0.10
VirtualX-RayImagingLibraryonGPU
Vec3.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 
65 //******************************************************************************
66 // Include
67 //******************************************************************************
68 #include <cmath>
69 
70 #ifndef __OutOfBoundsException_h
72 #endif
73 
74 
75 //******************************************************************************
76 // namespace
77 //******************************************************************************
78 namespace gVirtualXRay {
79 
80 
81 //------------------------------------------------------------------------
82 template<typename T> Vec3<T>::Vec3(const T& aX, const T& aY, const T& aZ):
83 //------------------------------------------------------------------------
84  m_x(aX), m_y(aY), m_z(aZ)
85 //------------------------------------------------------------------------
86 {
87 }
88 
89 
90 //------------------------------------------------------
91 template<typename T> void Vec3<T>::setX(const T& aValue)
92 //------------------------------------------------------
93 {
94  m_x = aValue;
95 }
96 
97 
98 //------------------------------------------------------
99 template<typename T> void Vec3<T>::setY(const T& aValue)
100 //------------------------------------------------------
101 {
102  m_y = aValue;
103 }
104 
105 
106 //------------------------------------------------------
107 template<typename T> void Vec3<T>::setZ(const T& aValue)
108 //------------------------------------------------------
109 {
110  m_z = aValue;
111 }
112 
113 
114 //------------------------------------------
115 template<typename T> T Vec3<T>::getX() const
116 //------------------------------------------
117 {
118  return (m_x);
119 }
120 
121 
122 //------------------------------------------
123 template<typename T> T Vec3<T>::getY() const
124 //------------------------------------------
125 {
126  return (m_y);
127 }
128 
129 
130 //------------------------------------------
131 template<typename T> T Vec3<T>::getZ() const
132 //------------------------------------------
133 {
134  return (m_z);
135 }
136 
137 
138 //-------------------------------------------------
139 template<typename T> double Vec3<T>::length() const
140 //-------------------------------------------------
141 {
142  return (sqrt(m_x * m_x + m_y * m_y + m_z * m_z));
143 }
144 
145 
146 //--------------------------------------------------
147 template<typename T> Vec3<T> Vec3<T>::normal() const
148 //--------------------------------------------------
149 {
150  return (Vec3(m_x, m_y, m_z) / length());
151 }
152 
153 
154 //--------------------------------------------
155 template<typename T> void Vec3<T>::normalize()
156 //--------------------------------------------
157 {
158  double vector_length(length());
159  m_x /= vector_length;
160  m_y /= vector_length;
161  m_z /= vector_length;
162 }
163 
164 
165 //--------------------------------------------
166 template<typename T> void Vec3<T>::normalise()
167 //--------------------------------------------
168 {
169  return (normalize());
170 }
171 
172 
173 //------------------------------------------------------------------------
174 template<typename T> double Vec3<T>::dotProduct(const Vec3& aVector) const
175 //------------------------------------------------------------------------
176 {
177  return (m_x * aVector.m_x + m_y * aVector.m_y + m_z * aVector.m_z);
178 }
179 
180 
181 //------------------------------------------------------------------------------
182 template<typename T> Vec3<T> Vec3<T>::crossProduct(const Vec3<T>& aVector) const
183 //------------------------------------------------------------------------------
184 {
185  return (Vec3( m_y * aVector.m_z - m_z * aVector.m_y,
186  -(m_x * aVector.m_z - m_z * aVector.m_x),
187  m_x * aVector.m_y - m_y * aVector.m_x));
188 }
189 
190 
191 /*
192 //-----------------------------------------------------------------------
193 template<typename T> double Vec3<T>::operator*(const Vec3& aVector) const
194 //-----------------------------------------------------------------------
195 {
196  return (m_x * aVector.m_x + m_y * aVector.m_y + m_z * aVector.m_z);
197 }*/
198 
199 
200 //---------------------------------------------------------------------------------
201 template<typename T> Vec3<T> Vec3<T>::elementWiseProduct(const Vec3& aVector) const
202 //---------------------------------------------------------------------------------
203 {
204  return (Vec3(m_x * aVector.m_x, m_y * aVector.m_y, m_z * aVector.m_z));
205 }
206 
207 
208 //----------------------------------------------------------------------
209 template<typename T> double Vec3<T>::distance(const Vec3& aVector) const
210 //----------------------------------------------------------------------
211 {
212  return (sqrt(
213  pow(m_x - aVector.m_x, 2.0) +
214  pow(m_y - aVector.m_y, 2.0) +
215  pow(m_z - aVector.m_z, 2.0)
216  )
217  );
218 }
219 
220 
221 //-----------------------------------------------------------------------
222 template<typename T> Vec3<T> Vec3<T>::operator+(const Vec3& aValue) const
223 //-----------------------------------------------------------------------
224 {
225  return (Vec3(m_x + aValue.m_x, m_y + aValue.m_y, m_z + aValue.m_z));
226 }
227 
228 
229 //--------------------------------------------------------------------
230 template<typename T> Vec3<T>& Vec3<T>::operator+=(const Vec3& aValue)
231 //--------------------------------------------------------------------
232 {
233  m_x += aValue.m_x;
234  m_y += aValue.m_y;
235  m_z += aValue.m_z;
236 
237  return (*this);
238 }
239 
240 
241 //-----------------------------------------------------------------------
242 template<typename T> Vec3<T> Vec3<T>::operator-(const Vec3& aValue) const
243 //-----------------------------------------------------------------------
244 {
245  return (Vec3(m_x - aValue.m_x, m_y - aValue.m_y, m_z - aValue.m_z));
246 }
247 
248 
249 //-------------------------------------------------------------------
250 template<typename T> Vec3<T>& Vec3<T>::operator-=(const Vec3& aValue)
251 //-------------------------------------------------------------------
252 {
253  m_x -= aValue.m_x;
254  m_y -= aValue.m_y;
255  m_z -= aValue.m_z;
256 
257  return (*this);
258 }
259 
260 
261 //-------------------------------------------------------------------------
262 template<typename T> Vec3<T> Vec3<T>::operator*(const double& aValue) const
263 //-------------------------------------------------------------------------
264 {
265  return (Vec3(m_x * aValue, m_y * aValue, m_z * aValue));
266 }
267 
268 
269 //---------------------------------------------------------------------
270 template<typename T> Vec3<T>& Vec3<T>::operator*=(const double& aValue)
271 //---------------------------------------------------------------------
272 {
273  m_x *= aValue;
274  m_y *= aValue;
275  m_z *= aValue;
276 
277  return (*this);
278 }
279 
280 
281 //------------------------------------------------------------------------
282 template<typename T> Vec3<T> Vec3<T>::operator*(const float& aValue) const
283 //------------------------------------------------------------------------
284 {
285  return (Vec3(m_x * aValue, m_y * aValue, m_z * aValue));
286 }
287 
288 
289 //--------------------------------------------------------------------
290 template<typename T> Vec3<T>& Vec3<T>::operator*=(const float& aValue)
291 //--------------------------------------------------------------------
292 {
293  m_x *= aValue;
294  m_y *= aValue;
295  m_z *= aValue;
296 
297  return (*this);
298 }
299 
300 
301 //-------------------------------------------------------------------------
302 template<typename T> Vec3<T> Vec3<T>::operator/(const double& aValue) const
303 //-------------------------------------------------------------------------
304 {
305  return (Vec3(m_x / aValue, m_y / aValue, m_z / aValue));
306 }
307 
308 
309 //---------------------------------------------------------------------
310 template<typename T> Vec3<T>& Vec3<T>::operator/=(const double& aValue)
311 //---------------------------------------------------------------------
312 {
313  m_x /= aValue;
314  m_y /= aValue;
315  m_z /= aValue;
316 
317  return (*this);
318 }
319 
320 
321 //------------------------------------------------------------------------
322 template<typename T> Vec3<T> Vec3<T>::operator/(const float& aValue) const
323 //------------------------------------------------------------------------
324 {
325  return (Vec3(m_x / aValue, m_y / aValue, m_z / aValue));
326 }
327 
328 
329 //--------------------------------------------------------------------
330 template<typename T> Vec3<T>& Vec3<T>::operator/=(const float& aValue)
331 //--------------------------------------------------------------------
332 {
333  m_x /= aValue;
334  m_y /= aValue;
335  m_z /= aValue;
336 
337  return (*this);
338 }
339 
340 
341 //-----------------------------------------------------------------------
342 template<typename T> Vec3<T> Vec3<T>::operator^(const Vec3& aValue) const
343 //-----------------------------------------------------------------------
344 {
345  return (crossProduct(aValue));
346 }
347 
348 
349 //-----------------------------------------------------------
350 template<typename T> Vec3<T> Vec3<T>::operator-() const
351 //-----------------------------------------------------------
352 {
353  return (Vec3(-m_x, -m_y, -m_z));
354 }
355 
356 
357 //-------------------------------------------------------------------------
358 template<typename T> bool Vec3<T>::operator==(const Vec3<T>& aVector) const
359 //-------------------------------------------------------------------------
360 {
361  bool return_value(
362  std::abs(m_x - aVector.m_x) < 0.0000001 &&
363  std::abs(m_y - aVector.m_y) < 0.0000001 &&
364  std::abs(m_z - aVector.m_z) < 0.0000001);
365 
366  return (return_value);
367 }
368 
369 
370 //-------------------------------------------------------------------------
371 template<typename T> bool Vec3<T>::operator!=(const Vec3<T>& aVector) const
372 //-------------------------------------------------------------------------
373 {
374  bool return_value(
375  std::abs(m_x - aVector.m_x) >= 0.0000001 ||
376  std::abs(m_y - aVector.m_y) >= 0.0000001 ||
377  std::abs(m_z - aVector.m_z) >= 0.0000001);
378 
379  return (return_value);
380 }
381 
382 
383 //---------------------------------------------------------
384 template<typename T> T& Vec3<T>::operator()(unsigned int i)
385 //---------------------------------------------------------
386 {
387  if (i == 0)
388  {
389  return (m_x);
390  }
391  else if (i == 1)
392  {
393  return (m_y);
394  }
395  else if (i == 2)
396  {
397  return (m_z);
398  }
399  else
400  {
401  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
402  }
403 }
404 
405 
406 //---------------------------------------------------------------------
407 template<typename T> const T& Vec3<T>::operator()(unsigned int i) const
408 //---------------------------------------------------------------------
409 {
410  if (i == 0)
411  {
412  return (m_x);
413  }
414  else if (i == 1)
415  {
416  return (m_y);
417  }
418  else if (i == 2)
419  {
420  return (m_z);
421  }
422  else
423  {
424  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
425  }
426 }
427 
428 
429 //---------------------------------------------------------
430 template<typename T> T& Vec3<T>::operator[](unsigned int i)
431 //---------------------------------------------------------
432 {
433  if (i == 0)
434  {
435  return (m_x);
436  }
437  else if (i == 1)
438  {
439  return (m_y);
440  }
441  else if (i == 2)
442  {
443  return (m_z);
444  }
445  else
446  {
447  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
448  }
449 }
450 
451 
452 //---------------------------------------------------------------------
453 template<typename T> const T& Vec3<T>::operator[](unsigned int i) const
454 //---------------------------------------------------------------------
455 {
456  if (i == 0)
457  {
458  return (m_x);
459  }
460  else if (i == 1)
461  {
462  return (m_y);
463  }
464  else if (i == 2)
465  {
466  return (m_z);
467  }
468  else
469  {
470  throw OutOfBoundsException(__FILE__, __FUNCTION__, __LINE__);
471  }
472 }
473 
474 
475 //-------------------------------------------------------------
476 template<typename T> Vec3<T> operator*(const double& aValue,
477  const Vec3<T>& aVector)
478 //-------------------------------------------------------------
479 {
480  return Vec3<T>(aVector.getX() * aValue, aVector.getY() * aValue, aVector.getZ() * aValue);
481 }
482 
483 
484 //-------------------------------------------------------------------------
485 template<typename T> std::ostream& operator<<(std::ostream& anOutputStream,
486  const Vec3<T>& aVector)
487 //-------------------------------------------------------------------------
488 {
489  anOutputStream << aVector.getX() << ", " << aVector.getY() << ", " << aVector.getZ();
490  return (anOutputStream);
491 }
492 
493 
494 } // namespace gVirtualXRay
T m_z
the position along the z-axi
Definition: Vec3.h:433
Image< T > operator+(const T &aValue, const Image< T > &anImage)
Definition: Image.inl:5137
T m_x
the position along the x-axis
Definition: Vec3.h:431
Vec3 is a template class to handle a 3D vector.
Definition: Vec3.h:88
T getX() const
Accessor on the position along the x-axis.
Definition: Vec3.inl:115
T getZ() const
Accessor on the position along the z-axis.
Definition: Vec3.inl:131
Image< T > operator/(const T &aValue, const Image< T > &anImage)
Definition: Image.inl:5113
T m_y
the position along the y-axi
Definition: Vec3.h:432
T getY() const
Accessor on the position along the y-axis.
Definition: Vec3.inl:123
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
Image< T > abs(const Image< T > &anImage)
Definition: Image.inl:5177
Class to handle exceptions when accessing an array cell that is not accessible, i.e. out of bounds memory access.
Image< T > operator*(const T &aValue, const Image< T > &anImage)
Definition: Image.inl:5104
Vec3(const T &aX=0, const T &aY=0, const T &aZ=0)
Default Constructor.
Definition: Vec3.inl:82