| 1 | # Copyright (C) 1998 Tuomas J. Lukka. | ||
| 2 | # All rights reserved, except redistribution | ||
| 3 | # with PDL under the PDL License permitted. | ||
| 4 | |||
| 5 | package PDL::Demos::TriDGallery; | ||
| 6 | |||
| 7 | use PDL; | ||
| 8 | use PDL::Graphics::TriD; | ||
| 9 | use PDL::Graphics::TriD::Image; | ||
| 10 | |||
| 11 | PDL::Demos::Routines->import(); | ||
| 12 | sub comment($); | ||
| 13 | sub act($); | ||
| 14 | sub actnw($); | ||
| 15 | sub output; | ||
| 16 | |||
| 17 | sub run { | ||
| 18 | |||
| 19 | |||
| 20 | comment q| | ||
| 21 | Welcome to the TriD Gallery | ||
| 22 | |||
| 23 | The following selection of scripts demonstrates that you | ||
| 24 | can generate interesting images with PDL (and the TriD | ||
| 25 | modules) with just a few lines of code. | ||
| 26 | |||
| 27 | These are the rules for scripts to be accepted for this | ||
| 28 | category: | ||
| 29 | |||
| 30 | |||
| 31 | 1) Must be legal Perl with a recent PDL version - may come with | ||
| 32 | a patch to PDL if the patch is general enough to be included | ||
| 33 | in the next release and usable outside the demo (e.g. | ||
| 34 | $x=mandelbrot($c) is NOT), i.e. you can introduce new | ||
| 35 | commands | ||
| 36 | |||
| 37 | 2) The code must fit in 4 lines, 72 columns. | ||
| 38 | |||
| 39 | 3) It must create an interesting image when fed to perl. | ||
| 40 | |||
| 41 | If you have an interesting new TriD M4LS (Maximal-4-lines-script) | ||
| 42 | submit it to the PDL mailing list (pdl-general@lists.sourceforge.net) | ||
| 43 | and there is a good chance it will soon be included in the gallery | ||
| 44 | |||
| 45 | Press 'q' in the graphics window for the next screen. | ||
| 46 | Rotate the image by pressing mouse button one and | ||
| 47 | dragging in the graphics window. | ||
| 48 | Zoom in/out by pressing MB3 and drag up/down. | ||
| 49 | |; | ||
| 50 | |||
| 51 | actnw q| | ||
| 52 | # B/W Mandelbrot... [Tjl] | ||
| 53 | |||
| 54 | use PDL; use PDL::Graphics::TriD; | ||
| 55 | $s=150;$a=zeroes $s,$s;$r=$a->xlinvals(-1.5,0.5);$i=$a->ylinvals(-1,1); | ||
| 56 | $t=$r;$u=$i; | ||
| 57 | for(0..12){$q=$r**2-$i**2+$t;$h=2*$r*$i+$u;($r,$i)=map{$_->clip(-5,5)}($q,$h);} | ||
| 58 | imagrgb[($r**2+$i**2)>2.0]; | ||
| 59 | |||
| 60 | # [press 'q' in the graphics window when done] | ||
| 61 | |; | ||
| 62 | |||
| 63 | if(0) { | ||
| 64 | actnw q| | ||
| 65 | # Greyscale Mandelbrot [Tjl] | ||
| 66 | |||
| 67 | use PDL; use PDL::Graphics::TriD;$a=zeroes 300,300;$r=$a->xlinvals(-1.5, | ||
| 68 | 0.5);$i=$a->ylinvals(-1,1);$t=$r;$u=$i;for(1..30){$q=$r**2-$i**2+$t;$h=2 | ||
| 69 | *$r*$i+$u;$d=$r**2+$i**2;$a=lclip($a,$_*($d>2.0)*($a==0));($r,$i)=map{$_ | ||
| 70 | ->clip(-5,5)}($q,$h);}imagrgb[$a/30]; | ||
| 71 | |||
| 72 | # [press 'q' in the graphics window when done] | ||
| 73 | |; | ||
| 74 | |||
| 75 | actnw q| | ||
| 76 | # Color Mandelbrot anim (nokeeptwiddling3d removed -> fits) [Tjl] | ||
| 77 | |||
| 78 | use PDL; use PDL::Graphics::TriD; | ||
| 79 | nokeeptwiddling3d(); | ||
| 80 | $a=zeroes 300,300;$r=$a->xlinvals(-1.5, | ||
| 81 | 0.5);$i=$a->ylinvals(-1,1);$t=$r;$u=$i;for(1..30){$q=$r**2-$i**2+$t;$h=2 | ||
| 82 | *$r*$i+$u;$d=$r**2+$i**2;$a=lclip($a,$_*($d>2.0)*($a==0));($r,$i)=map{$_ | ||
| 83 | ->clip(-5,5)}$q,$h;imagrgb[($a==0)*($r/2+0.75),($a==0)*($i+1)/2,$a/30]} | ||
| 84 | |||
| 85 | # [press 'q' in the graphics window when done] | ||
| 86 | |; | ||
| 87 | } | ||
| 88 | |||
| 89 | if(0){ | ||
| 90 | actnw q| | ||
| 91 | # Torus... (barrel) [Tjl] | ||
| 92 | |||
| 93 | use PDL; use PDL::Graphics::TriD; | ||
| 94 | $s=40;$a=zeroes $s,$s;$t=$a->xlinvals(0,6.284); | ||
| 95 | $u=$a->ylinvals(0,6.284);$o=5;$i=1;$v=$o+$i*sin$u; | ||
| 96 | imag3d([$v*sin$t,$v*cos$t,$i*cos$u]); | ||
| 97 | |; | ||
| 98 | |||
| 99 | actnw q| | ||
| 100 | # Ripply torus [Tjl] | ||
| 101 | |||
| 102 | use PDL; use PDL::Graphics::TriD; | ||
| 103 | $s=40;$a=zeroes 2*$s,$s/2;$t=$a->xlinvals(0,6.284); | ||
| 104 | $u=$a->ylinvals(0,6.284); $o=5;$i=1;$v=$o+$i*sin$u; | ||
| 105 | imag3d([$v*sin$t,$v*cos$t,$i*cos($u)+$o*sin(3*$t)]); | ||
| 106 | |; | ||
| 107 | |||
| 108 | actnw q| | ||
| 109 | # Ripply torus distorted [Tjl] | ||
| 110 | |||
| 111 | use PDL; use PDL::Graphics::TriD; | ||
| 112 | $s=40;$a=zeroes 2*$s,$s/2;$t=$a->xlinvals(0,6.284);$u=$a->ylinvals(0, | ||
| 113 | 6.284); $o=5;$i=1;$v=$o-$o/2*sin(3*$t)+$i*sin$u; | ||
| 114 | imag3d([$v*sin$t,$v*cos$t,$i*cos($u)+$o*sin(3*$t)]); | ||
| 115 | |; | ||
| 116 | |||
| 117 | actnw q~ | ||
| 118 | # Game of life [Robin Williams (edited by Tjl)] | ||
| 119 | |||
| 120 | use PDL; use PDL::Image2D; use PDL::Graphics::TriD;nokeeptwiddling3d; | ||
| 121 | $d=byte(random(zeroes(40,40))>0.85);$k=byte [[1,1,1],[1,0,1],[1,1,1]]; | ||
| 122 | do{ imagrgb [$d]; $s=conv2d($d,$k); | ||
| 123 | $d&=($s<4);$d&=($s>1);$d|=($s==3);} while (!twiddle3d); | ||
| 124 | |||
| 125 | ~; | ||
| 126 | |||
| 127 | actnw q~ | ||
| 128 | # Dewdney's voters (parallelized) [Tjl, inspired by the above 'life'] | ||
| 129 | |||
| 130 | use PDL; use PDL::Image2D; use PDL::Graphics::TriD;nokeeptwiddling3d;$d= | ||
| 131 | byte(random(zeroes(100,100))>0.5);do{$k=float [[1,1,1],[1,0,1],[1,1,1]]; | ||
| 132 | imagrgb[$d]; $s=conv2d($d,$k)/8; $r = $s->float->random; | ||
| 133 | $e = ($s>$r); $d .= $e; }while(!twiddle3d) | ||
| 134 | |||
| 135 | ~; | ||
| 136 | } | ||
| 137 | |||
| 138 | actnw q| | ||
| 139 | # Volume rendering [Robin Williams] | ||
| 140 | use PDL; use PDL::Graphics::TriD; keeptwiddling3d(); | ||
| 141 | $b=zeroes(50,50,50);$b=sin(0.3*$b->rvals)*cos(0.3*$b->xvals);$c=0; | ||
| 142 | $a=byte($b>$c);foreach(1,2,4){$t=($a->slice("0:-2")<<$_);$t+=$a->slice("1:-1"); | ||
| 143 | $a = $t->mv(0,2);} points3d [whichND(($a != 0) & ($a != 255))]; | ||
| 144 | |; | ||
| 145 | |||
| 146 | actnw q| | ||
| 147 | # Lucy deconvolution (AJ 79, 745) [Robin Williams (=> TriD by Tjl)] | ||
| 148 | use PDL; use PDL::Graphics::TriD; nokeeptwiddling3d(); | ||
| 149 | sub smth {use PDL::Image2D; conv2d($_[0],exp(-(rvals ones(3,3))**2));} | ||
| 150 | $a=rfits("m51.fits")->float; $c=$d=avg($a)+0*$a; | ||
| 151 | while(max $c>1.1) {$c=smth($a/smth($d));$d*=$c;imagrgb[$d/850];} | ||
| 152 | |; | ||
| 153 | |||
| 154 | |||
| 155 | # use PDL; use PDL::Image2D; use PDL::Graphics::TriD;nokeeptwiddling3d; | ||
| 156 | # $d=byte(random(zeroes(90,90))>0.5);do{$k=byte [[1,1,1],[1,0,1],[1,1,1]]; | ||
| 157 | # imagrgb[$d]if($k++%2); $s=conv2d($d,$k)/8;$i=90*90*random(50);$t=$d-> | ||
| 158 | # clump(2)-> index($i);$t.=($s->clump(2)->index($i)>.5);}while(!twiddle3d) | ||
| 159 | |||
| 160 | actnw q| | ||
| 161 | # spherical dynamics [Mark R Baker] | ||
| 162 | use PDL;use PDL::Graphics::TriD;for $c(1..99){$n=6.28*$c; $g=$c*rvals( | ||
| 163 | sin(zeros(5000))*$c);$cz=-1**$g*$c;$cy=$g*cos$g*$c;$cx=$c*rvals($g)*$c; | ||
| 164 | $g=cos($w=$cz+$cy+$cx);$r=sin$cy+$c+$cz;$b=sin$w;nokeeptwiddling3d(); | ||
| 165 | $i=$cz-$cx-$cy;$q=$i*$n;points3d[$b*sin$q,$r*cos$q,$g*sin$q],[$r,$g,$b]} | ||
| 166 | |; | ||
| 167 | |||
| 168 | actnw q~ | ||
| 169 | # Fractal mountain range [Tuomas Lukka] | ||
| 170 | use PDL;use PDL::Image2D;use PDL::Graphics::TriD; keeptwiddling3d(); $k=ones(5,5) / 25; | ||
| 171 | $a=5;$b=ones(1,1)/2;for(1..7){$c=$b->dummy(0,2)->clump(2)->xchg(0,1)-> | ||
| 172 | dummy(0,2)->clump(2)->xchg(0,1)->copy;$c+=$a*$c->random;$a/=3; | ||
| 173 | $b=conv2d($c,$k); imag3d[$b],{Lines => 0}; } | ||
| 174 | ~; | ||
| 175 | |||
| 176 | comment q| | ||
| 177 | We hope you did like that and got a feeling of | ||
| 178 | the power of PDL. | ||
| 179 | |||
| 180 | Now it's up to you to submit even better TriD M4LSs. | ||
| 181 | |||
| 182 | |; | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | if(0) { # one possible addition to volume rendering... | ||
| 187 | |||
| 188 | use PDL; use PDL::Graphics::TriD; | ||
| 189 | $b=zeroes(50,50,50);$b=sin(0.3*$b->rvals)*cos(0.3*$b->xvals);$c=0; | ||
| 190 | $a=byte($b>$c);foreach(1,2,4){$t=($a->slice("0:-2")<<$_);$t+=$a->slice("1:-1"); | ||
| 191 | $a = $t->mv(0,2);}points3d[map{$_+$_->float->random}whichND(($a!=0)&($a != 255))]; | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | # Neat, but too big variation of color mandelbrot | ||
| 196 | if(0) { | ||
| 197 | |||
| 198 | use PDL; use PDL::Graphics::TriD; | ||
| 199 | nokeeptwiddling3d(); | ||
| 200 | sub f {return abs(sin($_[0]*30))} | ||
| 201 | $a=zeroes 300,300;$r=$a->xlinvals(-1.5, | ||
| 202 | 0.5);$i=$a->ylinvals(-1,1);$t=$r;$u=$i;for(1..30){$q=$r**2-$i**2+$t;$h=2 | ||
| 203 | *$r*$i+$u;$d=$r**2+$i**2;$a=lclip($a,$_*($d>2.0)*($a==0));($r,$i)=map{$_ | ||
| 204 | ->clip(-5,5)}$q,$h;imagrgb[f(($a==0)*($r/2+0.75)),f(($a==0)*($i+1)/2),$a/30]} | ||
| 205 | |||
| 206 | } | ||
| 207 | 1; |