OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

searchableBox.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "searchableBox.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "SortableList.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035     defineTypeNameAndDebug(searchableBox, 0);
00036     addToRunTimeSelectionTable(searchableSurface, searchableBox, dict);
00037 }
00038 
00039 
00040 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00041 
00042 void Foam::searchableBox::projectOntoCoordPlane
00043 (
00044     const direction dir,
00045     const point& planePt,
00046     pointIndexHit& info
00047 ) const
00048 {
00049     // Set point
00050     info.rawPoint()[dir] = planePt[dir];
00051     // Set face
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 // Returns miss or hit with face (0..5) and region(always 0)
00071 Foam::pointIndexHit Foam::searchableBox::findNearest
00072 (
00073     const point& bbMid,
00074     const point& sample,
00075     const scalar nearestDistSqr
00076 ) const
00077 {
00078     // Point can be inside or outside. For every component direction can be
00079     // left of min, right of max or inbetween.
00080     // - outside points: project first one x plane (either min().x()
00081     // or max().x()), then onto y plane and finally z. You should be left
00082     // with intersection point
00083     // - inside point: find nearest side (compare to mid point). Project onto
00084     //   that.
00085 
00086     // The face is set to the last projected face.
00087 
00088 
00089     // Outside point projected onto cube. Assume faces 0..5.
00090     pointIndexHit info(true, sample, -1);
00091     bool outside = false;
00092 
00093     // (for internal points) per direction what nearest cube side is
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     // For outside points the info will be correct now. Handle inside points
00120     // using the three near distances. Project onto the nearest plane.
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                 // Project onto x plane
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     // Check if outside. Optimisation: could do some checks on distance already
00152     // on components above
00153     if (magSqr(info.rawPoint() - sample) > nearestDistSqr)
00154     {
00155         info.setMiss();
00156         info.setIndex(-1);
00157     }
00158 
00159     return info;
00160 }
00161 
00162 
00163 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00214 
00215 Foam::searchableBox::~searchableBox()
00216 {}
00217 
00218 
00219 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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     // Outside point projected onto cube. Assume faces 0..5.
00251     pointIndexHit info(true, sample, -1);
00252     bool outside = false;
00253 
00254     // (for internal points) per direction what nearest cube side is
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     // For outside points the info will be correct now. Handle inside points
00281     // using the three near distances. Project onto the nearest two planes.
00282     if (!outside)
00283     {
00284         // Get the per-component distance to nearest wall
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         // Project onto nearest
00294         projectOntoCoordPlane(sortedDist.indices()[0], near, info);
00295         // Project onto second nearest
00296         projectOntoCoordPlane(sortedDist.indices()[1], near, info);
00297     }
00298 
00299 
00300     // Check if outside. Optimisation: could do some checks on distance already
00301     // on components above
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             // Both start and end inside.
00343             foundInter = false;
00344         }
00345         else
00346         {
00347             // end is outside. Clip to bounding box.
00348             foundInter = intersects(end, start, info.rawPoint());
00349         }
00350     }
00351     else
00352     {
00353         // start is outside. Clip to bounding box.
00354         foundInter = intersects(start, end, info.rawPoint());
00355     }
00356 
00357 
00358     // Classify point
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     // Work array
00461     DynamicList<pointIndexHit, 1, 1> hits;
00462 
00463     // Tolerances:
00464     // To find all intersections we add a small vector to the last intersection
00465     // This is chosen such that
00466     // - it is significant (SMALL is smallest representative relative tolerance;
00467     //   we need something bigger since we're doing calculations)
00468     // - if the start-end vector is zero we still progress
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         // See if any intersection between pt and end
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                 // See if any intersection between pt and end
00492                 pointIndexHit inter = findLine(pt, end[pointI]);
00493 
00494                 // Check for not hit or hit same face as before (can happen
00495                 // if vector along surface of face)
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             // Set to what?
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 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd