Test-dynamicIndexedOctree.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) 2012 OpenFOAM Foundation
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 Description
25  Test the construction, insertion and removal of points from the dynamic
26  indexed octree.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "argList.H"
31 #include "cpuTime.H"
32 #include "treeBoundBox.H"
33 #include "dynamicIndexedOctree.H"
34 #include "dynamicTreeDataPoint.H"
35 #include "indexedOctree.H"
36 #include "treeDataPoint.H"
37 
38 using namespace Foam;
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 // Main program:
42 
43 int main(int argc, char *argv[])
44 {
45  cpuTime timer;
46 
47  treeBoundBox overallBb
48  (
49  point(0,0,0),
50  point(1,1,1)
51  );
52 
53  label size = 5e6;
54 
55  Info<< "Creating indexed octrees with " << size
56  << " points in bounding box: " << overallBb << endl;
57 
58 
59 // Create the data
60 
62  pointField pointFieldList(size);
63 
64  for (label pI = size - 1; pI >= 0; --pI)
65  {
66  scalar factor = pI/scalar(size);
67 
68  pointList.append(0.99*point(factor, factor, factor));
69  pointFieldList[pI] = 0.99*point(factor, factor, factor);
70  }
71 
72  for (label i=0; i<5; ++i)
73  {
74  pointList.append(point(0.95, 0.95,0.95));
75  pointFieldList.append(point(0.95, 0.95,0.95));
76  }
77 
78  Info<< "Time to construct lists of points: "
79  << timer.cpuTimeIncrement() << endl;
80 
81 
82 // Construct the trees
83 
85  (
87  overallBb, // overall search domain
88  20, // max levels
89  100, // maximum ratio of cubes v.s. cells
90  100.0 // max. duplicity; n/a since no bounding boxes.
91  );
92 
93  Info<< "Time to construct dynamic tree: "
94  << timer.cpuTimeIncrement() << endl;
95 
97  (
98  treeDataPoint(pointFieldList),
99  overallBb, // overall search domain
100  20, // max levels
101  100, // maximum ratio of cubes v.s. cells
102  100.0 // max. duplicity; n/a since no bounding boxes.
103  );
104 
105  Info<< "Time to construct normal tree: "
106  << timer.cpuTimeIncrement() << endl;
107 
108  Info<< "Statistics of the dynamic tree:" << endl;
109  tree.writeTreeInfo();
110 
111 
112 // Test calls to findNearest()
113 
114  point p(0.94,0.94,0.94);
115 
116  Info<< "Nearest point to " << p << " = "
117  << tree.findNearest(p, 0.4) << endl;
118 
119  for (label i = 0; i < size; ++i)
120  {
121  tree.findNearest
122  (
123  point(scalar(i)/size, scalar(i)/size, scalar(i)/size),
124  0.4
125  );
126  }
127 
128  Info<< "Time to perform " << size
129  << " findNearest() calls on the dynamic tree: "
130  << timer.cpuTimeIncrement() << endl;
131 
132  for (label i = 0; i < size; ++i)
133  {
134  tree2.findNearest
135  (
136  point(scalar(i)/size, scalar(i)/size, scalar(i)/size),
137  0.4
138  );
139  }
140 
141  Info<< "Time to perform " << size
142  << " findNearest() calls on the normal tree: "
143  << timer.cpuTimeIncrement() << endl;
144 
145 
146 // Test point insertion
147 
148  label index = pointList.size();
149  pointList.append(p);
150 
151  Info<< nl << "Inserting point " << p << " with index " << index << endl;
152 
153  // Now insert a point into the tree.
154  tree.insert(index, pointList.size());
155 
156  Info<< "Nearest point to " << p << " = "
157  << tree.findNearest(p, 0.4) << endl;
158 
159  index = pointList.size();
160  pointList.append(p);
161 
162  Info<< "Inserting same point " << p << " with index " << index << endl;
163 
164  tree.insert(index, pointList.size());
165 
166  Info<< "Nearest point to " << p << " = "
167  << tree.findNearest(p, 0.4) << endl;
168 
169  Info<< nl << "Tree after insertion:" << endl;
170  tree.writeTreeInfo();
171 
172 
173 // Test point removal
174 
175  label nRemoved = 0;
176  for (label pI = size - 5; pI >= 5; pI--)
177  {
178  tree.remove(pI);
179  ++nRemoved;
180  }
181 
182  Info<< "Time to remove " << nRemoved << " points from the tree: "
183  << timer.cpuTimeIncrement() << endl;
184 
185  Info<< nl << "Tree after removal:" << endl;
186  tree.writeTreeInfo();
187 
188  Info<< "Nearest point to " << p << " = "
189  << tree.findNearest(p, 0.4) << endl;
190 
191  return 0;
192 }
193 
194 
195 // ************************************************************************* //
Foam::cpuTime
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:52
dynamicTreeDataPoint.H
main
int main(int argc, char *argv[])
Definition: Test-dynamicIndexedOctree.C:43
Foam::dynamicIndexedOctree::insert
bool insert(label startIndex, label endIndex)
Insert a new object into the tree.
Definition: dynamicIndexedOctree.C:2672
p
p
Definition: pEqn.H:62
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::treeBoundBox
Standard boundBox + extra functionality for use in octree.
Definition: treeBoundBox.H:75
indexedOctree.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::treeDataPoint
Holds (reference to) pointField. Encapsulation of data needed for octree searches....
Definition: treeDataPoint.H:59
Foam::dynamicIndexedOctree
Non-pointer based hierarchical recursive searching. Storage is dynamic, so elements can be deleted.
Definition: dynamicIndexedOctree.H:55
Foam::dynamicTreeDataPoint
Holds (reference to) pointField. Encapsulation of data needed for octree searches....
Definition: dynamicTreeDataPoint.H:59
dynamicIndexedOctree.H
Foam::dynamicIndexedOctree::remove
bool remove(const label index)
Remove an object from the tree.
Definition: dynamicIndexedOctree.C:2795
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
treeDataPoint.H
Foam::List::append
void append(const T &)
Append an element at the end of the list.
treeBoundBox.H
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
argList.H
Foam::indexedOctree
Non-pointer based hierarchical recursive searching.
Definition: treeDataTriSurface.H:47
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
cpuTime.H
Foam::dynamicIndexedOctree::writeTreeInfo
void writeTreeInfo() const
Definition: dynamicIndexedOctree.C:2966
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::timer
Implements a timeout mechanism via sigalarm.
Definition: timer.H:81
Foam::indexedOctree::findNearest
void findNearest(const label nodeI, const linePointRef &ln, treeBoundBox &tightest, label &nearestShapeI, point &linePoint, point &nearestPoint, const FindNearestOp &fnOp) const
Find nearest point to line.
Definition: indexedOctree.C:590
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::dynamicIndexedOctree::findNearest
void findNearest(const label nodeI, const linePointRef &ln, treeBoundBox &tightest, label &nearestShapeI, point &linePoint, point &nearestPoint) const
Find nearest point to line.
Definition: dynamicIndexedOctree.C:563
Foam::pointList
List< point > pointList
A List of points.
Definition: pointList.H:50
Foam::point
vector point
Point is a vector.
Definition: point.H:41