/************************************************************ * file: voronoi.sl * author: Peter Stuart * date: 11/16/2002 * $Id: voronoi.sl,v 1.4 2003/11/18 19:56:53 ophi Exp $ * description: a 2d voronoi function similar to the voronoi * functions used in the Advanced RenderMan book. * ************************************************************/ #include "rmannotes.sl" void voronoi_f1_1d (float s, freq; float jitter; output float f1; output float posf) { float p = s*freq; float thiscell = floor(s*freq)+0.5; f1 = freq+1; uniform float i; for (i=-1; i<=1; i+=1) { float testcell = thiscell + i; float pos = testcell + jitter*(cellnoise(testcell) - 0.5); float offset = abs(pos - p); if (offset < f1) { f1 = offset; posf = pos; } } posf /= freq; } void voronoi_f1_2d (float s, t, freq; float jitter; output float f1; output float pos_s, pos_t) { point P = point(s*freq, t*freq, 0); /* transform s and t */ point thiscell = point(floor(s*freq)+0.5, floor(t*freq)+0.5, 0); f1 = freq+1; uniform float i, j; for (i=-1; i<=1; i+=1) { for (j=-1; j<=1; j+=1) { point testcell = thiscell + vector(i,j,0); point pos = testcell + jitter*(vector cellnoise(testcell) - 0.5); vector offset = pos - P; float dist = offset.offset; if (dist < f1) { f1 = dist; pos_s = xcomp(pos); pos_t = ycomp(pos); } } } /* transform back to s and t system */ pos_s /= freq; pos_t /= freq; f1 = sqrt(f1); } void voronoi_f2_2d (float s, t, freq; float jitter; output float f1, f2; output float pos_s, pos_t; output float pos2_s, pos2_t) { point P = point(s*freq, t*freq, 0); /* transform s and t */ point thiscell = point(floor(s*freq)+0.5, floor(t*freq)+0.5, 0); f1 = freq+1; f2 = 0; pos_s = 0; pos_t = 0; uniform float i, j; for (i=-1; i<=1; i+=1) { for (j=-1; j<=1; j+=1) { point testcell = thiscell + vector(i,j,0); point pos = testcell + jitter*(vector cellnoise(testcell) - 0.5); vector offset = pos - P; float dist = offset.offset; if (dist < f1) { f2 = f1; pos2_s = pos_s; pos2_t = pos_t; f1 = dist; pos_s = xcomp(pos); pos_t = ycomp(pos); } } } /* transform back to s and t system */ pos_s /= freq; pos_t /= freq; pos2_s /= freq; pos2_t /= freq; f1 = sqrt(f1); f2 = sqrt(f2); }