aabbcc
AABB.h
1 /*
2  Copyright (c) 2009 Erin Catto http://www.box2d.org
3  Copyright (c) 2016-2017 Lester Hedges <lester.hedges+aabbcc@gmail.com>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source distribution.
22 
23  This code was adapted from parts of the Box2D Physics Engine,
24  http://www.box2d.org
25 */
26 
27 #ifndef _AABB_H
28 #define _AABB_H
29 
30 #include <algorithm>
31 #include <cassert>
32 #include <cstdlib>
33 #include <iostream>
34 #include <limits>
35 #include <map>
36 #include <vector>
37 
39 const unsigned int NULL_NODE = 0xffffffff;
40 
41 namespace aabb
42 {
52  class AABB
53  {
54  public:
56  AABB();
57 
59 
62  AABB(unsigned int);
63 
65 
71  AABB(const std::vector<double>&, const std::vector<double>&);
72 
74  double computeSurfaceArea() const;
75 
77  double getSurfaceArea() const;
78 
80 
86  void merge(const AABB&, const AABB&);
87 
89 
95  bool contains(const AABB&) const;
96 
98 
104  bool overlaps(const AABB&) const;
105 
107 
110  std::vector<double> computeCentre();
111 
113 
116  void setDimension(unsigned int);
117 
119  std::vector<double> lowerBound;
120 
122  std::vector<double> upperBound;
123 
125  std::vector<double> centre;
126 
128  double surfaceArea;
129  };
130 
143  struct Node
144  {
146  Node();
147 
150 
152  unsigned int parent;
153 
155  unsigned int next;
156 
158  unsigned int left;
159 
161  unsigned int right;
162 
164  int height;
165 
167  unsigned int particle;
168 
170 
173  bool isLeaf() const;
174  };
175 
184  class Tree
185  {
186  public:
188 
198  Tree(unsigned int dimension_= 3, double skinThickness_ = 0.05, unsigned int nParticles = 16);
199 
201 
217  Tree(unsigned int, double, const std::vector<bool>&, const std::vector<double>&, unsigned int nParticles = 16);
218 
220 
223  void setPeriodicity(const std::vector<bool>&);
224 
226 
229  void setBoxSize(const std::vector<double>&);
230 
232 
241  void insertParticle(unsigned int, std::vector<double>&, double);
242 
244 
253  void insertParticle(unsigned int, std::vector<double>&, std::vector<double>&);
254 
256 
259  void removeParticle(unsigned int);
260 
262 
274  bool updateParticle(unsigned int, std::vector<double>&, double);
275 
277 
287  bool updateParticle(unsigned int, std::vector<double>&, std::vector<double>&);
288 
290 
296  std::vector<unsigned int> query(unsigned int);
297 
299 
308  std::vector<unsigned int> query(unsigned int, const AABB&);
309 
311 
317  std::vector<unsigned int> query(const AABB&);
318 
320 
323  const AABB& getAABB(unsigned int);
324 
326 
329  unsigned int getHeight() const;
330 
332 
335  unsigned int getNodeCount() const;
336 
338 
342  unsigned int computeMaximumBalance() const;
343 
345 
349  double computeSurfaceAreaRatio() const;
350 
352  void validate() const;
353 
355  void rebuild();
356 
357  private:
359  unsigned int root;
360 
362  std::vector<Node> nodes;
363 
365  unsigned int nodeCount;
366 
368  unsigned int nodeCapacity;
369 
371  unsigned int freeList;
372 
374  unsigned int dimension;
375 
377  bool isPeriodic;
378 
380  double skinThickness;
381 
383  std::vector<bool> periodicity;
384 
386  std::vector<double> boxSize;
387 
389  std::vector<double> negMinImage;
390 
392  std::vector<double> posMinImage;
393 
395  std::map<unsigned int, unsigned int> particleMap;
396 
398 
401  unsigned int allocateNode();
402 
404 
407  void freeNode(unsigned int);
408 
410 
413  void insertLeaf(unsigned int);
414 
416 
419  void removeLeaf(unsigned int);
420 
422 
425  unsigned int balance(unsigned int);
426 
428 
431  unsigned int computeHeight() const;
432 
434 
440  unsigned int computeHeight(unsigned int) const;
441 
443 
446  void validateStructure(unsigned int) const;
447 
449 
452  void validateMetrics(unsigned int) const;
453 
455  /* \param position
456  The position vector.
457  */
458  void periodicBoundaries(std::vector<double>&);
459 
461 
470  bool minimumImage(std::vector<double>&, std::vector<double>&);
471  };
472 }
473 
474 #endif /* _AABB_H */
bool contains(const AABB &) const
Test whether the AABB is contained within this one.
Definition: AABB.cc:93
std::vector< double > computeCentre()
Compute the centre of the AABB.
Definition: AABB.cc:130
unsigned int parent
Index of the parent node.
Definition: AABB.h:152
std::vector< double > lowerBound
Lower bound of AABB in each dimension.
Definition: AABB.h:119
unsigned int right
Index of the right-hand child.
Definition: AABB.h:161
AABB()
Constructor.
Definition: AABB.cc:31
bool overlaps(const AABB &) const
Test whether the AABB overlaps this one.
Definition: AABB.cc:106
double computeSurfaceArea() const
Compute the surface area of the box.
Definition: AABB.cc:50
unsigned int left
Index of the left-hand child.
Definition: AABB.h:158
void merge(const AABB &, const AABB &)
Merge two AABBs into this one.
Definition: AABB.cc:75
A node of the AABB tree.
Definition: AABB.h:143
double getSurfaceArea() const
Get the surface area of the box.
Definition: AABB.cc:70
std::vector< double > upperBound
Upper bound of AABB in each dimension.
Definition: AABB.h:122
unsigned int particle
The index of the particle that the node contains (leaf nodes only).
Definition: AABB.h:167
std::vector< double > centre
The position of the AABB centre.
Definition: AABB.h:125
AABB aabb
The fattened axis-aligned bounding box.
Definition: AABB.h:149
int height
Height of the node. This is 0 for a leaf and -1 for a free node.
Definition: AABB.h:164
void setDimension(unsigned int)
Set the dimensionality of the AABB.
Definition: AABB.cc:140
double surfaceArea
The AABB&#39;s surface area.
Definition: AABB.h:128
The axis-aligned bounding box object.
Definition: AABB.h:52
The dynamic AABB tree.
Definition: AABB.h:184
unsigned int next
Index of the next node.
Definition: AABB.h:155
Definition: AABB.cc:29