ConvexPolyhedron 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. *
  13. * ViewDependentShadow codes Copyright (C) 2008 Wojciech Lewandowski
  14. * Thanks to to my company http://www.ai.com.pl for allowing me free this work.
  15. */
  16. #ifndef OSGSHADOW_CONVEXPOLYHEDRON
  17. #define OSGSHADOW_CONVEXPOLYHEDRON 1
  18. #include <osg/Geometry>
  19. #include <osg/Polytope>
  20. #include <osgShadow/Export>
  21. ////////////////////////////////////////////////////////////////////////////////
  22. // Class based on CustomPolytope defined and used in osgSim::OverlayNode.cpp.
  23. // Honors should go to Robert Osfield for writing such useful piece of code.
  24. // First incarnations of my ConvexPolyhedron were derived from CustomPolytope.
  25. // Later I made a number of modifications aimed at improving convex hull
  26. // precision of intersection & extrusion operations and ended up with code
  27. // so mixed that I decided to rewrite it as separate class.
  28. ////////////////////////////////////////////////////////////////////////////////
  29. namespace osgShadow {
  30. class OSGSHADOW_EXPORT ConvexPolyhedron
  31. {
  32. public:
  33. typedef std::vector<osg::Vec3d> Vertices;
  34. static const osg::Matrix & defaultMatrix;
  35. struct Face
  36. {
  37. std::string name;
  38. osg::Plane plane;
  39. Vertices vertices;
  40. };
  41. typedef std::list<Face> Faces;
  42. Faces _faces;
  43. ConvexPolyhedron( void ) { }
  44. ConvexPolyhedron( const osg::Matrix& matrix, const osg::Matrix& inverse,
  45. const osg::BoundingBox& bb = osg::BoundingBox(-1,-1,-1,1,1,1));
  46. Face& createFace() { _faces.push_back(Face()); return _faces.back(); }
  47. void clear() { _faces.clear(); }
  48. void setToUnitFrustum(bool withNear=true, bool withFar=true);
  49. void setToBoundingBox(const osg::BoundingBox& bb);
  50. void transform(const osg::Matrix& matrix, const osg::Matrix& inverse);
  51. void transformClip(const osg::Matrix& matrix, const osg::Matrix& inverse);
  52. bool mergeFaces
  53. ( const Face & face0, const Face & face1, Face & face );
  54. void mergeCoplanarFaces( const double & plane_normal_dot_tolerance = 0.0,
  55. const double & plane_distance_tolerance = 0.0 );
  56. void removeDuplicateVertices( void );
  57. static int pointsColinear
  58. ( const osg::Vec3d & va, const osg::Vec3d & vb, const osg::Vec3d & vc,
  59. const double & edge_normal_dot_tolerance = 0.0,
  60. const double & null_edge_length_tolerance = 0.0 );
  61. static int isFacePolygonConvex( Face & face, bool ignoreCollinearVertices = true );
  62. bool checkCoherency
  63. ( bool checkForNonConvexPolys = false, const char * errorPrefix = NULL );
  64. void cut(const osg::Polytope& polytope);
  65. void cut(const ConvexPolyhedron& polytope);
  66. void cut(const osg::Plane& plane, const std::string& name=std::string());
  67. void extrude( const osg::Vec3d & offset );
  68. void translate( const osg::Vec3d & offset );
  69. void getPolytope(osg::Polytope& polytope) const;
  70. void getPoints(Vertices& vertices) const;
  71. osg::BoundingBox computeBoundingBox( const osg::Matrix & m = osgShadow::ConvexPolyhedron::defaultMatrix ) const;
  72. osg::Geometry* buildGeometry( const osg::Vec4d& colorOutline,
  73. const osg::Vec4d& colorInside,
  74. osg::Geometry* useGeometry = NULL ) const;
  75. bool dumpGeometry( const Face * face = NULL,
  76. const osg::Plane * plane = NULL,
  77. ConvexPolyhedron * basehull = NULL,
  78. const char * filename = "convexpolyhedron.osg",
  79. const osg::Vec4d& colorOutline = osg::Vec4( 0,1,0,0.5 ),
  80. const osg::Vec4d& colorInside = osg::Vec4( 0,1,0,0.25 ),
  81. const osg::Vec4d& faceColorOutline = osg::Vec4( 0,0,1,0.5 ),
  82. const osg::Vec4d& faceColorInside = osg::Vec4( 0,0,1,0.25 ),
  83. const osg::Vec4d& planeColorOutline = osg::Vec4( 1,0,0,0.5 ),
  84. const osg::Vec4d& planeColorInside = osg::Vec4( 1,0,0,0.25 ),
  85. const osg::Vec4d& baseColorOutline = osg::Vec4( 0,0,0,0.5 ),
  86. const osg::Vec4d& baseColorInside = osg::Vec4( 0,0,0,0.25 ) ) const;
  87. };
  88. } // namespace osgShadow
  89. #endif