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

SCM Repository

[diderot] Annotation of /branches/pure-cfg/test/iso2d.diderot
ViewVC logotype

Annotation of /branches/pure-cfg/test/iso2d.diderot

Parent Directory Parent Directory | Revision Log Revision Log


Revision 957 - (view) (download)

1 : glk 953 // iso2d
2 :     //
3 :     // Demo of finding isocontours via Newton-Raphson method.
4 :     // Initializes positions on a grid, and each update applies one
5 :     // step of Newton-Raphson.
6 :     //
7 :     // Process output with:
8 :     // unu jhisto -i mip.txt -b 512 512 -min 0 0 -max 63 63 | unu 2op neq - 0 | unu quantize -b 8 -o iso2d.png
9 :    
10 :     int gridSize = 100;
11 :     real isoval = 30.0;
12 :     image(2)[] img = load("../data/ddro-64-unit.nrrd");
13 :     field#1(2)[] F = img ⊛ ctmr;
14 : glk 954 int stepsMax = 10;
15 : glk 953 real epsilon = 0.001;
16 :    
17 :     strand sample (int ui, int vi) {
18 :     output vec2 pos = [lerp(1.0, 62.0, -0.5, real(ui), real(gridSize)-0.5),
19 :     lerp(1.0, 62.0, -0.5, real(vi), real(gridSize)-0.5)];
20 :     int steps = 0;
21 :     update {
22 :     // We bail if we're no longer inside or taken too many steps.
23 :     // We tag this strand result as garbage by setting it to a
24 :     // non-finite position; these will be filtered later.
25 :     // This is really what "die" is for, though (see below).
26 : glk 954 if (!inside(pos, F) || steps > stepsMax) {
27 : glk 953 // HEY (BUG) not able to use NaN as in "pos = [nan,nan]"
28 : glk 957 // generated C code "vec2f(nanf, nanf)" causes problems:
29 :     // iso2d.c: In function ‘sample_update’:
30 :     // iso2d.c:86: error: incompatible type for argument 1 of ‘vec2f’
31 :     // iso2d.c:86: error: incompatible type for argument 2 of ‘vec2f’
32 :     // uncaught exception Fail [Fail: error compiling/linking]
33 :     // raised at c-target/c-target.sml:323.14-323.44
34 :     pos = [∞,∞]; // should be: pos = [nan,nan];
35 : glk 953 // HEY (BUG) we shouldn't even have to use non-finite values;
36 :     // we should be able to just say "die;". Currently, using
37 :     // "die" leads to the program never finishing...
38 :     stabilize;
39 :     }
40 :     vec2 grad = ∇F(pos);
41 :     real gmsq = grad • grad;
42 :     // If |∇F|=0 we can't compute update; we have to bail
43 :     if (gmsq == 0.0) {
44 :     pos = [∞,∞];
45 :     stabilize;
46 :     }
47 :     // delta = Newton-Raphson step
48 :     vec2 delta = -(F(pos) - isoval)*grad/gmsq;
49 :     // We've converged if the update is small enough
50 :     if (|delta| < epsilon) {
51 :     stabilize;
52 :     }
53 :     pos = pos + delta;
54 :     steps = steps + 1;
55 :     }
56 :     }
57 :    
58 :     initially [ sample(ui, vi) | vi in 0..(gridSize-1), ui in 0..(gridSize-1) ];

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