geolib2
CompositeShape.cpp
Go to the documentation of this file.
2 
3 #include <console_bridge/console.h>
4 
5 #include <stdexcept>
6 
7 namespace geo {
8 
9 CompositeShape::CompositeShape() : Shape(), max_radius_(0), min_(1e10, 1e10, 1e10), max_(-1e10, -1e10, -1e10), bb_(-min_, -max_) {
10 }
11 
13 }
14 
16  return new CompositeShape(*this);
17 }
18 
19 bool CompositeShape::intersect(const Ray& r, float t0, float t1, double& distance) const {
20  if (!bb_.intersect(r, t0, t1, distance)) {
21  return false;
22  }
23 
24  bool hit = false;
25  double min_distance = t1;
26 
27 
28  for(std::vector<std::pair<ShapePtr, Transform> >::const_iterator it = shapes_.begin(); it != shapes_.end(); ++it) {
29  const Transform& pose_inv = it->second;
30 
31  const Shape& shape = *it->first;
32 
33  Ray r_t(pose_inv * r.getOrigin(), pose_inv.getBasis() * r.getDirection());
34 
35  double d;
36  if (shape.intersect(r_t, t0, min_distance, d)) {
37  min_distance = d;
38  hit = true;
39  }
40  }
41 
42  if (hit) {
43  distance = min_distance;
44  return true;
45  }
46 
47  return false;
48 }
49 
50 bool CompositeShape::intersect(const Vector3& p, const double radius) const {
51  if (!bb_.intersect(p, radius)) {
52  return false;
53  }
54  for(auto it = shapes_.begin(); it != shapes_.end(); ++it) {
55  const Transform& pose_inv = it->second;
56  Vector3 p_t = pose_inv * p;
57  if ((it->first)->intersect(p_t, radius)) {
58  return true;
59  }
60  }
61  return false;
62 }
63 
64 bool CompositeShape::contains(const Vector3& p) const {
65  if (!bb_.contains(p)) {
66  return false;
67  }
68  for(auto it = shapes_.begin(); it != shapes_.end(); ++it) {
69  const Transform& pose_inv = it->second;
70 
71  const Shape& shape = *it->first;
72 
73  Vector3 p_t = pose_inv * p;
74 
75  if (shape.contains(p_t)) {
76  return true;
77  }
78  }
79  return false;
80 }
81 
83  return max_radius_;
84 }
85 
86 void CompositeShape::addShape(const Shape& shape, const Pose3D& pose) {
87  // add to shapes
88  shapes_.push_back(std::pair<ShapePtr, Transform>(ShapePtr(shape.clone()), pose.inverse()));
89 
90  // add to mesh
91  const std::vector<Triangle>& triangles = shape.getMesh().getTriangles();
92  for(std::vector<Triangle>::const_iterator it = triangles.begin(); it != triangles.end(); ++it) {
93  Vector3 p1 = pose * it->p1();
94  Vector3 p2 = pose * it->p2();
95  Vector3 p3 = pose * it->p3();
96 
97  max_radius_ = std::max<double>(max_radius_, p1.length());
98  max_radius_ = std::max<double>(max_radius_, p2.length());
99  max_radius_ = std::max<double>(max_radius_, p3.length());
100 
101  min_.x = std::min<double>(min_.x, std::min(p1.x, std::min(p2.x, p3.x)));
102  min_.y = std::min<double>(min_.y, std::min(p1.y, std::min(p2.y, p3.y)));
103  min_.z = std::min<double>(min_.z, std::min(p1.z, std::min(p2.z, p3.z)));
104 
105  max_.x = std::max<double>(max_.x, std::max(p1.x, std::max(p2.x, p3.x)));
106  max_.y = std::max<double>(max_.y, std::max(p1.y, std::max(p2.y, p3.y)));
107  max_.z = std::max<double>(max_.z, std::max(p1.z, std::max(p2.z, p3.z)));
108  }
109 
110  mesh_.add(shape.getMesh().getTransformed(pose));
111 
112  bb_ = Box(min_, max_);
113 }
114 
116  return bb_;
117 }
118 
120  return shapes_;
121 }
122 
123 void CompositeShape::setMesh(const Mesh& /*mesh*/) {
124  std::string msg = "CompositeShape::setMesh: can not set mesh for CompositeShape";
125  CONSOLE_BRIDGE_logError(msg.c_str());
126  throw std::runtime_error(msg);
127 }
128 
129 }
geo::Vector3::y
const real & y() const
Definition: matrix.h:32
geo::Shape::intersect
virtual bool intersect(const Ray &r, float t0, float t1, double &distance) const
intersect: currently always throws a logic error
Definition: Shape.cpp:149
geo::Box::contains
bool contains(const Vector3 &p) const
Determines whether a point p lies within the shape.
Definition: Box.cpp:86
geo::CompositeShape::clone
CompositeShape * clone() const
Definition: CompositeShape.cpp:15
geo::ShapePtr
std::shared_ptr< Shape > ShapePtr
Definition: datatypes.h:12
std::string
geo::Ray::getOrigin
const Vector3 & getOrigin() const
Definition: Ray.h:16
geo
Definition: Box.h:6
std::pair
std::vector
geo::Box
Definition: Box.h:15
geo::Mesh::add
void add(const Mesh &mesh)
Definition: Mesh.cpp:31
geo::Transform3T
Definition: math_types.h:19
geo::CompositeShape::addShape
void addShape(const Shape &shape, const Pose3D &pose)
add a shape to the composite
Definition: CompositeShape.cpp:86
geo::Vector3::length
real length() const
Definition: matrix.h:43
geo::Shape::getMesh
virtual const Mesh & getMesh() const
return the mesh defining the shape
Definition: Shape.cpp:425
geo::CompositeShape::getBoundingBox
Box getBoundingBox() const
Returns the smallest box which includes all mesh points. Box is not rotated, but matches the axis of ...
Definition: CompositeShape.cpp:115
stdexcept
geo::Transform3T::inverse
Transform3T inverse() const
Definition: math_types.h:747
geo::CompositeShape::intersect
bool intersect(const Ray &r, float t0, float t1, double &distance) const
intersect: currently always throws a logic error
Definition: CompositeShape.cpp:19
geo::Mesh::getTransformed
Mesh getTransformed(const geo::Transform t) const
Definition: Mesh.cpp:59
geo::Mesh::getTriangles
const std::vector< Triangle > & getTriangles() const
Definition: Mesh.cpp:50
geo::CompositeShape::~CompositeShape
virtual ~CompositeShape()
Definition: CompositeShape.cpp:12
geo::CompositeShape::CompositeShape
CompositeShape()
Definition: CompositeShape.cpp:9
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
geo::Transform
Definition: matrix.h:170
geo::Ray
Definition: Ray.h:10
std::runtime_error
geo::CompositeShape::getMaxRadius
double getMaxRadius() const
Calculate the maximum distance from the origin of the shape to any point of the shape.
Definition: CompositeShape.cpp:82
geo::Vector3
Definition: matrix.h:12
geo::Vector3::z
const real & z() const
Definition: matrix.h:33
geo::CompositeShape::min_
Vector3 min_
Definition: CompositeShape.h:61
geo::Transform::getBasis
Matrix3x3 getBasis() const
Definition: matrix.h:217
std::min
T min(T... args)
geo::CompositeShape::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: CompositeShape.cpp:123
geo::CompositeShape
A geometric description of a shape as a union of other shapes.
Definition: CompositeShape.h:16
geo::CompositeShape::max_
Vector3 max_
Definition: CompositeShape.h:63
std::vector::begin
T begin(T... args)
CompositeShape.h
geo::CompositeShape::max_radius_
double max_radius_
Definition: CompositeShape.h:59
geo::Shape::contains
virtual bool contains(const Vector3 &p) const
Determines whether a point p lies within the shape.
Definition: Shape.cpp:229
geo::Vector3::x
const real & x() const
Definition: matrix.h:31
geo::Ray::getDirection
const Vector3 & getDirection() const
Definition: Ray.h:18
geo::CompositeShape::getShapes
const std::vector< std::pair< ShapePtr, Transform > > & getShapes() const
Get all the child shapes and their inverse pose relative to the "origin" of the CompositeShape.
Definition: CompositeShape.cpp:119
geo::CompositeShape::contains
bool contains(const Vector3 &p) const
Determines whether a point p lies within the shape.
Definition: CompositeShape.cpp:64
std::vector::end
T end(T... args)
geo::CompositeShape::shapes_
std::vector< std::pair< ShapePtr, Transform > > shapes_
Definition: CompositeShape.h:57
geo::CompositeShape::bb_
Box bb_
Definition: CompositeShape.h:65
std::max
T max(T... args)
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::Shape
A geometric description of a shape.
Definition: Shape.h:19
geo::Shape::clone
virtual Shape * clone() const
Definition: Shape.cpp:145