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
|
#!/usr/bin/env python
import numpy as np
import pygame
from pygame.locals import *
from camera import *
from geometry import *
from transform import *
from gpu import *
def view(geometry, name=''):
"""
Render `geometry` in a pygame window.
Movement:
- zoom: scroll the mouse wheel
- rotate: click and drag the mouse
- move: shift+click and drag the mouse
"""
lower_bound, upper_bound = get_bounds(geometry.mesh)
scale = np.linalg.norm(upper_bound-lower_bound)
gpu = GPU()
gpu.load_geometry(geometry)
cuda_raytrace = gpu.get_function('ray_trace')
cuda_rotate = gpu.get_function('rotate')
cuda_translate = gpu.get_function('translate')
pygame.init()
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
pygame.display.set_caption(name)
camera = Camera(size)
diagonal = np.linalg.norm(upper_bound-lower_bound)
point = np.array([0, diagonal*2, (lower_bound[2]+upper_bound[2])/2])
axis1 = np.array([1,0,0], dtype=np.double)
axis2 = np.array([0,0,1], dtype=np.double)
camera.position(point)
origin, direction = camera.get_rays()
pixels = np.empty(width*height, dtype=np.int32)
pixels_gpu = cuda.to_device(pixels)
origins_gpu = cuda.to_device(make_vector(origin))
directions_gpu = cuda.to_device(make_vector(direction))
nblocks = 64
gpu_kwargs = {'block': (nblocks,1,1), 'grid':(pixels.size/nblocks+1,1)}
def render():
"""Render the mesh and display to screen."""
cuda_raytrace(np.int32(pixels.size), origins_gpu, directions_gpu,
np.int32(geometry.first_leaf), pixels_gpu, **gpu_kwargs)
cuda.memcpy_dtoh(pixels, pixels_gpu)
pygame.surfarray.blit_array(screen, pixels.reshape(size))
pygame.display.flip()
render()
done = False
clicked = False
shift = False
while not done:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
if event.button == 4:
v = scale*np.cross(axis1,axis2)/10.0
cuda_translate(np.int32(pixels.size), origins_gpu, gpuarray.vec.make_float3(*v), **gpu_kwargs)
point += v
render()
if event.button == 5:
v = -scale*np.cross(axis1,axis2)/10.0
cuda_translate(np.int32(pixels.size), origins_gpu, gpuarray.vec.make_float3(*v), **gpu_kwargs)
point += v
render()
if event.button == 1:
clicked = True
mouse_position = pygame.mouse.get_rel()
if event.type == MOUSEBUTTONUP:
if event.button == 1:
clicked = False
if event.type == MOUSEMOTION and clicked:
movement = np.array(pygame.mouse.get_rel())
if (movement == 0).all():
continue
length = np.linalg.norm(movement)
mouse_direction = movement[0]*axis1 + movement[1]*axis2
mouse_direction /= np.linalg.norm(mouse_direction)
if shift:
v = mouse_direction*scale*length/float(width)
cuda_translate(np.int32(pixels.size), origins_gpu, gpuarray.vec.make_float3(*v), **gpu_kwargs)
point += v
render()
else:
phi = np.float32(2*np.pi*length/float(width))
n = rotate(mouse_direction, np.pi/2, \
-np.cross(axis1,axis2))
cuda_rotate(np.int32(pixels.size), origins_gpu, phi, gpuarray.vec.make_float3(*n), **gpu_kwargs)
cuda_rotate(np.int32(pixels.size), directions_gpu, phi, gpuarray.vec.make_float3(*n), **gpu_kwargs)
point = rotate(point, phi, n)
axis1 = rotate(axis1, phi, n)
axis2 = rotate(axis2, phi, n)
render()
if event.type == KEYDOWN:
if event.key == K_LSHIFT or event.key == K_RSHIFT:
shift = True
if event.key == K_ESCAPE:
done = True
break
if event.type == KEYUP:
if event.key == K_LSHIFT or event.key == K_RSHIFT:
shift = False
if __name__ == '__main__':
import os
import sys
import optparse
from stl import *
from materials import *
parser = optparse.OptionParser('%prog filename.stl')
parser.add_option('-b', '--bits', type='int', dest='bits',
help='bits for z-ordering space axes', default=6)
options, args = parser.parse_args()
if len(args) < 1:
sys.exit(parser.format_help())
head, tail = os.path.split(args[0])
root, ext = os.path.splitext(tail)
if ext.lower() == '.stl':
geometry = Geometry()
geometry.add_solid(Solid(read_stl(args[0]), vacuum, vacuum))
geometry.build(options.bits)
elif ext.lower() == '.pkl':
import pickle
from detectors import *
f = open(args[0], 'rb')
geometry = pickle.load(f)
f.close()
view(geometry, tail)
|