Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /tests/examples/iso2d/iso2d.diderot
ViewVC logotype

View of /tests/examples/iso2d/iso2d.diderot

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4640 - (download) (annotate)
Tue Sep 27 20:54:47 2016 UTC (2 years, 9 months ago) by glk
File size: 5369 byte(s)
initial result of svn export --username anonsvn --password=anonsvn https://svn.smlnj-gforge.cs.uchicago.edu/svn/diderot/branches/vis15/src/tests/
/* ==========================================
## iso2d.diderot: 2D isocontour sampling by independent (non-interacting) particles

First make a little dataset, using ../fs2d/fs2d-scl:

	../fs2d/fs2d-scl -size0 50 -size1 50 -which 3 -width 8 | unu save -f nrrd -o cubic.nrrd

Then compile this program; assuming the directions at
https://github.com/Diderot-Language/examples you can:

	../../vis12/bin/diderotc --exec iso2d.diderot

You should try `./iso2d --help` to see the available input variables.
Note that if a Diderot program refers to an image file, that image
file needs to exist at compile time (so that the compiler can generate
instructions specific to the data type and the image
orientation). Hence the need to create cubic.nrrd before running
diderotc.  If a needed .nrrd file is missing, the error message looks
like:

	uncaught exception Fail [Fail: Nrrd file "cubic.nrrd" does not exist]
	  raised at common/phase-timer.sml:78.50-78.52
	  raised at common/phase-timer.sml:78.50-78.52
	  raised at nrrd/nrrd-info.sml:146.15-146.74


To now run the isocontour sampling (starting with a 100x100 grid of points),
at the default isovalue 0:

	./iso2d -cmin -4 -4 -cmax 4 4 -size 100

This saves the output positions into `pos.nrrd` as a list of 2-vectors, which
can be seen by running `unu head pos.nrrd`. You can convert this to a
text file with `unu save -f text -i pos.nrrd -o pos.txt`.

In any case, Diderot doesn't itself supply a way of visualizing this
point set; some other graphics or plotting program is needed.  A really
quick-and-dirty way of showing the points is as a joint histogram
of their X and Y coordinates:

	unu jhisto -i pos.nrrd -b 500 500 -min -4 4 -max 4 -4 -t float | unu 2op gt - 0 | unu quantize -b 8 -o pos.png

Make sure your pos.png looks like [`pos-ref.png`](pos-ref.png); it may
not be an exact pixel match but it should be close.  If `pos.png`
looks very different (or blank), your Teem checkout may be old (a new
fix was committed Nov 5 2015).  Assuming your PNG viewer uses
conventional display orientation, the `-min -4 4 -max 4 -4` arguments
to `unu jhisto` should make the first coordinate ("x") increase to the
right, and the second coordinate ("y") increase towards up.

Even though you had to supply the input image at compile-time, you can
supply a different image at run-time, if the image is noted as an
"input" (as below), and provided that the new image exactly matches
the type and shape of the compile-time image.  We can add some noise
to the dataset, and then add a little ramp along the Y axis (to explicitly
break the over-all symmetry of the isocontour):

	unu 2op nrand cubic.nrrd 0.5 -s 42 -o noisy.nrrd
	../fs2d/fs2d-scl -size0 50 -size1 50 -which 1 -width 8 | unu save -f nrrd -o yramp.nrrd
	rm out.nrrd
	unu 2op x yramp.nrrd 3 | unu 2op + noisy.nrrd - -o noisy.nrrd

and then re-run the isocontouring and display on the new data.

	./iso2d -cmin -4 -4 -cmax 4 4 -size 100 -img noisy.nrrd -o pos2.nrrd
	unu jhisto -i pos2.nrrd -b 500 500 -min -4 4 -max 4 -4 -t float | unu 2op gt - 0 | unu quantize -b 8 -o pos2.png

Make sure your pos2.png looks like [`pos2-ref.png`](pos2-ref.png).

Things to try (to see their effect on the output positions, both the
number of outputs and their location):
* increasing or decreasing `stepsMax`
* increasing or decreasing `epsilon`
* increasing or decreasing `size`; if it goes too low then the program will not have any output
========================================== */

input real isoval ("isovalue of isosurface to sample") = 0;
input int stepsMax ("max # steps allowed for convergence") = 10;
input real epsilon ("convergence threshold") = 0.00001;
input image(2)[] img ("data to isocontour") = image("cubic.nrrd");

// way of specifying the grid on which sampling is done
input vec2 cmin ("# lower corner of sampling grid") = [-1,-1];
input vec2 cmax ("# upper corner of sampling grid") = [1,1];
input int size ("# samples on both axes of sampling grid") = 30;

// field is defined so that isocontour of interest is the zero levelset
field#1(2)[] F = c4hexic ⊛ img - isoval;

/*
The ID argument is not actually used, but it is included here as an
example of a way of identifying strands that can be useful for
debugging.
*/
strand iso(int ID, vec2 pos0) { // vec2 is a synonym for tensor[2]
   output vec2 pos = pos0;
   int steps = 0;
   update {
      // Stop if no longer inside, or have taken too many steps.
      if (!inside(pos, F) || steps > stepsMax) {
         die;
      }

      // Subsequent expressions are undefined if |∇F| is zero
      if (|∇F(pos)| == 0.0) {
         die;
      }

      // The Newton-Raphson step
      vec2 delta = -normalize(∇F(pos)) * F(pos)/|∇F(pos)|;
      pos += delta;

      // We've converged if delta is small enough
      if (|delta| < epsilon) {
         stabilize;
      }
      steps += 1;
   }
}

/*
** This generates a node-centered sampling of the region;
** a cell-centered sampling would be created with:
                [lerp(cmin[0], cmax[0], -0.5, idx0, size-0.5),
                 lerp(cmin[1], cmax[1], -0.5, idx1, size-0.5)])
*/
initially { iso(idx0 + size*idx1,
                [lerp(cmin[0], cmax[0], 0, idx0, size-1),
                 lerp(cmin[1], cmax[1], 0, idx1, size-1)])
           | idx1 in 0..(size-1),   // SLOWER axis
             idx0 in 0..(size-1) }; // FASTER axis

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0