]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_cyl.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_cyl.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 ##       s = vrml_cyl(x,...) 
17 ##
18 ## Makes a cylinder that links x(:,1) to x(:,2) 
19 ## 
20 ## Options : 
21 ##
22 ## "tran", transparency    : Transparency                  default = 0
23 ## "col" , col             : Color           default = [ 0.3 0.4 0.9 ]
24 ## "rad" , radius          : Radius of segments         default = 0.05
25 ## "balls"                 : Add balls to extremities
26 ## "brad"                  : Radius of balls             default = rad
27 ## "emit", bool            : Use or not emissiveColor
28 ## "noemit"                : Same as emit,0
29 ## "arrow"                 : Last segment is an arrow 
30 ## "hcol", hcol            : Set color of the head of the arrow.
31 ##                                                       default = col
32
33 function s = vrml_cyl (x,varargin)      #  pos 2.1.39 
34
35 rad = 0.05 ;
36 tran = 0 ;
37 col = [0.3;0.4;0.9] ;
38 hcol = nan;
39 brad = nan;
40
41 verbose = 0 ;
42 balls = 0 ;
43 emit = 0;
44 noemit = nan;
45 arrow = 0; 
46
47 if nargin > 1
48   op1 = " rad tran col hcol brad emit " ;
49   op0 = " verbose balls noemit arrow " ;
50
51   df = tars (rad, tran, col, hcol, verbose, balls, noemit, arrow, brad, \
52             emit);
53
54
55   s = read_options (varargin, "op1",op1,"op0",op0, "default",df); # pos 2.1.39
56
57   rad=     s.rad;
58   tran=    s.tran;
59   col=     s.col;
60   hcol=    s.hcol;
61   emit=    s.emit;
62   verbose= s.verbose;
63   balls=   s.balls;
64   noemit=  s.noemit;
65   arrow=   s.arrow;
66   brad=    s.brad;
67 end
68
69 if isnan (brad), brad = rad; end
70 if !isnan (noemit), emit = ! noemit; end
71 if !isnan (hcol), arrow = 1; end
72
73 s = "" ;
74
75 N = columns (x);
76
77                                 # Make col 3xN
78 if prod (size (col)) == 3, col = col(:); col = col(:, ones(1,N)); end
79 if emit, emitcol = col; else emitcol = nan(1,N); end
80 if prod (size (tran)) == 1, tran = tran(ones(1,N)); end
81 tran(find (tran==0)) = nan ;
82
83 for i = 2:N
84   d = x(:,i)-x(:,i-1) ;
85   n = norm(d) ;
86   if n,
87     d = d/n ;
88
89     ax = cross([0,1,0],d') ;
90     an = norm (ax) ;
91     if abs(an)>eps, ax = ax/an ; else ax = [1,0,0] ; end
92     an = acos(d(2)) ;
93
94     t = mean (x(:,[i,i-1])')' ;
95
96     if ! arrow || i != N
97
98       smat = vrml_material (col(:,i-1), emitcol(:,i-1), tran(i-1));
99                                 # Do a cylinder
100       s = [s,sprintf(["Transform {\n",\
101                       "  translation %8.3f %8.3f %8.3f\n",\
102                       "  children [\n",\
103                       "    Transform {\n",\
104                       "      rotation    %8.3f %8.3f %8.3f %8.3f\n",\
105                       "      children [\n",\
106                       "        Shape {\n",\
107                       "          appearance Appearance {\n",\
108                       "              %s",\
109                       "          }\n",\
110                       "          geometry Cylinder {\n",\
111                       "            height %8.3f\n",\
112                       "            radius %8.3f\n",\
113                       "          }\n",\
114                       "        }\n",\
115                       "      ]\n",\
116                       "    }\n",\
117                       "  ]\n",\
118                       "}\n"],\
119                      t,\
120                      ax,an,\
121                      smat,\
122                      n,\
123                      rad)];
124     else
125       t = x(:,i-1) ;
126       if isnan (hcol), hcol = col(:,i); end
127       arrowcol = [col(:,i) hcol(1:3)(:)];
128       s = [s,\
129            vrml_transfo(vrml_arrow ([n,nan,2*rad/n,rad/n],arrowcol,emit),\
130                         t, ax*an)];
131     end
132   end
133 end
134
135 if balls,
136   ## "balls on"
137   s = [s, vrml_points(x,"balls","col",col, "rad",brad,"emit",emit)];
138 elseif columns(x)>2,
139                                 # Make a rounded junction
140   s = [s, vrml_points(x(:,2:columns(x)-1),"balls","col",col,"rad",rad, \
141                       "emit", emit)];
142 end
143
144
145 endfunction
146