vector.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2019 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef CARDBOARD_SDK_UTIL_VECTOR_H_
  17. #define CARDBOARD_SDK_UTIL_VECTOR_H_
  18. #include <array>
  19. namespace cardboard {
  20. // Geometric N-dimensional Vector class.
  21. template <int Dimension>
  22. class Vector {
  23. public:
  24. // The default constructor zero-initializes all elements.
  25. Vector();
  26. // Dimension-specific constructors that are passed individual element values.
  27. constexpr Vector(double e0, double e1, double e2);
  28. constexpr Vector(double e0, double e1, double e2, double e3);
  29. // Constructor for a Vector of dimension N from a Vector of dimension N-1 and
  30. // a scalar of the correct type, assuming N is at least 2.
  31. // constexpr Vector(const Vector<Dimension - 1>& v, double s);
  32. void Set(double e0, double e1, double e2); // Only when Dimension == 3.
  33. void Set(double e0, double e1, double e2,
  34. double e3); // Only when Dimension == 4.
  35. // Mutable element accessor.
  36. double& operator[](int index) {
  37. return elem_[index];
  38. }
  39. // Element accessor.
  40. double operator[](int index) const {
  41. return elem_[index];
  42. }
  43. // Returns a Vector containing all zeroes.
  44. static Vector Zero();
  45. // Self-modifying operators.
  46. void operator+=(const Vector& v) {
  47. Add(v);
  48. }
  49. void operator-=(const Vector& v) {
  50. Subtract(v);
  51. }
  52. void operator*=(double s) {
  53. Multiply(s);
  54. }
  55. void operator/=(double s) {
  56. Divide(s);
  57. }
  58. // Unary negation operator.
  59. Vector operator-() const {
  60. return Negation();
  61. }
  62. // Binary operators.
  63. friend Vector operator+(const Vector& v0, const Vector& v1) {
  64. return Sum(v0, v1);
  65. }
  66. friend Vector operator-(const Vector& v0, const Vector& v1) {
  67. return Difference(v0, v1);
  68. }
  69. friend Vector operator*(const Vector& v, double s) {
  70. return Scale(v, s);
  71. }
  72. friend Vector operator*(double s, const Vector& v) {
  73. return Scale(v, s);
  74. }
  75. friend Vector operator*(const Vector& v, const Vector& s) {
  76. return Product(v, s);
  77. }
  78. friend Vector operator/(const Vector& v, double s) {
  79. return Divide(v, s);
  80. }
  81. // Self-modifying addition.
  82. void Add(const Vector& v);
  83. // Self-modifying subtraction.
  84. void Subtract(const Vector& v);
  85. // Self-modifying multiplication by a scalar.
  86. void Multiply(double s);
  87. // Self-modifying division by a scalar.
  88. void Divide(double s);
  89. // Unary negation.
  90. Vector Negation() const;
  91. // Binary component-wise multiplication.
  92. static Vector Product(const Vector& v0, const Vector& v1);
  93. // Binary component-wise addition.
  94. static Vector Sum(const Vector& v0, const Vector& v1);
  95. // Binary component-wise subtraction.
  96. static Vector Difference(const Vector& v0, const Vector& v1);
  97. // Binary multiplication by a scalar.
  98. static Vector Scale(const Vector& v, double s);
  99. // Binary division by a scalar.
  100. static Vector Divide(const Vector& v, double s);
  101. private:
  102. std::array<double, Dimension> elem_;
  103. };
  104. //------------------------------------------------------------------------------
  105. template <int Dimension>
  106. Vector<Dimension>::Vector() {
  107. for(int i = 0; i < Dimension; i++) {
  108. elem_[i] = 0;
  109. }
  110. }
  111. template <int Dimension>
  112. constexpr Vector<Dimension>::Vector(double e0, double e1, double e2)
  113. : elem_{e0, e1, e2} {
  114. }
  115. template <int Dimension>
  116. constexpr Vector<Dimension>::Vector(double e0, double e1, double e2, double e3)
  117. : elem_{e0, e1, e2, e3} {
  118. }
  119. /*
  120. template <>
  121. constexpr Vector<4>::Vector(const Vector<3>& v, double s)
  122. : elem_{v[0], v[1], v[2], s} {}
  123. */
  124. template <int Dimension>
  125. void Vector<Dimension>::Set(double e0, double e1, double e2) {
  126. elem_[0] = e0;
  127. elem_[1] = e1;
  128. elem_[2] = e2;
  129. }
  130. template <int Dimension>
  131. void Vector<Dimension>::Set(double e0, double e1, double e2, double e3) {
  132. elem_[0] = e0;
  133. elem_[1] = e1;
  134. elem_[2] = e2;
  135. elem_[3] = e3;
  136. }
  137. template <int Dimension>
  138. Vector<Dimension> Vector<Dimension>::Zero() {
  139. Vector<Dimension> v;
  140. return v;
  141. }
  142. template <int Dimension>
  143. void Vector<Dimension>::Add(const Vector& v) {
  144. for(int i = 0; i < Dimension; i++) {
  145. elem_[i] += v[i];
  146. }
  147. }
  148. template <int Dimension>
  149. void Vector<Dimension>::Subtract(const Vector& v) {
  150. for(int i = 0; i < Dimension; i++) {
  151. elem_[i] -= v[i];
  152. }
  153. }
  154. template <int Dimension>
  155. void Vector<Dimension>::Multiply(double s) {
  156. for(int i = 0; i < Dimension; i++) {
  157. elem_[i] *= s;
  158. }
  159. }
  160. template <int Dimension>
  161. void Vector<Dimension>::Divide(double s) {
  162. for(int i = 0; i < Dimension; i++) {
  163. elem_[i] /= s;
  164. }
  165. }
  166. template <int Dimension>
  167. Vector<Dimension> Vector<Dimension>::Negation() const {
  168. Vector<Dimension> ret;
  169. for(int i = 0; i < Dimension; i++) {
  170. ret.elem_[i] = -elem_[i];
  171. }
  172. return ret;
  173. }
  174. template <int Dimension>
  175. Vector<Dimension> Vector<Dimension>::Product(const Vector& v0, const Vector& v1) {
  176. Vector<Dimension> ret;
  177. for(int i = 0; i < Dimension; i++) {
  178. ret.elem_[i] = v0[i] * v1[i];
  179. }
  180. return ret;
  181. }
  182. template <int Dimension>
  183. Vector<Dimension> Vector<Dimension>::Sum(const Vector& v0, const Vector& v1) {
  184. Vector<Dimension> ret;
  185. for(int i = 0; i < Dimension; i++) {
  186. ret.elem_[i] = v0[i] + v1[i];
  187. }
  188. return ret;
  189. }
  190. template <int Dimension>
  191. Vector<Dimension> Vector<Dimension>::Difference(const Vector& v0, const Vector& v1) {
  192. Vector<Dimension> ret;
  193. for(int i = 0; i < Dimension; i++) {
  194. ret.elem_[i] = v0[i] - v1[i];
  195. }
  196. return ret;
  197. }
  198. template <int Dimension>
  199. Vector<Dimension> Vector<Dimension>::Scale(const Vector& v, double s) {
  200. Vector<Dimension> ret;
  201. for(int i = 0; i < Dimension; i++) {
  202. ret.elem_[i] = v[i] * s;
  203. }
  204. return ret;
  205. }
  206. template <int Dimension>
  207. Vector<Dimension> Vector<Dimension>::Divide(const Vector& v, double s) {
  208. Vector<Dimension> ret;
  209. for(int i = 0; i < Dimension; i++) {
  210. ret.elem_[i] = v[i] / s;
  211. }
  212. return ret;
  213. }
  214. typedef Vector<3> Vector3;
  215. typedef Vector<4> Vector4;
  216. } // namespace cardboard
  217. #endif // CARDBOARD_SDK_UTIL_VECTOR_H_