Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

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

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

Mon May 16 16:32:19 2016 UTC (3 years, 10 months ago) by cchiw
File size: 3994 byte(s)
`moved steps to run program`
```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, currently from 0 to 5") = 10;
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 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];

real ret = 0;
if (0 == which) {
// as intended to be represented
real t0 = b*x + c*y;
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 = t0+t1+t2+t3;
}
else if (1 == which) {
// scaling
real t0 = b*x + c*y;
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);
}
else {
print("Sorry, no function defined for which = ", which, "\n");
}
return ret;

}

strand sample(int idx0, int idx1) {
output real out = 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: 2\n");
print("sizes: ", sz0, " ", sz1, "\n");
print("kinds: space space\n");
print("centers: cell cell\n");
// NOTE: this assumes machine endianness
print("endian: little\n");
print("encoding: raw\n");
print("space dimension: 2\n");
// Diderot prints vectors like it parses them, e.g "[0.1,0.3]"
// but this is not how orientation vectors are stored in the NRRD
// header, hence the need to print individual components
print("space directions: (", 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
```