libatomprobe
Library for Atom Probe Tomography (APT) computation
K3DTree-exact.h
Go to the documentation of this file.
1 /*
2  * K3DTreeExact.h - Precise KD tree implementation
3  * Copyright (C) 2014 D. Haley
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef ATOMPROBE_K3DTREE_EXACT_H
20 #define ATOMPROBE_K3DTREE_EXACT_H
21 
22 //This is the second revision of my KD tree implementation
23 //The goals here are, as compared to the first
24 // - Improved build performance by minimising memory allocation calls
25 // and avoiding recursive implementations
26 // - index based construction for smaller in-tree storage
27 
28 
29 #include <vector>
30 #include <cstdlib>
31 
33 
34 
35 //Note that many functions in the tree are parallelised using openmp
36 namespace AtomProbe
37 {
38 
39 
42 {
43  public:
44  //Index of left child in parent tree array. -1 if no child
45  size_t childLeft;
46  //Index of right child in parent tree array. -1 if no child
47  size_t childRight;
48 
49  //Has this point been marked by an external algorithm?
50  bool tagged;
51 };
52 
55 {
56  private:
58  size_t maxDepth;
59 
61  //Second is original array index upon build
62  std::vector<std::pair<Point3D,size_t> > indexedPoints;
63 
65  std::vector<K3DNodeExact> nodes;
66 
68  size_t treeRoot;
69 
71  BoundCube treeBounds;
72 
73  //Callback for progress reporting
74  bool (*callback)(void);
75 
76  unsigned int *progress; //Progress counter pointer. Storage must be assigned
77 
78  public:
80 
82  K3DTreeExact();
83 
86 
88  void resetPts(std::vector<Point3D> &pts, bool clear=true);
90  void resetPts(std::vector<IonHit> &pts, bool clear=true);
91 
93 
97  bool build();
98 
100  void getBoundCube(BoundCube &b);
101 
103 
105  void dump(std::ostream &,size_t depth=0, size_t offset=-1) const;
106 
107 
109 
112  size_t findNearestUntagged(const Point3D &queryPt,
113  const BoundCube &b, bool tag=true);
114 
116 
118  void findUntaggedInRadius(const Point3D &queryPt, const BoundCube &b,
119  float radius, std::vector<size_t> &result);
120 
121  //Find the indices of all points that lie within the
122  // sphere (pts < radius) of given radius, centered upon
123  // this origin. Tags are not considered in this algorithm,
124  // and must be checked by the end
125  void ptsInSphere(const Point3D &origin, float radius,
126  std::vector<size_t> &pts) const;
127 
128 
129  //Obtain a point from its internal index
130  const Point3D *getPt(size_t index) const ;
131  //Obtain a point from its internal index
132  const Point3D &getPtRef(size_t index) const ;
133 
134  //reset the specified "tags" in the tree
135  void clearTags(std::vector<size_t> &tagsToClear);
136 
137  //Convert the "tree index" (the position in the tree) into the original point offset in the input array
138  size_t getOrigIndex(size_t treeIndex) const ;
139 
140  //Set the callback routine for progress reporting. If
141  // callback returns false during build process, build is
142  // aborted. Progress is also updated (0->100) during build
143  void setCallback(bool (*cb)(void)) {callback = cb;}
144 
145  //Set a pointer that can be used to write the current progress
146  void setProgressPointer(unsigned int *p) { progress=p;};
147 
148  //Erase tree contents
149  void clear() { nodes.clear(); indexedPoints.clear();};
150 
151  //mark a point as "tagged" (or untagged,if tagVal=false) via its tree index.
152  void tag(size_t treeIndex,bool tagVal=true);
153  //mark a vector of points as "tagged" (or untagged,if tagVal=false) via its tree index.
154  void tag(const std::vector<size_t> &treeIndicies,bool tagVal=true);
155 
156  //obtain the tag status for a given point, using the tree index
157  bool getTag(size_t treeIndex) const ;
158 
159  //obtain the number of points in the tree
160  size_t size() const;
161 
162  //Find the position of the root node in the tree
163  size_t rootIdx() const { return treeRoot;}
164 
165  //Find the number of tagged items in the tree
166  size_t tagCount() const;
167 
168  //reset all tagged points to untagged
169  void clearAllTags();
170 
171 #ifdef DEBUG
172  static bool runUnitTests();
173 #endif
174 };
175 
176 }
177 #endif
void setProgressPointer(unsigned int *p)
Node Class for storing point.
Definition: K3DTree-exact.h:41
bool callback()
Definition: kd-example.cpp:37
3D specific KD tree
Definition: K3DTree-exact.h:54
size_t rootIdx() const
~K3DTreeExact()
Cleans up tree, deallocates nodes.
Definition: K3DTree-exact.h:85
A 3D point data class storage.
Definition: point3D.h:39
void setCallback(bool(*cb)(void))
Helper class to define a bounding cube.
Definition: boundcube.h:29
unsigned int progress
Definition: kd-example.cpp:26