Moby
IndexedTriArray.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 _INDEXED_TRIANGLE_ARRAY_H
8 #define _INDEXED_TRIANGLE_ARRAY_H
9 
10 #include <map>
11 #include <iostream>
12 #include <cmath>
13 #include <list>
14 #include <string>
15 #include <boost/foreach.hpp>
16 #include <Ravelin/sorted_pair>
17 #include <Moby/Types.h>
18 #include <Moby/Triangle.h>
19 #include <Moby/InvalidIndexException.h>
20 #include <Moby/IndexedTri.h>
21 
22 namespace Moby {
23 
26 {
27  public:
28  IndexedTriArray() {}
29  IndexedTriArray(boost::shared_ptr<const std::vector<Ravelin::Origin3d> > vertices, const std::vector<IndexedTri>& facets);
30  IndexedTriArray(boost::shared_ptr<const std::vector<Ravelin::Origin3d> > vertices, boost::shared_ptr<const std::vector<IndexedTri> > facets);
31 
32  template <class ForwardIterator1, class ForwardIterator2>
33  IndexedTriArray(ForwardIterator1 vertices, ForwardIterator1 verts_end, ForwardIterator2 facets_begin, ForwardIterator2 facets_end);
34 
35  template <class OutputIterator>
36  OutputIterator get_tris(OutputIterator output_begin, boost::shared_ptr<const Ravelin::Pose3d> P) const;
37 
38  template <class OutputIterator>
39  static OutputIterator intersect(const IndexedTriArray& mesh_a, const IndexedTriArray& mesh_b, OutputIterator output_begin, boost::shared_ptr<const Ravelin::Pose3d> Pa, boost::shared_ptr<const Ravelin::Pose3d> Pb, bool exit_early);
40 
41  unsigned num_tris() const { return (_facets) ? _facets->size() : 0; }
42  Triangle get_triangle(unsigned i, boost::shared_ptr<const Ravelin::Pose3d> P) const;
43  IndexedTriArray transform(const Ravelin::Transform3d& T) const;
45  static IndexedTriArray read_from_obj(const std::string& filename);
46  static void write_to_obj(const IndexedTriArray& mesh, const std::string& filename);
47  void write_to_obj(const std::string& filename) const { write_to_obj(*this, filename); }
48  static IndexedTriArray merge(const IndexedTriArray& mesh1, const IndexedTriArray& mesh2, double equal_tol = 0.0);
49  IndexedTriArray& operator=(const IndexedTriArray& mesh);
50  std::vector<std::list<unsigned> > determine_vertex_edge_map() const;
51  std::vector<std::list<unsigned> > determine_vertex_facet_map() const;
52  std::map<Ravelin::sorted_pair<unsigned>, std::list<unsigned> > determine_edge_facet_map() const;
53  void calc_volume_ints(double volume_ints[10]) const;
54 
56  const std::list<unsigned>& get_incident_facets(unsigned i) const { if (i >= _vertices->size()) throw InvalidIndexException(); return (*_incident_facets)[i]; }
57 
59  boost::shared_ptr<const std::vector<IndexedTri> > get_facets_pointer() const { return _facets; }
60 
62  boost::shared_ptr<const std::vector<Ravelin::Origin3d> > get_vertices_pointer() const { return _vertices; }
63 
65  const std::vector<IndexedTri>& get_facets() const { return *_facets; }
66 
68  const std::vector<Ravelin::Origin3d>& get_vertices() const { return *_vertices; }
69 
71  bool is_coplanar(unsigned vidx) const { return std::binary_search(_coplanar_verts.begin(), _coplanar_verts.end(), vidx); }
72 
74  bool is_coplanar(unsigned v1, unsigned v2) const { return std::binary_search(_coplanar_edges.begin(), _coplanar_edges.end(), Ravelin::make_sorted_pair(v1, v2)); }
75 
76  private:
77  void determine_coplanar_features();
78  static bool query_intersect_tri_tri(const Triangle& t1, const Triangle& t2);
79  void validate() const;
80  void calc_incident_facets();
81 
83  std::vector<Ravelin::sorted_pair<unsigned> > _coplanar_edges;
84 
86  std::vector<unsigned> _coplanar_verts;
87 
88  boost::shared_ptr<const std::vector<IndexedTri> > _facets;
89  boost::shared_ptr<const std::vector<Ravelin::Origin3d> > _vertices;
90  boost::shared_ptr<const std::vector<std::list<unsigned > > > _incident_facets;
91 }; // end class
92 
93 // include inline methods
94 #include "IndexedTriArray.inl"
95 
96 } // end namespace
97 
98 #endif
99 
std::vector< std::list< unsigned > > determine_vertex_edge_map() const
Determines a map from vertices to edges.
Definition: IndexedTriArray.cpp:180
std::map< Ravelin::sorted_pair< unsigned >, std::list< unsigned > > determine_edge_facet_map() const
Determines a map from edges to facet indices.
Definition: IndexedTriArray.cpp:198
const std::list< unsigned > & get_incident_facets(unsigned i) const
Gets the indices of facets incident to a vertex.
Definition: IndexedTriArray.h:56
static IndexedTriArray read_from_obj(const std::string &filename)
Reads triangle mesh from a Wavefront OBJ file.
Definition: IndexedTriArray.cpp:413
boost::shared_ptr< const std::vector< Ravelin::Origin3d > > get_vertices_pointer() const
Gets the pointer to the vector of vertices.
Definition: IndexedTriArray.h:62
IndexedTriArray(ForwardIterator1 vertices, ForwardIterator1 verts_end, ForwardIterator2 facets_begin, ForwardIterator2 facets_end)
Creates an indexed triangle mesh from containers of vertices and facets.
Definition: IndexedTriArray.h:31
void calc_volume_ints(double volume_ints[10]) const
Calculates the volume integrals of this primitive as a triangle mesh.
Definition: IndexedTriArray.cpp:62
Triangle get_triangle(unsigned i, boost::shared_ptr< const Ravelin::Pose3d > P) const
Gets the i'th triangle from this mesh.
Definition: IndexedTriArray.cpp:213
An array of triangles indexed into shared vertices.
Definition: IndexedTriArray.h:25
static IndexedTriArray merge(const IndexedTriArray &mesh1, const IndexedTriArray &mesh2, double equal_tol=0.0)
Merges two meshes together to create a new mesh.
Definition: IndexedTriArray.cpp:241
Exception thrown when trying to access the index beyond the range of the data.
Definition: InvalidIndexException.h:15
const std::vector< IndexedTri > & get_facets() const
Gets the vector of facets.
Definition: IndexedTriArray.h:65
bool is_coplanar(unsigned vidx) const
Determines whether a vertex is coplanar (all faces touching the vertex are coplanar) ...
Definition: IndexedTriArray.h:71
IndexedTriArray transform(const Ravelin::Transform3d &T) const
Transforms this mesh to a new mesh.
Definition: IndexedTriArray.cpp:302
std::vector< std::list< unsigned > > determine_vertex_facet_map() const
Determines a map from vertices to facet indices.
Definition: IndexedTriArray.cpp:160
boost::shared_ptr< const std::vector< IndexedTri > > get_facets_pointer() const
Gets the pointer to the vector of facets.
Definition: IndexedTriArray.h:59
const std::vector< Ravelin::Origin3d > & get_vertices() const
Gets the vector of verties.
Definition: IndexedTriArray.h:68
IndexedTriArray & operator=(const IndexedTriArray &mesh)
Copies one mesh to another.
Definition: IndexedTriArray.cpp:148
IndexedTriArray compress_vertices() const
Compresses the vertices used in an IndexedTriArray to create a new mesh.
Definition: IndexedTriArray.cpp:341
OutputIterator get_tris(OutputIterator output_begin, boost::shared_ptr< const Ravelin::Pose3d > P) const
Converts an indexed triangle mesh to a container of triangles.
Definition: IndexedTriArray.h:51
static OutputIterator intersect(const IndexedTriArray &mesh_a, const IndexedTriArray &mesh_b, OutputIterator output_begin, boost::shared_ptr< const Ravelin::Pose3d > Pa, boost::shared_ptr< const Ravelin::Pose3d > Pb, bool exit_early)
Intersects two meshes together and returns indices of intersecting triangles.
Definition: IndexedTriArray.h:76
bool is_coplanar(unsigned v1, unsigned v2) const
Determines whether an edge (v1,v2) is coplanar (all faces touching the edge are coplanar) ...
Definition: IndexedTriArray.h:74