@@ -11,8 +11,8 @@ require File::Spec;
1111my @f = qw( PDL IO STL owl.stl) ;
1212our $owlfile = undef ;
1313foreach my $path ( @INC ) {
14- my $file = File::Spec-> catfile( $path , @f );
15- if ( -f $file ) { $owlfile = $file ; last ; }
14+ my $file = File::Spec-> catfile( $path , @f );
15+ if ( -f $file ) { $owlfile = $file ; last ; }
1616}
1717
1818sub info {(' 3d' , ' 3d demo (requires TriD with OpenGL or Mesa)' )}
@@ -22,204 +22,202 @@ use PDL::Graphics::TriD;
2222
2323my @demo = (
2424[comment => q|
25- Welcome to a short tour of the capabilities of
26- PDL::Graphics::TriD.
25+ Welcome to a short tour of the capabilities of
26+ PDL::Graphics::TriD.
2727
28- Press 'q' in the graphics window for the next screen.
29- Rotate the image by pressing mouse button one and
30- dragging in the graphics window.
31- Zoom in/out by pressing MB3 and drag up/down.
32- Note that a standalone TriD script must start with
28+ Press 'q' in the graphics window for the next screen.
29+ Rotate the image by pressing mouse button one and
30+ dragging in the graphics window.
31+ Zoom in/out by pressing MB3 and drag up/down.
32+ Note that a standalone TriD script must start with
3333
34- use PDL;
35- use PDL::Graphics::TriD;
34+ use PDL;
35+ use PDL::Graphics::TriD;
3636
37- to work properly.
37+ to work properly.
3838| ],
3939
4040[actnw => q|
41- # See if we had a 3D window open already
42- $| .__PACKAGE__ .q| ::we_opened = !defined $PDL::Graphics::TriD::current_window;
43- $vertices = pdl([ [0,0,-1], [1,0,-1], [0.5,1,-1], [0.5,0.5,0] ]);
44- $faceidx = pdl([ [0,2,1], [0,1,3], [0,3,2], [1,2,3] ]);
45- # show the vertex and face normal vectors on a triangular grid
46- trigrid3d($vertices,$faceidx,{ShowNormals=>1});
47- # [press 'q' in the graphics window when done]
41+ # See if we had a 3D window open already
42+ $| .__PACKAGE__ .q| ::we_opened = !defined $PDL::Graphics::TriD::current_window;
43+ $vertices = pdl([ [0,0,-1], [1,0,-1], [0.5,1,-1], [0.5,0.5,0] ]);
44+ $faceidx = pdl([ [0,2,1], [0,1,3], [0,3,2], [1,2,3] ]);
45+ # show the vertex and face normal vectors on a triangular grid
46+ trigrid3d($vertices,$faceidx,{ShowNormals=>1});
47+ # [press 'q' in the graphics window when done]
4848| ],
4949
5050[actnw => q|
51- # Show a PDL logo
52- use PDL::Constants qw(PI);
53- require PDL::Graphics::TriD::Logo;
54- $vertices = $PDL::Graphics::TriD::Logo::POINTS;
55- $faceidx = $PDL::Graphics::TriD::Logo::FACES;
56- $rotate_m = pdl [1,0,0],[0,0,1],[0,-1,0]; # top towards X axis
57- $c22 = cos(PI/8); $s22 = sin(PI/8);
58- $rot22 = pdl [$c22,$s22,0],[-$s22,$c22,0],[0,0,1]; # +22deg about vert
59- $vertices = ($vertices x $rotate_m x $rot22);
60- trigrid3d($vertices,$faceidx,{Smooth=>0});
61- # [press 'q' in the graphics window when done]
51+ # Show a PDL logo
52+ use PDL::Constants qw(PI);
53+ require PDL::Graphics::TriD::Logo;
54+ $vertices = $PDL::Graphics::TriD::Logo::POINTS;
55+ $faceidx = $PDL::Graphics::TriD::Logo::FACES;
56+ $rotate_m = pdl [1,0,0],[0,0,1],[0,-1,0]; # top towards X axis
57+ $c22 = cos(PI/8); $s22 = sin(PI/8);
58+ $rot22 = pdl [$c22,$s22,0],[-$s22,$c22,0],[0,0,1]; # +22deg about vert
59+ $vertices = ($vertices x $rotate_m x $rot22);
60+ trigrid3d($vertices,$faceidx,{Smooth=>0});
61+ # [press 'q' in the graphics window when done]
6262| ],
6363
6464(!defined $owlfile ? () : [actnw => q|
65- # Show an owl loaded from an STL file
66- use PDL::IO::STL;
67- ($vertices, $faceidx) = rstl $| .__PACKAGE__ .q| ::owlfile;
68- trigrid3d($vertices,$faceidx,{Smooth=>0});
69- # [press 'q' in the graphics window when done]
65+ # Show an owl loaded from an STL file
66+ use PDL::IO::STL;
67+ ($vertices, $faceidx) = rstl $| .__PACKAGE__ .q| ::owlfile;
68+ trigrid3d($vertices,$faceidx,{Smooth=>0});
69+ # [press 'q' in the graphics window when done]
7070| ]),
7171
7272[actnw => q|
73- # Number of subdivisions for lines / surfaces.
74- $size = 25;
75- $cz = (xvals $size+1) / $size; # interval 0..1
76- $cx = sin($cz*12.6); # Corkscrew
77- $cy = cos($cz*12.6);
78- line3d [$cx,$cy,$cz]; # Draw a line
79- # [press 'q' in the graphics window when done]
73+ # Number of subdivisions for lines / surfaces.
74+ $size = 25;
75+ $cz = (xvals $size+1) / $size; # interval 0..1
76+ $cx = sin($cz*12.6); # Corkscrew
77+ $cy = cos($cz*12.6);
78+ line3d [$cx,$cy,$cz]; # Draw a line
79+ # [press 'q' in the graphics window when done]
8080| ],
8181
8282[actnw => q|
83- $r = sin($cz*6.3)/2 + 0.5;
84- $g = cos($cz*6.3)/2 + 0.5;
85- $b = $cz;
86- line3d [$cx,$cy,$cz], [$r,$g,$b]; # Draw a colored line
87- # [press 'q' in the graphics window when done]
83+ $r = sin($cz*6.3)/2 + 0.5;
84+ $g = cos($cz*6.3)/2 + 0.5;
85+ $b = $cz;
86+ line3d [$cx,$cy,$cz], [$r,$g,$b]; # Draw a colored line
87+ # [press 'q' in the graphics window when done]
8888| ],
8989
9090[actnw => q|
91- $x = (xvals $size+1,$size+1) / $size;
92- $y = (yvals $size+1,$size+1) / $size;
93- $z = 0.5 + 0.5 * (sin($x*6.3) * sin($y*6.3)) ** 3; # Bumps
94- line3d [$x,$y,$z]; # Draw several lines
95- # [press 'q' in the graphics window when done]
91+ $x = (xvals $size+1,$size+1) / $size;
92+ $y = (yvals $size+1,$size+1) / $size;
93+ $z = 0.5 + 0.5 * (sin($x*6.3) * sin($y*6.3)) ** 3; # Bumps
94+ line3d [$x,$y,$z]; # Draw several lines
95+ # [press 'q' in the graphics window when done]
9696| ],
9797
9898[actnw => q|
99- $r = $x;
100- $g = $y;
101- $b = $z;
102- line3d [$x,$y,$z], [$r,$g,$b]; # Draw several colored lines
103- # [press 'q' in the graphics window when done]
99+ $r = $x;
100+ $g = $y;
101+ $b = $z;
102+ line3d [$x,$y,$z], [$r,$g,$b]; # Draw several colored lines
103+ # [press 'q' in the graphics window when done]
104104| ],
105105
106106[actnw => q|
107- lattice3d [$x,$y,$z], [$r,$g,$b]; # Draw a colored lattice
108- # [press 'q' in the graphics window when done]
107+ lattice3d [$x,$y,$z], [$r,$g,$b]; # Draw a colored lattice
108+ # [press 'q' in the graphics window when done]
109109| ],
110110
111111[actnw => q|
112- points3d [$x,$y,$z], [$r,$g,$b], {PointSize=>4}; # Draw colored points
113- # [press 'q' in the graphics window when done]
112+ points3d [$x,$y,$z], [$r,$g,$b], {PointSize=>4}; # Draw colored points
113+ # [press 'q' in the graphics window when done]
114114| ],
115115
116116[actnw => q|
117- imag3d_ns [$x,$y,$z], [$r,$g,$b]; # Draw a colored surface
118- # [press 'q' in the graphics window when done]
117+ imag3d_ns [$x,$y,$z], [$r,$g,$b]; # Draw a colored surface
118+ # [press 'q' in the graphics window when done]
119119| ],
120120
121121[actnw => q|
122- imag3d [$x,$y,$z]; # Draw a shaded surface
123- # [press 'q' in the graphics window when done]
122+ imag3d [$x,$y,$z]; # Draw a shaded surface
123+ # [press 'q' in the graphics window when done]
124124| ],
125125
126126[actnw => q|
127- # Draw a shaded, coloured, unsmoothed (default is on) surface
128- imag3d [$x,$y,$z], [$x,$y,$z], { Smooth => 0 };
129- # [press 'q' in the graphics window when done]
127+ # Draw a shaded, coloured, unsmoothed (default is on) surface
128+ imag3d [$x,$y,$z], [$x,$y,$z], { Smooth => 0 };
129+ # [press 'q' in the graphics window when done]
130130| ],
131131
132132[actnw => q|
133- # Draw a shaded, coloured, smoothed (the default) surface
134- imag3d [$x,$y,$z], [$x,$y,$z];
135- # [press 'q' in the graphics window when done]
133+ # Draw a shaded, coloured, smoothed (the default) surface
134+ imag3d [$x,$y,$z], [$x,$y,$z];
135+ # [press 'q' in the graphics window when done]
136136| ],
137137
138138[actnw => q|
139- hold3d(); # Leave the previous object in..
140- imag3d_ns [$x,$y,$z+1], [$r,$g,$b];
141- # ...and draw a colored surface on top of it...
142- # [press 'q' in the graphics window when done]
139+ hold3d(); # Leave the previous object in..
140+ imag3d_ns [$x,$y,$z+1], [$r,$g,$b];
141+ # ...and draw a colored surface on top of it...
142+ # [press 'q' in the graphics window when done]
143143| ],
144144
145145[actnw => q|
146- lattice3d [$x,$y,$z-1], [$r,$g,$b];
147- # ...and draw a colored lattice under it...
148- # [press 'q' in the graphics window when done]
146+ lattice3d [$x,$y,$z-1], [$r,$g,$b];
147+ # ...and draw a colored lattice under it...
148+ # [press 'q' in the graphics window when done]
149149| ],
150150
151151[actnw => q|
152- contour3d($z, [$x,$y,$z-1], {Labels=>[1,15]});
153- # ...and draw contours on that
154- # [press 'q' in the graphics window when done]
152+ contour3d($z, [$x,$y,$z-1], {Labels=>[1,15]}); # ...and draw contours on that
153+ # [press 'q' in the graphics window when done]
155154| ],
156155
157156[actnw => q|
158- nokeeptwiddling3d(); # Don't wait for user while drawing
159- for (-2,-1,0,1,2) {
160- line3d [$cx,$cy,$cz+$_]; # ... and corkscrews...
161- }
162- keeptwiddling3d(); # Do wait for user while drawing...
163- twiddle3d(); # and actually, wait right now.
164- release3d();
165- # [press 'q' in the graphics window when done]
157+ nokeeptwiddling3d(); # Don't wait for user while drawing
158+ for (-2,-1,0,1,2) {
159+ line3d [$cx,$cy,$cz+$_]; # ... and corkscrews...
160+ }
161+ keeptwiddling3d(); # Do wait for user while drawing...
162+ twiddle3d(); # and actually, wait right now.
163+ release3d();
164+ # [press 'q' in the graphics window when done]
166165| ],
167166
168167[actnw => q|
169- # The reason for the [] around $x,$y,$z:
170- # 1. You can give all the coordinates and colors in one ndarray.
171- $c = zeroes 3,$size+1;
172- $colors = $coords = sin((3+3*xvals $c)*yvals $c);
173- line3d $coords, $colors; # Draw a curved line, colored
174- # (this works also for lattices, etc.)
175- # [press 'q' in the graphics window when done]
168+ # The reason for the [] around $x,$y,$z:
169+ # 1. You can give all the coordinates and colors in one ndarray.
170+ $c = zeroes 3,$size+1;
171+ $colors = $coords = sin((3+3*xvals $c)*yvals $c);
172+ line3d $coords, $colors; # Draw a curved line, colored
173+ # (this works also for lattices, etc.)
174+ # [press 'q' in the graphics window when done]
176175| ],
177176
178177[actnw => q|
179- # 2. You can use defaults inside the brackets:
180- lattice3d [$z], [$r]; # Note: no $x, $y, and $r is greyscale
181- # [press 'q' in the graphics window when done]
178+ # 2. You can use defaults inside the brackets:
179+ lattice3d [$z], [$r]; # Note: no $x, $y, and $r is greyscale
180+ # [press 'q' in the graphics window when done]
182181| ],
183182
184183[actnw => q|
185- # 3. You can plot in certain other systems as defaults
186- imag3d_ns [POLAR2D, $z], [$r, $g, $b]; # Draw the familiar
187- # bumpy surface in polar
188- # coordinates
189- # [press 'q' in the graphics window when done]
184+ # 3. You can plot in certain other systems as defaults
185+ imag3d_ns [POLAR2D, $z], [$r, $g, $b]; # Draw the familiar
186+ # bumpy surface in polar coordinates
187+ # [press 'q' in the graphics window when done]
190188| ],
191189
192190[actnw => q|
193- # Show graph-evolver
194- use PDL::Graphics::TriD::MathGraph;
195- my @coords = ([0,-1,0], [-1,-1,-2], [3,5,2],
196- [2,1,-3], [1,3,1], [1,1,2]);
197- my $from = PDL->pdl(ulong, [0,1,2,3,4,4,4,5,5,5]);
198- my $to = PDL->pdl(ulong, [1,2,3,1,0,2,3,0,1,2]);
199- my $fromto = $from->t->append($to->t);
200- my @names = map ' '.join(",",@$_), @coords;
201- # Coords must be float, else will get converted and not flow
202- my $e = PDL::GraphEvolver->new(PDL->pdl(float, @coords));
203- $e->set_links($from,$to,PDL->ones(1));
204- my $c = $e->getcoords;
205- my $graph = PDL::Graphics::TriD::get_new_graph(); # also clears
206- hold3d();
207- nokeeptwiddling3d();
208- my $lab = labels3d($c, \@names);
209- my $lin = arrows3d($c, {FromTo => $fromto});
210- my $sph = spheres3d($c);
211- my $ind = 0;
212- while (1) {
213- $e->step();
214- if (++$ind%2 == 0) {
215- $_->data_changed for $lab, $lin, $sph;
216- $graph->scalethings() if (($ind % 200) == 0 or 1);
217- last if twiddle3d();
218- }
191+ # Show graph-evolver
192+ use PDL::Graphics::TriD::MathGraph;
193+ my @coords = ([0,-1,0], [-1,-1,-2], [3,5,2],
194+ [2,1,-3], [1,3,1], [1,1,2]);
195+ my $from = PDL->pdl(ulong, [0,1,2,3,4,4,4,5,5,5]);
196+ my $to = PDL->pdl(ulong, [1,2,3,1,0,2,3,0,1,2]);
197+ my $fromto = $from->t->append($to->t);
198+ my @names = map ' '.join(",",@$_), @coords;
199+ # Coords must be float, else will get converted and not flow
200+ my $e = PDL::GraphEvolver->new(PDL->pdl(float, @coords));
201+ $e->set_links($from,$to,PDL->ones(1));
202+ my $c = $e->getcoords;
203+ my $graph = PDL::Graphics::TriD::get_new_graph(); # also clears
204+ hold3d();
205+ nokeeptwiddling3d();
206+ my $lab = labels3d($c, \@names);
207+ my $lin = arrows3d($c, {FromTo => $fromto});
208+ my $sph = spheres3d($c);
209+ my $ind = 0;
210+ while (1) {
211+ $e->step();
212+ if (++$ind%2 == 0) {
213+ $_->data_changed for $lab, $lin, $sph;
214+ $graph->scalethings() if (($ind % 200) == 0 or 1);
215+ last if twiddle3d();
219216 }
220- keeptwiddling3d();
221- release3d();
222- # [press 'q' in the graphics window when done]
217+ }
218+ keeptwiddling3d();
219+ release3d();
220+ # [press 'q' in the graphics window when done]
223221| ],
224222
225223[actnw => q|
0 commit comments