Ion: math/vectorutils.h Source File

1 

18 #ifndef ION_MATH_VECTORUTILS_H_

19 #define ION_MATH_VECTORUTILS_H_

20 

25 

26 #include <algorithm>

27 #include <limits>

28 

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>

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>

72 }

73 

75 template <int Dimension, typename T>

79 }

80 

82 template <int Dimension, typename T>

84  return Length(p0 - p1);

85 }

86 

89 template <int Dimension, typename T>

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>

107  return result;

108  else

110 }

111 

114 template <typename T>

117 }

118 

123 template <typename T>

125  static const T kTolerance = static_cast<T>(0.0001);

127  if (Length(n) < kTolerance) {

129  if (Length(n) < kTolerance)

131  }

132  return n;

133 }

134 

139 template <int Dimension, typename T>

142 }

143 

146 template <int Dimension, typename T>

151  (Dot(v, onto_v) / len_squared) * onto_v;

152 }

153 

157 template <int Dimension, typename T>

160 }

161 

163 template <int Dimension, typename T>

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>

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>

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>

202  return start;

203 

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>

224 }

225 

227 template <int Dimension, typename T>

232 }

233 

235 template <int Dimension, typename T>

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>

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) {

300  return false;

301  }

302  return true;

303 }

304 

305 }

306 }

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)...

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.