13 #ifndef PORTAGE_SEARCH_KDTREE_H_ 14 #define PORTAGE_SEARCH_KDTREE_H_ 22 #include "wonton/support/Point.h" 29 #define SWAP(a, b, type) { \ 78 std::vector<int>& pfound);
83 std::vector<int>& pfound);
113 int i, j, r, l, mid, ia;
121 SWAP(prm[mid], prm[l+1],
int);
123 if (arr[prm[l+1]][icut] > arr[prm[r]][icut]) {
124 SWAP(prm[l+1], prm[r],
int);
127 if (arr[prm[l]][icut] > arr[prm[r]][icut]) {
128 SWAP(prm[l], prm[r],
int);
131 if (arr[prm[l+1]][icut] > arr[prm[l]][icut]) {
132 SWAP(prm[l+1], prm[l],
int);
140 while ( arr[prm[i]][icut] < arr[ia][icut] ) i++;
143 while ( arr[prm[j]][icut] > arr[ia][icut] ) j--;
146 SWAP(prm[i], prm[j],
int);
149 while ( arr[prm[i]][icut] < arr[ia][icut] ) i++;
152 while ( arr[prm[j]][icut] > arr[ia][icut] ) j--;
158 if (j >= k) r = j - 1;
162 if ( (r-l == 1) && (arr[prm[r]][icut] < arr[prm[l]][icut]) ) {
163 SWAP(prm[l], prm[r],
int);
192 std::vector<Point<D> > bbc;
195 int node, nextp, itop;
196 int imn, imx, icut, imd;
197 int icrstack[100], imin[100], imax[100];
202 if( sboxp.empty()) std::abort();
208 int const sboxp_size = sboxp.size();
210 ipoly =
new int[sboxp_size];
212 kdtree->
linkp =
new int[2*sboxp_size];
213 bbc.resize(sboxp_size);
215 for (i = 0; i < sboxp_size; i++) {
218 bbc[i] = sboxp[i].center();
221 kdtree->
sbox[0].add(sboxp[i]);
229 if (sboxp_size == 1 ) kdtree->
linkp[0] = 0;
236 dim = kdtree->
sbox[0].getMax() - kdtree->
sbox[0].getMin();
245 for (i = 0; i < sboxp_size; i++) ipoly[i] = i;
256 imax[itop] = sboxp.size()-1;
264 MaxComponent(dim,ict[itop]);
273 node = icrstack[itop];
281 kdtree->
linkp[node] = nextp;
298 MedianSelect(imd-imn+1,imx-imn+1,bbc,&(ipoly[imn]),icut);
306 kdtree->
linkp[nextp]= -ipoly[imn];
307 kdtree->
sbox[nextp] = sboxp[ipoly[imn]];
318 kdtree->
sbox[nextp] = sboxp[ipoly[imn]];
320 for (i=imn+1; i<=imd; i++)
321 kdtree->
sbox[nextp].add(sboxp[ipoly[i]]);
327 dim = kdtree->
sbox[nextp].getMax()
328 - kdtree->
sbox[nextp].getMin();
331 icrstack[itop] = nextp;
335 MaxComponent(dim,ict[itop]);
344 if ((imd+1) == imx) {
345 kdtree->
linkp[nextp] = -ipoly[imx];
346 kdtree->
sbox[nextp] = sboxp[ipoly[imx]];
357 kdtree->
sbox[nextp] = sboxp[ipoly[imd+1]];
359 for (i=imd+2; i<=imx; i++)
360 kdtree->
sbox[nextp].add(sboxp[ipoly[i]]);
366 dim = kdtree->
sbox[nextp].getMax()
367 - kdtree->
sbox[nextp].getMin();
370 icrstack[itop] = nextp;
374 MaxComponent(dim,ict[itop]);
393 std::vector<int>& pfound)
396 int itop, node, ind, j;
401 if (kdtree->
linkp[0] <= 0) {
402 if (kdtree->
sbox[0].intersect(qp))
403 pfound.push_back(-kdtree->
linkp[0]);
414 node = istack[itop]; itop--;
416 ind = kdtree->
linkp[node];
420 for (j=0; j<=1; j++) {
421 if (kdtree->
sbox[ind+j].intersect(qp)) {
423 if (kdtree->
linkp[ind+j] <= 0) {
424 pfound.push_back(-kdtree->
linkp[ind+j]);
428 istack[itop] = ind+j;
441 std::vector<int>& pfound)
444 int itop, node, ind, j;
448 if (kdtree->
linkp[0] <= 0) {
449 if (kdtree->
sbox[0].intersect(box))
450 pfound.push_back(-kdtree->
linkp[0]);
460 node = istack[itop]; itop--;
462 ind = kdtree->
linkp[node];
466 for (j=0; j<=1; j++) {
467 if (kdtree->
sbox[ind+j].intersect(box)) {
470 if (kdtree->
linkp[ind+j] <= 0) {
471 pfound.push_back(-kdtree->
linkp[ind+j]);
475 istack[itop] = ind+j;
486 #endif // PORTAGE_SEARCH_KDTREE_H_ ~KDTree()
Default destructor.
Definition: kdtree.h:49
IsotheticBBox< D > * sbox
Definition: kdtree.h:59
std::vector< T > vector
Definition: portage.h:238
void LocatePoint(const Point< D > &qp, const KDTree< D > *kdtree, std::vector< int > &pfound)
Definition: kdtree.h:391
Definition: coredriver.h:42
size_t num_entities
Definition: kdtree.h:57
KDTree()
Default constructor.
Definition: kdtree.h:43
An N-dimensional k-d tree for manipulating polygon data.
Definition: kdtree.h:40
An isothetic (axis-aligned) N-dimensional bounding box.
Definition: BoundBox.h:46
void MedianSelect(int, int, double *, int *, int)
KDTree< D > * KDTreeCreate(const std::vector< IsotheticBBox< D > > &bbox)
Definition: kdtree.h:189
void Intersect(const IsotheticBBox< D > &box, const KDTree< D > *kdtree, std::vector< int > &pfound)
Definition: kdtree.h:439
#define SWAP(a, b, type)
Definition: kdtree.h:29
int * linkp
Definition: kdtree.h:58