1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
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.
17#ifndef OSGSHADOW_CONVEXPOLYHEDRON
18#define OSGSHADOW_CONVEXPOLYHEDRON 1
20#include <osg/Geometry>
21#include <osg/Polytope>
22#include <osgShadow/Export>
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////////////////////////////////////////////////////////////////////////////////
35class OSGSHADOW_EXPORT ConvexPolyhedron
38 typedef std::vector<osg::Vec3d> Vertices;
40 static const osg::Matrix & defaultMatrix;
49 typedef std::list<Face> Faces;
52 ConvexPolyhedron( void ) { }
54 ConvexPolyhedron( const osg::Matrix& matrix, const osg::Matrix& inverse,
55 const osg::BoundingBox& bb = osg::BoundingBox(-1,-1,-1,1,1,1));
57 Face& createFace() { _faces.push_back(Face()); return _faces.back(); }
58 void clear() { _faces.clear(); }
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);
68 ( const Face & face0, const Face & face1, Face & face );
70 void mergeCoplanarFaces( const double & plane_normal_dot_tolerance = 0.0,
71 const double & plane_distance_tolerance = 0.0 );
73 void removeDuplicateVertices( void );
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 );
81 static int isFacePolygonConvex( Face & face, bool ignoreCollinearVertices = true );
84 ( bool checkForNonConvexPolys = false, const char * errorPrefix = NULL );
87 void cut(const osg::Polytope& polytope);
89 void cut(const ConvexPolyhedron& polytope);
91 void cut(const osg::Plane& plane, const std::string& name=std::string());
93 void extrude( const osg::Vec3d & offset );
95 void translate( const osg::Vec3d & offset );
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;
102 osg::Geometry* buildGeometry( const osg::Vec4d& colorOutline,
103 const osg::Vec4d& colorInside,
104 osg::Geometry* useGeometry = NULL ) const;
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;
121} // namespace osgShadow