I try to write a std::map< Vector3D, double > where colinear (parallel or anti-parallel) vectors should share the same key.
As a compare function I use the following function (with a 1e-9 tolerance in isEqualEnough()) that I created with the help of Using a (mathematical) vector in a std::map
struct Vector3DComparator
{
bool operator() (const Vector3D& lhsIn, const Vector3D& rhsIn) const
{
Vector3D lhs = lhsIn.absolute(); // make all members positive
Vector3D rhs = rhsIn.absolute();
if ((lhs.z < rhs.z))
return true;
if ((isEqualEnough(lhs.z, rhs.z))
&& (lhs.y < rhs.y))
return true;
if ((isEqualEnough(lhs.z, rhs.z))
&& (isEqualEnough(lhs.y, rhs.y))
&& (lhs.x < rhs.x))
return true;
return false;
}
};
When I insert the normals of a cube into my map I should get 3 different values (because I don't care about the direction) but I get 4:
- x=1 y=0 z=0
- x=0 y=1 z=0
- x=0 y=0 z=1
- x=0 y=2.2e-16 z=1
The compare function is somehow wrong but whenever I try to fix it I get an assert telling me "Expression: invalid comparator".
Anyone spots the mistake?