geolib2
Box.cpp
Go to the documentation of this file.
1 #include "geolib/Box.h"
2 
3 #include <console_bridge/console.h>
4 
5 #include <cmath>
6 #include <stdexcept>
7 #include <string>
8 
9 namespace geo {
10 
11 Box::Box(const Vector3 &min, const Vector3 &max) {
12  bounds[0] = min;
13  bounds[1] = max;
15 }
16 
17 Box* Box::clone() const {
18  return new Box(*this);
19 }
20 
21 bool Box::intersect(const Ray& r, float t0, float t1, double& distance) const {
22 
23  float tmin, tmax, tymin, tymax, tzmin, tzmax;
24  const geo::Vec3& origin = r.getOrigin();
25  const geo::Vec3& invDirection = r.getInvDirection();
26  const std::array<int, 3>& sign = r.getSign();
27  tmin = (bounds[sign[0]].x - origin.x) * invDirection.x;
28  tmax = (bounds[1-sign[0]].x - origin.x) * invDirection.x;
29  tymin = (bounds[sign[1]].y - origin.y) * invDirection.y;
30  tymax = (bounds[1-sign[1]].y - origin.y) * invDirection.y;
31 
32  if ( (tmin > tymax) || (tymin > tmax) )
33  return false;
34  if (tymin > tmin)
35  tmin = tymin;
36  if (tymax < tmax)
37  tmax = tymax;
38  tzmin = (bounds[sign[2]].z - origin.z) * invDirection.z;
39  tzmax = (bounds[1-sign[2]].z - origin.z) * invDirection.z;
40  if ( (tmin > tzmax) || (tzmin > tmax) )
41  return false;
42  if (tzmin > tmin)
43  tmin = tzmin;
44  if (tzmax < tmax)
45  tmax = tzmax;
46 
47  distance = tmin;
48  return t0 < tmax && tmin < t1;
49 }
50 
51 double Box::getMaxRadius() const {
52  return std::max(getMin().length(), getMax().length());
53 }
54 
55 bool Box::intersect(const Box& other) const {
56  const Vector3& c1 = getCenter();
57  const Vector3& c2 = other.getCenter();
58 
59  const Vector3& r1 = getSize() * 0.5;
60  const Vector3& r2 = other.getSize() * 0.5;
61 
62  if (std::abs(c1.x - c2.x) > (r1.x + r2.x)) return false;
63  if (std::abs(c1.y - c2.y) > (r1.y + r2.y)) return false;
64  if (std::abs(c1.y - c2.z) > (r1.z + r2.z)) return false;
65 
66  return true;
67 }
68 
69 bool Box::intersect(const Vector3& p, const double radius) const {
70  Vector3 c = getCenter();
71  if (radius <= 0) {
72  return contains(p);
73  }
74 
75  for (uint i = 0; i<3; ++i)
76  {
77  if (p[i] > bounds[0][i] && p[i] < bounds[1][i])
78  c[i] = p[i]; // If p is inside both bounds for this axis
79  else
80  c[i] = bounds[p[i] > c[i]][i]; // If p bigger than center take upper bound, otherwise lower bound
81  }
82 
83  return radius*radius >= (p-c).length2();
84 }
85 
86 bool Box::contains(const Vector3& p) const {
87  return (p.x >= bounds[0].x && p.x <= bounds[1].x
88  && p.y >= bounds[0].y && p.y <= bounds[1].y
89  && p.z >= bounds[0].z && p.z <= bounds[1].z);
90 }
91 
93  return *this;
94 }
95 
96 void Box::enclose(const Box& box, const Pose3D& pose) {
97  const std::vector<Vector3> points = box.getMesh().getTransformed(pose).getPoints();
98 
99  for(auto it = points.cbegin(); it != points.cend(); ++it) {
100  bounds[0].x = std::min<double>(bounds[0].x, it->x);
101  bounds[0].y = std::min<double>(bounds[0].y, it->y);
102  bounds[0].z = std::min<double>(bounds[0].z, it->z);
103 
104  bounds[1].x = std::max<double>(bounds[1].x, it->x);
105  bounds[1].y = std::max<double>(bounds[1].y, it->y);
106  bounds[1].z = std::max<double>(bounds[1].z, it->z);
107  }
108  generate_mesh_();
109 }
110 
112  return bounds[1] - bounds[0];
113 }
114 
116  return (bounds[0] + bounds[1]) / 2;
117 }
118 
119 const Vector3& Box::getMin() const {
120  return bounds[0];
121 }
122 
123 const Vector3& Box::getMax() const {
124  return bounds[1];
125 }
126 
127 void Box::setMesh(const Mesh& /*mesh*/) {
128  std::string msg = "Box::setMesh: can not set mesh for Box";
129  CONSOLE_BRIDGE_logError(msg.c_str());
130  throw std::runtime_error(msg);
131 }
132 
134  const Vector3& min = getMin();
135  const Vector3& max = getMax();
136 
137  unsigned int p0 = mesh_.addPoint(min.x, min.y, min.z); // 0
138  unsigned int p1 = mesh_.addPoint(max.x, min.y, min.z); // 1
139  unsigned int p2 = mesh_.addPoint(min.x, max.y, min.z); // 2
140  unsigned int p3 = mesh_.addPoint(max.x, max.y, min.z); // 3
141  unsigned int p4 = mesh_.addPoint(min.x, min.y, max.z); // 4
142  unsigned int p5 = mesh_.addPoint(max.x, min.y, max.z); // 5
143  unsigned int p6 = mesh_.addPoint(min.x, max.y, max.z); // 6
144  unsigned int p7 = mesh_.addPoint(max.x, max.y, max.z); // 7
145 
146  // back plane
147  mesh_.addTriangle(p1, p0, p2);
148  mesh_.addTriangle(p1, p2, p3);
149 
150  // front plane
151  mesh_.addTriangle(p4, p5, p6);
152  mesh_.addTriangle(p6, p5, p7);
153 
154  // left plane
155  mesh_.addTriangle(p0, p4, p2);
156  mesh_.addTriangle(p2, p4, p6);
157 
158  // right plane
159  mesh_.addTriangle(p5, p1, p3);
160  mesh_.addTriangle(p5, p3, p7);
161 
162  // top plane
163  mesh_.addTriangle(p0, p1, p4);
164  mesh_.addTriangle(p4, p1, p5);
165 
166  // bottom plane
167  mesh_.addTriangle(p3, p2, p6);
168  mesh_.addTriangle(p3, p6, p7);
169 }
170 
171 }
172 
geo::Vector3::y
const real & y() const
Definition: matrix.h:32
geo::Ray::getSign
const std::array< int, 3 > & getSign() const
Definition: Ray.h:24
geo::Box::contains
bool contains(const Vector3 &p) const
Determines whether a point p lies within the shape.
Definition: Box.cpp:86
geo::Box::enclose
void enclose(const Box &box, const Pose3D &pose)
Definition: Box.cpp:96
std::string
geo::Ray::getOrigin
const Vector3 & getOrigin() const
Definition: Ray.h:16
geo
Definition: Box.h:6
std::vector
geo::Box
Definition: Box.h:15
geo::Vec3T
Definition: math_types.h:13
geo::Box::getBoundingBox
Box getBoundingBox() const
Returns the smallest box which includes all mesh points. Box is not rotated, but matches the axis of ...
Definition: Box.cpp:92
geo::Transform3T
Definition: math_types.h:19
geo::Shape::getMesh
virtual const Mesh & getMesh() const
return the mesh defining the shape
Definition: Shape.cpp:425
cmath
geo::Box::getCenter
Vector3 getCenter() const
Determine the center of the box with respect to the origin of the box.
Definition: Box.cpp:115
geo::Box::bounds
Vector3 bounds[2]
Definition: Box.h:77
geo::Vec3T::y
T y
Definition: math_types.h:217
stdexcept
geo::Mesh::getPoints
const std::vector< geo::Vector3 > & getPoints() const
Definition: Mesh.cpp:42
geo::Mesh::getTransformed
Mesh getTransformed(const geo::Transform t) const
Definition: Mesh.cpp:59
geo::Mesh::addPoint
unsigned int addPoint(double x, double y, double z)
Definition: Mesh.cpp:15
std::string::c_str
T c_str(T... args)
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
std::array< int, 3 >
geo::Ray
Definition: Ray.h:10
std::runtime_error
geo::Vector3
Definition: matrix.h:12
geo::Vector3::z
const real & z() const
Definition: matrix.h:33
geo::Box::getSize
Vector3 getSize() const
get the size of the box along all axes
Definition: Box.cpp:111
geo::Box::getMin
const Vector3 & getMin() const
get vertex of the box with minimum coordinates
Definition: Box.cpp:119
geo::Box::setMesh
void setMesh(const Mesh &mesh)
set the Mesh Any child classes should throw a std::logic_error in case the mesh should not be changed...
Definition: Box.cpp:127
std::vector::cbegin
T cbegin(T... args)
geo::Box::getMaxRadius
double getMaxRadius() const
Calculate the maximum distance from the origin of the shape to any point of the shape.
Definition: Box.cpp:51
geo::Mesh::addTriangle
void addTriangle(unsigned int i1, unsigned int i2, unsigned int i3)
Definition: Mesh.cpp:26
geo::Vector3::x
const real & x() const
Definition: matrix.h:31
geo::Vec3T::z
T z
Definition: math_types.h:217
geo::Box::Box
Box(const Vector3 &min, const Vector3 &max)
Definition: Box.cpp:11
geo::Box::generate_mesh_
void generate_mesh_()
Should be called any time bounds is changed.
Definition: Box.cpp:133
Box.h
std::vector::cend
T cend(T... args)
geo::Ray::getInvDirection
const Vector3 & getInvDirection() const
Definition: Ray.h:20
std::max
T max(T... args)
c
void c()
geo::Box::clone
Box * clone() const
Definition: Box.cpp:17
geo::Shape::mesh_
Mesh mesh_
Should not be read or written to directly in general. Use setMesh and getMesh to write respectively r...
Definition: Shape.h:112
geo::Mesh
Definition: Mesh.h:25
geo::Box::getMax
const Vector3 & getMax() const
get vertex of the box with maximum coordinates
Definition: Box.cpp:123
geo::Vec3T::x
T x
Definition: math_types.h:217
string