-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuperquadric.html
298 lines (242 loc) · 11.1 KB
/
superquadric.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
287
288
289
290
291
292
293
294
295
296
297
<!--
super quick superquadric
Implementation of Barr's superquadric surface point visualization
in HTML5/JavaScript and threejs
@author odestcj / https://github.com/odestcj
Forgive my coding style. I am still a typedef struct kind of guy
with a noticeable disregard for proper scoping
-->
<html>
<body>
<!-- //////////////////////////////////////////////////
///// JAVASCRIPT INCLUDES
////////////////////////////////////////////////// -->
<!-- threejs - https://github.com/mrdoob/three.js/ -->
<script src="js/three.min.js"></script>
<!-- threejs camera controls helpers -->
<script src="js/OrbitControls.js"></script>
<!-- threejs keyboard input helper -->
<script src="js/THREEx.KeyboardState.js"></script>
<script>
//////////////////////////////////////////////////
///// MAIN FUNCTION CALLS
//////////////////////////////////////////////////
// initialize threejs scene, user input, and robot kinematics
init();
// main animation loop maintained by threejs
animate();
//////////////////////////////////////////////////
///// INITIALIZATION FUNCTION DEFINITONS
//////////////////////////////////////////////////
function init() {
// instantiate threejs scene graph
scene = new THREE.Scene();
// instantiate threejs camera and set its position in the world
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 1;
camera.position.z = 4;
// create two lights and add them to the scene
var light1 = new THREE.PointLight( 0xffffff, 0.5, 1000, 1 );
light1.position.set( 10, 10, 50 );
scene.add( light1 );
var light2 = new THREE.PointLight( 0xffffff, 0.5, 1000, 1 );
light2.position.set( -10, -10, -50 );
scene.add( light2 );
// instantiate threejs renderer and its dimensions
renderer = new THREE.WebGLRenderer();
//renderer.setClearColor(0x00234c,1); // blue
//renderer.setClearColor(0x001126,1); // blue
renderer.setClearColor(0x003472,1); // blue
renderer.setSize( window.innerWidth, window.innerHeight );
// attach threejs renderer to DOM
document.body.appendChild( renderer.domElement );
// instantiate threejs camera controls
camera_controls = new THREE.OrbitControls( camera );
camera_controls.addEventListener( 'change', renderer );
// instantiate threejs keyboard controls for continuous interactive controls
keyboard = new THREEx.KeyboardState();
// increment for shape parameters
parameter_increment = 0.05;
// create threejs geometry and material
temp_geom = new THREE.Geometry();
//temp_material = new THREE.MeshLambertMaterial( {color: 0xffc90b} );
temp_material = new THREE.MeshPhongMaterial( {color: 0xffc90b} );
// create object for superquadric shape
squad = [];
// specify superquadric scaling and exponent parameters
squad.scaling = {A:1, B:1, C:1};
//squad.shape = {r:2, s:2, t:2}; // start as ellipse
squad.shape = {r:10, s:10, t:10}; // start as rounded cuboid
//squad.shape = {r:0.6, s:0.6, t:0.6}; // my favorite
//squad.shape = {r:50, s:50, t:50}; // start as nearly cornered cuboid
// generate vertices for threejs geometry by sampling points from superquadric
squad.points = [];
for (sv=-Math.PI,i=0;sv<=Math.PI;sv+=Math.PI/100,i++) {
squad.points[i] = [];
for (su=-Math.PI,j=0;su<=Math.PI;su+=Math.PI/100,j++) {
squad.points[i][j] = {v:sv, u:su};
temp_point = superquadric(su,sv,
squad.scaling.A,squad.scaling.B,squad.scaling.C,
squad.shape.r,squad.shape.s,squad.shape.t);
squad.points[i][j].vertex = new THREE.Vector3(temp_point.x,temp_point.y,temp_point.z);
squad.points[i][j].id = temp_geom.vertices.length;
temp_geom.vertices.push(squad.points[i][j].vertex);
}
}
// generate faces for geometry by linking adjacent points into triangles
for (i=1;i<squad.points.length;i++) {
for (j=1;j<squad.points[i].length;j++) {
temp_geom.faces.push(new THREE.Face3(squad.points[i][j].id,squad.points[i-1][j].id,squad.points[i-1][j-1].id));
temp_geom.faces.push(new THREE.Face3(squad.points[i][j].id,squad.points[i-1][j-1].id,squad.points[i][j-1].id));
}
temp_geom.faces.push(new THREE.Face3(squad.points[i][0].id,squad.points[i-1][0].id,squad.points[i-1][squad.points[i].length-1].id));
temp_geom.faces.push(new THREE.Face3(squad.points[i][0].id,squad.points[i-1][squad.points[i].length-1].id,squad.points[i][squad.points[i].length-1].id));
}
for (j=1;j<squad.points[0].length;j++) {
temp_geom.faces.push(new THREE.Face3(squad.points[0][j].id,squad.points[squad.points.length-1][j].id,squad.points[squad.points.length-1][j-1].id));
temp_geom.faces.push(new THREE.Face3(squad.points[0][j].id,squad.points[squad.points.length-1][j-1].id,squad.points[0][j-1].id));
}
temp_geom.faces.push(new THREE.Face3(
squad.points[0][0].id,
squad.points[squad.points.length-1][0].id,
squad.points[squad.points.length-1][squad.points[0].length-1].id));
temp_geom.faces.push(new THREE.Face3(
squad.points[0][0].id,
squad.points[squad.points.length-1][squad.points[0].length-1].id,
squad.points[0][squad.points[0].length-1].id));
// request threejs compute normals
temp_geom.computeFaceNormals();
temp_geom.computeVertexNormals();
// add threejs mesh of shape from computed geometry and specified material
squad_mesh = new THREE.Mesh(temp_geom, temp_material);
scene.add(squad_mesh);
// create a textbar to display current shape parameters
textbar = document.createElement('div');
textbar.style.position = 'absolute';
//textbar.style.zIndex = 1; // if you still don't see the label, try uncommenting this
textbar.style.width = window.width-10;
textbar.style.height = 20;
//textbar.style.backgroundColor = "black";
textbar.style.color = "#ffffff";
textbar.innerHTML = "hi there!";
textbar.style.top = 10 + 'px';
textbar.style.left = 10 + 'px';
document.body.appendChild(textbar);
}
function superquadric(u,v,A,B,C,r,s,t) {
// parametric equations for superquadric, from http://en.wikipedia.org/wiki/Superquadrics
// with respect to surface lat-lon params (u,v)
// having scaling values along shape x,y,z axes (A,B,C)
// and shape definition exponents along shape x,y,z axes (r,s,t)
//
// x(u,v) = A*c(v,2/r)*c(u,2/r)
// y(u,v) = B*c(v,2/s)*s(u,2/s)
// z(u,v) = C*s(v,2/t)
//
// aux functions
// c(w,m) = sgn(cos(w))*abs(cos(w))^m
// s(w,m) = sgn(sin(w))*abs(sin(w))^m
var point = [];
point.x = A*superquadric_c(v,2/r)*superquadric_c(u,2/r);
point.y = B*superquadric_c(v,2/s)*superquadric_s(u,2/s);
point.z = C*superquadric_s(v,2/t);
return point;
}
function superquadric_c(w,m) {
if (typeof Math.sign !== 'undefined')
return Math.sign(Math.cos(w))*Math.pow(Math.abs(Math.cos(w)),m);
else
return Math_sign(Math.cos(w))*Math.pow(Math.abs(Math.cos(w)),m);
// why does Chrome not have Math.sign(); that seems unwise
}
function superquadric_s(w,m) {
if (typeof Math.sign !== 'undefined')
return Math.sign(Math.sin(w))*Math.pow(Math.abs(Math.sin(w)),m);
else
return Math_sign(Math.sin(w))*Math.pow(Math.abs(Math.sin(w)),m);
// why does Chrome not have Math.sign(); that seems unwise
}
function Math_sign(a) {
if (a < 0) return -1;
else if (a > 0) return 1;
else return 0;
}
//////////////////////////////////////////////////
///// ANIMATION AND INTERACTION LOOP
//////////////////////////////////////////////////
function animate() {
// note: three.js includes requestAnimationFrame shim
// alternative to using setInterval for updating in-browser drawing
// this effectively request that the animate function be called again for next draw
// http://learningwebgl.com/blog/?p=3189
requestAnimationFrame( animate );
// update shape parameters in textbar
textbar.innerHTML =
" Increase shape parameters with corresponding key; decrease using shift+key<br>" +
" a = " + squad.scaling.A.toFixed(2) +
" b = " + squad.scaling.B.toFixed(2) +
" c = " + squad.scaling.C.toFixed(2) +
" r = " + squad.shape.r.toFixed(2) +
" s = " + squad.shape.s.toFixed(2) +
" t = " + squad.shape.t.toFixed(2) +
" (i)ncrement = " + parameter_increment.toFixed(2) +
" <br> or try a preset: e(l)lipse, r(o)unded cuboid, cor(n)ered cuboid, s(p)erry, (u)nit scaling";
// update shape and increment parameters based on user input
if ( keyboard.pressed("shift+I") )
parameter_increment -= 0.01;
else if ( keyboard.pressed("i") )
parameter_increment += 0.01;
if ( keyboard.pressed("shift+A") )
squad.scaling.A -= parameter_increment;
else if ( keyboard.pressed("a") )
squad.scaling.A += parameter_increment;
if ( keyboard.pressed("shift+B") )
squad.scaling.B -= parameter_increment;
else if ( keyboard.pressed("b") )
squad.scaling.B += parameter_increment;
if ( keyboard.pressed("shift+C") )
squad.scaling.C -= parameter_increment;
else if ( keyboard.pressed("c") )
squad.scaling.C += parameter_increment;
if ( keyboard.pressed("shift+R") )
squad.shape.r -= parameter_increment;
else if ( keyboard.pressed("r") )
squad.shape.r += parameter_increment;
if ( keyboard.pressed("shift+S") )
squad.shape.s -= parameter_increment;
else if ( keyboard.pressed("s") )
squad.shape.s += parameter_increment;
if ( keyboard.pressed("shift+T") )
squad.shape.t -= parameter_increment;
else if ( keyboard.pressed("t") )
squad.shape.t += parameter_increment;
if ( keyboard.pressed("l") )
squad.shape = {r:2, s:2, t:2}; // ellipse
if ( keyboard.pressed("o") )
squad.shape = {r:10, s:10, t:10}; // rounded cuboid
if ( keyboard.pressed("p") )
squad.shape = {r:0.6, s:0.6, t:0.6}; // "sperry"
if ( keyboard.pressed("n") )
squad.shape = {r:40, s:40, t:40}; // nearly cornered cuboid
if ( keyboard.pressed("u") )
squad.scaling = {A:1, B:1, C:1};
// update vertices of geometry based on current superquadric parameters
for (i=0;i<squad.points.length;i++) {
for (j=0;j<squad.points[i].length;j++) {
temp_point = superquadric(squad.points[i][j].u,squad.points[i][j].v,
squad.scaling.A,squad.scaling.B,squad.scaling.C,
squad.shape.r,squad.shape.s,squad.shape.t);
squad.points[i][j].vertex.x = temp_point.x;
squad.points[i][j].vertex.y = temp_point.y;
squad.points[i][j].vertex.z = temp_point.z;
}
}
// request threejs update vertices and normals for rendering
squad_mesh.geometry.verticesNeedUpdate = true;
squad_mesh.geometry.normalsNeedUpdate = true;
// threejs rendering update
renderer.render( scene, camera );
}
</script>
</body>
</html>