INET Framework for OMNeT++/OMNEST
inet::BVHTree Class Reference

#include <BVHTree.h>

Classes

class  Axis
 
struct  AxisComparator
 
class  BVHTreeVisitor
 

Public Member Functions

 BVHTree (const Coord &boundingMin, const Coord &boundingMax, std::vector< const IPhysicalObject * > &objects, unsigned int start, unsigned int end, Axis axis, unsigned int leafCapacity)
 
virtual ~BVHTree ()
 
void lineSegmentQuery (const LineSegment &lineSegment, const IVisitor *visitor) const
 

Protected Member Functions

bool isLeaf () const
 
void buildHierarchy (std::vector< const IPhysicalObject * > &objects, unsigned int start, unsigned int end, Axis axis)
 
void computeBoundingBox (Coord &boundingMin, Coord &boundingMax, std::vector< const IPhysicalObject * > &objects, unsigned int start, unsigned int end) const
 
bool intersectWithLineSegment (const LineSegment &lineSegment) const
 

Protected Attributes

unsigned int leafCapacity
 
std::string axisOrder
 
Coord boundingMin
 
Coord boundingMax
 
Coord center
 
BVHTreeleft
 
BVHTreeright
 
std::vector< const IPhysicalObject * > objects
 

Constructor & Destructor Documentation

inet::BVHTree::BVHTree ( const Coord boundingMin,
const Coord boundingMax,
std::vector< const IPhysicalObject * > &  objects,
unsigned int  start,
unsigned int  end,
Axis  axis,
unsigned int  leafCapacity 
)

Referenced by buildHierarchy().

31 {
32  this->left = nullptr;
33  this->right = nullptr;
34  this->boundingMin = boundingMin;
35  this->boundingMax = boundingMax;
36  this->center = (boundingMax - boundingMin) / 2 + boundingMin;
37  this->leafCapacity = leafCapacity;
38  buildHierarchy(objects, start, end, axis);
39 }
Coord boundingMin
Definition: BVHTree.h:85
Coord center
Definition: BVHTree.h:86
std::vector< const IPhysicalObject * > objects
Definition: BVHTree.h:89
void buildHierarchy(std::vector< const IPhysicalObject * > &objects, unsigned int start, unsigned int end, Axis axis)
Definition: BVHTree.cc:41
BVHTree * right
Definition: BVHTree.h:88
BVHTree * left
Definition: BVHTree.h:87
Coord boundingMax
Definition: BVHTree.h:85
unsigned int leafCapacity
Definition: BVHTree.h:83
inet::BVHTree::~BVHTree ( )
virtual
124 {
125  delete left;
126  delete right;
127 }
BVHTree * right
Definition: BVHTree.h:88
BVHTree * left
Definition: BVHTree.h:87

Member Function Documentation

void inet::BVHTree::buildHierarchy ( std::vector< const IPhysicalObject * > &  objects,
unsigned int  start,
unsigned int  end,
Axis  axis 
)
protected

Referenced by BVHTree().

42 {
43  if (end - start + 1 <= leafCapacity)
44  {
45  for (unsigned int i = start; i <= end; i++)
46  this->objects.push_back(objects[i]);
47  }
48  else
49  {
50  auto s = objects.begin();
51  auto e = s;
52  std::advance(s, start);
53  std::advance(e, end + 1);
54  sort(s, e, AxisComparator(axis.getCurrentAxis()));
55  axis.getNextAxis();
56  Coord boundingMin, boundingMax;
57  computeBoundingBox(boundingMin, boundingMax, objects, start, (start + end) / 2);
58  left = new BVHTree(boundingMin, boundingMax, objects, start, (start + end) / 2, axis, leafCapacity);
59  computeBoundingBox(boundingMin, boundingMax, objects, 1 + (start + end) / 2, end);
60  right = new BVHTree(boundingMin, boundingMax, objects, 1 + (start + end) / 2, end, axis, leafCapacity);
61  }
62 }
Coord boundingMin
Definition: BVHTree.h:85
std::vector< const IPhysicalObject * > objects
Definition: BVHTree.h:89
BVHTree(const Coord &boundingMin, const Coord &boundingMax, std::vector< const IPhysicalObject * > &objects, unsigned int start, unsigned int end, Axis axis, unsigned int leafCapacity)
Definition: BVHTree.cc:30
BVHTree * right
Definition: BVHTree.h:88
BVHTree * left
Definition: BVHTree.h:87
void computeBoundingBox(Coord &boundingMin, Coord &boundingMax, std::vector< const IPhysicalObject * > &objects, unsigned int start, unsigned int end) const
Definition: BVHTree.cc:64
const value< double, units::C > e(1.602176487e-19)
void sort(std::vector< T > &v)
Definition: stlutils.h:112
Coord boundingMax
Definition: BVHTree.h:85
value< double, units::s > s
Definition: Units.h:1049
unsigned int leafCapacity
Definition: BVHTree.h:83
void inet::BVHTree::computeBoundingBox ( Coord boundingMin,
Coord boundingMax,
std::vector< const IPhysicalObject * > &  objects,
unsigned int  start,
unsigned int  end 
) const
protected

