queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
kd_dump.cpp File Reference

Go to the source code of this file.

Enumerations

enum  ANNtreeType { KD_TREE, BD_TREE }
 

Functions

static ANNkd_ptr annReadDump (istream &in, ANNtreeType tree_type, ANNpointArray &the_pts, ANNidxArray &the_pidx, int &the_dim, int &the_n_pts, int &the_bkt_size, ANNpoint &the_bnd_box_lo, ANNpoint &the_bnd_box_hi)
 
static ANNkd_ptr annReadTree (istream &in, ANNtreeType tree_type, ANNidxArray the_pidx, int &next_idx)
 

Variables

const int STRING_LEN = 500
 
const double EPSILON = 1E-5
 

Enumeration Type Documentation

Enumerator
KD_TREE 
BD_TREE 

Definition at line 46 of file kd_dump.cpp.

46 {KD_TREE, BD_TREE}; // tree types (used in loading)

Function Documentation

static ANNkd_ptr annReadDump ( istream &  in,
ANNtreeType  tree_type,
ANNpointArray the_pts,
ANNidxArray the_pidx,
int &  the_dim,
int &  the_n_pts,
int &  the_bkt_size,
ANNpoint the_bnd_box_lo,
ANNpoint the_bnd_box_hi 
)
static

Definition at line 253 of file kd_dump.cpp.

References ANNabort, annAllocPt(), annAllocPts(), annError(), annReadTree(), ANNwarn, and STRING_LEN.

263 {
264  int j;
265  char str[STRING_LEN]; // storage for string
266  char version[STRING_LEN]; // ANN version number
267  ANNkd_ptr the_root = NULL;
268 
269  //------------------------------------------------------------------
270  // Input file header
271  //------------------------------------------------------------------
272  in >> str; // input header
273  if (strcmp(str, "#ANN") != 0) { // incorrect header
274  annError("Incorrect header for dump file", ANNabort);
275  }
276  in.getline(version, STRING_LEN); // get version (ignore)
277 
278  //------------------------------------------------------------------
279  // Input the points
280  // An array the_pts is allocated and points are read from
281  // the dump file.
282  //------------------------------------------------------------------
283  in >> str; // get major heading
284  if (strcmp(str, "points") == 0) { // points section
285  in >> the_dim; // input dimension
286  in >> the_n_pts; // number of points
287  // allocate point storage
288  the_pts = annAllocPts(the_n_pts, the_dim);
289  for (int i = 0; i < the_n_pts; i++) { // input point coordinates
290  ANNidx idx; // point index
291  in >> idx; // input point index
292  if (idx < 0 || idx >= the_n_pts) {
293  annError("Point index is out of range", ANNabort);
294  }
295  for (j = 0; j < the_dim; j++) {
296  in >> the_pts[idx][j]; // read point coordinates
297  }
298  }
299  in >> str; // get next major heading
300  }
301  else { // no points were input
302  annError("Points must be supplied in the dump file", ANNabort);
303  }
304 
305  //------------------------------------------------------------------
306  // Input the tree
307  // After the basic header information, we invoke annReadTree
308  // to do all the heavy work. We create our own array of
309  // point indices (so we can pass them to annReadTree())
310  // but we do not deallocate them. They will be deallocated
311  // when the tree is destroyed.
312  //------------------------------------------------------------------
313  if (strcmp(str, "tree") == 0) { // tree section
314  in >> the_dim; // read dimension
315  in >> the_n_pts; // number of points
316  in >> the_bkt_size; // bucket size
317  the_bnd_box_lo = annAllocPt(the_dim); // allocate bounding box pts
318  the_bnd_box_hi = annAllocPt(the_dim);
319 
320  for (j = 0; j < the_dim; j++) { // read bounding box low
321  in >> the_bnd_box_lo[j];
322  }
323  for (j = 0; j < the_dim; j++) { // read bounding box low
324  in >> the_bnd_box_hi[j];
325  }
326  the_pidx = new ANNidx[the_n_pts]; // allocate point index array
327  int next_idx = 0; // number of indices filled
328  // read the tree and indices
329  the_root = annReadTree(in, tree_type, the_pidx, next_idx);
330  if (next_idx != the_n_pts) { // didn't see all the points?
331  annError("Didn't see as many points as expected", ANNwarn);
332  }
333  }
334  else {
335  annError("Illegal dump format. Expecting section heading", ANNabort);
336  }
337  return the_root;
338 }
int ANNidx
Definition: ANN.h:175
const int STRING_LEN
Definition: kd_dump.cpp:43
static ANNkd_ptr annReadTree(istream &in, ANNtreeType tree_type, ANNidxArray the_pidx, int &next_idx)
Definition: kd_dump.cpp:368
Definition: ANNx.h:48
DLL_API ANNpoint annAllocPt(int dim, ANNcoord c=0)
Definition: ANN.cpp:110
ANNkd_node * ANNkd_ptr
Definition: ANN.h:702
DLL_API ANNpointArray annAllocPts(int n, int dim)
Definition: ANN.cpp:117
void annError(const char *msg, ANNerr level)
Definition: ANN.cpp:169
Definition: ANNx.h:48
static ANNkd_ptr annReadTree ( istream &  in,
ANNtreeType  tree_type,
ANNidxArray  the_pidx,
int &  next_idx 
)
static

Definition at line 368 of file kd_dump.cpp.

References ANNabort, annError(), BD_TREE, KD_TRIVIAL, and STRING_LEN.

Referenced by annReadDump().

373 {
374  char tag[STRING_LEN]; // tag (leaf, split, shrink)
375  int n_pts; // number of points in leaf
376  int cd; // cut dimension
377  ANNcoord cv; // cut value
378  ANNcoord lb; // low bound
379  ANNcoord hb; // high bound
380  int n_bnds; // number of bounding sides
381  int sd; // which side
382 
383  in >> tag; // input node tag
384 
385  if (strcmp(tag, "null") == 0) { // null tree
386  return NULL;
387  }
388  //------------------------------------------------------------------
389  // Read a leaf
390  //------------------------------------------------------------------
391  if (strcmp(tag, "leaf") == 0) { // leaf node
392 
393  in >> n_pts; // input number of points
394  int old_idx = next_idx; // save next_idx
395  if (n_pts == 0) { // trivial leaf
396  return KD_TRIVIAL;
397  }
398  else {
399  for (int i = 0; i < n_pts; i++) { // input point indices
400  in >> the_pidx[next_idx++]; // store in array of indices
401  }
402  }
403  return new ANNkd_leaf(n_pts, &the_pidx[old_idx]);
404  }
405  //------------------------------------------------------------------
406  // Read a splitting node
407  //------------------------------------------------------------------
408  else if (strcmp(tag, "split") == 0) { // splitting node
409 
410  in >> cd >> cv >> lb >> hb;
411 
412  // read low and high subtrees
413  ANNkd_ptr lc = annReadTree(in, tree_type, the_pidx, next_idx);
414  ANNkd_ptr hc = annReadTree(in, tree_type, the_pidx, next_idx);
415  // create new node and return
416  return new ANNkd_split(cd, cv, lb, hb, lc, hc);
417  }
418  //------------------------------------------------------------------
419  // Read a shrinking node (bd-tree only)
420  //------------------------------------------------------------------
421  else if (strcmp(tag, "shrink") == 0) { // shrinking node
422  if (tree_type != BD_TREE) {
423  annError("Shrinking node not allowed in kd-tree", ANNabort);
424  }
425 
426  in >> n_bnds; // number of bounding sides
427  // allocate bounds array
428  ANNorthHSArray bds = new ANNorthHalfSpace[n_bnds];
429  for (int i = 0; i < n_bnds; i++) {
430  in >> cd >> cv >> sd; // input bounding halfspace
431  // copy to array
432  bds[i] = ANNorthHalfSpace(cd, cv, sd);
433  }
434  // read inner and outer subtrees
435  ANNkd_ptr ic = annReadTree(in, tree_type, the_pidx, next_idx);
436  ANNkd_ptr oc = annReadTree(in, tree_type, the_pidx, next_idx);
437  // create new node and return
438  return new ANNbd_shrink(n_bnds, bds, ic, oc);
439  }
440  else {
441  annError("Illegal node type in dump file", ANNabort);
442  exit(0); // to keep the compiler happy
443  }
444 }
const int STRING_LEN
Definition: kd_dump.cpp:43
double ANNcoord
Definition: ANN.h:158
static ANNkd_ptr annReadTree(istream &in, ANNtreeType tree_type, ANNidxArray the_pidx, int &next_idx)
Definition: kd_dump.cpp:368
ANNkd_leaf * KD_TRIVIAL
Definition: kd_tree.cpp:50
Definition: ANNx.h:48
ANNkd_node * ANNkd_ptr
Definition: ANN.h:702
void annError(const char *msg, ANNerr level)
Definition: ANN.cpp:169

Variable Documentation

const double EPSILON = 1E-5

Definition at line 44 of file kd_dump.cpp.

const int STRING_LEN = 500

Definition at line 43 of file kd_dump.cpp.

Referenced by annReadDump(), annReadTree(), and main().


Generated on Tue Jun 5 2018 19:48:56 for queso-0.57.1 by  doxygen 1.8.5