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

octreeLine.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 "octreeLine.H"
00028 #include "octree.H"
00029 
00030 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00031 
00032 // Calculate sorted list of intersections
00033 template <class Type>
00034 void Foam::octreeLine<Type>::calcSortedIntersections()
00035 {
00036     // Determine intersections and sort acc. to distance to start
00037 
00038     const labelList& indices = currentLeaf_->indices();
00039 
00040     sortedIntersections_.setSize(indices.size());
00041 
00042     const vector direction = endPoint_ - realStartPoint_;
00043 
00044     label nHits = 0;
00045 
00046     forAll(indices, elemI)
00047     {
00048         point pt;
00049         bool hit = tree_.shapes().intersects
00050         (
00051             indices[elemI],
00052             realStartPoint_,
00053             direction,
00054             pt
00055         );
00056 
00057         if (hit && (indices[elemI] != lastElem_))
00058         {
00059            sortedIntersections_[nHits++] = pointHitSort
00060             (
00061                 pointHit
00062                 (
00063                     true,
00064                     pt,
00065                     Foam::magSqr(pt - leafExitPoint_),
00066                     false
00067                 ),
00068                 indices[elemI]
00069             );
00070         }
00071     }
00072 
00073     sortedIntersections_.setSize(nHits);
00074 
00075     Foam::sort(sortedIntersections_);
00076 
00078     //forAll(sortedIntersections_, i)
00079     //{
00080     //    Pout<< "calcSortedIntersections: After sorting:"
00081     //        << i << "  distance:"
00082     //        << sortedIntersections_[i].inter().distance()
00083     //        << "  index:" << sortedIntersections_[i].index()
00084     //        << endl;
00085     //}
00086 
00087     lastElem_ = -1;
00088 
00089     if (nHits > 0)
00090     {
00091         lastElem_ = sortedIntersections_[nHits - 1].index();
00092 
00093         //Pout<< "Storing lastElem_:" << lastElem_ << endl;
00094     }
00095 
00096     // Reset index into sortedIntersections_
00097     sortedI_ = -1;
00098 }
00099 
00100 
00101 // Searches for leaf with intersected elements. Return true if found; false
00102 // otherwise. Sets currentLeaf_ and sortedIntersections_.
00103 template <class Type>
00104 bool Foam::octreeLine<Type>::getNextLeaf()
00105 {
00106     do
00107     {
00108         // No current leaf. Find first one.
00109         // Note: search starts from top every time
00110 
00111         point start(leafExitPoint_);
00112         currentLeaf_ = tree_.findLeafLine(start, endPoint_, leafExitPoint_);
00113 
00114         if (!currentLeaf_)
00115         {
00116             // No leaf found. Give up.
00117             return false;
00118         }
00119 
00120         // Get intersections and sort.
00121         calcSortedIntersections();
00122     }
00123     while (sortedIntersections_.empty());
00124 
00125     return true;
00126 }
00127 
00128 
00129 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00130 
00131 template <class Type>
00132 Foam::octreeLine<Type>::octreeLine
00133 (
00134     const octree<Type>& tree,
00135     const point& startPoint,
00136     const point& endPoint
00137 )
00138 :
00139     tree_(tree),
00140     startPoint_(startPoint),
00141     endPoint_(endPoint),
00142     realStartPoint_(startPoint),
00143     leafExitPoint_(startPoint_),
00144     currentLeaf_(NULL),
00145     sortedIntersections_(0),
00146     lastElem_(-1),
00147     sortedI_(-1)
00148 {}
00149 
00150 
00151 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00152 
00153 template <class Type>
00154 Foam::octreeLine<Type>::~octreeLine()
00155 {}
00156 
00157 
00158 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00159 
00160 template <class Type>
00161 bool Foam::octreeLine<Type>::getIntersection()
00162 {
00163     // Go to next element in sortedIntersections
00164 
00165     sortedI_++;
00166 
00167     if (sortedI_ >= sortedIntersections_.size())
00168     {
00169         // Past all sortedIntersections in current leaf. Go to next one.
00170         if (!getNextLeaf())
00171         {
00172             // No valid leaf found
00173             return false;
00174         }
00175         sortedI_ = 0;
00176     }
00177 
00178     return true;
00179 }
00180 
00181 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd