]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_transfo.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_transfo.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 ##       v = vrml_transfo(s,t,r,c,d)
17 ##  
18 ## s : string of vrml code.
19 ## t : 3      Translation                          default : [0,0,0]
20 ## r : 3x3    Rotation matrix, or                  default : eye(3)
21 ##     3      Scaled rotation axis.
22 ## c : 3 or 1 Scale                                default : 1
23 ## d : string DEF name                             default : ''
24 ##
25 ## v : string v is s, enclosed in a Transform {} vrml node with
26 ##     rotation, translation and scale params given by r, t and c.
27 ##
28
29 function v = vrml_transfo(s,t,r,c,DEF)
30
31 ## Hint : if s is "%s", you may later do v2 = sprintf (v,s2) which should be
32 ##        equal to  vrml_transfo (s2,etc)
33
34 verbose = 0;
35 ## 
36 t0 = [0;0;0];
37 r0 = [1,0,0,0];
38 c0 = [1;1;1];
39 if nargin<2 || isnan (t), t = t0; end # Default translation
40 if nargin<3 || isnan (r), r = r0; end # Default rotation
41 if nargin<4 || isnan (c), c = c0; end # Default scale
42 if nargin<5, DEF = ""; end
43
44 if verbose > 1
45   if nargin > 1, printf ("vrml_transfo : t   = %s\n",sprintf ("%f ",t)); end
46   if nargin > 2, printf ("vrml_transfo : r   = %s\n",sprintf ("%f ",r)); end
47   if nargin > 3, printf ("vrml_transfo : c   = %s\n",sprintf ("%f ",c)); end
48   if nargin > 4, printf ("vrml_transfo : DEF = %s\n",DEF); end
49 end
50 t = t(:);
51 c = c(:);
52 ## if nargin<4, s = "%s" ; end
53
54 if prod(size(t)) != 3, error("t has %i elements, not 3",prod(size(t))); end
55
56
57 if prod(size(c))==1, c = [c;c;c]; end
58
59 if all(size(r) == 3)
60   if abs (det (r) - 1) > sqrt (eps), r2 = orth (r);
61   else                               r2 = r;
62   end
63   [axis,ang] = rotparams (r2);
64 elseif prod(size(r)) == 4
65   ang = r(4);
66   axis = r(1:3);
67 elseif prod(size(r)) == 3
68   ang = norm(r);
69   if abs(ang)>eps, 
70     axis = r/ang;
71   else
72     axis = [0,1,0]; ang = 0;
73   end
74 else
75   error ("vrml_transfo : rotation should have size 3x3, 3 or 4\n");
76 end
77 if verbose,
78   printf (["vrml_transfo : %8.3f %8.3f %8.3f %8.3f\n",\
79            "               %8.3f %8.3f %8.3f\n"],\
80           axis,ang,t);
81   printf ("length of string is %i\n",prod(size(s)));
82 end
83
84                                 # Indent s by 4
85 if length (s) && strcmp(s(prod(size(s))),"\n") # chomp
86   s = s(1:prod(size(s))-1) ;
87 end
88 ## strrep is slow, as if it copied everything by hand. So don't indent.
89 #  mytic() ;
90 #  s = ["    ",strrep(s,"\n","\n    ")] ;
91 #  mytic()
92 if verbose, printf ("   done indenting s\n"); end
93
94 sr = st = ss = sd = "";
95 if abs (ang) > sqrt (eps)
96   sr = sprintf ("  rotation    %8.3f %8.3f %8.3f %8.3f\n",axis,ang);
97 end
98 if any (abs (t - t0)>sqrt (eps))
99   st = sprintf ("  translation %8.3f %8.3f %8.3f\n",t);
100 end
101 if any (abs (c - c0)>sqrt (eps))
102   ss = sprintf ("  scale       %8.3f %8.3f %8.3f\n",c);
103 end
104 if !isempty (DEF), sd = ["DEF ",DEF," "]; end 
105
106 v = sprintf([sd,"Transform {\n",sr,st,ss,\
107              "  children [\n%s\n",\
108              "           ]\n",\
109              "}\n",\
110              ],\
111             s) ;
112 ## keyboard
113
114 endfunction
115