} // end spread(); /// search kd-tree starting at *node, looking for overlap with bb, and placing /// found objects in *tris protected void search_node(LinkedList <Triangle> tris, Bbox bb, KDNode <Triangle> node) { if (node.isLeaf) { // we found a bucket node, so add all triangles and return. foreach (Triangle t in node.tris) { tris.AddLast(t); } //std::cout << " search_node Leaf bucket tris-size() = " << tris->size() << "\n"; return; // end recursion } else if ((node.dim % 2) == 0) { // cutting along a min-direction: 0, 2, 4 // not a bucket node, so recursevily search hi/lo branches of KDNode uint maxdim = (uint)(node.dim + 1); if (node.cutval > bb[maxdim]) { // search only lo search_node(tris, bb, node.lo); } else { // need to search both child nodes if (node.hi != null) { search_node(tris, bb, node.hi); } if (node.lo != null) { search_node(tris, bb, node.lo); } } } else { // cutting along a max-dimension: 1,3,5 uint mindim = (uint)(node.dim - 1); if (node.cutval < bb[mindim]) { // search only hi search_node(tris, bb, node.hi); } else { // need to search both child nodes if (node.hi != null) { search_node(tris, bb, node.hi); } if (node.lo != null) { search_node(tris, bb, node.lo); } } } return; // Done. We get here after all the recursive calls above. } // end search_kdtree();
/// Create a node which partitions(cuts) along dimension d, at /// cut value cv, with child-nodes hi_c and lo_c. /// If this is a bucket-node containing triangles, /// they are in the list tris /// depth indicates the depth of the node in the tree public KDNode(int d, double cv, KDNode <Triangle> parentNode, KDNode <Triangle> hi_child, KDNode <Triangle> lo_child, LinkedList <Triangle> tlist, int nodeDepth) // depth of node { dim = d; cutval = cv; parent = parentNode; hi = hi_child; lo = lo_child; tris = new LinkedList <Triangle>(); depth = nodeDepth; isLeaf = false; if (tlist != null) { isLeaf = true; foreach (Triangle bo in tlist) { tris.AddLast(bo); } } }
/// string repr //C++ TO C# CONVERTER WARNING: 'const' methods are not available in C#: //ORIGINAL LINE: string str() const; //C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found: // string str(); /// build and return a KDNode containing list *tris at depth dep. protected KDNode <Triangle> build_node(LinkedList <Triangle> tris, int dep, KDNode <Triangle> par) { // parent node //std::cout << "KDNode::build_node list.size()=" << tris->size() << "\n"; if (tris.Count == 0) { //this is a fatal error. Console.Write("ERROR: KDTree::build_node() called with tris->size()==0 !"); Debug.Assert(false); return(null); } Spread spr = calc_spread(tris); // calculate spread in order to know how to cut double cutvalue = spr.start + spr.val / 2; // cut in the middle //std::cout << " cutvalue= " << cutvalue << "\n"; if ((tris.Count <= bucketSize) || GlobalMembers.isZero_tol(spr.val)) { // then return a bucket/leaf node //std::cout << "KDNode::build_node BUCKET list.size()=" << tris->size() << "\n"; KDNode <Triangle> bucket; // dim cutv parent hi lo triangles depth bucket = new KDNode <Triangle>(spr.d, cutvalue, par, null, null, tris, dep); Debug.Assert(bucket.isLeaf); spr = null; return(bucket); // this is the leaf/end of the recursion-tree } // build lists of triangles for hi and lo child nodes LinkedList <Triangle> lolist = new LinkedList <Triangle>(); LinkedList <Triangle> hilist = new LinkedList <Triangle>(); foreach (Triangle t in tris) { // loop through each triangle and put it in either lolist or hilist if (t.bb[spr.d] > cutvalue) { hilist.AddLast(t); } else { lolist.AddLast(t); } } /* * if (hilist->empty() || lolist->empty()) {// an error ?? * std::cout << "kdtree: hilist.size()==0! or lolist.size()==0! \n"; * std::cout << "kdtree: tris->size()= " << tris->size()<< "\n"; * std::cout << "kdtree: hilist.size()= " << hilist->size()<< "\n"; * std::cout << "kdtree: lolist.size()= " << lolist->size()<< "\n"; * BOOST_FOREACH(Triangle t, *tris) { * std::cout << t << "\n"; * std::cout << t.bb << "\n"; * } * std::cout << "kdtree: spr->d= " << spr->d << "\n"; * std::cout << "kdtree: cutvalue= " << cutvalue << "\n"; * assert(0); * }*/ // create the current node dim value parent hi lo trilist depth KDNode <Triangle> node = new KDNode <Triangle>(spr.d, cutvalue, par, null, null, null, dep); // create the child-nodes through recursion // list depth parent if (hilist.Count > 0) { node.hi = build_node(hilist, dep + 1, node); } //else //std::cout << "hilist empty!\n"; if (lolist.Count > 0) { node.lo = build_node(lolist, dep + 1, node); } else { //std::cout << "lolist empty!\n"; } lolist.Clear(); hilist.Clear(); spr = null; lolist = null; hilist = null; return(node); // return a new node }