-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyplot3d.py
215 lines (181 loc) · 4.55 KB
/
pyplot3d.py
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
import os
import tempfile
import webbrowser
import time
from uuid import uuid4 as uuid
class Vector3:
def __init__(self):
self.x = 0.
self.y = 0.
self.z = 0.
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __init__(self, v):
self.x = v.x
self.y = v.y
self.z = v.z
def length(self):
math.sqrt( self.x*self.x + self.y*self.y + self.z*self.z )
def scalar_mul(self, a):
self.x *= a
self.y *= a
self.z *= a
def normalize(self):
l = self.length()
if( l != 0 ):
self.scalar_mul( 1./l )
def dot(self, v):
self.x*v.x + self.y*v.y + self.z*v.z
def cross(self, v):
self.x = self.y*v.z - self.z*v.y
self.y = self.z*v.x - self.x*v.z
self.z = self.x*v.y - self.y*v.x
def tup(self):
self.x, self.y, self.z
def dot(v1, v2):
v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
def cross(v1, v2):
v = Vector3( v1)
v.cross( v2)
class Point:
def __init__(self, *args):
self.p = Vector3( *args)
def transform(self, t):
self.p.transform( t)
_html_wrapper = """\
<html>
<head>
<title>Test Three.js app</title>
</head>
<body>
{PLOT_HTML}
</body>
</html>
"""
_plot_html = """
<canvas
id="{UUID}"
width="600"
height="400"
>
</canvas>
<script type="text/javascript" src="js/three.min.js"></script>
<script type="text/javascript" src="js/TrackBallControls.js"></script>
<script type="text/javascript" src="js/OrbitControls.js"></script>
{SCRIPT_MAIN}
"""
_script_main = """
<script>
var canvas =
document.getElementById("{UUID}");
var camera, controls;
var scene, renderer;
renderer = new THREE.WebGLRenderer({{
canvas: canvas,
alpha: true,
antialiasing: true
}});
renderer.setSize( canvas.width, canvas.height);
scene = new THREE.Scene();
init_camera();
init_lights();
init_controls();
init_scene();
render();
animate();
{INIT_CAMERA}
{INIT_LIGHTS}
{INIT_CONTROLS}
{INIT_SCENE}
{RENDER}
{ANIMATE}
</script>
"""
_init_camera = """
function init_camera() {
camera = new THREE.PerspectiveCamera(
75,
canvas.width/canvas.height,
0.1,
100
);
camera.position.set( -3., 10., 3. );
camera.up = new THREE.Vector3( 0, 0, 1 );
camera.lookAt( new THREE.Vector3( 0, 0, 0 ) );
}
"""
_init_lights = """
function init_lights() {
var light = new THREE.DirectionalLight( 0x882222 );
camera.add( light );
light.position.set( 0, 100., 30. );
light = new THREE.DirectionalLight( 0x228822 );
camera.add( light );
light.position.set( 60., 80., 30. );
light = new THREE.DirectionalLight( 0x222288 );
camera.add( light );
light.position.set( 0., 80., 90. );
light = new THREE.AmbientLight( 0x444444 );
scene.add( light );
scene.add( camera );
}
"""
_init_controls = """
function init_controls() {
controls = new THREE.OrbitControls( camera, canvas );
controls.rotateSpeed = 5.0;
controls.zoomSpeed = 1.2;
controls.noZoom = false;
controls.noPan = true;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.addEventListener( 'change', render );
}
"""
_init_scene = """
function init_scene() {
var material = new THREE.MeshPhongMaterial( {
specular: 0x444444,
color: 0xcccccc,
emissive: 0x000000,
shininess: 100
});
var geometry = new THREE.SphereGeometry( 1, 32, 32 );
var sphere = new THREE.Mesh( geometry, material );
scene.add( sphere );
geometry = new THREE.BoxGeometry( 0.8, 0.9, 1. );
var cube = new THREE.Mesh( geometry, material );
cube.position.set( -1, -1, -1 );
scene.add( cube );
}
"""
_render = """
function render(){
renderer.render(scene, camera);
}
"""
_animate = """
function animate(){
requestAnimationFrame( animate );
controls.update();
}
"""
if __name__ == '__main__':
# f = tempfile.NamedTemporaryFile(suffix='.html')
f = open('temp.html', 'w+b')
uu = uuid()
text = script_main.format(UUID=uu,
INIT_CAMERA=script_init_camera,
INIT_LIGHTS=script_init_lights,
INIT_CONTROLS=script_init_controls,
INIT_SCENE=script_init_scene,
RENDER=script_render,
ANIMATE=script_animate)
text = plot_html.format(UUID=uu, SCRIPT_MAIN=text)
text = html_wrapper.format(PLOT_HTML=text)
print( f.name)
f.write(text.encode('utf-8'))
f.close()
webbrowser.open('file://'+os.path.realpath(f.name))