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 "searchableBox.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "SortableList.H"
00030
00031
00032
00033 namespace Foam
00034 {
00035 defineTypeNameAndDebug(searchableBox, 0);
00036 addToRunTimeSelectionTable(searchableSurface, searchableBox, dict);
00037 }
00038
00039
00040
00041
00042 void Foam::searchableBox::projectOntoCoordPlane
00043 (
00044 const direction dir,
00045 const point& planePt,
00046 pointIndexHit& info
00047 ) const
00048 {
00049
00050 info.rawPoint()[dir] = planePt[dir];
00051
00052 if (planePt[dir] == min()[dir])
00053 {
00054 info.setIndex(dir*2);
00055 }
00056 else if (planePt[dir] == max()[dir])
00057 {
00058 info.setIndex(dir*2+1);
00059 }
00060 else
00061 {
00062 FatalErrorIn("searchableBox::projectOntoCoordPlane(..)")
00063 << "Point on plane " << planePt
00064 << " is not on coordinate " << min()[dir]
00065 << " nor " << max()[dir] << abort(FatalError);
00066 }
00067 }
00068
00069
00070
00071 Foam::pointIndexHit Foam::searchableBox::findNearest
00072 (
00073 const point& bbMid,
00074 const point& sample,
00075 const scalar nearestDistSqr
00076 ) const
00077 {
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 pointIndexHit info(true, sample, -1);
00091 bool outside = false;
00092
00093
00094 point near;
00095
00096 for (direction dir = 0; dir < vector::nComponents; dir++)
00097 {
00098 if (info.rawPoint()[dir] < min()[dir])
00099 {
00100 projectOntoCoordPlane(dir, min(), info);
00101 outside = true;
00102 }
00103 else if (info.rawPoint()[dir] > max()[dir])
00104 {
00105 projectOntoCoordPlane(dir, max(), info);
00106 outside = true;
00107 }
00108 else if (info.rawPoint()[dir] > bbMid[dir])
00109 {
00110 near[dir] = max()[dir];
00111 }
00112 else
00113 {
00114 near[dir] = min()[dir];
00115 }
00116 }
00117
00118
00119
00120
00121 if (!outside)
00122 {
00123 vector dist(cmptMag(info.rawPoint() - near));
00124
00125 if (dist.x() < dist.y())
00126 {
00127 if (dist.x() < dist.z())
00128 {
00129
00130 projectOntoCoordPlane(vector::X, near, info);
00131 }
00132 else
00133 {
00134 projectOntoCoordPlane(vector::Z, near, info);
00135 }
00136 }
00137 else
00138 {
00139 if (dist.y() < dist.z())
00140 {
00141 projectOntoCoordPlane(vector::Y, near, info);
00142 }
00143 else
00144 {
00145 projectOntoCoordPlane(vector::Z, near, info);
00146 }
00147 }
00148 }
00149
00150
00151
00152
00153 if (magSqr(info.rawPoint() - sample) > nearestDistSqr)
00154 {
00155 info.setMiss();
00156 info.setIndex(-1);
00157 }
00158
00159 return info;
00160 }
00161
00162
00163
00164
00165 Foam::searchableBox::searchableBox
00166 (
00167 const IOobject& io,
00168 const treeBoundBox& bb
00169 )
00170 :
00171 searchableSurface(io),
00172 treeBoundBox(bb)
00173 {
00174 if (!contains(midpoint()))
00175 {
00176 FatalErrorIn
00177 (
00178 "Foam::searchableBox::searchableBox\n"
00179 "(\n"
00180 " const IOobject& io,\n"
00181 " const treeBoundBox& bb\n"
00182 ")\n"
00183 ) << "Illegal bounding box specification : "
00184 << static_cast<const treeBoundBox>(*this) << exit(FatalError);
00185 }
00186 }
00187
00188
00189 Foam::searchableBox::searchableBox
00190 (
00191 const IOobject& io,
00192 const dictionary& dict
00193 )
00194 :
00195 searchableSurface(io),
00196 treeBoundBox(dict.lookup("min"), dict.lookup("max"))
00197 {
00198 if (!contains(midpoint()))
00199 {
00200 FatalErrorIn
00201 (
00202 "Foam::searchableBox::searchableBox\n"
00203 "(\n"
00204 " const IOobject& io,\n"
00205 " const treeBoundBox& bb\n"
00206 ")\n"
00207 ) << "Illegal bounding box specification : "
00208 << static_cast<const treeBoundBox>(*this) << exit(FatalError);
00209 }
00210 }
00211
00212
00213
00214
00215 Foam::searchableBox::~searchableBox()
00216 {}
00217
00218
00219
00220
00221 const Foam::wordList& Foam::searchableBox::regions() const
00222 {
00223 if (regions_.empty())
00224 {
00225 regions_.setSize(1);
00226 regions_[0] = "region0";
00227 }
00228 return regions_;
00229 }
00230
00231
00232 Foam::pointIndexHit Foam::searchableBox::findNearest
00233 (
00234 const point& sample,
00235 const scalar nearestDistSqr
00236 ) const
00237 {
00238 return findNearest(midpoint(), sample, nearestDistSqr);
00239 }
00240
00241
00242 Foam::pointIndexHit Foam::searchableBox::findNearestOnEdge
00243 (
00244 const point& sample,
00245 const scalar nearestDistSqr
00246 ) const
00247 {
00248 const point bbMid(midpoint());
00249
00250
00251 pointIndexHit info(true, sample, -1);
00252 bool outside = false;
00253
00254
00255 point near;
00256
00257 for (direction dir = 0; dir < vector::nComponents; dir++)
00258 {
00259 if (info.rawPoint()[dir] < min()[dir])
00260 {
00261 projectOntoCoordPlane(dir, min(), info);
00262 outside = true;
00263 }
00264 else if (info.rawPoint()[dir] > max()[dir])
00265 {
00266 projectOntoCoordPlane(dir, max(), info);
00267 outside = true;
00268 }
00269 else if (info.rawPoint()[dir] > bbMid[dir])
00270 {
00271 near[dir] = max()[dir];
00272 }
00273 else
00274 {
00275 near[dir] = min()[dir];
00276 }
00277 }
00278
00279
00280
00281
00282 if (!outside)
00283 {
00284
00285 vector dist(cmptMag(info.rawPoint() - near));
00286
00287 SortableList<scalar> sortedDist(3);
00288 sortedDist[0] = dist[0];
00289 sortedDist[1] = dist[1];
00290 sortedDist[2] = dist[2];
00291 sortedDist.sort();
00292
00293
00294 projectOntoCoordPlane(sortedDist.indices()[0], near, info);
00295
00296 projectOntoCoordPlane(sortedDist.indices()[1], near, info);
00297 }
00298
00299
00300
00301
00302 if (magSqr(info.rawPoint() - sample) > nearestDistSqr)
00303 {
00304 info.setMiss();
00305 info.setIndex(-1);
00306 }
00307
00308 return info;
00309 }
00310
00311
00312 Foam::pointIndexHit Foam::searchableBox::findNearest
00313 (
00314 const linePointRef& ln,
00315 treeBoundBox& tightest,
00316 point& linePoint
00317 ) const
00318 {
00319 notImplemented
00320 (
00321 "searchableBox::findNearest"
00322 "(const linePointRef&, treeBoundBox&, point&)"
00323 );
00324 return pointIndexHit();
00325 }
00326
00327
00328 Foam::pointIndexHit Foam::searchableBox::findLine
00329 (
00330 const point& start,
00331 const point& end
00332 ) const
00333 {
00334 pointIndexHit info(false, start, -1);
00335
00336 bool foundInter;
00337
00338 if (posBits(start) == 0)
00339 {
00340 if (posBits(end) == 0)
00341 {
00342
00343 foundInter = false;
00344 }
00345 else
00346 {
00347
00348 foundInter = intersects(end, start, info.rawPoint());
00349 }
00350 }
00351 else
00352 {
00353
00354 foundInter = intersects(start, end, info.rawPoint());
00355 }
00356
00357
00358
00359 if (foundInter)
00360 {
00361 info.setHit();
00362
00363 for (direction dir = 0; dir < vector::nComponents; dir++)
00364 {
00365 if (info.rawPoint()[dir] == min()[dir])
00366 {
00367 info.setIndex(2*dir);
00368 break;
00369 }
00370 else if (info.rawPoint()[dir] == max()[dir])
00371 {
00372 info.setIndex(2*dir+1);
00373 break;
00374 }
00375 }
00376
00377 if (info.index() == -1)
00378 {
00379 FatalErrorIn("searchableBox::findLine(const point&, const point&)")
00380 << "point " << info.rawPoint()
00381 << " on segment " << start << end
00382 << " should be on face of " << *this
00383 << " but it isn't." << abort(FatalError);
00384 }
00385 }
00386
00387 return info;
00388 }
00389
00390
00391 Foam::pointIndexHit Foam::searchableBox::findLineAny
00392 (
00393 const point& start,
00394 const point& end
00395 ) const
00396 {
00397 return findLine(start, end);
00398 }
00399
00400
00401 void Foam::searchableBox::findNearest
00402 (
00403 const pointField& samples,
00404 const scalarField& nearestDistSqr,
00405 List<pointIndexHit>& info
00406 ) const
00407 {
00408 info.setSize(samples.size());
00409
00410 const point bbMid(midpoint());
00411
00412 forAll(samples, i)
00413 {
00414 info[i] = findNearest(bbMid, samples[i], nearestDistSqr[i]);
00415 }
00416 }
00417
00418
00419 void Foam::searchableBox::findLine
00420 (
00421 const pointField& start,
00422 const pointField& end,
00423 List<pointIndexHit>& info
00424 ) const
00425 {
00426 info.setSize(start.size());
00427
00428 forAll(start, i)
00429 {
00430 info[i] = findLine(start[i], end[i]);
00431 }
00432 }
00433
00434
00435 void Foam::searchableBox::findLineAny
00436 (
00437 const pointField& start,
00438 const pointField& end,
00439 List<pointIndexHit>& info
00440 ) const
00441 {
00442 info.setSize(start.size());
00443
00444 forAll(start, i)
00445 {
00446 info[i] = findLineAny(start[i], end[i]);
00447 }
00448 }
00449
00450
00451 void Foam::searchableBox::findLineAll
00452 (
00453 const pointField& start,
00454 const pointField& end,
00455 List<List<pointIndexHit> >& info
00456 ) const
00457 {
00458 info.setSize(start.size());
00459
00460
00461 DynamicList<pointIndexHit, 1, 1> hits;
00462
00463
00464
00465
00466
00467
00468
00469 const vectorField dirVec(end-start);
00470 const scalarField magSqrDirVec(magSqr(dirVec));
00471 const vectorField smallVec
00472 (
00473 Foam::sqrt(SMALL)*dirVec
00474 + vector(ROOTVSMALL,ROOTVSMALL,ROOTVSMALL)
00475 );
00476
00477 forAll(start, pointI)
00478 {
00479
00480 pointIndexHit inter = findLine(start[pointI], end[pointI]);
00481
00482 if (inter.hit())
00483 {
00484 hits.clear();
00485 hits.append(inter);
00486
00487 point pt = inter.hitPoint() + smallVec[pointI];
00488
00489 while (((pt-start[pointI])&dirVec[pointI]) <= magSqrDirVec[pointI])
00490 {
00491
00492 pointIndexHit inter = findLine(pt, end[pointI]);
00493
00494
00495
00496 if
00497 (
00498 !inter.hit()
00499 || (inter.index() == hits[hits.size()-1].index())
00500 )
00501 {
00502 break;
00503 }
00504 hits.append(inter);
00505
00506 pt = inter.hitPoint() + smallVec[pointI];
00507 }
00508
00509 info[pointI].transfer(hits);
00510 }
00511 else
00512 {
00513 info[pointI].clear();
00514 }
00515 }
00516 }
00517
00518
00519 void Foam::searchableBox::getRegion
00520 (
00521 const List<pointIndexHit>& info,
00522 labelList& region
00523 ) const
00524 {
00525 region.setSize(info.size());
00526 region = 0;
00527 }
00528
00529
00530 void Foam::searchableBox::getNormal
00531 (
00532 const List<pointIndexHit>& info,
00533 vectorField& normal
00534 ) const
00535 {
00536 normal.setSize(info.size());
00537 normal = vector::zero;
00538
00539 forAll(info, i)
00540 {
00541 if (info[i].hit())
00542 {
00543 normal[i] = treeBoundBox::faceNormals[info[i].index()];
00544 }
00545 else
00546 {
00547
00548 }
00549 }
00550 }
00551
00552
00553 void Foam::searchableBox::getVolumeType
00554 (
00555 const pointField& points,
00556 List<volumeType>& volType
00557 ) const
00558 {
00559 volType.setSize(points.size());
00560 volType = INSIDE;
00561
00562 forAll(points, pointI)
00563 {
00564 const point& pt = points[pointI];
00565
00566 for (direction dir = 0; dir < vector::nComponents; dir++)
00567 {
00568 if (pt[dir] < min()[dir] || pt[dir] > max()[dir])
00569 {
00570 volType[pointI] = OUTSIDE;
00571 break;
00572 }
00573 }
00574 }
00575 }
00576
00577
00578