FreeFOAM The Cross-Platform CFD Toolkit
octreeLine.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "octreeLine.H"
27 #include "octree.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 // Calculate sorted list of intersections
32 template <class Type>
34 {
35  // Determine intersections and sort acc. to distance to start
36 
37  const labelList& indices = currentLeaf_->indices();
38 
39  sortedIntersections_.setSize(indices.size());
40 
41  const vector direction = endPoint_ - realStartPoint_;
42 
43  label nHits = 0;
44 
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  );
55 
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  }
71 
72  sortedIntersections_.setSize(nHits);
73 
74  Foam::sort(sortedIntersections_);
75 
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  //}
85 
86  lastElem_ = -1;
87 
88  if (nHits > 0)
89  {
90  lastElem_ = sortedIntersections_[nHits - 1].index();
91 
92  //Pout<< "Storing lastElem_:" << lastElem_ << endl;
93  }
94 
95  // Reset index into sortedIntersections_
96  sortedI_ = -1;
97 }
98 
99 
100 // Searches for leaf with intersected elements. Return true if found; false
101 // otherwise. Sets currentLeaf_ and sortedIntersections_.
102 template <class Type>
104 {
105  do
106  {
107  // No current leaf. Find first one.
108  // Note: search starts from top every time
109 
110  point start(leafExitPoint_);
111  currentLeaf_ = tree_.findLeafLine(start, endPoint_, leafExitPoint_);
112 
113  if (!currentLeaf_)
114  {
115  // No leaf found. Give up.
116  return false;
117  }
118 
119  // Get intersections and sort.
120  calcSortedIntersections();
121  }
122  while (sortedIntersections_.empty());
123 
124  return true;
125 }
126 
127 
128 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
129 
130 template <class Type>
132 (
133  const octree<Type>& tree,
134  const point& startPoint,
135  const point& endPoint
136 )
137 :
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)
147 {}
148 
149 
150 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
151 
152 template <class Type>
154 {}
155 
156 
157 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
158 
159 template <class Type>
161 {
162  // Go to next element in sortedIntersections
163 
164  sortedI_++;
165 
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  }
176 
177  return true;
178 }
179 
180 // ************************ vim: set sw=4 sts=4 et: ************************ //