]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_arrow.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_arrow.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 ## s = vrml_arrow (sz, col)     - Arrow pointing in "y" direction
17 ##
18 ## INPUT :
19 ## -------
20 ## Arguments are optional. NaN's are replaced by default values.
21 ##                                                         <default>
22 ## sz = [len, alen, dc, dr] has size 1, 2, 3 or 4, where
23 ##
24 ##   len  : total length                                   <1>
25 ##   alen : If positive: length of cone/total length       <1/4>
26 ##          If negative: -(length of cone)
27 ##   dc   : If positive: diameter of cone base/total len   <1/16>
28 ##          If negative: -(diameter of cone) 
29 ##   dr   : If positive: diameter of rod/total length      <min(dc, len/32)>
30 ##          If negative: -(diameter of rod) 
31 ##
32 ## col    : 3 or 3x2 : Color of body and cone              <[0.3 0.4 0.9]>
33 ##
34 ## OUTPUT :
35 ## --------
36 ## s      : string   : vrml representation of an arrow (a rod and a cone)
37
38 function v = vrml_arrow (sz, col, emit)
39
40 if nargin < 3, emit = 1; end
41
42 if nargin<2  || isempty (col),    col = [0.3 0.4 0.9;0.3 0.4 0.9];
43 elseif prod (size (col)) == 3,    col = [1;1]*col(:)';
44 elseif all (size (col) == [3,2]), col = col';
45 elseif any (size (col) != [2,3]),
46   error("vrml_arrow : col has size %dx%d (should be 3 or 3x2)\n",size(col));
47   ## keyboard
48 end
49 col = col' ;
50
51 s0 = [1, 1/4, 1/16, 1/32];
52                                 # Get absolute size
53 if nargin<1 || isempty (sz) || isnan (sz),  sz = s0 ;
54 elseif length (sz) == 4,      sz = [sz(1),sz(2),sz(3),sz(4)];
55 elseif length (sz) == 3,      sz = [sz(1),sz(2),sz(3),s0(4)];
56 elseif length (sz) == 2,      sz = [sz(1),sz(2),s0(3),s0(4)];
57 elseif length (sz) == 1,      sz = [sz(1),s0(2),s0(3),s0(4)];
58 else 
59   error ("vrml_arrow : sz has size %dx%d. (should be in 1-4)\n", size(sz));
60   ## keyboard
61 end
62
63 assert (sz(1) > 0)
64
65 if !isempty (tmp = find(isnan(sz))), sz(tmp) = s0(tmp) ; end
66
67 for i = 2:4
68   if sz(i) >= 0
69         sz(i) *= sz(1);
70   else
71         sz(i) = -sz(i);
72   endif
73 endfor
74
75                                 # Do material nodes
76 smat1 = vrml_material (col(:,1), emit);
77 smat2 = vrml_material (col(:,2), emit);
78
79 v = sprintf (["Group {\n",\
80               "  children [\n",\
81               "    Transform {\n",\
82               "      translation  %8.3g %8.3g %8.3g\n",\
83               "      children [\n",\
84               "        Shape {\n",\
85               "          appearance Appearance {\n",\
86               smat1,\
87               "          }\n"\
88               "          geometry Cylinder {\n",\
89               "            radius %8.3g\n",\
90               "            height %8.3g\n",\
91               "          }\n",\
92               "        }\n",\
93               "      ]\n",\
94               "    }\n",\
95               "    Transform {\n",\
96               "      translation  %8.3g %8.3g %8.3g\n",\
97               "      children [\n",\
98               "        Shape {\n",\
99               "          appearance Appearance {\n",\
100               smat2,\
101               "          }\n",\
102               "          geometry Cone { \n",\
103               "            bottomRadius  %8.3g \n",\
104               "            height  %8.3g\n",\
105               "          }\n",\
106               "        }\n",\
107               "      ]\n",\
108               "    }\n",\
109               "  ]\n",\
110               "}\n"],\
111              [0,(sz(1)-sz(2))/2,0],\
112              sz(4),\
113              sz(1)-sz(2),\
114              [0,sz(2)/2+sz(1)-sz(2),0],\
115              sz(3),\
116              sz(2));
117 endfunction
118