<s> from meshtool . filters . base_filters import OptimizationFilter <EOL> import collada <EOL> import numpy <EOL> import inspect <EOL> args , varargs , keywords , defaults = inspect . getargspec ( numpy . unique ) <EOL> if '<STR_LIT>' not in args : <EOL> numpy . unique = numpy . unique1d <EOL> def optimizeSources ( mesh ) : <EOL> for geom in mesh . geometries : <EOL> src_by_semantic = { } <EOL> for prim_index , prim in enumerate ( geom . primitives ) : <EOL> if type ( prim ) is not collada . triangleset . TriangleSet : <EOL> continue <EOL> inpl = prim . getInputList ( ) . getList ( ) <EOL> for offset , semantic , srcid , setid in inpl : <EOL> src = geom . sourceById [ srcid [ <NUM_LIT:1> : ] ] <EOL> semantic_sources = src_by_semantic . get ( semantic , { } ) <EOL> semantic_sources [ src . id ] = src <EOL> src_by_semantic [ semantic ] = semantic_sources <EOL> for semantic , sources in src_by_semantic . iteritems ( ) : <EOL> source_placements = { } <EOL> to_concat = [ ] <EOL> cur_index = <NUM_LIT:0> <EOL> for src in sources . itervalues ( ) : <EOL> srcid = src . id <EOL> components = src . components <EOL> to_concat . append ( src . data ) <EOL> source_placements [ src . id ] = cur_index <EOL> cur_index += len ( src . data ) <EOL> new_data = numpy . concatenate ( to_concat ) <EOL> unique_data , index_locs = numpy . unique ( new_data . view ( [ ( '<STR_LIT>' , new_data . dtype ) ] * new_data . shape [ <NUM_LIT:1> ] ) , return_inverse = True ) <EOL> unique_data = unique_data . view ( new_data . dtype ) . reshape ( - <NUM_LIT:1> , new_data . shape [ <NUM_LIT:1> ] ) <EOL> base_source_name = srcid + '<STR_LIT>' <EOL> source_name = base_source_name <EOL> ct = <NUM_LIT:0> <EOL> while source_name in geom . sourceById : <EOL> source_name = '<STR_LIT>' % ( base_source_name , ct ) <EOL> ct += <NUM_LIT:1> <EOL> new_source = collada . source . FloatSource ( source_name , unique_data , components ) <EOL> geom . sourceById [ source_name ] = new_source <EOL> src_by_semantic [ semantic ] = ( source_placements , source_name , unique_data , index_locs ) <EOL> prims_to_delete = [ ] <EOL> prims_to_add = [ ] <EOL> for prim_index , prim in enumerate ( geom . primitives ) : <EOL> if type ( prim ) is not collada . triangleset . TriangleSet : <EOL> continue <EOL> old_input_list = prim . getInputList ( ) . getList ( ) <EOL> inpl = collada . source . InputList ( ) <EOL> new_index = numpy . arange ( len ( old_input_list ) * len ( prim . index ) * <NUM_LIT:3> ) <EOL> new_index . shape = ( len ( prim . index ) , <NUM_LIT:3> , len ( old_input_list ) ) <EOL> new_offset = <NUM_LIT:0> <EOL> for offset , semantic , srcid , setid in old_input_list : <EOL> ( source_placements , source_name , unique_data , index_locs ) = src_by_semantic [ semantic ] <EOL> map_start = source_placements [ srcid [ <NUM_LIT:1> : ] ] <EOL> map_end = map_start + len ( geom . sourceById [ srcid [ <NUM_LIT:1> : ] ] ) <EOL> new_index [ : , : , new_offset ] = index_locs [ map_start : map_end ] [ prim . index [ : , : , offset ] ] <EOL> offset = new_offset <EOL> new_offset += <NUM_LIT:1> <EOL> srcid = "<STR_LIT:#>" + source_name <EOL> inpl . addInput ( offset , semantic , srcid , setid ) <EOL> prims_to_add . append ( ( new_index , inpl , prim . material ) ) <EOL> prims_to_delete . append ( prim_index ) <EOL> for i in sorted ( prims_to_delete , reverse = True ) : <EOL> del geom . primitives [ i ] <EOL> for new_index , inpl , mat in prims_to_add : <EOL> newtriset = geom . createTriangleSet ( new_index , inpl , mat ) <EOL> geom . primitives . append ( newtriset ) <EOL> def FilterGenerator ( ) : <EOL> class OptimizeSourcesFilter ( OptimizationFilter ) : <EOL> def __init__ ( self ) : <EOL> super ( OptimizeSourcesFilter , self ) . __init__ ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> def apply ( self , mesh ) : <EOL> optimizeSources ( mesh ) <EOL> return mesh <EOL> return OptimizeSourcesFilter ( ) <EOL> from meshtool . filters import factory <EOL> factory . register ( FilterGenerator ( ) . name , FilterGenerator ) </s>
