geolib2
HeightMapNode.cpp
Go to the documentation of this file.
1 #include "geolib/HeightMapNode.h"
2 
3 namespace geo {
4 
5 HeightMapNode::HeightMapNode(const Box& box) : box_(box), occupied_(false) {
6  for(unsigned int i = 0; i < 4; ++i) {
7  children_[i] = nullptr;
8  }
9 }
10 
11 HeightMapNode::HeightMapNode(const HeightMapNode& orig) : box_(orig.box_), occupied_(orig.occupied_) {
12  for(unsigned int i = 0; i < 4; ++i) {
13  if (orig.children_[i]) {
14  children_[i] = orig.children_[i]->clone();
15  } else {
16  children_[i] = nullptr;
17  }
18  }
19 }
20 
21 HeightMapNode::HeightMapNode(HeightMapNode&& orig) : box_(std::move(orig.box_)), occupied_(orig.occupied_) {
22  for(unsigned int i = 0; i < 4; ++i) {
23  if (orig.children_[i]) {
24  children_[i] = orig.children_[i];
25  orig.children_[i] = nullptr;
26  } else {
27  children_[i] = nullptr;
28  }
29  }
30 }
31 
33 }
34 
36  return new HeightMapNode(*this);
37 }
38 
39 bool HeightMapNode::intersect(const Ray& r, float t0, float t1, double& distance) const {
40  /*if (box_.intersect(r.origin)) {
41  std::cout << "TEST!" << std::endl;
42  if (box_.intersect(r, t0, t1, distance)) {
43  std::cout << " YES: " << distance << std::endl;
44  }
45  }*/
46 
47  if (!box_.intersect(r, t0, t1, distance)) {
48  return false;
49  }
50 
51  if (occupied_) {
52  return true;
53  }
54 
55  unsigned int i_child_origin = 5;
56  for(unsigned int i = 0; i < 4; ++i) {
57  if (children_[i] && children_[i]->box_.contains(r.getOrigin())) {
58  if (children_[i]->intersect(r, t0, t1, distance)) {
59  return true;
60  }
61  i_child_origin = i;
62  }
63  }
64 
65 
66  for(unsigned int i = 0; i < 4; ++i) {
67  if (i != i_child_origin && children_[i] && children_[i]->intersect(r, t0, t1, distance)) {
68  return true;
69  }
70  }
71 
72  return false;
73 }
74 
75 }
geo::Box::contains
bool contains(const Vector3 &p) const
Determines whether a point p lies within the shape.
Definition: Box.cpp:86
geo::HeightMapNode
Definition: HeightMapNode.h:17
geo::Ray::getOrigin
const Vector3 & getOrigin() const
Definition: Ray.h:16
geo
Definition: Box.h:6
geo::Box
Definition: Box.h:15
geo::HeightMapNode::~HeightMapNode
virtual ~HeightMapNode()
Definition: HeightMapNode.cpp:32
geo::Box::intersect
bool intersect(const Ray &r, float t0, float t1, double &distance) const
intersect: currently always throws a logic error
Definition: Box.cpp:21
geo::HeightMapNode::clone
HeightMapNode * clone() const
Definition: HeightMapNode.cpp:35
geo::HeightMapNode::intersect
bool intersect(const Ray &r, float t0, float t1, double &distance) const
Definition: HeightMapNode.cpp:39
geo::Ray
Definition: Ray.h:10
geo::HeightMapNode::occupied_
bool occupied_
Definition: HeightMapNode.h:40
geo::HeightMapNode::children_
HeightMapNode * children_[4]
Definition: HeightMapNode.h:37
std
geo::HeightMapNode::box_
Box box_
Definition: HeightMapNode.h:34
HeightMapNode.h
geo::HeightMapNode::HeightMapNode
HeightMapNode(const Box &box)
Definition: HeightMapNode.cpp:5