Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

[diderot] View of /tests/vis15-bugs/plot1d2d.diderot
 [diderot] / tests / vis15-bugs / plot1d2d.diderot

# View of /tests/vis15-bugs/plot1d2d.diderot

Wed Oct 19 22:30:38 2016 UTC (2 years, 8 months ago) by glk
File size: 2963 byte(s)
`adding plot1d2d`
```input image(1)[] img = image("plot1d2d-data.nrrd");
image(1)[] wimg = wrap(img);

input vec2 xmm = [-4,4];
input vec2 ymm = [-0.9,1.1];
input int xsize = 800;
input int ysize = 400;

field#4(1)[] FF0 = c4hexic ⊛ wimg;

/*
// this does work to define FF2 as second derivative of FF0
field#3(1)[] FF1 = ∇FF0;
field#2(1)[] FF2 = ∇FF1;
*/
/*
// this also works
field#2(1)[] FF2 = ∇(∇FF0);
*/

field#2(1)[] FF2 = ∇∇FF0; // BUG no, does not work

// aspect ratio of pixels in world-space
real asp = ((ysize-1)/(ymm[1]-ymm[0])) / ((xsize-1)/(xmm[1]-xmm[0]));

// plot is a trapezoid, centered at origin, at height 1 for width th,
// with sides going down to zero over unit interval on either side
function real tzoid (real d, real th) {
real ret = 0;
if (|d| < th/2) {
ret = 1;
} else {
ret = max(0, lerp(1, 0, th/2, |d|, th/2 + 1)); // HEY use clerp
}
return ret;
}

// indicates partial membership in a line of given thickness,
// at distance d away from the line
function real aline(real d) = tzoid(d, 4);
function real pline(real d) = tzoid(d, 7);

// indicates partial membership in 0-centered tickmark,
// at distance d from axis
function real tick(real d) = tzoid(d, 20);

// rounds v to nearest integer
//function real round(real v) = (v+0.5 - fmod(v+0.5, 1)
//                               if (v > 0) else
//                               -(-v+0.5 - fmod(-v+0.5, 1)));

/*
** For converting between index and world, along X and Y. Note
** that the function inverse is obtained by switching the input
** interval (args 3 and 5) with the output interval (args 1 and 2).
*/
function real xw2i(real w) = lerp(-0.5, xsize-0.5, xmm[0], w, xmm[1]);
function real xi2w(real i) = lerp(xmm[0], xmm[1], -0.5, i, xsize-0.5);
function real yw2i(real w) = lerp(-0.5, ysize-0.5, ymm[1], w, ymm[0]);
function real yi2w(real i) = lerp(ymm[1], ymm[0], -0.5, i, ysize-0.5);

strand plot(int xi, int yi) {
output vec3 rgb = [1,1,1];
update {
// image is cell-centered sampling of world-space
// [xmm[0],xmm[1]] × [ymm[0],ymm[1]]

// convert (0,0) from world to index, and draw axes
real y0i = yw2i(0);
real x0i = xw2i(0);
rgb = lerp(rgb, [0.8,0.8,0.9], aline(y0i - yi));
rgb = lerp(rgb, [0.8,0.8,0.9], aline(x0i - xi));

// tick marks at integers
rgb = lerp(rgb, [0.7,0.7,0.8], tick(y0i - yi)*aline(xw2i(round(xi2w(xi))) - xi));
rgb = lerp(rgb, [0.7,0.7,0.8], tick(x0i - xi)*aline(yw2i(round(yi2w(yi))) - yi));

real xw = xi2w(xi);     // convert X position from index to world

real fxi = yw2i(FF2(xw));
real mm = asp*(∇FF2)(xw);

/*
real fxi = yw2i(∇(∇FF0)(xw));
real mm = asp*(∇(∇(∇FF0)))(xw);
*/
rgb = lerp(rgb, [0.4,0,0], pline((yi - fxi)/sqrt(1 + mm^2)));
stabilize;
}
}

initially [ plot(xi, yi)
| yi in 0..(ysize-1),   // SLOWER axis
xi in 0..(xsize-1) ]; // FASTER axis
```