-
Notifications
You must be signed in to change notification settings - Fork 0
/
so3.html
286 lines (250 loc) · 12.4 KB
/
so3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Visualizing Euler's Rotation Theorem</title>
<style type="text/css">p,label { font-family: "Arial", "Liberation Sans", sans-serif; }</style>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/three.min.js"></script>
<script type="text/javascript">
function setup () {
var width = 800;
var height = 600;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera (3, width/height, 0.1, 5000);
camera.position.z = 3000;
camera.position.x = 0;
camera.position.y = 0;
var renderer = new THREE.WebGLRenderer();
renderer.sortObjects = false;
renderer.setSize(width,height);
document.getElementById('animation-space').appendChild(renderer.domElement);
// Set up the earth
var sphereGeometry = new THREE.SphereGeometry (50,30,30);
var sphereTex = THREE.ImageUtils.loadTexture('pix/world3.png',{},function () {renderer.render(scene,camera);});
sphereTex.wrapS = sphereTex.wrapT = THREE.ClampToEdgeWrapping;
var sphereMat = new THREE.MeshLambertMaterial({map: sphereTex,opacity: 1});
var sphere = new THREE.Mesh (sphereGeometry, sphereMat);
sphere.position.set(0,0,0);
scene.add(sphere);
//
//
// Set up the axis descriptor
var cylMat = new THREE.MeshLambertMaterial({color:0xff0000});
var cylGeom = new THREE.CylinderGeometry(0.6,0.6,400);
var cyl = new THREE.Mesh (cylGeom,cylMat);
//THREE.GeometryUtils.merge(sphereGeometry,cylGeom);
var cylMatrix = new THREE.Matrix4(1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1);
cyl.applyMatrix(cylMatrix);
cyl.position.set(0,0,0);
scene.add(cyl);
//
//
// Set up the light source
var pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.x = 0;
pointLight.position.y = 0;
pointLight.position.z = 3000;
scene.add(pointLight);
function makePin (col) {
var chiMat = new THREE.MeshLambertMaterial({color:col});
var chiSGeom = new THREE.SphereGeometry(1,32,32);
var chiCGeom = new THREE.CylinderGeometry(0.4,0.4,5);
var chiS = new THREE.Mesh(chiSGeom,chiMat);
var chiC = new THREE.Mesh(chiCGeom,chiMat);
chiC.position.y -= 2;
THREE.GeometryUtils.merge(chiSGeom,chiC);
scene.add(chiS);
return chiS;
}
var chiLat = 41.8819;
var chiLong = -87.6278;
var vx = Math.sin(chiLat/180*Math.PI)*Math.cos(-chiLong/ 180*Math.PI);
var vy = Math.cos(chiLat/180*Math.PI);
var vz = Math.sin(chiLat/180*Math.PI)*Math.sin(-chiLong/180*Math.PI);
var chiVector = new THREE.Vector3(vx,vy,vz);
function movePin (pin,dir) {
var chiRad = 54;
var chiMatrix = new THREE.Matrix4();
rotationForVector(dir,chiMatrix,false);
pin.rotation.x = 0;
pin.rotation.y = 0;
pin.rotation.z = 0;
pin.updateMatrix();
pin.applyMatrix(chiMatrix);
pin.position.set(chiRad*dir.x,chiRad*dir.y,chiRad*dir.z);
}
function makeColor (frac) {
var p = Math.floor(frac*255);
return 'rgb(' + p + ',' + p + ',' + p + ')';
}
var nChiPins = 0;
var chiPins = [];
function changeNumberOfPins () {
var n = document.getElementById('num-points').value;
if (n == nChiPins) return;
else {
for (var j = 0 ; j < nChiPins ; ++j) {
var pin = chiPins.pop();
scene.remove(pin);
}
for (var j = 0 ; j < n ; ++j) {
var col = makeColor(j/n);
chiPins.push(makePin(col));
}
}
nChiPins = n;
}
document.getElementById('num-points').onchange = changeNumberOfPins;
function moveChiVectors () {
var chiVectorT = new THREE.Vector3();
chiVectorT.copy(chiVector);
for (var j = 0 ; j < nChiPins ; ++j) {
movePin(chiPins[j],chiVectorT);
chiVectorT.applyMatrix4(sphere.matrix);
}
}
changeNumberOfPins(5);
var makeAdvMatrix;
var advMatrix = new THREE.Matrix4();
var rx,ry,rz;
function updateDirection () {
rx = Math.random()-0.5;
ry = Math.random()-0.5;
rz = Math.random()-0.5;
}
updateDirection();
setInterval(updateDirection,2000);
var angle = 0;
var myangle = 0;
var space_age = false;
renderer.domElement.addEventListener("click",
function () { space_age = !space_age; myangle = 0; });
render = function () {
requestAnimationFrame(render);
if (!space_age) {
var n = Math.sqrt(rx*rx+ry*ry+rz*rz);
rx = rx/n;
ry = ry/n;
rz = rz/n;
// Rotate sphere
sphere.rotation.x += rx/50;
sphere.rotation.y += ry/50;
sphere.rotation.z += rz/50;
sphere.updateMatrix();
//
// Calculate cylinder position
//
var a = sphere.rotation.x;
var b = sphere.rotation.y;
var c = sphere.rotation.z;
// Coordinates of the fixed point (eigenvector)
var vx = 1/Math.sqrt((Math.pow((-Math.cos(a)*Math.cos(c)*Math.sin(b) + Math.sin(a)*Math.sin(c) + Math.sin(b)),2) + Math.pow((Math.cos(a)*Math.sin(b)*Math.sin(c) - Math.cos(b)*Math.sin(a) + Math.cos(c)*Math.sin(a)),2) + Math.pow((Math.sin(a)*Math.sin(b)*Math.sin(c) + Math.cos(a)*Math.cos(b) - (Math.cos(a) + Math.cos(b))*Math.cos(c) + 1),2))/Math.pow((-Math.cos(a)*Math.cos(c)*Math.sin(b) + Math.sin(a)*Math.sin(c) + Math.sin(b)),2));
var vy = -((Math.cos(a)*Math.sin(b)*Math.sin(c) - Math.cos(b)*Math.sin(a) + Math.cos(c)*Math.sin(a))/((Math.cos(a)*Math.cos(c)*Math.sin(b) - Math.sin(a)*Math.sin(c) - Math.sin(b))*Math.sqrt((Math.pow((-Math.cos(a)*Math.cos(c)*Math.sin(b) + Math.sin(a)*Math.sin(c) + Math.sin(b)),2) + Math.pow((Math.cos(a)*Math.sin(b)*Math.sin(c) - Math.cos(b)*Math.sin(a) + Math.cos(c)*Math.sin(a)),2) + Math.pow((Math.sin(a)*Math.sin(b)*Math.sin(c) + Math.cos(a)*Math.cos(b) - (Math.cos(a) + Math.cos(b))*Math.cos(c) + 1),2))/Math.pow((-Math.cos(a)*Math.cos(c)*Math.sin(b) + Math.sin(a)*Math.sin(c) + Math.sin(b)),2))));
var vz = -(Math.sin(a)*Math.sin(b)*Math.sin(c) + Math.cos(a)*Math.cos(b) - (Math.cos(a) + Math.cos(b))*Math.cos(c) + 1)/((Math.cos(a)*Math.cos(c)*Math.sin(b) - Math.sin(a)*Math.sin(c) - Math.sin(b))*Math.sqrt((Math.pow((-Math.cos(a)*Math.cos(c)*Math.sin(b) + Math.sin(a)*Math.sin(c) + Math.sin(b)),2) + Math.pow((Math.cos(a)*Math.sin(b)*Math.sin(c) - Math.cos(b)*Math.sin(a) + Math.cos(c)*Math.sin(a)),2) + Math.pow((Math.sin(a)*Math.sin(b)*Math.sin(c) + Math.cos(a)*Math.cos(b) - (Math.cos(a) + Math.cos(b))*Math.cos(c) + 1),2))/Math.pow((-Math.cos(a)*Math.cos(c)*Math.sin(b) + Math.sin(a)*Math.sin(c) + Math.sin(b)),2)));
// angle of rotation about the fixed axis
angle = Math.acos(0.5*(-Math.sin(a)*Math.sin(b)*Math.sin(c) + Math.cos(a)*Math.cos(b) + Math.cos(a)*Math.cos(c) + Math.cos(b)*Math.cos(c)-1));
// make cylMatrix point in the direction we want
var v1 = new THREE.Vector3(vx,vy,vz);
rotationForVector(v1,cylMatrix,true);
// get the inverse of cylMatrix
var cylMatrixInv = new THREE.Matrix4;
cylMatrixInv.getInverse(cylMatrix);
moveChiVectors();
// figure out which direction we need to rotate
var v2 = new THREE.Vector3(Math.random(),Math.random(),Math.random());
var v2t = new THREE.Vector3();
v2t.copy(v2);
v2t.applyMatrix4(sphere.matrix);
var signMatrix = new THREE.Matrix3();
signMatrix.set(v1.x,v2.x,v2t.x,
v1.y,v2.y,v2t.y,
v1.z,v2.z,v2t.z);
if (signMatrix.determinant() < 0) {
angle = -angle;
}
// set up matrix to rotate a little bit back to the original position
makeAdvMatrix = function (t) {
t *= Math.sign(angle);
var rotMatrix = new THREE.Matrix4 (Math.cos(t),0,-Math.sin(t),0,0,1,0,0,Math.sin(t),0,Math.cos(t),0,0,0,0,1);
advMatrix.copy(cylMatrix);
advMatrix.multiply(rotMatrix);
advMatrix.multiply(cylMatrixInv);
}
makeAdvMatrix(0.04);
cyl.rotation.x = 0;
cyl.rotation.y = 0;
cyl.rotation.z = 0;
cyl.updateMatrix();
cyl.applyMatrix(cylMatrix);
} else { // space_age
if (myangle < Math.abs(angle)-0.04) {
sphere.applyMatrix(advMatrix);
myangle += 0.04;
} else if (myangle < Math.abs(angle)) {
makeAdvMatrix(Math.abs(angle)-myangle);
sphere.applyMatrix(advMatrix);
myangle = Math.abs(angle);
}
moveChiVectors();
}
renderer.render(scene,camera);
}
render();
}
function rotationForVector(v1,outMatrix,flip) {
var r21 = Math.random();
var r22 = Math.random();
var r23 = Math.random();
var r31 = Math.random();
var r32 = Math.random();
var r33 = Math.random();
var v2 = new THREE.Vector3(r21,r22,r23);
v1t = new THREE.Vector3();
v1t.copy(v1);
v1t.multiplyScalar(v2.dot(v1));
v2.sub(v1t);
v2.divideScalar(v2.length());
var v3 = new THREE.Vector3(r31,r32,r33);
v1t.copy(v1);
v1t.multiplyScalar(v3.dot(v1));
var v2t = new THREE.Vector3();
v2t.copy(v2);
v2t.multiplyScalar(v3.dot(v2));
v3.sub(v1t);
v3.sub(v2t);
v3.divideScalar(v3.length());
outMatrix.set(v2.x,v1.x,v3.x,0,
v2.y,v1.y,v3.y,0,
v2.z,v1.z,v3.z,0,
0 ,0 ,0 ,1);
if (outMatrix.determinant() < 0) {
if (flip) {
v1.multiplyScalar(-1);
} else {
v2.multiplyScalar(-1);
}
outMatrix.set(v2.x,v1.x,v3.x,0,
v2.y,v1.y,v3.y,0,
v2.z,v1.z,v3.z,0,
0 ,0 ,0 ,1);
}
}
window.onload = setup;
</script>
</head>
<body>
<p>
<a href="http://en.wikipedia.org/wiki/Euler's_rotation_theorem">Euler's Rotation Theorem</a> states that any orientation-preserving isometry (rigid motion) of a sphere is equivalent to a rotation by some amount about some axis. As the earth wobbles randomly in the animation below, the red line indicates the axis around which the earth must be rotated from its current position to regain its starting position. Thus the isometry given by movement of the earth from its starting position to its current position is the opposite rotation (again about the red line). To see the rotation back to the starting position, click anywhere on the figure. Click again to resume the random wobbling.
</p>
<div id="animation-space">
</div>
<p>One way to see this theorem as plausible is to think about the orbit of a point under the action of the motion (rotation) from the starting orientation to the current orientation. You can see some points from the orbit of Chicago by incrementing the value of the box below. Note that if the value is set to 1, only the zeroeth iteration (the starting position of Chicago) will be shown.</p>
<input type="number" min="0" max="50" step="1" value="0" name="num-points" id="num-points" /><label for="num-points">Number of Chicago orbit points</label>
<p><small>Don't see anything? Most likely your browser isn't set up for <a href="http://www.khronos.org/webgl/">WebGL</a>. Maybe <a href="http://get.webgl.org/">get.webgl.org</a> will be helpful. If you see the spinning cube on that website but don't see anything here, <a href="https://github.com/ajdunlap/visualizing-so3/issues">do complain</a>.</small></p>
<hr />
<p><small>Source available on <a href="https://github.com/ajdunlap/visualizing-so3">GitHub</a>. Created with <a href="http://threejs.org">three.js</a>. World map from <a href="http://commons.wikimedia.org/wiki/User:Koba-chan">User:Koba-chan</a> on <a href="https://commons.wikimedia.org/wiki/File:WorldMap-A_non-Frame.png">Wikimedia Commons</a>, <a href="http://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA-3.0</a>.</small></p>
</body>
</html>