Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vectorutils.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_VECTORUTILS_H_
19 #define ION_MATH_VECTORUTILS_H_
20 
25 
26 #include <algorithm>
27 #include <limits>
28 
29 #include "ion/base/logging.h"
30 #include "ion/math/utils.h"
31 #include "ion/math/vector.h"
32 
33 namespace ion {
34 namespace math {
35 
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]);
42  return dot;
43 }
44 
47 template <typename T>
49  return Vector<3, T>(v0[1] * v1[2] - v0[2] * v1[1],
50  v0[2] * v1[0] - v0[0] * v1[2],
51  v0[0] * v1[1] - v0[1] * v1[0]);
52 }
53 
57 template <typename T>
58 T Cross(const Vector<2, T>& v0, const Vector<2, T>& v1) {
59  return (v0[0] * v1[1] - v0[1] * v1[0]);
60 }
61 
63 template <int Dimension, typename T>
65  return Dot(v, v);
66 }
67 
69 template <int Dimension, typename T>
71  return static_cast<T>(Sqrt(LengthSquared(v)));
72 }
73 
75 template <int Dimension, typename T>
77  const Point<Dimension, T>& p1) {
78  return LengthSquared(p0 - p1);
79 }
80 
82 template <int Dimension, typename T>
84  return Length(p0 - p1);
85 }
86 
89 template <int Dimension, typename T>
91  DCHECK(v);
92  const T len = Length(*v);
93  if (len == static_cast<T>(0)) {
94  return false;
95  } else {
96  (*v) /= len;
97  return true;
98  }
99 }
100 
103 template <int Dimension, typename T>
105  Vector<Dimension, T> result = v;
106  if (Normalize(&result))
107  return result;
108  else
110 }
111 
114 template <typename T>
116  return Vector<2, T>(-v[1], v[0]);
117 }
118 
123 template <typename T>
125  static const T kTolerance = static_cast<T>(0.0001);
127  if (Length(n) < kTolerance) {
128  n = Cross(v, Vector<3, T>::AxisY());
129  if (Length(n) < kTolerance)
130  n = Cross(v, Vector<3, T>::AxisZ());
131  }
132  return n;
133 }
134 
139 template <int Dimension, typename T>
141  return Normalized(Orthogonal(v));
142 }
143 
146 template <int Dimension, typename T>
148  const Vector<Dimension, T>& onto_v) {
149  const T len_squared = LengthSquared(onto_v);
150  return len_squared == static_cast<T>(0) ? Vector<Dimension, T>::Zero() :
151  (Dot(v, onto_v) / len_squared) * onto_v;
152 }
153 
157 template <int Dimension, typename T>
159  return Normalized(v) * length;
160 }
161 
163 template <int Dimension, typename T>
165  const Vector<Dimension, T>& v1, T tolerance) {
166  for (int i = 0; i < Dimension; ++i) {
167  if (Abs(v0[i] - v1[i]) > Abs(tolerance))
168  return false;
169  }
170  return true;
171 }
172 
175 template <int Dimension, typename T>
177  const Point<Dimension, T>& p1) {
178  Point<Dimension, T> min_point;
179  for (int i = 0; i < Dimension; ++i)
180  min_point[i] = std::min(p0[i], p1[i]);
181  return min_point;
182 }
183 
186 template <int Dimension, typename T>
188  const Point<Dimension, T>& p1) {
189  Point<Dimension, T> max_point;
190  for (int i = 0; i < Dimension; ++i)
191  max_point[i] = std::max(p0[i], p1[i]);
192  return max_point;
193 }
194 
196 template <int Dimension, typename T>
198  const Point<Dimension, T>& start,
199  const Point<Dimension, T>& end) {
200  const Vector<Dimension, T> diff = end - start;
201  if (LengthSquared(diff) == static_cast<T>(0))
202  return start;
203 
204  const Vector<Dimension, T> to_min = p - start;
205  const double projection = Dot(to_min, diff);
206  const double length_squared = Dot(diff, diff);
207  if (projection <= static_cast<T>(0)) {
208  return start;
209  } else if (length_squared <= projection) {
210  return end;
211  } else {
212  const T t = projection / length_squared;
213  return start + t * diff;
214  }
215 }
216 
219 template <int Dimension, typename T>
221  const Point<Dimension, T>& start,
222  const Point<Dimension, T>& end) {
223  return DistanceSquared(p, ClosestPointOnSegment(p, start, end));
224 }
225 
227 template <int Dimension, typename T>
229  const Point<Dimension, T>& start,
230  const Point<Dimension, T>& end) {
231  return Distance(p, ClosestPointOnSegment(p, start, end));
232 }
233 
235 template <int Dimension, typename T>
237  const Point<Dimension, T>& v1, T tolerance) {
238  for (int i = 0; i < Dimension; ++i) {
239  if (Abs(v0[i] - v1[i]) > Abs(tolerance))
240  return false;
241  }
242  return true;
243 }
244 
261 template <int InDimension, int OutDimension, typename T>
263  const char* swizzle_string, VectorBase<OutDimension, T>* output) {
264  for (int i = 0; i < OutDimension; ++i) {
265  int dim;
266  switch (swizzle_string[i]) {
267  case 'x': case 'r' : case 's':
268  case 'X': case 'R' : case 'S':
269  dim = 0;
270  break;
271  case 'y': case 'g' : case 't':
272  case 'Y': case 'G' : case 'T':
273  dim = 1;
274  break;
275  case 'z': case 'b' : case 'p':
276  case 'Z': case 'B' : case 'P':
277  dim = 2;
278  break;
279  case 'w': case 'a' : case 'q':
280  case 'W': case 'A' : case 'Q':
281  dim = 3;
282  break;
283  default:
286  return false;
287  }
288  if (dim >= InDimension)
289  return false;
290  (*output)[i] = input[dim];
291  }
292  return true;
293 }
294 
296 template <int Dimension, typename T>
298  for (int i = 0; i < Dimension; ++i) {
299  if (!IsFinite(v[i]))
300  return false;
301  }
302  return true;
303 }
304 
305 } // namespace math
306 } // namespace ion
307 
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)...
Definition: vectorutils.h:262
VectorBase.
Definition: vector.h:43
bool Normalize(Vector< Dimension, T > *v)
Normalizes a Vector to unit length.
Definition: vectorutils.h:90
static const Vector Zero()
Returns a Vector containing all zeroes.
Definition: vector.h:201
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...
Definition: vectorutils.h:176
#define DCHECK(expr)
Definition: logging.h:331
bool IsFinite(T x)
Tests whether a numeric value is finite.
Definition: utils.h:34
T Dot(const Vector< Dimension, T > &v0, const Vector< Dimension, T > &v1)
Returns the dot (inner) product of two Vectors.
Definition: vectorutils.h:38
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.
Definition: vectorutils.h:220
const Vector< Dimension, T > Orthonormal(const Vector< Dimension, T > &v)
Returns a normalized Vector that is orthonormal to the passed one.
Definition: vectorutils.h:140
uint32 length
T Sqrt(const T &val)
Returns the square root of a value.
Definition: utils.h:59
T Distance(const Point< Dimension, T > &p0, const Point< Dimension, T > &p1)
Returns the geometric distance between two Points.
Definition: vectorutils.h:83
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.
Definition: vectorutils.h:147
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...
Definition: vectorutils.h:187
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.
Definition: vectorutils.h:236
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.
Definition: vectorutils.h:228
T DistanceSquared(const Point< Dimension, T > &p0, const Point< Dimension, T > &p1)
Returns the square of the distance between two Points.
Definition: vectorutils.h:76
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.
Definition: vectorutils.h:197
const Vector< Dimension, T > Normalized(const Vector< Dimension, T > &v)
Returns a unit-length version of a Vector.
Definition: vectorutils.h:104
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.
Definition: vectorutils.h:158
Copyright 2016 Google Inc.
Vector.
Definition: vector.h:180
const Vector< 2, T > Orthogonal(const Vector< 2, T > &v)
Returns an unnormalized Vector2 that is orthonormal to the passed one.
Definition: vectorutils.h:115
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.
Definition: vectorutils.h:164
Point.
Definition: vector.h:296
bool IsVectorFinite(const VectorBase< Dimension, T > &v)
Returns true if all components of VectorBase v are finite, otherwise false.
Definition: vectorutils.h:297
const T Abs(const T &val)
Returns the absolute value of a number in a type-safe way.
Definition: utils.h:42
Vector< 3, T > Cross(const Vector< 3, T > &v0, const Vector< 3, T > &v1)
Returns the 3-dimensional cross product of 2 Vectors.
Definition: vectorutils.h:48
T Length(const Vector< Dimension, T > &v)
Returns the geometric length of a Vector.
Definition: vectorutils.h:70
T LengthSquared(const Vector< Dimension, T > &v)
Returns the square of the length of a Vector.
Definition: vectorutils.h:64