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 #include "indexedOctree.H"
00028 #include "linePointRef.H"
00029 #include "meshTools.H"
00030 #include "OFstream.H"
00031
00032
00033
00034 template <class Type>
00035 Foam::scalar Foam::indexedOctree<Type>::perturbTol_ = 10*SMALL;
00036
00037
00038
00039
00040
00041
00042 template <class Type>
00043 bool Foam::indexedOctree<Type>::overlaps
00044 (
00045 const point& p0,
00046 const point& p1,
00047 const scalar nearestDistSqr,
00048 const point& sample
00049 )
00050 {
00051
00052
00053 scalar distSqr = 0;
00054
00055 for (direction dir = 0; dir < vector::nComponents; dir++)
00056 {
00057 scalar d0 = p0[dir] - sample[dir];
00058 scalar d1 = p1[dir] - sample[dir];
00059
00060 if ((d0 > 0) != (d1 > 0))
00061 {
00062
00063
00064 }
00065 else if (mag(d0) < mag(d1))
00066 {
00067 distSqr += d0*d0;
00068 }
00069 else
00070 {
00071 distSqr += d1*d1;
00072 }
00073
00074 if (distSqr > nearestDistSqr)
00075 {
00076 return false;
00077 }
00078 }
00079
00080 return true;
00081 }
00082
00083
00084
00085
00086 template <class Type>
00087 bool Foam::indexedOctree<Type>::overlaps
00088 (
00089 const treeBoundBox& parentBb,
00090 const direction octant,
00091 const scalar nearestDistSqr,
00092 const point& sample
00093 )
00094 {
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 const point& min = parentBb.min();
00106 const point& max = parentBb.max();
00107
00108 point other;
00109
00110 if (octant & treeBoundBox::RIGHTHALF)
00111 {
00112 other.x() = max.x();
00113 }
00114 else
00115 {
00116 other.x() = min.x();
00117 }
00118
00119 if (octant & treeBoundBox::TOPHALF)
00120 {
00121 other.y() = max.y();
00122 }
00123 else
00124 {
00125 other.y() = min.y();
00126 }
00127
00128 if (octant & treeBoundBox::FRONTHALF)
00129 {
00130 other.z() = max.z();
00131 }
00132 else
00133 {
00134 other.z() = min.z();
00135 }
00136
00137 const point mid(0.5*(min+max));
00138
00139 return overlaps(mid, other, nearestDistSqr, sample);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 template <class Type>
00150 void Foam::indexedOctree<Type>::divide
00151 (
00152 const labelList& indices,
00153 const treeBoundBox& bb,
00154 labelListList& result
00155 ) const
00156 {
00157 List<DynamicList<label> > subIndices(8);
00158 for (direction octant = 0; octant < subIndices.size(); octant++)
00159 {
00160 subIndices[octant].setCapacity(indices.size()/8);
00161 }
00162
00163
00164 FixedList<treeBoundBox, 8> subBbs;
00165 for (direction octant = 0; octant < subBbs.size(); octant++)
00166 {
00167 subBbs[octant] = bb.subBbox(octant);
00168 }
00169
00170 forAll(indices, i)
00171 {
00172 label shapeI = indices[i];
00173
00174 for (direction octant = 0; octant < 8; octant++)
00175 {
00176 if (shapes_.overlaps(shapeI, subBbs[octant]))
00177 {
00178 subIndices[octant].append(shapeI);
00179 }
00180 }
00181 }
00182
00183 result.setSize(8);
00184 for (direction octant = 0; octant < subIndices.size(); octant++)
00185 {
00186 result[octant].transfer(subIndices[octant]);
00187 }
00188 }
00189
00190
00191
00192 template <class Type>
00193 typename Foam::indexedOctree<Type>::node
00194 Foam::indexedOctree<Type>::divide
00195 (
00196 const treeBoundBox& bb,
00197 DynamicList<labelList>& contents,
00198 const label contentI
00199 ) const
00200 {
00201 const labelList& indices = contents[contentI];
00202
00203 node nod;
00204
00205 if
00206 (
00207 bb.min()[0] >= bb.max()[0]
00208 || bb.min()[1] >= bb.max()[1]
00209 || bb.min()[2] >= bb.max()[2]
00210 )
00211 {
00212 FatalErrorIn("indexedOctree<Type>::divide(..)")
00213 << "Badly formed bounding box:" << bb
00214 << abort(FatalError);
00215 }
00216
00217 nod.bb_ = bb;
00218 nod.parent_ = -1;
00219
00220 labelListList dividedIndices(8);
00221 divide(indices, bb, dividedIndices);
00222
00223
00224
00225
00226 bool replaced = false;
00227
00228 for (direction octant = 0; octant < dividedIndices.size(); octant++)
00229 {
00230 labelList& subIndices = dividedIndices[octant];
00231
00232 if (subIndices.size())
00233 {
00234 if (!replaced)
00235 {
00236 contents[contentI].transfer(subIndices);
00237 nod.subNodes_[octant] = contentPlusOctant(contentI, octant);
00238 replaced = true;
00239 }
00240 else
00241 {
00242
00243
00244 label sz = contents.size();
00245 contents.append(labelList(0));
00246 contents[sz].transfer(subIndices);
00247 nod.subNodes_[octant] = contentPlusOctant(sz, octant);
00248 }
00249 }
00250 else
00251 {
00252
00253 nod.subNodes_[octant] = emptyPlusOctant(octant);
00254 }
00255 }
00256
00257 return nod;
00258 }
00259
00260
00261
00262 template <class Type>
00263 void Foam::indexedOctree<Type>::splitNodes
00264 (
00265 const label minSize,
00266 DynamicList<indexedOctree<Type>::node>& nodes,
00267 DynamicList<labelList>& contents
00268 ) const
00269 {
00270 label currentSize = nodes.size();
00271
00272
00273
00274
00275 for (label nodeI = 0; nodeI < currentSize; nodeI++)
00276 {
00277 for
00278 (
00279 direction octant = 0;
00280 octant < nodes[nodeI].subNodes_.size();
00281 octant++
00282 )
00283 {
00284 labelBits index = nodes[nodeI].subNodes_[octant];
00285
00286 if (isNode(index))
00287 {
00288
00289 }
00290 else if (isContent(index))
00291 {
00292 label contentI = getContent(index);
00293
00294 if (contents[contentI].size() > minSize)
00295 {
00296
00297
00298
00299 const node& nod = nodes[nodeI];
00300 const treeBoundBox bb(nod.bb_.subBbox(octant));
00301
00302 node subNode(divide(bb, contents, contentI));
00303 subNode.parent_ = nodeI;
00304 label sz = nodes.size();
00305 nodes.append(subNode);
00306 nodes[nodeI].subNodes_[octant] = nodePlusOctant(sz, octant);
00307 }
00308 }
00309 }
00310 }
00311 }
00312
00313
00314
00315
00316 template <class Type>
00317 Foam::label Foam::indexedOctree<Type>::compactContents
00318 (
00319 DynamicList<node>& nodes,
00320 DynamicList<labelList>& contents,
00321 const label compactLevel,
00322 const label nodeI,
00323 const label level,
00324
00325 List<labelList>& compactedContents,
00326 label& compactI
00327 )
00328 {
00329 const node& nod = nodes[nodeI];
00330
00331 label nNodes = 0;
00332
00333 if (level < compactLevel)
00334 {
00335 for (direction octant = 0; octant < nod.subNodes_.size(); octant++)
00336 {
00337 labelBits index = nod.subNodes_[octant];
00338
00339 if (isNode(index))
00340 {
00341 nNodes += compactContents
00342 (
00343 nodes,
00344 contents,
00345 compactLevel,
00346 getNode(index),
00347 level+1,
00348 compactedContents,
00349 compactI
00350 );
00351 }
00352 }
00353 }
00354 else if (level == compactLevel)
00355 {
00356
00357 for (direction octant = 0; octant < nod.subNodes_.size(); octant++)
00358 {
00359 labelBits index = nod.subNodes_[octant];
00360
00361 if (isContent(index))
00362 {
00363 label contentI = getContent(index);
00364
00365 compactedContents[compactI].transfer(contents[contentI]);
00366
00367
00368 nodes[nodeI].subNodes_[octant] =
00369 contentPlusOctant(compactI, octant);
00370
00371 compactI++;
00372 }
00373 else if (isNode(index))
00374 {
00375 nNodes++;
00376 }
00377 }
00378 }
00379 return nNodes;
00380 }
00381
00382
00383
00384
00385
00386 template <class Type>
00387 typename Foam::indexedOctree<Type>::volumeType
00388 Foam::indexedOctree<Type>::calcVolumeType
00389 (
00390 const label nodeI
00391 ) const
00392 {
00393 const node& nod = nodes_[nodeI];
00394
00395 volumeType myType = UNKNOWN;
00396
00397 for (direction octant = 0; octant < nod.subNodes_.size(); octant++)
00398 {
00399 volumeType subType;
00400
00401 labelBits index = nod.subNodes_[octant];
00402
00403 if (isNode(index))
00404 {
00405
00406 subType = calcVolumeType(getNode(index));
00407 }
00408 else if (isContent(index))
00409 {
00410
00411
00412 subType = MIXED;
00413 }
00414 else
00415 {
00416
00417
00418 const treeBoundBox subBb = nod.bb_.subBbox(octant);
00419
00420 subType = volumeType
00421 (
00422 shapes_.getVolumeType(*this, subBb.midpoint())
00423 );
00424 }
00425
00426
00427 nodeTypes_.set((nodeI<<3)+octant, subType);
00428
00429
00430
00431 if (myType == UNKNOWN)
00432 {
00433 myType = subType;
00434 }
00435 else if (subType != myType)
00436 {
00437 myType = MIXED;
00438 }
00439 }
00440 return myType;
00441 }
00442
00443
00444 template <class Type>
00445 typename Foam::indexedOctree<Type>::volumeType
00446 Foam::indexedOctree<Type>::getVolumeType
00447 (
00448 const label nodeI,
00449 const point& sample
00450 ) const
00451 {
00452 const node& nod = nodes_[nodeI];
00453
00454 direction octant = nod.bb_.subOctant(sample);
00455
00456 volumeType octantType = volumeType(nodeTypes_.get((nodeI<<3)+octant));
00457
00458 if (octantType == INSIDE)
00459 {
00460 return octantType;
00461 }
00462 else if (octantType == OUTSIDE)
00463 {
00464 return octantType;
00465 }
00466 else if (octantType == UNKNOWN)
00467 {
00468
00469 return octantType;
00470 }
00471 else if (octantType == MIXED)
00472 {
00473 labelBits index = nod.subNodes_[octant];
00474
00475 if (isNode(index))
00476 {
00477
00478 volumeType subType = getVolumeType(getNode(index), sample);
00479
00480 return subType;
00481 }
00482 else if (isContent(index))
00483 {
00484
00485 return volumeType(shapes_.getVolumeType(*this, sample));
00486 }
00487 else
00488 {
00489
00490
00491 FatalErrorIn
00492 (
00493 "indexedOctree<Type>::getVolumeType"
00494 "(const label, const point&)"
00495 ) << "Sample:" << sample << " node:" << nodeI
00496 << " with bb:" << nodes_[nodeI].bb_ << nl
00497 << "Empty subnode has invalid volume type MIXED."
00498 << abort(FatalError);
00499
00500 return UNKNOWN;
00501 }
00502 }
00503 else
00504 {
00505 FatalErrorIn
00506 (
00507 "indexedOctree<Type>::getVolumeType"
00508 "(const label, const point&)"
00509 ) << "Sample:" << sample << " at node:" << nodeI
00510 << " octant:" << octant
00511 << " with bb:" << nod.bb_.subBbox(octant) << nl
00512 << "Node has invalid volume type " << octantType
00513 << abort(FatalError);
00514
00515 return UNKNOWN;
00516 }
00517 }
00518
00519
00520 template <class Type>
00521 typename Foam::indexedOctree<Type>::volumeType
00522 Foam::indexedOctree<Type>::getSide
00523 (
00524 const vector& outsideNormal,
00525 const vector& vec
00526 )
00527 {
00528 if ((outsideNormal&vec) >= 0)
00529 {
00530 return OUTSIDE;
00531 }
00532 else
00533 {
00534 return INSIDE;
00535 }
00536 }
00537
00538
00539
00540
00541
00542
00543
00544
00545 template <class Type>
00546 void Foam::indexedOctree<Type>::findNearest
00547 (
00548 const label nodeI,
00549 const point& sample,
00550
00551 scalar& nearestDistSqr,
00552 label& nearestShapeI,
00553 point& nearestPoint
00554 ) const
00555 {
00556 const node& nod = nodes_[nodeI];
00557
00558
00559 FixedList<direction, 8> octantOrder;
00560 nod.bb_.searchOrder(sample, octantOrder);
00561
00562
00563 for (direction i = 0; i < 8; i++)
00564 {
00565 direction octant = octantOrder[i];
00566
00567 labelBits index = nod.subNodes_[octant];
00568
00569 if (isNode(index))
00570 {
00571 label subNodeI = getNode(index);
00572
00573 const treeBoundBox& subBb = nodes_[subNodeI].bb_;
00574
00575 if (overlaps(subBb.min(), subBb.max(), nearestDistSqr, sample))
00576 {
00577 findNearest
00578 (
00579 subNodeI,
00580 sample,
00581
00582 nearestDistSqr,
00583 nearestShapeI,
00584 nearestPoint
00585 );
00586 }
00587 }
00588 else if (isContent(index))
00589 {
00590 if
00591 (
00592 overlaps
00593 (
00594 nod.bb_,
00595 octant,
00596 nearestDistSqr,
00597 sample
00598 )
00599 )
00600 {
00601 shapes_.findNearest
00602 (
00603 contents_[getContent(index)],
00604 sample,
00605
00606 nearestDistSqr,
00607 nearestShapeI,
00608 nearestPoint
00609 );
00610 }
00611 }
00612 }
00613 }
00614
00615
00616
00617 template <class Type>
00618 void Foam::indexedOctree<Type>::findNearest
00619 (
00620 const label nodeI,
00621 const linePointRef& ln,
00622
00623 treeBoundBox& tightest,
00624 label& nearestShapeI,
00625 point& linePoint,
00626 point& nearestPoint
00627 ) const
00628 {
00629 const node& nod = nodes_[nodeI];
00630 const treeBoundBox& nodeBb = nod.bb_;
00631
00632
00633 FixedList<direction, 8> octantOrder;
00634 nod.bb_.searchOrder(ln.centre(), octantOrder);
00635
00636
00637 for (direction i = 0; i < 8; i++)
00638 {
00639 direction octant = octantOrder[i];
00640
00641 labelBits index = nod.subNodes_[octant];
00642
00643 if (isNode(index))
00644 {
00645 const treeBoundBox& subBb = nodes_[getNode(index)].bb_;
00646
00647 if (subBb.overlaps(tightest))
00648 {
00649 findNearest
00650 (
00651 getNode(index),
00652 ln,
00653
00654 tightest,
00655 nearestShapeI,
00656 linePoint,
00657 nearestPoint
00658 );
00659 }
00660 }
00661 else if (isContent(index))
00662 {
00663 const treeBoundBox subBb(nodeBb.subBbox(octant));
00664
00665 if (subBb.overlaps(tightest))
00666 {
00667 shapes_.findNearest
00668 (
00669 contents_[getContent(index)],
00670 ln,
00671
00672 tightest,
00673 nearestShapeI,
00674 linePoint,
00675 nearestPoint
00676 );
00677 }
00678 }
00679 }
00680 }
00681
00682
00683 template <class Type>
00684 Foam::treeBoundBox Foam::indexedOctree<Type>::subBbox
00685 (
00686 const label parentNodeI,
00687 const direction octant
00688 ) const
00689 {
00690
00691 const node& nod = nodes_[parentNodeI];
00692 labelBits index = nod.subNodes_[octant];
00693
00694 if (isNode(index))
00695 {
00696
00697 return nodes_[getNode(index)].bb_;
00698 }
00699 else
00700 {
00701
00702 return nod.bb_.subBbox(octant);
00703 }
00704 }
00705
00706
00707
00708
00709 template <class Type>
00710 Foam::point Foam::indexedOctree<Type>::pushPoint
00711 (
00712 const treeBoundBox& bb,
00713 const point& pt,
00714 const bool pushInside
00715 )
00716 {
00717
00718 const vector perturbVec = perturbTol_*(bb.span());
00719
00720 point perturbedPt(pt);
00721
00722
00723
00724
00725 if (pushInside)
00726 {
00727 for (direction dir = 0; dir < vector::nComponents; dir++)
00728 {
00729 if (mag(pt[dir]-bb.min()[dir]) < mag(perturbVec[dir]))
00730 {
00731
00732 scalar perturbDist = perturbVec[dir] + ROOTVSMALL;
00733 perturbedPt[dir] = bb.min()[dir] + perturbDist;
00734 }
00735 else if (mag(pt[dir]-bb.max()[dir]) < mag(perturbVec[dir]))
00736 {
00737
00738 scalar perturbDist = perturbVec[dir] + ROOTVSMALL;
00739 perturbedPt[dir] = bb.max()[dir] - perturbDist;
00740 }
00741 }
00742 }
00743 else
00744 {
00745 for (direction dir = 0; dir < vector::nComponents; dir++)
00746 {
00747 if (mag(pt[dir]-bb.min()[dir]) < mag(perturbVec[dir]))
00748 {
00749 scalar perturbDist = perturbVec[dir] + ROOTVSMALL;
00750 perturbedPt[dir] = bb.min()[dir] - perturbDist;
00751 }
00752 else if (mag(pt[dir]-bb.max()[dir]) < mag(perturbVec[dir]))
00753 {
00754 scalar perturbDist = perturbVec[dir] + ROOTVSMALL;
00755 perturbedPt[dir] = bb.max()[dir] + perturbDist;
00756 }
00757 }
00758 }
00759
00760 if (debug)
00761 {
00762 if (pushInside != bb.contains(perturbedPt))
00763 {
00764 FatalErrorIn("indexedOctree<Type>::pushPoint(..)")
00765 << "pushed point:" << pt
00766 << " to:" << perturbedPt
00767 << " wanted side:" << pushInside
00768 << " obtained side:" << bb.contains(perturbedPt)
00769 << " of bb:" << bb
00770 << abort(FatalError);
00771 }
00772 }
00773
00774 return perturbedPt;
00775 }
00776
00777
00778
00779
00780 template <class Type>
00781 Foam::point Foam::indexedOctree<Type>::pushPoint
00782 (
00783 const treeBoundBox& bb,
00784 const direction faceID,
00785 const point& pt,
00786 const bool pushInside
00787 )
00788 {
00789
00790 const vector perturbVec = perturbTol_*bb.span();
00791
00792 point perturbedPt(pt);
00793
00794
00795
00796
00797 if (faceID == 0)
00798 {
00799 FatalErrorIn("indexedOctree<Type>::pushPoint(..)")
00800 << abort(FatalError);
00801 }
00802
00803 if (faceID & treeBoundBox::LEFTBIT)
00804 {
00805 if (pushInside)
00806 {
00807 perturbedPt[0] = bb.min()[0] + (perturbVec[0] + ROOTVSMALL);
00808 }
00809 else
00810 {
00811 perturbedPt[0] = bb.min()[0] - (perturbVec[0] + ROOTVSMALL);
00812 }
00813 }
00814 else if (faceID & treeBoundBox::RIGHTBIT)
00815 {
00816 if (pushInside)
00817 {
00818 perturbedPt[0] = bb.max()[0] - (perturbVec[0] + ROOTVSMALL);
00819 }
00820 else
00821 {
00822 perturbedPt[0] = bb.max()[0] + (perturbVec[0] + ROOTVSMALL);
00823 }
00824 }
00825
00826 if (faceID & treeBoundBox::BOTTOMBIT)
00827 {
00828 if (pushInside)
00829 {
00830 perturbedPt[1] = bb.min()[1] + (perturbVec[1] + ROOTVSMALL);
00831 }
00832 else
00833 {
00834 perturbedPt[1] = bb.min()[1] - (perturbVec[1] + ROOTVSMALL);
00835 }
00836 }
00837 else if (faceID & treeBoundBox::TOPBIT)
00838 {
00839 if (pushInside)
00840 {
00841 perturbedPt[1] = bb.max()[1] - (perturbVec[1] + ROOTVSMALL);
00842 }
00843 else
00844 {
00845 perturbedPt[1] = bb.max()[1] + (perturbVec[1] + ROOTVSMALL);
00846 }
00847 }
00848
00849 if (faceID & treeBoundBox::BACKBIT)
00850 {
00851 if (pushInside)
00852 {
00853 perturbedPt[2] = bb.min()[2] + (perturbVec[2] + ROOTVSMALL);
00854 }
00855 else
00856 {
00857 perturbedPt[2] = bb.min()[2] - (perturbVec[2] + ROOTVSMALL);
00858 }
00859 }
00860 else if (faceID & treeBoundBox::FRONTBIT)
00861 {
00862 if (pushInside)
00863 {
00864 perturbedPt[2] = bb.max()[2] - (perturbVec[2] + ROOTVSMALL);
00865 }
00866 else
00867 {
00868 perturbedPt[2] = bb.max()[2] + (perturbVec[2] + ROOTVSMALL);
00869 }
00870 }
00871
00872 if (debug)
00873 {
00874 if (pushInside != bb.contains(perturbedPt))
00875 {
00876 FatalErrorIn("indexedOctree<Type>::pushPoint(..)")
00877 << "pushed point:" << pt << " on face:" << faceString(faceID)
00878 << " to:" << perturbedPt
00879 << " wanted side:" << pushInside
00880 << " obtained side:" << bb.contains(perturbedPt)
00881 << " of bb:" << bb
00882 << abort(FatalError);
00883 }
00884 }
00885
00886 return perturbedPt;
00887 }
00888
00889
00890
00891
00892
00893 template <class Type>
00894 Foam::point Foam::indexedOctree<Type>::pushPointIntoFace
00895 (
00896 const treeBoundBox& bb,
00897 const vector& dir,
00898 const point& pt
00899 )
00900 {
00901 if (debug)
00902 {
00903 if (bb.posBits(pt) != 0)
00904 {
00905 FatalErrorIn("indexedOctree<Type>::pushPointIntoFace(..)")
00906 << " bb:" << bb << endl
00907 << "does not contain point " << pt << abort(FatalError);
00908 }
00909 }
00910
00911
00912
00913
00914
00915
00916 direction ptFaceID = bb.faceBits(pt);
00917
00918 direction nFaces = 0;
00919 FixedList<direction, 3> faceIndices;
00920
00921 if (ptFaceID & treeBoundBox::LEFTBIT)
00922 {
00923 faceIndices[nFaces++] = treeBoundBox::LEFT;
00924 }
00925 else if (ptFaceID & treeBoundBox::RIGHTBIT)
00926 {
00927 faceIndices[nFaces++] = treeBoundBox::RIGHT;
00928 }
00929
00930 if (ptFaceID & treeBoundBox::BOTTOMBIT)
00931 {
00932 faceIndices[nFaces++] = treeBoundBox::BOTTOM;
00933 }
00934 else if (ptFaceID & treeBoundBox::TOPBIT)
00935 {
00936 faceIndices[nFaces++] = treeBoundBox::TOP;
00937 }
00938
00939 if (ptFaceID & treeBoundBox::BACKBIT)
00940 {
00941 faceIndices[nFaces++] = treeBoundBox::BACK;
00942 }
00943 else if (ptFaceID & treeBoundBox::FRONTBIT)
00944 {
00945 faceIndices[nFaces++] = treeBoundBox::FRONT;
00946 }
00947
00948
00949
00950
00951 direction keepFaceID;
00952
00953 if (nFaces == 0)
00954 {
00955
00956 return pt;
00957 }
00958 else if (nFaces == 1)
00959 {
00960 keepFaceID = faceIndices[0];
00961 }
00962 else
00963 {
00964
00965
00966
00967 keepFaceID = faceIndices[0];
00968 scalar maxInproduct = mag(treeBoundBox::faceNormals[keepFaceID] & dir);
00969
00970 for (direction i = 1; i < nFaces; i++)
00971 {
00972 direction face = faceIndices[i];
00973 scalar s = mag(treeBoundBox::faceNormals[face] & dir);
00974 if (s > maxInproduct)
00975 {
00976 maxInproduct = s;
00977 keepFaceID = face;
00978 }
00979 }
00980 }
00981
00982
00983
00984
00985 point facePoint(pushPoint(bb, pt, true));
00986 direction faceID = 0;
00987
00988
00989
00990 if (keepFaceID == treeBoundBox::LEFT)
00991 {
00992 facePoint.x() = bb.min().x();
00993 faceID = treeBoundBox::LEFTBIT;
00994 }
00995 else if (keepFaceID == treeBoundBox::RIGHT)
00996 {
00997 facePoint.x() = bb.max().x();
00998 faceID = treeBoundBox::RIGHTBIT;
00999 }
01000 else if (keepFaceID == treeBoundBox::BOTTOM)
01001 {
01002 facePoint.y() = bb.min().y();
01003 faceID = treeBoundBox::BOTTOMBIT;
01004 }
01005 else if (keepFaceID == treeBoundBox::TOP)
01006 {
01007 facePoint.y() = bb.max().y();
01008 faceID = treeBoundBox::TOPBIT;
01009 }
01010 else if (keepFaceID == treeBoundBox::BACK)
01011 {
01012 facePoint.z() = bb.min().z();
01013 faceID = treeBoundBox::BACKBIT;
01014 }
01015 else if (keepFaceID == treeBoundBox::FRONT)
01016 {
01017 facePoint.z() = bb.max().z();
01018 faceID = treeBoundBox::FRONTBIT;
01019 }
01020
01021
01022 if (debug)
01023 {
01024 if (faceID != bb.faceBits(facePoint))
01025 {
01026 FatalErrorIn("indexedOctree<Type>::pushPointIntoFace(..)")
01027 << "Pushed point from " << pt
01028 << " on face:" << ptFaceID << " of bb:" << bb << endl
01029 << "onto " << facePoint
01030 << " on face:" << faceID
01031 << " which is not consistent with geometric face "
01032 << bb.faceBits(facePoint)
01033 << abort(FatalError);
01034 }
01035 if (bb.posBits(facePoint) != 0)
01036 {
01037 FatalErrorIn("indexedOctree<Type>::pushPointIntoFace(..)")
01038 << " bb:" << bb << endl
01039 << "does not contain perturbed point "
01040 << facePoint << abort(FatalError);
01041 }
01042 }
01043
01044
01045 return facePoint;
01046 }
01047
01048
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247 template <class Type>
01248 bool Foam::indexedOctree<Type>::walkToParent
01249 (
01250 const label nodeI,
01251 const direction octant,
01252
01253 label& parentNodeI,
01254 label& parentOctant
01255 ) const
01256 {
01257 parentNodeI = nodes_[nodeI].parent_;
01258
01259 if (parentNodeI == -1)
01260 {
01261
01262 return false;
01263 }
01264
01265 const node& parentNode = nodes_[parentNodeI];
01266
01267
01268 parentOctant = 255;
01269
01270 for (direction i = 0; i < parentNode.subNodes_.size(); i++)
01271 {
01272 labelBits index = parentNode.subNodes_[i];
01273
01274 if (isNode(index) && getNode(index) == nodeI)
01275 {
01276 parentOctant = i;
01277 break;
01278 }
01279 }
01280
01281 if (parentOctant == 255)
01282 {
01283 FatalErrorIn("walkToParent(..)")
01284 << "Problem: no parent found for octant:" << octant
01285 << " node:" << nodeI
01286 << abort(FatalError);
01287 }
01288
01289 return true;
01290 }
01291
01292
01293
01294
01295
01296
01297 template <class Type>
01298 bool Foam::indexedOctree<Type>::walkToNeighbour
01299 (
01300 const point& facePoint,
01301 const direction faceID,
01302 label& nodeI,
01303 direction& octant
01304 ) const
01305 {
01306 label oldNodeI = nodeI;
01307 direction oldOctant = octant;
01308
01309
01310
01311
01312
01313
01314 const direction X = treeBoundBox::RIGHTHALF;
01315 const direction Y = treeBoundBox::TOPHALF;
01316 const direction Z = treeBoundBox::FRONTHALF;
01317
01318 direction octantMask = 0;
01319 direction wantedValue = 0;
01320
01321 if ((faceID & treeBoundBox::LEFTBIT) != 0)
01322 {
01323
01324 octantMask |= X;
01325 wantedValue |= X;
01326 }
01327 else if ((faceID & treeBoundBox::RIGHTBIT) != 0)
01328 {
01329 octantMask |= X;
01330 }
01331
01332 if ((faceID & treeBoundBox::BOTTOMBIT) != 0)
01333 {
01334
01335 octantMask |= Y;
01336 wantedValue |= Y;
01337 }
01338 else if ((faceID & treeBoundBox::TOPBIT) != 0)
01339 {
01340
01341 octantMask |= Y;
01342 }
01343
01344 if ((faceID & treeBoundBox::BACKBIT) != 0)
01345 {
01346 octantMask |= Z;
01347 wantedValue |= Z;
01348 }
01349 else if ((faceID & treeBoundBox::FRONTBIT) != 0)
01350 {
01351 octantMask |= Z;
01352 }
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381 while (wantedValue != (octant & octantMask))
01382 {
01383
01384
01385
01386
01387 if (wantedValue & X)
01388 {
01389
01390 if (octant & X)
01391 {
01392
01393 octantMask &= ~X;
01394 wantedValue &= ~X;
01395 }
01396 }
01397 else
01398 {
01399 if (!(octant & X))
01400 {
01401
01402 octantMask &= ~X;
01403 wantedValue &= ~X;
01404 }
01405 }
01406
01407 if (wantedValue & Y)
01408 {
01409 if (octant & Y)
01410 {
01411 octantMask &= ~Y;
01412 wantedValue &= ~Y;
01413 }
01414 }
01415 else
01416 {
01417 if (!(octant & Y))
01418 {
01419 octantMask &= ~Y;
01420 wantedValue &= ~Y;
01421 }
01422 }
01423
01424 if (wantedValue & Z)
01425 {
01426 if (octant & Z)
01427 {
01428 octantMask &= ~Z;
01429 wantedValue &= ~Z;
01430 }
01431 }
01432 else
01433 {
01434 if (!(octant & Z))
01435 {
01436 octantMask &= ~Z;
01437 wantedValue &= ~Z;
01438 }
01439 }
01440
01441
01442 label parentNodeI;
01443 label parentOctant;
01444 walkToParent(nodeI, octant, parentNodeI, parentOctant);
01445
01446 if (parentNodeI == -1)
01447 {
01448
01449 return false;
01450 }
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461 nodeI = parentNodeI;
01462 octant = parentOctant;
01463 }
01464
01465
01466
01467
01468 octant ^= octantMask;
01469
01470
01471
01472
01473
01474 if (debug)
01475 {
01476 const treeBoundBox subBb(subBbox(nodeI, octant));
01477
01478 if (!subBb.contains(facePoint))
01479 {
01480 FatalErrorIn("indexedOctree<Type>::walkToNeighbour(..)")
01481 << "When searching for " << facePoint
01482 << " ended up in node:" << nodeI
01483 << " octant:" << octant
01484 << " with bb:" << subBb
01485 << abort(FatalError);
01486 }
01487 }
01488
01489
01490
01491
01492 labelBits index = nodes_[nodeI].subNodes_[octant];
01493
01494 if (isNode(index))
01495 {
01496 labelBits node = findNode(getNode(index), facePoint);
01497
01498 nodeI = getNode(node);
01499 octant = getOctant(node);
01500 }
01501
01502
01503 if (debug)
01504 {
01505 const treeBoundBox subBb(subBbox(nodeI, octant));
01506
01507 if (nodeI == oldNodeI && octant == oldOctant)
01508 {
01509 FatalErrorIn("indexedOctree<Type>::walkToNeighbour(..)")
01510 << "Did not go to neighbour when searching for " << facePoint
01511 << endl
01512 << " starting from face:" << faceString(faceID)
01513 << " node:" << nodeI
01514 << " octant:" << octant
01515 << " bb:" << subBb
01516 << abort(FatalError);
01517 }
01518
01519 if (!subBb.contains(facePoint))
01520 {
01521 FatalErrorIn("indexedOctree<Type>::walkToNeighbour(..)")
01522 << "When searching for " << facePoint
01523 << " ended up in node:" << nodeI
01524 << " octant:" << octant
01525 << " bb:" << subBb
01526 << abort(FatalError);
01527 }
01528 }
01529
01530
01531 return true;
01532 }
01533
01534
01535 template <class Type>
01536 Foam::word Foam::indexedOctree<Type>::faceString
01537 (
01538 const direction faceID
01539 )
01540 {
01541 word desc;
01542
01543 if (faceID == 0)
01544 {
01545 desc = "noFace";
01546 }
01547 if (faceID & treeBoundBox::LEFTBIT)
01548 {
01549 if (!desc.empty()) desc += "+";
01550 desc += "left";
01551 }
01552 if (faceID & treeBoundBox::RIGHTBIT)
01553 {
01554 if (!desc.empty()) desc += "+";
01555 desc += "right";
01556 }
01557 if (faceID & treeBoundBox::BOTTOMBIT)
01558 {
01559 if (!desc.empty()) desc += "+";
01560 desc += "bottom";
01561 }
01562 if (faceID & treeBoundBox::TOPBIT)
01563 {
01564 if (!desc.empty()) desc += "+";
01565 desc += "top";
01566 }
01567 if (faceID & treeBoundBox::BACKBIT)
01568 {
01569 if (!desc.empty()) desc += "+";
01570 desc += "back";
01571 }
01572 if (faceID & treeBoundBox::FRONTBIT)
01573 {
01574 if (!desc.empty()) desc += "+";
01575 desc += "front";
01576 }
01577 return desc;
01578 }
01579
01580
01581
01582
01583
01584
01585
01586
01587 template <class Type>
01588 void Foam::indexedOctree<Type>::traverseNode
01589 (
01590 const bool findAny,
01591 const point& treeStart,
01592 const vector& treeVec,
01593
01594 const point& start,
01595 const point& end,
01596 const label nodeI,
01597 const direction octant,
01598
01599 pointIndexHit& hitInfo,
01600 direction& hitBits
01601 ) const
01602 {
01603 if (debug)
01604 {
01605 const treeBoundBox octantBb(subBbox(nodeI, octant));
01606
01607 if (octantBb.posBits(start) != 0)
01608 {
01609 FatalErrorIn("indexedOctree<Type>::traverseNode(..)")
01610 << "Node:" << nodeI << " octant:" << octant
01611 << " bb:" << octantBb << endl
01612 << "does not contain point " << start << abort(FatalError);
01613 }
01614 }
01615
01616
01617 const node& nod = nodes_[nodeI];
01618
01619 labelBits index = nod.subNodes_[octant];
01620
01621 if (isContent(index))
01622 {
01623 const labelList& indices = contents_[getContent(index)];
01624
01625 if (findAny)
01626 {
01627
01628
01629 forAll(indices, elemI)
01630 {
01631 label shapeI = indices[elemI];
01632
01633 point pt;
01634 bool hit = shapes_.intersects(shapeI, start, end, pt);
01635
01636 if (hit)
01637 {
01638
01639
01640 hitInfo.setHit();
01641 hitInfo.setIndex(shapeI);
01642 hitInfo.setPoint(pt);
01643 return;
01644 }
01645 }
01646 }
01647 else
01648 {
01649
01650
01651 point nearestPoint(end);
01652
01653 forAll(indices, elemI)
01654 {
01655 label shapeI = indices[elemI];
01656
01657 point pt;
01658 bool hit = shapes_.intersects(shapeI, start, nearestPoint, pt);
01659
01660 if (hit)
01661 {
01662
01663 nearestPoint = pt;
01664
01665 hitInfo.setHit();
01666 hitInfo.setIndex(shapeI);
01667 hitInfo.setPoint(pt);
01668 }
01669 }
01670
01671 if (hitInfo.hit())
01672 {
01673
01674 return;
01675 }
01676 }
01677 }
01678
01679
01680
01681
01682
01683
01684 const treeBoundBox octantBb(subBbox(nodeI, octant));
01685
01686 point pt;
01687 bool intersected = octantBb.intersects
01688 (
01689 end,
01690 (start-end),
01691
01692 end,
01693 start,
01694
01695 pt,
01696 hitBits
01697 );
01698
01699
01700 if (intersected)
01701 {
01702
01703 hitInfo.setPoint(pt);
01704 }
01705 else
01706 {
01707
01708
01709
01710
01711
01712 point perturbedEnd(pushPoint(octantBb, end, false));
01713
01714
01715
01716 {
01717
01718 writeOBJ(nodeI, octant);
01719
01720 {
01721 OFstream str("ray.obj");
01722 meshTools::writeOBJ(str, start);
01723 meshTools::writeOBJ(str, end);
01724 str << "l 1 2" << nl;
01725 }
01726 WarningIn("indexedOctree<Type>::traverseNode(..)")
01727 << "Did not intersect ray from endpoint:" << end
01728 << " to startpoint:" << start
01729 << " with bounding box:" << octantBb << nl
01730 << "Re-intersecting with perturbed endpoint:" << perturbedEnd
01731 << endl;
01732 }
01733
01734 traverseNode
01735 (
01736 findAny,
01737 treeStart,
01738 treeVec,
01739 start,
01740 perturbedEnd,
01741 nodeI,
01742 octant,
01743
01744 hitInfo,
01745 hitBits
01746 );
01747 }
01748 }
01749
01750
01751
01752 template <class Type>
01753 Foam::pointIndexHit Foam::indexedOctree<Type>::findLine
01754 (
01755 const bool findAny,
01756 const point& treeStart,
01757 const point& treeEnd,
01758 const label startNodeI,
01759 const direction startOctant,
01760 const bool verbose
01761 ) const
01762 {
01763 const vector treeVec(treeEnd - treeStart);
01764
01765
01766 label nodeI = startNodeI;
01767 direction octant = startOctant;
01768
01769 if (verbose)
01770 {
01771 Pout<< "findLine : treeStart:" << treeStart
01772 << " treeEnd:" << treeEnd << endl
01773 << "node:" << nodeI
01774 << " octant:" << octant
01775 << " bb:" << subBbox(nodeI, octant) << endl;
01776 }
01777
01778
01779 pointIndexHit hitInfo(false, treeStart, -1);
01780
01781
01782 label i = 0;
01783 for (; i < 100000; i++)
01784 {
01785 if (verbose)
01786 {
01787 Pout<< "iter:" << i
01788 << " at startPoint:" << hitInfo.rawPoint() << endl
01789 << " node:" << nodeI
01790 << " octant:" << octant
01791 << " bb:" << subBbox(nodeI, octant) << endl;
01792 }
01793
01794
01795
01796
01797
01798 const treeBoundBox octantBb(subBbox(nodeI, octant));
01799
01800
01801 point startPoint
01802 (
01803 pushPointIntoFace
01804 (
01805 octantBb,
01806 treeVec,
01807 hitInfo.rawPoint()
01808 )
01809 );
01810
01811
01812 direction hitFaceID = 0;
01813
01814 traverseNode
01815 (
01816 findAny,
01817 treeStart,
01818 treeVec,
01819
01820 startPoint,
01821
01822 treeEnd,
01823 nodeI,
01824 octant,
01825
01826 hitInfo,
01827 hitFaceID
01828 );
01829
01830
01831 if (hitInfo.hit())
01832 {
01833 break;
01834 }
01835
01836 if (hitFaceID == 0)
01837 {
01838
01839 break;
01840 }
01841
01842
01843 if (verbose)
01844 {
01845 Pout<< " iter:" << i
01846 << " hit face:" << faceString(hitFaceID)
01847 << " at:" << hitInfo.rawPoint() << nl
01848 << " node:" << nodeI
01849 << " octant:" << octant
01850 << " bb:" << subBbox(nodeI, octant) << nl
01851 << " walking to neighbour containing:"
01852 << pushPoint
01853 (
01854 octantBb,
01855 hitFaceID,
01856 hitInfo.rawPoint(),
01857 false
01858 )
01859 << endl;
01860 }
01861
01862
01863
01864
01865
01866
01867 bool ok = walkToNeighbour
01868 (
01869 pushPoint
01870 (
01871 octantBb,
01872 hitFaceID,
01873 hitInfo.rawPoint(),
01874 false
01875 ),
01876 hitFaceID,
01877
01878 nodeI,
01879 octant
01880 );
01881
01882 if (!ok)
01883 {
01884
01885 break;
01886 }
01887
01888 if (verbose)
01889 {
01890 const treeBoundBox octantBb(subBbox(nodeI, octant));
01891 Pout<< " walked for point:" << hitInfo.rawPoint() << endl
01892 << " to neighbour node:" << nodeI
01893 << " octant:" << octant
01894 << " face:" << faceString(octantBb.faceBits(hitInfo.rawPoint()))
01895 << " of octantBb:" << octantBb << endl
01896 << endl;
01897 }
01898 }
01899
01900 if (i == 100000)
01901 {
01902
01903 if (!verbose)
01904 {
01905
01906 return findLine
01907 (
01908 findAny,
01909 treeStart,
01910 treeEnd,
01911 startNodeI,
01912 startOctant,
01913 true
01914 );
01915 }
01916 if (debug)
01917 {
01918 FatalErrorIn("indexedOctree<Type>::findLine(..)")
01919 << "Got stuck in loop raytracing from:" << treeStart
01920 << " to:" << treeEnd << endl
01921 << "inside top box:" << subBbox(startNodeI, startOctant)
01922 << abort(FatalError);
01923 }
01924 else
01925 {
01926 WarningIn("indexedOctree<Type>::findLine(..)")
01927 << "Got stuck in loop raytracing from:" << treeStart
01928 << " to:" << treeEnd << endl
01929 << "inside top box:" << subBbox(startNodeI, startOctant)
01930 << endl;
01931 }
01932 }
01933
01934 return hitInfo;
01935 }
01936
01937
01938
01939 template <class Type>
01940 Foam::pointIndexHit Foam::indexedOctree<Type>::findLine
01941 (
01942 const bool findAny,
01943 const point& start,
01944 const point& end
01945 ) const
01946 {
01947 pointIndexHit hitInfo;
01948
01949 if (nodes_.size())
01950 {
01951 const treeBoundBox& treeBb = nodes_[0].bb_;
01952
01953
01954
01955
01956 direction startBit = treeBb.posBits(start);
01957 direction endBit = treeBb.posBits(end);
01958
01959 if ((startBit & endBit) != 0)
01960 {
01961
01962 return pointIndexHit(false, vector::zero, -1);
01963 }
01964
01965
01966
01967
01968 point trackStart(start);
01969 point trackEnd(end);
01970
01971 if (startBit != 0)
01972 {
01973
01974 if (!treeBb.intersects(start, end, trackStart))
01975 {
01976 return pointIndexHit(false, vector::zero, -1);
01977 }
01978 }
01979
01980 if (endBit != 0)
01981 {
01982
01983 if (!treeBb.intersects(end, trackStart, trackEnd))
01984 {
01985 return pointIndexHit(false, vector::zero, -1);
01986 }
01987 }
01988
01989
01990
01991 labelBits index = findNode(0, trackStart);
01992
01993 label parentNodeI = getNode(index);
01994 direction octant = getOctant(index);
01995
01996 hitInfo = findLine
01997 (
01998 findAny,
01999 trackStart,
02000 trackEnd,
02001 parentNodeI,
02002 octant
02003 );
02004 }
02005
02006 return hitInfo;
02007 }
02008
02009
02010 template <class Type>
02011 void Foam::indexedOctree<Type>::findBox
02012 (
02013 const label nodeI,
02014 const treeBoundBox& searchBox,
02015 labelHashSet& elements
02016 ) const
02017 {
02018 const node& nod = nodes_[nodeI];
02019 const treeBoundBox& nodeBb = nod.bb_;
02020
02021 for (direction octant = 0; octant < nod.subNodes_.size(); octant++)
02022 {
02023 labelBits index = nod.subNodes_[octant];
02024
02025 if (isNode(index))
02026 {
02027 const treeBoundBox& subBb = nodes_[getNode(index)].bb_;
02028
02029 if (subBb.overlaps(searchBox))
02030 {
02031 findBox(getNode(index), searchBox, elements);
02032 }
02033 }
02034 else if (isContent(index))
02035 {
02036 const treeBoundBox subBb(nodeBb.subBbox(octant));
02037
02038 if (subBb.overlaps(searchBox))
02039 {
02040 const labelList& indices = contents_[getContent(index)];
02041
02042 forAll(indices, i)
02043 {
02044 label shapeI = indices[i];
02045
02046 if (shapes_.overlaps(shapeI, searchBox))
02047 {
02048 elements.insert(shapeI);
02049 }
02050 }
02051 }
02052 }
02053 }
02054 }
02055
02056
02057
02058 template <class Type>
02059 Foam::label Foam::indexedOctree<Type>::countElements
02060 (
02061 const labelBits index
02062 ) const
02063 {
02064 label nElems = 0;
02065
02066 if (isNode(index))
02067 {
02068
02069 label nodeI = getNode(index);
02070
02071 const node& nod = nodes_[nodeI];
02072
02073 for (direction octant = 0; octant < nod.subNodes_.size(); octant++)
02074 {
02075 nElems += countElements(nod.subNodes_[octant]);
02076 }
02077 }
02078 else if (isContent(index))
02079 {
02080 nElems += contents_[getContent(index)].size();
02081 }
02082 else
02083 {
02084
02085 }
02086
02087 return nElems;
02088 }
02089
02090
02091 template <class Type>
02092 void Foam::indexedOctree<Type>::writeOBJ
02093 (
02094 const label nodeI,
02095 const direction octant
02096 ) const
02097 {
02098 OFstream str
02099 (
02100 "node" + Foam::name(nodeI) + "_octant" + Foam::name(octant) + ".obj"
02101 );
02102
02103 labelBits index = nodes_[nodeI].subNodes_[octant];
02104
02105 treeBoundBox subBb;
02106
02107 if (isNode(index))
02108 {
02109 subBb = nodes_[getNode(index)].bb_;
02110 }
02111 else if (isContent(index))
02112 {
02113 subBb = nodes_[nodeI].bb_.subBbox(octant);
02114 }
02115
02116 Pout<< "dumpContentNode : writing node:" << nodeI << " octant:" << octant
02117 << " to " << str.name() << endl;
02118
02119 label vertI = 0;
02120
02121
02122 pointField bbPoints(subBb.points());
02123
02124 label pointVertI = vertI;
02125 forAll(bbPoints, i)
02126 {
02127 meshTools::writeOBJ(str, bbPoints[i]);
02128 vertI++;
02129 }
02130
02131 forAll(treeBoundBox::edges, i)
02132 {
02133 const edge& e = treeBoundBox::edges[i];
02134
02135 str<< "l " << e[0]+pointVertI+1 << ' ' << e[1]+pointVertI+1 << nl;
02136 }
02137 }
02138
02139
02140
02141
02142 template <class Type>
02143 Foam::indexedOctree<Type>::indexedOctree(const Type& shapes)
02144 :
02145 shapes_(shapes),
02146 nodes_(0),
02147 contents_(0),
02148 nodeTypes_(0)
02149 {}
02150
02151
02152 template <class Type>
02153 Foam::indexedOctree<Type>::indexedOctree
02154 (
02155 const Type& shapes,
02156 const List<node>& nodes,
02157 const labelListList& contents
02158 )
02159 :
02160 shapes_(shapes),
02161 nodes_(nodes),
02162 contents_(contents),
02163 nodeTypes_(0)
02164 {}
02165
02166
02167 template <class Type>
02168 Foam::indexedOctree<Type>::indexedOctree
02169 (
02170 const Type& shapes,
02171 const treeBoundBox& bb,
02172 const label maxLevels,
02173 const scalar maxLeafRatio,
02174 const scalar maxDuplicity
02175 )
02176 :
02177 shapes_(shapes),
02178 nodes_(0),
02179 contents_(0),
02180 nodeTypes_(0)
02181 {
02182 if (debug)
02183 {
02184 Pout<< "indexedOctree<Type>::indexedOctree:" << nl
02185 << " shapes:" << shapes.size() << nl
02186 << " bb:" << bb << nl
02187 << endl;
02188 }
02189
02190 if (shapes.size() == 0)
02191 {
02192 return;
02193 }
02194
02195
02196 DynamicList<node> nodes(label(shapes.size() / maxLeafRatio));
02197 DynamicList<labelList> contents(label(shapes.size() / maxLeafRatio));
02198 contents.append(identity(shapes.size()));
02199
02200
02201 node topNode(divide(bb, contents, 0));
02202 nodes.append(topNode);
02203
02204
02205
02206
02207
02208 label nLevels = 1;
02209
02210 for (; nLevels < maxLevels; nLevels++)
02211 {
02212
02213 label nEntries = 0;
02214 forAll(contents, i)
02215 {
02216 nEntries += contents[i].size();
02217 }
02218
02219 if (debug)
02220 {
02221 Pout<< "indexedOctree<Type>::indexedOctree:" << nl
02222 << " nLevels:" << nLevels << nl
02223 << " nEntries per treeLeaf:" << nEntries/contents.size()
02224 << nl
02225 << " nEntries per shape (duplicity):"
02226 << nEntries/shapes.size()
02227 << nl
02228 << endl;
02229 }
02230
02231 if
02232 (
02233
02234
02235 nEntries > maxDuplicity*shapes.size()
02236 )
02237 {
02238 break;
02239 }
02240
02241
02242
02243 label nOldNodes = nodes.size();
02244 splitNodes
02245 (
02246 label(maxLeafRatio),
02247 nodes,
02248 contents
02249 );
02250
02251 if (nOldNodes == nodes.size())
02252 {
02253 break;
02254 }
02255 }
02256
02257
02258 nodes.shrink();
02259 contents.shrink();
02260
02261
02262
02263
02264
02265 contents_.setSize(contents.size());
02266 label compactI = 0;
02267
02268 label level = 0;
02269
02270 while (true)
02271 {
02272 compactContents
02273 (
02274 nodes,
02275 contents,
02276 level,
02277 0,
02278 0,
02279 contents_,
02280 compactI
02281 );
02282
02283 if (compactI == contents_.size())
02284 {
02285
02286 break;
02287 }
02288
02289 level++;
02290 }
02291 nodes_.transfer(nodes);
02292 nodes.clear();
02293
02294 if (debug)
02295 {
02296 label nEntries = 0;
02297 forAll(contents_, i)
02298 {
02299 nEntries += contents_[i].size();
02300 }
02301
02302 Pout<< "indexedOctree<Type>::indexedOctree"
02303 << " : finished construction of tree of:" << shapes.typeName
02304 << nl
02305 << " bb:" << this->bb() << nl
02306 << " shapes:" << shapes.size() << nl
02307 << " nLevels:" << nLevels << nl
02308 << " treeNodes:" << nodes_.size() << nl
02309 << " nEntries:" << nEntries << nl
02310 << " per treeLeaf:"
02311 << scalar(nEntries)/contents.size() << nl
02312 << " per shape (duplicity):"
02313 << scalar(nEntries)/shapes.size() << nl
02314 << endl;
02315 }
02316 }
02317
02318
02319 template <class Type>
02320 Foam::indexedOctree<Type>::indexedOctree
02321 (
02322 const Type& shapes,
02323 Istream& is
02324 )
02325 :
02326 shapes_(shapes),
02327 nodes_(is),
02328 contents_(is),
02329 nodeTypes_(0)
02330 {}
02331
02332
02333
02334
02335 template <class Type>
02336 Foam::scalar& Foam::indexedOctree<Type>::perturbTol()
02337 {
02338 return perturbTol_;
02339 }
02340
02341
02342 template <class Type>
02343 Foam::pointIndexHit Foam::indexedOctree<Type>::findNearest
02344 (
02345 const point& sample,
02346 const scalar startDistSqr
02347 ) const
02348 {
02349 scalar nearestDistSqr = startDistSqr;
02350 label nearestShapeI = -1;
02351 point nearestPoint;
02352
02353 if (nodes_.size())
02354 {
02355 findNearest
02356 (
02357 0,
02358 sample,
02359
02360 nearestDistSqr,
02361 nearestShapeI,
02362 nearestPoint
02363 );
02364 }
02365 else
02366 {
02367 nearestPoint = vector::zero;
02368 }
02369
02370 return pointIndexHit(nearestShapeI != -1, nearestPoint, nearestShapeI);
02371 }
02372
02373
02374 template <class Type>
02375 Foam::pointIndexHit Foam::indexedOctree<Type>::findNearest
02376 (
02377 const linePointRef& ln,
02378 treeBoundBox& tightest,
02379 point& linePoint
02380 ) const
02381 {
02382 label nearestShapeI = -1;
02383 point nearestPoint;
02384
02385 if (nodes_.size())
02386 {
02387 findNearest
02388 (
02389 0,
02390 ln,
02391
02392 tightest,
02393 nearestShapeI,
02394 linePoint,
02395 nearestPoint
02396 );
02397 }
02398 else
02399 {
02400 nearestPoint = vector::zero;
02401 }
02402
02403 return pointIndexHit(nearestShapeI != -1, nearestPoint, nearestShapeI);
02404 }
02405
02406
02407
02408 template <class Type>
02409 Foam::pointIndexHit Foam::indexedOctree<Type>::findLine
02410 (
02411 const point& start,
02412 const point& end
02413 ) const
02414 {
02415 return findLine(false, start, end);
02416 }
02417
02418
02419
02420 template <class Type>
02421 Foam::pointIndexHit Foam::indexedOctree<Type>::findLineAny
02422 (
02423 const point& start,
02424 const point& end
02425 ) const
02426 {
02427 return findLine(true, start, end);
02428 }
02429
02430
02431 template <class Type>
02432 Foam::labelList Foam::indexedOctree<Type>::findBox
02433 (
02434 const boundBox& searchBox
02435 ) const
02436 {
02437
02438 labelHashSet elements(shapes_.size() / 100);
02439
02440 if (nodes_.size())
02441 {
02442 findBox(0, searchBox, elements);
02443 }
02444
02445 return elements.toc();
02446 }
02447
02448
02449
02450 template <class Type>
02451 Foam::labelBits Foam::indexedOctree<Type>::findNode
02452 (
02453 const label nodeI,
02454 const point& sample
02455 ) const
02456 {
02457 if (nodes_.empty())
02458 {
02459
02460 return nodePlusOctant(nodeI, 0);
02461 }
02462
02463 const node& nod = nodes_[nodeI];
02464
02465 if (debug)
02466 {
02467 if (!nod.bb_.contains(sample))
02468 {
02469 FatalErrorIn("findNode(..)")
02470 << "Cannot find " << sample << " in node " << nodeI
02471 << abort(FatalError);
02472 }
02473 }
02474
02475 direction octant = nod.bb_.subOctant(sample);
02476
02477 labelBits index = nod.subNodes_[octant];
02478
02479 if (isNode(index))
02480 {
02481
02482 return findNode(getNode(index), sample);
02483 }
02484 else if (isContent(index))
02485 {
02486
02487 return nodePlusOctant(nodeI, octant);
02488 }
02489 else
02490 {
02491
02492 return nodePlusOctant(nodeI, octant);
02493 }
02494 }
02495
02496
02497
02498 template <class Type>
02499 typename Foam::indexedOctree<Type>::volumeType
02500 Foam::indexedOctree<Type>::getVolumeType
02501 (
02502 const point& sample
02503 ) const
02504 {
02505 if (nodes_.empty())
02506 {
02507 return UNKNOWN;
02508 }
02509
02510 if (nodeTypes_.size() != 8*nodes_.size())
02511 {
02512
02513
02514 nodeTypes_.setSize(8*nodes_.size());
02515 nodeTypes_ = UNKNOWN;
02516
02517 calcVolumeType(0);
02518
02519 if (debug)
02520 {
02521 label nUNKNOWN = 0;
02522 label nMIXED = 0;
02523 label nINSIDE = 0;
02524 label nOUTSIDE = 0;
02525
02526 forAll(nodeTypes_, i)
02527 {
02528 volumeType type = volumeType(nodeTypes_.get(i));
02529
02530 if (type == UNKNOWN)
02531 {
02532 nUNKNOWN++;
02533 }
02534 else if (type == MIXED)
02535 {
02536 nMIXED++;
02537 }
02538 else if (type == INSIDE)
02539 {
02540 nINSIDE++;
02541 }
02542 else if (type == OUTSIDE)
02543 {
02544 nOUTSIDE++;
02545 }
02546 else
02547 {
02548 FatalErrorIn("getVolumeType") << abort(FatalError);
02549 }
02550 }
02551
02552 Pout<< "indexedOctree<Type>::getVolumeType : "
02553 << " bb:" << bb()
02554 << " nodes_:" << nodes_.size()
02555 << " nodeTypes_:" << nodeTypes_.size()
02556 << " nUNKNOWN:" << nUNKNOWN
02557 << " nMIXED:" << nMIXED
02558 << " nINSIDE:" << nINSIDE
02559 << " nOUTSIDE:" << nOUTSIDE
02560 << endl;
02561 }
02562 }
02563
02564 return getVolumeType(0, sample);
02565 }
02566
02567
02568
02569 template <class Type>
02570 void Foam::indexedOctree<Type>::print
02571 (
02572 prefixOSstream& os,
02573 const bool printContents,
02574 const label nodeI
02575 ) const
02576 {
02577 const node& nod = nodes_[nodeI];
02578 const treeBoundBox& bb = nod.bb_;
02579
02580 os << "nodeI:" << nodeI << " bb:" << bb << nl
02581 << "parent:" << nod.parent_ << nl
02582 << "n:" << countElements(nodePlusOctant(nodeI, 0)) << nl;
02583
02584 for (direction octant = 0; octant < nod.subNodes_.size(); octant++)
02585 {
02586 const treeBoundBox subBb(bb.subBbox(octant));
02587
02588 labelBits index = nod.subNodes_[octant];
02589
02590 if (isNode(index))
02591 {
02592
02593 label subNodeI = getNode(index);
02594
02595 os << "octant:" << octant
02596 << " node: n:" << countElements(index)
02597 << " bb:" << subBb << endl;
02598
02599 string oldPrefix = os.prefix();
02600 os.prefix() = " " + oldPrefix;
02601
02602 print(os, printContents, subNodeI);
02603
02604 os.prefix() = oldPrefix;
02605 }
02606 else if (isContent(index))
02607 {
02608 const labelList& indices = contents_[getContent(index)];
02609
02610 os << "octant:" << octant
02611 << " content: n:" << indices.size()
02612 << " bb:" << subBb;
02613
02614 if (printContents)
02615 {
02616 os << " contents:";
02617 forAll(indices, j)
02618 {
02619 os << ' ' << indices[j];
02620 }
02621 }
02622 os << endl;
02623 }
02624 else
02625 {
02626 os << "octant:" << octant << " empty:" << subBb << endl;
02627 }
02628 }
02629 }
02630
02631
02632
02633 template <class Type>
02634 bool Foam::indexedOctree<Type>::write(Ostream& os) const
02635 {
02636 os << *this;
02637
02638 return os.good();
02639 }
02640
02641
02642 template <class Type>
02643 Foam::Ostream& Foam::operator<<(Ostream& os, const indexedOctree<Type>& t)
02644 {
02645 return
02646 os << t.bb() << token::SPACE << t.nodes()
02647 << token::SPACE << t.contents();
02648 }
02649
02650
02651