<s> import numpy <EOL> import collada <EOL> from meshtool . filters . panda_filters import pdae_utils <EOL> from meshtool . args import FileArgument , FilterArgument <EOL> from meshtool . filters . base_filters import SimplifyFilter , FilterException <EOL> def add_back_pm ( mesh , pm_file , percent ) : <EOL> refinements = pdae_utils . readPDAE ( pm_file ) <EOL> num_to_load = len ( refinements ) * ( percent / <NUM_LIT> ) <EOL> num_to_load = min ( num_to_load , len ( refinements ) ) <EOL> num_to_load = int ( round ( num_to_load ) ) <EOL> if num_to_load <= <NUM_LIT:0> : <EOL> return mesh <EOL> assert ( len ( mesh . geometries ) == <NUM_LIT:1> ) <EOL> geom = mesh . geometries [ <NUM_LIT:0> ] <EOL> assert ( len ( geom . primitives ) == <NUM_LIT:1> ) <EOL> triset = geom . primitives [ <NUM_LIT:0> ] <EOL> assert ( isinstance ( triset , collada . triangleset . TriangleSet ) ) <EOL> indices2stack = [ triset . vertex_index . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) , triset . normal_index . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) , triset . texcoord_indexset [ <NUM_LIT:0> ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) ] <EOL> stacked_indices = numpy . hstack ( indices2stack ) . flatten ( ) . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> unique_stacked_indices , index_map , inverse_map = numpy . unique ( stacked_indices . view ( [ ( '<STR_LIT>' , stacked_indices . dtype ) ] * stacked_indices . shape [ <NUM_LIT:1> ] ) , return_index = True , return_inverse = True ) <EOL> unique_stacked_indices = unique_stacked_indices . view ( stacked_indices . dtype ) . reshape ( - <NUM_LIT:1> , stacked_indices . shape [ <NUM_LIT:1> ] ) <EOL> index_map = numpy . cast [ '<STR_LIT>' ] ( index_map ) <EOL> inverse_map = numpy . cast [ '<STR_LIT>' ] ( inverse_map ) <EOL> sorted_map = numpy . cast [ '<STR_LIT>' ] ( numpy . argsort ( index_map ) ) <EOL> backwards_map = numpy . zeros_like ( sorted_map ) <EOL> backwards_map [ sorted_map ] = numpy . arange ( len ( sorted_map ) , dtype = numpy . uint32 ) <EOL> unique_stacked_indices = unique_stacked_indices [ sorted_map ] <EOL> inverse_map = backwards_map [ inverse_map ] <EOL> alldata = [ triset . vertex , triset . normal , triset . texcoordset [ <NUM_LIT:0> ] ] <EOL> data2stack = [ ] <EOL> for idx , data in enumerate ( alldata ) : <EOL> data2stack . append ( data [ unique_stacked_indices [ : , idx ] ] ) <EOL> unique_stacked_data = numpy . hstack ( data2stack ) . flatten ( ) <EOL> num_added_verts = <NUM_LIT:0> <EOL> num_added_tris = <NUM_LIT:0> <EOL> for refinement_index in range ( num_to_load ) : <EOL> for op in refinements [ refinement_index ] : <EOL> if op [ <NUM_LIT:0> ] == pdae_utils . PM_OP . VERTEX_ADDITION : <EOL> num_added_verts += <NUM_LIT:1> <EOL> elif op [ <NUM_LIT:0> ] == pdae_utils . PM_OP . TRIANGLE_ADDITION : <EOL> num_added_tris += <NUM_LIT:1> <EOL> cur_vertex = len ( unique_stacked_data ) <EOL> cur_triangle = len ( inverse_map ) <EOL> unique_stacked_data = numpy . append ( unique_stacked_data , numpy . zeros ( num_added_verts * <NUM_LIT:8> , dtype = numpy . float32 ) ) <EOL> inverse_map = numpy . append ( inverse_map , numpy . zeros ( num_added_tris * <NUM_LIT:3> , dtype = numpy . uint32 ) ) <EOL> for refinement_index in range ( num_to_load ) : <EOL> for operation in refinements [ refinement_index ] : <EOL> vals = list ( operation ) <EOL> op = vals . pop ( <NUM_LIT:0> ) <EOL> if op == pdae_utils . PM_OP . VERTEX_ADDITION : <EOL> vx , vy , vz , nx , ny , nz , s , t = map ( float , vals ) <EOL> unique_stacked_data [ cur_vertex : cur_vertex + <NUM_LIT:8> ] = vx , vy , vz , nx , ny , nz , s , t <EOL> cur_vertex += <NUM_LIT:8> <EOL> elif op == pdae_utils . PM_OP . TRIANGLE_ADDITION : <EOL> v1 , v2 , v3 = map ( int , vals ) <EOL> inverse_map [ cur_triangle : cur_triangle + <NUM_LIT:3> ] = v1 , v2 , v3 <EOL> cur_triangle += <NUM_LIT:3> <EOL> elif op == pdae_utils . PM_OP . INDEX_UPDATE : <EOL> tindex , vindex = map ( int , vals ) <EOL> inverse_map [ tindex ] = vindex <EOL> oldgeom = geom <EOL> mesh . geometries . pop ( <NUM_LIT:0> ) <EOL> unique_stacked_data . shape = ( - <NUM_LIT:1> , <NUM_LIT:8> ) <EOL> vertex = numpy . copy ( unique_stacked_data [ : , <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> normal = numpy . copy ( unique_stacked_data [ : , <NUM_LIT:3> : <NUM_LIT:6> ] ) <EOL> uvs = numpy . copy ( unique_stacked_data [ : , <NUM_LIT:6> : <NUM_LIT:8> ] ) <EOL> vert_src = collada . source . FloatSource ( "<STR_LIT>" , vertex , ( '<STR_LIT:X>' , '<STR_LIT:Y>' , '<STR_LIT>' ) ) <EOL> normal_src = collada . source . FloatSource ( "<STR_LIT>" , normal , ( '<STR_LIT:X>' , '<STR_LIT:Y>' , '<STR_LIT>' ) ) <EOL> uv_src = collada . source . FloatSource ( "<STR_LIT>" , uvs , ( '<STR_LIT:S>' , '<STR_LIT:T>' ) ) <EOL> geom = collada . geometry . Geometry ( mesh , geom . id , geom . name , [ vert_src , normal_src , uv_src ] ) <EOL> input_list = collada . source . InputList ( ) <EOL> input_list . addInput ( <NUM_LIT:0> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> input_list . addInput ( <NUM_LIT:0> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> input_list . addInput ( <NUM_LIT:0> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> triset = geom . createTriangleSet ( inverse_map , input_list , triset . material ) <EOL> geom . primitives . append ( triset ) <EOL> mesh . geometries . append ( geom ) <EOL> for scene in mesh . scenes : <EOL> nodes_to_check = [ ] <EOL> nodes_to_check . extend ( scene . nodes ) <EOL> while len ( nodes_to_check ) > <NUM_LIT:0> : <EOL> curnode = nodes_to_check . pop ( ) <EOL> for i , node in enumerate ( curnode . children ) : <EOL> if isinstance ( node , collada . scene . Node ) : <EOL> nodes_to_check . append ( node ) <EOL> elif isinstance ( node , collada . scene . GeometryNode ) : <EOL> if node . geometry == oldgeom : <EOL> node . geometry = geom <EOL> return mesh <EOL> def FilterGenerator ( ) : <EOL> class AddBackPm ( SimplifyFilter ) : <EOL> def __init__ ( self ) : <EOL> super ( AddBackPm , self ) . __init__ ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . arguments . append ( FileArgument ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . arguments . append ( FilterArgument ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def apply ( self , mesh , pm_file , percent ) : <EOL> try : <EOL> pmin = open ( pm_file , '<STR_LIT:r>' ) <EOL> except IOError : <EOL> raise FilterException ( "<STR_LIT>" ) <EOL> try : <EOL> percent = float ( percent ) <EOL> except ValueError : <EOL> percent = None <EOL> if percent is None or percent < <NUM_LIT:0.0> : <EOL> raise FilterException ( "<STR_LIT>" ) <EOL> mesh = add_back_pm ( mesh , pmin , percent ) <EOL> return mesh <EOL> return AddBackPm ( ) <EOL> from meshtool . filters import factory <EOL> factory . register ( FilterGenerator ( ) . name , FilterGenerator ) </s>
