How to compare QVector3D or Eigen::Vector3d for equality with tolerance?

474 views Asked by At

I would like to use the BOOST_TEST machinery to compare mathematical vector types using plain (in)equality operators.

I can only find how to tell Boost.Test that it should do that for a type (by specializing boost::math::fpc::tolerance_based for that type), given the presence of the usual arithmetic and comparison operators, but I can't tell it to do the comparison in a specific way (I'd like the element-wise comparison here, and only really need (in)equality, no less/greater etc.).

Is there any customization point for this functionality? If not, how can I easily enable such behaviour only in my tests?

1

There are 1 answers

1
Matt On

You can always write a custom operator that will do the comparison. For example:

bool operator == (const Vector3d &lhs, const double &rhs){
    return (lhs.array() == rhs).all();
}

My guess would be the authors of Eigen didn't include these because they provided the array class to give you access for element-wise operations. Also there are many ways to define inequalities like this and everyone will have a different set of requirements.