Moby
BV.h
1 /****************************************************************************
2  * Copyright 2009 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef _MOBY_BV_H_
8 #define _MOBY_BV_H_
9 
10 #include <stack>
11 #include <iostream>
12 #include <queue>
13 #include <boost/tuple/tuple.hpp>
14 #include <boost/enable_shared_from_this.hpp>
15 #include <boost/foreach.hpp>
16 #include <Ravelin/Vector3d.h>
17 #include <Ravelin/Pose3d.h>
18 #include <Moby/Constants.h>
19 #include <Moby/Types.h>
20 
21 namespace Moby {
22 
23 class AABB;
24 class OBB;
25 class BoundingSphere;
26 class SSR;
27 class SSL;
28 
30 
38 class BV : public boost::enable_shared_from_this<BV>
39 {
40  public:
41  virtual ~BV() {}
42 
44  virtual std::ostream& to_vrml(std::ostream& out, const Ravelin::Pose3d& T) const = 0
45 ;
46 
48  virtual bool outside(const Point3d& point, double tol = NEAR_ZERO) const = 0;
49 
51  virtual bool intersects(const LineSeg3& seg, double& tmin, double tmax, Point3d& q) const = 0;
52 
54  virtual boost::shared_ptr<const Ravelin::Pose3d> get_relative_pose() const = 0;
55 
57  virtual void transform(const Ravelin::Transform3d& T, BV* result) const = 0;
58 
60 
65  virtual BVPtr calc_swept_BV(CollisionGeometryPtr g, const Ravelin::SVelocityd& v) const = 0;
66 
68  static bool intersects(BVPtr a, BVPtr b) { return intersects(a.get(), b.get()); }
69 
71  static bool intersects(BVPtr a, BVPtr b, const Ravelin::Transform3d& T) { return intersects(a.get(), b.get(), T); }
72 
74  static double calc_distance(BVPtr a, BVPtr b, Point3d& cp1, Point3d& cp2) { return calc_distance(a.get(), b.get(), cp1, cp2); }
75 
77  static double calc_distance(BVPtr a, BVPtr b, const Ravelin::Transform3d& aTb, Point3d& cp1, Point3d& cp2) { return calc_distance(a.get(), b.get(), aTb, cp1, cp2); }
78 
79  static bool intersects(const BV* a, const BV* b);
80  static bool intersects(const BV* a, const BV* b, const Ravelin::Transform3d& T);
81  static double calc_distance(const BV* a, const BV* b, Point3d& cp1, Point3d& cp2);
82  static double calc_distance(const BV* a, const BV* b, const Ravelin::Transform3d& aTb, Point3d& cp1, Point3d& cp2);
83 
84  BVPtr get_this() { return boost::dynamic_pointer_cast<BV>(shared_from_this()); }
85  boost::shared_ptr<const BV> get_this() const { return boost::dynamic_pointer_cast<const BV>(shared_from_this()); }
86  bool is_leaf() const { return children.empty(); }
87 
88  template <class OutputIterator>
89  OutputIterator get_all_BVs(OutputIterator begin) const;
90 
91  template <class OutputIterator>
92  OutputIterator get_all_leafs(OutputIterator begin) const;
93 
94  template <class OutputIterator>
95  static OutputIterator intersect_BV_trees(BVPtr a, BVPtr b, const Ravelin::Transform3d& aTb, const Ravelin::Transform3d& bTa, OutputIterator output_begin);
96 
98  boost::shared_ptr<void> userdata;
99 
102 
104  std::list<BVPtr> children;
105 
107  virtual double calc_volume() const = 0;
108 
110  virtual Point3d get_lower_bounds() const = 0;
111 
113  virtual Point3d get_upper_bounds() const = 0;
114 
115  private:
116 
117  static bool intersects(const OBB* O, const BoundingSphere* S);
118  static bool intersects(const OBB* O, const BoundingSphere* S, const Ravelin::Transform3d& OTS);
119  static bool intersects(const OBB* O, const AABB* A);
120  static bool intersects(const OBB* O, const AABB* A, const Ravelin::Transform3d& OTA);
121  static bool intersects(const OBB* O, const SSR* S);
122  static bool intersects(const OBB* O, const SSR* S, const Ravelin::Transform3d& OTS);
123  static bool intersects(const OBB* O, const SSL* S);
124  static bool intersects(const OBB* O, const SSL* S, const Ravelin::Transform3d& OTS);
125  static bool intersects(const AABB* A, const BoundingSphere* S);
126  static bool intersects(const AABB* A, const BoundingSphere* S, const Ravelin::Transform3d& ATS);
127  static bool intersects(const AABB* A, const SSL* S);
128  static bool intersects(const AABB* A, const SSL* S, const Ravelin::Transform3d& ATS);
129  static bool intersects(const SSR* S, const AABB* A);
130  static bool intersects(const SSR* S, const AABB* A, const Ravelin::Transform3d& STA);
131  static bool intersects(const SSR* S, const BoundingSphere* B);
132  static bool intersects(const SSR* S, const BoundingSphere* B, const Ravelin::Transform3d& STB);
133  static bool intersects(const SSR* S, const SSL* B);
134  static bool intersects(const SSR* S, const SSL* B, const Ravelin::Transform3d& STB);
135  static bool intersects(const SSL* S, const BoundingSphere* B);
136  static bool intersects(const SSL* S, const BoundingSphere* B, const Ravelin::Transform3d& STB);
137 }; // end class
138 
139 // include inline functions
140 #include "BV.inl"
141 
142 } // end namespace
143 
144 #endif
145 
static bool intersects(BVPtr a, BVPtr b)
Convenience method.
Definition: BV.h:68
virtual boost::shared_ptr< const Ravelin::Pose3d > get_relative_pose() const =0
Gets the associated pose for this bounding volume.
virtual Point3d get_upper_bounds() const =0
Gets the upper bound on a AABB around the bounding volume when a transform of T is applied...
virtual Point3d get_lower_bounds() const =0
Gets the lower bound on a AABB around the bounding volume when a transform of T is applied...
boost::shared_ptr< BV > BVPtr
Bounding volume (BV) smart pointer.
Definition: Types.h:92
static double calc_distance(BVPtr a, BVPtr b, Point3d &cp1, Point3d &cp2)
Convenience method.
Definition: BV.h:74
A sphere-swept rectangle (SSR) that optionally allows building an SSR tree.
Definition: SSR.h:35
An oriented bounding box that optionally allows building an OBB tree.
Definition: OBB.h:38
An abstract bounding volume.
Definition: BV.h:38
virtual std::ostream & to_vrml(std::ostream &out, const Ravelin::Pose3d &T) const =0
Virtual function for outputting the bounding volume to VRML.
virtual BVPtr calc_swept_BV(CollisionGeometryPtr g, const Ravelin::SVelocityd &v) const =0
Virtual function that calculates a velocity-expanded BV.
static double calc_distance(BVPtr a, BVPtr b, const Ravelin::Transform3d &aTb, Point3d &cp1, Point3d &cp2)
Convenience method.
Definition: BV.h:77
static bool intersects(BVPtr a, BVPtr b, const Ravelin::Transform3d &T)
Convenience method.
Definition: BV.h:71
OutputIterator get_all_leafs(OutputIterator begin) const
Gets all leaf nodes.
Definition: BV.h:32
boost::shared_ptr< CollisionGeometry > CollisionGeometryPtr
Collision geometry smart pointer.
Definition: Types.h:77
boost::shared_ptr< void > userdata
Userdata for the BV.
Definition: BV.h:98
An axis-aligned bounding box.
Definition: AABB.h:16
A sphere-swept line (SSL) that optionally allows building an SSL tree.
Definition: SSL.h:26
virtual double calc_volume() const =0
Gets the volume for this bounding volume.
OutputIterator get_all_BVs(OutputIterator begin) const
Gets all BV nodes.
Definition: BV.h:13
Ravelin::Vector3d Point3d
Typedef to distinguish between a 3D vector and a point.
Definition: Types.h:47
CollisionGeometryPtr geom
The collision geometry associated with this bounding volume.
Definition: BV.h:101
std::pair< Point3d, Point3d > LineSeg3
Typedef to make specifying line segments easier.
Definition: Types.h:50
virtual bool outside(const Point3d &point, double tol=NEAR_ZERO) const =0
Determines whether a point is outside the bounding volume.
static OutputIterator intersect_BV_trees(BVPtr a, BVPtr b, const Ravelin::Transform3d &aTb, const Ravelin::Transform3d &bTa, OutputIterator output_begin)
Intersects two BV trees; returns list of all leaf-level intersecting BVs.
Definition: BV.h:70
A sphere used for bounding geometry.
Definition: BoundingSphere.h:16
std::list< BVPtr > children
The children of this BV.
Definition: BV.h:104
virtual bool intersects(const LineSeg3 &seg, double &tmin, double tmax, Point3d &q) const =0
Determines whether a line segment intersects the bounding volume.
virtual void transform(const Ravelin::Transform3d &T, BV *result) const =0
Virtual function for transforming the BV.