-
Notifications
You must be signed in to change notification settings - Fork 0
/
app4.html
191 lines (134 loc) · 4.67 KB
/
app4.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="author" content="Olivier BOES">
<title>Canonicalization</title>
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="./Three.js"></script>
<script src="./TrackballControls.js"></script>
<script src="./Chain.js"></script>
<script type="text/javascript">
var renderer, camera, controls, scene;
var infotop, infobottom;
var chain;
function init() {
var container = document.createElement( "div" );
document.body.appendChild( container );
infotop = document.createElement( 'div' );
infotop.style.position = "absolute";
infotop.style.top = "10px";
infotop.style.width = "100%";
infotop.style.textAlign = "center";
infotop.style.color = "#ffffff";
container.appendChild( infotop );
renderer = new THREE.WebGLRenderer( { clearAlpha: 1, antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 0, 100 );
scene = new THREE.Scene();
var ceilingLight = new THREE.SpotLight( 0xffffff );
ceilingLight.position.set( 0, 0, 40 );
scene.add( ceilingLight );
var cameraLight = new THREE.DirectionalLight( 0xffffff );
cameraLight.position = camera.position;
scene.add( cameraLight );
chain = new Chain();
scene.add( chain );
window.addEventListener( "resize", function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
} );
document.addEventListener( "mousedown", mousedown );
window.addEventListener( "keydown", keydown );
}
var mode = 0;
var ichain = 10;
var points = [];
var origin;
var params = [];
init();
infotop.innerHTML = "Use the mouse to draw a flat chain and press SPACE when you're done. Please make it nonacute !";
scene.add( new THREE.AxisHelper(100) );
animate();
function animate() {
requestAnimationFrame( animate );
if ( mode > 0 ) {
controls.update();
}
renderer.render( scene, camera );
};
function ismonotone( p, dir ) {
for ( var i = 1 ; i < p.length ; i++ ) {
var e = new THREE.Vector3().subVectors( p[i], p[i-1] );
if ( e.dot( dir ) < -0.1 ) return false;
}
return true;
}
function keydown( event ) {
if ( event.keyCode == 32 ) {
if ( mode == 0 ) {
mode = 1;
document.removeEventListener( "mousedown", mousedown );
controls = new THREE.TrackballControls( camera );
origin = points[0];
params = points2params( points );
points = params2points( params, origin );
chain.update( points );
ichain = params.length - 1;
infotop.innerHTML = "Press SPACE to go to the next step in the canonicalization algorithm.";
}
else if (mode == 1 ) {
params[ichain][2] = 90;
points = params2points( params, origin );
if ( points[ichain+1].z < 0 ) {
params[ichain][2] = 270;
points = params2points( params, origin );
}
chain.update( points );
mode = 2;
if ( ichain == 0 ) {
infotop.innerHTML = "Canonicalization done... Hope it worked.";
mode = 3;
}
}
else if ( mode == 2 ) {
if ( ichain+1 < params.length ) {
var dir = new THREE.Vector3().subVectors( points[ichain], points[ichain-1] );
params[ichain+1][2] = 0;
points = params2points( params, origin );
if ( ! ismonotone( points.slice(ichain), dir ) ) {
params[ichain+1][2] = 180;
points = params2points( params, origin );
}
}
chain.update( points );
mode = 1;
ichain--;
}
}
}
function mousedown( event ) {
var x = 2 * ( event.clientX / window.innerWidth ) - 1;
var y = -2 * ( event.clientY / window.innerHeight ) + 1;
var vector = new THREE.Vector3( camera.position.z*x, camera.position.z*y, -1 );
var projector = new THREE.Projector();
projector.unprojectVector( vector, camera );
vector.z = 0;
points.push( vector );
chain.update( points );
}
</script>
</body>
</html>