| 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 | |
|---|
| 17 | #ifndef OSGSHADOW_CONVEXPOLYHEDRON |
|---|
| 18 | #define OSGSHADOW_CONVEXPOLYHEDRON 1 |
|---|
| 19 | |
|---|
| 20 | #include <osg/Geometry> |
|---|
| 21 | #include <osg/Polytope> |
|---|
| 22 | #include <osgShadow/Export> |
|---|
| 23 | |
|---|
| 24 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 25 | // Class based on CustomPolytope defined and used in osgSim::OverlayNode.cpp. |
|---|
| 26 | // Honors should go to Robert Osfield for writing such useful piece of code. |
|---|
| 27 | // First incarnations of my ConvexPolyhedron were derived from CustomPolytope. |
|---|
| 28 | // Later I made a number of modifications aimed at improving convex hull |
|---|
| 29 | // precision of intersection & extrusion operations and ended up with code |
|---|
| 30 | // so mixed that I decided to rewrite it as separate class. |
|---|
| 31 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 32 | |
|---|
| 33 | namespace osgShadow { |
|---|
| 34 | |
|---|
| 35 | class OSGSHADOW_EXPORT ConvexPolyhedron |
|---|
| 36 | { |
|---|
| 37 | public: |
|---|
| 38 | typedef std::vector<osg::Vec3d> Vertices; |
|---|
| 39 | |
|---|
| 40 | static const osg::Matrix & defaultMatrix; |
|---|
| 41 | |
|---|
| 42 | struct Face |
|---|
| 43 | { |
|---|
| 44 | std::string name; |
|---|
| 45 | osg::Plane plane; |
|---|
| 46 | Vertices vertices; |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | typedef std::list<Face> Faces; |
|---|
| 50 | Faces _faces; |
|---|
| 51 | |
|---|
| 52 | ConvexPolyhedron( void ) { } |
|---|
| 53 | |
|---|
| 54 | ConvexPolyhedron( const osg::Matrix& matrix, const osg::Matrix& inverse, |
|---|
| 55 | const osg::BoundingBox& bb = osg::BoundingBox(-1,-1,-1,1,1,1)); |
|---|
| 56 | |
|---|
| 57 | Face& createFace() { _faces.push_back(Face()); return _faces.back(); } |
|---|
| 58 | void clear() { _faces.clear(); } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | void setToUnitFrustum(bool withNear=true, bool withFar=true); |
|---|
| 62 | void setToBoundingBox(const osg::BoundingBox& bb); |
|---|
| 63 | void transform(const osg::Matrix& matrix, const osg::Matrix& inverse); |
|---|
| 64 | void transformClip(const osg::Matrix& matrix, const osg::Matrix& inverse); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | bool mergeFaces |
|---|
| 68 | ( const Face & face0, const Face & face1, Face & face ); |
|---|
| 69 | |
|---|
| 70 | void mergeCoplanarFaces( const double & plane_normal_dot_tolerance = 0.0, |
|---|
| 71 | const double & plane_distance_tolerance = 0.0 ); |
|---|
| 72 | |
|---|
| 73 | void removeDuplicateVertices( void ); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | static int pointsColinear |
|---|
| 77 | ( const osg::Vec3d & va, const osg::Vec3d & vb, const osg::Vec3d & vc, |
|---|
| 78 | const double & edge_normal_dot_tolerance = 0.0, |
|---|
| 79 | const double & null_edge_length_tolerance = 0.0 ); |
|---|
| 80 | |
|---|
| 81 | static int isFacePolygonConvex( Face & face, bool ignoreCollinearVertices = true ); |
|---|
| 82 | |
|---|
| 83 | bool checkCoherency |
|---|
| 84 | ( bool checkForNonConvexPolys = false, const char * errorPrefix = NULL ); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | void cut(const osg::Polytope& polytope); |
|---|
| 88 | |
|---|
| 89 | void cut(const ConvexPolyhedron& polytope); |
|---|
| 90 | |
|---|
| 91 | void cut(const osg::Plane& plane, const std::string& name=std::string()); |
|---|
| 92 | |
|---|
| 93 | void extrude( const osg::Vec3d & offset ); |
|---|
| 94 | |
|---|
| 95 | void translate( const osg::Vec3d & offset ); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | void getPolytope(osg::Polytope& polytope) const; |
|---|
| 99 | void getPoints(Vertices& vertices) const; |
|---|
| 100 | osg::BoundingBox computeBoundingBox( const osg::Matrix & m = osgShadow::ConvexPolyhedron::defaultMatrix ) const; |
|---|
| 101 | |
|---|
| 102 | osg::Geometry* buildGeometry( const osg::Vec4d& colorOutline, |
|---|
| 103 | const osg::Vec4d& colorInside, |
|---|
| 104 | osg::Geometry* useGeometry = NULL ) const; |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | bool dumpGeometry( const Face * face = NULL, |
|---|
| 108 | const osg::Plane * plane = NULL, |
|---|
| 109 | ConvexPolyhedron * basehull = NULL, |
|---|
| 110 | const char * filename = "convexpolyhedron.osg", |
|---|
| 111 | const osg::Vec4d& colorOutline = osg::Vec4( 0,1,0,0.5 ), |
|---|
| 112 | const osg::Vec4d& colorInside = osg::Vec4( 0,1,0,0.25 ), |
|---|
| 113 | const osg::Vec4d& faceColorOutline = osg::Vec4( 0,0,1,0.5 ), |
|---|
| 114 | const osg::Vec4d& faceColorInside = osg::Vec4( 0,0,1,0.25 ), |
|---|
| 115 | const osg::Vec4d& planeColorOutline = osg::Vec4( 1,0,0,0.5 ), |
|---|
| 116 | const osg::Vec4d& planeColorInside = osg::Vec4( 1,0,0,0.25 ), |
|---|
| 117 | const osg::Vec4d& baseColorOutline = osg::Vec4( 0,0,0,0.5 ), |
|---|
| 118 | const osg::Vec4d& baseColorInside = osg::Vec4( 0,0,0,0.25 ) ) const; |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | } // namespace osgShadow |
|---|
| 122 | |
|---|
| 123 | #endif |
|---|