00001 00005 /* Copyright © 2009, 2010 James Legg. 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 */ 00011 #ifndef LIBTRACK_MESHFACES_H_ 00012 #define LIBTRACK_MESHFACES_H_ 00013 00014 #include <vector> 00015 #include <set> 00016 #include <iostream> 00017 00018 #include <LinearMath/btVector3.h> 00019 #include <LinearMath/btMatrix3x3.h> 00020 #include <BulletCollision/CollisionShapes/btTriangleMesh.h> 00021 00022 /* gcc >= 4.3 Depreciates hash_set & hash_map, causing warnings to be emited when 00023 * including boost graph library headers. 00024 * We don't need the hash-based storage selectors anyway, so turn them off. 00025 */ 00026 #define BOOST_NO_HASH 00027 #include <boost/graph/adjacency_list.hpp> 00028 #undef BOOST_NO_HASH 00029 00030 #include "../AxisAlignedBoundingBox.h" 00031 #include "../Geometry.h" 00032 00033 namespace Track 00034 { 00035 00044 class MeshFaces 00045 : virtual public AABBBounded 00046 { 00047 public: 00051 MeshFaces(std::istream & source); 00052 00056 MeshFaces(std::string & filename); 00057 00060 MeshFaces(); 00061 virtual ~MeshFaces(); 00062 00064 struct FaceVertex 00065 { 00067 FaceVertex(); 00071 FaceVertex(std::size_t vertex_index); 00073 std::size_t vertex_index; 00075 btVector3 normal; 00077 float texture_coord_u; 00078 float texture_coord_v; 00079 }; 00080 00082 struct Face 00083 { 00085 Face(); 00087 Face(std::size_t v1, std::size_t v2, std::size_t v3); 00089 Face (const FaceVertex & fv1, const FaceVertex & fv2, 00090 const FaceVertex & fv3); 00092 FaceVertex fv1; 00094 FaceVertex fv2; 00096 FaceVertex fv3; 00097 00099 bool is_edge; 00101 unsigned long int object_name; 00102 }; 00103 00104 // extra information about a face useful in the graph 00105 struct FaceGraph : public Face 00106 { 00107 FaceGraph() {}; 00108 FaceGraph(const Face & face); 00110 btVector3 face_centre; 00112 btVector3 face_normal; 00113 btVector3 vertex_positions[3]; 00115 btVector3 edges[2]; 00116 00118 btMatrix3x3 world_to_face; 00120 btMatrix3x3 face_to_world; 00121 00123 Plane edge_planes[3]; 00125 Line edge_line_segments[3]; 00127 Plane face_plane; 00128 00130 void set_conversions(); 00131 00133 btVector3 to_face_coords(btVector3 world_coords) const; 00135 btVector3 to_world_coords(btVector3 face_coords) const; 00137 btVector3 find_nearest_point(btVector3 position) const; 00138 }; 00139 00141 struct Edge 00142 { 00143 Edge() {}; 00145 Edge(std::size_t v1, std::size_t v2); 00147 std::size_t v1; 00149 std::size_t v2; 00157 bool operator< (Edge o) const; 00158 }; 00159 00161 struct EdgeGraph : public Edge 00162 { 00163 EdgeGraph() 00164 {}; 00165 00166 EdgeGraph(std::size_t v1, std::size_t v2) 00167 : Edge(v1, v2) 00168 {}; 00169 00170 btScalar length; 00171 btVector3 v2_pos; 00172 btVector3 v1_pos; 00173 }; 00174 00176 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 00177 FaceGraph, EdgeGraph> Graph; 00178 00180 struct FaceEdgePointers 00181 { 00182 FaceEdgePointers() {}; 00183 FaceEdgePointers(Face * face, FaceVertex * source, 00184 FaceVertex * target, btScalar length) 00185 : face(face) 00186 , source(source) 00187 , target(target) 00188 , length(length) 00189 {}; 00190 Face * face; 00191 FaceVertex * source; 00192 FaceVertex * target; 00193 btScalar length; 00194 }; 00195 00197 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 00198 btVector3, FaceEdgePointers> VertexGraph; 00199 00200 public: 00204 btVector3 get_vertex_pos(std::size_t index) const; 00205 00207 void add_vertex(const btVector3 & position); 00208 00210 void add_normal(const btVector3 & normal); 00211 00213 void add_texture_coord(const btVector3 & normal); 00214 00216 std::size_t get_num_vertices() const; 00217 00219 const Face & get_face(std::size_t index) const; 00220 00222 std::vector<Edge> get_edges_for_face(std::size_t face) const; 00223 00226 void add_face(std::size_t v1, std::size_t v2, std::size_t v3); 00227 00230 void add_face(std::size_t v1, std::size_t n1, std::size_t v2, std::size_t n2, std::size_t v3, std::size_t n3); 00231 00234 void add_face(Face face); 00235 00237 std::size_t get_number_of_faces() const; 00238 00253 Graph get_connectivity() const; 00254 00259 VertexGraph get_vertex_graph(); 00260 00267 void copy_faces(const MeshFaces & other); 00268 00270 void add_faces(btTriangleMesh & shape) const; 00271 00273 virtual AxisAlignedBoundingBox get_bounds() const; 00274 00281 void merge_doubles(btScalar distance2 = 1.0 / 32.0); 00282 00286 void operator|=(const MeshFaces & source); 00287 00293 void set_source(bool is_edge, unsigned long int object_name); 00294 protected: 00295 void load_from_stream(std::istream & source); 00296 00297 void add_edge(std::size_t v1, std::size_t v2, std::size_t face_index, 00298 std::map<EdgeGraph, std::size_t> & edge_to_face, 00299 std::vector<std::pair<std::size_t, std::size_t> > & graph_edges, 00300 std::vector<EdgeGraph> & edge_properties) const; 00301 00304 std::vector<btVector3> vertices_position; 00305 00307 std::vector<Face> faces; 00308 00309 AxisAlignedBoundingBox bounds; 00310 }; 00311 00312 } 00313 00314 #endif /*LIBTRACK_MESHFACES_H_*/
Generated at Mon Sep 6 00:41:11 2010 by Doxygen version 1.4.7 for Racer version svn335.