16 #ifndef MATHFU_VECTOR_H_
17 #define MATHFU_VECTOR_H_
30 #pragma warning(disable : 4127) // conditional expression is constant
31 #if _MSC_VER >= 1900 // MSVC 2015
32 #pragma warning(disable : 4456) // allow shadowing in unrolled loops
33 #endif // _MSC_VER >= 1900
34 #elif defined(__clang__)
35 #pragma clang diagnostic push
36 #pragma clang diagnostic ignored "-Warray-bounds"
40 #define MATHFU_VECTOR_OPERATION(OP) MATHFU_UNROLLED_LOOP(i, d, OP)
44 #define MATHFU_VECTOR_OPERATOR(OP) \
46 Vector<T, d> result; \
47 MATHFU_VECTOR_OPERATION(result[i] = OP); \
53 #define MATHFU_VECTOR_SELF_OPERATOR(OP) \
55 MATHFU_VECTOR_OPERATION(OP); \
62 template <
class T,
int d>
66 template <
class T,
int d>
79 template <
typename T,
int d,
typename CompatibleT>
80 static inline Vector<T, d> FromTypeHelper(
const CompatibleT& compatible);
82 template <
typename T,
int d,
typename CompatibleT>
83 static inline CompatibleT ToTypeHelper(
const Vector<T, d>& v);
120 template <
class T,
int d>
156 template <
class T,
int d>
169 MATHFU_VECTOR_OPERATION(
data_[i] = v.
data_[i]);
179 template <
typename U>
181 MATHFU_VECTOR_OPERATION(
data_[i] = static_cast<T>(v[i]));
188 explicit inline Vector(
const T& s) { MATHFU_VECTOR_OPERATION(
data_[i] = s); }
194 MATHFU_VECTOR_OPERATION(
data_[i] = a[i]);
203 inline Vector(
const T& s1,
const T& s2) {
216 inline Vector(
const T& s1,
const T& s2,
const T& s3) {
244 inline Vector(
const T& s1,
const T& s2,
const T& s3,
const T& s4) {
261 data_[0] = vector3[0];
262 data_[1] = vector3[1];
263 data_[2] = vector3[2];
285 MATHFU_VECTOR_OPERATION(
data_[i] = vector.
data[i]);
374 MATHFU_VECTOR_OPERATION(vector->
data[i] =
data_[i]);
404 template <
typename CompatibleT>
406 return FromTypeHelper<T, d, CompatibleT>(compatible);
416 template <
typename CompatibleT>
418 return ToTypeHelper<T, d, CompatibleT>(v);
427 return DotProductHelper(v1, v2);
510 template <
class T,
int d>
512 for (
int i = 0; i < d; ++i) {
513 if (lhs[i] != rhs[i])
return false;
521 template <
class T,
int d>
523 return !(lhs == rhs);
529 template <
class T,
int d>
531 MATHFU_VECTOR_OPERATOR(-v.
data_[i]);
542 template <
class T,
int d>
544 MATHFU_VECTOR_OPERATOR(v.
data_[i] * s);
555 template <
class T,
int d>
557 MATHFU_VECTOR_OPERATOR(v.
data_[i] / s);
566 template <
class T,
int d>
568 MATHFU_VECTOR_OPERATOR(v.
data_[i] + s);
577 template <
class T,
int d>
579 MATHFU_VECTOR_OPERATOR(v.
data_[i] - s);
588 template <
class T,
int d>
600 template <
class T,
int d>
603 MATHFU_VECTOR_OPERATOR(lhs.
data_[i] / rhs[i]);
611 template <
class T,
int d>
614 MATHFU_VECTOR_OPERATOR(lhs.
data_[i] + rhs[i]);
622 template <
class T,
int d>
625 MATHFU_VECTOR_OPERATOR(lhs.
data_[i] - rhs[i]);
633 template <
class T,
int d>
635 MATHFU_VECTOR_OPERATOR(v.
data_[i] * s);
643 template <
class T,
int d>
645 MATHFU_VECTOR_OPERATOR(v.
data_[i] + s);
653 template <
class T,
int d>
655 MATHFU_VECTOR_OPERATOR(v.
data_[i] - s);
664 template <
class T,
int d>
666 MATHFU_VECTOR_OPERATION(lhs.
data_[i] *= rhs[i]);
676 template <
class T,
int d>
678 MATHFU_VECTOR_OPERATION(lhs.
data_[i] /= rhs[i]);
687 template <
class T,
int d>
689 MATHFU_VECTOR_OPERATION(lhs.
data_[i] += rhs[i]);
698 template <
class T,
int d>
700 MATHFU_VECTOR_OPERATION(lhs.
data_[i] -= rhs[i]);
709 template <
class T,
int d>
711 MATHFU_VECTOR_OPERATION(v.
data_[i] *= s);
720 template <
class T,
int d>
722 MATHFU_VECTOR_OPERATION(v.
data_[i] /= s);
731 template <
class T,
int d>
733 MATHFU_VECTOR_OPERATION(v.
data_[i] += s);
742 template <
class T,
int d>
744 MATHFU_VECTOR_OPERATION(v.
data_[i] -= s);
753 template <
class T,
int d>
756 MATHFU_VECTOR_OPERATOR(v1[i] * v2[i]);
769 v1[2] * v2[0] - v1[0] * v2[2],
770 v1[0] * v2[1] - v1[1] * v2[0]);
777 template <
class T,
int d>
779 return DotProductHelper(v, v);
786 template <
class T,
int d>
795 template <
class T,
int d>
798 v *= (T(1) / length);
806 template <
class T,
int d>
817 template <
class T,
int d>
820 const T one_minus_percent =
static_cast<T
>(1.0) - percent;
821 MATHFU_VECTOR_OPERATOR(one_minus_percent * v1[i] + percent * v2[i]);
829 template <
class T,
int d>
833 MATHFU_VECTOR_OPERATION(result[i] = mathfu::RandomInRange<T>(min[i], max[i]));
842 template <
class T,
int d>
845 MATHFU_VECTOR_OPERATION(result[i] = std::max(v1[i], v2[i]));
854 template <
class T,
int d>
857 MATHFU_VECTOR_OPERATION(result[i] = std::min(v1[i], v2[i]));
874 return InRange(val[0], range_start[0], range_end[0]) &&
875 InRange(val[1], range_start[1], range_end[1]);
885 template <
class T,
int d>
886 static inline T DotProductHelper(
const Vector<T, d>& v1,
887 const Vector<T, d>& v2) {
889 MATHFU_VECTOR_OPERATION(result += v1[i] * v2[i]);
896 static inline T DotProductHelper(
const Vector<T, 2>& v1,
897 const Vector<T, 2>& v2) {
898 return v1[0] * v2[0] + v1[1] * v2[1];
904 static inline T DotProductHelper(
const Vector<T, 3>& v1,
905 const Vector<T, 3>& v2) {
906 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
912 static inline T DotProductHelper(
const Vector<T, 4>& v1,
913 const Vector<T, 4>& v2) {
914 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3];
919 template <
typename T,
int d,
typename CompatibleT>
920 static inline Vector<T, d> FromTypeHelper(
const CompatibleT& compatible) {
922 #if __cplusplus >= 201103L
924 union ConversionUnion {
926 CompatibleT compatible;
927 VectorPacked<T, d> packed;
929 static_assert(
sizeof(u.compatible) == d *
sizeof(T),
930 "Conversion size mismatch.");
934 u.compatible = compatible;
937 return Vector<T, d>(u.packed);
948 assert(
sizeof(compatible) == d *
sizeof(T));
949 memcpy(&v, &compatible,
sizeof(compatible));
951 #endif // __cplusplus >= 201103L
956 template <
typename T,
int d,
typename CompatibleT>
957 static inline CompatibleT ToTypeHelper(
const Vector<T, d>& v) {
959 #if __cplusplus >= 201103L
960 union ConversionUnion {
962 CompatibleT compatible;
963 VectorPacked<T, d> packed;
965 static_assert(
sizeof(u.compatible) == d *
sizeof(T),
"Conversion size mismatch.");
969 CompatibleT compatible;
970 assert(
sizeof(compatible) == d *
sizeof(T));
971 memcpy(&compatible, &v,
sizeof(compatible));
973 #endif // __cplusplus >= 201103L
983 template <
typename T,
int d>
1001 #if defined(_MSC_VER)
1002 #pragma warning(pop)
1003 #elif defined(__clang__)
1004 #pragma clang diagnostic pop
1007 #endif // MATHFU_VECTOR_H_
Vector< T, d > & operator*=(Vector< T, d > &lhs, const Vector< T, d > &rhs)
Multiply (in-place) a vector with another Vector.
Definition: vector.h:665
Definition: vector_3.h:24
T LengthHelper(const Vector< T, d > &v)
Calculate the length of a vector.
Definition: vector.h:787
Vector(const Vector< T, 2 > &v12, const T &s3)
Create a vector from a 2 component vector and a third value.
Definition: vector.h:229
T LengthSquared() const
Calculate the squared length of this vector.
Definition: vector.h:380
T RoundUpToPowerOf2(T x)
Round a value up to the nearest power of 2.
Definition: utilities.h:425
const Vector< T, 3 > xyz() const
GLSL style 3 element accessor.
Definition: vector.h:329
VectorPacked & operator=(const Vector< T, d > &vector)
Definition: vector.h:136
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
Utility macros and functions.
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
VectorPacked()
Create an uninitialized VectorPacked.
Definition: vector.h:123
Vector< T, d > NormalizedHelper(const Vector< T, d > &v)
Calculate the normalized version of a vector.
Definition: vector.h:807
Vector(const T &s1, const T &s2)
Create a vector from two values.
Definition: vector.h:203
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
Vector< T, d > & operator+=(Vector< T, d > &lhs, const Vector< T, d > &rhs)
Add (in-place) a vector with another Vector.
Definition: vector.h:688
MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE T data_[d]
Elements of the vector.
Definition: vector.h:495
Vector(const Vector< T, 3 > &vector3, const T &value)
Create a 4-dimensional vector from a Vector<T, 3>.
Definition: vector.h:259
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
#define MATHFU_STATIC_ASSERT(x)
Compile time assert for pre-C++11 compilers.
Definition: utilities.h:244
T Normalize()
Normalize this vector in-place.
Definition: vector.h:390
Vector< T, d > operator+(const Vector< T, d > &lhs, const Vector< T, d > &rhs)
Add a vector with another Vector.
Definition: vector.h:612
Vector< T, d > operator/(const Vector< T, d > &v, const T &s)
Divide a Vector by a scalar.
Definition: vector.h:556
Vector< T, d > & operator-=(Vector< T, d > &lhs, const Vector< T, d > &rhs)
Subtract (in-place) another Vector from a vector.
Definition: vector.h:699
Vector(const Vector< T, d > &v)
Create a vector from another vector copying each element.
Definition: vector.h:168
Vector< T, d > operator*(const Vector< T, d > &lhs, const Vector< T, d > &rhs)
Multiply a vector by another Vector.
Definition: vector.h:589
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
bool InRange2D(const mathfu::Vector< T, 2 > &val, const mathfu::Vector< T, 2 > &range_start, const mathfu::Vector< T, 2 > &range_end)
Check if val is within [range_start..range_end), denoting a rectangular area.
Definition: vector.h:871
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
Vector< T, 2 > zw()
GLSL style 2 element accessor.
Definition: vector.h:356
Vector(const T &s)
Create a vector from a single float.
Definition: vector.h:188
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
const Vector< T, 2 > xy() const
GLSL style 2 element accessor.
Definition: vector.h:347
Vector(const T &s1, const T &s2, const T &s3, const T &s4)
Create a vector from four values.
Definition: vector.h:244
Vector< T, d > operator*(const T &s, const Vector< T, d > &v)
Multiply a Vector by a scalar.
Definition: vector.h:543
Vector< T, 2 > xy()
GLSL style 2 element accessor.
Definition: vector.h:338
Vector(const T &s1, const T &s2, const T &s3)
Create a vector from three values.
Definition: vector.h:216
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
const T & operator()(const int i) const
Access an element of the vector.
Definition: vector.h:299
bool InRange(T val, T range_start, T range_end)
Check if val is within [range_start..range_end).
Definition: utilities.h:343
Vector(const Vector< T, 2 > &v12, const Vector< T, 2 > &v34)
Create a vector from two 2 component vectors.
Definition: vector.h:273
Vector< T, d > operator+(const T &s, const Vector< T, d > &v)
Add a scalar to each element of a Vector.
Definition: vector.h:567
Vector< T, d > operator-(const Vector< T, d > &v)
Negate all elements of the Vector.
Definition: vector.h:530
Vector(const T *a)
Create a vector form the first d elements of an array.
Definition: vector.h:193
3-dimensional specialization of mathfu::Vector for SIMD optimized builds.
Vector< T, d > RandomInRangeHelper(const Vector< T, d > &min, const Vector< T, d > &max)
Generates a random vector.
Definition: vector.h:830
Vector< T, d > operator/(const Vector< T, d > &lhs, const Vector< T, d > &rhs)
Divide a vector by another Vector.
Definition: vector.h:601
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
4-dimensional specialization of mathfu::Vector for SIMD optimized builds.
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, d > &vector)
Definition: vector.h:129
T & operator()(const int i)
Access an element of the vector.
Definition: vector.h:293
const Vector< T, 2 > zw() const
GLSL style 2 element accessor.
Definition: vector.h:365
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
bool operator==(const Rect< T > &r1, const Rect< T > &r2)
Check if two rects are identical.
Definition: rect.h:67
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
2-dimensional specialization of mathfu::Vector for SIMD optimized builds.
const T & operator[](const int i) const
Access an element of the vector.
Definition: vector.h:312
Vector()
Create an uninitialized Vector.
Definition: vector.h:163
Vector(const Vector< U, d > &v)
Create a vector from another vector of a different type.
Definition: vector.h:180
Vector(const VectorPacked< T, d > &vector)
Create a vector from packed vector (VectorPacked).
Definition: vector.h:284
Vector< T, d > operator-(const T &s, const Vector< T, d > &v)
Subtract a scalar from each element of a Vector.
Definition: vector.h:578
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, d > & operator/=(Vector< T, d > &lhs, const Vector< T, d > &rhs)
Divide (in-place) a vector by another Vector.
Definition: vector.h:677
Vector< T, 3 > xyz()
GLSL style 3 element accessor.
Definition: vector.h:319
bool operator!=(const Rect< T > &r1, const Rect< T > &r2)
Check if two rects are not identical.
Definition: rect.h:76
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