]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_ellipsoid.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_ellipsoid.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_ellipsoid (moment, col) - Ellipsoid 
17 ##
18 ## moment : 3x3 : Define elipsoid by x'*moment*x = 1
19 ##  or      3   :  use diag(moment)
20 ##  or      1   :  use diag(moment([1,1,1]))   default : eye(3)
21 ##
22 ## col    : 3   : Color                        default : [0.3 0.4 0.9]
23 ##
24
25 function v = vrml_ellipsoid(moment, col)
26
27
28 if nargin<2  || isempty (col) || isnan (col), col = [0.3 0.4 0.9];
29 else
30   error ("vrml_ellipsoid : col has size %d x %d. This won't do\n",size(col));
31   ## keyboard
32 end
33 col = col' ;
34
35
36 if nargin<1 || isempty (moment),  moment = eye(3),
37 elseif all (size (moment) == 3), # Do nothing
38 elseif prod( size (moment)) == 3, moment = diag (moment);
39 elseif length (moment) == 1,      moment = moment*eye(3);
40 else 
41   error ("vrml_ellipsoid : moment has size %d x %d. This won't do\n",size(moment));
42   ## keyboard
43 end
44
45 [u,d,v] = svd (moment);
46 d = diag(d);
47 d(find(d)) = 1 ./ nze (d) ;
48
49 [r,a] = rotparams (u');
50
51 v = sprintf (["Transform {\n",\
52               "  translation  0 0 0\n",\
53               "  rotation     %8.3g %8.3g %8.3g %8.3g\n",\
54               "  scale        %8.3g %8.3g %8.3g\n",\
55               "  children [\n",\
56               "    Shape {\n",\
57               "      appearance  Appearance {\n",\
58               "        material Material {\n",\
59               "          diffuseColor  %8.3g %8.3g %8.3g\n",\
60               "          emissiveColor %8.3g %8.3g %8.3g\n",\
61               "        }\n",\
62               "      }\n",\
63               "      geometry Sphere {}\n",\
64               "    }\n",\
65               "  ]\n",\
66               "}\n",\
67               ],\
68              r,a,\
69              d,\
70              col,\
71              col/20);
72
73 endfunction
74 ## Example
75 ## d0 = sort (1+2*rand(1,3));
76 ## d0 = 1:3;
77 ## rv = randn (1,3); 
78 ## uu = rotv (rv);
79 ## dd = diag (1./d0);
80 ## mm = uu*dd*uu';
81 ## vrml_browse ([vrml_frame ([0,0,0],uu',"col",eye(3),"scale",2*d0), vrml_ellipsoid(mm)]);