How can I make QList <QVector3D> unique
I have a QList composed of QVector3D . A QVector3D represents a vertex or point. This list also contains all the vertices of a STL-File . The problem is that the vertex exists several times in the list. Need a list of unique vertices of the STL file. How can I implement it using Qt 5.0.2 ?
QSet uses a hash function to ensure that the value is unique (QMap uses the <operator) For QVector3D, Qt does not have a qHash implementation. You can implement your own, for example. as in the example:
//place anywhere in Qt-code #include <QSet> #include <QVector3D> #include <QList> uint qHash(const QVector3D &v) { return qHash( QString( "%1x%2x%3" ).arg(vx()).arg(vy()).arg(vz()) ) ; } int foo() { QList<QVector3D> uvector3D_1; QSet<QVector3D> uvector3D_2; uvector3D_2 = QSet<QVector3D>::fromList(uvector3D_1); return 0; } static int testFoo = foo(); Because of this, it is not the fastest; it relies on the qHash Qt function for QString. But I think this is good for demonstration.
QList<QVector3D> originalVector = ...; either:
QSet<QVector3D> noDublicatesSet = QSet<QVector3D>::fromList(originalVector); or
QSet<QVector3D> noDublicatesSet = originalVector.toSet(); also you can add something like if you need a QList back ..
QList<QVector3D> destinationVector = QList<QVector3D>::fromSet(noDublicatesSet); you will also need these things (sorry that they have been in my code for ages .. forgot that they are external) .. you may want to change the hash function:
#define ROTL10(x) (((x) << 10) | (((x) >> 22) & 0x000000ff)) #define ROTL20(x) (((x) << 20) | (((x) >> 12) & 0x0000ffff)) uint qHash(double data) { union U { quint64 n; double f; }; U u; uf = data; return uf; } inline uint qHash(const QVector3D &v, uint seed) { return qHash(vx()) ^ ROTL10(qHash(vy())) ^ ROTL20(qHash(vz())); } PS what is the code for Qt 5.0, actually for adding missing qHash () for vectors, why they did not fit in QSet / QHash by default