o3dgcVector.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #ifndef O3DGC_VECTOR_H
  21. #define O3DGC_VECTOR_H
  22. #include "o3dgcCommon.h"
  23. namespace o3dgc
  24. {
  25. const unsigned long O3DGC_DEFAULT_VECTOR_SIZE = 32;
  26. //!
  27. template < typename T > class Vector
  28. {
  29. public:
  30. //! Constructor.
  31. Vector()
  32. {
  33. m_allocated = 0;
  34. m_size = 0;
  35. m_buffer = 0;
  36. };
  37. //! Destructor.
  38. ~Vector(void)
  39. {
  40. delete [] m_buffer;
  41. };
  42. T & operator[](unsigned long i)
  43. {
  44. return m_buffer[i];
  45. }
  46. const T & operator[](unsigned long i) const
  47. {
  48. return m_buffer[i];
  49. }
  50. void Allocate(unsigned long size)
  51. {
  52. if (size > m_allocated)
  53. {
  54. m_allocated = size;
  55. T * tmp = new T [m_allocated];
  56. if (m_size > 0)
  57. {
  58. memcpy(tmp, m_buffer, m_size * sizeof(T) );
  59. delete [] m_buffer;
  60. }
  61. m_buffer = tmp;
  62. }
  63. };
  64. void PushBack(const T & value)
  65. {
  66. if (m_size == m_allocated)
  67. {
  68. m_allocated *= 2;
  69. if (m_allocated < O3DGC_DEFAULT_VECTOR_SIZE)
  70. {
  71. m_allocated = O3DGC_DEFAULT_VECTOR_SIZE;
  72. }
  73. T * tmp = new T [m_allocated];
  74. if (m_size > 0)
  75. {
  76. memcpy(tmp, m_buffer, m_size * sizeof(T) );
  77. delete [] m_buffer;
  78. }
  79. m_buffer = tmp;
  80. }
  81. assert(m_size < m_allocated);
  82. m_buffer[m_size++] = value;
  83. }
  84. const T * GetBuffer() const { return m_buffer;};
  85. T * GetBuffer() { return m_buffer;};
  86. unsigned long GetSize() const { return m_size;};
  87. void SetSize(unsigned long size)
  88. {
  89. assert(size <= m_allocated);
  90. m_size = size;
  91. };
  92. unsigned long GetAllocatedSize() const { return m_allocated;};
  93. void Clear(){ m_size = 0;};
  94. private:
  95. T * m_buffer;
  96. unsigned long m_allocated;
  97. unsigned long m_size;
  98. };
  99. //! Vector dim 3.
  100. template < typename T > class Vec3
  101. {
  102. public:
  103. T & operator[](unsigned long i) { return m_data[i];}
  104. const T & operator[](unsigned long i) const { return m_data[i];}
  105. T & X();
  106. T & Y();
  107. T & Z();
  108. const T & X() const;
  109. const T & Y() const;
  110. const T & Z() const;
  111. double GetNorm() const;
  112. void operator= (const Vec3 & rhs);
  113. void operator+=(const Vec3 & rhs);
  114. void operator-=(const Vec3 & rhs);
  115. void operator-=(T a);
  116. void operator+=(T a);
  117. void operator/=(T a);
  118. void operator*=(T a);
  119. Vec3 operator^ (const Vec3 & rhs) const;
  120. T operator* (const Vec3 & rhs) const;
  121. Vec3 operator+ (const Vec3 & rhs) const;
  122. Vec3 operator- (const Vec3 & rhs) const;
  123. Vec3 operator- () const;
  124. Vec3 operator* (T rhs) const;
  125. Vec3 operator/ (T rhs) const;
  126. Vec3();
  127. Vec3(T a);
  128. Vec3(T x, T y, T z);
  129. Vec3(const Vec3 & rhs);
  130. ~Vec3(void);
  131. private:
  132. T m_data[3];
  133. };
  134. //! Vector dim 2.
  135. template < typename T > class Vec2
  136. {
  137. public:
  138. T & operator[](unsigned long i) { return m_data[i];}
  139. const T & operator[](unsigned long i) const { return m_data[i];}
  140. T & X();
  141. T & Y();
  142. const T & X() const;
  143. const T & Y() const;
  144. double GetNorm() const;
  145. void operator= (const Vec2 & rhs);
  146. void operator+=(const Vec2 & rhs);
  147. void operator-=(const Vec2 & rhs);
  148. void operator-=(T a);
  149. void operator+=(T a);
  150. void operator/=(T a);
  151. void operator*=(T a);
  152. T operator^ (const Vec2 & rhs) const;
  153. T operator* (const Vec2 & rhs) const;
  154. Vec2 operator+ (const Vec2 & rhs) const;
  155. Vec2 operator- (const Vec2 & rhs) const;
  156. Vec2 operator- () const;
  157. Vec2 operator* (T rhs) const;
  158. Vec2 operator/ (T rhs) const;
  159. Vec2();
  160. Vec2(T a);
  161. Vec2(T x, T y);
  162. Vec2(const Vec2 & rhs);
  163. ~Vec2(void);
  164. private:
  165. T m_data[2];
  166. };
  167. }
  168. #include "o3dgcVector.inl" // template implementation
  169. #endif // O3DGC_VECTOR_H