summaryrefslogtreecommitdiff
path: root/camera.py
blob: 37dc072f9d60b7db9ae967e11dd77dcad5b27faa (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import numpy as np
from itertools import product, count
from threading import Thread, Lock
import time
import datetime
import os
import sys
from color import map_wavelength

from transform import rotate

import pygame
from pygame.locals import *

from pycuda import gpuarray
from pycuda.characterize import sizeof
import pycuda.driver as cuda

from subprocess import call
import shutil
import tempfile

def timeit(func):
    def f(*args, **kwargs):
        t0 = time.time()
        func(*args, **kwargs)
        elapsed = time.time() - t0
        print '%s elapsed in %s().' % (datetime.timedelta(seconds=elapsed), func.__name__)
    return f

def encode_movie(dir):
    root, ext = 'movie', 'avi'
    for i in count():
        filename = '.'.join([root + str(i).zfill(5), ext])

        if not os.path.exists(filename):
            break

    call(['mencoder', 'mf://' + dir + '/*.png', '-mf', 'fps=10', '-o', filename, '-ovc', 'xvid', '-xvidencopts', 'bitrate=3000'])

    shutil.rmtree(dir)

    print 'movie saved to %s.' % filename

def get_rays(position, size = (800, 600), film_size = (0.035, 0.024), focal_length=0.05):
    """
    Generate ray positions and directions from a pinhole camera facing the negative y direction.

    Args:
        - position: tuple,
            Position of the camera.
        - size: tuple, *optional*
            Pixel array shape.
        - film_size: tuple, *optional*
            Physical size of photographic film. Defaults to 35mm film size.
        - focal_length: float, *optional*
            Focal length of camera.
    """

    x = np.linspace(-film_size[0]/2, film_size[0]/2, size[0])
    z = np.linspace(-film_size[1]/2, film_size[1]/2, size[1])

    grid = np.array(tuple(product(x,[0],z)))

    grid += (0,focal_length,0)
    focal_point = np.zeros(3)

    grid += position
    focal_point += position

    return grid, focal_point-grid

class Camera(Thread):
    def __init__(self, geometry, module, context, lock, size=(800,600)):
        Thread.__init__(self)
        self.geometry = geometry
        self.module = module
        self.context = context
        self.lock = lock

        self.size = size
        self.width, self.height = size

        pygame.init()
        self.screen = pygame.display.set_mode(size)
        pygame.display.set_caption('')
        self.clock = pygame.time.Clock()

        with self.lock:
            self.context.push()
            self.ray_trace_kernel = self.module.get_function('ray_trace')
            self.rotate_kernel = self.module.get_function('rotate')
            self.rotate_around_point_kernel = self.module.get_function('rotate_around_point')
            self.translate_kernel = self.module.get_function('translate')
            self.update_xyz_lookup_kernel = self.module.get_function('update_xyz_lookup')
            self.update_xyz_image_kernel = self.module.get_function('update_xyz_image')
            self.process_image_kernel = self.module.get_function('process_image')
            self.init_rng_kernel = self.module.get_function('init_rng')
            self.context.pop()

        lower_bound, upper_bound = self.geometry.mesh.get_bounds()
        self.scale = np.linalg.norm(upper_bound-lower_bound)

        self.nblocks = 64

        self.point = np.array([0, self.scale*1.75, (lower_bound[2]+upper_bound[2])/2])
        self.axis1 = np.array([1,0,0], float)
        self.axis2 = np.array([0,0,1], float)

        origins, directions = get_rays(self.point, self.size)

        with self.lock:
            self.context.push()
            self.origins_gpu = gpuarray.to_gpu(origins.astype(np.float32).view(gpuarray.vec.float3))
            self.directions_gpu = gpuarray.to_gpu(directions.astype(np.float32).view(gpuarray.vec.float3))
            self.pixels_gpu = gpuarray.zeros(self.width*self.height, dtype=np.int32)
            self.context.pop()

        self.movie = False
        self.movie_index = 0
        self.movie_dir = None
        self.render = False

    @timeit
    def initialize_render(self):
        with self.lock:
            self.context.push()
            self.rng_states_gpu = cuda.mem_alloc(self.width*self.height*sizeof('curandStateXORWOW', '#include <curand_kernel.h>'))
            self.init_rng_kernel(np.int32(self.width*self.height), self.rng_states_gpu, np.int32(0), np.int32(0), block=(self.nblocks,1,1), grid=(self.width*self.height//self.nblocks+1,1))
            self.xyz_lookup1_gpu = gpuarray.zeros(len(self.geometry.mesh.triangles), dtype=gpuarray.vec.float3)
            self.xyz_lookup2_gpu = gpuarray.zeros(len(self.geometry.mesh.triangles), dtype=gpuarray.vec.float3)
            self.image_gpu = gpuarray.zeros(self.width*self.height, dtype=gpuarray.vec.float3)
            self.context.synchronize()
            self.context.pop()

        self.source_position = self.point

        self.nimages = 0
        self.nlookup_calls = 0
        self.max_steps = 10

    def clear_xyz_lookup(self):
        with self.lock:
            self.context.push()
            self.xyz_lookup1_gpu.fill(gpuarray.vec.make_float3(0.0,0.0,0.0))
            self.xyz_lookup2_gpu.fill(gpuarray.vec.make_float3(0.0,0.0,0.0))
            self.context.pop()

        self.nlookup_calls = 0

    def update_xyz_lookup(self, source_position):
        with self.lock:
            self.context.push()
            for i in range(self.xyz_lookup1_gpu.size//(self.width*self.height)+1):
                self.update_xyz_lookup_kernel(np.int32(self.width*self.height), np.int32(self.xyz_lookup1_gpu.size), np.int32(i*self.width*self.height), gpuarray.vec.make_float3(*source_position), self.rng_states_gpu, np.float32(685.0), gpuarray.vec.make_float3(1.0,0.0,0.0), self.xyz_lookup1_gpu, self.xyz_lookup2_gpu, np.int32(self.max_steps), block=(self.nblocks,1,1), grid=(self.width*self.height//self.nblocks+1,1))

            for i in range(self.xyz_lookup1_gpu.size//(self.width*self.height)+1):
                self.update_xyz_lookup_kernel(np.int32(self.width*self.height), np.int32(self.xyz_lookup1_gpu.size), np.int32(i*self.width*self.height), gpuarray.vec.make_float3(*source_position), self.rng_states_gpu, np.float32(545.0), gpuarray.vec.make_float3(0.0,1.0,0.0), self.xyz_lookup1_gpu, self.xyz_lookup2_gpu, np.int32(self.max_steps), block=(self.nblocks,1,1), grid=(self.width*self.height//self.nblocks+1,1))

            for i in range(self.xyz_lookup1_gpu.size//(self.width*self.height)+1):
                self.update_xyz_lookup_kernel(np.int32(self.width*self.height), np.int32(self.xyz_lookup1_gpu.size), np.int32(i*self.width*self.height), gpuarray.vec.make_float3(*source_position), self.rng_states_gpu, np.float32(445.0), gpuarray.vec.make_float3(0.0,0.0,1.0), self.xyz_lookup1_gpu, self.xyz_lookup2_gpu, np.int32(self.max_steps), block=(self.nblocks,1,1), grid=(self.width*self.height//self.nblocks+1,1))
            self.context.pop()

        self.nlookup_calls += 1

    def clear_image(self):
        with self.lock:
            self.context.push()
            self.image_gpu.fill(gpuarray.vec.make_float3(0.0,0.0,0.0))
            self.context.pop()

        self.nimages = 0

    def update_image(self):
        with self.lock:
            self.context.push()
            self.update_xyz_image_kernel(np.int32(self.width*self.height), self.rng_states_gpu, self.origins_gpu, self.directions_gpu, np.float32(685.0), gpuarray.vec.make_float3(1.0,0.0,0.0), self.xyz_lookup1_gpu, self.xyz_lookup2_gpu, self.image_gpu, np.int32(self.nlookup_calls), np.int32(self.max_steps), block=(self.nblocks,1,1), grid=(self.width*self.height//self.nblocks+1,1))

            self.update_xyz_image_kernel(np.int32(self.width*self.height), self.rng_states_gpu, self.origins_gpu, self.directions_gpu, np.float32(545.0), gpuarray.vec.make_float3(0.0,1.0,0.0), self.xyz_lookup1_gpu, self.xyz_lookup2_gpu, self.image_gpu, np.int32(self.nlookup_calls), np.int32(self.max_steps), block=(self.nblocks,1,1), grid=(self.width*self.height//self.nblocks+1,1))

            self.update_xyz_image_kernel(np.int32(self.width*self.height), self.rng_states_gpu, self.origins_gpu, self.directions_gpu, np.float32(445.0), gpuarray.vec.make_float3(0.0,0.0,1.0), self.xyz_lookup1_gpu, self.xyz_lookup2_gpu, self.image_gpu, np.int32(self.nlookup_calls), np.int32(self.max_steps), block=(self.nblocks,1,1), grid=(self.width*self.height//self.nblocks+1,1))
            self.context.pop()

        self.nimages += 1

    def process_image(self):
        with self.lock:
            self.context.push()
            self.process_image_kernel(np.int32(self.width*self.height), self.image_gpu, self.pixels_gpu, np.int32(self.nimages), block=(self.nblocks,1,1), grid=((self.width*self.height)//self.nblocks+1,1))
            self.context.pop()

    def screenshot(self, dir='', start=0):
        root, ext = 'screenshot', 'png'

        for i in count(start):
            filename = os.path.join(dir, '.'.join([root + str(i).zfill(5), ext]))

            if not os.path.exists(filename):
                break

        pygame.image.save(self.screen, filename)
        print 'image saved to %s' % filename

    def rotate(self, phi, n):
        with self.lock:
            self.context.push()
            self.rotate_kernel(np.int32(self.pixels_gpu.size), self.origins_gpu, np.float32(phi), gpuarray.vec.make_float3(*n), block=(self.nblocks,1,1), grid=(self.pixels_gpu.size//self.nblocks+1,1))
            self.rotate_kernel(np.int32(self.pixels_gpu.size), self.directions_gpu, np.float32(phi), gpuarray.vec.make_float3(*n), block=(self.nblocks,1,1), grid=(self.pixels_gpu.size//self.nblocks+1,1))

            self.point = rotate(self.point, phi, n)
            self.axis1 = rotate(self.axis1, phi, n)
            self.axis2 = rotate(self.axis2, phi, n)
            self.context.pop()

        if self.render:
            self.clear_image()

        self.update()

    def rotate_around_point(self, phi, n, point):
        with self.lock:
            self.context.push()
            self.rotate_around_point_kernel(np.int32(self.origins_gpu.size), self.origins_gpu, np.float32(phi), gpuarray.vec.make_float3(*n), gpuarray.vec.make_float3(*point), block=(self.nblocks,1,1), grid=(self.origins_gpu.size//self.nblocks+1,1))
            self.rotate_kernel(np.int32(self.directions_gpu.size), self.directions_gpu, np.float32(phi), gpuarray.vec.make_float3(*n), block=(self.nblocks,1,1), grid=(self.directions_gpu.size//self.nblocks+1,1))
            self.context.pop()

        self.axis1 = rotate(self.axis1, phi, n)
        self.axis2 = rotate(self.axis2, phi, n)

        if self.render:
            self.clear_image()

        self.update()

    def translate(self, v):
        with self.lock:
            self.context.push()
            self.translate_kernel(np.int32(self.pixels_gpu.size), self.origins_gpu, gpuarray.vec.make_float3(*v), block=(self.nblocks,1,1), grid=(self.pixels_gpu.size//self.nblocks,1))

            self.point += v
            self.context.pop()

        if self.render:
            self.clear_image()

        self.update()

    def update(self):
        if self.render:
            while self.nlookup_calls < 10:
                self.update_xyz_lookup(self.source_position)
            self.update_image()
            self.process_image()
        else:
            with self.lock:
                self.context.push()
                self.ray_trace_kernel(np.int32(self.pixels_gpu.size), self.origins_gpu, self.directions_gpu, self.pixels_gpu, block=(self.nblocks,1,1), grid=(self.pixels_gpu.size//self.nblocks+1,1))
                self.context.pop()

        with self.lock:
            self.context.push()
            pygame.surfarray.blit_array(self.screen, self.pixels_gpu.get().reshape(self.size))
            pygame.display.flip()
            self.context.pop()

        if self.movie:
            self.screenshot(self.movie_dir, self.movie_index)
            self.movie_index += 1

    def run(self):
        self.update()
        
        done = False
        clicked = False
        shift = False
        ctrl = False

        #current_layer = 0

        while not done:
            self.clock.tick(20)

            if self.render and not clicked and not pygame.event.peek(KEYDOWN):
                self.update()

            for event in pygame.event.get():
                if event.type == MOUSEBUTTONDOWN:
                    if event.button == 4:
                        v = self.scale*np.cross(self.axis1,self.axis2)/10.0
                        self.translate(v)

                    if event.button == 5:
                        v = -self.scale*np.cross(self.axis1,self.axis2)/10.0
                        self.translate(v)

                    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]*self.axis1 + movement[1]*self.axis2
                    mouse_direction /= np.linalg.norm(mouse_direction)

                    if shift:
                        v = mouse_direction*self.scale*length/float(self.width)
                        self.translate(v)
                    else:
                        phi = np.float32(2*np.pi*length/float(self.width))
                        n = rotate(mouse_direction, np.pi/2, -np.cross(self.axis1,self.axis2))

                        if ctrl:
                            self.rotate_around_point(phi, n, self.point)
                        else:
                            self.rotate(phi, n)

                if event.type == KEYDOWN:
                    if event.key == K_a:
                        v = self.scale*self.axis1/10.0
                        self.translate(v)

                    if event.key == K_d:
                        v = -self.scale*self.axis1/10.0
                        self.translate(v)

                    if event.key == K_w:
                        v = self.scale*np.cross(self.axis1,self.axis2)/10.0
                        self.translate(v)

                    if event.key == K_s:
                        v = -self.scale*np.cross(self.axis1,self.axis2)/10.0
                        self.translate(v)

                    if event.key == K_SPACE:
                        v = self.scale*self.axis2/10.0
                        self.translate(v)

                    # if event.key == K_LCTRL:
                    #     v = -self.scale*self.axis2/10.0
                    #     self.translate(v)

                    if event.key == K_F6:
                        self.clear_xyz_lookup()
                        self.clear_image()
                        self.source_position = self.point

                    if event.key == K_p:
                        for i in range(10):
                            self.update_xyz_lookup(self.point)
                        self.source_position = self.point

                    if event.key == K_LSHIFT or event.key == K_RSHIFT:
                        shift = True

                    if event.key == K_LCTRL or event.key == K_RCTRL:
                        ctrl = True

                    if event.key == K_ESCAPE:
                        done = True
                        break

                    #if event.key == K_PAGEUP and load_bvh:
                    #    try:
                    #        if current_layer+1 >= len(bvhg):
                    #            raise IndexError

                    #        geometry = bvhg[current_layer+1]
                    #        current_layer += 1

                    #        geometry.load(module, color=True)
                    #        update()
                    #    except IndexError:
                    #        print 'no further layers to view'

                    #if event.key == K_PAGEDOWN and load_bvh:
                    #    try:
                    #        if current_layer-1 < 0:
                    #            raise IndexError

                    #        geometry = bvhg[current_layer-1]
                    #        current_layer -= 1

                    #        geometry.load(module, color=True)
                    #        update()
                    #    except IndexError:
                    #        print 'no further layers to view'

                    if event.key == K_F12:
                        self.screenshot()

                    if event.key == K_F5:
                        if not hasattr(self, 'rng_states_gpu'):
                            self.initialize_render()

                        self.render = not self.render
                        self.clear_image()
                        self.update()

                    if event.key == K_m:
                        if self.movie:
                            encode_movie(self.movie_dir)
                            self.movie_dir = None
                            self.movie = False
                        else:
                            self.movie_index = 0
                            self.movie_dir = tempfile.mkdtemp()
                            self.movie = True

                if event.type == KEYUP:
                    if event.key == K_LSHIFT or event.key == K_RSHIFT:
                        shift = False

                    if event.key == K_LCTRL or event.key == K_RCTRL:
                        ctrl = False

                if event.type == pygame.QUIT:
                    done = True
                    break

        if self.movie:
            encode_movie(self.movie_dir)

        pygame.display.quit()

if __name__ == '__main__':
    import optparse
    import inspect

    import solids
    import detectors
    import scenes
    from stl import mesh_from_stl
    import src
    from view import build

    from pycuda.compiler import SourceModule

    parser = optparse.OptionParser('%prog filename.stl')
    parser.add_option('-b', '--bits', type='int', dest='bits',
                      help='bits for z-ordering space axes', default=8)
    #parser.add_option('-l', action='store_true', dest='load_bvh',
    #                  help='load bounding volumes', default=False)
    parser.add_option('-r', '--resolution', dest='resolution',
                      help='specify resolution', default='800,600')
    options, args = parser.parse_args()

    if len(args) < 1:
        sys.exit(parser.format_help())

    size = [int(s) for s in options.resolution.split(',')]

    if os.path.exists(args[0]):
        root, ext = os.path.splitext(os.path.split(args[0])[1])

        if ext.lower() == '.stl':
            obj = mesh_from_stl(args[0])
    else:
        members = dict(inspect.getmembers(detectors) + inspect.getmembers(solids) + inspect.getmembers(scenes))

        buildable_lookup = {}
        for member in members.values():
            if inspect.isfunction(member) and \
                    hasattr(member, 'buildable') and member.buildable == True:
                buildable_lookup[member.identifier] = member

        if args[0] in buildable_lookup:
            obj = buildable_lookup[args[0]]
        else:
            raise Exception("can't find object %s" % args[0])

    from pycuda.tools import make_default_context

    cuda.init()
    context = make_default_context()
    print 'device %s' % context.get_device().name()

    module = SourceModule(src.kernel, options=['-I' + src.dir], no_extern_c=True, cache_dir=False)
    geometry = build(obj, options.bits)
    geometry.load(module)

    lock = Lock()

    camera = Camera(geometry, module, context, lock, size)

    context.pop()
    camera.start()