]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_browse.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_browse.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ##  p = vrml_browse ([s])       - View vrml code s with FreeWRL
17 ##      vrml_browse ("-kill")   - Kill the browser
18 ##
19 ## s : string : VRML code, as returned by the vrml_XYZ functions. 
20 ##              If  s  is not specified, a sombrero is showed
21 ##
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.
25 ##
26 ## Some keystrokes for FreeWRL (more in the freewrl manpage) :
27 ##
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'
33 ## 'q'   : Quit
34 ## 
35 ## WARNING : FreeWRL >0.25 (http://www.crc.ca/FreeWRL/) must be installed.
36 ##
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
40 ##           "-kill".
41
42 function p = vrml_browse (varargin)
43
44 verbose = 0;
45
46 best_option = " ";
47                                 # Allow scripting and specify where
48                                 # browser's output goes 
49 ##out_option = "--ps --psout /tmp/octave_browser_out.txt " ;
50 out_option = " " ;
51 geo_option = " " ;
52 global vrml_b_pid = 0;
53 global vrml_b_name = [];
54
55 p = vrml_b_pid ;
56 bop = "";
57 go_to_bg = 0;
58
59 if nargin<1
60   s = "";
61 else
62   i = 1;
63   while i <= length (varargin)
64     o = varargin{i++};
65     if strcmp (o, "-kill")
66       vrml_kill(); return;
67     elseif strcmp (o, "-bop")   # Browser options
68       bop = [bop," ",varargin{i++}," "];
69     elseif strcmp (o, "-bg")    # Browser options
70       go_to_bg = 1;
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);
76       else
77         geo_option = sprintf ("--geometry %s",geo_option);
78       end
79     end
80   end
81   s = varargin{length (varargin)};
82 end
83
84 if ! index (s, "Background") 
85   s = [s, vrml_Background("skyColor",[.7 .7 .9])];
86 end
87
88
89 vrml_b_name = "whitedune" ;
90 ##vrml_b_name = "/home/etienne/bin/my_freewrl.sh";
91
92 ##b_opt = [out_option," ",bop," ",best_option," --server --snapb octave.snap "]
93 ##;
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] ;
97
98
99 b_temp = "/tmp/octave_vrml_output.wrl" ;
100 b_log  = " &> /tmp/octave_vrml_browser.log";
101
102 new_browser = 0 ;
103                                 # ####################################
104 if vrml_b_pid > 0               # There's already a browser ##########
105
106                                 # Check that browser is really alive
107
108   [status, dum] = system (sprintf ("kill -CONT %d &> /dev/null",vrml_b_pid));
109   
110   if ! status
111     if verbose
112       printf ( "vrml_browse : browser pid=%d is still alive\n",vrml_b_pid);
113     end
114   else 
115     if verbose
116       printf ( ["vrml_browse : ",\
117                 "browser pid=%d died. I'll spawn another\n"], vrml_b_pid);
118     end
119     vrml_b_pid = -1 ;
120   end
121 end                             # End of check if old browser's alive 
122                                 # ####################################
123
124                                 # ####################################
125                                 # Check that temp file exists ########
126 [status, dum] = system (["test -e ",b_temp]);
127 if !length (s)
128
129   if verbose
130     printf( "vrml_browse : Trying to create temp file\n");
131   end
132                                 # Do a sombrero
133   n = 30 ;
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);
138   x /= 4 ; y /= 4 ;
139
140   ##  pix = ones (n) ;
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 ];
146
147   ##    rtmp = rows(tmp)+2;
148   ##    for i=1:rtmp:n-rtmp,
149   ##      pix(i+[1:rows(tmp)],1:columns(tmp)) = tmp ;
150   ##    end
151   ##    pix = flipud (pix);
152   ##    col = [1;1;1]*pix(:)' ;
153   ## keyboard
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 );
160 end
161
162
163 save_vrml (b_temp, s); # "nobg", s) ;
164                                 # End of preparing temp file #########
165                                 # ####################################
166
167                                 # ####################################
168                                 # Eventually start browser ###########
169 if vrml_b_pid <= 0
170   new_browser = 1 ;
171   if verbose, 
172     printf( "vrml_browse : spawning browser ...\n");
173   end
174   ## keyboard
175                                 # Starting a background browser : 
176                                 # 
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
182                                 #        process has exited
183                                 # 
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.
189                                 # Phew!  
190   if 1
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);
194     ##  cmd
195     if status,
196     
197       printf("vrml_browse : Can't start browser '%s'. Is it installed?\n",\
198              vrml_b_name);
199       p = vrml_b_pid ;
200       return ;
201     else
202
203       vrml_b_pid = -1;
204       server_works = 0;
205     end
206     if server_works
207       vrml_b_pid = str2num (out);
208     end
209
210     if verbose, printf( "vrml_browse : OK\n"); end
211   end
212
213 end                             # End of starting new browser ########
214                                 # ####################################
215
216 if (!new_browser) && (vrml_b_pid > 1)
217                                 # ####################################
218                                 # Send USR1 signal to browser to tell it to
219                                 # raise itself.
220   [status, dum] = system (sprintf ("kill -USR1 %d &> /dev/null",vrml_b_pid));
221
222   if status,  
223     printf ("vrml_browse : browser pid=%d can't be signaled\n",vrml_b_pid);
224   else 
225     if verbose, 
226       printf( ["vrml_browse : USR1 sent to browser pid=%d\n"], vrml_b_pid);
227     end
228   end
229 end                             # End of signaling  browser ##########
230                                 # ####################################
231
232 p = vrml_b_pid ;
233 endfunction
234