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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 *
 *
 * Copyright (C) 2004 Sandia Corporation.  Under the terms of Contract DE-AC04-94AL85000
 * with Sandia Corporation, the U.S. Government retains certain rights in this software.
 *
 * This file is part of facetbool--contact via [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *
 */

#include "KdTree.hpp"
#include <stack>
#include <vector>
#include <math.h>

const int XCUT = 0;
const int YCUT = 1;
const int ZCUT = 2;

KDTree::KDTree()
{
  epsilonkd = 1.e-6;
}

KDTree::~KDTree()
{
int i;
FSBoundingBox* fsbb;
 for ( i = 0; i < 2*numtris-1; i++ ) {
    fsbb = treebox[i];
    delete fsbb;
  }
  delete [] treebox;
  delete [] nextbranch; 
}

int KDTree::makeKDTree(int npoly, const FSBOXVECTOR& boxlist)
{
double *xcenterdata, *ycenterdata, *zcenterdata, *whichcut[3], *cutarray; 
int i, cuttingdir, min_tri_sequence_value, max_tri_sequence_value, 
    median_value;
double xcen, ycen, zcen;
std::vector<TreeStack* > mytreestack;
int icnt;
TreeStack *thistreestack;
FSBoundingBox *thisbox;

  numtris = npoly;
  boxlistptr = boxlist;
  isequence = new int[npoly];
  xcenterdata = new double[npoly];
  ycenterdata = new double[npoly];
  zcenterdata = new double[npoly];
  nextbranch = new int[2*npoly];
  treebox = new FSBoundingBox*[2*npoly];
  
//  Get the arrays of bounding box center points for x, y, and z.  We will use 
//  these arrays to find the median values along each direction.
  for ( i = 0; i < npoly; i++ ) {
    isequence[i] = i;
    xcen = 0.5*(boxlist[i]->xmin + boxlist[i]->xmax);
    ycen = 0.5*(boxlist[i]->ymin + boxlist[i]->ymax);
    zcen = 0.5*(boxlist[i]->zmin + boxlist[i]->zmax);
    xcenterdata[i] = xcen;
    ycenterdata[i] = ycen;
    zcenterdata[i] = zcen;  
  }
  whichcut[0] = xcenterdata; whichcut[1] = ycenterdata; whichcut[2] = zcenterdata;
  
  //  Get the bounding box of the root node -- the entire set of bounding boxes.
  thisbox = getbox(0,npoly-1);
  icnt = 0;
  treebox[icnt] = thisbox;
  nextbranch[icnt] = icnt;
   
  max_tri_sequence_value = npoly-1;
  min_tri_sequence_value = 0;
 
  if ( max_tri_sequence_value == 0 ) return 1;  //  Just one triangle in the data set.

  //  Get the cutting direction for the next branch.  
  cuttingdir = getcuttingdirection(thisbox); 
     
  thistreestack = new TreeStack(min_tri_sequence_value,
	                                   max_tri_sequence_value,
					   cuttingdir,icnt);
  icnt++;                                         
  mytreestack.push_back(thistreestack);
    
  while ( mytreestack.size() > 0 ) {
    TreeStack* thisone = mytreestack[mytreestack.size()-1];
    mytreestack.pop_back();
    cuttingdir = thisone->cuttingdir;
    min_tri_sequence_value = thisone->min;
    max_tri_sequence_value = thisone->max;


    median_value = (min_tri_sequence_value + max_tri_sequence_value)/2;
    cutarray = whichcut[cuttingdir];
    
    find_the_median(median_value-min_tri_sequence_value,0,
                    max_tri_sequence_value-min_tri_sequence_value,
                    cutarray,&isequence[min_tri_sequence_value]);
    if ( min_tri_sequence_value == median_value ) {  //  This is a leaf.
//      FSBoundingBox *thisbox = boxlist[isequence[median_value]];
      thisbox = new FSBoundingBox(
                             boxlist[isequence[median_value]]->xmin,
			     boxlist[isequence[median_value]]->ymin, 
                             boxlist[isequence[median_value]]->zmin,
			     boxlist[isequence[median_value]]->xmax,
			     boxlist[isequence[median_value]]->ymax,
			     boxlist[isequence[median_value]]->zmax);


      treebox[icnt] = thisbox;
      nextbranch[icnt] = -isequence[median_value];
      nextbranch[thisone->sequence] = icnt;
      icnt++;
    } else {
      thisbox = getbox(min_tri_sequence_value,median_value);
        nextbranch[thisone->sequence] = icnt;
      treebox[icnt] = thisbox;
//      nextbranch[icnt] = 11111;
      cuttingdir = getcuttingdirection(thisbox);

      thistreestack = new TreeStack(min_tri_sequence_value,
	                                   median_value,cuttingdir,icnt);
      icnt++;                                    
      mytreestack.push_back(thistreestack);
    } 
    //  The delete that follows is because we need to delete items that
    //  were created and placed on mytreestack.  This one comes from the pop()
    
    delete thisone;
    
    if ( median_value+1 == max_tri_sequence_value ) {  //  This is a leaf.
//      FSBoundingBox *thisbox = boxlist[isequence[max_tri_sequence_value]];
      thisbox = new FSBoundingBox(
                             boxlist[isequence[max_tri_sequence_value]]->xmin,
			     boxlist[isequence[max_tri_sequence_value]]->ymin, 
                             boxlist[isequence[max_tri_sequence_value]]->zmin,
			     boxlist[isequence[max_tri_sequence_value]]->xmax,
			     boxlist[isequence[max_tri_sequence_value]]->ymax,
			     boxlist[isequence[max_tri_sequence_value]]->zmax);


      treebox[icnt] = thisbox;
      nextbranch[icnt] = -isequence[max_tri_sequence_value];
      icnt++;
    } else {
      thisbox = getbox(median_value+1,max_tri_sequence_value);
      treebox[icnt] = thisbox;
//      nextbranch[icnt] = 22222;
      cuttingdir = getcuttingdirection(thisbox);
      thistreestack = new TreeStack(median_value+1,
	                                   max_tri_sequence_value,
					   cuttingdir,icnt);
      icnt++;                                     
      mytreestack.push_back(thistreestack);    
    }     

  } 
 
  delete [] isequence;
  delete [] xcenterdata;
  delete [] ycenterdata;
  delete [] zcenterdata;
   
  return 1;
}

FSBoundingBox* KDTree::getbox(int min, int max)
{
FSBoundingBox *thisbox;
int i;
//  Makes a bounding box and sets its size to include all of the boxes in the
//  sequence of bounding boxes from min to max of index isequence[].  Returns pointer
//  to this box.

  thisbox = new FSBoundingBox(1.e20,1.e20,1.e20,-1.e20,-1.e20,-1.e20);
double xmin, ymin, zmin, xmax, ymax, zmax;
  
  for ( i = min; i <= max; i++ ) {
    xmin = boxlistptr[isequence[i]]->xmin;
    ymin = boxlistptr[isequence[i]]->ymin;
    zmin = boxlistptr[isequence[i]]->zmin;
    xmax = boxlistptr[isequence[i]]->xmax;
    ymax = boxlistptr[isequence[i]]->ymax;
    zmax = boxlistptr[isequence[i]]->zmax;

    if ( xmin < thisbox->xmin ) thisbox->xmin = xmin;
    if ( ymin < thisbox->ymin ) thisbox->ymin = ymin;
    if ( zmin < thisbox->zmin ) thisbox->zmin = zmin;    
    if ( xmax > thisbox->xmax ) thisbox->xmax = xmax;
    if ( ymax > thisbox->ymax ) thisbox->ymax = ymax;
    if ( zmax > thisbox->zmax ) thisbox->zmax = zmax;
     
  }

  return thisbox;
}

void KDTree::box_kdtree_intersect(FSBoundingBox& bbox, int *count, int *indexlist) const
{
int index, i, child;
std::stack<int> mystack;

  *count = 0;
  if ( (bbox.xmax < (treebox[0]->xmin - epsilonkd) ) ||
       (bbox.xmin > (treebox[0]->xmax + epsilonkd) ) ||
       (bbox.ymax < (treebox[0]->ymin - epsilonkd) ) ||
       (bbox.ymin > (treebox[0]->ymax + epsilonkd) ) ||
       (bbox.zmax < (treebox[0]->zmin - epsilonkd) ) ||
       (bbox.zmin > (treebox[0]->zmax + epsilonkd) ) )
    return;
  //  Gotta put something on the stack, so do the root node 
  //  evaluation here.
  if ( nextbranch[0] <= 0 ) {
    indexlist[*count] = -nextbranch[0]; *count += 1;
    return;
  }
  mystack.push(0);

  while ( mystack.size() > 0 ) {
    index = mystack.top();
    mystack.pop();
    child = nextbranch[index];
    
    for ( i = 0; i < 2; i++ ) {
      if ( (bbox.xmax < (treebox[child+i]->xmin - epsilonkd) ) ||
           (bbox.xmin > (treebox[child+i]->xmax + epsilonkd) ) ||
           (bbox.ymax < (treebox[child+i]->ymin - epsilonkd) ) ||
           (bbox.ymin > (treebox[child+i]->ymax + epsilonkd) ) ||
           (bbox.zmax < (treebox[child+i]->zmin - epsilonkd) ) ||
           (bbox.zmin > (treebox[child+i]->zmax + epsilonkd) ) )
      continue;
      if ( nextbranch[child+i] <= 0 ) {        
		indexlist[*count] = -nextbranch[child+i]; *count += 1;	
      } else {            
        mystack.push(child+i);   
      }  
    }
  }  
  return;
}


void KDTree::find_the_median(int k, int l, int r, double *array, int *ia)
{
int i, j;
double t;

  while ( r > l ) {
    t = array[ia[k]];
    i = l;
    j = r;

    SWAP(ia[l],ia[k]);
    if ( array[ia[r]] > t ) SWAP(ia[l],ia[r]);
    while ( i < j ) {
      SWAP(ia[i],ia[j]);
      i += 1; j -= 1;
      while ( array[ia[i]] < t ) i++;
      while ( array[ia[j]] > t ) j--;
    }
    if ( array[ia[l]] == t ) SWAP(ia[l],ia[j]);
    else {
      j += 1;
      SWAP(ia[r],ia[j]);
    }
    if ( j <= k ) l = j + 1;
    if ( k <= j ) r = j - 1;
  }

}

int KDTree::getcuttingdirection(FSBoundingBox* box)
{
double xlen, ylen, zlen;

  xlen = box->xmax - box->xmin;
  ylen = box->ymax - box->ymin;
  zlen = box->zmax - box->zmin;
  
  if ( (xlen >= ylen) && (xlen >= zlen) ) return XCUT; 
  else if ( ylen >= zlen ) return YCUT;
  else return ZCUT;
}


bool KDTree::rayintersectsbox(FSBoundingBox *box)
{
double pmin, pmax, tmin, tmax;
double dtemp;

//  Get the parametric distance along each direction from the min point to
//  the min and max box planes.  If the min dist is grater than the max
//  dist, we have to swap them.  Keep a running total of the max min and the
//  min max.  The ray intersects the box iff tmin <= tmax and tmax >= 0.0.
//  Otherwise, the ray misses the box or points away from the box (with the
//  starting point outside).

  tmin = (box->xmin - rayxstart)/dx;
  tmax = (box->xmax - rayxstart)/dx;
  if ( tmin > tmax ) {
  dtemp = tmin; tmin = tmax; tmax = dtemp;
  }
  pmin = (box->ymin - rayystart)/dy;
  pmax = (box->ymax - rayystart)/dy;
  if ( pmin > pmax ) {
  dtemp = pmin; pmin = pmax; pmax = dtemp;
  }
  tmin = MAXX(pmin,tmin);
  tmax = MINN(pmax,tmax);

  if ( tmin > tmax ) return false;
  
  pmin = (box->zmin - rayzstart)/dz;
  pmax = (box->zmax - rayzstart)/dz;
  if ( pmin > pmax ) {
  dtemp = pmin; pmin = pmax; pmax = dtemp;
  }
  tmin = MAXX(pmin,tmin);
  tmax = MINN(pmax,tmax);
  
  if ( (tmax < 0.0) || (tmin > tmax) ) return false;
  
  return true;
}