MathFu
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
vector_2.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_2_H_
17 #define MATHFU_INTERNAL_VECTOR_2_H_
18 
19 #include "mathfu/vector.h"
20 
21 namespace mathfu {
22 
23 template <class T>
24 class Vector<T, 2> {
25  public:
26  typedef T Scalar;
27  static const int d = 2;
28 
29  inline Vector() {}
30 
31  inline Vector(const Vector<T, 2>& v) {
32  MATHFU_VECTOR_OPERATION(data_[i] = v.data_[i]);
33  }
34 
35  template <typename U>
36  explicit inline Vector(const Vector<U, 2>& 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) {
47  x = s1;
48  y = s2;
49  }
50 
51  explicit inline Vector(const VectorPacked<T, 2>& vector) {
52  MATHFU_VECTOR_OPERATION(data_[i] = vector.data[i]);
53  }
54 
55  inline T& operator()(const int i) { return data_[i]; }
56 
57  inline const T& operator()(const int i) const { return data_[i]; }
58 
59  inline T& operator[](const int i) { return data_[i]; }
60 
61  inline const T& operator[](const int i) const { return data_[i]; }
62 
63  inline Vector<T, 2> xy() { return Vector<T, 2>(x, y); }
64 
65  inline const Vector<T, 2> xy() const { return Vector<T, 2>(x, y); }
66 
67  inline void Pack(VectorPacked<T, 2>* const vector) const {
68  MATHFU_VECTOR_OPERATION(vector->data[i] = data_[i]);
69  }
70 
71  inline T LengthSquared() const { return LengthSquaredHelper(*this); }
72 
73  inline T Length() const { return LengthHelper(*this); }
74 
75  inline T Normalize() { return NormalizeHelper(*this); }
76 
77  inline Vector<T, 2> Normalized() const { return NormalizedHelper(*this); }
78 
79  template <typename CompatibleT>
80  static inline Vector<T, 2> FromType(const CompatibleT& compatible) {
81  return FromTypeHelper<T, d, CompatibleT>(compatible);
82  }
83 
84  template <typename CompatibleT>
85  static inline CompatibleT ToType(const Vector<T, 2>& v) {
86  return ToTypeHelper<T, d, CompatibleT>(v);
87  }
88 
89  static inline T DotProduct(const Vector<T, 2>& v1, const Vector<T, 2>& v2) {
90  return DotProductHelper(v1, v2);
91  }
92 
93  static inline Vector<T, 2> HadamardProduct(const Vector<T, 2>& v1,
94  const Vector<T, 2>& v2) {
95  return HadamardProductHelper(v1, v2);
96  }
97 
98  static inline Vector<T, 2> Lerp(const Vector<T, 2>& v1,
99  const Vector<T, 2>& v2, const T percent) {
100  return LerpHelper(v1, v2, percent);
101  }
102 
103  static inline Vector<T, 2> RandomInRange(const Vector<T, 2>& min,
104  const Vector<T, 2>& max) {
105  return RandomInRangeHelper(min, max);
106  }
107 
108  static inline Vector<T, 2> Max(const Vector<T, 2>& v1,
109  const Vector<T, 2>& v2) {
110  return MaxHelper(v1, v2);
111  }
112 
113  static inline Vector<T, 2> Min(const Vector<T, 2>& v1,
114  const Vector<T, 2>& v2) {
115  return MinHelper(v1, v2);
116  }
117 
119 
120 #include "mathfu/internal/disable_warnings_begin.h"
121  union {
122  T data_[2];
123  struct {
124  T x;
125  T y;
126  };
127  };
128 #include "mathfu/internal/disable_warnings_end.h"
129 };
130 
131 template <class T>
132 struct VectorPacked<T, 2> {
133  /// Create an uninitialized VectorPacked.
135 
136  /// Create a VectorPacked from a Vector.
137  ///
138  /// Both VectorPacked and Vector must have the same number of dimensions.
139  /// @param vector Vector to create the VectorPacked from.
140  explicit VectorPacked(const Vector<T, 2>& vector) { vector.Pack(this); }
141 
142  /// Copy a Vector to a VectorPacked.
143  ///
144  /// Both VectorPacked and Vector must have the same number of dimensions.
145  /// @param vector Vector to copy to the VectorPacked.
146  /// @returns A reference to this VectorPacked.
148  vector.Pack(this);
149  return *this;
150  }
151 
152 #include "mathfu/internal/disable_warnings_begin.h"
153  /// Elements of the packed vector one per dimension.
154  union {
155  T data[2];
156  struct {
157  T x;
158  T y;
159  };
160  };
161 #include "mathfu/internal/disable_warnings_end.h"
162 };
163 
164 } // namespace mathfu
165 
166 #endif // MATHFU_INTERNAL_VECTOR_2_H_
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
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
T LengthSquaredHelper(const Vector< T, d > &v)
Calculate the squared length of a vector.
Definition: vector.h:778
Definition: vector_2.h:132
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
VectorPacked(const Vector< T, 2 > &vector)
Definition: vector_2.h:140
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
VectorPacked()
Create an uninitialized VectorPacked.
Definition: vector_2.h:134
VectorPacked & operator=(const Vector< T, 2 > &vector)
Definition: vector_2.h:147
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
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