FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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