diff options
author | Anthony LaTorre <telatorre@gmail.com> | 2011-05-17 16:58:36 -0400 |
---|---|---|
committer | Anthony LaTorre <telatorre@gmail.com> | 2011-05-17 16:58:36 -0400 |
commit | 909309302c83423994e9c1dd36a3309890a67b90 (patch) | |
tree | ade31f97ba43172820efd7bb80d5dd70bffa87c5 /view.py | |
parent | 8df5c2109151613d6ed1c124095c8e6e0f98f3af (diff) | |
download | chroma-909309302c83423994e9c1dd36a3309890a67b90.tar.gz chroma-909309302c83423994e9c1dd36a3309890a67b90.tar.bz2 chroma-909309302c83423994e9c1dd36a3309890a67b90.zip |
added documentation
Diffstat (limited to 'view.py')
-rwxr-xr-x[-rw-r--r--] | view.py | 143 |
1 files changed, 73 insertions, 70 deletions
@@ -1,108 +1,111 @@ +#!/usr/bin/env python import numpy as np from stl import * from geometry import * from materials import * from camera import * from gpu import * - -import sys -import optparse +from transform import * import pygame from pygame.locals import * -parser = optparse.OptionParser('%prog filename.stl') -parser.add_option('-b', '--bits', type='int', dest='bits', help='number of bits for z ordering space axes', default=4) -options, args = parser.parse_args() +def view(geometry, name=''): + mesh = geometry.mesh + lower_bound = np.array([np.min(mesh[:,:,0]), np.min(mesh[:,:,1]), np.min(mesh[:,:,2])]) + upper_bound = np.array([np.max(mesh[:,:,0]), np.max(mesh[:,:,1]), np.max(mesh[:,:,2])]) -if len(args) < 1: - sys.exit(parser.format_help()) + scale = np.linalg.norm(upper_bound-lower_bound)/10.0 -geometry = Geometry() -geometry.add_solid(Solid(read_stl(args[0]), vacuum, vacuum)) -geometry.build(options.bits) + gpu = GPU() + gpu.load_geometry(geometry) -mesh = geometry.mesh -lower_bound = np.array([np.min(mesh[:,:,0]), np.min(mesh[:,:,1]), np.min(mesh[:,:,2])]) -upper_bound = np.array([np.max(mesh[:,:,0]), np.max(mesh[:,:,1]), np.max(mesh[:,:,2])]) + pygame.init() + size = width, height = 800, 600 + screen = pygame.display.set_mode(size) + pygame.display.set_caption(name) -scale = np.linalg.norm(upper_bound-lower_bound)/10.0 + camera = Camera(size) + camera.position((0, upper_bound[1]*5, np.mean([lower_bound[2], upper_bound[2]]))) -gpu = GPU() -gpu.load_geometry(geometry) + origin, direction = camera.get_rays() -pygame.init() -size = width, height = 800, 600 -screen = pygame.display.set_mode(size) -pygame.display.set_caption(os.path.split(args[0])[-1]) + pixels = np.empty(width*height, dtype=np.int32) + states = np.empty(width*height, dtype=np.int32) -camera = Camera(size) -camera.position((0, upper_bound[1]*5, np.mean([lower_bound[2], upper_bound[2]]))) + origin_gpu = cuda.to_device(make_vector(origin)) + direction_gpu = cuda.to_device(make_vector(direction)) -origin, direction = camera.get_rays() + pixels_gpu = cuda.to_device(pixels) + states_gpu = cuda.to_device(states) -pixels = np.empty(width*height, dtype=np.int32) -states = np.empty(width*height, dtype=np.int32) + block_size = 64 + gpu_kwargs = {'block': (block_size,1,1), 'grid': (pixels.size//block_size+1,1)} -origin_gpu = cuda.to_device(make_vector(origin)) -direction_gpu = cuda.to_device(make_vector(direction)) + def render(): + gpu.call(np.int32(pixels.size), origin_gpu, direction_gpu, np.int32(geometry.first_leaf), states_gpu, pixels_gpu, **gpu_kwargs) -pixels_gpu = cuda.to_device(pixels) -states_gpu = cuda.to_device(states) + cuda.memcpy_dtoh(pixels, pixels_gpu) -block_size = 64 -gpu_kwargs = {'block': (block_size,1,1), 'grid': (pixels.size//block_size+1,1)} + pygame.surfarray.blit_array(screen, pixels.reshape(size)) + pygame.display.flip() -def render(): - gpu.call(np.int32(pixels.size), origin_gpu, direction_gpu, np.int32(geometry.first_leaf), states_gpu, pixels_gpu, **gpu_kwargs) + render() - cuda.memcpy_dtoh(pixels, pixels_gpu) - - pygame.surfarray.blit_array(screen, pixels.reshape(size)) - pygame.display.flip() + done = False + clicked = False + to_origin = np.array([0,-1,0]) + while not done: + for event in pygame.event.get(): + if event.type == MOUSEBUTTONDOWN: + if event.button == 4: + cuda_translate(np.int32(pixels.size), origin_gpu, gpuarray.vec.make_float3(*(scale*to_origin)), **gpu_kwargs) + render() -render() + if event.button == 5: + cuda_translate(np.int32(pixels.size), origin_gpu, gpuarray.vec.make_float3(*(-scale*to_origin)), **gpu_kwargs) + render() -from transform import * + if event.button == 1: + clicked = True + mouse_position = pygame.mouse.get_rel() -done = False -clicked = False -to_origin = np.array([0,-1,0]) -while not done: - for event in pygame.event.get(): - if event.type == MOUSEBUTTONDOWN: - if event.button == 4: - cuda_translate(np.int32(pixels.size), origin_gpu, gpuarray.vec.make_float3(*(scale*to_origin)), **gpu_kwargs) - render() + if event.type == MOUSEBUTTONUP: + if event.button == 1: + clicked = False - if event.button == 5: - cuda_translate(np.int32(pixels.size), origin_gpu, gpuarray.vec.make_float3(*(-scale*to_origin)), **gpu_kwargs) - render() + if event.type == MOUSEMOTION and clicked: + movement = pygame.mouse.get_rel()[0]/float(width) + + if movement == 0: + continue - if event.button == 1: - clicked = True - mouse_position = pygame.mouse.get_rel() + cuda_rotate(np.int32(pixels.size), origin_gpu, np.float32(movement*2*np.pi), gpuarray.vec.make_float3(0,0,1), **gpu_kwargs) - if event.type == MOUSEBUTTONUP: - if event.button == 1: - clicked = False + cuda_rotate(np.int32(pixels.size), direction_gpu, np.float32(movement*2*np.pi), gpuarray.vec.make_float3(0,0,1), **gpu_kwargs) - if event.type == MOUSEMOTION and clicked: - movement = pygame.mouse.get_rel()[0]/float(width) + to_origin = rotate(to_origin, movement*2*np.pi, (0,0,1)) - if movement == 0: - continue + render() - cuda_rotate(np.int32(pixels.size), origin_gpu, np.float32(movement*2*np.pi), gpuarray.vec.make_float3(0,0,1), **gpu_kwargs) + if event.type == KEYUP or event.type == KEYDOWN: + if event.key == K_ESCAPE: + done = True + break - cuda_rotate(np.int32(pixels.size), direction_gpu, np.float32(movement*2*np.pi), gpuarray.vec.make_float3(0,0,1), **gpu_kwargs) +if __name__ == '__main__': + import sys + import optparse - to_origin = rotate(to_origin, movement*2*np.pi, (0,0,1)) + parser = optparse.OptionParser('%prog filename.stl') + parser.add_option('-b', '--bits', type='int', dest='bits', help='number of bits for z ordering space axes', default=4) + options, args = parser.parse_args() - render() + if len(args) < 1: + sys.exit(parser.format_help()) - if event.type == KEYUP or event.type == KEYDOWN: - if event.key == K_ESCAPE: - done = True - break + geometry = Geometry() + geometry.add_solid(Solid(read_stl(args[0]), vacuum, vacuum)) + geometry.build(options.bits) + view(geometry, os.path.split(args[0])[-1]) |