i took the code above from philho and made the triangulation. just went through each region and saved a triangle pool.
app displays delaunay (d), voronoi (v) and trianglesoup (t)
Code:
import javax.media.opengl.*;
import processing.opengl.*;
import megamu.mesh.*;
import vitamin.math.*;
ArrayList meshPoints;
Delaunay delaunay;
Voronoi voro;
float startTime;
float time;
float resetTime;
float countTime;
class Triangle
{
Triangle()
{
a = new Vector3();
b = new Vector3();
c = new Vector3();
col = color( random(0, 255), random(0, 255), random(0, 255), 104 );
}
void draw()
{
fill( col ); //255, 0, 255, 104 );
stroke( 255, 255, 0, 4 );
ellipse( a.x, a.y, 5, 5 );
ellipse( b.x, b.y, 5, 5 );
ellipse( c.x, c.y, 5, 5 );
beginShape( TRIANGLES );
vertex( a.x, a.y, a.z );
vertex( b.x, b.y, b.z );
vertex( c.x, c.y, c.z );
endShape();
}
int col;
Vector3 a, b, c;
}
ArrayList triList;
void setup()
{
size( 800, 600, OPENGL );
meshPoints = new ArrayList();
triList = new ArrayList();
startTime = millis() * 0.001;
time = startTime;
countTime = startTime;
resetTime = startTime;
}
void draw()
{
time = (millis()*0.001) - startTime;
background( 13, 13, 13 );
if (delaunay != null)
{
DisplayDelaunay();
}
if (voro != null)
{
DisplayVoronoi();
}
for (int i = 0; i < meshPoints.size(); i++)
{
((MeshPoint) meshPoints.get(i)).Display();
}
if( triList != null && keyPressed && key == 't' )
{
for (int i = 0; i < triList.size(); i++)
{
((Triangle) triList.get(i)).draw();
}
}
countTime = time - resetTime;
if( countTime > 0.3 )
{
resetTime = millis()*0.001 - startTime;
float sphereRadius = random( 0, 200);
float angle = random( -360, 360 );
float x = width*0.5 + cos( angle ) * sphereRadius;
float y = height*0.5 + sin( angle ) * sphereRadius;
meshPoints.add( new MeshPoint( x, y ) ); //random(0,width), random(0,height) ) );
ComputeVoronoi();
}
}
void mousePressed()
{
MeshPoint pt = new MeshPoint(mouseX, mouseY);
meshPoints.add(pt);
}
class MeshPoint
{
float x, y;
float linkCount;
// Radius
int r = 5;
// Point color
color c = #FF0000;
MeshPoint(float px, float py)
{
x = px;
y = py;
}
float GetX() { return x; }
float GetY() { return y; }
void Display()
{
fill(c);
noStroke();
ellipse(x, y, r, r);
}
}
void keyPressed()
{
// Undo
if (key == 'u')
{
if( meshPoints.size() > 0 )
meshPoints.remove(meshPoints.size() - 1);
}
else if (key == 'd')
{
ComputeDelaunay();
}
else if (key == 'v' )
{
ComputeVoronoi();
}
}
void ComputeDelaunay()
{
float[][] points = new float[meshPoints.size()][2];
println("Computing Delaunay for " + meshPoints.size() + " points");
for (int i = 0; i < meshPoints.size(); i++)
{
MeshPoint mp = (MeshPoint) meshPoints.get(i);
points[i][0] = mp.GetX();
points[i][1] = mp.GetY();
}
delaunay = new Delaunay(points);
}
void DisplayDelaunay()
{
float[][] edges = delaunay.getEdges();
// println("Displaying Delaunay for " + edges.length + " edges");
stroke(#000088);
for (int i = 0; i < edges.length; i++)
{
float startX = edges[i][0];
float startY = edges[i][1];
float endX = edges[i][2];
float endY = edges[i][3];
// println(startX + " " + startY + " " + endX + " " + endY);
line(startX, startY, endX, endY);
}
}
void ComputeVoronoi()
{
float[][] points = new float[meshPoints.size()][2];
println("Computing Voronoi for " + meshPoints.size() + " points");
for (int i = 0; i < meshPoints.size(); i++)
{
MeshPoint mp = (MeshPoint) meshPoints.get(i);
points[i][0] = mp.GetX();
points[i][1] = mp.GetY();
}
voro = new Voronoi( points );
triList.clear();
MPolygon[] myRegions = voro.getRegions();
for(int i=0; i<myRegions.length; i++)
{
MPolygon region = myRegions[i];
// an array of points
float[][] coords = region.getCoords();
//
// Triangulate this region
//
// Point zero for the fan
Vector3 p0 = new Vector3( coords[0][0], coords[0][1], 0 );
int col = color( random(0, 255), random(0, 255), random(0, 255), 104 );
println( "region verts: " + region.count() );
for( int ti=0; ti<region.count()-2; ti++ )
{
Triangle tri = new Triangle();
tri.col = col;
tri.a.set( p0 );
tri.b.set( coords[ti+1][0], coords[ti+1][1], 0 );
tri.c.set( coords[ti+2][0], coords[ti+2][1], 0 );
triList.add( tri );
}
}
println( "number of tris: " + triList.size() );
}
void DisplayVoronoi()
{
float[][] edges = voro.getEdges();
// println("Displaying Delaunay for " + edges.length + " edges");
stroke(#000088);
for (int i = 0; i < edges.length; i++)
{
float startX = edges[i][0];
float startY = edges[i][1];
float endX = edges[i][2];
float endY = edges[i][3];
// println(startX + " " + startY + " " + endX + " " + endY);
line(startX, startY, endX, endY);
}
}