MathFu
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
vector_3.h
1 /*
2 * Copyright 2016 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 MATHFU_INTERNAL_VECTOR_3_H_
17 #define MATHFU_INTERNAL_VECTOR_3_H_
18 
19 #include "mathfu/vector.h"
20 
21 namespace mathfu {
22 
23 template <class T>
24 class Vector<T, 3> {
25  public:
26  typedef T Scalar;
27  static const int d = 3;
28 
29  inline Vector() {}
30 
31  inline Vector(const Vector<T, 3>& v) {
32  MATHFU_VECTOR_OPERATION(data_[i] = v.data_[i]);
33  }
34 
35  template <typename U>
36  explicit inline Vector(const Vector<U, 3>& v) {
37  MATHFU_VECTOR_OPERATION(data_[i] = static_cast<T>(v[i]));
38  }
39 
40  explicit inline Vector(const T& s) { MATHFU_VECTOR_OPERATION(data_[i] = s); }
41 
42  explicit inline Vector(const T* a) {
43  MATHFU_VECTOR_OPERATION(data_[i] = a[i]);
44  }
45 
46  inline Vector(const T& s1, const T& s2, const T& s3) {
47  x = s1;
48  y = s2;
49  z = s3;
50  }
51 
52  inline Vector(const Vector<T, 2>& v12, const T& s3) {
53  x = v12[0];
54  y = v12[1];
55  z = s3;
56  }
57 
58  explicit inline Vector(const VectorPacked<T, 3>& vector) {
59  MATHFU_VECTOR_OPERATION(data_[i] = vector.data[i]);
60  }
61 
62  inline T& operator()(const int i) { return data_[i]; }
63 
64  inline const T& operator()(const int i) const { return data_[i]; }
65 
66  inline T& operator[](const int i) { return data_[i]; }
67 
68  inline const T& operator[](const int i) const { return data_[i]; }
69 
70  inline Vector<T, 3> xyz() { return Vector<T, 3>(x, y, z); }
71 
72  inline const Vector<T, 3> xyz() const { return Vector<T, 3>(x, y, z); }
73 
74  inline Vector<T, 2> xy() { return Vector<T, 2>(x, y); }
75 
76  inline const Vector<T, 2> xy() const { return Vector<T, 2>(x, y); }
77 
78  inline void Pack(VectorPacked<T, 3>* const vector) const {
79  MATHFU_VECTOR_OPERATION(vector->data[i] = data_[i]);
80  }
81 
82  inline T LengthSquared() const { return LengthSquaredHelper(*this); }
83 
84  inline T Length() const { return LengthHelper(*this); }
85 
86  inline T Normalize() { return NormalizeHelper(*this); }
87 
88  inline Vector<T, 3> Normalized() const { return NormalizedHelper(*this); }
89 
90  template <typename CompatibleT>
91  static inline Vector<T, 3> FromType(const CompatibleT& compatible) {
92  return FromTypeHelper<T, d, CompatibleT>(compatible);
93  }
94 
95  template <typename CompatibleT>
96  static inline CompatibleT ToType(const Vector<T, 3>& v) {
97  return ToTypeHelper<T, d, CompatibleT>(v);
98  }
99 
100  static inline T DotProduct(const Vector<T, 3>& v1, const Vector<T, 3>& v2) {
101  return DotProductHelper(v1, v2);
102  }
103 
104  static inline Vector<T, 3> HadamardProduct(const Vector<T, 3>& v1,
105  const Vector<T, 3>& v2) {
106  return HadamardProductHelper(v1, v2);
107  }
108 
109  static inline Vector<T, 3> CrossProduct(const Vector<T, 3>& v1,
110  const Vector<T, 3>& v2) {
111  return CrossProductHelper(v1, v2);
112  }
113 
114  static inline Vector<T, 3> Lerp(const Vector<T, 3>& v1,
115  const Vector<T, 3>& v2, const T percent) {
116  return LerpHelper(v1, v2, percent);
117  }
118 
119  static inline Vector<T, 3> RandomInRange(const Vector<T, 3>& min,
120  const Vector<T, 3>& max) {
121  return RandomInRangeHelper(min, max);
122  }
123 
124  static inline Vector<T, 3> Max(const Vector<T, 3>& v1,
125  const Vector<T, 3>& v2) {
126  return MaxHelper(v1, v2);
127  }
128 
129  static inline Vector<T, 3> Min(const Vector<T, 3>& v1,
130  const Vector<T, 3>& v2) {
131  return MinHelper(v1, v2);
132  }
133 
135 
136 #include "mathfu/internal/disable_warnings_begin.h"
137  union {
138  T data_[3];
139  struct {
140  T x;
141  T y;
142  T z;
143  };
144  };
145 #include "mathfu/internal/disable_warnings_end.h"
146 };
147 
148 template <class T>
149 struct VectorPacked<T, 3> {
150  /// Create an uninitialized VectorPacked.
152 
153  /// Create a VectorPacked from a Vector.
154  ///
155  /// Both VectorPacked and Vector must have the same number of dimensions.
156  /// @param vector Vector to create the VectorPacked from.
157  explicit VectorPacked(const Vector<T, 3>& vector) { vector.Pack(this); }
158 
159  /// Copy a Vector to a VectorPacked.
160  ///
161  /// Both VectorPacked and Vector must have the same number of dimensions.
162  /// @param vector Vector to copy to the VectorPacked.
163  /// @returns A reference to this VectorPacked.
165  vector.Pack(this);
166  return *this;
167  }
168 
169 #include "mathfu/internal/disable_warnings_begin.h"
170  /// Elements of the packed vector one per dimension.
171  union {
172  T data[3];
173  struct {
174  T x;
175  T y;
176  T z;
177  };
178  };
179 #include "mathfu/internal/disable_warnings_end.h"
180 };
181 
182 } // namespace mathfu
183 
184 #endif // MATHFU_INTERNAL_VECTOR_3_H_
Definition: vector_3.h:24
T LengthHelper(const Vector< T, d > &v)
Calculate the length of a vector.
Definition: vector.h:787
VectorPacked & operator=(const Vector< T, 3 > &vector)
Definition: vector_3.h:164
T LengthSquared() const
Calculate the squared length of this vector.
Definition: vector.h:380
Definition: vector_3.h:149
Vector of d elements with type T.
Definition: vector.h:63
Definition: vector_2.h:24
static Vector< T, d > Min(const Vector< T, d > &v1, const Vector< T, d > &v2)
Compare each component and returns min values.
Definition: vector.h:487
T & operator[](const int i)
Access an element of the vector.
Definition: vector.h:306
T Length() const
Calculate the length of this vector.
Definition: vector.h:385
#define MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
Macro which defines the new and delete for MathFu classes.
Definition: utilities.h:600
Vector< T, d > NormalizedHelper(const Vector< T, d > &v)
Calculate the normalized version of a vector.
Definition: vector.h:807
static Vector< T, d > Max(const Vector< T, d > &v1, const Vector< T, d > &v2)
Compare each component and returns max values.
Definition: vector.h:477
MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE T data_[d]
Elements of the vector.
Definition: vector.h:495
Vector< T, d > MinHelper(const Vector< T, d > &v1, const Vector< T, d > &v2)
Compare each component and returns min values.
Definition: vector.h:855
T Normalize()
Normalize this vector in-place.
Definition: vector.h:390
static Vector< T, d > HadamardProduct(const Vector< T, d > &v1, const Vector< T, d > &v2)
Calculate the hadamard or componentwise product of two vectors.
Definition: vector.h:435
static CompatibleT ToType(const Vector< T, d > &v)
Load into any type that is some formulation of a length d array of type T.
Definition: vector.h:417
Vector< T, 3 > CrossProductHelper(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
Calculate the cross product of two vectors.
Definition: vector.h:766
T LengthSquaredHelper(const Vector< T, d > &v)
Calculate the squared length of a vector.
Definition: vector.h:778
Vector< T, d > HadamardProductHelper(const Vector< T, d > &v1, const Vector< T, d > &v2)
Calculate the hadamard or componentwise product of two vectors.
Definition: vector.h:754
Vector< T, 2 > xy()
GLSL style 2 element accessor.
Definition: vector.h:338
T data[d]
Elements of the packed vector one per dimension.
Definition: vector.h:142
Vector< T, d > Normalized() const
Calculate the normalized version of this vector.
Definition: vector.h:395
static T DotProduct(const Vector< T, d > &v1, const Vector< T, d > &v2)
Calculate the dot product of two vectors.
Definition: vector.h:426
Vector< T, d > RandomInRangeHelper(const Vector< T, d > &min, const Vector< T, d > &max)
Generates a random vector.
Definition: vector.h:830
Packed N-dimensional vector.
Definition: vector.h:121
void Pack(VectorPacked< T, d > *const vector) const
Pack a Vector to a packed "d" element vector structure.
Definition: vector.h:373
Vector class and functions.
static Vector< T, d > RandomInRange(const Vector< T, d > &min, const Vector< T, d > &max)
Generates a random vector.
Definition: vector.h:467
VectorPacked(const Vector< T, 3 > &vector)
Definition: vector_3.h:157
T & operator()(const int i)
Access an element of the vector.
Definition: vector.h:293
static Vector< T, d > Lerp(const Vector< T, d > &v1, const Vector< T, d > &v2, const T percent)
Linearly interpolate two vectors.
Definition: vector.h:457
T NormalizeHelper(Vector< T, d > &v)
Normalize a vector in-place.
Definition: vector.h:796
static Vector< T, 3 > CrossProduct(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
Calculate the cross product of two vectors.
Definition: vector.h:446
Vector()
Create an uninitialized Vector.
Definition: vector.h:163
Vector< T, d > MaxHelper(const Vector< T, d > &v1, const Vector< T, d > &v2)
Compare each component and returns max values.
Definition: vector.h:843
VectorPacked()
Create an uninitialized VectorPacked.
Definition: vector_3.h:151
Vector< T, 3 > xyz()
GLSL style 3 element accessor.
Definition: vector.h:319
static Vector< T, d > FromType(const CompatibleT &compatible)
Load from any type that is some formulation of a length d array of type T.
Definition: vector.h:405
Vector< T, d > LerpHelper(const Vector< T, d > &v1, const Vector< T, d > &v2, const T percent)
Linearly interpolate two vectors.
Definition: vector.h:818
T Scalar
Element type to enable reference by other classes.
Definition: vector.h:160