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

@ -197,20 +197,25 @@ class QuaternionBase : public RotationBase<Derived, 3>
#else #else
template<typename NewScalarType> template<typename NewScalarType>
EIGEN_DEVICE_FUNC inline EIGEN_DEVICE_FUNC inline
typename internal::enable_if<internal::is_same<Scalar,NewScalarType>::value,const Derived&>::type cast() const typename internal::enable_if<internal::is_same<Scalar,NewScalarType>::value,const Derived&>::type cast() const
{ {
return derived(); return derived();
} }
template<typename NewScalarType> template<typename NewScalarType>
EIGEN_DEVICE_FUNC inline EIGEN_DEVICE_FUNC inline
typename internal::enable_if<!internal::is_same<Scalar,NewScalarType>::value,Quaternion<NewScalarType> >::type cast() const typename internal::enable_if<!internal::is_same<Scalar,NewScalarType>::value,Quaternion<NewScalarType> >::type cast() const
{ {
return Quaternion<NewScalarType>(coeffs().template cast<NewScalarType>()); return Quaternion<NewScalarType>(coeffs().template cast<NewScalarType>());
} }
#endif #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 #ifdef EIGEN_QUATERNIONBASE_PLUGIN
# include EIGEN_QUATERNIONBASE_PLUGIN # include EIGEN_QUATERNIONBASE_PLUGIN
#endif #endif

View File

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