Nektar++
Octree.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File Octree.h
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Octree header.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
36 #include <vector>
37 #include <memory>
38 #include <limits>
39 
40 namespace Nektar
41 {
42 namespace FieldUtils
43 {
44 
45 class Octree
46 {
47  class Octant;
48  using OctantSharedPtr = std::shared_ptr<Octant>;
49  using OctantWeakPtr = std::weak_ptr<Octant>;
50 
51 public:
52  // Empty constructor
53  Octree();
54  // Constructor with bounds
55  Octree(const Array<OneD, Array<OneD, NekDouble> > &pts, int maxPts,
56  const Array<OneD, NekDouble> &bounds);
57  // Constructor without bounds
58  Octree(const Array<OneD, Array<OneD, NekDouble> > &pts, int maxPts);
59 
60  // Returns the ID of the leaf node that contains 'coords'
61  int QueryNode(const Array<OneD, NekDouble> &coords,
62  int depth=std::numeric_limits<int>::max());
63  // Returns the ID of the point in 'pts' closest to 'coords'
65  const Array<OneD, NekDouble> &coords, double &distance,
66  int pointInd=-1);
67 
68  // Returns the ID of the points inside the node 'nodeID'
69  std::vector<int> QueryPoints(int nodeID);
70  // Returns the IDs of the leaf nodes neighbouring 'nodeID'
71  std::vector<int> QueryNeighbours(int nodeID);
72 
73  int QueryNpoints(int nodeID) { return m_nodes[nodeID]->GetNpoints(); }
74  int QueryLocation(int nodeID) { return m_nodes[nodeID]->GetLoc(); }
75  int QueryDepth(int nodeID) { return m_nodes[nodeID]->GetDepth(); }
76  int QueryDelta(int nodeID) { return m_nodes[nodeID]->GetDelta(); }
77 
78  // Get some statistics about the octree
79  void GetStats(int &maxPts, int &nPts, int &nNodes,
80  int &nLeaves, int &depth);
81 
82 private:
83  class Octant : public std::enable_shared_from_this<Octant>
84  {
85  public:
86  // Empty constructor
87  Octant();
88  // Constructor
89  Octant(int loc, int depth, int id, const Array<OneD,
90  NekDouble> &bounds);
91  // Copy constructor
92  Octant(int loc, Octant &parent);
93 
94  int GetNpoints() const { return m_nPts; }
95  int GetLoc() const { return m_loc; }
96  int GetDepth() const { return m_depth; }
97  double GetDelta() const { return m_delta; }
98  int GetID() const { return m_id; }
99  bool IsLeaf() const { return m_isLeaf; }
102  std::vector<int>& GetIndices() { return m_pointInd; }
104  std::vector<OctantWeakPtr>& GetNeighbours() { return m_neighbours; }
105 
106  void SetID(int id) { m_id = id; }
107 
108  void GetLeaves(std::vector<OctantSharedPtr>& leaves);
109  void SetIndices(const std::vector<int> &indices);
110  void AddNeighbours(const std::vector<OctantSharedPtr> &neighbours);
111  void AddPoints(const Array<OneD, Array<OneD, NekDouble> > &pts,
112  const std::vector<int> &indices);
113  void Subdivide(int maxPts,
114  const Array<OneD, Array<OneD, NekDouble> > &pts,
115  std::vector<OctantSharedPtr> &nodes);
116  int GetLocInNode(const Array<OneD, NekDouble> &coords);
117 
118  private:
119  /// Number of points in the octant
120  int m_nPts;
121  /// Position in the father octant (1-8)
122  int m_loc;
123  /// Depth in octree (root is 1)
124  int m_depth;
125  /// ID of the octant (index in 'm_nodes' vector)
126  int m_id;
127  /// Length of the octant side
128  double m_delta;
129  /// Coordinates of the centre of the octant
131  /// Min/max coordinates of the octant (x, y, z)
133  /// Indices of the points comprised by the octant
134  std::vector<int> m_pointInd;
135  /// True for a leaf octant, false otherwise
136  bool m_isLeaf;
137 
138  /// Vector of pointers to the children octants
140  /// Vector of pointers to the neighbouring octants
141  std::vector<OctantWeakPtr> m_neighbours;
142  };
143 
144  /// Max points allowed in each octant
145  int m_maxPts;
146  /// Number of points in the mesh
148  /// Number of octants in the tree
149  int m_nNodes;
150  /// Number of leaf nodes in the tree
152  /// Maximum depth of the tree
154 
155  /// First node of the tree, linked to the rest
157  /// Vector of pointers to every octant in the tree
158  std::vector<OctantSharedPtr> m_nodes;
159 
160  void AdvanceToStats(int nodeID);
161  void SetNeighbours(int nodeID);
162 };
163 }
164 }
Array< OneD, NekDouble > m_centre
Coordinates of the centre of the octant.
Definition: Octree.h:130
void AddPoints(const Array< OneD, Array< OneD, NekDouble > > &pts, const std::vector< int > &indices)
Adds to 'm_pointInd' the IDs of the points in 'pts' that fall inside the octant.
Definition: Octree.cpp:553
int m_depth
Depth in octree (root is 1)
Definition: Octree.h:124
Array< OneD, NekDouble > & GetBounds()
Definition: Octree.h:101
double m_delta
Length of the octant side.
Definition: Octree.h:128
std::vector< OctantWeakPtr > & GetNeighbours()
Definition: Octree.h:104
void AddNeighbours(const std::vector< OctantSharedPtr > &neighbours)
Adds to 'm_neighbours' the octants that are not already in the list.
Definition: Octree.cpp:525
int m_loc
Position in the father octant (1-8)
Definition: Octree.h:122
void GetLeaves(std::vector< OctantSharedPtr > &leaves)
Updates 'leaves' so that it contains all the leaf nodes belonging to the octant.
Definition: Octree.cpp:491
void SetIndices(const std::vector< int > &indices)
Sets the values of 'm_pointInd' to those in 'indices'.
Definition: Octree.cpp:511
std::vector< int > m_pointInd
Indices of the points comprised by the octant.
Definition: Octree.h:134
std::vector< OctantWeakPtr > m_neighbours
Vector of pointers to the neighbouring octants.
Definition: Octree.h:141
int m_nPts
Number of points in the octant.
Definition: Octree.h:120
void Subdivide(int maxPts, const Array< OneD, Array< OneD, NekDouble > > &pts, std::vector< OctantSharedPtr > &nodes)
Recursively divides the octant into 8 children and fills the leaf nodes with their corresponding poin...
Definition: Octree.cpp:590
Array< OneD, NekDouble > m_bounds
Min/max coordinates of the octant (x, y, z)
Definition: Octree.h:132
bool m_isLeaf
True for a leaf octant, false otherwise.
Definition: Octree.h:136
Array< OneD, OctantSharedPtr > & GetChildren()
Definition: Octree.h:103
std::vector< int > & GetIndices()
Definition: Octree.h:102
Array< OneD, NekDouble > & GetCentre()
Definition: Octree.h:100
int GetLocInNode(const Array< OneD, NekDouble > &coords)
Returns the position inside an octant in the range (1-8). The name convention is as follows: let be ...
Definition: Octree.cpp:633
int m_id
ID of the octant (index in 'm_nodes' vector)
Definition: Octree.h:126
Array< OneD, OctantSharedPtr > m_children
Vector of pointers to the children octants.
Definition: Octree.h:139
Octant()
Construct a new Octree::Octant object.
Definition: Octree.cpp:357
void SetNeighbours(int nodeID)
Once the nodes of the octree are created, sets their neighbours as explained in 'Octree::QueryNeighbo...
Definition: Octree.cpp:312
int QueryDepth(int nodeID)
Definition: Octree.h:75
OctantSharedPtr m_root
First node of the tree, linked to the rest.
Definition: Octree.h:156
std::vector< int > QueryNeighbours(int nodeID)
Returns the IDs of the octants that surround the queried node. First, it finds the neighbouring nodes...
Definition: Octree.cpp:251
std::vector< int > QueryPoints(int nodeID)
Returns the indices of the points of the mesh contained in the tree.
Definition: Octree.cpp:236
int m_nNodes
Number of octants in the tree.
Definition: Octree.h:149
void GetStats(int &maxPts, int &nPts, int &nNodes, int &nLeaves, int &depth)
Returns some characteristic values of the tree.
Definition: Octree.cpp:270
int m_nLeaves
Number of leaf nodes in the tree.
Definition: Octree.h:151
std::shared_ptr< Octant > OctantSharedPtr
Definition: Octree.h:48
int QueryClosest(const Array< OneD, Array< OneD, NekDouble > > &pts, const Array< OneD, NekDouble > &coords, double &distance, int pointInd=-1)
Finds the ID of the closest point in 'pts' to the one specified by 'coords'. It also returns the dist...
Definition: Octree.cpp:177
std::weak_ptr< Octant > OctantWeakPtr
Definition: Octree.h:49
int m_maxPts
Max points allowed in each octant.
Definition: Octree.h:145
int m_maxDepth
Maximum depth of the tree.
Definition: Octree.h:153
int QueryDelta(int nodeID)
Definition: Octree.h:76
int QueryLocation(int nodeID)
Definition: Octree.h:74
void AdvanceToStats(int nodeID)
Goes through all the nodes of the octree counting the number of octants and the maximum depth reached...
Definition: Octree.cpp:286
int QueryNpoints(int nodeID)
Definition: Octree.h:73
Octree()
Construct a new octree object.
Definition: Octree.cpp:48
int m_nMshPts
Number of points in the mesh.
Definition: Octree.h:147
std::vector< OctantSharedPtr > m_nodes
Vector of pointers to every octant in the tree.
Definition: Octree.h:158
int QueryNode(const Array< OneD, NekDouble > &coords, int depth=std::numeric_limits< int >::max())
Given the coordinates 'coords' of a point, returns the leaf octant that contains it....
Definition: Octree.cpp:138
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
double NekDouble