]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_TimeSensor.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_TimeSensor.m
1 ## Copyright (C) 2005-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_TimeSensor (...)   - Low-level vrml TimeSensor node
17 ##  
18 ##  s is a vrml node with possible fields :
19 ##  ------------------------------------------------------------------
20 ## TimeSensor { 
21 ##   exposedField SFTime   cycleInterval 1       # (0,inf)
22 ##   exposedField SFBool   enabled       TRUE
23 ##   exposedField SFBool   loop          FALSE
24 ##   exposedField SFTime   startTime     0       # (-inf,inf)
25 ##   exposedField SFTime   stopTime      0       # (-inf,inf)
26 ##   eventOut     SFTime   cycleTime
27 ##   eventOut     SFFloat  fraction_changed      # [0, 1]
28 ##   eventOut     SFBool   isActive
29 ##   eventOut     SFTime   time
30 ## }
31 ##  ------------------------------------------------------------------
32 ##
33 ## Options :
34 ## Beyond all the fields of the node, it is also possible to use the option
35 ##
36 ## "DEF", name : The created node will be preceded by 'DEF name ', so that
37 ##               it is further possible to refer to it.
38 ##
39 ## See also : 
40
41 function s = vrml_TimeSensor (varargin)
42
43 verbose = 0;
44
45 tpl = struct ("cycleInterval", "SFTime",\
46 "startTime",     "SFTime",\
47 "stopTime",      "SFTime",\
48 "enabled",       "SFBool",\
49 "loop",          "SFBool"
50 );
51
52 headpar = {};
53 dnode = struct ();
54
55                                 # Transform varargin into key-value pairs
56 i = j = k = 1;                  # i:pos in new varargin, j:pos in headpar,
57                                 # k:pos is old varargin.
58 while i <= length (varargin) && \
59       ! (ischar (varargin{i}) && isfield (tpl, varargin{i}))
60   
61   if j <= length (headpar)
62
63     if verbose
64       printf ("vrml_TimeSensor : Assume arg %i is '%s'\n",k,headpar{j});
65     end
66
67     ##varargin = splice (varargin, i, 0, headpar(j));
68     varargin = {varargin{1:i-1}, headpar(j), varargin{i:end}};
69     j ++; 
70     i += 2;
71     k++;
72   else
73     error ("vrml_TimeSensor : Argument %i should be string, not '%s'",\
74            k,typeinfo (varargin{i}));
75   end
76 end
77
78 DEF = 0;
79
80 if rem (length (varargin), 2), error ("vrml_TimeSensor : Odd n. of arguments"); end
81
82 l = {"TimeSensor {\n"};
83 i = 1;
84 while i < length (varargin)
85
86   k = varargin{i++};    # Read key
87
88   if ! ischar (k)
89     error ("vrml_TimeSensor : Arg n. %i should be a string, not a %s.",\
90            i-1, typeinfo (k));
91   end
92   if ! isfield (tpl, k) && ! strcmp (k,"DEF")
93     error ("vrml_TimeSensor : Unknown field '%s'. Should be one of :\n%s",\
94             k, sprintf ("   '%s'\n",fieldnames (tpl)'{:}));
95   end
96
97   v = varargin{i++};    # Read value
98                                 # Add DEF
99   if strcmp (k,"DEF")
100
101     if verbose, printf ("vrml_TimeSensor : Defining node '%s'\n",v); end
102
103     if DEF, error ("vrml_TimeSensor : Multiple DEFs found"); end
104     l = {sprintf("DEF %s ", v), l{:}};
105     DEF = 1;
106
107   else                          # Add data field
108
109     if verbose
110       printf ("vrml_TimeSensor : Adding '%s' of type %s, with arg of type %s\n",\
111               k,getfield(tpl,k),typeinfo (v));
112     end
113     tmp = getfield(tpl,k);
114     if strcmp (tmp(2:end), "FNode")
115
116       if verbose, printf ("vrml_TimeSensor : Trying to learn type of node\n"); end
117
118       if iscell (v)             # v is list of arguments
119
120                                 # Check whether 1st arg is node type's name.
121         n = v{1};
122
123         if all (exist (["vrml_",tn]) != [2,3,5])
124                                 # If it isn't type's name, use default type.
125           if isfield (dnode, k)
126             if verbose
127               printf ("vrml_TimeSensor : Using default type : %s\n",getfield(dnode,k));
128             end
129             v = {getfield(dnode,k), v{:}};
130           else
131             error ("vrml_TimeSensor : Can't determine type of node '%s'",k);
132           end
133         else
134           if verbose
135             printf ("vrml_TimeSensor : 1st list element is type : %s\n",tn);
136           end
137         end
138                                 # If v is not a list, maybe it has a default
139                                 # node type type (otherwise, it's be sent
140                                 # plain.
141       elseif isfield (dnode, k)
142         if verbose
143           printf ("vrml_TimeSensor : Using default type : %s\n",dnode.(k));
144         end
145         v = {getfield(dnode,k), v{:}};
146       end
147     end
148     l = {l{:}, k, " ", data2vrml(getfield(tpl,k),v),"\n"};
149   end
150   
151 end
152
153 l{end+1} = "}\n";
154
155 s = "";
156 for i=1:numel(l)
157   s = [s, sprintf(l{i})];
158 endfor
159 ### Stupid strcat removes trailing spaces in l's elements
160 ### s = strcat (l{:});
161 endfunction
162