libatomprobe
Library for Atom Probe Tomography (APT) computation
K3DTree-approx.h
Go to the documentation of this file.
1 /* K3Dtree-approx.h: KD tree class for approximate calculations
2  * Copyright (C) 2014 Daniel Haley
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef ATOMPROBE_K3DTREE_H
18 #define ATOMPROBE_K3DTREE_H
19 
20 
21 #include <algorithm>
22 #include <stack>
23 #include <vector>
24 #include <deque>
25 #include <cmath>
26 #include <iostream>
27 
28 
31 
32 namespace AtomProbe
33 {
34 
35 
37 
42 {
43  private:
44  unsigned int axis;
45  public:
46  AxisCompare();
47  void setAxis(unsigned int Axis);
48  inline bool operator()(const Point3D &p1,const Point3D &p2) const
49  {return p1[axis]<p2[axis];};
50 };
51 
54 {
55  private:
56  K3DNodeApprox *childLeft;
57  K3DNodeApprox *childRight;
58  Point3D loc;
60  unsigned int axis;
61  public:
62  //K3DNodeApprox();
63  //get and sets.
64 
66  Point3D getLoc() const;
68  inline const Point3D *getLocRef() const { return &loc;};
70  inline void setLeft(K3DNodeApprox *node) {childLeft= node;};
72  inline void setRight(K3DNodeApprox *node) {childRight=node;};
73 
75  void setLoc(const Point3D &);
77  inline void setAxis(unsigned int newAxis) {axis=newAxis;};
79  inline unsigned int getAxis() const{ return axis;};
81  inline float getAxisVal() const { return loc.getValue(axis);};
83  inline float getLocVal(unsigned int pos) const {return loc.getValue(pos);};
85  inline float sqrDist(const Point3D &pt) const {return loc.sqrDist(pt);};
87  inline K3DNodeApprox *left() const {return childLeft;};
89  inline K3DNodeApprox *right() const {return childRight;};
90 
92  void deleteChildren();
94  void dump(std::ostream &, unsigned int) const;
95 };
96 
99 {
100  private:
102  unsigned int treeSize;
104  unsigned int maxDepth;
105 
107  K3DNodeApprox *root;
109  K3DNodeApprox *buildRecurse(std::vector<Point3D>::iterator pts_start, std::vector<Point3D>::iterator pts_end, unsigned int depth );
110  public:
111 
112  //KD Tree constructor
113  K3DTreeApprox();
114 
116  ~K3DTreeApprox();
117 
119 
123  void build(const std::vector<Point3D> &pts);
124 
126 
129  void buildByRef(std::vector<Point3D> &pts);
130 
131 
133  void kill();
134 
136 
141  const Point3D *findNearest(const Point3D &, const BoundCube &,
142 
143  float deadDistSqr) const;
144 
146 
149  void findKNearest(const Point3D & sourcePoint, const BoundCube& treeDomain,
150  unsigned int numNNs, std::vector<const Point3D *> &results,
151  float deadDistSqr=0.0f) const;
152 
154 
156  void dump(std::ostream &) const;
157 
159  inline unsigned int nodeCount() const{return treeSize;};
160 
161 #ifdef DEBUG
162  static bool test();
163 #endif
164 };
165 
166 }
167 
168 #endif
float sqrDist(const Point3D &pt) const
Returns the square of distance to another Point3D.
Definition: point3D.cpp:272
unsigned int getAxis() const
Retrieve the axis that this node operates on.
float getLocVal(unsigned int pos) const
Obtain the coordinates at dimension "pos".
bool operator()(const Point3D &p1, const Point3D &p2) const
unsigned int nodeCount() const
Print the number of nodes stored in the tree.
float sqrDist(const Point3D &pt) const
Obtain the distance between this node and another point.
const Point3D * getLocRef() const
Return a pointer to the point from the node.
K3DNodeApprox * left() const
Obtain pointer to left child.
A 3D point data class storage.
Definition: point3D.h:39
void setLeft(K3DNodeApprox *node)
Set the left child node.
K3DNodeApprox * right() const
Obtain pointer to right child.
float getAxisVal() const
retrieve the value associated with this axis
Helper class to define a bounding cube.
Definition: boundcube.h:29
3D specific KD tree
float getValue(unsigned int ui) const
Get value of ith dimension (0, 1, 2)
Definition: point3D.h:98
void setAxis(unsigned int Axis)
Node Class for storing point.
Functor allowing for sorting of points in 3D.
void setRight(K3DNodeApprox *node)
Set the right child node.
void setAxis(unsigned int newAxis)
Set the axis that this node operates on.