2
votes

The compiler can't figure out the less than operator for the type. I've also tried with a lambda and predicate function.

#include <Eigen/Dense>
typedef Eigen::Vector3f vec3;

inline bool operator<(const vec3 &lhs, const vec3 &rhs) {
    return lhs.x() < rhs.x() && lhs.y() < rhs.y() && lhs.z() < rhs.z();
}

inline bool cmpVecs(const vec3 &lhs, const vec3 &rhs) {
    return lhs.x() < rhs.x() && lhs.y() < rhs.y() && lhs.z() < rhs.z();
}


inline void removeDuplicates(std::vector<vec3> &con)
{
    std::sort(con.data(), con.data() + con.size());
    auto itr = std::unique(con.begin(), con.end(), cmpVecs);
    con.resize(itr - con.begin());
}

void init(std::vector<vec3> &verts) {
    removeDuplicates(verts);
}

VS 2012 error:

algorithm(3618): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'Eigen::Matrix<_Scalar,_Rows,_Cols>' (or there is no acceptable conversion) 1> with 1> [ 1> _Scalar=float, 1> _Rows=3, 1>
_Cols=1 1> ]

Related Posts:

1
Your comparison functions are incorrect, as std::sort require a strict weak ordering predicate. { if (lhs.x() < rhs.x()) return true; if (rhs.x() < lhs.x()) return false; if (lhs.y() < rhs.y()) return true; if (rhs.y() < lhs.y()) return false; return lhs.z() < rhs.z(); }dalle
The comparison in the comment above works for me. This was quite difficult to find, despite that unique is very common in matlab.Armin Meisterhirn

1 Answers

1
votes

std::sort has an override available which allows you to specify which comparator you want to use, for example:

struct vec3comp{
    bool operator()(const vec3 &lhs, const vec3 &rhs){
        return lhs.x() < rhs.x() && lhs.y() < rhs.y() && lhs.z() < rhs.z();
    }
} mycomp;

std::sort(con.data(), con.data() + con.size(),mycomp);`

Edit: can you show your code with a lambda function? It should work fine.