PolyVox  0.2.1
Open source voxel management library
Vector.inl
Go to the documentation of this file.
1 /*******************************************************************************
2 Copyright (c) 2005-2009 David Williams
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16 
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19 
20  3. This notice may not be removed or altered from any source
21  distribution.
22 *******************************************************************************/
23 
24 namespace PolyVox
25 {
26  //-------------------------- Constructors, etc ---------------------------------
32  template <uint32_t Size,typename Type>
33  Vector<Size,Type>::Vector(Type x, Type y)
34  {
35  m_tElements[0] = x;
36  m_tElements[1] = y;
37 
38  }
39 
46  template <uint32_t Size,typename Type>
47  Vector<Size,Type>::Vector(Type x, Type y, Type z)
48  {
49  m_tElements[0] = x;
50  m_tElements[1] = y;
51  m_tElements[2] = z;
52 
53  }
54 
62  template <uint32_t Size,typename Type>
63  Vector<Size,Type>::Vector(Type x, Type y, Type z, Type w)
64  {
65  m_tElements[0] = x;
66  m_tElements[1] = y;
67  m_tElements[2] = z;
68  m_tElements[3] = w;
69  }
70 
74  template <uint32_t Size, typename Type>
76  {
77  }
78 
83  template <uint32_t Size, typename Type>
85  {
86  std::memcpy(m_tElements, vector.m_tElements, sizeof(Type) * Size);
87  }
88 
98  template <uint32_t Size, typename Type>
99  template <typename CastType>
101  {
102  for(uint32_t ct = 0; ct < Size; ++ct)
103  {
104  m_tElements[ct] = static_cast<Type>(vector.getElement(ct));
105  }
106  }
107 
111  template <uint32_t Size, typename Type>
113  {
114  }
115 
121  template <uint32_t Size, typename Type>
123  {
124  if(this == &rhs)
125  {
126  return *this;
127  }
128  std::memcpy(m_tElements, rhs.m_tElements, sizeof(Type) * Size);
129  return *this;
130  }
131 
138  template <uint32_t Size, typename Type>
140  {
141  bool equal = true;
142  for(uint32_t ct = 0; ct < Size; ++ct)
143  {
144  if(m_tElements[ct] != rhs.m_tElements[ct])
145  {
146  equal = false;
147  break;
148  }
149  }
150  return equal;
151  }
152 
159  template <uint32_t Size, typename Type>
161  {
162  return !(*this == rhs); //Just call equality operator and invert the result.
163  }
164 
172  template <uint32_t Size, typename Type>
173  inline bool Vector<Size, Type>::operator<(const Vector<Size, Type> &rhs) const
174  {
175  for(uint32_t ct = 0; ct < Size; ++ct)
176  {
177  if (m_tElements[ct] < rhs.m_tElements[ct])
178  return true;
179  if (rhs.m_tElements[ct] < m_tElements[ct])
180  return false;
181  }
182  return false;
183  }
184 
190  template <uint32_t Size, typename Type>
192  {
193  for(uint32_t ct = 0; ct < Size; ++ct)
194  {
195  m_tElements[ct] += rhs.m_tElements[ct];
196  }
197  return *this;
198  }
199 
205  template <uint32_t Size, typename Type>
207  {
208  for(uint32_t ct = 0; ct < Size; ++ct)
209  {
210  m_tElements[ct] -= rhs.m_tElements[ct];
211  }
212  return *this;
213  }
214 
220  template <uint32_t Size, typename Type>
222  {
223  for(uint32_t ct = 0; ct < Size; ++ct)
224  {
225  m_tElements[ct] *= rhs.m_tElements[ct];
226  }
227  return *this;
228  }
229 
235  template <uint32_t Size, typename Type>
237  {
238  for(uint32_t ct = 0; ct < Size; ++ct)
239  {
240  m_tElements[ct] /= rhs.m_tElements[ct];
241  }
242  return *this;
243  }
244 
250  template <uint32_t Size, typename Type>
252  {
253  for(uint32_t ct = 0; ct < Size; ++ct)
254  {
255  m_tElements[ct] *= rhs;
256  }
257  return *this;
258  }
259 
265  template <uint32_t Size, typename Type>
267  {
268  for(uint32_t ct = 0; ct < Size; ++ct)
269  {
270  m_tElements[ct] /= rhs;
271  }
272  return *this;
273  }
274 
281  template <uint32_t Size,typename Type>
283  {
284  Vector<Size,Type> result = lhs;
285  result += rhs;
286  return result;
287  }
288 
295  template <uint32_t Size,typename Type>
297  {
298  Vector<Size,Type> result = lhs;
299  result -= rhs;
300  return result;
301  }
302 
309  template <uint32_t Size,typename Type>
311  {
312  Vector<Size,Type> result = lhs;
313  result *= rhs;
314  return result;
315  }
316 
323  template <uint32_t Size,typename Type>
325  {
326  Vector<Size,Type> result = lhs;
327  result /= rhs;
328  return result;
329  }
330 
337  template <uint32_t Size,typename Type>
338  Vector<Size,Type> operator*(const Vector<Size,Type>& lhs, const Type& rhs)
339  {
340  Vector<Size,Type> result = lhs;
341  result *= rhs;
342  return result;
343  }
344 
351  template <uint32_t Size,typename Type>
352  Vector<Size,Type> operator/(const Vector<Size,Type>& lhs, const Type& rhs)
353  {
354  Vector<Size,Type> result = lhs;
355  result /= rhs;
356  return result;
357  }
358 
365  template <uint32_t Size, typename Type>
366  std::ostream& operator<<(std::ostream& os, const Vector<Size, Type>& vector)
367  {
368  os << "(";
369  for(uint32_t ct = 0; ct < Size; ++ct)
370  {
371  os << vector.getElement(ct);
372  if(ct < (Size-1))
373  {
374  os << ",";
375  }
376  }
377  os << ")";
378  return os;
379  }
380 
386  template <uint32_t Size, typename Type>
387  inline Type Vector<Size, Type>::getElement(uint32_t index) const
388  {
389  return m_tElements[index];
390  }
391 
395  template <uint32_t Size, typename Type>
396  inline Type Vector<Size, Type>::getX(void) const
397  {
398  return m_tElements[0];
399  }
400 
404  template <uint32_t Size, typename Type>
405  inline Type Vector<Size, Type>::getY(void) const
406  {
407  return m_tElements[1];
408  }
409 
413  template <uint32_t Size, typename Type>
414  inline Type Vector<Size, Type>::getZ(void) const
415  {
416  return m_tElements[2];
417  }
418 
422  template <uint32_t Size, typename Type>
423  inline Type Vector<Size, Type>::getW(void) const
424  {
425  return m_tElements[3];
426  }
427 
432  template <uint32_t Size, typename Type>
433  inline void Vector<Size, Type>::setElement(uint32_t index, Type tValue)
434  {
435  m_tElements[index] = tValue;
436  }
437 
443  template <uint32_t Size,typename Type>
444  inline void Vector<Size,Type>::setElements(Type x, Type y)
445  {
446  m_tElements[0] = x;
447  m_tElements[1] = y;
448 
449  }
450 
457  template <uint32_t Size,typename Type>
458  inline void Vector<Size,Type>::setElements(Type x, Type y, Type z)
459  {
460  m_tElements[0] = x;
461  m_tElements[1] = y;
462  m_tElements[2] = z;
463 
464  }
465 
473  template <uint32_t Size,typename Type>
474  inline void Vector<Size,Type>::setElements(Type x, Type y, Type z, Type w)
475  {
476  m_tElements[0] = x;
477  m_tElements[1] = y;
478  m_tElements[2] = z;
479  m_tElements[3] = w;
480  }
481 
485  template <uint32_t Size, typename Type>
486  inline void Vector<Size, Type>::setX(Type tX)
487  {
488  m_tElements[0] = tX;
489  }
490 
494  template <uint32_t Size, typename Type>
495  inline void Vector<Size, Type>::setY(Type tY)
496  {
497  m_tElements[1] = tY;
498  }
499 
503  template <uint32_t Size, typename Type>
504  inline void Vector<Size, Type>::setZ(Type tZ)
505  {
506  m_tElements[2] = tZ;
507  }
508 
512  template <uint32_t Size, typename Type>
513  inline void Vector<Size, Type>::setW(Type tW)
514  {
515  m_tElements[3] = tW;
516  }
517 
522  template <uint32_t Size, typename Type>
523  inline double Vector<Size, Type>::length(void) const
524  {
525  return sqrt(lengthSquared());
526  }
527 
531  template <uint32_t Size, typename Type>
532  inline double Vector<Size, Type>::lengthSquared(void) const
533  {
534  double result = 0.0f;
535  for(uint32_t ct = 0; ct < Size; ++ct)
536  {
537  result += m_tElements[ct] * m_tElements[ct];
538  }
539  return result;
540  }
541 
551  template <uint32_t Size, typename Type>
552  inline double Vector<Size, Type>::angleTo(const Vector<Size, Type>& vector) const
553  {
554  return acos(dot(vector) / (vector.length() * this->length()));
555  }
556 
569  template <uint32_t Size, typename Type>
571  {
572  Type i = vector.getZ() * this->getY() - vector.getY() * this->getZ();
573  Type j = vector.getX() * this->getZ() - vector.getZ() * this->getX();
574  Type k = vector.getY() * this->getX() - vector.getX() * this->getY();
575  return Vector<Size, Type>(i,j,k);
576  }
577 
585  template <uint32_t Size, typename Type>
586  inline Type Vector<Size, Type>::dot(const Vector<Size, Type>& rhs) const
587  {
588  Type dotProduct = static_cast<Type>(0);
589  for(uint32_t ct = 0; ct < Size; ++ct)
590  {
591  dotProduct += m_tElements[ct] * rhs.m_tElements[ct];
592  }
593  return dotProduct;
594  }
595 
601  template <uint32_t Size, typename Type>
603  {
604  Type tLength = static_cast<Type>(this->length());
605  //FIXME - throw div by zero exception?
606  if(tLength < 0.0001f)
607  {
608  return;
609  }
610  for(uint32_t ct = 0; ct < Size; ++ct)
611  {
612  m_tElements[ct] /= tLength;
613  }
614  }
615 }//namespace PolyVox