18 #ifndef ION_MATH_VECTORUTILS_H_
19 #define ION_MATH_VECTORUTILS_H_
37 template <
int Dimension,
typename T>
39 T dot =
static_cast<T>(0);
40 for (
int i = 0; i < Dimension; ++i)
41 dot += (v0[i] * v1[i]);
50 v0[2] * v1[0] - v0[0] * v1[2],
51 v0[0] * v1[1] - v0[1] * v1[0]);
59 return (v0[0] * v1[1] - v0[1] * v1[0]);
63 template <
int Dimension,
typename T>
69 template <
int Dimension,
typename T>
75 template <
int Dimension,
typename T>
82 template <
int Dimension,
typename T>
89 template <
int Dimension,
typename T>
93 if (len == static_cast<T>(0)) {
103 template <
int Dimension,
typename T>
114 template <
typename T>
123 template <
typename T>
125 static const T kTolerance =
static_cast<T>(0.0001);
127 if (
Length(n) < kTolerance) {
129 if (
Length(n) < kTolerance)
139 template <
int Dimension,
typename T>
146 template <
int Dimension,
typename T>
151 (
Dot(v, onto_v) / len_squared) * onto_v;
157 template <
int Dimension,
typename T>
163 template <
int Dimension,
typename T>
166 for (
int i = 0; i < Dimension; ++i) {
167 if (
Abs(v0[i] - v1[i]) >
Abs(tolerance))
175 template <
int Dimension,
typename T>
179 for (
int i = 0; i < Dimension; ++i)
180 min_point[i] = std::min(p0[i], p1[i]);
186 template <
int Dimension,
typename T>
190 for (
int i = 0; i < Dimension; ++i)
191 max_point[i] = std::max(p0[i], p1[i]);
196 template <
int Dimension,
typename T>
205 const double projection =
Dot(to_min, diff);
206 const double length_squared =
Dot(diff, diff);
207 if (projection <= static_cast<T>(0)) {
209 }
else if (length_squared <= projection) {
212 const T t = projection / length_squared;
213 return start + t * diff;
219 template <
int Dimension,
typename T>
227 template <
int Dimension,
typename T>
235 template <
int Dimension,
typename T>
238 for (
int i = 0; i < Dimension; ++i) {
239 if (
Abs(v0[i] - v1[i]) >
Abs(tolerance))
261 template <
int InDimension,
int OutDimension,
typename T>
264 for (
int i = 0; i < OutDimension; ++i) {
266 switch (swizzle_string[i]) {
267 case 'x':
case 'r' :
case 's':
268 case 'X':
case 'R' :
case 'S':
271 case 'y':
case 'g' :
case 't':
272 case 'Y':
case 'G' :
case 'T':
275 case 'z':
case 'b' :
case 'p':
276 case 'Z':
case 'B' :
case 'P':
279 case 'w':
case 'a' :
case 'q':
280 case 'W':
case 'A' :
case 'Q':
288 if (dim >= InDimension)
290 (*output)[i] = input[dim];
296 template <
int Dimension,
typename T>
298 for (
int i = 0; i < Dimension; ++i) {
308 #endif // ION_MATH_VECTORUTILS_H_
bool Swizzle(const VectorBase< InDimension, T > &input, const char *swizzle_string, VectorBase< OutDimension, T > *output)
Computes the result of swizzling a Vector or Point (or anything else derived from VectorBase)...
bool Normalize(Vector< Dimension, T > *v)
Normalizes a Vector to unit length.
static const Vector Zero()
Returns a Vector containing all zeroes.
const Point< Dimension, T > MinBoundPoint(const Point< Dimension, T > &p0, const Point< Dimension, T > &p1)
Returns a Point in which each element is the minimum of the corresponding elements of two Points...
bool IsFinite(T x)
Tests whether a numeric value is finite.
T Dot(const Vector< Dimension, T > &v0, const Vector< Dimension, T > &v1)
Returns the dot (inner) product of two Vectors.
const T DistanceSquaredToSegment(const Point< Dimension, T > &p, const Point< Dimension, T > &start, const Point< Dimension, T > &end)
Returns the squared distance from a Point to a line segment described by two Points.
const Vector< Dimension, T > Orthonormal(const Vector< Dimension, T > &v)
Returns a normalized Vector that is orthonormal to the passed one.
T Sqrt(const T &val)
Returns the square root of a value.
T Distance(const Point< Dimension, T > &p0, const Point< Dimension, T > &p1)
Returns the geometric distance between two Points.
const Vector< Dimension, T > Projection(const Vector< Dimension, T > &v, const Vector< Dimension, T > &onto_v)
Returns the Vector resulting from projecting of one Vector onto another.
const Point< Dimension, T > MaxBoundPoint(const Point< Dimension, T > &p0, const Point< Dimension, T > &p1)
Returns a Point in which each element is the maximum of the corresponding elements of two Points...
bool PointsAlmostEqual(const Point< Dimension, T > &v0, const Point< Dimension, T > &v1, T tolerance)
Returns true if all elements of two Points are equal within a tolerance.
const T DistanceToSegment(const Point< Dimension, T > &p, const Point< Dimension, T > &start, const Point< Dimension, T > &end)
Returns the distance from a Point to a line segment described by two Points.
T DistanceSquared(const Point< Dimension, T > &p0, const Point< Dimension, T > &p1)
Returns the square of the distance between two Points.
Point< Dimension, T > ClosestPointOnSegment(const Point< Dimension, T > &p, const Point< Dimension, T > &start, const Point< Dimension, T > &end)
Returns the closest point to p on the line segment defined by start and end.
const Vector< Dimension, T > Normalized(const Vector< Dimension, T > &v)
Returns a unit-length version of a Vector.
const Vector< Dimension, T > Rescale(const Vector< Dimension, T > &v, T length)
Returns a Vector in the same direction as the passed vector but with the passed length.
Copyright 2016 Google Inc.
const Vector< 2, T > Orthogonal(const Vector< 2, T > &v)
Returns an unnormalized Vector2 that is orthonormal to the passed one.
bool VectorsAlmostEqual(const Vector< Dimension, T > &v0, const Vector< Dimension, T > &v1, T tolerance)
Returns true if all elements of two Vectors are equal within a tolerance.
bool IsVectorFinite(const VectorBase< Dimension, T > &v)
Returns true if all components of VectorBase v are finite, otherwise false.
const T Abs(const T &val)
Returns the absolute value of a number in a type-safe way.
Vector< 3, T > Cross(const Vector< 3, T > &v0, const Vector< 3, T > &v1)
Returns the 3-dimensional cross product of 2 Vectors.
T Length(const Vector< Dimension, T > &v)
Returns the geometric length of a Vector.
T LengthSquared(const Vector< Dimension, T > &v)
Returns the square of the length of a Vector.