initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / octree / octreeLine.C
blob76e56177b6a1583fefed864dd6ec387829158d12
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "octreeLine.H"
28 #include "octree.H"
30 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
32 // Calculate sorted list of intersections
33 template <class Type>
34 void Foam::octreeLine<Type>::calcSortedIntersections()
36     // Determine intersections and sort acc. to distance to start
38     const labelList& indices = currentLeaf_->indices();
40     sortedIntersections_.setSize(indices.size());
42     const vector direction = endPoint_ - realStartPoint_;
44     label nHits = 0;
46     forAll(indices, elemI)
47     {
48         point pt;
49         bool hit = tree_.shapes().intersects
50         (
51             indices[elemI],
52             realStartPoint_,
53             direction,
54             pt
55         );
57         if (hit && (indices[elemI] != lastElem_))
58         {
59            sortedIntersections_[nHits++] = pointHitSort
60             (
61                 pointHit
62                 (
63                     true,
64                     pt,
65                     Foam::magSqr(pt - leafExitPoint_),
66                     false
67                 ),
68                 indices[elemI]
69             );
70         }
71     }
73     sortedIntersections_.setSize(nHits);
75     Foam::sort(sortedIntersections_);
77     //// After sorting
78     //forAll(sortedIntersections_, i)
79     //{
80     //    Pout<< "calcSortedIntersections: After sorting:"
81     //        << i << "  distance:"
82     //        << sortedIntersections_[i].inter().distance()
83     //        << "  index:" << sortedIntersections_[i].index()
84     //        << endl;
85     //}
87     lastElem_ = -1;
89     if (nHits > 0)
90     {
91         lastElem_ = sortedIntersections_[nHits - 1].index();
93         //Pout<< "Storing lastElem_:" << lastElem_ << endl;
94     }
96     // Reset index into sortedIntersections_
97     sortedI_ = -1;
101 // Searches for leaf with intersected elements. Return true if found; false
102 // otherwise. Sets currentLeaf_ and sortedIntersections_.
103 template <class Type>
104 bool Foam::octreeLine<Type>::getNextLeaf()
106     do
107     {
108         // No current leaf. Find first one.
109         // Note: search starts from top every time
111         point start(leafExitPoint_);
112         currentLeaf_ = tree_.findLeafLine(start, endPoint_, leafExitPoint_);
114         if (!currentLeaf_)
115         {
116             // No leaf found. Give up.
117             return false;
118         }
120         // Get intersections and sort.
121         calcSortedIntersections();
122     }
123     while (sortedIntersections_.empty());
125     return true;
129 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
131 template <class Type>
132 Foam::octreeLine<Type>::octreeLine
134     const octree<Type>& tree,
135     const point& startPoint,
136     const point& endPoint
139     tree_(tree),
140     startPoint_(startPoint),
141     endPoint_(endPoint),
142     realStartPoint_(startPoint),
143     leafExitPoint_(startPoint_),
144     currentLeaf_(NULL),
145     sortedIntersections_(0),
146     lastElem_(-1),
147     sortedI_(-1)
151 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
153 template <class Type>
154 Foam::octreeLine<Type>::~octreeLine()
158 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
160 template <class Type>
161 bool Foam::octreeLine<Type>::getIntersection()
163     // Go to next element in sortedIntersections
165     sortedI_++;
167     if (sortedI_ >= sortedIntersections_.size())
168     {
169         // Past all sortedIntersections in current leaf. Go to next one.
170         if (!getNextLeaf())
171         {
172             // No valid leaf found
173             return false;
174         }
175         sortedI_ = 0;
176     }
178     return true;
181 // ************************************************************************* //