-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathballtree.h
119 lines (98 loc) · 4.31 KB
/
balltree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/****************************************************************************
* MeshLab o o *
* A versatile mesh processing toolbox o o *
* _ O _ *
* Copyright(C) 2005 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef BALLTREE_H
#define BALLTREE_H
#include <vcg/space/point3.h>
#include <vcg/space/box3.h>
#include <vcg/space/index/kdtree/kdtree.h>
namespace GaelMls {
template<typename _Scalar>
class Neighborhood
{
public:
typedef _Scalar Scalar;
int index(int i) const { return mIndices.at(i); }
Scalar squaredDistance(int i) const { return mSqDists.at(i); }
void clear() { mIndices.clear(); mSqDists.clear(); }
void resize(int size) { mIndices.resize(size); mSqDists.resize(size); }
void reserve(int size) { mIndices.reserve(size); mSqDists.reserve(size); }
int size() { return mIndices.size(); }
void insert(int id, Scalar d2) { mIndices.push_back(id); mSqDists.push_back(d2); }
protected:
std::vector<int> mIndices;
std::vector<Scalar> mSqDists;
};
template<typename _Scalar>
class BallTree
{
public:
typedef _Scalar Scalar;
typedef vcg::Point3<Scalar> VectorType;
BallTree(const vcg::ConstDataWrapper<VectorType>& points, const vcg::ConstDataWrapper<Scalar>& radii);
void computeNeighbors(const VectorType& x, Neighborhood<Scalar>* pNei) const;
void setRadiusScale(Scalar v) { mRadiusScale = v; mTreeIsUptodate = false; }
protected:
struct Node
{
~Node()
{
if (!leaf)
{
delete children[0];
delete children[1];
}
else
{
delete[] indices;
}
}
Scalar splitValue;
unsigned char dim:2;
unsigned char leaf:1;
union {
Node* children[2];
struct {
unsigned int* indices;
unsigned int size;
};
};
};
typedef std::vector<int> IndexArray;
typedef vcg::Box3<Scalar> AxisAlignedBoxType;
void rebuild();
void split(const IndexArray& indices, const AxisAlignedBoxType& aabbLeft, const AxisAlignedBoxType& aabbRight,
IndexArray& iLeft, IndexArray& iRight);
void buildNode(Node& node, std::vector<int>& indices, AxisAlignedBoxType aabb, int level);
void queryNode(Node& node, Neighborhood<Scalar>* pNei) const;
protected:
vcg::ConstDataWrapper<VectorType> mPoints;
vcg::ConstDataWrapper<Scalar> mRadii;
Scalar mRadiusScale;
int mMaxTreeDepth;
int mTargetCellSize;
mutable bool mTreeIsUptodate;
mutable VectorType mQueryPosition;
Node* mRootNode;
};
}
#endif