Moby
SSR.h
1 /****************************************************************************
2  * Copyright 2010 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_SSR_H_
8 #define _MOBY_SSR_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/LinAlgd.h>
16 #include <Ravelin/Vector3d.h>
17 #include <Ravelin/AAngled.h>
18 #include <Moby/BV.h>
19 #include <Moby/Constants.h>
20 #include <Moby/FastThreadable.h>
21 #include <Moby/Types.h>
22 #include <Moby/CompGeom.h>
23 
24 namespace Moby {
25 
27 
35 class SSR : public BV
36 {
37  public:
38  SSR();
39  SSR(const SSR& obb) { operator=(obb); }
40  SSR(const Point3d& center, const Ravelin::Matrix3d& R, const Ravelin::Vector2d& l, double radius);
41  SSR(const SSR& s, const Ravelin::Vector3d& v);
42  SSR& operator=(const SSR& s);
43  virtual BVPtr calc_swept_BV(CollisionGeometryPtr g, const Ravelin::SVelocityd& v) const;
44  static double calc_dist(const SSR& a, const Point3d& p);
45  static double calc_dist(const SSR& a, const LineSeg3& s);
46  static double calc_dist(const SSR& a, const SSR& b, Point3d& cpa, Point3d& cpb);
47  static double calc_dist(const SSR& a, const SSR& b, const Ravelin::Transform3d& aTb, Point3d& cpa, Point3d& cpb);
48  static bool intersects(const SSR& a, const SSR& b);
49  static bool intersects(const SSR& a, const SSR& b, const Ravelin::Transform3d& T);
50  static bool intersects(const SSR& a, const LineSeg3& seg, double& tmin, double tmax, Point3d& q);
51  virtual bool intersects(const LineSeg3& seg, double& tmin, double tmax, Point3d& q) const { return SSR::intersects(*this, seg, tmin, tmax, q); }
52  static bool outside(const SSR& a, const Point3d& point, double tol = NEAR_ZERO);
53  virtual bool outside(const Point3d& point, double tol = NEAR_ZERO) const { return SSR::outside(*this, point, tol); }
54  boost::shared_ptr<SSR> get_this() { return boost::dynamic_pointer_cast<SSR>(shared_from_this()); }
55  boost::shared_ptr<const SSR> get_this() const { return boost::dynamic_pointer_cast<const SSR>(shared_from_this()); }
56  virtual std::ostream& to_vrml(std::ostream& out, const Ravelin::Pose3d& T) const;
57  unsigned calc_size() const;
58  virtual boost::shared_ptr<const Ravelin::Pose3d> get_relative_pose() const { return center.pose; }
59  virtual void transform(const Ravelin::Transform3d& T, BV* result) const;
60  virtual Point3d get_lower_bounds() const;
61  virtual Point3d get_upper_bounds() const;
62  void get_rect_verts(Point3d rect_verts[4]) const;
63 
64  template <class ForwardIterator>
65  void expand_to_fit(ForwardIterator begin, ForwardIterator end);
66 
67  template <class ForwardIterator>
68  SSR(ForwardIterator begin, ForwardIterator end);
69 
71  virtual double calc_volume() const { return std::sqrt(l[0]*l[0] + l[1]*l[1]) + 2*radius; }
72 
75 
77  Ravelin::Vector2d l;
78 
80  Ravelin::Matrix3d R;
81 
83  double radius;
84 
85  private:
86  static double calc_sq_dist(const Point3d& p, const Point3d& rect_center, const Ravelin::Vector3d& axis1, const Ravelin::Vector3d& axis2, const Ravelin::Vector2d& lengths, Point3d& cp_rect);
87  static double calc_sq_dist(const Point3d& origin, const Ravelin::Vector3d& dir, const LineSeg3& seg, Point3d& cp_line, Point3d& cp_seg, double& line_param);
88  static double calc_sq_dist(const Point3d& origin, const Ravelin::Vector3d& dir, const Point3d& rect_center, const Ravelin::Vector3d& axis1, const Ravelin::Vector3d& axis2, const Ravelin::Vector2d& lengths, Point3d& cp_line, Point3d& cp_rect, double& line_param);
89  static double calc_sq_dist(const LineSeg3& seg, const Point3d& rect_center, const Ravelin::Vector3d& axis1, const Ravelin::Vector3d& axis2, const Ravelin::Vector2d& lengths, Point3d& cp_seg, Point3d& cp_rect);
90  static double calc_sq_dist(const Point3d& a_center, const Ravelin::Vector3d& aaxis1, const Ravelin::Vector3d& aaxis2, const Ravelin::Vector2d& alengths, const Point3d& b_center, const Ravelin::Vector3d& baxis1, const Ravelin::Vector3d& baxis2, const Ravelin::Vector2d& blengths, Point3d& cpa, Point3d& cpb);
91 
92  template <class ForwardIterator>
93  void calc_lengths_and_radius(ForwardIterator begin, ForwardIterator end);
94 
95  template <class ForwardIterator>
96  static void align(ForwardIterator begin, ForwardIterator end, const Ravelin::Vector3d& d1, Ravelin::Vector3d& d2);
97 }; // end class
98 
99 // include inline functions
100 #include "SSR.inl"
101 
102 } // end namespace
103 
104 #endif
105 
boost::shared_ptr< BV > BVPtr
Bounding volume (BV) smart pointer.
Definition: Types.h:92
SSR()
Initializes an empty SSR.
Definition: SSR.cpp:16
A sphere-swept rectangle (SSR) that optionally allows building an SSR tree.
Definition: SSR.h:35
unsigned calc_size() const
Calculates the size (number of elements in) a SSR tree.
Definition: SSR.cpp:573
virtual void transform(const Ravelin::Transform3d &T, BV *result) const
Transforms the SSR using the given transform.
Definition: SSR.cpp:45
Ravelin::Matrix3d R
Orientation of this SSR.
Definition: SSR.h:80
virtual std::ostream & to_vrml(std::ostream &out, const Ravelin::Pose3d &T) const
Outputs the SSR to VRML (not yet implemented)
Definition: SSR.cpp:659
An abstract bounding volume.
Definition: BV.h:38
virtual bool intersects(const LineSeg3 &seg, double &tmin, double tmax, Point3d &q) const
Determines whether a line segment intersects the bounding volume.
Definition: SSR.h:51
virtual Point3d get_upper_bounds() const
Gets the upper bounds of the SSR.
Definition: SSR.cpp:610
static bool intersects(const SSR &a, const SSR &b)
Determines whether two SSRs intersect.
Definition: SSR.cpp:503
boost::shared_ptr< CollisionGeometry > CollisionGeometryPtr
Collision geometry smart pointer.
Definition: Types.h:77
Ravelin::Vector2d l
Lengths of the rectangle sides.
Definition: SSR.h:77
double radius
Radius of the spherical addition.
Definition: SSR.h:83
virtual double calc_volume() const
Calculates (approximate?) volume of the SSR.
Definition: SSR.h:71
void get_rect_verts(Point3d rect_verts[4]) const
Gets the vertices of the rectangle.
Definition: SSR.cpp:637
Ravelin::Vector3d Point3d
Typedef to distinguish between a 3D vector and a point.
Definition: Types.h:47
static bool outside(const SSR &a, const Point3d &point, double tol=NEAR_ZERO)
Determines whether a point is outside the SSR.
Definition: SSR.cpp:566
virtual Point3d get_lower_bounds() const
Gets the lower bounds of the SSR.
Definition: SSR.cpp:583
SSR & operator=(const SSR &s)
Copies one SSR to another.
Definition: SSR.cpp:61
virtual bool outside(const Point3d &point, double tol=NEAR_ZERO) const
Determines whether a point is outside the bounding volume.
Definition: SSR.h:53
Point3d center
Center of the volume.
Definition: SSR.h:74
std::pair< Point3d, Point3d > LineSeg3
Typedef to make specifying line segments easier.
Definition: Types.h:50
virtual boost::shared_ptr< const Ravelin::Pose3d > get_relative_pose() const
Gets the associated pose for this bounding volume.
Definition: SSR.h:58
static double calc_dist(const SSR &a, const Point3d &p)
Calculates the distance of the sphere-swept rectangle from a point.
Definition: SSR.cpp:97
virtual BVPtr calc_swept_BV(CollisionGeometryPtr g, const Ravelin::SVelocityd &v) const
Calculates the velocity-expanded bounding volume.
Definition: SSR.cpp:72