MathFu
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
vector_4.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_4_H_
17 #define MATHFU_INTERNAL_VECTOR_4_H_
18 
19 // Prefer including vector.h directly, since it includes specializations.
20 #include "mathfu/vector.h"
21 
22 namespace mathfu {
23 
24 template <class T>
25 class Vector<T, 4> {
26  public:
27  typedef T Scalar;
28  static const int d = 4;
29 
30  inline Vector() {}
31 
32  inline Vector(const Vector<T, 4>& v) {
33  MATHFU_VECTOR_OPERATION(data_[i] = v.data_[i]);
34  }
35 
36  template <typename U>
37  explicit inline Vector(const Vector<U, 4>& v) {
38  MATHFU_VECTOR_OPERATION(data_[i] = static_cast<T>(v[i]));
39  }
40 
41  explicit inline Vector(const T& s) { MATHFU_VECTOR_OPERATION(data_[i] = s); }
42 
43  explicit inline Vector(const T* a) {
44  MATHFU_VECTOR_OPERATION(data_[i] = a[i]);
45  }
46 
47  inline Vector(const T& s1, const T& s2, const T& s3, const T& s4) {
48  x = s1;
49  y = s2;
50  z = s3;
51  w = s4;
52  }
53 
54  inline Vector(const Vector<T, 3>& vector3, const T& value) {
55  x = vector3[0];
56  y = vector3[1];
57  z = vector3[2];
58  w = value;
59  }
60 
61  inline Vector(const Vector<T, 2>& v12, const Vector<T, 2>& v34) {
62  x = v12[0];
63  y = v12[1];
64  z = v34[0];
65  w = v34[1];
66  }
67 
68  explicit inline Vector(const VectorPacked<T, 4>& vector) {
69  MATHFU_VECTOR_OPERATION(data_[i] = vector.data[i]);
70  }
71 
72  inline T& operator()(const int i) { return data_[i]; }
73 
74  inline const T& operator()(const int i) const { return data_[i]; }
75 
76  inline T& operator[](const int i) { return data_[i]; }
77 
78  inline const T& operator[](const int i) const { return data_[i]; }
79 
80  inline Vector<T, 3> xyz() { return Vector<T, 3>(x, y, z); }
81 
82  inline const Vector<T, 3> xyz() const { return Vector<T, 3>(x, y, z); }
83 
84  inline Vector<T, 2> xy() { return Vector<T, 2>(x, y); }
85 
86  inline const Vector<T, 2> xy() const { return Vector<T, 2>(x, y); }
87 
88  inline Vector<T, 2> zw() { return Vector<T, 2>(z, w); }
89 
90  inline const Vector<T, 2> zw() const { return Vector<T, 2>(z, w); }
91 
92  inline void Pack(VectorPacked<T, 4>* const vector) const {
93  MATHFU_VECTOR_OPERATION(vector->data[i] = data_[i]);
94  }
95 
96  inline T LengthSquared() const { return LengthSquaredHelper(*this); }
97 
98  inline T Length() const { return LengthHelper(*this); }
99 
100  inline T Normalize() { return NormalizeHelper(*this); }
101 
102  inline Vector<T, 4> Normalized() const { return NormalizedHelper(*this); }
103 
104  template <typename CompatibleT>
105  static inline Vector<T, 4> FromType(const CompatibleT& compatible) {
106  return FromTypeHelper<T, d, CompatibleT>(compatible);
107  }
108 
109  template <typename CompatibleT>
110  static inline CompatibleT ToType(const Vector<T, 4>& v) {
111  return ToTypeHelper<T, d, CompatibleT>(v);
112  }
113 
114  static inline T DotProduct(const Vector<T, 4>& v1, const Vector<T, 4>& v2) {
115  return DotProductHelper(v1, v2);
116  }
117 
118  static inline Vector<T, 4> HadamardProduct(const Vector<T, 4>& v1,
119  const Vector<T, 4>& v2) {
120  return HadamardProductHelper(v1, v2);
121  }
122 
123  static inline Vector<T, 4> Lerp(const Vector<T, 4>& v1,
124  const Vector<T, 4>& v2, const T percent) {
125  return LerpHelper(v1, v2, percent);
126  }
127 
128  static inline Vector<T, 4> RandomInRange(const Vector<T, 4>& min,
129  const Vector<T, 4>& max) {
130  return RandomInRangeHelper(min, max);
131  }
132 
133  static inline Vector<T, 4> Max(const Vector<T, 4>& v1,
134  const Vector<T, 4>& v2) {
135  return MaxHelper(v1, v2);
136  }
137 
138  static inline Vector<T, 4> Min(const Vector<T, 4>& v1,
139  const Vector<T, 4>& v2) {
140  return MinHelper(v1, v2);
141  }
142 
144 
145 #include "mathfu/internal/disable_warnings_begin.h"
146  union {
147  T data_[4];
148  struct {
149  T x;
150  T y;
151  T z;
152  T w;
153  };
154  };
155 #include "mathfu/internal/disable_warnings_end.h"
156 };
157 
158 template <class T>
159 struct VectorPacked<T, 4> {
160  /// Create an uninitialized VectorPacked.
162 
163  /// Create a VectorPacked from a Vector.
164  ///
165  /// Both VectorPacked and Vector must have the same number of dimensions.
166  /// @param vector Vector to create the VectorPacked from.
167  explicit VectorPacked(const Vector<T, 4>& vector) { vector.Pack(this); }
168 
169  /// Copy a Vector to a VectorPacked.
170  ///
171  /// Both VectorPacked and Vector must have the same number of dimensions.
172  /// @param vector Vector to copy to the VectorPacked.
173  /// @returns A reference to this VectorPacked.
175  vector.Pack(this);
176  return *this;
177  }
178 
179 #include "mathfu/internal/disable_warnings_begin.h"
180  /// Elements of the packed vector one per dimension.
181  union {
182  T data[4];
183  struct {
184  T x;
185  T y;
186  T z;
187  T w;
188  };
189  };
190 #include "mathfu/internal/disable_warnings_end.h"
191 };
192 
193 } // namespace mathfu
194 
195 #endif // MATHFU_INTERNAL_VECTOR_4_H_
Definition: vector_3.h:24
T LengthHelper(const Vector< T, d > &v)
Calculate the length of a vector.
Definition: vector.h:787
T LengthSquared() const
Calculate the squared length of this vector.
Definition: vector.h:380
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
VectorPacked(const Vector< T, 4 > &vector)
Definition: vector_4.h:167
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, 2 > zw()
GLSL style 2 element accessor.
Definition: vector.h:356
T LengthSquaredHelper(const Vector< T, d > &v)
Calculate the squared length of a vector.
Definition: vector.h:778
Definition: vector_4.h:25
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
Definition: vector_4.h:159
VectorPacked()
Create an uninitialized VectorPacked.
Definition: vector_4.h:161
VectorPacked & operator=(const Vector< T, 4 > &vector)
Definition: vector_4.h:174
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
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
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
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