00001
00002
00003
00004
00005
00006
00007 #ifndef _POLYHEDRON_H
00008 #define _POLYHEDRON_H
00009
00010 #include <ostream>
00011 #include <vector>
00012 #include <list>
00013 #include <map>
00014 #include <Physsim/Matrix4.h>
00015
00016 namespace Physsim {
00017
00018 class Triangle;
00019
00021
00025 class Polyhedron
00026 {
00027 public:
00028 enum LocationType { Inside, Outside, OnVertex, OnEdge, OnFace };
00029
00030 Polyhedron() {}
00031 Polyhedron(const std::vector<TriangleConstPtr>& mesh);
00032 Polyhedron(const Polyhedron& p) { operator=(p); }
00033 void operator=(const Polyhedron& p);
00034 void construct_from_mesh(const std::vector<TriangleConstPtr >& mesh);
00035 const std::list<TriangleConstPtr>& get_incident_facets(Vector3ConstPtr v) const;
00036 void transform(const Matrix4& T);
00037 const std::vector<Vector3ConstPtr>& get_vertices() const { return _vertices; }
00038 const std::vector<TriangleConstPtr>& get_facets() const { return _facets; }
00039 bool inside(const Vector3& point) const;
00040 bool inside_or_on(const Vector3& point) const;
00041 LocationType location(const Vector3& point) const;
00042 bool contains_facet(TriangleConstPtr t) const;
00043 bool contains_vertex(boost::shared_ptr<Vector3> v) const;
00044 std::list<PolyhedronPtr > convexify(unsigned max_pieces = std::numeric_limits<unsigned>::max(), Real max_volume = std::numeric_limits<Real>::max()) const;
00045 static void to_VRML(std::ostream& out, const Polyhedron& p, Vector3 diffuse_color = Vector3(1,1,1), bool wireframe = false);
00046 Real calc_volume() const;
00047
00049 std::pair<Vector3, Vector3> get_bounding_box_corners() const { return std::make_pair(_bb_min, _bb_max); }
00050
00052 bool is_convex() const { return _convex; }
00053
00054 private:
00055 bool consistent() const;
00056 static void calc_subexpressions(Real w0, Real w1, Real w2, Real& f1, Real& f2, Real& f3, Real& g0, Real& g1, Real& g2);
00057 bool det_convexity() const;
00058 static boost::shared_ptr<Vector3> intersect_plane(const Vector3& normal, Real d, const Vector3& p1, const Vector3& p2);
00059 bool split(Polyhedron& t1, Polyhedron& t2, const Vector3& normal, Real plane_d, Real epsilon) const;
00060
00061 bool _convex;
00062 Vector3 _bb_min, _bb_max;
00063 std::vector<TriangleConstPtr> _facets;
00064 std::vector<Vector3ConstPtr> _vertices;
00065 std::map<Vector3ConstPtr, std::list<TriangleConstPtr> > _incident_facets;
00066 };
00067
00068 std::ostream& operator<<(std::ostream& out, const Polyhedron& p);
00069
00070 }
00071
00072 #endif