def generate_rigid(filename, density=1000.0): cmd = Sofa.build_dir() + "/bin/GenerateRigid" args = filename try: output = Popen([cmd, args], stdout=PIPE) line = output.stdout.read().split("\n") except OSError: print "error when calling GenerateRigid, do you have GenerateRigid built in SOFA ?" raise start = 2 # print line mass = float(line[start].split(" ")[1]) volm = float(line[start + 1].split(" ")[1]) inrt = map(float, line[start + 2].split(" ")[1:]) com = map(float, line[start + 3].split(" ")[1:]) # TODO extract principal axes basis if needed # or at least say that we screwd up res = MassInfo() # by default, GenerateRigid assumes 1000 kg/m^3 already res.mass = (density / 1000.0) * mass res.inertia = [mass * x for x in inrt] res.com = com return res
def generate_rigid(filename, density=1000.0, scale=[1, 1, 1], rotation=[0, 0, 0], rigidFilename=None): # TODO bind GenerateRigid # - faster than writing in a file # - more robust (if several processes try to work in the same file) if rigidFilename is None: tmpfilename = Tools.path(__file__) + "/tmp.rigid" else: tmpfilename = rigidFilename cmdRel = [ 'GenerateRigid', filename, tmpfilename, str(density), str(scale[0]), str(scale[1]), str(scale[2]), str(rotation[0]), str(rotation[1]), str(rotation[2]) ] cmd = list(cmdRel) cmd[0] = Sofa.build_dir() + '/bin/' + cmd[0] #print cmd try: output = Popen(cmd, stdout=PIPE) except OSError: # try the debug version cmd[0] += 'd' try: output = Popen(cmd, stdout=PIPE) except OSError: try: #try if it is accessible from PATH output = Popen(cmdRel, stdout=PIPE) except OSError: # try the debug version cmdRel[0] += 'd' try: output = Popen(cmdRel, stdout=PIPE) except OSError: print 'error when calling GenerateRigid, do you have GenerateRigid built in SOFA?' raise output.communicate() # wait until Popen command is finished!!! return read_rigid(tmpfilename)
def generate_rigid(filename, density = 1000.0, scale=[1,1,1], rotation=[0,0,0], rigidFilename=None): # TODO bind GenerateRigid # - faster than writing in a file # - more robust (if several processes try to work in the same file) if rigidFilename is None: tmpfilename = Tools.path( __file__ ) +"/tmp.rigid" else: tmpfilename = rigidFilename cmdRel = [ 'GenerateRigid', filename, tmpfilename, str(density), str(scale[0]), str(scale[1]), str(scale[2]), str(rotation[0]), str(rotation[1]), str(rotation[2]) ] cmd = list(cmdRel) cmd[0] = Sofa.build_dir() + '/bin/' + cmd[0] #print cmd try: output = Popen(cmd, stdout=PIPE) except OSError: # try the debug version cmd[0] += 'd' try: output = Popen(cmd, stdout=PIPE) except OSError: try: #try if it is accessible from PATH output = Popen(cmdRel, stdout=PIPE) except OSError: # try the debug version cmdRel[0] += 'd' try: output = Popen(cmdRel, stdout=PIPE) except OSError: print 'error when calling GenerateRigid, do you have GenerateRigid built in SOFA?' raise output.communicate() # wait until Popen command is finished!!! return read_rigid(tmpfilename)
def __init__(self, name): directory = Sofa.build_dir() import platform system = platform.system() extension = 'so' if system == 'Windows': extension = 'dll' elif system == 'Darwin': extension = 'dylib' prefix = 'lib' suffix = extension self.dll = ctypes.CDLL('{}/lib/{}{}.{}'.format(directory, prefix, name, suffix)) self.dll.argtypes.argtypes = [ type(self.dll.argtypes) ] self.dll.argtypes.restype = ctypes.c_char_p self.dll.restype.argtypes = [ type(self.dll.restype) ] self.dll.restype.restype = ctypes.c_char_p
def __init__(self, name): directory = Sofa.build_dir() import platform system = platform.system() extension = "so" if system == "Windows": extension = "dll" elif system == "Darwin": extension = "dylib" prefix = "lib" suffix = extension self.dll = ctypes.CDLL("{}/lib/{}{}.{}".format(directory, prefix, name, suffix)) self.dll.argtypes.argtypes = [type(self.dll.argtypes)] self.dll.argtypes.restype = ctypes.c_char_p self.dll.restype.argtypes = [type(self.dll.restype)] self.dll.restype.restype = ctypes.c_char_p
def generate_rigid(filename, density = 1000.0, scale=[1,1,1], rotation=[0,0,0]): # TODO bind GenerateRigid # - faster than writing in a file # - more robust (if several processes try to work in the same file) tmpfilename = Tools.path( __file__ ) +"/tmp.rigid" cmd = [ Sofa.build_dir() + '/bin/GenerateRigid', filename, tmpfilename, str(density), str(scale[0]), str(scale[1]), str(scale[2]), str(rotation[0]), str(rotation[1]), str(rotation[2]) ] # print cmd try: output = Popen(cmd, stdout=PIPE) except OSError: # try the debug version cmd[0] += 'd' try: output = Popen(cmd, stdout=PIPE) except OSError: print 'error when calling GenerateRigid, do you have GenerateRigid built in SOFA?' raise output.communicate() # wait until Popen command is finished!!! # GenerateRigid output is stored in the file tmpfilename rigidFile = open( tmpfilename, "r" ) line = list( rigidFile ) rigidFile.close() # for i in xrange(len(line)): # print str(i) + str(line[i]) start = 1 res = MassInfo() res.mass = float( line[start].split(' ')[1] ) #volm = float( line[start + 1].split(' ')[1] ) res.com = map(float, line[start + 3].split(' ')[1:] ) inertia = map(float, line[start + 2].split(' ')[1:] ) # pick inertia matrix from file res.inertia = array( [res.mass * x for x in inertia] ).reshape( 3, 3 ) # convert it in numpy 3x3 matrix # extracting principal axes basis and corresponding rotation and diagonal inertia if inertia[1]>1e-5 or inertia[2]>1e-5 or inertia[5]>1e-5 : # if !diagonal (1e-5 seems big but the precision from a mesh is poor) # print res.inertia U, res.diagonal_inertia, V = linalg.svd(res.inertia) # det should be 1->rotation or -1->reflexion if linalg.det(U) < 0 : # reflexion # made it a rotation by negating a column # print "REFLEXION" U[:,0] = -U[:,0] res.inertia_rotation = quat.from_matrix( U ) # print "generate_rigid not diagonal U" +str(U) # print "generate_rigid not diagonal V" +str(V) # print "generate_rigid not diagonal d" +str(res.diagonal_inertia) else : res.diagonal_inertia = res.inertia.diagonal() res.inertia_rotation = [0,0,0,1] # print "generate_rigid " + str(res.mass) + " " + str( res.inertia ) + " " + str( res.diagonal_inertia ) return res