Add operator<< to print a quaternion.

This commit is contained in:
Rasmus Munk Larsen 2020-07-09 12:49:58 -07:00
parent ee4715ff48
commit fb77b7288c
2 changed files with 12 additions and 2 deletions

View File

@ -211,6 +211,11 @@ class QuaternionBase : public RotationBase<Derived, 3>
}
#endif
friend std::ostream& operator<<(std::ostream& s, const QuaternionBase<Derived>& q) {
s << q.x() << "i + " << q.y() << "j + " << q.z() << "k" << " + " << q.w();
return s;
}
#ifdef EIGEN_QUATERNIONBASE_PLUGIN
# include EIGEN_QUATERNIONBASE_PLUGIN
#endif

View File

@ -75,6 +75,11 @@ template<typename Scalar, int Options> void quaternion(void)
q1.coeffs().setRandom();
VERIFY_IS_APPROX(q1.coeffs(), (q1*q2).coeffs());
// Printing
std::ostringstream ss;
ss << q2;
VERIFY(ss.str() == "0i + 0j + 0k + 1");
// concatenation
q1 *= q2;