source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/math/Euler.js@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 6.7 KB
Line 
1/**
2 * @author mrdoob / http://mrdoob.com/
3 * @author WestLangley / http://github.com/WestLangley
4 * @author bhouston / http://exocortex.com
5 */
6
7THREE.Euler = function ( x, y, z, order ) {
8
9 this._x = x || 0;
10 this._y = y || 0;
11 this._z = z || 0;
12 this._order = order || THREE.Euler.DefaultOrder;
13
14};
15
16THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
17
18THREE.Euler.DefaultOrder = 'XYZ';
19
20THREE.Euler.prototype = {
21
22 constructor: THREE.Euler,
23
24 _x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,
25
26 _quaternion: undefined,
27
28 _updateQuaternion: function () {
29
30 if ( this._quaternion !== undefined ) {
31
32 this._quaternion.setFromEuler( this, false );
33
34 }
35
36 },
37
38 get x () {
39
40 return this._x;
41
42 },
43
44 set x ( value ) {
45
46 this._x = value;
47 this._updateQuaternion();
48
49 },
50
51 get y () {
52
53 return this._y;
54
55 },
56
57 set y ( value ) {
58
59 this._y = value;
60 this._updateQuaternion();
61
62 },
63
64 get z () {
65
66 return this._z;
67
68 },
69
70 set z ( value ) {
71
72 this._z = value;
73 this._updateQuaternion();
74
75 },
76
77 get order () {
78
79 return this._order;
80
81 },
82
83 set order ( value ) {
84
85 this._order = value;
86 this._updateQuaternion();
87
88 },
89
90 set: function ( x, y, z, order ) {
91
92 this._x = x;
93 this._y = y;
94 this._z = z;
95 this._order = order || this._order;
96
97 this._updateQuaternion();
98
99 return this;
100
101 },
102
103 copy: function ( euler ) {
104
105 this._x = euler._x;
106 this._y = euler._y;
107 this._z = euler._z;
108 this._order = euler._order;
109
110 this._updateQuaternion();
111
112 return this;
113
114 },
115
116 setFromRotationMatrix: function ( m, order ) {
117
118 // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
119
120 // clamp, to handle numerical problems
121
122 function clamp( x ) {
123
124 return Math.min( Math.max( x, -1 ), 1 );
125
126 }
127
128 var te = m.elements;
129 var m11 = te[0], m12 = te[4], m13 = te[8];
130 var m21 = te[1], m22 = te[5], m23 = te[9];
131 var m31 = te[2], m32 = te[6], m33 = te[10];
132
133 order = order || this._order;
134
135 if ( order === 'XYZ' ) {
136
137 this._y = Math.asin( clamp( m13 ) );
138
139 if ( Math.abs( m13 ) < 0.99999 ) {
140
141 this._x = Math.atan2( - m23, m33 );
142 this._z = Math.atan2( - m12, m11 );
143
144 } else {
145
146 this._x = Math.atan2( m32, m22 );
147 this._z = 0;
148
149 }
150
151 } else if ( order === 'YXZ' ) {
152
153 this._x = Math.asin( - clamp( m23 ) );
154
155 if ( Math.abs( m23 ) < 0.99999 ) {
156
157 this._y = Math.atan2( m13, m33 );
158 this._z = Math.atan2( m21, m22 );
159
160 } else {
161
162 this._y = Math.atan2( - m31, m11 );
163 this._z = 0;
164
165 }
166
167 } else if ( order === 'ZXY' ) {
168
169 this._x = Math.asin( clamp( m32 ) );
170
171 if ( Math.abs( m32 ) < 0.99999 ) {
172
173 this._y = Math.atan2( - m31, m33 );
174 this._z = Math.atan2( - m12, m22 );
175
176 } else {
177
178 this._y = 0;
179 this._z = Math.atan2( m21, m11 );
180
181 }
182
183 } else if ( order === 'ZYX' ) {
184
185 this._y = Math.asin( - clamp( m31 ) );
186
187 if ( Math.abs( m31 ) < 0.99999 ) {
188
189 this._x = Math.atan2( m32, m33 );
190 this._z = Math.atan2( m21, m11 );
191
192 } else {
193
194 this._x = 0;
195 this._z = Math.atan2( - m12, m22 );
196
197 }
198
199 } else if ( order === 'YZX' ) {
200
201 this._z = Math.asin( clamp( m21 ) );
202
203 if ( Math.abs( m21 ) < 0.99999 ) {
204
205 this._x = Math.atan2( - m23, m22 );
206 this._y = Math.atan2( - m31, m11 );
207
208 } else {
209
210 this._x = 0;
211 this._y = Math.atan2( m13, m33 );
212
213 }
214
215 } else if ( order === 'XZY' ) {
216
217 this._z = Math.asin( - clamp( m12 ) );
218
219 if ( Math.abs( m12 ) < 0.99999 ) {
220
221 this._x = Math.atan2( m32, m22 );
222 this._y = Math.atan2( m13, m11 );
223
224 } else {
225
226 this._x = Math.atan2( - m23, m33 );
227 this._y = 0;
228
229 }
230
231 } else {
232
233 console.warn( 'WARNING: Euler.setFromRotationMatrix() given unsupported order: ' + order )
234
235 }
236
237 this._order = order;
238
239 this._updateQuaternion();
240
241 return this;
242
243 },
244
245 setFromQuaternion: function ( q, order, update ) {
246
247 // q is assumed to be normalized
248
249 // clamp, to handle numerical problems
250
251 function clamp( x ) {
252
253 return Math.min( Math.max( x, -1 ), 1 );
254
255 }
256
257 // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
258
259 var sqx = q.x * q.x;
260 var sqy = q.y * q.y;
261 var sqz = q.z * q.z;
262 var sqw = q.w * q.w;
263
264 order = order || this._order;
265
266 if ( order === 'XYZ' ) {
267
268 this._x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
269 this._y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
270 this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
271
272 } else if ( order === 'YXZ' ) {
273
274 this._x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
275 this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
276 this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
277
278 } else if ( order === 'ZXY' ) {
279
280 this._x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
281 this._y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
282 this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
283
284 } else if ( order === 'ZYX' ) {
285
286 this._x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
287 this._y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
288 this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
289
290 } else if ( order === 'YZX' ) {
291
292 this._x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
293 this._y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
294 this._z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
295
296 } else if ( order === 'XZY' ) {
297
298 this._x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
299 this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
300 this._z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
301
302 } else {
303
304 console.warn( 'WARNING: Euler.setFromQuaternion() given unsupported order: ' + order )
305
306 }
307
308 this._order = order;
309
310 if ( update !== false ) this._updateQuaternion();
311
312 return this;
313
314 },
315
316 reorder: function () {
317
318 // WARNING: this discards revolution information -bhouston
319
320 var q = new THREE.Quaternion();
321
322 return function ( newOrder ) {
323
324 q.setFromEuler( this );
325 this.setFromQuaternion( q, newOrder );
326
327 };
328
329
330 }(),
331
332 fromArray: function ( array ) {
333
334 this._x = array[ 0 ];
335 this._y = array[ 1 ];
336 this._z = array[ 2 ];
337 if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
338
339 this._updateQuaternion();
340
341 return this;
342
343 },
344
345 toArray: function () {
346
347 return [ this._x, this._y, this._z, this._order ];
348
349 },
350
351 equals: function ( euler ) {
352
353 return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
354
355 },
356
357 clone: function () {
358
359 return new THREE.Euler( this._x, this._y, this._z, this._order );
360
361 }
362
363};
Note: See TracBrowser for help on using the repository browser.