Moby
TriangleMeshPrimitive.h
1 /****************************************************************************
2  * Copyright 2005 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 _TRIANGLE_MESH_PRIMITIVE_H
8 #define _TRIANGLE_MESH_PRIMITIVE_H
9 
10 #include <queue>
11 #include <fstream>
12 #include <iostream>
13 #include <cmath>
14 #include <list>
15 #include <string>
16 #include <Moby/Types.h>
17 #include <Moby/Primitive.h>
18 
19 namespace Moby {
20 
23 {
24  public:
26  TriangleMeshPrimitive(const std::string& filename, bool center = true);
27  TriangleMeshPrimitive(const std::string& filename, const Ravelin::Pose3d& T, bool center = true);
28  void set_edge_sample_length(double len);
29  virtual osg::Node* create_visualization();
30  virtual double get_bounding_radius() const { return 0.0; }
32  double get_edge_sample_length() const { return _edge_sample_length; }
33 
34  virtual double calc_signed_dist(const Point3d& p) const;
35  virtual void load_from_xml(boost::shared_ptr<const XMLTree> node, std::map<std::string, BasePtr>& id_map);
36  virtual void save_to_xml(XMLTreePtr node, std::list<boost::shared_ptr<const Base> >& shared_objects) const;
38  virtual void get_vertices(boost::shared_ptr<const Ravelin::Pose3d> P, std::vector<Point3d>& vertices) const;
39  virtual double calc_dist_and_normal(const Point3d& point, std::vector<Ravelin::Vector3d>& normals) const;
40  virtual boost::shared_ptr<const IndexedTriArray> get_mesh(boost::shared_ptr<const Ravelin::Pose3d> P) { return _mesh; }
41  void set_mesh(boost::shared_ptr<const IndexedTriArray> mesh);
42  virtual void set_pose(const Ravelin::Pose3d& T);
43  virtual double calc_signed_dist(boost::shared_ptr<const Primitive> p, Point3d& pthis, Point3d& pp) const;
44  virtual bool is_convex() const;
45 
46 
47  private:
48  void center();
49  virtual void calc_mass_properties();
50 
52  bool _convexify_inertia;
53 
55  std::map<CollisionGeometryPtr, BVPtr> _roots;
56 
58 
61  boost::shared_ptr<const IndexedTriArray> _mesh;
62 
64  double _edge_sample_length;
65 
67  class AThickTri : public ThickTriangle
68  {
69  public:
70  AThickTri(const Triangle& tri, double tol) : ThickTriangle(tri, tol) {}
71  boost::shared_ptr<const IndexedTriArray> mesh; // the mesh that this triangle came from
72  unsigned tri_idx; // the index of this triangle
73  };
74 
75  void construct_mesh_vertices(boost::shared_ptr<const IndexedTriArray> mesh, CollisionGeometryPtr geom);
76  void build_BB_tree(CollisionGeometryPtr geom);
77  void split_tris(const Point3d& point, const Ravelin::Vector3d& normal, const IndexedTriArray& orig_mesh, const std::list<unsigned>& ofacets, std::list<unsigned>& pfacets, std::list<unsigned>& nfacets);
78  bool split(boost::shared_ptr<const IndexedTriArray> mesh, BVPtr source, BVPtr& tgt1, BVPtr& tgt2, const Ravelin::Vector3d& axis);
79 
80  template <class InputIterator, class OutputIterator>
81  static OutputIterator get_vertices(const IndexedTriArray& tris, InputIterator fselect_begin, InputIterator fselect_end, OutputIterator output, boost::shared_ptr<const Ravelin::Pose3d> P);
82 
84  std::map<BVPtr, std::list<unsigned> > _mesh_tris;
85 
87  std::vector<Point3d> _vertices;
88 
90  std::map<BVPtr, std::list<unsigned> > _mesh_vertices;
91 
93  std::map<BVPtr, std::list<boost::shared_ptr<AThickTri> > > _tris;
94 
96  std::pair<boost::shared_ptr<const IndexedTriArray>, std::list<unsigned> > _smesh;
97 }; // end class
98 
99 #include "TriangleMeshPrimitive.inl"
100 
101 } // end namespace
102 
103 #endif
104 
virtual void save_to_xml(XMLTreePtr node, std::list< boost::shared_ptr< const Base > > &shared_objects) const
Implements Base::save_to_xml()
Definition: TriangleMeshPrimitive.cpp:253
boost::shared_ptr< BV > BVPtr
Bounding volume (BV) smart pointer.
Definition: Types.h:92
virtual boost::shared_ptr< const IndexedTriArray > get_mesh(boost::shared_ptr< const Ravelin::Pose3d > P)
Gets the underlying triangle mesh for this primitive.
Definition: TriangleMeshPrimitive.h:40
virtual bool is_convex() const
Returns whether the mesh is convex (currently mesh must be convex)
Definition: TriangleMeshPrimitive.cpp:411
TriangleMeshPrimitive()
Creates the triangle mesh primitive.
Definition: TriangleMeshPrimitive.cpp:48
virtual double calc_dist_and_normal(const Point3d &point, std::vector< Ravelin::Vector3d > &normals) const
Computes the distance and normal from a point on the mesh.
Definition: TriangleMeshPrimitive.cpp:455
Defines a triangle-mesh-based primitive type used for inertial property calculation and geometry prov...
Definition: Primitive.h:41
void set_edge_sample_length(double len)
Sets the edge sample length for this triangle mesh.
Definition: TriangleMeshPrimitive.cpp:101
virtual osg::Node * create_visualization()
Creates the visualization for this primitive.
Definition: TriangleMeshPrimitive.cpp:111
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
Ravelin::Vector3d Point3d
Typedef to distinguish between a 3D vector and a point.
Definition: Types.h:47
virtual void get_vertices(boost::shared_ptr< const Ravelin::Pose3d > P, std::vector< Point3d > &vertices) const
Get vertices corresponding to this primitive.
double get_edge_sample_length() const
Gets the length of an edge in the mesh above which point sub-samples are created. ...
Definition: TriangleMeshPrimitive.h:32
virtual BVPtr get_BVH_root(CollisionGeometryPtr geom)
Gets the pointer to the root bounding box.
Definition: TriangleMeshPrimitive.cpp:400
void set_mesh(boost::shared_ptr< const IndexedTriArray > mesh)
Sets the mesh.
Definition: TriangleMeshPrimitive.cpp:300
virtual void load_from_xml(boost::shared_ptr< const XMLTree > node, std::map< std::string, BasePtr > &id_map)
Implements Base::load_from_xml()
Definition: TriangleMeshPrimitive.cpp:193
virtual void set_pose(const Ravelin::Pose3d &T)
Transforms this primitive.
Definition: TriangleMeshPrimitive.cpp:742
Represents a triangle mesh "primitive" for inertia properties, collision detection, and visualization.
Definition: TriangleMeshPrimitive.h:22
virtual double calc_signed_dist(const Point3d &p) const
Computes the signed distance to a point from the mesh.
Definition: TriangleMeshPrimitive.cpp:417