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 "treeBoundBox.H"
00028 #include "ListOps.H"
00029
00030
00031
00032 const Foam::scalar Foam::treeBoundBox::great(GREAT);
00033
00034 const Foam::treeBoundBox Foam::treeBoundBox::greatBox
00035 (
00036 vector(-GREAT, -GREAT, -GREAT),
00037 vector(GREAT, GREAT, GREAT)
00038 );
00039
00040
00041 const Foam::treeBoundBox Foam::treeBoundBox::invertedBox
00042 (
00043 vector(GREAT, GREAT, GREAT),
00044 vector(-GREAT, -GREAT, -GREAT)
00045 );
00046
00047
00049 const Foam::label facesArray[6][4] =
00050 {
00051 {0, 4, 6, 2},
00052 {1, 3, 7, 5},
00053 {0, 1, 5, 4},
00054 {2, 6, 7, 3},
00055 {0, 2, 3, 1},
00056 {4, 5, 7, 6}
00057 };
00059
00060
00061 const Foam::faceList Foam::treeBoundBox::faces
00062 (
00063 initListList<face, label, 6, 4>(facesArray)
00064 );
00065
00066
00068 const Foam::label edgesArray[12][2] =
00069 {
00070 {0, 1},
00071 {1, 3},
00072 {2, 3},
00073 {0, 2},
00074 {4, 5},
00075 {5, 7},
00076 {6, 7},
00077 {4, 6},
00078 {0, 4},
00079 {1, 5},
00080 {3, 7},
00081 {2, 6}
00082 };
00084
00085
00086 const Foam::edgeList Foam::treeBoundBox::edges
00087 (
00088 initListList<edge, label, 12, 2>(edgesArray)
00089 );
00090
00091
00092 const Foam::FixedList<Foam::vector, 6> Foam::treeBoundBox::faceNormals
00093 (
00094 calcFaceNormals()
00095 );
00096
00097
00098
00099
00100 Foam::FixedList<Foam::vector, 6> Foam::treeBoundBox::calcFaceNormals()
00101 {
00102 FixedList<vector, 6> normals;
00103 normals[LEFT] = vector(-1, 0, 0);
00104 normals[RIGHT] = vector( 1, 0, 0);
00105 normals[BOTTOM] = vector( 0, -1, 0);
00106 normals[TOP] = vector( 0, 1, 0);
00107 normals[BACK] = vector( 0, 0, -1);
00108 normals[FRONT] = vector( 0, 0, 1);
00109 return normals;
00110 }
00111
00112
00113
00114
00115
00116 Foam::treeBoundBox::treeBoundBox(const UList<point>& points)
00117 :
00118 boundBox()
00119 {
00120 if (points.empty())
00121 {
00122 WarningIn
00123 (
00124 "treeBoundBox::treeBoundBox(const UList<point>&)"
00125 ) << "cannot find bounding box for zero-sized pointField"
00126 << "returning zero" << endl;
00127
00128 return;
00129 }
00130
00131 min() = points[0];
00132 max() = points[0];
00133
00134 for (label i = 1; i < points.size(); i++)
00135 {
00136 min() = ::Foam::min(min(), points[i]);
00137 max() = ::Foam::max(max(), points[i]);
00138 }
00139 }
00140
00141
00142
00143 Foam::treeBoundBox::treeBoundBox
00144 (
00145 const UList<point>& points,
00146 const UList<label>& meshPoints
00147 )
00148 :
00149 boundBox()
00150 {
00151 if (points.empty() || meshPoints.empty())
00152 {
00153 WarningIn
00154 (
00155 "treeBoundBox::treeBoundBox"
00156 "(const UList<point>&, const UList<label>&)"
00157 ) << "cannot find bounding box for zero-sized pointField"
00158 << "returning zero" << endl;
00159
00160 return;
00161 }
00162
00163 min() = points[meshPoints[0]];
00164 max() = points[meshPoints[0]];
00165
00166 for (label i = 1; i < meshPoints.size(); i++)
00167 {
00168 min() = ::Foam::min(min(), points[meshPoints[i]]);
00169 max() = ::Foam::max(max(), points[meshPoints[i]]);
00170 }
00171 }
00172
00173
00174
00175 Foam::treeBoundBox::treeBoundBox(Istream& is)
00176 :
00177 boundBox(is)
00178 {}
00179
00180
00181
00182
00183 Foam::pointField Foam::treeBoundBox::points() const
00184 {
00185 pointField points(8);
00186 forAll(points, octant)
00187 {
00188 points[octant] = corner(octant);
00189
00190 }
00191 return points;
00192 }
00193
00194
00195 Foam::treeBoundBox Foam::treeBoundBox::subBbox(const direction octant) const
00196 {
00197 return subBbox(midpoint(), octant);
00198 }
00199
00200
00201
00202 Foam::treeBoundBox Foam::treeBoundBox::subBbox
00203 (
00204 const point& mid,
00205 const direction octant
00206 ) const
00207 {
00208 if (octant > 7)
00209 {
00210 FatalErrorIn
00211 (
00212 "treeBoundBox::subBbox(const point&, const direction)"
00213 ) << "octant should be [0..7]"
00214 << abort(FatalError);
00215 }
00216
00217
00218 treeBoundBox subBb(*this);
00219 point& bbMin = subBb.min();
00220 point& bbMax = subBb.max();
00221
00222 if (octant & treeBoundBox::RIGHTHALF)
00223 {
00224 bbMin.x() = mid.x();
00225 }
00226 else
00227 {
00228 bbMax.x() = mid.x();
00229 }
00230
00231 if (octant & treeBoundBox::TOPHALF)
00232 {
00233 bbMin.y() = mid.y();
00234 }
00235 else
00236 {
00237 bbMax.y() = mid.y();
00238 }
00239
00240 if (octant & treeBoundBox::FRONTHALF)
00241 {
00242 bbMin.z() = mid.z();
00243 }
00244 else
00245 {
00246 bbMax.z() = mid.z();
00247 }
00248
00249 return subBb;
00250 }
00251
00252
00253 bool Foam::treeBoundBox::overlaps
00254 (
00255 const point& centre,
00256 const scalar radiusSqr
00257 ) const
00258 {
00259
00260
00261 scalar distSqr = 0;
00262
00263 for (direction dir = 0; dir < vector::nComponents; dir++)
00264 {
00265 scalar d0 = min()[dir] - centre[dir];
00266 scalar d1 = max()[dir] - centre[dir];
00267
00268 if ((d0 > 0) != (d1 > 0))
00269 {
00270
00271
00272 }
00273 else if (Foam::mag(d0) < Foam::mag(d1))
00274 {
00275 distSqr += d0*d0;
00276 }
00277 else
00278 {
00279 distSqr += d1*d1;
00280 }
00281
00282 if (distSqr > radiusSqr)
00283 {
00284 return false;
00285 }
00286 }
00287
00288 return true;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 bool Foam::treeBoundBox::intersects
00311 (
00312 const point& overallStart,
00313 const vector& overallVec,
00314 const point& start,
00315 const point& end,
00316 point& pt,
00317 direction& ptOnFaces
00318 ) const
00319 {
00320 const direction endBits = posBits(end);
00321 pt = start;
00322
00323 while (true)
00324 {
00325 direction ptBits = posBits(pt);
00326
00327 if (ptBits == 0)
00328 {
00329
00330 ptOnFaces = faceBits(pt);
00331 return true;
00332 }
00333
00334 if ((ptBits & endBits) != 0)
00335 {
00336
00337 ptOnFaces = faceBits(pt);
00338 return false;
00339 }
00340
00341 if (ptBits & LEFTBIT)
00342 {
00343
00344 if (Foam::mag(overallVec.x()) > VSMALL)
00345 {
00346 scalar s = (min().x() - overallStart.x())/overallVec.x();
00347 pt.x() = min().x();
00348 pt.y() = overallStart.y() + overallVec.y()*s;
00349 pt.z() = overallStart.z() + overallVec.z()*s;
00350 }
00351 else
00352 {
00353
00354
00355 pt.x() = min().x();
00356 }
00357 }
00358 else if (ptBits & RIGHTBIT)
00359 {
00360
00361 if (Foam::mag(overallVec.x()) > VSMALL)
00362 {
00363 scalar s = (max().x() - overallStart.x())/overallVec.x();
00364 pt.x() = max().x();
00365 pt.y() = overallStart.y() + overallVec.y()*s;
00366 pt.z() = overallStart.z() + overallVec.z()*s;
00367 }
00368 else
00369 {
00370 pt.x() = max().x();
00371 }
00372 }
00373 else if (ptBits & BOTTOMBIT)
00374 {
00375
00376 if (Foam::mag(overallVec.y()) > VSMALL)
00377 {
00378 scalar s = (min().y() - overallStart.y())/overallVec.y();
00379 pt.x() = overallStart.x() + overallVec.x()*s;
00380 pt.y() = min().y();
00381 pt.z() = overallStart.z() + overallVec.z()*s;
00382 }
00383 else
00384 {
00385 pt.x() = min().y();
00386 }
00387 }
00388 else if (ptBits & TOPBIT)
00389 {
00390
00391 if (Foam::mag(overallVec.y()) > VSMALL)
00392 {
00393 scalar s = (max().y() - overallStart.y())/overallVec.y();
00394 pt.x() = overallStart.x() + overallVec.x()*s;
00395 pt.y() = max().y();
00396 pt.z() = overallStart.z() + overallVec.z()*s;
00397 }
00398 else
00399 {
00400 pt.y() = max().y();
00401 }
00402 }
00403 else if (ptBits & BACKBIT)
00404 {
00405
00406 if (Foam::mag(overallVec.z()) > VSMALL)
00407 {
00408 scalar s = (min().z() - overallStart.z())/overallVec.z();
00409 pt.x() = overallStart.x() + overallVec.x()*s;
00410 pt.y() = overallStart.y() + overallVec.y()*s;
00411 pt.z() = min().z();
00412 }
00413 else
00414 {
00415 pt.z() = min().z();
00416 }
00417 }
00418 else if (ptBits & FRONTBIT)
00419 {
00420
00421 if (Foam::mag(overallVec.z()) > VSMALL)
00422 {
00423 scalar s = (max().z() - overallStart.z())/overallVec.z();
00424 pt.x() = overallStart.x() + overallVec.x()*s;
00425 pt.y() = overallStart.y() + overallVec.y()*s;
00426 pt.z() = max().z();
00427 }
00428 else
00429 {
00430 pt.z() = max().z();
00431 }
00432 }
00433 }
00434 }
00435
00436
00437 bool Foam::treeBoundBox::intersects
00438 (
00439 const point& start,
00440 const point& end,
00441 point& pt
00442 ) const
00443 {
00444 direction ptBits;
00445 return intersects(start, end-start, start, end, pt, ptBits);
00446 }
00447
00448
00449
00450 bool Foam::treeBoundBox::contains(const treeBoundBox& bb) const
00451 {
00452 return contains(bb.min()) && contains(bb.max());
00453 }
00454
00455
00456 bool Foam::treeBoundBox::contains(const vector& dir, const point& pt) const
00457 {
00458
00459
00460
00461 for (direction cmpt=0; cmpt<3; cmpt++)
00462 {
00463 if (pt[cmpt] < min()[cmpt])
00464 {
00465 return false;
00466 }
00467 else if (pt[cmpt] == min()[cmpt])
00468 {
00469
00470 if (dir[cmpt] < 0)
00471 {
00472 return false;
00473 }
00474 }
00475
00476 if (pt[cmpt] > max()[cmpt])
00477 {
00478 return false;
00479 }
00480 else if (pt[cmpt] == max()[cmpt])
00481 {
00482
00483 if (dir[cmpt] > 0)
00484 {
00485 return false;
00486 }
00487 }
00488 }
00489
00490
00491 return true;
00492 }
00493
00494
00495
00496 Foam::direction Foam::treeBoundBox::faceBits(const point& pt) const
00497 {
00498 direction faceBits = 0;
00499 if (pt.x() == min().x())
00500 {
00501 faceBits |= LEFTBIT;
00502 }
00503 else if (pt.x() == max().x())
00504 {
00505 faceBits |= RIGHTBIT;
00506 }
00507
00508 if (pt.y() == min().y())
00509 {
00510 faceBits |= BOTTOMBIT;
00511 }
00512 else if (pt.y() == max().y())
00513 {
00514 faceBits |= TOPBIT;
00515 }
00516
00517 if (pt.z() == min().z())
00518 {
00519 faceBits |= BACKBIT;
00520 }
00521 else if (pt.z() == max().z())
00522 {
00523 faceBits |= FRONTBIT;
00524 }
00525 return faceBits;
00526 }
00527
00528
00529
00530 Foam::direction Foam::treeBoundBox::posBits(const point& pt) const
00531 {
00532 direction posBits = 0;
00533
00534 if (pt.x() < min().x())
00535 {
00536 posBits |= LEFTBIT;
00537 }
00538 else if (pt.x() > max().x())
00539 {
00540 posBits |= RIGHTBIT;
00541 }
00542
00543 if (pt.y() < min().y())
00544 {
00545 posBits |= BOTTOMBIT;
00546 }
00547 else if (pt.y() > max().y())
00548 {
00549 posBits |= TOPBIT;
00550 }
00551
00552 if (pt.z() < min().z())
00553 {
00554 posBits |= BACKBIT;
00555 }
00556 else if (pt.z() > max().z())
00557 {
00558 posBits |= FRONTBIT;
00559 }
00560 return posBits;
00561 }
00562
00563
00564
00565
00566 void Foam::treeBoundBox::calcExtremities
00567 (
00568 const point& pt,
00569 point& nearest,
00570 point& furthest
00571 ) const
00572 {
00573 scalar nearX, nearY, nearZ;
00574 scalar farX, farY, farZ;
00575
00576 if (Foam::mag(min().x() - pt.x()) < Foam::mag(max().x() - pt.x()))
00577 {
00578 nearX = min().x();
00579 farX = max().x();
00580 }
00581 else
00582 {
00583 nearX = max().x();
00584 farX = min().x();
00585 }
00586
00587 if (Foam::mag(min().y() - pt.y()) < Foam::mag(max().y() - pt.y()))
00588 {
00589 nearY = min().y();
00590 farY = max().y();
00591 }
00592 else
00593 {
00594 nearY = max().y();
00595 farY = min().y();
00596 }
00597
00598 if (Foam::mag(min().z() - pt.z()) < Foam::mag(max().z() - pt.z()))
00599 {
00600 nearZ = min().z();
00601 farZ = max().z();
00602 }
00603 else
00604 {
00605 nearZ = max().z();
00606 farZ = min().z();
00607 }
00608
00609 nearest = point(nearX, nearY, nearZ);
00610 furthest = point(farX, farY, farZ);
00611 }
00612
00613
00614 Foam::scalar Foam::treeBoundBox::maxDist(const point& pt) const
00615 {
00616 point near, far;
00617 calcExtremities(pt, near, far);
00618
00619 return Foam::mag(far - pt);
00620 }
00621
00622
00623
00624
00625
00626 Foam::label Foam::treeBoundBox::distanceCmp
00627 (
00628 const point& pt,
00629 const treeBoundBox& other
00630 ) const
00631 {
00632
00633
00634
00635
00636 point nearThis, farThis;
00637
00638
00639 calcExtremities(pt, nearThis, farThis);
00640
00641 const scalar minDistThis =
00642 sqr(nearThis.x() - pt.x())
00643 + sqr(nearThis.y() - pt.y())
00644 + sqr(nearThis.z() - pt.z());
00645 const scalar maxDistThis =
00646 sqr(farThis.x() - pt.x())
00647 + sqr(farThis.y() - pt.y())
00648 + sqr(farThis.z() - pt.z());
00649
00650
00651
00652
00653
00654 point nearOther, farOther;
00655
00656
00657 other.calcExtremities(pt, nearOther, farOther);
00658
00659 const scalar minDistOther =
00660 sqr(nearOther.x() - pt.x())
00661 + sqr(nearOther.y() - pt.y())
00662 + sqr(nearOther.z() - pt.z());
00663 const scalar maxDistOther =
00664 sqr(farOther.x() - pt.x())
00665 + sqr(farOther.y() - pt.y())
00666 + sqr(farOther.z() - pt.z());
00667
00668
00669
00670
00671 if (maxDistThis < minDistOther)
00672 {
00673
00674 return -1;
00675 }
00676 else if (minDistThis > maxDistOther)
00677 {
00678
00679 return 1;
00680 }
00681 else
00682 {
00683
00684 return 0;
00685 }
00686 }
00687
00688
00689
00690
00691 bool Foam::operator==(const treeBoundBox& a, const treeBoundBox& b)
00692 {
00693 return operator==
00694 (
00695 static_cast<const boundBox&>(a),
00696 static_cast<const boundBox&>(b)
00697 );
00698 }
00699
00700
00701 bool Foam::operator!=(const treeBoundBox& a, const treeBoundBox& b)
00702 {
00703 return !(a == b);
00704 }
00705
00706
00707
00708
00709 Foam::Ostream& Foam::operator<<(Ostream& os, const treeBoundBox& bb)
00710 {
00711 return os << static_cast<const boundBox&>(bb);
00712 }
00713
00714
00715 Foam::Istream& Foam::operator>>(Istream& is, treeBoundBox& bb)
00716 {
00717 return is >> static_cast<boundBox&>(bb);
00718 }
00719
00720
00721