Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

[diderot] View of /branches/ein16/synth/d2/symb_fv2d2.diderot
 [diderot] / branches / ein16 / synth / d2 / symb_fv2d2.diderot

# View of /branches/ein16/synth/d2/symb_fv2d2.diderot

Wed May 18 16:45:54 2016 UTC (3 years, 10 months ago) by cchiw
File size: 4710 byte(s)
`added some binary operators for 2-d`
```input int sz0 ("# samples on faster axis") = 101;
input int sz1 ("# samples on slower axis") = 100;
input real width ("approx width of world-space region sampled") = 1;
input vec2 off ("translation offset from origin-centered grid") = [0,0];
input real shear ("amount of shear in sampling grid") = 0;
input real angle ("orientation (in degrees) of faster axis") = 0;
input int which ("which function to sample") = 9;
input vec4 parm ("parameters that functions may use") = [0,0,0,0];

real theta = angle*π/180;
// rotation by theta of [1,0] towards [0,1]
tensor[2,2] rot = [[cos(theta),-sin(theta)],[sin(theta),cos(theta)]];
// sample spacing on faster and (unsheared) slower axis
vec2 spc = [width/(sz0-1), width/(sz1-1)];
// inter-sample vector on faster axis
vec2 edge0 = rot•[spc[0], 0];
// inter-sample vector on slower axis
vec2 edge1 = rot•[0, spc[1]] + shear*edge0;
// location of first sample
vec2 orig = -(edge0*(sz0-1) + edge1*(sz1-1))/2 + off;

//-- field defined by coefficients --
//-- single--
input vec2 base = [0, 0];    // b*x, c*y
//--squared--
input vec2 xsq = [0, 0];     // d*x^2, g*y*x^2
input vec2 ysq = [0, 0];     // f*y^2, h*x*y^2
//--diagonal--
input vec3 diag = [0, 0, 0];    // a, e*x*y,  i*y^2*x^2

input real scale = 1;

function real definef(vec2 pos) {
real x = pos[0];
real y = pos[1];
real z = pos[2];
real b = base[0];
real c = base[1];
real d = xsq[0];
real g = xsq[1];
real f = ysq[0];
real h = ysq[1];
real a = diag[0];
real e = diag[1];
real i = diag[2];

real ret = 0;
// as intended to be represented
//-- single--
real t0 = b*x + c*y;
//--squared--
real t1 = d*x^2 + g*y*x^2;
real t2 = f*y^2 + h*x*y^2;
real t3 = a + e*x*y + i*(y^2)*(x^2);
ret = scale*(t0+t1+t2+t3);
return ret;
}

function vec2 func(vec2 pos) {
real x = pos[0];
real y = pos[1];
real z = pos[2];
real b = base[0];
real c = base[1];
real d = xsq[0];
real g = xsq[1];
real f = ysq[0];
real h = ysq[1];
real a = diag[0];
real e = diag[1];
real i = diag[2];
vec2 ret = [0, 0];
if (0 == which) {        // 0: [x,y] ramp
ret = [x,y];
} else if (1 == which) { // 1:
ret = [x*x,y*y];
} else if (2 == which) { // 2:
ret = [x,y*y];
} else if (3 == which) {
//-- x-axis--
real x0 = b;
real x1 = d*2*x + g*y*2*x;
real x2 = h*y^2;
real x3 = e*y + i*(y^2)*(2*x);
real xaxis = (x0+x1+x2+x3);
//-- y-axis--
real y0 = c;
real y1 = g*x^2;
real y2 = f*2*y + h*x*2*y;
real y3 = e*x + i*(2*y)*(x^2);
real yaxis = (y0+y1+y2+y3);
ret = [xaxis, yaxis];
} else if (4 == which){
real val = definef(pos);
ret =[val,2*val];
}
else {
print("Sorry, -no function defined for which = ", which, "\n");
}
return ret;
}

strand sample(int idx0, int idx1) {
output vec2 out = [0.0,0.0];
update {
/* Diderot doesn't (currently) allow print statements from
global initialization, so to print something once per
program, you need to test for a condition that will be true
for one strand.  By the immediate stabilize, this will only
run for one iteration. */
if (0 == idx0 && 0 == idx1) {
print("NRRD0004\n");
print("# Complete NRRD file format specification at:\n");
print("# http://teem.sourceforge.net/nrrd/format.html\n");
// NOTE: this assumes we haven't been compiled with --double
print("type: float\n");
print("dimension: 3\n");
print("sizes: 2 ", sz0, " ", sz1, "\n");
print("kinds: 2-vector space space\n");

//         print("centers: cell cell\n");

print("endian: little\n");         // NOTE: this assumes machine endianness
print("encoding: raw\n");
print("space dimension: 2\n");
print("space directions: none (", edge0[0], ",", edge0[1],
") (", edge1[0], ",", edge1[1], ")\n");
print("space origin: (", orig[0], ",", orig[1], ")\n");
// NOTE: this assumes output filename is not explicitly set
print("data file: out.nrrd\n");
print("byte skip: -1\n");
}
out = func(orig + idx0*edge0 + idx1*edge1);
stabilize;
}
}

/*
** Create one strand per sample point. The "initially [ ]" creates a grid
** of strands (as opposed to a collection).
*/
initially [ sample(idx0, idx1)
| idx1 in 0..(sz1-1),   // SLOWER axis
idx0 in 0..(sz0-1) ]; // FASTER axis
```