Referenced by buildHierarchy().

65 {
66  double xMin = std::numeric_limits<double>::max();
67  double yMin = xMin;
68  double zMin = xMin;
69  double xMax = -std::numeric_limits<double>::max();
70  double yMax = xMax;
71  double zMax = xMax;
72  for (unsigned int i = start; i <= end; i++)
73  {
74  const IPhysicalObject *phyObj = objects[i];
75  Coord pos = phyObj->getPosition();
76  Coord size = phyObj->getShape()->computeBoundingBoxSize();
77  size /= 2;
78  Coord objMaxBounding = pos + size;
79  Coord objMinBounding = pos - size;
80  if (objMaxBounding.x > xMax)
81  xMax = objMaxBounding.x;
82  if (objMaxBounding.y > yMax)
83  yMax = objMaxBounding.y;
84  if (objMaxBounding.z > zMax)
85  zMax = objMaxBounding.z;
86  if (objMinBounding.x < xMin)
87  xMin = objMinBounding.x;
88  if (objMinBounding.y < yMin)
89  yMin = objMinBounding.y;
90  if (objMinBounding.z < zMin)
91  zMin = objMinBounding.z;
92  }
93  boundingMin = Coord(xMin, yMin, zMin);
94  boundingMax = Coord(xMax, yMax, zMax);
95 }
Coord boundingMin
Definition: BVHTree.h:85
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
std::vector< const IPhysicalObject * > objects
Definition: BVHTree.h:89
Coord boundingMax
Definition: BVHTree.h:85
bool inet::BVHTree::intersectWithLineSegment ( const LineSegment lineSegment) const
protected

Referenced by lineSegmentQuery().

98 {
100  Coord p0 = lineSegment.getPoint1() - center;
101  Coord p1 = lineSegment.getPoint2() - center;
102  Cuboid cuboid(size);
103  LineSegment translatedLineSegment(p0, p1);
104  Coord intersection1, intersection2, normal1, normal2; // TODO: implement a bool computeIntersection(lineSegment) function
105  return cuboid.computeIntersection(translatedLineSegment, intersection1, intersection2, normal1, normal2);
106 }
Coord boundingMin
Definition: BVHTree.h:85
double z
Definition: Coord.h:51
Coord center
Definition: BVHTree.h:86
Coord boundingMax
Definition: BVHTree.h:85
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
bool inet::BVHTree::isLeaf ( ) const
protected

Referenced by lineSegmentQuery().

26 {
27  return objects.size() != 0;
28 }
std::vector< const IPhysicalObject * > objects
Definition: BVHTree.h:89
void inet::BVHTree::lineSegmentQuery ( const LineSegment lineSegment,
const IVisitor visitor 
) const

Referenced by lineSegmentQuery(), and inet::physicalenvironment::BVHObjectCache::visitObjects().

109 {
110  if (isLeaf())
111  {
112  for (auto & elem : objects)
113  // TODO: avoid dynamic_cast
114  visitor->visit(dynamic_cast<const cObject *>(elem));
115  }
116  else if (intersectWithLineSegment(lineSegment))
117  {
118  left->lineSegmentQuery(lineSegment, visitor);
119  right->lineSegmentQuery(lineSegment, visitor);
120  }
121 }
void lineSegmentQuery(const LineSegment &lineSegment, const IVisitor *visitor) const
Definition: BVHTree.cc:108
std::vector< const IPhysicalObject * > objects
Definition: BVHTree.h:89
bool intersectWithLineSegment(const LineSegment &lineSegment) const
Definition: BVHTree.cc:97
bool isLeaf() const
Definition: BVHTree.cc:25
BVHTree * right
Definition: BVHTree.h:88
BVHTree * left
Definition: BVHTree.h:87

Member Data Documentation

std::string inet::BVHTree::axisOrder
protected
Coord inet::BVHTree::boundingMax
protected
Coord inet::BVHTree::boundingMin
protected
Coord inet::BVHTree::center
protected
unsigned int inet::BVHTree::leafCapacity
protected

Referenced by buildHierarchy(), and BVHTree().

BVHTree* inet::BVHTree::left
protected
std::vector<const IPhysicalObject *> inet::BVHTree::objects
protected

Referenced by isLeaf(), and lineSegmentQuery().

BVHTree* inet::BVHTree::right
protected

The documentation for this class was generated from the following files: