

Compiling under R:

Tue 12 Mar 2002

Makevars file allows me to control what CFLAGS are used, e.g.
  PKG_CFLAGS=-g  provides CFLAGS for the C code.
Now using the R_alloc, S_realloc() routines for  creating and
reallocating memory -- R frees this memory once the .C() call exits,
saving me the trouble of remembering them.  (If there is other memory
that I want to control, I should use Calloc, Realloc and Free()
accordnig to the R extensions manual.)

error() now working to show any errors within C.

Older notes:

Had to ensure we pass arguments as pointers, rather than actual
values.

sjevor.c is now one of the files that gets built into sjevor.c

All float types changed to Sfloat, which is a double.  (could use Real
to make it non-S specific)


Will need a RCS subdirectory, then consider merging with the 
matlab code so that we only have one common version.

For R, we also should use "Rprintf" rather than printf.

http://www.netlib.org/voronoi/sweep2
(saved in this directory as "sweep2")


----------------------------------------------------------------------
			 Voronoi tesselations


* Voronoi source code: ftp://netlib.bell-labs.com/netlib/voronoi/sweep2.Z

To compile the code on the DEC, I had to comment out line 16 of defs.h:

   /* char *malloc(); */ /* sje: comment out */


I also changed output.c:20 from:

	printf("l %f %f %f", e->a, e->b, e->c);
	to
	printf("l %f %f %f\n", e->a, e->b, e->c);

so that each command is on its own line.


Sun 17 Jan 1999: Further change to output.c so that it outputs the
number of the sites that the line perpendicularly bisects.



Matlab also has voronoi and delaunay code.

* How it works
Descriptions of the tesselation procedure are available from:

http://www.mhri.edu.au/~pdb/modelling/triangulate/

Voronoi tiles and Delauney triangles are closely related: triangle
edges are perpendicular bisectors of the tile edges.


* Data file formats


** Delauney triangles

Each line of the file is three numbers making a triangle.  Each number
is the index number of a data point.  So for example, the line: `31 58
83' indicates that a triangle can be drawn between data points 31, 58
and 83.

To convert the delaunay triangles into a postscript file (via gnuplot)
use the script delaunay (which calls delaunay.gp).

** Voronoi regions

The format of the file is described in the man page.  Line numbers (l)
and vertices (v) are numbered successively from zero.

To create the postscript file of the Voronoi regions, use the script
read_vor (which uses the script vor.gp)


* Examples:

The file `t' is an example of random inputs from the unit square.

The delaunay triangles are produced using:

voronoi -t t > tt
make del.ps

* Finding the area of a polygon.

Given n vertices (x_i, y_i) i=0..n-1, the area of the polygon is:

  Area = 0.5  * sum_{i=0}{n-1}  [ x_{i} y_{i+1} - x_{i+1} y_{i} ]

Assume that the polygon is closed, so vertex 0 and n-1 are the same
point.

(Taken from http://www.mhri.edu.au/~pdb/geometry/polyarea/index.html
 - this has a very neat and simple proof.)




* Problems:

When using the dataset t.horiz,  (two points, one above the other), we
get a floating exception in edgelist.c:
	bucket = (p->x - xmin)/deltax * ELhashsize;

since deltax is zero.  This only happens when all the datapoints have
the same x value, since deltax == xmax - xmin.  I've just put a little
check around the code for now.



* Copyright issues.

To get Voronoi code into octave, I've been tinkering with a perlscript
(read_vor) that assembles files in relevant ways for reading into
octave.

** Linking the Voronoi code into Octave.

This page has a link to Steve Fortune's home page, but it doesn't seem
to work. 

http://www.cs.sunysb.edu/~algorith/files/voronoi-diagrams.shtml

http://www.netlib.org/ Netlib analysis. says that most routines
have no restrictions, but best to contact author anyway.


Looks like Matlab is using Delaunay code.

Late-Breaking News for the 5.1 Product Family

Comparison of techniques for sorting.
http://goanna.cs.rmit.edu.au/~gl/research/comp_geom/delaunay/delaunay.html

http://oxi.upc.es/matlab/techdoc/relnotes/softwa1.html

MATLAB Software Acknowledgments

MATLAB and its associated products incorporate the following
third-party software:

The Delaunay function is based on code from 

Steve J. Fortune (1987) A Sweepline Algorithm for Voronoi Diagrams,
Algorithmica 2, 153-174. 





* Filenames

old		new

vnnds		vor_nnds
vareas		vor_areas



* Delaunay information in R

Here i ranges from 1:M, assuming there are M delaunay triangles

The following arrays are all of size (M,3)
v$delids[i,1]  gives the id number of the 1 (or 2,3) point for the ith
               triangle.

v$delangs[i,1] gives the 1 (or 2,3) angle of the ith delaunay triangle.

v$dellens[i,1] gives the 1 (or 2,3) delaunay segment length of the ith
               delaunay triangle.


From these base arrays, we can then reject triangles which include
reject sites.  These are then stored in:
v$adellens     same as dellens, but accepted points only.
v$adelangs     same as delangs, but accepted points only.


Given the Delaunay triangulation, we can generate the list of nearest
neighbours.  However, given the Voronoi tesselation, it may not be so
easy to generate the Delaunay tesselation, unless we remember the
points of the polygon... all academic...

