]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_points.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_points.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_points(x,options)
17 ## 
18 ## x : 3xP  : 3D points
19 ##
20 ## Makes a vrml2 "point [ ... ]" node from a 3xP matrix x.
21 ## 
22 ## OPTIONS (name and size/type, if applicable):
23 ## ---------------------------------------
24 ## "balls"      : Displays spheres rather than points. Overrides the
25 ##                "hide" options and no Coordinate node is defined;makes
26 ##                "name" ineffective.
27 ##
28 ## "boxes" or
29 ## "cubes"      : Displays cubes rather than points. Overrides the "hide"
30 ##                options and no Coordinate node is defined;makes "name"
31 ##                ineffective. 
32 ##
33 ## "rad", 1 or P: Radius of balls/size of cubes.              default = 0.1
34 ##
35 ## "nums"       : Displays numbers rather than points. Overrides the
36 ##                "hide" options and no Coordinate node is defined;
37 ##                makes "name" ineffective.
38 ##
39 ##       WARNING : This option seems to make freewrl 0.34 hang, so that it
40 ##                 is necessary to kill it (do vrml_browse ("-kill")). Other
41 ##                 browsers can can view the code produced by this option.
42 ##
43 ##  "col", 3x1  : Points will have RGB col.          default = [0.3,0.4,0.9]
44 ##      or 3xP  : The color of each point.
45 ##  "tran", 1x1 : Transparency                                   default = 0
46 ##  "emit", e   : Use or not emissiveColor                       default = 1
47 ##
48 ## "name", str  : The Coordinate node will be called name
49 ##                (default="allpoints").
50 ## "hide"       : The points will be defined, but not showed.
51
52 function s = vrml_points(x,varargin)
53 ## varargin
54 hide = 0;
55 cubes = balls = nums = 0;
56 rad = 0.1 ;
57 name = "allpoints" ;
58 col = [0.3 0.4 0.9];
59 emit = 1;
60 tran = 0;
61
62 i = 1;                  # pos 2.1.39
63
64 while i <= nargin-1
65
66   tmp = varargin{i++};
67   if strcmp(tmp,"hide") ,
68     hide = 1;
69   elseif strcmp(tmp,"balls") ,
70     balls = 1;
71   elseif strcmp(tmp,"cubes") || strcmp(tmp,"boxes") ,
72     cubes = 1;
73   elseif strcmp(tmp,"rad") ,
74
75     rad = varargin{i++};
76   elseif strcmp(tmp,"nums") ,
77     nums = 1;
78   elseif strcmp(tmp,"emit") ,
79
80     emit = varargin{i++};
81   elseif strcmp(tmp,"col") ,
82
83     col = varargin{i++};
84   elseif strcmp(tmp,"name") ,
85
86     name = varargin{i++};
87   elseif strcmp(tmp,"tran") ,
88     tran = varargin{i++};
89   end
90 end
91
92 if rows (x) != 3,
93   if columns (x) == 3,
94     x = x' ;
95   else
96     error ("vrml_points : input is neither 3xP or Px3\n");
97     ## keyboard
98   end
99 end
100 P = columns (x) ;
101                                 # Produce a PointSet
102 if !balls && !cubes && !nums,
103
104   if prod (size (col)) == 3*P   # One color per point
105     smat = "";
106     scol = sprintf ("  color Color { color [\n   %s]\n   }\n",\
107                     sprintf ("      %8.3f %8.3f %8.3f\n", col));
108   else                          # One color
109     smat = ["  appearance Appearance {\n",\
110             vrml_material(col, emit),"\n",\
111             "  }\n"];
112     scol = "";
113   end
114
115   s = sprintf(["Shape {\n",\
116                smat,\
117                "  geometry PointSet {\n",\
118                scol,\
119                "  coord DEF %s Coordinate {\n  point [\n  " ],name); # ] 
120   
121   
122   s0 = sprintf("%10.6g %10.6g %10.6g ,\n  ",x);
123   
124   s = sprintf("%s%s]\n  }\n  }\n  }\n",s,s0); # [
125   
126   if hide, s = ["Switch {\nchoice\n[\n",s,"\n]\n}"]; end
127
128                                 # Use numbers
129 elseif nums,
130   printf ("Foo\n");
131   s = "";
132   if prod (size (col)) == 3, col = col(:) * ones (1,P); end
133   for i = 1:P,
134     s0 = sprintf([\
135                   "Transform {\n",\
136                   "  translation %10.6g %10.6g %10.6g\n",\
137                   "  children [\n",\ # ]
138                   "    Billboard {\n",\
139                   "      children [\n",\ # ]
140                   "        Shape {\n",\
141                   "          appearance Appearance {\n",\
142                   vrml_material(col(:,i), emit, tran),"\n",\
143                   "          }\n",\
144                   "          geometry Text {\n",\
145                   "            string \"%s\"\n",\
146                   "            fontStyle FontStyle { size 0.25 }\n",\
147                   "          }\n",\
148                   "        }\n",\
149                   "      ]\n",\
150                   "    }\n",\
151                   "  ]\n",\
152                   "}\n"],\ # [
153                  x(:,i),sprintf("%d",i-1));
154                  ## x(:,i),col,col,sprintf("%d",i-1));
155     ## "              emissiveColor %8.3f %8.3f %8.3f\n",\
156     ## "      axisOfRotation 0.0 0.0 0.0\n",\ 
157
158     s = sprintf("%s%s",s,s0);
159   end
160 else
161                                 # If all radiuses are the same, do a single
162                                 # geometry node for all points
163   if all (size (rad) == 1) || ! any (abs (diff (rad))>eps)
164     all_same_rad = 1;
165     if balls, shapestr = sprintf("Sphere { radius %8.3f}",rad) ; 
166     else      shapestr = sprintf("Box { size %8.3f %8.3f %8.3f}",rad,rad,rad) ;
167     end
168   else
169     all_same_rad = 0;
170   end
171
172                                 # If all colors are the same, do a single
173                                 # geometry node for all points
174   if prod (size (col)) == 3 || ! any (abs (diff (col'))>eps)
175     all_same_col = 1;  
176     colorstr = vrml_material (col(1:3), emit);
177   else
178     all_same_col = 0;
179   end
180
181   s = "";
182   for i = 1:P
183                                 # If some radiuses differ, I must do
184                                 # geometry nodes individually
185     if ! all_same_rad
186                                 # Skip zero-sized objects
187       if ! rad(:,i), continue; end
188
189       if balls
190         shapestr = sprintf("Sphere { radius %8.3f}",rad(:,i));
191       else
192         shapestr = sprintf("Box { size %8.3f %8.3f %8.3f}",rad(:,[i,i,i]));
193       end
194     end
195                                 # If some colors differ, I must do material
196                                 # nodes individually
197     if ! all_same_col, colorstr = vrml_material (col(:,i), emit); end
198
199     s0 = sprintf([\
200                   "Transform {\n",\
201                   "  translation %10.6g %10.6g %10.6g\n",\
202                   "  children [\n",\ # ]
203                   "    Shape {\n",\
204                   "      appearance Appearance {\n",\
205                   colorstr,"\n",\
206                   "      }\n",\
207                   "      geometry ",shapestr,"\n",\
208                   "    }\n",\
209                   "  ]\n",\
210                   "}\n"],\
211                  x(:,i));
212     ## "          emissiveColor %8.3f %8.3f %8.3f\n",\
213     ##           x(:,i),col,col,shape);
214     s = sprintf("%s%s",s,s0);
215   end
216 end
217 endfunction
218
219