]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_Viewpoint.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_Viewpoint.m
1 ## Copyright (C) 2010 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_Viewpoint (...)   - Vrml Viewpoint node
17 ##  
18 ##  s is a string of the form :
19 ##  ------------------------------------------------------------------
20 ## Viewpoint { 
21 ##    eventIn      SFBool     set_bind
22 ##    exposedField SFFloat    fieldOfView    0.785398  # (0,pi)
23 ##    exposedField SFBool     jump           TRUE
24 ##    exposedField SFRotation orientation    0 0 1 0   # [-1,1],(-pi,pi)
25 ##    exposedField SFVec3f    position       0 0 10    # (-,)
26 ##    field        SFString   description    ""
27 ##    eventOut     SFTime     bindTime
28 ##    eventOut     SFBool     isBound
29 ##  }
30 ##  ------------------------------------------------------------------
31 ##
32 ## Options :
33 ## All the fields of the node
34 ##
35 ## Example : s = vrml_Viewpoint ("location",[0 0 1]);
36 ##
37 ## See also : vrml_DirectionalLight
38
39 function s = vrml_Viewpoint (varargin)
40
41 if mod(nargin,2) != 0, print_usage; end
42
43 h = struct (varargin{:});
44  
45 tpl = struct ("fieldOfView",      "%8.3f",\
46               "jump",             "%s",\
47               "orientation",      "%8.3f %8.3f %8.3f %8.3f",\
48               "orientation0",     "%8.3f %8.3f %8.3f %8.3f",\
49               "position",         "%8.3f %8.3f %8.3f",\
50               "description",      "\"%s\"",\
51               "DEF",              "");
52 DEF = "";
53 defaultPos = [0 0 10];
54
55 for [val,key]  = h
56   if ! isfield (tpl, key)
57     error (sprintf ("vrml_Viewpoint : unknown field '%s'",key));
58   end
59 end
60 if isfield (h, "DEF")
61   DEF = h.DEF;
62   h = rmfield (h, "DEF");
63 end
64
65 if isfield (h, "orientation0")
66
67   o = h.orientation0;
68   if numel (o) == 3
69     o = [o(:)'/norm(o), norm(o)];
70   elseif numel (o) == 4
71     o(4) = sign(o(4)) * rem (abs (o(4)), pi);
72     o = [o(1:3)(:)'/norm(o(1:3)), o(4)];
73   else
74     error ("Option 'orientation0' has size %i, should be 3 or 4", numel(o));
75   endif
76   
77   if isfield (h, "position")
78     p = h.position(:);
79   else
80     p = defaultPos(:);
81   end
82
83   h = rmfield (h, "orientation0");
84
85   h.orientation = o;
86   h.position = rotv (o(1:3), o(4))' * p;  
87
88 elseif isfield (h, "orientation")
89
90   o = h.orientation;
91   if numel (o) == 3
92     o = [o(:)'/norm(o), norm(o)];
93   elseif numel (o) == 4
94     o(4) = sign(o(4)) * rem (abs (o(4)), pi);
95     o = [o(1:3)(:)'/norm(o(1:3)), o(4)];
96   else
97     error ("Option 'orientation' has size %i, should be 3 or 4", numel(o));
98   endif
99   h.orientation = o;
100 end
101
102 if isfield (h, "position") && numel (h.position) != 3
103   error ("Option 'position' has size %i, should be 3", numel (h.position));
104 endif
105
106 if isfield (h, "jump")
107   if h.jump
108     h.jump = "TRUE";
109   else
110     h.jump = "FALSE";
111   endif
112 endif
113
114 body = "";
115 for [val, key] = h
116   body = [body,\
117           sprintf("   %-20s   %s\n",key, \
118                   sprintf (getfield (tpl,key), val))];
119 end
120
121 s = sprintf ("Viewpoint {\n%s}\n", body);
122 if !isempty (DEF)
123   s = ["DEF ", DEF," ",s];
124 end 
125 endfunction
126