CUGL
Cornell University Game Library
Public Member Functions | List of all members
cugl::SimpleTriangulator Class Reference

#include <CUSimpleTriangulator.h>

Public Member Functions

 SimpleTriangulator ()
 
 SimpleTriangulator (const std::vector< Vec2 > &points)
 
 SimpleTriangulator (const Poly2 &poly)
 
 ~SimpleTriangulator ()
 
void set (const Poly2 &poly)
 
void set (const std::vector< Vec2 > &points)
 
void reset ()
 
void clear ()
 
void calculate ()
 
std::vector< unsigned short > getTriangulation ()
 
size_t getTriangulation (std::vector< unsigned short > &buffer)
 
Poly2 getPolygon ()
 
Poly2getPolygon (Poly2 *buffer)
 

Detailed Description

This class is a factory for producing solid Poly2 objects from a set of vertices.

For all but the simplist of shapes, it is important to have a triangulator that can divide up the polygon into triangles for drawing. This is a simple implementation of the the ear cutting algorithm to triangulate simple polygons. It will not handle polygons with holes or with self intersections.

As with all factories, the methods are broken up into three phases: initialization, calculation, and materialization. To use the factory, you first set the data (in this case a set of vertices or another Poly2) with the initialization methods. You then call the calculation method. Finally, you use the materialization methods to access the data in several different ways.

This division allows us to support multithreaded calculation if the data generation takes too long. However, note that this factory is not thread safe in that you cannot access data while it is still in mid-calculation.

Constructor & Destructor Documentation

cugl::SimpleTriangulator::SimpleTriangulator ( )
inline

Creates a triangulator with no vertex data.

cugl::SimpleTriangulator::SimpleTriangulator ( const std::vector< Vec2 > &  points)
inline

Creates a triangulator with the given vertex data.

The vertex data is copied. The triangulator does not retain any references to the original data.

Parameters
pointsThe vertices to triangulate
cugl::SimpleTriangulator::SimpleTriangulator ( const Poly2 poly)
inline

Creates a triangulator with the given vertex data.

The triangulator only uses the vertex data from the polygon. It ignores any existing indices.

The vertex data is copied. The triangulator does not retain any references to the original data.

Parameters
polyThe vertices to triangulate
cugl::SimpleTriangulator::~SimpleTriangulator ( )
inline

Deletes this triangulator, releasing all resources.

Member Function Documentation

void cugl::SimpleTriangulator::calculate ( )

Performs a triangulation of the current vertex data.

void cugl::SimpleTriangulator::clear ( )
inline

Clears all internal data, the initial vertex data.

When this method is called, you will need to set a new vertices before calling calculate.

Poly2 cugl::SimpleTriangulator::getPolygon ( )

Returns a polygon representing the triangulation.

The polygon contains the original vertices together with the new indices defining a solid shape. The triangulator does not maintain references to this polygon and it is safe to modify it.

If the calculation is not yet performed, this method will return the empty polygon.

Returns
a polygon representing the triangulation.
Poly2* cugl::SimpleTriangulator::getPolygon ( Poly2 buffer)

Stores the triangulation in the given buffer.

This method will add both the original vertices, and the corresponding indices to the new buffer. If the buffer is not empty, the indices will be adjusted accordingly. You should clear the buffer first if you do not want to preserve the original data.

If the calculation is not yet performed, this method will do nothing.

Parameters
bufferThe buffer to store the triangulated polygon
Returns
a reference to the buffer for chaining.
std::vector<unsigned short> cugl::SimpleTriangulator::getTriangulation ( )

Returns a list of indices representing the triangulation.

The indices represent positions in the original vertex list. If you have modified that list, these indices may no longer be valid.

The triangulator does not retain a reference to the returned list; it is safe to modify it.

If the calculation is not yet performed, this method will return the empty list.

Returns
a list of indices representing the triangulation.
size_t cugl::SimpleTriangulator::getTriangulation ( std::vector< unsigned short > &  buffer)

Stores the triangulation indices in the given buffer.

The indices represent positions in the original vertex list. If you have modified that list, these indices may no longer be valid.

The indices will be appended to the provided vector. You should clear the vector first if you do not want to preserve the original data.

If the calculation is not yet performed, this method will do nothing.

Returns
the number of elements added to the buffer
void cugl::SimpleTriangulator::reset ( )
inline

Clears all internal data, but still maintains the initial vertex data.

void cugl::SimpleTriangulator::set ( const Poly2 poly)
inline

Sets the vertex data for this triangulator..

The triangulator only uses the vertex data from the polygon. It ignores any existing indices.

The vertex data is copied. The triangulator does not retain any references to the original data.

This method resets all interal data. You will need to reperform the calculation before accessing data.

Parameters
polyThe vertices to triangulate
void cugl::SimpleTriangulator::set ( const std::vector< Vec2 > &  points)
inline

Sets the vertex data for this triangulator..

The vertex data is copied. The triangulator does not retain any references to the original data.

This method resets all interal data. You will need to reperform the calculation before accessing data.

Parameters
pointsThe vertices to triangulate

The documentation for this class was generated from the following file: