1 #ifndef MATH_ARRAY3_H_INCLUDED
2 #define MATH_ARRAY3_H_INCLUDED
38 inline double Norm(
const std::array<double, 3>& u)
40 return std::sqrt( u[0] * u[0] + u[1] * u[1] + u[2] * u[2] );
44 inline double Norm2(
const std::array<double, 3>& u)
46 return u[0] * u[0] + u[1] * u[1] + u[2] * u[2];
54 inline std::array<double, 3>& operator+=(std::array<double, 3>& u,
const std::array<double, 3>& v)
63 inline std::array<double, 3>& operator-=(std::array<double, 3>& u,
const std::array<double, 3>& v)
72 inline std::array<double, 3>& operator*=(std::array<double, 3>& u,
double m)
81 inline std::array<double, 3>& operator/=(std::array<double, 3>& u,
double m)
94 inline std::array<double, 3> operator+(
const std::array<double, 3>& u,
const std::array<double, 3>& v)
96 return {{ u[0] + v[0], u[1] + v[1], u[2] + v[2] }};
100 inline std::array<double, 3> operator-(
const std::array<double, 3>& u,
const std::array<double, 3>& v)
102 return {{ u[0] - v[0], u[1] - v[1], u[2] - v[2] }};
106 inline std::array<double, 3> operator* (
const std::array<double, 3>& u,
double m)
108 return {{u[0]*m, u[1]*m, u[2]*m}};
112 inline std::array<double, 3> operator* (
double m,
const std::array<double, 3>& v)
118 inline std::array<double, 3> operator/ (
const std::array<double, 3>& u,
double d)
120 return {{u[0]/d, u[1]/d, u[2]/d}};
128 inline std::array<double, 3> Normalize(
const std::array<double, 3>& u)
135 inline std::array<double, 3> CrossProduct(
const std::array<double, 3>& u,
const std::array<double, 3>& v)
137 return {{ u[1]*v[2] - u[2]*v[1], u[2]*v[0] - u[0]*v[2], u[0]*v[1] - u[1]*v[0] }};
141 inline double InnerProduct(
const std::array<double, 3>& u,
const std::array<double, 3>& v)
143 return (u[0] * v[0] + u[1] * v[1] + u[2] * v[2]);
147 inline std::array<double, 3> Orthogonalize(
const std::array<double, 3>& u)
149 return {{ u[1], -u[0], 0.0 }};
153 inline double Angle(
const std::array<double, 3>& u,
const std::array<double, 3>& v)
155 return std::acos(std::min(1.0, std::max(-1.0, InnerProduct(u, v) / (Norm(u) * Norm(v)))));
159 inline bool IsSameDirection(
const std::array<double, 3>& u,
const std::array<double, 3>& v)
165 inline double SignedAngle(
const std::array<double, 3>& u,
const std::array<double, 3>& v)
167 double cos_angle = InnerProduct(u, v) / (Norm(u) * Norm(v));
170 if (cos_angle <= -1.0) {
173 else if (cos_angle >= 1.0) {
186 inline std::ostream& operator<<(std::ostream& os, const std::array<double, 3>& v)
188 os <<
"(" << v[0] <<
", " << v[1] <<
", " << v[2] <<
")";
194 #endif // include guard
constexpr int signum(T x, std::false_type)
Overload for unsigned types.
constexpr double pi()
Math constant pi.