We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › all triangle of delauny diagram
Page Index Toggle Pages: 1
all triangle of delauny diagram (Read 1644 times)
all triangle of delauny diagram
Jan 30th, 2009, 1:05pm
 
Is there a way to get all triangles of a Delaunay Diagram created with Lee Byrons Mesh library?
Re: all triangle of delauny diagram
Reply #1 - Jan 31st, 2009, 5:45pm
 
That's a good, interesting question.
Interesting because I recently started to look at this library, as it might allow to do cool stuff.
Good, because if we want to colorize the triangles, we must know them.

I first made a little simple sketch to test the library:
Code:
import megamu.mesh.Voronoi;
import megamu.mesh.Delaunay;
import megamu.mesh.Hull;

ArrayList meshPoints;
Delaunay delaunay;

void setup()
{
size(800, 500);

meshPoints = new ArrayList();
}

void draw()
{
background(#CCFFEE);

if (delaunay != null)
{
DisplayDelaunay();
}
for (int i = 0; i < meshPoints.size(); i++)
{
((MeshPoint) meshPoints.get(i)).Display();
}
}

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')
{
meshPoints.remove(meshPoints.size() - 1);
}
else if (key == 'd')
{
ComputeDelaunay();
}
}

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);
}
}

I removed preliminary code to handle triangles.

Now, it seems hard to get the triangles out of the edges or links. Unless I miss something obvious... I imagined some algorithms, but nothing complete yet.

But instead of brute force, I though we should go to the source, and use, like the Mesh library, directly the quickhull3D library.
I found it can provide faces, I hope these are the triangles we need.

I will experiment and show the result here.
Re: all triangle of delauny diagram
Reply #2 - Feb 1st, 2009, 8:54am
 
Yeah I've been looking for something similar. But there is the old triangulate library.

http://www.processing.org/hacks/hacks:triangulation

It gives triangles. merging these two libraries wouldn't be a bad idea.
Re: all triangle of delauny diagram
Reply #3 - Feb 2nd, 2009, 10:07am
 
Thanks both of you, I'll take a look at the source of mesh and try to combine both.
Re: all triangle of delauny diagram
Reply #4 - Mar 16th, 2009, 3:50pm
 
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);
 }
}
Re: all triangle of delauny diagram
Reply #5 - Mar 22nd, 2009, 10:01pm
 
The Mesh library contains the quickhull3d jar and you can use that to pull out the face data for the delaunay triangulation.

(the middle section of this is just copy pasted out of the Mesh lib source)

Quote:
import quickhull3d.QuickHull3D;

void setup()
{
  size( 500, 500 );
  smooth();
 
  float[][] points = new float[100][2];
  for (int i=0; i<points.length; i++)
  {
    points[i][0] = random(0,width);
    points[i][1] = random(0,height);
  }
   
  /////////////////////////////////////////
  // pulled from mesh\source\Delaunay.java
  /////////////////////////////////////////
  double qPoints[] = new double[ points.length*3 + 9 ];
  for(int i=0; i<points.length; i++)
  {
    qPoints[i*3] = points[i][0];
    qPoints[i*3+1] = points[i][1];
    qPoints[i*3+2] = -(points[i][0]*points[i][0] + points[i][1]*points[i][1]); // standard half-squared eucledian distance
  }
  
  // 1
  qPoints[ qPoints.length-9 ] = -2000;
  qPoints[ qPoints.length-8 ] = 0;
  qPoints[ qPoints.length-7 ] = -4000000;
  // 2
  qPoints[ qPoints.length-6 ] = 2000;
  qPoints[ qPoints.length-5 ] = 2000;
  qPoints[ qPoints.length-4 ] = -8000000;
  // 3
  qPoints[ qPoints.length-3 ] = 2000;
  qPoints[ qPoints.length-2 ] = -2000;
  qPoints[ qPoints.length-1 ] = -8000000;
  
  QuickHull3D quickHull = new QuickHull3D(qPoints);
  quickHull.triangulate();
  int[][] faces = quickHull.getFaces(QuickHull3D.POINT_RELATIVE + QuickHull3D.CLOCKWISE);
  /////////////////////////////////////////
  
  for (int i = 0; i < faces.length; i++)
  {
     fill( random(100, 200) );
     beginShape();  
     for (int j = 0; j< faces[i].length; j++)
     {
        int pt = faces[i][j];
        if ( pt < points.length  )
        {
          vertex( points[pt][0], points[pt][1] );
        }
     }
     endShape(CLOSE);
   }
  
}

Page Index Toggle Pages: 1