]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_frame.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_frame.m
1 ## Copyright (C) 2002-2012 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_frame (t, r, ...)
17 ##  
18 ## t : 3      Translation                          Default : [0,0,0]
19 ## r : 3x3    Matrix, or                           Default : eye(3)
20 ##     3      Argument for rotv
21 ## OPTIONS
22 ##  name   : size     : function                                   : default
23 ## "scale" : 3 or 1   : Length of frame's branches (including cone)   <1>
24 ## "diam"  : 3 or 1   : Diameter of cone's base
25 ## "col"   : 3 or 3x3 : Color of branches (may be stacked vertically) <[3 4 9]/10>
26 ## "hcol"  : 3 or 3x3 : Color of head     (may be stacked vertically) <col>
27 ##                                                  
28
29 function v = vrml_frame (varargin)
30
31 ### Test with : frame with R,G,B vectors of len 3,2,1 and cone's diam are .2,
32 ### .4, .6.
33 ##
34 ### vrml_browse (vrml_frame ("scale",[3,2,1],"diam",0.2*[1,2,3],"col",eye(3)));
35
36 t = zeros (3,1);
37 r = eye(3);
38
39 scale = ones (1,3);
40 diam = [nan,nan,nan];
41 col = [0.3 0.4 0.9];
42 hcol = [];
43
44 dc = nan(1,3);
45 dr = nan(1,3);
46
47 ######################################################################
48 ## Read options
49 numeric_args = 0;
50 args = nargin;  # nargin is now a function
51 while args && numeric_args<2 && numeric_args<numel(varargin)
52   tmp = varargin{numeric_args + 1};
53   if ischar (tmp), break; end
54   --args;
55   numeric_args++;
56   if numeric_args == 1, t = tmp ; 
57   else                  r = tmp ;  break;
58   end
59 end
60
61 if args
62
63   leftover_args = {varargin{numeric_args+1:end}};       # pos 2.1.39
64
65   verbose = 0;
66
67   ## df = tars (col, hcol, diam, scale, verbose);
68   df = struct ("dc",     dc,    \ # Diameter of cone (absolute)
69                "dr",     dr,    \ # Diameter of rod (absolute)
70                "col",    col,   \ # Color
71                "hcol",   hcol,  \ # Color of head (if different)
72                "diam",   diam,  \ # Diameter of cone relative to branch length
73                "scale",  scale, \ # Lenght of branches
74                "verbose",verbose);
75   op1 = " col hcol diam scale dc dr ";
76   op0 = " verbose ";
77   s = read_options (leftover_args, "op1",op1,"op0",op0,"default",df,"skipnan",1);
78   dc = s.dc;
79   dr = s.dr;
80   col = s.col;
81   hcol = s.hcol;
82   diam = s.diam;
83   scale = s.scale;
84 end
85
86 ######################################################################
87 if isempty (hcol), hcol = col; end
88
89 t = t(:);                       # t : 3x1
90
91 if prod (size (r)) == 3, r = rotv (r); end
92
93 ## col is 3x3 from now on
94 if prod (size (col))==3,   col =   [1;1;1]*col(:)' ; end
95 if prod (size (hcol))==3,  hcol =  [1;1;1]*hcol(:)' ; end
96 if prod (size (diam))==1,  diam =  [1,1,1]*diam ; end
97 if prod (size (scale))==1, scale = [1,1,1]*scale ; end
98
99 scale = scale(:)' ;
100
101 diam = - ones(1,3) * nanmax (scale) / 16;
102 rdiam = - ones(1,3) * nanmax (scale) / 32;
103
104 sz = [scale; nan*ones(1,3); diam; rdiam] ;
105
106 if any (! isnan (dc))
107   ii = find (! isnan (dc));
108   sz(3,ii) = dc(ii);
109 end
110 if any (! isnan (dr))
111   ii = find (! isnan (dr));
112   sz(4,ii) = dr(ii);
113 end
114
115
116 ## roddiam = min (nze (scale))/12 ;
117 ## if roddiam, d = roddiam.*scale ; else d = [nan,nan,nan] ; end
118
119 ## diam = diam.*scale ;
120 ## d = diam = nan*scale;
121 r2 = r;
122 n = sqrt (sum (r2.^2));
123 r2./= [1;1;1] * n;
124 sz(1,:) .*= n;
125 sz(3,:) ./= n;
126 sz(4,:) ./= n;
127
128 tmp = [r2(:,1), null(r2(:,1)')](:,[2,1,3])';
129 if det (tmp) < 0, tmp(3,:) *= -1; end
130 a1 = vrml_transfo (vrml_arrow(sz(:,1),[col(1,:);hcol(1,:)],0),\
131                    [0,0,0],tmp);
132 ## keyboard
133 tmp = [r2(:,2), null(r2(:,2)')](:,[2,1,3])';
134 if det (tmp) < 0, tmp(3,:) *= -1; end
135 a2 = vrml_transfo (vrml_arrow(sz(:,2),[col(2,:);hcol(2,:)],0),\
136                    [0,0,0],tmp);
137
138 tmp = [r2(:,3), null(r2(:,3)')](:,[2,1,3])';
139 if det (tmp) < 0, tmp(3,:) *= -1; end
140 a3 = vrml_transfo (vrml_arrow(sz(:,3),[col(3,:);hcol(3,:)],0),\
141                    [0,0,0],tmp);
142
143 f0 = vrml_group (a1, a2, a3);
144 v = vrml_transfo (f0, t, nan);
145
146
147 endfunction
148