Moby
TessellatedPolyhedron.h
1 /****************************************************************************
2  * Copyright 2006 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 _TESSELLATED_POLYHEDRON_H
8 #define _TESSELLATED_POLYHEDRON_H
9 
10 #include <ostream>
11 #include <vector>
12 #include <list>
13 #include <map>
14 #include <Moby/IndexedTriArray.h>
15 #include <Moby/Constants.h>
16 #include <Moby/Polyhedron.h>
17 #include <Moby/InvalidIndexException.h>
18 
19 namespace Moby {
20 
21 class Triangle;
22 
24 
29 {
30  public:
31  enum LocationType { eInside, eOutside, eOnVertex, eOnEdge, eOnFace };
32 
33  TessellatedPolyhedron() { _convexity_computed = false; }
35  TessellatedPolyhedron(const TessellatedPolyhedron& p) { _convexity_computed = false; operator=(p); }
36  static TessellatedPolyhedronPtr minkowski(TessellatedPolyhedron& p1, boost::shared_ptr<const Ravelin::Pose3d> T1, TessellatedPolyhedron& p2, boost::shared_ptr<const Ravelin::Pose3d> T2, bool reflect_p2 = true);
37  void operator=(const TessellatedPolyhedron& p);
38  const IndexedTriArray& get_mesh() const { return _mesh; }
39  void transform(const Ravelin::Transform3d& T);
40  const std::vector<Ravelin::Origin3d>& get_vertices() const { return _mesh.get_vertices(); }
41  const std::vector<IndexedTri>& get_facets() const { return _mesh.get_facets(); }
42  bool inside(const Ravelin::Origin3d& point, double tol = NEAR_ZERO);
43  bool inside_or_on(const Ravelin::Origin3d& point, double tol = NEAR_ZERO);
44  LocationType location(const Ravelin::Origin3d& point, double tol = NEAR_ZERO) const;
45  static void to_vrml(std::ostream& out, const TessellatedPolyhedron& p, Ravelin::Origin3d diffuse_color = Ravelin::Origin3d(1,1,1), bool wireframe = false);
46  double calc_volume() const;
47  bool consistent() const;
48  bool degenerate() const;
52  const Ravelin::Origin3d& find_extreme_vertex(const Ravelin::Origin3d& direction);
53  void to_polyhedron(Polyhedron& p) const;
54 
55  template <class InputIterator1, class InputIterator2>
56  TessellatedPolyhedron(InputIterator1 verts_begin, InputIterator1 verts_end, InputIterator2 facets_begin, InputIterator2 facets_end);
57 
59  const std::list<unsigned>& get_incident_facets(unsigned i) const { return _mesh.get_incident_facets(i); }
60 
62  double calc_signed_distance(const Ravelin::Origin3d& point, unsigned& closest_facet);
63 
65  double calc_signed_distance(const Ravelin::Origin3d& point) { unsigned discard; return calc_signed_distance(point, discard); }
66 
68  std::pair<Ravelin::Origin3d, Ravelin::Origin3d> get_bounding_box_corners() const { return std::make_pair(_bb_min, _bb_max); }
69 
71  bool is_convex() { return convexity() < NEAR_ZERO; }
72 
73 
75 
80  double convexity() { if (!_convexity_computed) determine_convexity(); return _convexity; }
81 
82  private:
83  static void replace_edge(const std::vector<Ravelin::Origin3d>& v, std::vector<IndexedTri>& f, unsigned a, unsigned b, unsigned c, std::vector<unsigned>& del_list);
84  static bool find_vertex(const std::vector<Ravelin::Origin3d>& vertices, const Ravelin::Origin3d& v);
85  void calc_bounding_box();
86  static void calc_subexpressions(double w0, double w1, double w2, double& f1, double& f2, double& f3, double& g0, double& g1, double& g2);
87  void determine_convexity();
88  static Ravelin::Origin3d intersect_plane(const Ravelin::Vector3d& normal, double d, const Ravelin::Origin3d& p1, const Ravelin::Origin3d& p2);
89  static void slice(const TessellatedPolyhedron& p1, const TessellatedPolyhedron& p2, IndexedTriArray& mesh1, IndexedTriArray& mesh2);
90  static void remove_outside(IndexedTriArray& mesh, TessellatedPolyhedron& p, bool remove_shared);
91  static void remove_inside(IndexedTriArray& mesh, TessellatedPolyhedron& p, bool remove_shared);
92  static bool bisects(const Triangle& a, const Triangle& b);
93  static bool bisect(const Triangle& tbi, std::vector<Ravelin::Origin3d>& v, std::vector<IndexedTri>& f, unsigned i);
94  static unsigned add_vertex(std::vector<Ravelin::Origin3d>& vertices, const Ravelin::Origin3d& v);
95 
96  Ravelin::Origin3d _bb_min, _bb_max;
97  IndexedTriArray _mesh;
98  double _convexity;
99  bool _convexity_computed;
100 };
101 
102 std::ostream& operator<<(std::ostream& out, const TessellatedPolyhedron& p);
103 
104 // include inline functions
105 #include "TessellatedPolyhedron.inl"
106 
107 } // end namespace
108 
109 #endif
void to_polyhedron(Polyhedron &p) const
Creates a polyhedron from this tessellated polyhedron.
Definition: TessellatedPolyhedron.cpp:203
void transform(const Ravelin::Transform3d &T)
Transforms this polyhedron by the given transformation matrix.
Definition: TessellatedPolyhedron.cpp:728
const std::list< unsigned > & get_incident_facets(unsigned i) const
Gets the indices of facets incident to a vertex.
Definition: IndexedTriArray.h:56
const Ravelin::Origin3d & find_extreme_vertex(const Ravelin::Origin3d &direction)
Does an extremal point query for the Polyhedron; returns a vertex furthest along the given direction...
Definition: TessellatedPolyhedron.cpp:1423
bool inside(const Ravelin::Origin3d &point, double tol=NEAR_ZERO)
Determines whether the specified point is strictly inside this polyhedron.
Definition: TessellatedPolyhedron.cpp:574
bool is_convex()
Determines whether this polyhedron convex (to w/in floating point tolerance)
Definition: TessellatedPolyhedron.h:71
const std::list< unsigned > & get_incident_facets(unsigned i) const
Gets the list of facets coincident to the i'th facet.
Definition: TessellatedPolyhedron.h:59
LocationType location(const Ravelin::Origin3d &point, double tol=NEAR_ZERO) const
Determines the location of the specified point with respect to this polyhedron.
Definition: TessellatedPolyhedron.cpp:627
static TessellatedPolyhedronPtr minkowski(TessellatedPolyhedron &p1, boost::shared_ptr< const Ravelin::Pose3d > T1, TessellatedPolyhedron &p2, boost::shared_ptr< const Ravelin::Pose3d > T2, bool reflect_p2=true)
Computes the Minkowski sum of two convex polyhedra.
Definition: TessellatedPolyhedron.cpp:473
bool degenerate() const
Checks whether this polyhedron is degenerate.
Definition: TessellatedPolyhedron.cpp:503
static void to_vrml(std::ostream &out, const TessellatedPolyhedron &p, Ravelin::Origin3d diffuse_color=Ravelin::Origin3d(1, 1, 1), bool wireframe=false)
Sends this polyhedron to the specified stream using VRML.
Definition: TessellatedPolyhedron.cpp:827
A potentially-non-convex polyhedron of genus 0.
Definition: Polyhedron.h:28
An array of triangles indexed into shared vertices.
Definition: IndexedTriArray.h:25
boost::shared_ptr< TessellatedPolyhedron > TessellatedPolyhedronPtr
TessellatedPolyhedron smart pointer.
Definition: Types.h:56
double calc_signed_distance(const Ravelin::Origin3d &point)
Gets the signed distance from a point to the polyhedron.
Definition: TessellatedPolyhedron.h:65
std::pair< Ravelin::Origin3d, Ravelin::Origin3d > get_bounding_box_corners() const
Gets the corners of the axis-aligned bounding box of this polyhedron.
Definition: TessellatedPolyhedron.h:68
static IndexedTriArray construct_union(TessellatedPolyhedron &p1, TessellatedPolyhedron &p2)
Unions two non-convex polyhedra.
Definition: TessellatedPolyhedron.cpp:1357
const std::vector< IndexedTri > & get_facets() const
Gets the vector of facets.
Definition: IndexedTriArray.h:65
Represents a three-dimensional polyhedron.
Definition: TessellatedPolyhedron.h:28
double calc_signed_distance(const Ravelin::Origin3d &point, unsigned &closest_facet)
Gets the signed distance and closest facet to a point.
double calc_volume() const
Calculates the volume of this polyhedron.
Definition: TessellatedPolyhedron.cpp:871
bool consistent() const
Checks whether this polyhedron is consistent.
Definition: TessellatedPolyhedron.cpp:522
const std::vector< Ravelin::Origin3d > & get_vertices() const
Gets the vector of verties.
Definition: IndexedTriArray.h:68
void operator=(const TessellatedPolyhedron &p)
Copies a polyhedron.
Definition: TessellatedPolyhedron.cpp:193
static IndexedTriArray construct_difference(TessellatedPolyhedron &p1, TessellatedPolyhedron &p2)
Constructs the Boolean difference p1 - p2.
Definition: TessellatedPolyhedron.cpp:1374
static IndexedTriArray construct_intersection(TessellatedPolyhedron &p1, TessellatedPolyhedron &p2)
Intersects two non-convex polyhedra.
Definition: TessellatedPolyhedron.cpp:1333
bool inside_or_on(const Ravelin::Origin3d &point, double tol=NEAR_ZERO)
Determines whether the specified point is in or on this polyhedron.
Definition: TessellatedPolyhedron.cpp:598
double convexity()
Gets the convexity of this polyhedron.
Definition: TessellatedPolyhedron.h:80