2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-04-17 14:52:56 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2003-04-17 14:52:56 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-04-17 14:52:56 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-04-17 14:52:56 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2003-04-17 03:21:27 +08:00
|
|
|
#ifndef OSGUTIL_DELAUNAYTRIANGULATOR_
|
|
|
|
#define OSGUTIL_DELAUNAYTRIANGULATOR_
|
|
|
|
|
2005-10-25 21:28:48 +08:00
|
|
|
#include <list>
|
|
|
|
|
2003-04-17 03:21:27 +08:00
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Array>
|
|
|
|
#include <osg/Referenced>
|
|
|
|
#include <osg/CopyOp>
|
|
|
|
#include <osg/PrimitiveSet>
|
2005-10-25 21:28:48 +08:00
|
|
|
#include <osg/Geometry>
|
2003-04-17 03:21:27 +08:00
|
|
|
|
2003-04-17 14:52:56 +08:00
|
|
|
#include <osgUtil/Export>
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
namespace osgUtil
|
2003-04-17 03:21:27 +08:00
|
|
|
{
|
|
|
|
|
2005-10-25 21:28:48 +08:00
|
|
|
/** DelaunayTriangulator: Utility class that triangulates an irregular network of sample points.
|
2003-04-17 14:52:56 +08:00
|
|
|
Just create a DelaunayTriangulator, assign it the sample point array and call
|
|
|
|
its triangulate() method to start the triangulation. Then you can obtain the
|
|
|
|
generated primitive by calling the getTriangles() method.
|
2005-10-25 21:28:48 +08:00
|
|
|
|
|
|
|
Add DelaunayConstraints (or derived class) to control the triangulation edges.
|
2003-04-17 14:52:56 +08:00
|
|
|
*/
|
2005-10-25 21:28:48 +08:00
|
|
|
class OSGUTIL_EXPORT DelaunayConstraint: public osg::Geometry {
|
|
|
|
// controls the edges in a Delaunay triangulation.
|
|
|
|
// constraints can be linear (with width), areal (contains an area)
|
|
|
|
// uses: to replace part of a terrain with an alternative textured model (roads, lakes).
|
|
|
|
// the primitive sets in this are either LINE_LOOP or LINE_STRIP
|
|
|
|
public:
|
|
|
|
DelaunayConstraint() { }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Each primitiveset is a list of vertices which may be closed by joining up to its start
|
2005-10-25 21:28:48 +08:00
|
|
|
* to make a loop. Constraints should be simple lines, not crossing themselves.
|
|
|
|
* Constraints which cross other constraints can cause difficulties - see the example
|
|
|
|
* for methods of dealing with them. */
|
|
|
|
|
|
|
|
/** collect up indices of triangle from delaunay triangles.
|
|
|
|
* The delaunay triangles inside the DelaunayConstraint area can be used to fill
|
|
|
|
* the area or generate geometry that terrain follows the area in some way.
|
|
|
|
* These triangles can form a canopy or a field. */
|
2007-04-04 16:05:23 +08:00
|
|
|
void addtriangle(int i1, int i2, int i3);
|
2005-10-25 21:28:48 +08:00
|
|
|
|
|
|
|
/** Get the filling primitive. One:
|
|
|
|
* triangulate must have bneen called and
|
2012-03-22 01:36:20 +08:00
|
|
|
* two: triangle list is filled when
|
2005-10-25 21:28:48 +08:00
|
|
|
* DelaunayTriangulator::removeInternalTriangles is called.
|
2012-03-22 01:36:20 +08:00
|
|
|
* These return the triangles removed from the delaunay triangulation by
|
2005-10-25 21:28:48 +08:00
|
|
|
* DelaunayTriangulator::removeInternalTriangles. */
|
2010-08-10 00:14:25 +08:00
|
|
|
inline const osg::DrawElementsUInt *getTriangles() const { return prim_tris_.get(); }
|
|
|
|
|
|
|
|
inline osg::DrawElementsUInt *getTriangles() { return prim_tris_.get(); }
|
2005-10-25 21:28:48 +08:00
|
|
|
|
|
|
|
/** Call BEFORE makeDrawable to reorder points to make optimised set
|
|
|
|
*/
|
|
|
|
osg::Vec3Array *getPoints(const osg::Vec3Array *points);
|
|
|
|
|
|
|
|
/** converts simple list of triangles into a drawarray.
|
|
|
|
*/
|
|
|
|
osg::DrawElementsUInt *makeDrawable();
|
|
|
|
|
|
|
|
/** Add vertices and constraint loops from dco
|
|
|
|
* Can be used to generate extra vertices where dco crosses 'this' using
|
2007-01-09 05:29:49 +08:00
|
|
|
* osgUtil::Tessellator to insert overlap vertices.
|
2005-10-25 21:28:48 +08:00
|
|
|
*/
|
|
|
|
void merge(DelaunayConstraint *dco);
|
|
|
|
|
|
|
|
/** remove from line the vertices that are inside dco
|
|
|
|
*/
|
|
|
|
void removeVerticesInside(const DelaunayConstraint *dco);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-25 21:28:48 +08:00
|
|
|
/** return winding number as a float of loop around testpoint; may use multiple loops
|
|
|
|
* does not reject points on the edge or very very close to the edge */
|
2007-04-04 16:05:23 +08:00
|
|
|
float windingNumber(const osg::Vec3 &testpoint) const ;
|
2005-10-25 21:28:48 +08:00
|
|
|
|
|
|
|
/** true if testpoint is internal (or external) to constraint. */
|
2007-04-04 16:05:23 +08:00
|
|
|
virtual bool contains(const osg::Vec3 &testpoint) const;
|
|
|
|
virtual bool outside(const osg::Vec3 &testpoint) const;
|
2005-10-25 21:28:48 +08:00
|
|
|
|
2007-01-09 05:29:49 +08:00
|
|
|
/** Tessellate the constraint loops so that the crossing points are interpolated
|
2005-10-25 21:28:48 +08:00
|
|
|
* and added to the contraints for the triangulation. */
|
2005-11-16 05:47:24 +08:00
|
|
|
void handleOverlaps(void);
|
2005-10-25 21:28:48 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~DelaunayConstraint() {}
|
|
|
|
|
|
|
|
typedef std::vector< int* > trilist; // array of indices in points array defining triangles
|
|
|
|
|
|
|
|
trilist _interiorTris; // list of triangles that fits the area.
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::DrawElementsUInt> prim_tris_; // returns a PrimitiveSet to draw the interior of this DC
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-04-17 14:52:56 +08:00
|
|
|
class OSGUTIL_EXPORT DelaunayTriangulator: public osg::Referenced {
|
|
|
|
public:
|
|
|
|
|
|
|
|
DelaunayTriangulator();
|
|
|
|
explicit DelaunayTriangulator(osg::Vec3Array *points, osg::Vec3Array *normals = 0);
|
|
|
|
DelaunayTriangulator(const DelaunayTriangulator ©, const osg::CopyOp ©op = osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
2005-10-25 21:28:48 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<DelaunayConstraint> > linelist;
|
|
|
|
|
2010-08-10 00:14:25 +08:00
|
|
|
/** Set the input point array. */
|
|
|
|
inline void setInputPointArray(osg::Vec3Array* points) { points_ = points; }
|
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Get the const input point array. */
|
2010-08-10 00:14:25 +08:00
|
|
|
inline const osg::Vec3Array* getInputPointArray() const { return points_.get(); }
|
2003-04-17 14:52:56 +08:00
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Get the input point array. */
|
2010-08-10 00:14:25 +08:00
|
|
|
inline osg::Vec3Array* getInputPointArray() { return points_.get(); }
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the output normal array (optional). */
|
|
|
|
inline void setOutputNormalArray(osg::Vec3Array* normals) { normals_ = normals; }
|
|
|
|
|
|
|
|
/** Get the const output normal array (optional). */
|
|
|
|
inline const osg::Vec3Array *getOutputNormalArray() const { return normals_.get(); }
|
|
|
|
|
|
|
|
/** Get the output normal array (optional). */
|
|
|
|
inline osg::Vec3Array *getOutputNormalArray() { return normals_.get(); }
|
2003-04-17 14:52:56 +08:00
|
|
|
|
|
|
|
|
2005-10-25 21:28:48 +08:00
|
|
|
/** Add an input constraint loop.
|
|
|
|
** the edges of the loop will constrain the triangulation.
|
|
|
|
** if remove!=0, the internal triangles of the constraint will be removed;
|
|
|
|
** the user may the replace the constraint line with an equivalent geometry.
|
|
|
|
** GWM July 2005 */
|
2010-08-10 00:14:25 +08:00
|
|
|
void addInputConstraint(DelaunayConstraint *dc) { constraint_lines.push_back(dc); }
|
2005-10-25 21:28:48 +08:00
|
|
|
|
2003-04-17 14:52:56 +08:00
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Start triangulation. */
|
2003-04-17 14:52:56 +08:00
|
|
|
bool triangulate();
|
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Get the generated primitive (call triangulate() first). */
|
2010-08-10 00:14:25 +08:00
|
|
|
inline const osg::DrawElementsUInt *getTriangles() const { return prim_tris_.get(); }
|
2003-04-17 14:52:56 +08:00
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Get the generated primitive (call triangulate() first). */
|
2010-08-10 00:14:25 +08:00
|
|
|
inline osg::DrawElementsUInt *getTriangles() { return prim_tris_.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-10-25 21:28:48 +08:00
|
|
|
/** remove the triangles internal to the constraint loops.
|
|
|
|
* (Line strips cannot remove any internal triangles). */
|
|
|
|
void removeInternalTriangles(DelaunayConstraint *constraint);
|
2003-04-17 14:52:56 +08:00
|
|
|
|
2010-08-10 00:14:25 +08:00
|
|
|
|
2003-04-17 14:52:56 +08:00
|
|
|
protected:
|
|
|
|
virtual ~DelaunayTriangulator();
|
|
|
|
DelaunayTriangulator &operator=(const DelaunayTriangulator &) { return *this; }
|
2007-04-04 16:05:23 +08:00
|
|
|
int getindex(const osg::Vec3 &pt,const osg::Vec3Array *points);
|
2003-04-17 14:52:56 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
osg::ref_ptr<osg::Vec3Array> points_;
|
|
|
|
osg::ref_ptr<osg::Vec3Array> normals_;
|
|
|
|
osg::ref_ptr<osg::DrawElementsUInt> prim_tris_;
|
2005-10-25 21:28:48 +08:00
|
|
|
|
|
|
|
// GWM these lines provide required edges in the triangulated shape.
|
|
|
|
linelist constraint_lines;
|
2006-02-09 07:41:32 +08:00
|
|
|
|
|
|
|
void _uniqueifyPoints();
|
2003-04-17 14:52:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// INLINE METHODS
|
|
|
|
|
2005-10-25 21:28:48 +08:00
|
|
|
|
|
|
|
|
2003-04-17 03:21:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2005-10-25 21:28:48 +08:00
|
|
|
|