1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## p = vrml_browse ([s]) - View vrml code s with FreeWRL
17 ## vrml_browse ("-kill") - Kill the browser
19 ## s : string : VRML code, as returned by the vrml_XYZ functions.
20 ## If s is not specified, a sombrero is showed
22 ## p : int : pid of the current browser. If freewrl has not been started
23 ## or has died, a new one is started. p is zero or negative in
24 ## case of failure to start freewrl.
26 ## Some keystrokes for FreeWRL (more in the freewrl manpage) :
28 ## 'e' : Examine : mouse 1 and drag rotates the scene
29 ## mouse 3 and drag moves closer/farther
30 ## 'w' : Walk : mouse 1 and drag moves for/backward, turns
31 ## mouse 3 and drag translates parallel to the screen
32 ## 's' : Save a snapshot in files 'octave.snapshot.NNNN.ppm'
35 ## WARNING : FreeWRL >0.25 (http://www.crc.ca/FreeWRL/) must be installed.
37 ## BUG : The vrml browser is not killed when octave exits. Sometimes the
38 ## vrml browser does not get raised or gets raised improperly
39 ## (shows the contents of the topmost window above it). Use
42 function p = vrml_browse (varargin)
47 # Allow scripting and specify where
48 # browser's output goes
49 ##out_option = "--ps --psout /tmp/octave_browser_out.txt " ;
52 global vrml_b_pid = 0;
53 global vrml_b_name = [];
63 while i <= length (varargin)
65 if strcmp (o, "-kill")
67 elseif strcmp (o, "-bop") # Browser options
68 bop = [bop," ",varargin{i++}," "];
69 elseif strcmp (o, "-bg") # Browser options
71 elseif strcmp (o, "-geometry") # Browser options
72 geo_option = varargin{i++};
73 if !ischar (geo_option)
74 assert (length (geo_option) == 2)
75 geo_option = sprintf ("--geometry %ix%i",geo_option);
77 geo_option = sprintf ("--geometry %s",geo_option);
81 s = varargin{length (varargin)};
84 if ! index (s, "Background")
85 s = [s, vrml_Background("skyColor",[.7 .7 .9])];
89 vrml_b_name = "whitedune" ;
90 ##vrml_b_name = "/home/etienne/bin/my_freewrl.sh";
92 ##b_opt = [out_option," ",bop," ",best_option," --server --snapb octave.snap "]
94 ##b_opt = [out_option," ",bop," ",best_option," --server "] ;
95 ##b_opt = [out_option," ",bop," ",best_option," --snapb octave_freewrl_snapshots "] ;
96 b_opt = [out_option," ",bop," ",best_option, " ",geo_option] ;
99 b_temp = "/tmp/octave_vrml_output.wrl" ;
100 b_log = " &> /tmp/octave_vrml_browser.log";
103 # ####################################
104 if vrml_b_pid > 0 # There's already a browser ##########
106 # Check that browser is really alive
108 [status, dum] = system (sprintf ("kill -CONT %d &> /dev/null",vrml_b_pid));
112 printf ( "vrml_browse : browser pid=%d is still alive\n",vrml_b_pid);
116 printf ( ["vrml_browse : ",\
117 "browser pid=%d died. I'll spawn another\n"], vrml_b_pid);
121 end # End of check if old browser's alive
122 # ####################################
124 # ####################################
125 # Check that temp file exists ########
126 [status, dum] = system (["test -e ",b_temp]);
130 printf( "vrml_browse : Trying to create temp file\n");
134 x = y = linspace (-8, 8, n)';
135 [xx, yy] = meshgrid (x, y);
136 r = sqrt (xx .^ 2 + yy .^ 2) + eps;
137 z = 3*sin (r) ./ (2*r);
141 ## tmp = [1 0 0 1 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 0 ;\
142 ## 0 1 1 0 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 0 1 1 0 1 1 ;\
143 ## 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 0 1 1 0 0 0 ;\
144 ## 0 1 1 0 1 0 1 1 0 1 1 1 0 1 1 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 ;\
145 ## 1 0 0 1 1 1 0 0 1 1 1 1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 1 0 0 0 ];
147 ## rtmp = rows(tmp)+2;
148 ## for i=1:rtmp:n-rtmp,
149 ## pix(i+[1:rows(tmp)],1:columns(tmp)) = tmp ;
151 ## pix = flipud (pix);
152 ## col = [1;1;1]*pix(:)' ;
154 rmat = [0.90000 -0.38730 0.20000;
155 0.38730 0.50000 -0.77460;
156 0.20000 0.77460 0.60000];
157 ## s = vrml_points ([x(:),y(:),z(:)],"balls");
158 ## s = vrml_transfo (vrml_thick_surf (x,y,z, "col",col), [0.25,0,0],rmat);
159 s = vrml_transfo (vrml_thick_surf (x,y,z), [0.25,0,0],rmat );
163 save_vrml (b_temp, s); # "nobg", s) ;
164 # End of preparing temp file #########
165 # ####################################
167 # ####################################
168 # Eventually start browser ###########
172 printf( "vrml_browse : spawning browser ...\n");
175 # Starting a background browser :
177 # popen2 seems broken.
178 # popen had some problem, can't recall what
179 # system "async" does not give me pid of
180 # browser, but pid of shell
181 # system "sync" only returns when child
184 # So my solution is : "system" a process that
185 # forks. Its child is a browser. The parent
186 # prints the child's pid (given by fork())
187 # and exits. Octave reads the output of the
188 # system call, which is the browser's pid.
191 cmd = [vrml_b_name," ",b_opt," ",b_temp," "]
192 ## [status, out] = system (cmd = [vrml_b_name," ",b_opt," \"file:",b_temp,"\""], 1);
193 [status, out] = system (cmd = [vrml_b_name," ",b_opt," ",b_temp], 1);
197 printf("vrml_browse : Can't start browser '%s'. Is it installed?\n",\
207 vrml_b_pid = str2num (out);
210 if verbose, printf( "vrml_browse : OK\n"); end
213 end # End of starting new browser ########
214 # ####################################
216 if (!new_browser) && (vrml_b_pid > 1)
217 # ####################################
218 # Send USR1 signal to browser to tell it to
220 [status, dum] = system (sprintf ("kill -USR1 %d &> /dev/null",vrml_b_pid));
223 printf ("vrml_browse : browser pid=%d can't be signaled\n",vrml_b_pid);
226 printf( ["vrml_browse : USR1 sent to browser pid=%d\n"], vrml_b_pid);
229 end # End of signaling browser ##########
230 # ####################################