Moby
OBB.h
1 /****************************************************************************
2  * Copyright 2008 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_OBB_H_
8 #define _MOBY_OBB_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 <Ravelin/Vector3d.h>
16 #include <Ravelin/AAngled.h>
17 #include <Ravelin/MatrixNd.h>
18 #include <Ravelin/VectorNd.h>
19 #include <Ravelin/LinAlgd.h>
20 #include <Moby/BV.h>
21 #include <Moby/Constants.h>
22 #include <Moby/Types.h>
23 #include <Moby/FastThreadable.h>
24 #include <Moby/CompGeom.h>
25 //#include <Moby/TriangleMeshPrimitive.h>
26 
27 namespace Moby {
28 
30 
38 class OBB : public BV
39 {
40  public:
41  OBB();
42  OBB(const OBB& obb) { operator=(obb); }
43  OBB(const Point3d& center, const Ravelin::Matrix3d& R, const Ravelin::Vector3d& l);
44  OBB(const OBB& o, const Ravelin::Vector3d& v);
45  OBB& operator=(const OBB& obb);
46  virtual void transform(const Ravelin::Transform3d& T, BV* result) const;
47  virtual BVPtr calc_swept_BV(CollisionGeometryPtr g, const Ravelin::SVelocityd& v) const;
48  static double calc_sq_dist(const OBB& o, const Point3d& p);
49  static double calc_dist(const OBB& a, const OBB& b, Point3d& cpa, Point3d& cpb);
50  static double calc_dist(const OBB& a, const OBB& b, const Ravelin::Transform3d& aTb, Point3d& cpa, Point3d& cpb);
51  static bool intersects(const OBB& a, const OBB& b);
52  static bool intersects(const OBB& a, const OBB& b, const Ravelin::Transform3d& aTb);
53  static bool intersects(const OBB& a, const LineSeg3& seg, double& tmin, double tmax, Point3d& q);
54  virtual bool intersects(const LineSeg3& seg, double& tmin, double tmax, Point3d& q) const { return OBB::intersects(*this, seg, tmin, tmax, q); }
55  static bool outside(const OBB& a, const Point3d& point, double tol = NEAR_ZERO);
56  virtual bool outside(const Point3d& point, double tol = NEAR_ZERO) const { return OBB::outside(*this, point, tol); }
57  virtual std::ostream& to_vrml(std::ostream& out, const Ravelin::Pose3d& T) const;
58  unsigned calc_size() const;
60  virtual boost::shared_ptr<const Ravelin::Pose3d> get_relative_pose() const { return center.pose; }
61  virtual Point3d get_lower_bounds() const;
62  virtual Point3d get_upper_bounds() const;
63  static OBBPtr load_from_xml(boost::shared_ptr<const XMLTree> root);
64 
65  template <class ForwardIterator>
66  void expand_to_fit(ForwardIterator begin, ForwardIterator end);
67 
68  template <class ForwardIterator>
69  OBB(ForwardIterator begin, ForwardIterator end);
70 
71  template <class ForwardIterator>
72  static OBB calc_min_volume_OBB(ForwardIterator begin, ForwardIterator end);
73 
74  template <class OutputIterator>
75  OutputIterator get_vertices(OutputIterator begin) const;
76 
78  virtual double calc_volume() const { return l[0] * l[1] * l[2]; }
79 
82 
84  Ravelin::Vector3d l;
85 
87  Ravelin::Matrix3d R;
88 
89  private:
90  template <class ForwardIterator>
91  static OBB calc_low_dim_OBB(ForwardIterator begin, ForwardIterator end);
92 
93  template <class ForwardIterator>
94  static void calc_lengths(const Ravelin::Vector3d& d1, const Ravelin::Vector3d& d2, const Ravelin::Vector3d& d3, const Point3d& center, ForwardIterator begin, ForwardIterator end, double lengths[3]);
95 
96  template <class ForwardIterator>
97  static void align(ForwardIterator begin, ForwardIterator end, const Ravelin::Vector3d& d1, Ravelin::Vector3d& d2);
98 }; // end class
99 
100 // include inline functions
101 #include "OBB.inl"
102 
103 } // end namespace
104 
105 #endif
106 
XMLTreePtr save_to_xml_tree() const
Saves a OBB hiearchy to a XMLTree.
Definition: OBB.cpp:756
boost::shared_ptr< BV > BVPtr
Bounding volume (BV) smart pointer.
Definition: Types.h:92
virtual Point3d get_upper_bounds() const
Gets the upper bounds on the OBB.
Definition: OBB.cpp:904
virtual BVPtr calc_swept_BV(CollisionGeometryPtr g, const Ravelin::SVelocityd &v) const
Calculates the velocity-expanded OBB for a body.
Definition: OBB.cpp:790
static double calc_sq_dist(const OBB &o, const Point3d &p)
Computes the squared distance from a point to an OBB.
Definition: OBB.cpp:219
static OBBPtr load_from_xml(boost::shared_ptr< const XMLTree > root)
Loads an OBB hierarchy from a XML tree.
Definition: OBB.cpp:675
An oriented bounding box that optionally allows building an OBB tree.
Definition: OBB.h:38
Ravelin::Vector3d l
Half-lengths of the OBBs in the three axes directions (i.e., in the box frame)
Definition: OBB.h:84
static double calc_dist(const OBB &a, const OBB &b, Point3d &cpa, Point3d &cpb)
Determines the distance between two OBBs.
Definition: OBB.cpp:350
virtual void transform(const Ravelin::Transform3d &T, BV *result) const
Transforms the OBB using the given transform.
Definition: OBB.cpp:36
unsigned calc_size() const
Constructs an OBB from a triangle mesh using the mesh inertia method [Ericson, 2003].
Definition: OBB.cpp:209
boost::shared_ptr< OBB > OBBPtr
Oriented bounding box (OBB) smart pointer.
Definition: Types.h:98
OBB & operator=(const OBB &obb)
Copies an OBB.
Definition: OBB.cpp:55
An abstract bounding volume.
Definition: BV.h:38
OutputIterator get_vertices(OutputIterator begin) const
Gets the eight vertices of the bounding box.
Definition: OBB.h:723
virtual double calc_volume() const
Calculates 1/8th of the volume of the bounding box.
Definition: OBB.h:78
boost::shared_ptr< CollisionGeometry > CollisionGeometryPtr
Collision geometry smart pointer.
Definition: Types.h:77
boost::shared_ptr< XMLTree > XMLTreePtr
XML tree smart pointer.
Definition: Types.h:104
virtual boost::shared_ptr< const Ravelin::Pose3d > get_relative_pose() const
Gets the associated pose for this bounding volume.
Definition: OBB.h:60
virtual bool intersects(const LineSeg3 &seg, double &tmin, double tmax, Point3d &q) const
Determines whether a line segment intersects the bounding volume.
Definition: OBB.h:54
Ravelin::Vector3d Point3d
Typedef to distinguish between a 3D vector and a point.
Definition: Types.h:47
virtual Point3d get_lower_bounds() const
Gets the lower bounds on the OBB.
Definition: OBB.cpp:884
virtual std::ostream & to_vrml(std::ostream &out, const Ravelin::Pose3d &T) const
Outputs the OBB in VRML format to the given stream.
Definition: OBB.cpp:546
std::pair< Point3d, Point3d > LineSeg3
Typedef to make specifying line segments easier.
Definition: Types.h:50
static OBB calc_min_volume_OBB(ForwardIterator begin, ForwardIterator end)
Computes the minimum OBB from a set of points.
Definition: OBB.h:226
static bool outside(const OBB &a, const Point3d &point, double tol=NEAR_ZERO)
Determines whether a point is outside a OBB to within the given tolerance.
Definition: OBB.cpp:243
virtual bool outside(const Point3d &point, double tol=NEAR_ZERO) const
Determines whether a point is outside the bounding volume.
Definition: OBB.h:56
Point3d center
Center of the bounding box.
Definition: OBB.h:81
void expand_to_fit(ForwardIterator begin, ForwardIterator end)
Expands this OBB (if necessary) to fit the given points.
Definition: OBB.h:661
Ravelin::Matrix3d R
Orientation of this OBB.
Definition: OBB.h:87
static bool intersects(const OBB &a, const OBB &b)
Determines whether two OBBs intersect another.
Definition: OBB.cpp:403