Added a VectorConstDataWrapper to simply create a kdtree from a vector of point3f

This commit is contained in:
Paolo Cignoni 2014-04-17 09:50:21 +00:00
parent dbe0d2b7f5
commit 03206b6bc4
1 changed files with 65 additions and 56 deletions

View File

@ -13,22 +13,31 @@ template<typename _DataType>
class ConstDataWrapper class ConstDataWrapper
{ {
public: public:
typedef _DataType DataType; typedef _DataType DataType;
inline ConstDataWrapper() inline ConstDataWrapper()
: mpData(0), mStride(0), mSize(0) : mpData(0), mStride(0), mSize(0)
{} {}
inline ConstDataWrapper(const DataType* pData, int size, int stride = sizeof(DataType)) inline ConstDataWrapper(const DataType* pData, int size, int stride = sizeof(DataType))
: mpData(reinterpret_cast<const unsigned char*>(pData)), mStride(stride), mSize(size) : mpData(reinterpret_cast<const unsigned char*>(pData)), mStride(stride), mSize(size)
{} {}
inline const DataType& operator[] (int i) const inline const DataType& operator[] (int i) const
{ {
return *reinterpret_cast<const DataType*>(mpData + i*mStride); return *reinterpret_cast<const DataType*>(mpData + i*mStride);
} }
inline size_t size() const { return mSize; } inline size_t size() const { return mSize; }
protected: protected:
const unsigned char* mpData; const unsigned char* mpData;
int mStride; int mStride;
size_t mSize; size_t mSize;
};
template<class StdVectorType>
class VectorConstDataWrapper :public ConstDataWrapper<typename StdVectorType::value_type>
{
public:
inline VectorConstDataWrapper(StdVectorType &vec):
ConstDataWrapper<typename StdVectorType::value_type> ( &(vec[0]), vec.size(), sizeof(typename StdVectorType::value_type))
{}
}; };
template<class MeshType> template<class MeshType>
@ -48,64 +57,64 @@ class KdTree
{ {
public: public:
typedef _Scalar Scalar; typedef _Scalar Scalar;
typedef vcg::Point3<Scalar> VectorType; typedef vcg::Point3<Scalar> VectorType;
typedef vcg::Box3<Scalar> AxisAlignedBoxType; typedef vcg::Box3<Scalar> AxisAlignedBoxType;
struct Node struct Node
{ {
union { union {
//standard node //standard node
struct { struct {
Scalar splitValue; Scalar splitValue;
unsigned int firstChildId:24; unsigned int firstChildId:24;
unsigned int dim:2; unsigned int dim:2;
unsigned int leaf:1; unsigned int leaf:1;
}; };
//leaf //leaf
struct { struct {
unsigned int start; unsigned int start;
unsigned short size; unsigned short size;
}; };
}; };
}; };
typedef std::vector<Node> NodeList; typedef std::vector<Node> NodeList;
// return the protected members which store the nodes and the points list // return the protected members which store the nodes and the points list
inline const NodeList& _getNodes(void) { return mNodes; } inline const NodeList& _getNodes(void) { return mNodes; }
inline const std::vector<VectorType>& _getPoints(void) { return mPoints; } inline const std::vector<VectorType>& _getPoints(void) { return mPoints; }
void setMaxNofNeighbors(unsigned int k); void setMaxNofNeighbors(unsigned int k);
inline int getNofFoundNeighbors(void) { return mNeighborQueue.getNofElements(); } inline int getNofFoundNeighbors(void) { return mNeighborQueue.getNofElements(); }
inline const VectorType& getNeighbor(int i) { return mPoints[ mNeighborQueue.getIndex(i) ]; } inline const VectorType& getNeighbor(int i) { return mPoints[ mNeighborQueue.getIndex(i) ]; }
inline unsigned int getNeighborId(int i) { return mIndices[mNeighborQueue.getIndex(i)]; } inline unsigned int getNeighborId(int i) { return mIndices[mNeighborQueue.getIndex(i)]; }
inline float getNeighborSquaredDistance(int i) { return mNeighborQueue.getWeight(i); } inline float getNeighborSquaredDistance(int i) { return mNeighborQueue.getWeight(i); }
public: public:
KdTree(const ConstDataWrapper<VectorType>& points, unsigned int nofPointsPerCell = 16, unsigned int maxDepth = 64); KdTree(const ConstDataWrapper<VectorType>& points, unsigned int nofPointsPerCell = 16, unsigned int maxDepth = 64);
~KdTree(); ~KdTree();
void doQueryK(const VectorType& p); void doQueryK(const VectorType& p);
protected: protected:
// element of the stack // element of the stack
struct QueryNode struct QueryNode
{ {
QueryNode() {} QueryNode() {}
QueryNode(unsigned int id) : nodeId(id) {} QueryNode(unsigned int id) : nodeId(id) {}
unsigned int nodeId; // id of the next node unsigned int nodeId; // id of the next node
Scalar sq; // squared distance to the next node Scalar sq; // squared distance to the next node
}; };
// used to build the tree: split the subset [start..end[ according to dim and splitValue, // used to build the tree: split the subset [start..end[ according to dim and splitValue,
// and returns the index of the first element of the second subset // and returns the index of the first element of the second subset
unsigned int split(int start, int end, unsigned int dim, float splitValue); unsigned int split(int start, int end, unsigned int dim, float splitValue);
void createTree(unsigned int nodeId, unsigned int start, unsigned int end, unsigned int level, unsigned int targetCellsize, unsigned int targetMaxDepth); void createTree(unsigned int nodeId, unsigned int start, unsigned int end, unsigned int level, unsigned int targetCellsize, unsigned int targetMaxDepth);
protected: protected: