00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <stdlib.h>
00043 #include <math.h>
00044 #include <algorithm>
00045
00046
00047 namespace pcl
00048 {
00049 namespace poisson
00050 {
00051
00053
00055 template<class NodeData, class Real> const int OctNode<NodeData, Real>::DepthShift = 5;
00056 template<class NodeData, class Real> const int OctNode<NodeData, Real>::OffsetShift = 19;
00057 template<class NodeData, class Real> const int OctNode<NodeData, Real>::DepthMask = (1 << DepthShift) - 1;
00058 template<class NodeData, class Real> const int OctNode<NodeData, Real>::OffsetMask = (1 << OffsetShift) - 1;
00059 template<class NodeData, class Real> const int OctNode<NodeData, Real>::OffsetShift1 = DepthShift;
00060 template<class NodeData, class Real> const int OctNode<NodeData, Real>::OffsetShift2 = OffsetShift1 + OffsetShift;
00061 template<class NodeData, class Real> const int OctNode<NodeData, Real>::OffsetShift3 = OffsetShift2 + OffsetShift;
00062
00063 template<class NodeData, class Real> int OctNode<NodeData, Real>::UseAlloc = 0;
00064 template<class NodeData, class Real> Allocator<OctNode<NodeData, Real> > OctNode<NodeData, Real>::AllocatorOctNode;
00065
00067 template<class NodeData, class Real> void
00068 OctNode<NodeData, Real>::SetAllocator (int blockSize)
00069 {
00070 if (blockSize > 0)
00071 {
00072 UseAlloc = 1;
00073 AllocatorOctNode.set (blockSize);
00074 }
00075 else
00076 UseAlloc = 0;
00077 }
00078
00080 template<class NodeData, class Real> int
00081 OctNode<NodeData, Real>::UseAllocator (void)
00082 {
00083 return (UseAlloc);
00084 }
00085
00087 template <class NodeData, class Real>
00088 OctNode<NodeData, Real>::OctNode () : parent (NULL), children (NULL), d (0), off (), nodeData ()
00089 {
00090 off[0] = off[1] = off[2] = 0;
00091 }
00092
00094 template <class NodeData, class Real>
00095 OctNode<NodeData, Real>::~OctNode ()
00096 {
00097 if (!UseAlloc)
00098 {
00099 if (children)
00100 {
00101 delete[] children;
00102 }
00103 }
00104 parent = children = NULL;
00105 }
00106
00108 template <class NodeData, class Real> void
00109 OctNode<NodeData, Real>::setFullDepth (const int& maxDepth)
00110 {
00111 if (maxDepth)
00112 {
00113 if (!children)
00114 {
00115 initChildren ();
00116 }
00117 for (int i = 0; i < 8; i++)
00118 {
00119 children[i].setFullDepth (maxDepth - 1);
00120 }
00121 }
00122 }
00123
00124 template <class NodeData, class Real>
00125 int OctNode<NodeData, Real>
00126 ::initChildren (void)
00127 {
00128 int i, j, k;
00129
00130 if (UseAlloc)
00131 {
00132 children = AllocatorOctNode.newElements (8);
00133 }
00134 else
00135 {
00136 if (children)
00137 {
00138 delete[] children;
00139 }
00140 children = NULL;
00141 children = new OctNode[Cube::CORNERS];
00142 }
00143 if (!children)
00144 {
00145 fprintf (stderr, "Failed to initialize children in OctNode::initChildren\n");
00146 exit (0);
00147 return 0;
00148 }
00149 int d, off[3];
00150 depthAndOffset (d, off);
00151 for (i = 0; i < 2; i++)
00152 {
00153 for (j = 0; j < 2; j++)
00154 {
00155 for (k = 0; k < 2; k++)
00156 {
00157 int idx = Cube::CornerIndex (i, j, k);
00158 children[idx].parent = this;
00159 children[idx].children = NULL;
00160 int off2[3];
00161 off2[0] = (off[0] << 1) + i;
00162 off2[1] = (off[1] << 1) + j;
00163 off2[2] = (off[2] << 1) + k;
00164 Index (d + 1, off2, children[idx].d, children[idx].off);
00165 }
00166 }
00167 }
00168 return 1;
00169 }
00170
00171 template <class NodeData, class Real>
00172 inline void OctNode<NodeData, Real>
00173 ::Index (const int& depth, const int offset[3], short& d, short off[3])
00174 {
00175 d = short(depth);
00176 off[0] = short((1 << depth) + offset[0] - 1);
00177 off[1] = short((1 << depth) + offset[1] - 1);
00178 off[2] = short((1 << depth) + offset[2] - 1);
00179 }
00180
00181 template<class NodeData, class Real>
00182 inline void OctNode<NodeData, Real>
00183 ::depthAndOffset (int& depth, int offset[3]) const
00184 {
00185 depth = int(d);
00186 offset[0] = (int(off[0]) + 1)&(~(1 << depth));
00187 offset[1] = (int(off[1]) + 1)&(~(1 << depth));
00188 offset[2] = (int(off[2]) + 1)&(~(1 << depth));
00189 }
00190
00191 template<class NodeData, class Real>
00192 inline int OctNode<NodeData, Real>
00193 ::depth (void) const
00194 {
00195 return int(d);
00196 }
00197
00198 template<class NodeData, class Real>
00199 inline void OctNode<NodeData, Real>
00200 ::DepthAndOffset (const long long& index, int& depth, int offset[3])
00201 {
00202 depth = int(index & DepthMask);
00203 offset[0] = (int((index >> OffsetShift1) & OffsetMask) + 1)&(~(1 << depth));
00204 offset[1] = (int((index >> OffsetShift2) & OffsetMask) + 1)&(~(1 << depth));
00205 offset[2] = (int((index >> OffsetShift3) & OffsetMask) + 1)&(~(1 << depth));
00206 }
00207
00208 template<class NodeData, class Real>
00209 inline int OctNode<NodeData, Real>
00210 ::Depth (const long long& index)
00211 {
00212 return int(index & DepthMask);
00213 }
00214
00215 template <class NodeData, class Real>
00216 void OctNode<NodeData, Real>
00217 ::centerAndWidth (Point3D<Real>& center, Real& width) const
00218 {
00219 int depth, offset[3];
00220 depth = int(d);
00221 offset[0] = (int(off[0]) + 1)&(~(1 << depth));
00222 offset[1] = (int(off[1]) + 1)&(~(1 << depth));
00223 offset[2] = (int(off[2]) + 1)&(~(1 << depth));
00224 width = Real (1.0 / (1 << depth));
00225 for (int dim = 0; dim < DIMENSION; dim++)
00226 {
00227 center.coords[dim] = Real (0.5 + offset[dim]) * width;
00228 }
00229 }
00230
00231 template <class NodeData, class Real>
00232 inline void OctNode<NodeData, Real>
00233 ::CenterAndWidth (const long long& index, Point3D<Real>& center, Real& width)
00234 {
00235 int depth, offset[3];
00236 depth = index&DepthMask;
00237 offset[0] = (int((index >> OffsetShift1) & OffsetMask) + 1)&(~(1 << depth));
00238 offset[1] = (int((index >> OffsetShift2) & OffsetMask) + 1)&(~(1 << depth));
00239 offset[2] = (int((index >> OffsetShift3) & OffsetMask) + 1)&(~(1 << depth));
00240 width = Real (1.0 / (1 << depth));
00241 for (int dim = 0; dim < DIMENSION; dim++)
00242 {
00243 center.coords[dim] = Real (0.5 + offset[dim]) * width;
00244 }
00245 }
00246
00247 template <class NodeData, class Real>
00248 int OctNode<NodeData, Real>
00249 ::maxDepth (void) const
00250 {
00251 if (!children)
00252 {
00253 return 0;
00254 }
00255 else
00256 {
00257 int c = 0, d;
00258 for (int i = 0; i < Cube::CORNERS; i++)
00259 {
00260 d = children[i].maxDepth ();
00261 if (!i || d > c)
00262 {
00263 c = d;
00264 }
00265 }
00266 return c + 1;
00267 }
00268 }
00269
00270 template <class NodeData, class Real>
00271 int OctNode<NodeData, Real>
00272 ::nodes (void) const
00273 {
00274 if (!children)
00275 {
00276 return 1;
00277 }
00278 else
00279 {
00280 int c = 0;
00281 for (int i = 0; i < Cube::CORNERS; i++)
00282 {
00283 c += children[i].nodes ();
00284 }
00285 return c + 1;
00286 }
00287 }
00288
00289 template <class NodeData, class Real>
00290 int OctNode<NodeData, Real>
00291 ::leaves (void) const
00292 {
00293 if (!children)
00294 {
00295 return 1;
00296 }
00297 else
00298 {
00299 int c = 0;
00300 for (int i = 0; i < Cube::CORNERS; i++)
00301 {
00302 c += children[i].leaves ();
00303 }
00304 return c;
00305 }
00306 }
00307
00308 template<class NodeData, class Real>
00309 int OctNode<NodeData, Real>
00310 ::maxDepthLeaves (const int& maxDepth) const
00311 {
00312 if (depth () > maxDepth)
00313 {
00314 return 0;
00315 }
00316 if (!children)
00317 {
00318 return 1;
00319 }
00320 else
00321 {
00322 int c = 0;
00323 for (int i = 0; i < Cube::CORNERS; i++)
00324 {
00325 c += children[i].maxDepthLeaves (maxDepth);
00326 }
00327 return c;
00328 }
00329 }
00330
00331 template <class NodeData, class Real>
00332 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
00333 ::root (void) const
00334 {
00335 const OctNode* temp = this;
00336 while (temp->parent)
00337 {
00338 temp = temp->parent;
00339 }
00340 return temp;
00341 }
00342
00343 template <class NodeData, class Real>
00344 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
00345 ::nextBranch (const OctNode* current) const
00346 {
00347 if (!current->parent || current == this)
00348 {
00349 return NULL;
00350 }
00351 if (current - current->parent->children == Cube::CORNERS - 1)
00352 {
00353 return nextBranch (current->parent);
00354 }
00355 else
00356 {
00357 return current + 1;
00358 }
00359 }
00360
00361 template <class NodeData, class Real>
00362 OctNode<NodeData, Real>* OctNode<NodeData, Real>
00363 ::nextBranch (OctNode* current)
00364 {
00365 if (!current->parent || current == this)
00366 {
00367 return NULL;
00368 }
00369 if (current - current->parent->children == Cube::CORNERS - 1)
00370 {
00371 return nextBranch (current->parent);
00372 }
00373 else
00374 {
00375 return current + 1;
00376 }
00377 }
00378
00379 template <class NodeData, class Real>
00380 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
00381 ::nextLeaf (const OctNode* current) const
00382 {
00383 if (!current)
00384 {
00385 const OctNode<NodeData, Real>* temp = this;
00386 while (temp->children)
00387 {
00388 temp = &temp->children[0];
00389 }
00390 return temp;
00391 }
00392 if (current->children)
00393 {
00394 return current->nextLeaf ();
00395 }
00396 const OctNode* temp = nextBranch (current);
00397 if (!temp)
00398 {
00399 return NULL;
00400 }
00401 else
00402 {
00403 return temp->nextLeaf ();
00404 }
00405 }
00406
00407 template <class NodeData, class Real>
00408 OctNode<NodeData, Real>* OctNode<NodeData, Real>
00409 ::nextLeaf (OctNode* current)
00410 {
00411 if (!current)
00412 {
00413 OctNode<NodeData, Real>* temp = this;
00414 while (temp->children)
00415 {
00416 temp = &temp->children[0];
00417 }
00418 return temp;
00419 }
00420 if (current->children)
00421 {
00422 return current->nextLeaf ();
00423 }
00424 OctNode* temp = nextBranch (current);
00425 if (!temp)
00426 {
00427 return NULL;
00428 }
00429 else
00430 {
00431 return temp->nextLeaf ();
00432 }
00433 }
00434
00435 template <class NodeData, class Real>
00436 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
00437 ::nextNode (const OctNode* current) const
00438 {
00439 if (!current)
00440 {
00441 return this;
00442 }
00443 else if (current->children)
00444 {
00445 return ¤t->children[0];
00446 }
00447 else
00448 {
00449 return nextBranch (current);
00450 }
00451 }
00452
00453 template <class NodeData, class Real>
00454 OctNode<NodeData, Real>* OctNode<NodeData, Real>
00455 ::nextNode (OctNode* current)
00456 {
00457 if (!current)
00458 {
00459 return this;
00460 }
00461 else if (current->children)
00462 {
00463 return ¤t->children[0];
00464 }
00465 else
00466 {
00467 return nextBranch (current);
00468 }
00469 }
00470
00471 template <class NodeData, class Real>
00472 void OctNode<NodeData, Real>
00473 ::printRange (void) const
00474 {
00475 Point3D<Real> center;
00476 Real width;
00477 centerAndWidth (center, width);
00478 for (int dim = 0; dim < DIMENSION; dim++)
00479 {
00480 printf ("%[%f,%f]", center.coords[dim] - width / 2, center.coords[dim] + width / 2);
00481 if (dim < DIMENSION - 1)
00482 {
00483 printf ("x");
00484 }
00485 else printf ("\n");
00486 }
00487 }
00488
00489 template <class NodeData, class Real>
00490 void OctNode<NodeData, Real>
00491 ::AdjacencyCountFunction::Function (const OctNode<NodeData, Real>* node1, const OctNode<NodeData, Real>* node2)
00492 {
00493 count++;
00494 }
00495
00496 template <class NodeData, class Real>
00497 template<class NodeAdjacencyFunction>
00498 void OctNode<NodeData, Real>
00499 ::processNodeNodes (OctNode* node, NodeAdjacencyFunction* F, const int& processCurrent)
00500 {
00501 if (processCurrent)
00502 {
00503 F->Function (this, node);
00504 }
00505 if (children)
00506 {
00507 __processNodeNodes (node, F);
00508 }
00509 }
00510
00511 template <class NodeData, class Real>
00512 template<class NodeAdjacencyFunction>
00513 void OctNode<NodeData, Real>
00514 ::processNodeFaces (OctNode* node, NodeAdjacencyFunction* F, const int& fIndex, const int& processCurrent)
00515 {
00516 if (processCurrent)
00517 {
00518 F->Function (this, node);
00519 }
00520 if (children)
00521 {
00522 int c1, c2, c3, c4;
00523 Cube::FaceCorners (fIndex, c1, c2, c3, c4);
00524 __processNodeFaces (node, F, c1, c2, c3, c4);
00525 }
00526 }
00527
00528 template <class NodeData, class Real>
00529 template<class NodeAdjacencyFunction>
00530 void OctNode<NodeData, Real>
00531 ::processNodeEdges (OctNode* node, NodeAdjacencyFunction* F, const int& eIndex, const int& processCurrent)
00532 {
00533 if (processCurrent)
00534 {
00535 F->Function (this, node);
00536 }
00537 if (children)
00538 {
00539 int c1, c2;
00540 Cube::EdgeCorners (eIndex, c1, c2);
00541 __processNodeEdges (node, F, c1, c2);
00542 }
00543 }
00544
00545 template <class NodeData, class Real>
00546 template<class NodeAdjacencyFunction>
00547 void OctNode<NodeData, Real>
00548 ::processNodeCorners (OctNode* node, NodeAdjacencyFunction* F, const int& cIndex, const int& processCurrent)
00549 {
00550 if (processCurrent)
00551 {
00552 F->Function (this, node);
00553 }
00554 OctNode<NodeData, Real>* temp = this;
00555 while (temp->children)
00556 {
00557 temp = &temp->children[cIndex];
00558 F->Function (temp, node);
00559 }
00560 }
00561
00562 template <class NodeData, class Real>
00563 template<class NodeAdjacencyFunction>
00564 void OctNode<NodeData, Real>
00565 ::__processNodeNodes (OctNode* node, NodeAdjacencyFunction* F)
00566 {
00567 F->Function (&children[0], node);
00568 F->Function (&children[1], node);
00569 F->Function (&children[2], node);
00570 F->Function (&children[3], node);
00571 F->Function (&children[4], node);
00572 F->Function (&children[5], node);
00573 F->Function (&children[6], node);
00574 F->Function (&children[7], node);
00575 if (children[0].children)
00576 {
00577 children[0].__processNodeNodes (node, F);
00578 }
00579 if (children[1].children)
00580 {
00581 children[1].__processNodeNodes (node, F);
00582 }
00583 if (children[2].children)
00584 {
00585 children[2].__processNodeNodes (node, F);
00586 }
00587 if (children[3].children)
00588 {
00589 children[3].__processNodeNodes (node, F);
00590 }
00591 if (children[4].children)
00592 {
00593 children[4].__processNodeNodes (node, F);
00594 }
00595 if (children[5].children)
00596 {
00597 children[5].__processNodeNodes (node, F);
00598 }
00599 if (children[6].children)
00600 {
00601 children[6].__processNodeNodes (node, F);
00602 }
00603 if (children[7].children)
00604 {
00605 children[7].__processNodeNodes (node, F);
00606 }
00607 }
00608
00609 template <class NodeData, class Real>
00610 template<class NodeAdjacencyFunction>
00611 void OctNode<NodeData, Real>
00612 ::__processNodeEdges (OctNode* node, NodeAdjacencyFunction* F, const int& cIndex1, const int& cIndex2)
00613 {
00614 F->Function (&children[cIndex1], node);
00615 F->Function (&children[cIndex2], node);
00616 if (children[cIndex1].children)
00617 {
00618 children[cIndex1].__processNodeEdges (node, F, cIndex1, cIndex2);
00619 }
00620 if (children[cIndex2].children)
00621 {
00622 children[cIndex2].__processNodeEdges (node, F, cIndex1, cIndex2);
00623 }
00624 }
00625
00626 template <class NodeData, class Real>
00627 template<class NodeAdjacencyFunction>
00628 void OctNode<NodeData, Real>
00629 ::__processNodeFaces (OctNode* node, NodeAdjacencyFunction* F, const int& cIndex1, const int& cIndex2, const int& cIndex3, const int& cIndex4)
00630 {
00631 F->Function (&children[cIndex1], node);
00632 F->Function (&children[cIndex2], node);
00633 F->Function (&children[cIndex3], node);
00634 F->Function (&children[cIndex4], node);
00635 if (children[cIndex1].children)
00636 {
00637 children[cIndex1].__processNodeFaces (node, F, cIndex1, cIndex2, cIndex3, cIndex4);
00638 }
00639 if (children[cIndex2].children)
00640 {
00641 children[cIndex2].__processNodeFaces (node, F, cIndex1, cIndex2, cIndex3, cIndex4);
00642 }
00643 if (children[cIndex3].children)
00644 {
00645 children[cIndex3].__processNodeFaces (node, F, cIndex1, cIndex2, cIndex3, cIndex4);
00646 }
00647 if (children[cIndex4].children)
00648 {
00649 children[cIndex4].__processNodeFaces (node, F, cIndex1, cIndex2, cIndex3, cIndex4);
00650 }
00651 }
00652
00653 template<class NodeData, class Real>
00654 template<class NodeAdjacencyFunction>
00655 void OctNode<NodeData, Real>
00656 ::ProcessNodeAdjacentNodes (const int& maxDepth, OctNode* node1, const int& width1, OctNode* node2, const int& width2, NodeAdjacencyFunction* F, const int& processCurrent)
00657 {
00658 int c1[3], c2[3], w1, w2;
00659 node1->centerIndex (maxDepth + 1, c1);
00660 node2->centerIndex (maxDepth + 1, c2);
00661 w1 = node1->width (maxDepth + 1);
00662 w2 = node2->width (maxDepth + 1);
00663
00664 ProcessNodeAdjacentNodes (c1[0] - c2[0], c1[1] - c2[1], c1[2] - c2[2], node1, (width1 * w1) >> 1, node2, (width2 * w2) >> 1, w2, F, processCurrent);
00665 }
00666
00667 template<class NodeData, class Real>
00668 template<class NodeAdjacencyFunction>
00669 void OctNode<NodeData, Real>
00670 ::ProcessNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
00671 OctNode<NodeData, Real>* node1, const int& radius1,
00672 OctNode<NodeData, Real>* node2, const int& radius2, const int& width2,
00673 NodeAdjacencyFunction* F, const int& processCurrent)
00674 {
00675 if (!Overlap (dx, dy, dz, radius1 + radius2))
00676 {
00677 return;
00678 }
00679 if (processCurrent)
00680 {
00681 F->Function (node2, node1);
00682 }
00683 if (!node2->children)
00684 {
00685 return;
00686 }
00687 __ProcessNodeAdjacentNodes (-dx, -dy, -dz, node1, radius1, node2, radius2, width2 / 2, F);
00688 }
00689
00690 template<class NodeData, class Real>
00691 template<class TerminatingNodeAdjacencyFunction>
00692 void OctNode<NodeData, Real>
00693 ::ProcessTerminatingNodeAdjacentNodes (const int& maxDepth, OctNode* node1, const int& width1, OctNode* node2, const int& width2, TerminatingNodeAdjacencyFunction* F, const int& processCurrent)
00694 {
00695 int c1[3], c2[3], w1, w2;
00696 node1->centerIndex (maxDepth + 1, c1);
00697 node2->centerIndex (maxDepth + 1, c2);
00698 w1 = node1->width (maxDepth + 1);
00699 w2 = node2->width (maxDepth + 1);
00700
00701 ProcessTerminatingNodeAdjacentNodes (c1[0] - c2[0], c1[1] - c2[1], c1[2] - c2[2], node1, (width1 * w1) >> 1, node2, (width2 * w2) >> 1, w2, F, processCurrent);
00702 }
00703
00704 template<class NodeData, class Real>
00705 template<class TerminatingNodeAdjacencyFunction>
00706 void OctNode<NodeData, Real>
00707 ::ProcessTerminatingNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
00708 OctNode<NodeData, Real>* node1, const int& radius1,
00709 OctNode<NodeData, Real>* node2, const int& radius2, const int& width2,
00710 TerminatingNodeAdjacencyFunction* F, const int& processCurrent)
00711 {
00712 if (!Overlap (dx, dy, dz, radius1 + radius2))
00713 {
00714 return;
00715 }
00716 if (processCurrent)
00717 {
00718 F->Function (node2, node1);
00719 }
00720 if (!node2->children)
00721 {
00722 return;
00723 }
00724 __ProcessTerminatingNodeAdjacentNodes (-dx, -dy, -dz, node1, radius1, node2, radius2, width2 / 2, F);
00725 }
00726
00727 template<class NodeData, class Real>
00728 template<class PointAdjacencyFunction>
00729 void OctNode<NodeData, Real>
00730 ::ProcessPointAdjacentNodes (const int& maxDepth, const int c1[3], OctNode* node2, const int& width2, PointAdjacencyFunction* F, const int& processCurrent)
00731 {
00732 int c2[3], w2;
00733 node2->centerIndex (maxDepth + 1, c2);
00734 w2 = node2->width (maxDepth + 1);
00735 ProcessPointAdjacentNodes (c1[0] - c2[0], c1[1] - c2[1], c1[2] - c2[2], node2, (width2 * w2) >> 1, w2, F, processCurrent);
00736 }
00737
00738 template<class NodeData, class Real>
00739 template<class PointAdjacencyFunction>
00740 void OctNode<NodeData, Real>
00741 ::ProcessPointAdjacentNodes (const int& dx, const int& dy, const int& dz,
00742 OctNode<NodeData, Real>* node2, const int& radius2, const int& width2,
00743 PointAdjacencyFunction* F, const int& processCurrent)
00744 {
00745 if (!Overlap (dx, dy, dz, radius2))
00746 {
00747 return;
00748 }
00749 if (processCurrent)
00750 {
00751 F->Function (node2);
00752 }
00753 if (!node2->children)
00754 {
00755 return;
00756 }
00757 __ProcessPointAdjacentNodes (-dx, -dy, -dz, node2, radius2, width2 >> 1, F);
00758 }
00759
00760 template<class NodeData, class Real>
00761 template<class NodeAdjacencyFunction>
00762 void OctNode<NodeData, Real>
00763 ::ProcessFixedDepthNodeAdjacentNodes (const int& maxDepth,
00764 OctNode<NodeData, Real>* node1, const int& width1,
00765 OctNode<NodeData, Real>* node2, const int& width2,
00766 const int& depth, NodeAdjacencyFunction* F, const int& processCurrent)
00767 {
00768 int c1[3], c2[3], w1, w2;
00769 node1->centerIndex (maxDepth + 1, c1);
00770 node2->centerIndex (maxDepth + 1, c2);
00771 w1 = node1->width (maxDepth + 1);
00772 w2 = node2->width (maxDepth + 1);
00773
00774 ProcessFixedDepthNodeAdjacentNodes (c1[0] - c2[0], c1[1] - c2[1], c1[2] - c2[2], node1, (width1 * w1) >> 1, node2, (width2 * w2) >> 1, w2, depth, F, processCurrent);
00775 }
00776
00777 template<class NodeData, class Real>
00778 template<class NodeAdjacencyFunction>
00779 void OctNode<NodeData, Real>
00780 ::ProcessFixedDepthNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
00781 OctNode<NodeData, Real>* node1, const int& radius1,
00782 OctNode<NodeData, Real>* node2, const int& radius2, const int& width2,
00783 const int& depth, NodeAdjacencyFunction* F, const int& processCurrent)
00784 {
00785 int d = node2->depth ();
00786 if (d > depth)
00787 {
00788 return;
00789 }
00790 if (!Overlap (dx, dy, dz, radius1 + radius2))
00791 {
00792 return;
00793 }
00794 if (d == depth)
00795 {
00796 if (processCurrent)
00797 {
00798 F->Function (node2, node1);
00799 }
00800 }
00801 else
00802 {
00803 if (!node2->children)
00804 {
00805 return;
00806 }
00807 __ProcessFixedDepthNodeAdjacentNodes (-dx, -dy, -dz, node1, radius1, node2, radius2, width2 / 2, depth - 1, F);
00808 }
00809 }
00810
00811 template<class NodeData, class Real>
00812 template<class NodeAdjacencyFunction>
00813 void OctNode<NodeData, Real>
00814 ::ProcessMaxDepthNodeAdjacentNodes (const int& maxDepth,
00815 OctNode<NodeData, Real>* node1, const int& width1,
00816 OctNode<NodeData, Real>* node2, const int& width2,
00817 const int& depth, NodeAdjacencyFunction* F, const int& processCurrent)
00818 {
00819 int c1[3], c2[3], w1, w2;
00820 node1->centerIndex (maxDepth + 1, c1);
00821 node2->centerIndex (maxDepth + 1, c2);
00822 w1 = node1->width (maxDepth + 1);
00823 w2 = node2->width (maxDepth + 1);
00824 ProcessMaxDepthNodeAdjacentNodes (c1[0] - c2[0], c1[1] - c2[1], c1[2] - c2[2], node1, (width1 * w1) >> 1, node2, (width2 * w2) >> 1, w2, depth, F, processCurrent);
00825 }
00826
00827 template<class NodeData, class Real>
00828 template<class NodeAdjacencyFunction>
00829 void OctNode<NodeData, Real>
00830 ::ProcessMaxDepthNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
00831 OctNode<NodeData, Real>* node1, const int& radius1,
00832 OctNode<NodeData, Real>* node2, const int& radius2, const int& width2,
00833 const int& depth, NodeAdjacencyFunction* F, const int& processCurrent)
00834 {
00835 int d = node2->depth ();
00836 if (d > depth)
00837 {
00838 return;
00839 }
00840 if (!Overlap (dx, dy, dz, radius1 + radius2))
00841 {
00842 return;
00843 }
00844 if (processCurrent)
00845 {
00846 F->Function (node2, node1);
00847 }
00848 if (d < depth && node2->children)
00849 {
00850 __ProcessMaxDepthNodeAdjacentNodes (-dx, -dy, -dz, node1, radius1, node2, radius2, width2 >> 1, depth - 1, F);
00851 }
00852 }
00853
00854 template <class NodeData, class Real>
00855 template<class NodeAdjacencyFunction>
00856 void OctNode<NodeData, Real>
00857 ::__ProcessNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
00858 OctNode* node1, const int& radius1,
00859 OctNode* node2, const int& radius2, const int& cWidth2,
00860 NodeAdjacencyFunction* F)
00861 {
00862 int cWidth = cWidth2 >> 1;
00863 int radius = radius2 >> 1;
00864 int o = ChildOverlap (dx, dy, dz, radius1 + radius, cWidth);
00865 if (o)
00866 {
00867 int dx1 = dx - cWidth;
00868 int dx2 = dx + cWidth;
00869 int dy1 = dy - cWidth;
00870 int dy2 = dy + cWidth;
00871 int dz1 = dz - cWidth;
00872 int dz2 = dz + cWidth;
00873 if (o & 1)
00874 {
00875 F->Function (&node2->children[0], node1);
00876 if (node2->children[0].children)
00877 {
00878 __ProcessNodeAdjacentNodes (dx1, dy1, dz1, node1, radius1, &node2->children[0], radius, cWidth, F);
00879 }
00880 }
00881 if (o & 2)
00882 {
00883 F->Function (&node2->children[1], node1);
00884 if (node2->children[1].children)
00885 {
00886 __ProcessNodeAdjacentNodes (dx2, dy1, dz1, node1, radius1, &node2->children[1], radius, cWidth, F);
00887 }
00888 }
00889 if (o & 4)
00890 {
00891 F->Function (&node2->children[2], node1);
00892 if (node2->children[2].children)
00893 {
00894 __ProcessNodeAdjacentNodes (dx1, dy2, dz1, node1, radius1, &node2->children[2], radius, cWidth, F);
00895 }
00896 }
00897 if (o & 8)
00898 {
00899 F->Function (&node2->children[3], node1);
00900 if (node2->children[3].children)
00901 {
00902 __ProcessNodeAdjacentNodes (dx2, dy2, dz1, node1, radius1, &node2->children[3], radius, cWidth, F);
00903 }
00904 }
00905 if (o & 16)
00906 {
00907 F->Function (&node2->children[4], node1);
00908 if (node2->children[4].children)
00909 {
00910 __ProcessNodeAdjacentNodes (dx1, dy1, dz2, node1, radius1, &node2->children[4], radius, cWidth, F);
00911 }
00912 }
00913 if (o & 32)
00914 {
00915 F->Function (&node2->children[5], node1);
00916 if (node2->children[5].children)
00917 {
00918 __ProcessNodeAdjacentNodes (dx2, dy1, dz2, node1, radius1, &node2->children[5], radius, cWidth, F);
00919 }
00920 }
00921 if (o & 64)
00922 {
00923 F->Function (&node2->children[6], node1);
00924 if (node2->children[6].children)
00925 {
00926 __ProcessNodeAdjacentNodes (dx1, dy2, dz2, node1, radius1, &node2->children[6], radius, cWidth, F);
00927 }
00928 }
00929 if (o & 128)
00930 {
00931 F->Function (&node2->children[7], node1);
00932 if (node2->children[7].children)
00933 {
00934 __ProcessNodeAdjacentNodes (dx2, dy2, dz2, node1, radius1, &node2->children[7], radius, cWidth, F);
00935 }
00936 }
00937 }
00938 }
00939
00940 template <class NodeData, class Real>
00941 template<class TerminatingNodeAdjacencyFunction>
00942 void OctNode<NodeData, Real>
00943 ::__ProcessTerminatingNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
00944 OctNode* node1, const int& radius1,
00945 OctNode* node2, const int& radius2, const int& cWidth2,
00946 TerminatingNodeAdjacencyFunction* F)
00947 {
00948 int cWidth = cWidth2 >> 1;
00949 int radius = radius2 >> 1;
00950 int o = ChildOverlap (dx, dy, dz, radius1 + radius, cWidth);
00951 if (o)
00952 {
00953 int dx1 = dx - cWidth;
00954 int dx2 = dx + cWidth;
00955 int dy1 = dy - cWidth;
00956 int dy2 = dy + cWidth;
00957 int dz1 = dz - cWidth;
00958 int dz2 = dz + cWidth;
00959 if (o & 1)
00960 {
00961 if (F->Function (&node2->children[0], node1) && node2->children[0].children)
00962 {
00963 __ProcessTerminatingNodeAdjacentNodes (dx1, dy1, dz1, node1, radius1, &node2->children[0], radius, cWidth, F);
00964 }
00965 }
00966 if (o & 2)
00967 {
00968 if (F->Function (&node2->children[1], node1) && node2->children[1].children)
00969 {
00970 __ProcessTerminatingNodeAdjacentNodes (dx2, dy1, dz1, node1, radius1, &node2->children[1], radius, cWidth, F);
00971 }
00972 }
00973 if (o & 4)
00974 {
00975 if (F->Function (&node2->children[2], node1) && node2->children[2].children)
00976 {
00977 __ProcessTerminatingNodeAdjacentNodes (dx1, dy2, dz1, node1, radius1, &node2->children[2], radius, cWidth, F);
00978 }
00979 }
00980 if (o & 8)
00981 {
00982 if (F->Function (&node2->children[3], node1) && node2->children[3].children)
00983 {
00984 __ProcessTerminatingNodeAdjacentNodes (dx2, dy2, dz1, node1, radius1, &node2->children[3], radius, cWidth, F);
00985 }
00986 }
00987 if (o & 16)
00988 {
00989 if (F->Function (&node2->children[4], node1) && node2->children[4].children)
00990 {
00991 __ProcessTerminatingNodeAdjacentNodes (dx1, dy1, dz2, node1, radius1, &node2->children[4], radius, cWidth, F);
00992 }
00993 }
00994 if (o & 32)
00995 {
00996 if (F->Function (&node2->children[5], node1) && node2->children[5].children)
00997 {
00998 __ProcessTerminatingNodeAdjacentNodes (dx2, dy1, dz2, node1, radius1, &node2->children[5], radius, cWidth, F);
00999 }
01000 }
01001 if (o & 64)
01002 {
01003 if (F->Function (&node2->children[6], node1) && node2->children[6].children)
01004 {
01005 __ProcessTerminatingNodeAdjacentNodes (dx1, dy2, dz2, node1, radius1, &node2->children[6], radius, cWidth, F);
01006 }
01007 }
01008 if (o & 128)
01009 {
01010 if (F->Function (&node2->children[7], node1) && node2->children[7].children)
01011 {
01012 __ProcessTerminatingNodeAdjacentNodes (dx2, dy2, dz2, node1, radius1, &node2->children[7], radius, cWidth, F);
01013 }
01014 }
01015 }
01016 }
01017
01018 template <class NodeData, class Real>
01019 template<class PointAdjacencyFunction>
01020 void OctNode<NodeData, Real>
01021 ::__ProcessPointAdjacentNodes (const int& dx, const int& dy, const int& dz,
01022 OctNode* node2, const int& radius2, const int& cWidth2,
01023 PointAdjacencyFunction* F)
01024 {
01025 int cWidth = cWidth2 >> 1;
01026 int radius = radius2 >> 1;
01027 int o = ChildOverlap (dx, dy, dz, radius, cWidth);
01028 if (o)
01029 {
01030 int dx1 = dx - cWidth;
01031 int dx2 = dx + cWidth;
01032 int dy1 = dy - cWidth;
01033 int dy2 = dy + cWidth;
01034 int dz1 = dz - cWidth;
01035 int dz2 = dz + cWidth;
01036 if (o & 1)
01037 {
01038 F->Function (&node2->children[0]);
01039 if (node2->children[0].children)
01040 {
01041 __ProcessPointAdjacentNodes (dx1, dy1, dz1, &node2->children[0], radius, cWidth, F);
01042 }
01043 }
01044 if (o & 2)
01045 {
01046 F->Function (&node2->children[1]);
01047 if (node2->children[1].children)
01048 {
01049 __ProcessPointAdjacentNodes (dx2, dy1, dz1, &node2->children[1], radius, cWidth, F);
01050 }
01051 }
01052 if (o & 4)
01053 {
01054 F->Function (&node2->children[2]);
01055 if (node2->children[2].children)
01056 {
01057 __ProcessPointAdjacentNodes (dx1, dy2, dz1, &node2->children[2], radius, cWidth, F);
01058 }
01059 }
01060 if (o & 8)
01061 {
01062 F->Function (&node2->children[3]);
01063 if (node2->children[3].children)
01064 {
01065 __ProcessPointAdjacentNodes (dx2, dy2, dz1, &node2->children[3], radius, cWidth, F);
01066 }
01067 }
01068 if (o & 16)
01069 {
01070 F->Function (&node2->children[4]);
01071 if (node2->children[4].children)
01072 {
01073 __ProcessPointAdjacentNodes (dx1, dy1, dz2, &node2->children[4], radius, cWidth, F);
01074 }
01075 }
01076 if (o & 32)
01077 {
01078 F->Function (&node2->children[5]);
01079 if (node2->children[5].children)
01080 {
01081 __ProcessPointAdjacentNodes (dx2, dy1, dz2, &node2->children[5], radius, cWidth, F);
01082 }
01083 }
01084 if (o & 64)
01085 {
01086 F->Function (&node2->children[6]);
01087 if (node2->children[6].children)
01088 {
01089 __ProcessPointAdjacentNodes (dx1, dy2, dz2, &node2->children[6], radius, cWidth, F);
01090 }
01091 }
01092 if (o & 128)
01093 {
01094 F->Function (&node2->children[7]);
01095 if (node2->children[7].children)
01096 {
01097 __ProcessPointAdjacentNodes (dx2, dy2, dz2, &node2->children[7], radius, cWidth, F);
01098 }
01099 }
01100 }
01101 }
01102
01103 template <class NodeData, class Real>
01104 template<class NodeAdjacencyFunction>
01105 void OctNode<NodeData, Real>
01106 ::__ProcessFixedDepthNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
01107 OctNode* node1, const int& radius1,
01108 OctNode* node2, const int& radius2, const int& cWidth2,
01109 const int& depth, NodeAdjacencyFunction* F)
01110 {
01111 int cWidth = cWidth2 >> 1;
01112 int radius = radius2 >> 1;
01113 int o = ChildOverlap (dx, dy, dz, radius1 + radius, cWidth);
01114 if (o)
01115 {
01116 int dx1 = dx - cWidth;
01117 int dx2 = dx + cWidth;
01118 int dy1 = dy - cWidth;
01119 int dy2 = dy + cWidth;
01120 int dz1 = dz - cWidth;
01121 int dz2 = dz + cWidth;
01122 if (node2->depth () == depth)
01123 {
01124 if (o & 1)
01125 {
01126 F->Function (&node2->children[0], node1);
01127 }
01128 if (o & 2)
01129 {
01130 F->Function (&node2->children[1], node1);
01131 }
01132 if (o & 4)
01133 {
01134 F->Function (&node2->children[2], node1);
01135 }
01136 if (o & 8)
01137 {
01138 F->Function (&node2->children[3], node1);
01139 }
01140 if (o & 16)
01141 {
01142 F->Function (&node2->children[4], node1);
01143 }
01144 if (o & 32)
01145 {
01146 F->Function (&node2->children[5], node1);
01147 }
01148 if (o & 64)
01149 {
01150 F->Function (&node2->children[6], node1);
01151 }
01152 if (o & 128)
01153 {
01154 F->Function (&node2->children[7], node1);
01155 }
01156 }
01157 else
01158 {
01159 if (o & 1)
01160 {
01161 if (node2->children[0].children)
01162 {
01163 __ProcessFixedDepthNodeAdjacentNodes (dx1, dy1, dz1, node1, radius1, &node2->children[0], radius, cWidth, depth, F);
01164 }
01165 }
01166 if (o & 2)
01167 {
01168 if (node2->children[1].children)
01169 {
01170 __ProcessFixedDepthNodeAdjacentNodes (dx2, dy1, dz1, node1, radius1, &node2->children[1], radius, cWidth, depth, F);
01171 }
01172 }
01173 if (o & 4)
01174 {
01175 if (node2->children[2].children)
01176 {
01177 __ProcessFixedDepthNodeAdjacentNodes (dx1, dy2, dz1, node1, radius1, &node2->children[2], radius, cWidth, depth, F);
01178 }
01179 }
01180 if (o & 8)
01181 {
01182 if (node2->children[3].children)
01183 {
01184 __ProcessFixedDepthNodeAdjacentNodes (dx2, dy2, dz1, node1, radius1, &node2->children[3], radius, cWidth, depth, F);
01185 }
01186 }
01187 if (o & 16)
01188 {
01189 if (node2->children[4].children)
01190 {
01191 __ProcessFixedDepthNodeAdjacentNodes (dx1, dy1, dz2, node1, radius1, &node2->children[4], radius, cWidth, depth, F);
01192 }
01193 }
01194 if (o & 32)
01195 {
01196 if (node2->children[5].children)
01197 {
01198 __ProcessFixedDepthNodeAdjacentNodes (dx2, dy1, dz2, node1, radius1, &node2->children[5], radius, cWidth, depth, F);
01199 }
01200 }
01201 if (o & 64)
01202 {
01203 if (node2->children[6].children)
01204 {
01205 __ProcessFixedDepthNodeAdjacentNodes (dx1, dy2, dz2, node1, radius1, &node2->children[6], radius, cWidth, depth, F);
01206 }
01207 }
01208 if (o & 128)
01209 {
01210 if (node2->children[7].children)
01211 {
01212 __ProcessFixedDepthNodeAdjacentNodes (dx2, dy2, dz2, node1, radius1, &node2->children[7], radius, cWidth, depth, F);
01213 }
01214 }
01215 }
01216 }
01217 }
01218
01219 template <class NodeData, class Real>
01220 template<class NodeAdjacencyFunction>
01221 void OctNode<NodeData, Real>
01222 ::__ProcessMaxDepthNodeAdjacentNodes (const int& dx, const int& dy, const int& dz,
01223 OctNode* node1, const int& radius1,
01224 OctNode* node2, const int& radius2, const int& cWidth2,
01225 const int& depth, NodeAdjacencyFunction* F)
01226 {
01227 int cWidth = cWidth2 >> 1;
01228 int radius = radius2 >> 1;
01229 int o = ChildOverlap (dx, dy, dz, radius1 + radius, cWidth);
01230 if (o)
01231 {
01232 int dx1 = dx - cWidth;
01233 int dx2 = dx + cWidth;
01234 int dy1 = dy - cWidth;
01235 int dy2 = dy + cWidth;
01236 int dz1 = dz - cWidth;
01237 int dz2 = dz + cWidth;
01238 if (node2->depth () <= depth)
01239 {
01240 if (o & 1)
01241 {
01242 F->Function (&node2->children[0], node1);
01243 }
01244 if (o & 2)
01245 {
01246 F->Function (&node2->children[1], node1);
01247 }
01248 if (o & 4)
01249 {
01250 F->Function (&node2->children[2], node1);
01251 }
01252 if (o & 8)
01253 {
01254 F->Function (&node2->children[3], node1);
01255 }
01256 if (o & 16)
01257 {
01258 F->Function (&node2->children[4], node1);
01259 }
01260 if (o & 32)
01261 {
01262 F->Function (&node2->children[5], node1);
01263 }
01264 if (o & 64)
01265 {
01266 F->Function (&node2->children[6], node1);
01267 }
01268 if (o & 128)
01269 {
01270 F->Function (&node2->children[7], node1);
01271 }
01272 }
01273 if (node2->depth () < depth)
01274 {
01275 if (o & 1)
01276 {
01277 if (node2->children[0].children)
01278 {
01279 __ProcessMaxDepthNodeAdjacentNodes (dx1, dy1, dz1, node1, radius1, &node2->children[0], radius, cWidth, depth, F);
01280 }
01281 }
01282 if (o & 2)
01283 {
01284 if (node2->children[1].children)
01285 {
01286 __ProcessMaxDepthNodeAdjacentNodes (dx2, dy1, dz1, node1, radius1, &node2->children[1], radius, cWidth, depth, F);
01287 }
01288 }
01289 if (o & 4)
01290 {
01291 if (node2->children[2].children)
01292 {
01293 __ProcessMaxDepthNodeAdjacentNodes (dx1, dy2, dz1, node1, radius1, &node2->children[2], radius, cWidth, depth, F);
01294 }
01295 }
01296 if (o & 8)
01297 {
01298 if (node2->children[3].children)
01299 {
01300 __ProcessMaxDepthNodeAdjacentNodes (dx2, dy2, dz1, node1, radius1, &node2->children[3], radius, cWidth, depth, F);
01301 }
01302 }
01303 if (o & 16)
01304 {
01305 if (node2->children[4].children)
01306 {
01307 __ProcessMaxDepthNodeAdjacentNodes (dx1, dy1, dz2, node1, radius1, &node2->children[4], radius, cWidth, depth, F);
01308 }
01309 }
01310 if (o & 32)
01311 {
01312 if (node2->children[5].children)
01313 {
01314 __ProcessMaxDepthNodeAdjacentNodes (dx2, dy1, dz2, node1, radius1, &node2->children[5], radius, cWidth, depth, F);
01315 }
01316 }
01317 if (o & 64)
01318 {
01319 if (node2->children[6].children)
01320 {
01321 __ProcessMaxDepthNodeAdjacentNodes (dx1, dy2, dz2, node1, radius1, &node2->children[6], radius, cWidth, depth, F);
01322 }
01323 }
01324 if (o & 128)
01325 {
01326 if (node2->children[7].children)
01327 {
01328 __ProcessMaxDepthNodeAdjacentNodes (dx2, dy2, dz2, node1, radius1, &node2->children[7], radius, cWidth, depth, F);
01329 }
01330 }
01331 }
01332 }
01333 }
01334
01335 template <class NodeData, class Real>
01336 inline int OctNode<NodeData, Real>
01337 ::ChildOverlap (const int& dx, const int& dy, const int& dz, const int& d, const int& cRadius2)
01338 {
01339 int w1 = d - cRadius2;
01340 int w2 = d + cRadius2;
01341 int overlap = 0;
01342
01343 int test = 0, test1 = 0;
01344 if (dx < w2 && dx>-w1)
01345 {
01346 test = 1;
01347 }
01348 if (dx < w1 && dx>-w2)
01349 {
01350 test |= 2;
01351 }
01352
01353 if (!test)
01354 {
01355 return 0;
01356 }
01357 if (dz < w2 && dz>-w1)
01358 {
01359 test1 = test;
01360 }
01361 if (dz < w1 && dz>-w2)
01362 {
01363 test1 |= test << 4;
01364 }
01365
01366 if (!test1)
01367 {
01368 return 0;
01369 }
01370 if (dy < w2 && dy>-w1)
01371 {
01372 overlap = test1;
01373 }
01374 if (dy < w1 && dy>-w2)
01375 {
01376 overlap |= test1 << 2;
01377 }
01378 return overlap;
01379 }
01380
01381 template <class NodeData, class Real>
01382 OctNode<NodeData, Real>* OctNode<NodeData, Real>
01383 ::getNearestLeaf (const Point3D<Real>& p)
01384 {
01385 Point3D<Real> center;
01386 Real width;
01387 OctNode<NodeData, Real>* temp;
01388 int cIndex;
01389 if (!children)
01390 {
01391 return this;
01392 }
01393 centerAndWidth (center, width);
01394 temp = this;
01395 while (temp->children)
01396 {
01397 cIndex = CornerIndex (center, p);
01398 temp = &temp->children[cIndex];
01399 width /= 2;
01400 if (cIndex & 1)
01401 {
01402 center.coords[0] += width / 2;
01403 }
01404 else
01405 {
01406 center.coords[0] -= width / 2;
01407 }
01408 if (cIndex & 2)
01409 {
01410 center.coords[1] += width / 2;
01411 }
01412 else
01413 {
01414 center.coords[1] -= width / 2;
01415 }
01416 if (cIndex & 4)
01417 {
01418 center.coords[2] += width / 2;
01419 }
01420 else
01421 {
01422 center.coords[2] -= width / 2;
01423 }
01424 }
01425 return temp;
01426 }
01427
01428 template <class NodeData, class Real>
01429 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
01430 ::getNearestLeaf (const Point3D<Real>& p) const
01431 {
01432 int nearest;
01433 Real temp, dist2;
01434 if (!children)
01435 {
01436 return this;
01437 }
01438 for (int i = 0; i < Cube::CORNERS; i++)
01439 {
01440 temp = SquareDistance (children[i].center, p);
01441 if (!i || temp < dist2)
01442 {
01443 dist2 = temp;
01444 nearest = i;
01445 }
01446 }
01447 return children[nearest].getNearestLeaf (p);
01448 }
01449
01450 template <class NodeData, class Real>
01451 int OctNode<NodeData, Real>
01452 ::CommonEdge (const OctNode<NodeData, Real>* node1, const int& eIndex1, const OctNode<NodeData, Real>* node2, const int& eIndex2)
01453 {
01454 int o1, o2, i1, i2, j1, j2;
01455
01456 Cube::FactorEdgeIndex (eIndex1, o1, i1, j1);
01457 Cube::FactorEdgeIndex (eIndex2, o2, i2, j2);
01458 if (o1 != o2)
01459 {
01460 return 0;
01461 }
01462
01463 int dir[2];
01464 int idx1[2];
01465 int idx2[2];
01466 switch (o1)
01467 {
01468 case 0: dir[0] = 1;
01469 dir[1] = 2;
01470 break;
01471 case 1: dir[0] = 0;
01472 dir[1] = 2;
01473 break;
01474 case 2: dir[0] = 0;
01475 dir[1] = 1;
01476 break;
01477 };
01478 int d1, d2, off1[3], off2[3];
01479 node1->depthAndOffset (d1, off1);
01480 node2->depthAndOffset (d2, off2);
01481 idx1[0] = off1[dir[0]]+(1 << d1) + i1;
01482 idx1[1] = off1[dir[1]]+(1 << d1) + j1;
01483 idx2[0] = off2[dir[0]]+(1 << d2) + i2;
01484 idx2[1] = off2[dir[1]]+(1 << d2) + j2;
01485 if (d1 > d2)
01486 {
01487 idx2[0] <<= (d1 - d2);
01488 idx2[1] <<= (d1 - d2);
01489 }
01490 else
01491 {
01492 idx1[0] <<= (d2 - d1);
01493 idx1[1] <<= (d2 - d1);
01494 }
01495 if (idx1[0] == idx2[0] && idx1[1] == idx2[1])
01496 {
01497 return 1;
01498 }
01499 else
01500 {
01501 return 0;
01502 }
01503 }
01504
01505 template<class NodeData, class Real>
01506 int OctNode<NodeData, Real>
01507 ::CornerIndex (const Point3D<Real>& center, const Point3D<Real>& p)
01508 {
01509 int cIndex = 0;
01510 if (p.coords[0] > center.coords[0])
01511 {
01512 cIndex |= 1;
01513 }
01514 if (p.coords[1] > center.coords[1])
01515 {
01516 cIndex |= 2;
01517 }
01518 if (p.coords[2] > center.coords[2])
01519 {
01520 cIndex |= 4;
01521 }
01522 return cIndex;
01523 }
01524
01525 template <class NodeData, class Real>
01526 template<class NodeData2>
01527 OctNode<NodeData, Real>& OctNode<NodeData, Real>::operator = (const OctNode<NodeData2, Real>& node)
01528 {
01529 int i;
01530 if (children)
01531 {
01532 delete[] children;
01533 }
01534 children = NULL;
01535
01536 depth = node.depth;
01537 for (i = 0; i < DIMENSION; i++)
01538 {
01539 this->offset[i] = node.offset[i];
01540 }
01541 if (node.children)
01542 {
01543 initChildren ();
01544 for (i = 0; i < Cube::CORNERS; i++)
01545 {
01546 children[i] = node.children[i];
01547 }
01548 }
01549 return (*this);
01550 }
01551
01552 template <class NodeData, class Real> int
01553 OctNode<NodeData, Real>::CompareForwardDepths (const void* v1, const void* v2)
01554 {
01555 return (static_cast<const OctNode<NodeData, Real>*> (v1))->depth - (static_cast<const OctNode<NodeData, Real>*> (v2))->depth;
01556 }
01557
01558 template <class NodeData, class Real> int
01559 OctNode<NodeData, Real>::CompareForwardPointerDepths (const void* v1, const void* v2)
01560 {
01561 typedef const OctNode<NodeData, Real> Ty;
01562 typedef Ty** Tyy;
01563 Ty *n1 = *Tyy (v1);
01564 Ty *n2 = *Tyy (v2);
01565
01566 if (n1->d != n2->d)
01567 return (int (n1->d) - int (n2->d));
01568
01569 while (n1->parent != n2->parent)
01570 {
01571 n1 = n1->parent;
01572 n2 = n2->parent;
01573 }
01574 if (n1->off[0] != n2->off[0])
01575 return (int (n1->off[0]) - int (n2->off[0]));
01576
01577 if (n1->off[1] != n2->off[1])
01578 return (int (n1->off[1]) - int(n2->off[1]));
01579
01580 return (int (n1->off[2]) - int(n2->off[2]));
01581 }
01582
01583 template <class NodeData, class Real> int
01584 OctNode<NodeData, Real>::CompareBackwardDepths (const void* v1, const void* v2)
01585 {
01586 return ((static_cast<const OctNode<NodeData, Real>*> (v2))->depth - (static_cast<const OctNode<NodeData, Real>*>(v1))->depth);
01587 }
01588
01589 template <class NodeData, class Real>
01590 int OctNode<NodeData, Real>
01591 ::CompareBackwardPointerDepths (const void* v1, const void* v2)
01592 {
01593 return (*reinterpret_cast<const OctNode<NodeData, Real>**>(v2))->depth ()-(*reinterpret_cast<const OctNode<NodeData, Real>**> (v1))->depth ();
01594 }
01595
01596 template <class NodeData, class Real>
01597 inline int OctNode<NodeData, Real>
01598 ::Overlap2 (const int &depth1, const int offSet1[DIMENSION], const Real& multiplier1, const int &depth2, const int offSet2[DIMENSION], const Real& multiplier2)
01599 {
01600 int d = depth2 - depth1;
01601 Real w = multiplier2 + multiplier1 * static_cast<Real> (1 << d);
01602 Real w2 = Real ((1 << (d - 1)) - 0.5);
01603 if (
01604 fabs (Real (offSet2[0]-(offSet1[0] << d)) - w2) >= w ||
01605 fabs (Real (offSet2[1]-(offSet1[1] << d)) - w2) >= w ||
01606 fabs (Real (offSet2[2]-(offSet1[2] << d)) - w2) >= w
01607 )
01608 {
01609 return 0;
01610 }
01611 return 1;
01612 }
01613
01614 template <class NodeData, class Real>
01615 inline int OctNode<NodeData, Real>
01616 ::Overlap (const int& c1, const int& c2, const int& c3, const int& dWidth)
01617 {
01618 if (c1 >= dWidth || c1 <= -dWidth || c2 >= dWidth || c2 <= -dWidth || c3 >= dWidth || c3 <= -dWidth)
01619 {
01620 return 0;
01621 }
01622 else
01623 {
01624 return 1;
01625 }
01626 }
01627
01628 template <class NodeData, class Real>
01629 OctNode<NodeData, Real>* OctNode<NodeData, Real>
01630 ::faceNeighbor (const int& faceIndex, const int& forceChildren)
01631 {
01632 return __faceNeighbor (faceIndex >> 1, faceIndex & 1, forceChildren);
01633 }
01634
01635 template <class NodeData, class Real>
01636 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
01637 ::faceNeighbor (const int& faceIndex) const
01638 {
01639 return __faceNeighbor (faceIndex >> 1, faceIndex & 1);
01640 }
01641
01642 template <class NodeData, class Real>
01643 OctNode<NodeData, Real>* OctNode<NodeData, Real>
01644 ::__faceNeighbor (const int& dir, const int& off, const int& forceChildren)
01645 {
01646 if (!parent)
01647 {
01648 return NULL;
01649 }
01650 int pIndex = int(this-parent->children);
01651 pIndex ^= (1 << dir);
01652 if ((pIndex & (1 << dir)) == (off << dir))
01653 {
01654 return &parent->children[pIndex];
01655 }
01656
01657 else
01658 {
01659 OctNode* temp = parent->__faceNeighbor (dir, off, forceChildren);
01660 if (!temp)
01661 {
01662 return NULL;
01663 }
01664 if (!temp->children)
01665 {
01666 if (forceChildren)
01667 {
01668 temp->initChildren ();
01669 }
01670 else
01671 {
01672 return temp;
01673 }
01674 }
01675 return &temp->children[pIndex];
01676 }
01677 }
01678
01679 template <class NodeData, class Real>
01680 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
01681 ::__faceNeighbor (const int& dir, const int& off) const
01682 {
01683 if (!parent)
01684 {
01685 return NULL;
01686 }
01687 int pIndex = int(this-parent->children);
01688 pIndex ^= (1 << dir);
01689 if ((pIndex & (1 << dir)) == (off << dir))
01690 {
01691 return &parent->children[pIndex];
01692 }
01693
01694 else
01695 {
01696 const OctNode* temp = parent->__faceNeighbor (dir, off);
01697 if (!temp || !temp->children)
01698 {
01699 return temp;
01700 }
01701 else
01702 {
01703 return &temp->children[pIndex];
01704 }
01705 }
01706 }
01707
01708 template <class NodeData, class Real>
01709 OctNode<NodeData, Real>* OctNode<NodeData, Real>
01710 ::edgeNeighbor (const int& edgeIndex, const int& forceChildren)
01711 {
01712 int idx[2], o, i[2];
01713 Cube::FactorEdgeIndex (edgeIndex, o, i[0], i[1]);
01714 switch (o)
01715 {
01716 case 0: idx[0] = 1;
01717 idx[1] = 2;
01718 break;
01719 case 1: idx[0] = 0;
01720 idx[1] = 2;
01721 break;
01722 case 2: idx[0] = 0;
01723 idx[1] = 1;
01724 break;
01725 };
01726 return __edgeNeighbor (o, i, idx, forceChildren);
01727 }
01728
01729 template <class NodeData, class Real>
01730 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
01731 ::edgeNeighbor (const int& edgeIndex) const
01732 {
01733 int idx[2], o, i[2];
01734 Cube::FactorEdgeIndex (edgeIndex, o, i[0], i[1]);
01735 switch (o)
01736 {
01737 case 0: idx[0] = 1;
01738 idx[1] = 2;
01739 break;
01740 case 1: idx[0] = 0;
01741 idx[1] = 2;
01742 break;
01743 case 2: idx[0] = 0;
01744 idx[1] = 1;
01745 break;
01746 };
01747 return __edgeNeighbor (o, i, idx);
01748 }
01749
01750 template <class NodeData, class Real>
01751 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
01752 ::__edgeNeighbor (const int& o, const int i[2], const int idx[2]) const
01753 {
01754 if (!parent)
01755 {
01756 return NULL;
01757 }
01758 int pIndex = int(this-parent->children);
01759 int aIndex, x[DIMENSION];
01760
01761 Cube::FactorCornerIndex (pIndex, x[0], x[1], x[2]);
01762 aIndex = (~((i[0] ^ x[idx[0]]) | ((i[1] ^ x[idx[1]]) << 1))) & 3;
01763 pIndex ^= (7 ^ (1 << o));
01764 if (aIndex == 1)
01765 {
01766 const OctNode* temp = parent->__faceNeighbor (idx[0], i[0]);
01767 if (!temp || !temp->children)
01768 {
01769 return NULL;
01770 }
01771 else
01772 {
01773 return &temp->children[pIndex];
01774 }
01775 }
01776 else if (aIndex == 2)
01777 {
01778 const OctNode* temp = parent->__faceNeighbor (idx[1], i[1]);
01779 if (!temp || !temp->children)
01780 {
01781 return NULL;
01782 }
01783 else
01784 {
01785 return &temp->children[pIndex];
01786 }
01787 }
01788 else if (aIndex == 0)
01789 {
01790 return &parent->children[pIndex];
01791 }
01792 else if (aIndex == 3)
01793 {
01794 const OctNode* temp = parent->__edgeNeighbor (o, i, idx);
01795 if (!temp || !temp->children)
01796 {
01797 return temp;
01798 }
01799 else
01800 {
01801 return &temp->children[pIndex];
01802 }
01803 }
01804 else
01805 {
01806 return NULL;
01807 }
01808 }
01809
01810 template <class NodeData, class Real>
01811 OctNode<NodeData, Real>* OctNode<NodeData, Real>
01812 ::__edgeNeighbor (const int& o, const int i[2], const int idx[2], const int& forceChildren)
01813 {
01814 if (!parent)
01815 {
01816 return NULL;
01817 }
01818 int pIndex = int(this-parent->children);
01819 int aIndex, x[DIMENSION];
01820
01821 Cube::FactorCornerIndex (pIndex, x[0], x[1], x[2]);
01822 aIndex = (~((i[0] ^ x[idx[0]]) | ((i[1] ^ x[idx[1]]) << 1))) & 3;
01823 pIndex ^= (7 ^ (1 << o));
01824 if (aIndex == 1)
01825 {
01826 OctNode* temp = parent->__faceNeighbor (idx[0], i[0], 0);
01827 if (!temp || !temp->children)
01828 {
01829 return NULL;
01830 }
01831 else
01832 {
01833 return &temp->children[pIndex];
01834 }
01835 }
01836 else if (aIndex == 2)
01837 {
01838 OctNode* temp = parent->__faceNeighbor (idx[1], i[1], 0);
01839 if (!temp || !temp->children)
01840 {
01841 return NULL;
01842 }
01843 else
01844 {
01845 return &temp->children[pIndex];
01846 }
01847 }
01848 else if (aIndex == 0)
01849 {
01850 return &parent->children[pIndex];
01851 }
01852 else if (aIndex == 3)
01853 {
01854 OctNode* temp = parent->__edgeNeighbor (o, i, idx, forceChildren);
01855 if (!temp)
01856 {
01857 return NULL;
01858 }
01859 if (!temp->children)
01860 {
01861 if (forceChildren)
01862 {
01863 temp->initChildren ();
01864 }
01865 else
01866 {
01867 return temp;
01868 }
01869 }
01870 return &temp->children[pIndex];
01871 }
01872 else
01873 {
01874 return NULL;
01875 }
01876 }
01877
01878 template <class NodeData, class Real>
01879 const OctNode<NodeData, Real>* OctNode<NodeData, Real>
01880 ::cornerNeighbor (const int& cornerIndex) const
01881 {
01882 int pIndex, aIndex = 0;
01883 if (!parent)
01884 {
01885 return NULL;
01886 }
01887
01888 pIndex = int(this-parent->children);
01889 aIndex = (cornerIndex ^ pIndex);
01890 pIndex = (~pIndex)&7;
01891 if (aIndex == 7)
01892 {
01893 return &parent->children[pIndex];
01894 }
01895 else if (aIndex == 0)
01896 {
01897 const OctNode* temp = (reinterpret_cast<const OctNode*> (parent))->cornerNeighbor (cornerIndex);
01898 if (!temp || !temp->children)
01899 {
01900 return temp;
01901 }
01902 else
01903 {
01904 return &temp->children[pIndex];
01905 }
01906 }
01907 else if (aIndex == 6)
01908 {
01909 const OctNode* temp = (reinterpret_cast<const OctNode*> (parent))->__faceNeighbor (0, cornerIndex & 1);
01910 if (!temp || !temp->children)
01911 {
01912 return NULL;
01913 }
01914 else
01915 {
01916 return & temp->children[pIndex];
01917 }
01918 }
01919 else if (aIndex == 5)
01920 {
01921 const OctNode* temp = (reinterpret_cast<const OctNode*> (parent))->__faceNeighbor (1, (cornerIndex & 2) >> 1);
01922 if (!temp || !temp->children)
01923 {
01924 return NULL;
01925 }
01926 else
01927 {
01928 return & temp->children[pIndex];
01929 }
01930 }
01931 else if (aIndex == 3)
01932 {
01933 const OctNode* temp = (reinterpret_cast<const OctNode*> (parent))->__faceNeighbor (2, (cornerIndex & 4) >> 2);
01934 if (!temp || !temp->children)
01935 {
01936 return NULL;
01937 }
01938 else
01939 {
01940 return & temp->children[pIndex];
01941 }
01942 }
01943 else if (aIndex == 4)
01944 {
01945 const OctNode* temp = (reinterpret_cast<const OctNode*> (parent))->edgeNeighbor (8 | (cornerIndex & 1) | (cornerIndex & 2));
01946 if (!temp || !temp->children)
01947 {
01948 return NULL;
01949 }
01950 else
01951 {
01952 return & temp->children[pIndex];
01953 }
01954 }
01955 else if (aIndex == 2)
01956 {
01957 const OctNode* temp = (reinterpret_cast<const OctNode*> (parent))->edgeNeighbor (4 | (cornerIndex & 1) | ((cornerIndex & 4) >> 1));
01958 if (!temp || !temp->children)
01959 {
01960 return NULL;
01961 }
01962 else
01963 {
01964 return & temp->children[pIndex];
01965 }
01966 }
01967 else if (aIndex == 1)
01968 {
01969 const OctNode* temp = (reinterpret_cast<const OctNode*> (parent))->edgeNeighbor (((cornerIndex & 2) | (cornerIndex & 4)) >> 1);
01970 if (!temp || !temp->children)
01971 {
01972 return NULL;
01973 }
01974 else
01975 {
01976 return & temp->children[pIndex];
01977 }
01978 }
01979 else
01980 {
01981 return NULL;
01982 }
01983 }
01984
01986 template <class NodeData, class Real> OctNode<NodeData, Real>*
01987 OctNode<NodeData, Real> ::cornerNeighbor (const int& cornerIndex, const int& forceChildren)
01988 {
01989 int pIndex, aIndex = 0;
01990 if (!parent)
01991 return (NULL);
01992
01993 pIndex = int(this-parent->children);
01994 aIndex = (cornerIndex ^ pIndex);
01995 pIndex = (~pIndex)&7;
01996 if (aIndex == 7)
01997 {
01998 return (&parent->children[pIndex]);
01999 }
02000 else if (aIndex == 0)
02001 {
02002 OctNode* temp = (static_cast<OctNode*> (parent))->cornerNeighbor (cornerIndex, forceChildren);
02003 if (!temp)
02004 return (NULL);
02005 if (!temp->children)
02006 {
02007 if (forceChildren)
02008 temp->initChildren ();
02009 else
02010 return (temp);
02011 }
02012 return (&temp->children[pIndex]);
02013 }
02014 else if (aIndex == 6)
02015 {
02016 OctNode* temp = (static_cast<OctNode*> (parent))->__faceNeighbor (0, cornerIndex & 1, 0);
02017 if (!temp || !temp->children)
02018 return (NULL);
02019 else
02020 return (&temp->children[pIndex]);
02021 }
02022 else if (aIndex == 5)
02023 {
02024 OctNode* temp = (static_cast<OctNode*> (parent))->__faceNeighbor (1, (cornerIndex & 2) >> 1, 0);
02025 if (!temp || !temp->children)
02026 return (NULL);
02027 else
02028 return (&temp->children[pIndex]);
02029 }
02030 else if (aIndex == 3)
02031 {
02032 OctNode* temp = (static_cast<OctNode*> (parent))->__faceNeighbor (2, (cornerIndex & 4) >> 2, 0);
02033 if (!temp || !temp->children)
02034 return (NULL);
02035 else
02036 return (&temp->children[pIndex]);
02037 }
02038 else if (aIndex == 4)
02039 {
02040 OctNode* temp = (static_cast<OctNode*> (parent))->edgeNeighbor (8 | (cornerIndex & 1) | (cornerIndex & 2));
02041 if (!temp || !temp->children)
02042 return (NULL);
02043 else
02044 return (&temp->children[pIndex]);
02045 }
02046 else if (aIndex == 2)
02047 {
02048 OctNode* temp = (static_cast<OctNode*> (parent))->edgeNeighbor (4 | (cornerIndex & 1) | ((cornerIndex & 4) >> 1));
02049 if (!temp || !temp->children)
02050 return (NULL);
02051 else
02052 return (&temp->children[pIndex]);
02053 }
02054 else if (aIndex == 1)
02055 {
02056 OctNode* temp = (static_cast<OctNode*> (parent))->edgeNeighbor (((cornerIndex & 2) | (cornerIndex & 4)) >> 1);
02057 if (!temp || !temp->children)
02058 return (NULL);
02059 else
02060 return (&temp->children[pIndex]);
02061 }
02062 else
02063 return (NULL);
02064 }
02066
02068
02070 template<class NodeData, class Real>
02071 OctNode<NodeData, Real>::Neighbors::Neighbors ()
02072 {
02073 clear ();
02074 }
02075
02077 template<class NodeData, class Real> void
02078 OctNode<NodeData, Real>::Neighbors::clear ()
02079 {
02080 for (int i = 0; i < 3; i++)
02081 {
02082 for (int j = 0; j < 3; j++)
02083 {
02084 for (int k = 0; k < 3; k++)
02085 {
02086 neighbors[i][j][k] = NULL;
02087 }
02088 }
02089 }
02090 }
02091
02093 template<class NodeData, class Real>
02094 OctNode<NodeData, Real>::NeighborKey::NeighborKey () : neighbors (NULL)
02095 {
02096 }
02097
02099 template<class NodeData, class Real>
02100 OctNode<NodeData, Real>::NeighborKey::~NeighborKey ()
02101 {
02102 if (neighbors)
02103 delete[] neighbors;
02104 neighbors = NULL;
02105 }
02106
02108 template<class NodeData, class Real> void
02109 OctNode<NodeData, Real>::NeighborKey::set (const int& d)
02110 {
02111 if (neighbors)
02112 {
02113 delete[] neighbors;
02114 }
02115 neighbors = NULL;
02116 if (d < 0)
02117 {
02118 return;
02119 }
02120 neighbors = new Neighbors[d + 1];
02121 }
02122
02123 template<class NodeData, class Real>
02124 typename OctNode<NodeData, Real>::Neighbors& OctNode<NodeData, Real>
02125 ::NeighborKey::setNeighbors (OctNode<NodeData, Real>* node)
02126 {
02127 int d = node->depth ();
02128 if (node != neighbors[d].neighbors[1][1][1])
02129 {
02130 neighbors[d].clear ();
02131
02132 if (!node->parent)
02133 {
02134 neighbors[d].neighbors[1][1][1] = node;
02135 }
02136 else
02137 {
02138 int i, j, k, x1, y1, z1, x2, y2, z2;
02139 int idx = int(node - node->parent->children);
02140 Cube::FactorCornerIndex (idx, x1, y1, z1);
02141 Cube::FactorCornerIndex ((~idx)&7, x2, y2, z2);
02142 for (i = 0; i < 2; i++)
02143 {
02144 for (j = 0; j < 2; j++)
02145 {
02146 for (k = 0; k < 2; k++)
02147 {
02148 neighbors[d].neighbors[x2 + i][y2 + j][z2 + k] = &node->parent->children[Cube::CornerIndex (i, j, k)];
02149 }
02150 }
02151 }
02152 Neighbors& temp = setNeighbors (node->parent);
02153
02154
02155 i = x1 << 1;
02156 if (temp.neighbors[i][1][1])
02157 {
02158 if (!temp.neighbors[i][1][1]->children)
02159 {
02160 temp.neighbors[i][1][1]->initChildren ();
02161 }
02162 for (j = 0; j < 2; j++)
02163 {
02164 for (k = 0; k < 2; k++)
02165 {
02166 neighbors[d].neighbors[i][y2 + j][z2 + k] = &temp.neighbors[i][1][1]->children[Cube::CornerIndex (x2, j, k)];
02167 }
02168 }
02169 }
02170 j = y1 << 1;
02171 if (temp.neighbors[1][j][1])
02172 {
02173 if (!temp.neighbors[1][j][1]->children)
02174 {
02175 temp.neighbors[1][j][1]->initChildren ();
02176 }
02177 for (i = 0; i < 2; i++)
02178 {
02179 for (k = 0; k < 2; k++)
02180 {
02181 neighbors[d].neighbors[x2 + i][j][z2 + k] = &temp.neighbors[1][j][1]->children[Cube::CornerIndex (i, y2, k)];
02182 }
02183 }
02184 }
02185 k = z1 << 1;
02186 if (temp.neighbors[1][1][k])
02187 {
02188 if (!temp.neighbors[1][1][k]->children)
02189 {
02190 temp.neighbors[1][1][k]->initChildren ();
02191 }
02192 for (i = 0; i < 2; i++)
02193 {
02194 for (j = 0; j < 2; j++)
02195 {
02196 neighbors[d].neighbors[x2 + i][y2 + j][k] = &temp.neighbors[1][1][k]->children[Cube::CornerIndex (i, j, z2)];
02197 }
02198 }
02199 }
02200
02201
02202 i = x1 << 1;
02203 j = y1 << 1;
02204 if (temp.neighbors[i][j][1])
02205 {
02206 if (!temp.neighbors[i][j][1]->children)
02207 {
02208 temp.neighbors[i][j][1]->initChildren ();
02209 }
02210 for (k = 0; k < 2; k++)
02211 {
02212 neighbors[d].neighbors[i][j][z2 + k] = &temp.neighbors[i][j][1]->children[Cube::CornerIndex (x2, y2, k)];
02213 }
02214 }
02215 i = x1 << 1;
02216 k = z1 << 1;
02217 if (temp.neighbors[i][1][k])
02218 {
02219 if (!temp.neighbors[i][1][k]->children)
02220 {
02221 temp.neighbors[i][1][k]->initChildren ();
02222 }
02223 for (j = 0; j < 2; j++)
02224 {
02225 neighbors[d].neighbors[i][y2 + j][k] = &temp.neighbors[i][1][k]->children[Cube::CornerIndex (x2, j, z2)];
02226 }
02227 }
02228 j = y1 << 1;
02229 k = z1 << 1;
02230 if (temp.neighbors[1][j][k])
02231 {
02232 if (!temp.neighbors[1][j][k]->children)
02233 {
02234 temp.neighbors[1][j][k]->initChildren ();
02235 }
02236 for (i = 0; i < 2; i++)
02237 {
02238 neighbors[d].neighbors[x2 + i][j][k] = &temp.neighbors[1][j][k]->children[Cube::CornerIndex (i, y2, z2)];
02239 }
02240 }
02241
02242
02243 i = x1 << 1;
02244 j = y1 << 1;
02245 k = z1 << 1;
02246 if (temp.neighbors[i][j][k])
02247 {
02248 if (!temp.neighbors[i][j][k]->children)
02249 {
02250 temp.neighbors[i][j][k]->initChildren ();
02251 }
02252 neighbors[d].neighbors[i][j][k] = &temp.neighbors[i][j][k]->children[Cube::CornerIndex (x2, y2, z2)];
02253 }
02254 }
02255 }
02256 return neighbors[d];
02257 }
02258
02259 template<class NodeData, class Real>
02260 typename OctNode<NodeData, Real>::Neighbors& OctNode<NodeData, Real>
02261 ::NeighborKey::getNeighbors (OctNode<NodeData, Real>* node)
02262 {
02263 int d = node->depth ();
02264 if (node != neighbors[d].neighbors[1][1][1])
02265 {
02266 neighbors[d].clear ();
02267
02268 if (!node->parent)
02269 {
02270 neighbors[d].neighbors[1][1][1] = node;
02271 }
02272 else
02273 {
02274 int i, j, k, x1, y1, z1, x2, y2, z2;
02275 int idx = int(node - node->parent->children);
02276 Cube::FactorCornerIndex (idx, x1, y1, z1);
02277 Cube::FactorCornerIndex ((~idx)&7, x2, y2, z2);
02278 for (i = 0; i < 2; i++)
02279 {
02280 for (j = 0; j < 2; j++)
02281 {
02282 for (k = 0; k < 2; k++)
02283 {
02284 neighbors[d].neighbors[x2 + i][y2 + j][z2 + k] = &node->parent->children[Cube::CornerIndex (i, j, k)];
02285 }
02286 }
02287 }
02288 Neighbors& temp = getNeighbors (node->parent);
02289
02290
02291 i = x1 << 1;
02292 if (temp.neighbors[i][1][1] && temp.neighbors[i][1][1]->children)
02293 {
02294 for (j = 0; j < 2; j++)
02295 {
02296 for (k = 0; k < 2; k++)
02297 {
02298 neighbors[d].neighbors[i][y2 + j][z2 + k] = &temp.neighbors[i][1][1]->children[Cube::CornerIndex (x2, j, k)];
02299 }
02300 }
02301 }
02302 j = y1 << 1;
02303 if (temp.neighbors[1][j][1] && temp.neighbors[1][j][1]->children)
02304 {
02305 for (i = 0; i < 2; i++)
02306 {
02307 for (k = 0; k < 2; k++)
02308 {
02309 neighbors[d].neighbors[x2 + i][j][z2 + k] = &temp.neighbors[1][j][1]->children[Cube::CornerIndex (i, y2, k)];
02310 }
02311 }
02312 }
02313 k = z1 << 1;
02314 if (temp.neighbors[1][1][k] && temp.neighbors[1][1][k]->children)
02315 {
02316 for (i = 0; i < 2; i++)
02317 {
02318 for (j = 0; j < 2; j++)
02319 {
02320 neighbors[d].neighbors[x2 + i][y2 + j][k] = &temp.neighbors[1][1][k]->children[Cube::CornerIndex (i, j, z2)];
02321 }
02322 }
02323 }
02324
02325
02326 i = x1 << 1;
02327 j = y1 << 1;
02328 if (temp.neighbors[i][j][1] && temp.neighbors[i][j][1]->children)
02329 {
02330 for (k = 0; k < 2; k++)
02331 {
02332 neighbors[d].neighbors[i][j][z2 + k] = &temp.neighbors[i][j][1]->children[Cube::CornerIndex (x2, y2, k)];
02333 }
02334 }
02335 i = x1 << 1;
02336 k = z1 << 1;
02337 if (temp.neighbors[i][1][k] && temp.neighbors[i][1][k]->children)
02338 {
02339 for (j = 0; j < 2; j++)
02340 {
02341 neighbors[d].neighbors[i][y2 + j][k] = &temp.neighbors[i][1][k]->children[Cube::CornerIndex (x2, j, z2)];
02342 }
02343 }
02344 j = y1 << 1;
02345 k = z1 << 1;
02346 if (temp.neighbors[1][j][k] && temp.neighbors[1][j][k]->children)
02347 {
02348 for (i = 0; i < 2; i++)
02349 {
02350 neighbors[d].neighbors[x2 + i][j][k] = &temp.neighbors[1][j][k]->children[Cube::CornerIndex (i, y2, z2)];
02351 }
02352 }
02353
02354
02355 i = x1 << 1;
02356 j = y1 << 1;
02357 k = z1 << 1;
02358 if (temp.neighbors[i][j][k] && temp.neighbors[i][j][k]->children)
02359 {
02360 neighbors[d].neighbors[i][j][k] = &temp.neighbors[i][j][k]->children[Cube::CornerIndex (x2, y2, z2)];
02361 }
02362 }
02363 }
02364 return neighbors[node->depth ()];
02365 }
02366
02368
02370
02372 template<class NodeData, class Real>
02373 OctNode<NodeData, Real>::Neighbors2::Neighbors2 ()
02374 {
02375 clear ();
02376 }
02377
02379 template<class NodeData, class Real> void
02380 OctNode<NodeData, Real>::Neighbors2::clear ()
02381 {
02382 for (int i = 0; i < 3; i++)
02383 {
02384 for (int j = 0; j < 3; j++)
02385 {
02386 for (int k = 0; k < 3; k++)
02387 {
02388 neighbors[i][j][k] = NULL;
02389 }
02390 }
02391 }
02392 }
02393
02395 template<class NodeData, class Real>
02396 OctNode<NodeData, Real>::NeighborKey2::NeighborKey2 () : neighbors (NULL)
02397 {
02398 }
02399
02401 template<class NodeData, class Real>
02402 OctNode<NodeData, Real>::NeighborKey2::~NeighborKey2 ()
02403 {
02404 if (neighbors)
02405 {
02406 delete[] neighbors;
02407 }
02408 neighbors = NULL;
02409 }
02410
02412 template<class NodeData, class Real> void
02413 OctNode<NodeData, Real>::NeighborKey2::set (const int& d)
02414 {
02415 if (neighbors)
02416 {
02417 delete[] neighbors;
02418 }
02419 neighbors = NULL;
02420 if (d < 0)
02421 {
02422 return;
02423 }
02424 neighbors = new Neighbors2[d + 1];
02425 }
02426
02427 template<class NodeData, class Real>
02428 typename OctNode<NodeData, Real>::Neighbors2& OctNode<NodeData, Real>
02429 ::NeighborKey2::getNeighbors (const OctNode<NodeData, Real>* node)
02430 {
02431 int d = node->depth ();
02432 if (node != neighbors[d].neighbors[1][1][1])
02433 {
02434 neighbors[d].clear ();
02435
02436 if (!node->parent)
02437 {
02438 neighbors[d].neighbors[1][1][1] = node;
02439 }
02440 else
02441 {
02442 int i, j, k, x1, y1, z1, x2, y2, z2;
02443 int idx = int(node - node->parent->children);
02444 Cube::FactorCornerIndex (idx, x1, y1, z1);
02445 Cube::FactorCornerIndex ((~idx)&7, x2, y2, z2);
02446 for (i = 0; i < 2; i++)
02447 {
02448 for (j = 0; j < 2; j++)
02449 {
02450 for (k = 0; k < 2; k++)
02451 {
02452 neighbors[d].neighbors[x2 + i][y2 + j][z2 + k] = &node->parent->children[Cube::CornerIndex (i, j, k)];
02453 }
02454 }
02455 }
02456 Neighbors2& temp = getNeighbors (node->parent);
02457
02458
02459 i = x1 << 1;
02460 if (temp.neighbors[i][1][1] && temp.neighbors[i][1][1]->children)
02461 {
02462 for (j = 0; j < 2; j++)
02463 {
02464 for (k = 0; k < 2; k++)
02465 {
02466 neighbors[d].neighbors[i][y2 + j][z2 + k] = &temp.neighbors[i][1][1]->children[Cube::CornerIndex (x2, j, k)];
02467 }
02468 }
02469 }
02470 j = y1 << 1;
02471 if (temp.neighbors[1][j][1] && temp.neighbors[1][j][1]->children)
02472 {
02473 for (i = 0; i < 2; i++)
02474 {
02475 for (k = 0; k < 2; k++)
02476 {
02477 neighbors[d].neighbors[x2 + i][j][z2 + k] = &temp.neighbors[1][j][1]->children[Cube::CornerIndex (i, y2, k)];
02478 }
02479 }
02480 }
02481 k = z1 << 1;
02482 if (temp.neighbors[1][1][k] && temp.neighbors[1][1][k]->children)
02483 {
02484 for (i = 0; i < 2; i++)
02485 {
02486 for (j = 0; j < 2; j++)
02487 {
02488 neighbors[d].neighbors[x2 + i][y2 + j][k] = &temp.neighbors[1][1][k]->children[Cube::CornerIndex (i, j, z2)];
02489 }
02490 }
02491 }
02492
02493
02494 i = x1 << 1;
02495 j = y1 << 1;
02496 if (temp.neighbors[i][j][1] && temp.neighbors[i][j][1]->children)
02497 {
02498 for (k = 0; k < 2; k++)
02499 {
02500 neighbors[d].neighbors[i][j][z2 + k] = &temp.neighbors[i][j][1]->children[Cube::CornerIndex (x2, y2, k)];
02501 }
02502 }
02503 i = x1 << 1;
02504 k = z1 << 1;
02505 if (temp.neighbors[i][1][k] && temp.neighbors[i][1][k]->children)
02506 {
02507 for (j = 0; j < 2; j++)
02508 {
02509 neighbors[d].neighbors[i][y2 + j][k] = &temp.neighbors[i][1][k]->children[Cube::CornerIndex (x2, j, z2)];
02510 }
02511 }
02512 j = y1 << 1;
02513 k = z1 << 1;
02514 if (temp.neighbors[1][j][k] && temp.neighbors[1][j][k]->children)
02515 {
02516 for (i = 0; i < 2; i++)
02517 {
02518 neighbors[d].neighbors[x2 + i][j][k] = &temp.neighbors[1][j][k]->children[Cube::CornerIndex (i, y2, z2)];
02519 }
02520 }
02521
02522
02523 i = x1 << 1;
02524 j = y1 << 1;
02525 k = z1 << 1;
02526 if (temp.neighbors[i][j][k] && temp.neighbors[i][j][k]->children)
02527 {
02528 neighbors[d].neighbors[i][j][k] = &temp.neighbors[i][j][k]->children[Cube::CornerIndex (x2, y2, z2)];
02529 }
02530 }
02531 }
02532 return neighbors[node->depth ()];
02533 }
02534
02535 template <class NodeData, class Real>
02536 int OctNode<NodeData, Real>
02537 ::write (const char* fileName) const
02538 {
02539 FILE* fp = fopen (fileName, "wb");
02540 if (!fp)
02541 {
02542 return 0;
02543 }
02544 int ret = write (fp);
02545 fclose (fp);
02546 return ret;
02547 }
02548
02549 template <class NodeData, class Real>
02550 int OctNode<NodeData, Real>
02551 ::write (FILE* fp) const
02552 {
02553 fwrite (this, sizeof (OctNode<NodeData, Real>), 1, fp);
02554 if (children)
02555 {
02556 for (int i = 0; i < Cube::CORNERS; i++)
02557 {
02558 children[i].write (fp);
02559 }
02560 }
02561 return 1;
02562 }
02563
02564 template <class NodeData, class Real>
02565 int OctNode<NodeData, Real>
02566 ::read (const char* fileName)
02567 {
02568 FILE* fp = fopen (fileName, "rb");
02569 if (!fp)
02570 {
02571 return 0;
02572 }
02573 int ret = read (fp);
02574 fclose (fp);
02575 return ret;
02576 }
02577
02578 template <class NodeData, class Real>
02579 int OctNode<NodeData, Real>
02580 ::read (FILE* fp)
02581 {
02582 fread (this, sizeof (OctNode<NodeData, Real>), 1, fp);
02583 parent = NULL;
02584 if (children)
02585 {
02586 children = NULL;
02587 initChildren ();
02588 for (int i = 0; i < Cube::CORNERS; i++)
02589 {
02590 children[i].read (fp);
02591 children[i].parent = this;
02592 }
02593 }
02594 return 1;
02595 }
02596
02597 template<class NodeData, class Real>
02598 int OctNode<NodeData, Real>
02599 ::width (const int& maxDepth) const
02600 {
02601 int d = depth ();
02602 return 1 << (maxDepth - d);
02603 }
02604
02605 template<class NodeData, class Real>
02606 void OctNode<NodeData, Real>
02607 ::centerIndex (const int& maxDepth, int index[DIMENSION]) const
02608 {
02609 int d, o[3];
02610 depthAndOffset (d, o);
02611 for (int i = 0; i < DIMENSION; i++)
02612 {
02613 index[i] = BinaryNode<Real>::CornerIndex (maxDepth, d + 1, o[i] << 1, 1);
02614 }
02615 }
02616
02617
02618 }
02619 }
02620