ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / meshTools / octree / octreeLine.C
blob6497858f60ed574cee4c21d44588079cc8e1b9c9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "octreeLine.H"
27 #include "octree.H"
29 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
31 // Calculate sorted list of intersections
32 template <class Type>
33 void Foam::octreeLine<Type>::calcSortedIntersections()
35     // Determine intersections and sort acc. to distance to start
37     const labelList& indices = currentLeaf_->indices();
39     sortedIntersections_.setSize(indices.size());
41     const vector direction = endPoint_ - realStartPoint_;
43     label nHits = 0;
45     forAll(indices, elemI)
46     {
47         point pt;
48         bool hit = tree_.shapes().intersects
49         (
50             indices[elemI],
51             realStartPoint_,
52             direction,
53             pt
54         );
56         if (hit && (indices[elemI] != lastElem_))
57         {
58            sortedIntersections_[nHits++] = pointHitSort
59             (
60                 pointHit
61                 (
62                     true,
63                     pt,
64                     Foam::magSqr(pt - leafExitPoint_),
65                     false
66                 ),
67                 indices[elemI]
68             );
69         }
70     }
72     sortedIntersections_.setSize(nHits);
74     Foam::sort(sortedIntersections_);
76     //// After sorting
77     //forAll(sortedIntersections_, i)
78     //{
79     //    Pout<< "calcSortedIntersections: After sorting:"
80     //        << i << "  distance:"
81     //        << sortedIntersections_[i].inter().distance()
82     //        << "  index:" << sortedIntersections_[i].index()
83     //        << endl;
84     //}
86     lastElem_ = -1;
88     if (nHits > 0)
89     {
90         lastElem_ = sortedIntersections_[nHits - 1].index();
92         //Pout<< "Storing lastElem_:" << lastElem_ << endl;
93     }
95     // Reset index into sortedIntersections_
96     sortedI_ = -1;
100 // Searches for leaf with intersected elements. Return true if found; false
101 // otherwise. Sets currentLeaf_ and sortedIntersections_.
102 template <class Type>
103 bool Foam::octreeLine<Type>::getNextLeaf()
105     do
106     {
107         // No current leaf. Find first one.
108         // Note: search starts from top every time
110         point start(leafExitPoint_);
111         currentLeaf_ = tree_.findLeafLine(start, endPoint_, leafExitPoint_);
113         if (!currentLeaf_)
114         {
115             // No leaf found. Give up.
116             return false;
117         }
119         // Get intersections and sort.
120         calcSortedIntersections();
121     }
122     while (sortedIntersections_.empty());
124     return true;
128 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
130 template <class Type>
131 Foam::octreeLine<Type>::octreeLine
133     const octree<Type>& tree,
134     const point& startPoint,
135     const point& endPoint
138     tree_(tree),
139     startPoint_(startPoint),
140     endPoint_(endPoint),
141     realStartPoint_(startPoint),
142     leafExitPoint_(startPoint_),
143     currentLeaf_(NULL),
144     sortedIntersections_(0),
145     lastElem_(-1),
146     sortedI_(-1)
150 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
152 template <class Type>
153 Foam::octreeLine<Type>::~octreeLine()
157 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
159 template <class Type>
160 bool Foam::octreeLine<Type>::getIntersection()
162     // Go to next element in sortedIntersections
164     sortedI_++;
166     if (sortedI_ >= sortedIntersections_.size())
167     {
168         // Past all sortedIntersections in current leaf. Go to next one.
169         if (!getNextLeaf())
170         {
171             // No valid leaf found
172             return false;
173         }
174         sortedI_ = 0;
175     }
177     return true;
180 // ************************************************************************* //