<s> import laspy <EOL> import numpy as np <EOL> import random <EOL> import sys <EOL> import OpenGL . GL as gl <EOL> import OpenGL . GLU as glu <EOL> import OpenGL . GLUT as glut <EOL> from OpenGL . arrays import vbo <EOL> def run_glviewer ( file_object , mode , dim ) : <EOL> glviewer = pcl_image ( file_object , mode , dim ) <EOL> return ( <NUM_LIT:0> ) <EOL> class VBO_Provider ( ) : <EOL> def __init__ ( self , file_object , vbsize , means , mode , dim , scaled = True ) : <EOL> self . vbos = [ ] <EOL> self . allcolor = False <EOL> start_idx = <NUM_LIT:0> <EOL> self . file_object = file_object <EOL> end_idx = vbsize <EOL> i = <NUM_LIT:1> <EOL> while ( start_idx < len ( ( file_object ) ) ) : <EOL> i += <NUM_LIT:1> <EOL> try : <EOL> end_idx = min ( len ( file_object ) , start_idx + vbsize ) <EOL> print ( "<STR_LIT>" + str ( start_idx ) + "<STR_LIT>" + str ( ( end_idx ) ) ) <EOL> dat = self . slice_file ( start_idx , end_idx , means , scaled ) <EOL> self . set_color_mode ( mode , dim , start_idx , end_idx , dat ) <EOL> _vbo = vbo . VBO ( data = np . array ( dat , dtype = np . float32 ) , <EOL> usage = gl . GL_DYNAMIC_DRAW , target = gl . GL_ARRAY_BUFFER ) <EOL> self . vbos . append ( ( _vbo , end_idx - start_idx ) ) <EOL> start_idx += vbsize <EOL> except Exception , err : <EOL> print ( "<STR_LIT>" ) <EOL> print ( err ) <EOL> def slice_file ( self , start_idx , end_idx , means , scaled ) : <EOL> if scaled : <EOL> return ( np . array ( np . vstack ( ( self . file_object . x [ start_idx : end_idx ] , self . file_object . y [ start_idx : end_idx ] , self . file_object . z [ start_idx : end_idx ] , <EOL> np . zeros ( end_idx - start_idx ) , np . zeros ( end_idx - start_idx ) , np . zeros ( end_idx - start_idx ) ) ) . T ) - means ) <EOL> else : <EOL> scale = np . array ( self . file_object . header . scale + [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , dtype = np . float64 ) <EOL> dat = ( np . array ( np . vstack ( ( self . file_object . X [ start_idx : end_idx ] , self . file_object . Y [ start_idx : end_idx ] , self . file_object . Z [ start_idx : end_idx ] , <EOL> np . zeros ( end_idx - start_idx ) , np . zeros ( end_idx - start_idx ) , np . zeros ( end_idx - start_idx ) ) ) . T ) - means ) <EOL> dat *= ( scale * <NUM_LIT:100> ) <EOL> return ( dat ) <EOL> def bind ( self ) : <EOL> for _vbo in self . vbos : <EOL> _vbo [ <NUM_LIT:0> ] . bind ( ) <EOL> def unbind ( self ) : <EOL> for _vbo in self . vbos : <EOL> _vbo [ <NUM_LIT:0> ] . unbind ( ) <EOL> def draw ( self ) : <EOL> for _vbo in self . vbos : <EOL> _vbo [ <NUM_LIT:0> ] . bind ( ) <EOL> gl . glVertexPointer ( <NUM_LIT:3> , gl . GL_FLOAT , <NUM_LIT> , _vbo [ <NUM_LIT:0> ] ) <EOL> gl . glColorPointer ( <NUM_LIT:3> , gl . GL_FLOAT , <NUM_LIT> , _vbo [ <NUM_LIT:0> ] + <NUM_LIT:12> ) <EOL> gl . glDrawArrays ( gl . GL_POINTS , <NUM_LIT:0> , _vbo [ <NUM_LIT:1> ] ) <EOL> _vbo [ <NUM_LIT:0> ] . unbind ( ) <EOL> def set_color_mode ( self , mode , dim , start_idx , end_idx , data ) : <EOL> if ( mode == "<STR_LIT:default>" ) : <EOL> if ( all ( [ x in self . file_object . point_format . lookup for x in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ] ) ) : <EOL> if ( ( all ( self . file_object . red [ <NUM_LIT:0> : len ( self . file_object ) : ( len ( self . file_object ) / <NUM_LIT:1000> ) ] == <NUM_LIT:0> ) ) and <EOL> ( all ( self . file_object . green [ <NUM_LIT:0> : len ( self . file_object ) : ( len ( self . file_object ) / <NUM_LIT:1000> ) ] == <NUM_LIT:0> ) ) and <EOL> ( all ( self . file_object . blue [ <NUM_LIT:0> : len ( self . file_object ) : ( len ( self . file_object ) / <NUM_LIT:1000> ) ] == <NUM_LIT:0> ) ) ) : <EOL> print ( "<STR_LIT>" ) <EOL> mode = "<STR_LIT>" <EOL> else : <EOL> mode = "<STR_LIT>" <EOL> else : <EOL> mode = "<STR_LIT>" <EOL> if mode == "<STR_LIT>" and not "<STR_LIT>" in self . file_object . point_format . lookup : <EOL> print ( "<STR_LIT>" ) <EOL> mode = "<STR_LIT>" <EOL> if mode in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if type ( self . allcolor ) == bool : <EOL> self . allcolor = self . file_object . reader . get_dimension ( dim ) / float ( np . max ( self . file_object . reader . get_dimension ( dim ) ) ) <EOL> scaled = self . allcolor [ start_idx : end_idx ] + <NUM_LIT:0.1> <EOL> col = np . array ( ( np . vstack ( ( scaled , scaled , scaled ) ) . T ) , dtype = np . float32 ) <EOL> data [ : , <NUM_LIT:3> : <NUM_LIT:6> ] += col <EOL> return ( data ) <EOL> elif ( mode == "<STR_LIT>" or ( mode == "<STR_LIT>" and dim == "<STR_LIT:z>" ) ) : <EOL> if type ( self . allcolor ) == bool : <EOL> self . allcolor = self . heatmap ( self . file_object . z ) <EOL> col = self . allcolor [ start_idx : end_idx ] <EOL> data [ : , <NUM_LIT:3> : <NUM_LIT:6> ] += col <EOL> return ( data ) <EOL> elif ( mode == "<STR_LIT>" and dim != "<STR_LIT:z>" ) : <EOL> if type ( self . allcolor ) == bool : <EOL> self . allcolor = self . heatmap ( self . file_object . reader . get_dimension ( dim ) ) <EOL> col = self . allcolor [ start_idx : end_idx ] <EOL> data [ : , <NUM_LIT:3> : <NUM_LIT:6> ] += col <EOL> return ( data ) <EOL> elif mode == "<STR_LIT>" : <EOL> _max = max ( np . max ( self . file_object . red ) , np . max ( self . file_object . green ) , np . max ( self . file_object . blue ) ) <EOL> _min = min ( np . min ( self . file_object . red ) , np . min ( self . file_object . green ) , np . min ( self . file_object . blue ) ) <EOL> diff = _max - _min <EOL> col = np . array ( np . vstack ( ( self . file_object . red [ start_idx : end_idx ] , self . file_object . green [ start_idx : end_idx ] , self . file_object . blue [ start_idx : end_idx ] ) ) . T , dtype = np . float32 ) <EOL> col -= _min <EOL> col /= diff <EOL> data [ : , <NUM_LIT:3> : <NUM_LIT:6> ] += col <EOL> return ( data ) <EOL> def heatmap ( self , vec , mode = <NUM_LIT:1> ) : <EOL> _max = np . max ( vec ) <EOL> _min = np . min ( vec ) <EOL> diff = _max - _min <EOL> red = ( vec - _min ) / float ( diff ) <EOL> if mode == <NUM_LIT:1> : <EOL> col = np . array ( np . vstack ( ( red ** <NUM_LIT:4> , np . sqrt ( <NUM_LIT> - ( <NUM_LIT:0.5> - red ) ** <NUM_LIT:4> ) , ( <NUM_LIT:1> - red ) ** <NUM_LIT:4> ) ) , dtype = np . float32 ) . T <EOL> else : <EOL> col = np . array ( np . vstack ( ( red ** <NUM_LIT:4> , np . zeros ( self . N ) , ( <NUM_LIT:1> - red ) ** <NUM_LIT:4> ) ) , dtype = np . float32 ) . T <EOL> return ( col ) <EOL> class pcl_image ( ) : <EOL> def __init__ ( self , file_object , mode , dim ) : <EOL> self . file_object = file_object <EOL> self . read_data ( mode , dim ) <EOL> self . movement_granularity = <NUM_LIT:1.0> <EOL> self . look_granularity = <NUM_LIT> <EOL> self . main ( ) <EOL> def main ( self ) : <EOL> self . location = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) <EOL> self . focus = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> self . up = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> self . mousex = <NUM_LIT:0> <EOL> self . mousey = <NUM_LIT:0> <EOL> self . mouse_drag = gl . GL_FALSE <EOL> glut . glutInit ( sys . argv ) <EOL> glut . glutInitDisplayMode ( glut . GLUT_RGB | glut . GLUT_DOUBLE | glut . GLUT_DEPTH ) <EOL> glut . glutInitWindowSize ( <NUM_LIT> , <NUM_LIT> ) <EOL> glut . glutInitWindowPosition ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> glut . glutCreateWindow ( "<STR_LIT>" ) <EOL> glut . glutDisplayFunc ( self . display ) <EOL> glut . glutReshapeFunc ( self . reshape ) <EOL> glut . glutMouseFunc ( self . mouse ) <EOL> glut . glutMotionFunc ( self . mouse_motion ) <EOL> glut . glutKeyboardFunc ( self . keyboard ) <EOL> gl . glClearColor ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> glut . glutTimerFunc ( <NUM_LIT:10> , self . timerEvent , <NUM_LIT:1> ) <EOL> glut . glutMainLoop ( ) <EOL> return <NUM_LIT:0> <EOL> def read_data ( self , mode , dim ) : <EOL> if ( np . max ( self . file_object . x ) - np . min ( self . file_object . x ) ) < <NUM_LIT:1> : <EOL> means = np . array ( [ np . mean ( self . file_object . X , dtype = np . float64 ) , <EOL> np . mean ( self . file_object . Y , dtype = np . float64 ) , <EOL> np . mean ( self . file_object . Z , dtype = np . float64 ) , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> scaled = False <EOL> else : <EOL> means = np . array ( [ np . mean ( self . file_object . x , dtype = np . float64 ) , <EOL> np . mean ( self . file_object . y , dtype = np . float64 ) , <EOL> np . mean ( self . file_object . z , dtype = np . float64 ) , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> scaled = True <EOL> self . N = len ( self . file_object ) <EOL> self . data_buffer = VBO_Provider ( self . file_object , <NUM_LIT> , means , mode , dim , scaled ) <EOL> def reshape ( self , w , h ) : <EOL> print ( "<STR_LIT>" + str ( w ) + "<STR_LIT:U+002CU+0020>" + str ( h ) ) <EOL> ratio = w if h == <NUM_LIT:0> else float ( w ) / h <EOL> gl . glMatrixMode ( gl . GL_PROJECTION ) <EOL> gl . glLoadIdentity ( ) <EOL> gl . glViewport ( <NUM_LIT:0> , <NUM_LIT:0> , w , h ) <EOL> gl . glLoadIdentity ( ) <EOL> glu . gluPerspective ( <NUM_LIT> , float ( ratio ) , <NUM_LIT> , <NUM_LIT> ) ; <EOL> gl . glMatrixMode ( gl . GL_MODELVIEW ) <EOL> def timerEvent ( self , arg ) : <EOL> glut . glutPostRedisplay ( ) <EOL> glut . glutTimerFunc ( <NUM_LIT:10> , self . timerEvent , <NUM_LIT:1> ) <EOL> def draw_points ( self , num ) : <EOL> gl . glEnableClientState ( gl . GL_VERTEX_ARRAY ) <EOL> gl . glEnableClientState ( gl . GL_COLOR_ARRAY ) <EOL> self . data_buffer . draw ( ) <EOL> gl . glDisableClientState ( gl . GL_COLOR_ARRAY ) <EOL> gl . glDisableClientState ( gl . GL_VERTEX_ARRAY ) <EOL> def rotate_vector ( self , vec_rot , vec_about , theta ) : <EOL> d = np . sqrt ( vec_about . dot ( vec_about ) ) <EOL> L = np . array ( ( <NUM_LIT:0> , vec_about [ <NUM_LIT:2> ] , - vec_about [ <NUM_LIT:1> ] , <EOL> - vec_about [ <NUM_LIT:2> ] , <NUM_LIT:0> , vec_about [ <NUM_LIT:0> ] , <EOL> vec_about [ <NUM_LIT:1> ] , - vec_about [ <NUM_LIT:0> ] , <NUM_LIT:0> ) ) <EOL> L . shape = ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> try : <EOL> R = ( np . identity ( <NUM_LIT:3> ) + np . sin ( theta ) / d * L + <EOL> ( <NUM_LIT:1> - np . cos ( theta ) ) / ( d * d ) * ( L . dot ( L ) ) ) <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return ( ) <EOL> return ( vec_rot . dot ( R ) ) <EOL> def display ( self ) : <EOL> gl . glClear ( gl . GL_COLOR_BUFFER_BIT | gl . GL_DEPTH_BUFFER_BIT ) <EOL> gl . glLoadIdentity ( ) <EOL> glu . gluLookAt ( self . location [ <NUM_LIT:0> ] , self . location [ <NUM_LIT:1> ] , self . location [ <NUM_LIT:2> ] , <EOL> self . focus [ <NUM_LIT:0> ] , self . focus [ <NUM_LIT:1> ] , self . focus [ <NUM_LIT:2> ] , <EOL> self . up [ <NUM_LIT:0> ] , self . up [ <NUM_LIT:1> ] , self . up [ <NUM_LIT:2> ] ) <EOL> self . draw_points ( self . N ) <EOL> glut . glutSwapBuffers ( ) <EOL> def camera_reset ( self ) : <EOL> self . location = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) <EOL> self . focus = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> self . up = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> def camera_move ( self , ammount , axis = <NUM_LIT:1> ) : <EOL> if axis == <NUM_LIT:1> : <EOL> pointing = self . focus - self . location <EOL> pnorm = np . sqrt ( pointing . dot ( pointing ) ) <EOL> pointing /= pnorm <EOL> self . location = self . location + ammount * pointing <EOL> self . focus = self . location + pnorm * pointing <EOL> elif axis == <NUM_LIT:2> : <EOL> pointing = self . focus - self . location <EOL> direction = np . cross ( self . up , pointing ) <EOL> direction /= np . sqrt ( direction . dot ( direction ) ) <EOL> self . location = self . location + ammount * direction <EOL> self . focus = self . location + pointing <EOL> def camera_yaw ( self , theta ) : <EOL> pointing = self . focus - self . location <EOL> newpointing = self . rotate_vector ( pointing , self . up , theta ) <EOL> self . focus = newpointing + self . location <EOL> def camera_roll ( self , theta ) : <EOL> self . up = self . rotate_vector ( self . up , self . focus - self . location , theta ) <EOL> def camera_pitch ( self , theta ) : <EOL> pointing = self . focus - self . location <EOL> axis = np . cross ( self . up , pointing ) <EOL> newpointing = self . rotate_vector ( pointing , axis , theta ) <EOL> self . focus = newpointing + self . location <EOL> self . up = np . cross ( newpointing , axis ) <EOL> self . up /= np . sqrt ( self . up . dot ( self . up ) ) <EOL> def mouse ( self , button , state , x , y ) : <EOL> if button == glut . GLUT_LEFT_BUTTON : <EOL> if state == glut . GLUT_DOWN : <EOL> self . mouse_drag = gl . GL_TRUE <EOL> self . mousex = x <EOL> self . mousey = y <EOL> elif state == glut . GLUT_UP and self . mouse_drag : <EOL> self . mouse_drag = gl . GL_FALSE <EOL> elif button == <NUM_LIT:3> : <EOL> pass <EOL> elif button == <NUM_LIT:4> : <EOL> pass <EOL> def mouse_motion ( self , x , y ) : <EOL> if self . mouse_drag : <EOL> self . mousex = x <EOL> self . mousey = y <EOL> def keyboard ( self , key , x , y ) : <EOL> if key == "<STR_LIT:a>" : <EOL> self . camera_yaw ( np . pi / ( self . look_granularity ) ) <EOL> elif key == "<STR_LIT:d>" : <EOL> self . camera_yaw ( - np . pi / self . look_granularity ) <EOL> elif key == "<STR_LIT:w>" : <EOL> self . camera_pitch ( - np . pi / self . look_granularity ) <EOL> elif key == "<STR_LIT:s>" : <EOL> self . camera_pitch ( np . pi / self . look_granularity ) <EOL> elif key == "<STR_LIT:e>" : <EOL> self . camera_roll ( np . pi / self . look_granularity ) <EOL> elif key == "<STR_LIT:q>" : <EOL> self . camera_roll ( - np . pi / self . look_granularity ) <EOL> elif key == "<STR_LIT>" : <EOL> self . camera_move ( self . movement_granularity * <NUM_LIT> ) <EOL> elif key == "<STR_LIT:S>" : <EOL> self . camera_move ( self . movement_granularity * - <NUM_LIT> ) <EOL> elif key == "<STR_LIT:A>" : <EOL> self . camera_move ( self . movement_granularity * <NUM_LIT> , axis = <NUM_LIT:2> ) <EOL> elif key == "<STR_LIT:D>" : <EOL> self . camera_move ( self . movement_granularity * - <NUM_LIT> , axis = <NUM_LIT:2> ) <EOL> elif key in ( "<STR_LIT:R>" , "<STR_LIT:r>" ) : <EOL> self . camera_reset ( ) <EOL> elif key == "<STR_LIT:+>" : <EOL> self . movement_granularity *= <NUM_LIT> <EOL> self . look_granularity /= <NUM_LIT> <EOL> elif key == "<STR_LIT:->" : <EOL> self . movement_granularity /= <NUM_LIT> <EOL> self . look_granularity *= <NUM_LIT> <EOL> elif key in ( "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ) : <EOL> self . set_up_axis ( key ) <EOL> print ( key ) <EOL> pass <EOL> def set_up_axis ( self , key ) : <EOL> if key == "<STR_LIT:x>" : <EOL> self . up = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . focus [ <NUM_LIT:0> ] = self . location [ <NUM_LIT:0> ] <EOL> elif key == "<STR_LIT:y>" : <EOL> self . up = np . array ( [ <NUM_LIT> , <NUM_LIT:1.0> , <NUM_LIT> ] ) <EOL> self . focus [ <NUM_LIT:1> ] = self . location [ <NUM_LIT:1> ] <EOL> elif key == "<STR_LIT:z>" : <EOL> self . up = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.0> ] ) <EOL> self . focus [ <NUM_LIT:2> ] = self . location [ <NUM_LIT:2> ] <EOL> if all ( self . focus == self . location ) : <EOL> self . focus [ { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> , "<STR_LIT:z>" : <NUM_LIT:0> } [ key ] ] += <NUM_LIT> </s>
