def __init__(self, node, filepath, scale3d, offset, name_suffix='', generatedDir=None): r = Quaternion.to_euler(offset[3:]) * 180.0 / math.pi self.node = node.createChild('collision' + name_suffix) # node self.loader = SofaPython.Tools.meshLoader(self.node, filename=filepath, name='loader', scale3d=concat(scale3d), translation=concat( offset[:3]), rotation=concat(r), triangulate=True) self.topology = self.node.createObject('MeshTopology', name='topology', src='@loader') self.dofs = self.node.createObject('MechanicalObject', name='dofs', template='Vec3' + template_suffix) self.triangles = self.node.createObject('TriangleModel', name='model') if generatedDir is None: self.mapping = self.node.createObject('LinearMapping', template='Affine,Vec3' + template_suffix, name='mapping') else: serialization.importLinearMapping( self.node, generatedDir + "_collisionmapping.json") self.normals = None
def __init__(self, node, filepath, scale3d, offset, name_suffix='', generatedDir=None): r = Quaternion.to_euler(offset[3:]) * 180.0 / math.pi global idxVisualModel self.node = node.createChild('visual' + name_suffix) # node self.model = self.node.createObject('VisualModel', name='visual' + str(idxVisualModel), fileMesh=filepath, scale3d=concat(scale3d), translation=concat(offset[:3]), rotation=concat(r)) if generatedDir is None: self.mapping = self.node.createObject( 'LinearMapping', template='Affine,ExtVec3f', name='mapping') else: serialization.importLinearMapping( self.node, generatedDir + "_visualmapping.json") idxVisualModel += 1
def setManually(self, filepath=None, offset=[[0,0,0,0,0,0,1]], voxelSize=0.01, density=1000, generatedDir=None): if len(offset) == 0: Sofa.msg_error("RigidScale.API","ShearlessAffineBody should have at least 1 ShearLessAffine") return self.framecom = Frame.Frame() self.bodyOffset = Frame.Frame([0,0,0,0,0,0,1]) path_affine_rigid = '@'+ Tools.node_path_rel(self.affineNode, self.rigidNode) path_affine_scale = '@'+ Tools.node_path_rel(self.affineNode, self.scaleNode) if len(offset) == 1: self.frame = [Frame.Frame(offset[0])] str_position = "" for p in offset: str_position = str_position + concat(p) + " " ### scene creation # rigid dof self.rigidDofs = self.rigidNode.createObject('MechanicalObject', template='Rigid3'+template_suffix, name='dofs', position=str_position, rest_position=str_position) # scale dofs self.scaleDofs = self.scaleNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='dofs', position=concat([1,1,1]*len(offset))) positiveNode = self.scaleNode.createChild('positive') positiveNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='positivescaleDOFs') positiveNode.createObject('DifferenceFromTargetMapping', template='Vec3'+template_suffix+',Vec3'+template_suffix, applyRestPosition=1, targets=concat(target_scale)) positiveNode.createObject('UniformCompliance', isCompliance=1, compliance=0) positiveNode.createObject('UnilateralConstraint') positiveNode.createObject('Stabilization', name='Stabilization') # affine dofs self.affineDofs = self.affineNode.createObject('MechanicalObject', template='Affine', name='parent', showObject=0) self.affineNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3,Affine', input1=path_affine_rigid, input2=path_affine_scale, output='@.', autoInit='1', printLog='0') if filepath: self.image = SofaImage.API.Image(self.affineNode, name="image_" + self.name, imageType="ImageUC") self.shapeFunction = Flexible.API.ShapeFunction(self.affineNode) if generatedDir is None: self.image.addMeshLoader(filepath, value=1, insideValue=1, scale=scale3d) # TODO support multiple meshes closingValue=1, self.image.addMeshToImage(voxelSize) self.shapeFunction.addVoronoi(self.image, position='@dofs.rest_position') # mass self.affineMassNode = self.affineNode.createChild('mass') self.affineMassNode.createObject('TransferFunction', name='density', template='ImageUC,ImageD', inputImage='@../image.image', param='0 0 1 '+str(density)) self.affineMassNode.createObject('MechanicalObject', template='Vec3'+template_suffix) self.affineMassNode.createObject('LinearMapping', template='Affine,Vec3'+template_suffix) self.affineMassNode.createObject('MassFromDensity', name='MassFromDensity', template='Affine,ImageD', image='@density.outputImage', transform='@../image.transform', lumping='0') self.mass = self.affineNode.createObject('AffineMass', massMatrix='@mass/MassFromDensity.massMatrix') else: self.image.addContainer(filename=self.node.name + "_rasterization.raw", directory=generatedDir) self.shapeFunction.shapeFunction = serialization.importImageShapeFunction(self.affineNode, generatedDir+self.node.name+"_SF_indices.raw", generatedDir+self.node.name+"_SF_weights.raw", 'dofs') self.mass = serialization.importAffineMass(self.affineNode, generatedDir+self.node.name+"_affinemass.json") # computation of the object mass center massInfo = SofaPython.mass.RigidMassInfo() massInfo.setFromMesh(filepath, density, [1,1,1]) # get the object mass center self.framecom.rotation = massInfo.inertia_rotation self.framecom.translation = massInfo.com else: print "You need a mesh to create an articulated system" self.frame = [] for o in offset: self.frame.append(Frame.Frame(o))
def __init__(self, node, filepath, scale3d, offset, name_suffix='', generatedDir=None): r = Quaternion.to_euler(offset[3:]) * 180.0 / math.pi global idxVisualModel; self.node = node.createChild('visual'+name_suffix) # node self.model = self.node.createObject('VisualModel', name='visual'+str(idxVisualModel), fileMesh=filepath, scale3d=concat(scale3d), translation=concat(offset[:3]), rotation=concat(r)) if generatedDir is None: self.mapping = self.node.createObject('LinearMapping', template='Affine,ExtVec3f', name='mapping') else: serialization.importLinearMapping(self.node, generatedDir+"_visualmapping.json") idxVisualModel+=1
def setFromMesh(self, filepath, density=1000, offset=[0,0,0,0,0,0,1], scale3d=[1,1,1], voxelSize=0.01, numberOfPoints=1, generatedDir=None): # variables self.bodyOffset = Frame.Frame(offset) path_affine_rigid = '@' + Tools.node_path_rel(self.affineNode, self.rigidNode) path_affine_scale = '@' + Tools.node_path_rel(self.affineNode, self.scaleNode) massInfo = SofaPython.mass.RigidMassInfo() massInfo.setFromMesh(filepath, density, scale3d) self.image = SofaImage.API.Image(self.node, name="image_" + self.name, imageType="ImageUC") self.image.node.addChild(self.affineNode) # for initialization self.image.node.addChild(self.rigidNode) # for initialization self.shapeFunction = Flexible.API.ShapeFunction(self.rigidNode) if generatedDir is None: self.image.addMeshLoader(filepath, value=1, insideValue=1, offset=offset, scale=scale3d) # TODO support multiple meshes closingValue=1, self.image.addMeshToImage(voxelSize) # rigid dofs self.sampler = SofaImage.API.Sampler(self.rigidNode) self.sampler.addImageSampler(self.image, numberOfPoints) self.rigidDofs = self.sampler.addMechanicalObject('Rigid3'+template_suffix) else: self.image.addContainer(filename=self.node.name+"_rasterization.raw", directory=generatedDir) self.rigidDofs = serialization.importRigidDofs(self.rigidNode, generatedDir+"/"+self.node.name+'_dofs.json') # scale dofs self.scaleDofs = self.scaleNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='dofs', position=concat([1,1,1]*numberOfPoints)) positiveNode = self.scaleNode.createChild('positive') positiveNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='positivescaleDOFs') positiveNode.createObject('DifferenceFromTargetMapping', template='Vec3d,Vec3'+template_suffix, applyRestPosition=1, targets=concat(target_scale)) positiveNode.createObject('UniformCompliance', isCompliance=1, compliance=0) positiveNode.createObject('UnilateralConstraint') positiveNode.createObject('Stabilization', name='Stabilization') # affine dofs self.affineDofs = self.affineNode.createObject('MechanicalObject', template='Affine', name='dofs') self.affineNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3d,Affine', input1=path_affine_rigid, input2=path_affine_scale, output='@.', autoInit='1', printLog='0') # shapefunction and mass if generatedDir is None: self.shapeFunction.addVoronoi(self.image, position='@dofs.rest_position') # mass densityImage = self.image.createTransferFunction(self.affineNode, "density", param='0 0 1 '+str(density)) affineMass = Flexible.API.AffineMass(self.affineNode) affineMass.massFromDensityImage(self.affineNode, densityImage=densityImage) self.mass = affineMass.mass else: self.shapeFunction.shapeFunction = serialization.importImageShapeFunction(self.affineNode, generatedDir+self.node.name+"_SF_indices.raw", generatedDir+self.node.name+"_SF_weights.raw", 'dofs') self.mass = serialization.importAffineMass(self.affineNode, generatedDir+self.node.name+"_affinemass.json") # hack to get the frame position self.node.init() for p in self.rigidDofs.position: p.extend([0,0,0,1]) self.frame.append(Frame.Frame(p))
def __init__(self, node, filepath, scale3d, offset, name_suffix='', generatedDir=None): r = Quaternion.to_euler(offset[3:]) * 180.0 / math.pi self.node = node.createChild('collision'+name_suffix) # node self.loader = SofaPython.Tools.meshLoader(self.node, filename=filepath, name='loader', scale3d=concat(scale3d), translation=concat(offset[:3]) , rotation=concat(r), triangulate=True) self.topology = self.node.createObject('MeshTopology', name='topology', src='@loader') self.dofs = self.node.createObject('MechanicalObject', name='dofs', template='Vec3'+template_suffix) self.triangles = self.node.createObject('TriangleModel', name='model') if generatedDir is None: self.mapping = self.node.createObject('LinearMapping', template='Affine,Vec3'+template_suffix, name='mapping') else: serialization.importLinearMapping(self.node, generatedDir+"_collisionmapping.json") self.normals = None
def setFromMesh(self, filepath, density=1000, offset=[0,0,0,0,0,0,1], scale3d=[1,1,1], voxelSize=0.01, numberOfPoints=1, generatedDir=None): # variables self.bodyOffset = Frame.Frame(offset) path_affine_rigid = '@' + Tools.node_path_rel(self.affineNode, self.rigidNode) path_affine_scale = '@' + Tools.node_path_rel(self.affineNode, self.scaleNode) massInfo = SofaPython.mass.RigidMassInfo() massInfo.setFromMesh(filepath, density, scale3d) self.image = SofaImage.API.Image(self.node, name="image_" + self.name, imageType="ImageUC") self.image.node.addChild(self.affineNode) # for initialization self.image.node.addChild(self.rigidNode) # for initialization self.shapeFunction = Flexible.API.ShapeFunction(self.rigidNode) if generatedDir is None: self.image.addMeshLoader(filepath, value=1, insideValue=1, offset=offset, scale=scale3d) # TODO support multiple meshes closingValue=1, self.image.addMeshToImage(voxelSize) # rigid dofs self.sampler = SofaImage.API.Sampler(self.rigidNode) self.sampler.addImageSampler(self.image, numberOfPoints) self.rigidDofs = self.sampler.addMechanicalObject('Rigid3'+template_suffix) else: self.image.addContainer(filename=self.node.name+"_rasterization.raw", directory=generatedDir) self.rigidDofs = serialization.importRigidDofs(self.rigidNode, generatedDir+"/"+self.node.name+'_dofs.json') # scale dofs self.scaleDofs = self.scaleNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='dofs', position=concat([1,1,1]*numberOfPoints)) positiveNode = self.scaleNode.createChild('positive') positiveNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='positivescaleDOFs') positiveNode.createObject('DifferenceFromTargetMapping', template='Vec3d,Vec3'+template_suffix, applyRestPosition=1, targets=concat(target_scale)) positiveNode.createObject('UniformCompliance', isCompliance=1, compliance=0) positiveNode.createObject('UnilateralConstraint') # affine dofs self.affineDofs = self.affineNode.createObject('MechanicalObject', template='Affine', name='dofs') self.affineNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3d,Affine', input1=path_affine_rigid, input2=path_affine_scale, output='@.', autoInit='1', printLog='0') # shapefunction and mass if generatedDir is None: self.shapeFunction.addVoronoi(self.image, position='@dofs.rest_position') # mass densityImage = self.image.createTransferFunction(self.affineNode, "density", param='0 0 1 '+str(density)) affineMass = Flexible.API.AffineMass(self.affineNode) affineMass.massFromDensityImage(self.affineNode, densityImage=densityImage) self.mass = affineMass.mass else: self.shapeFunction.shapeFunction = serialization.importImageShapeFunction(self.affineNode, generatedDir+self.node.name+"_SF_indices.raw", generatedDir+self.node.name+"_SF_weights.raw", 'dofs') self.mass = serialization.importAffineMass(self.affineNode, generatedDir+self.node.name+"_affinemass.json") # hack to get the frame position self.node.init() for p in self.rigidDofs.position: p.extend([0,0,0,1]) self.frame.append(Frame.Frame(p))
def exportMappings(self, path="./generated"): if self.collision is not None: serialization.exportLinearMapping(self.collision.mapping, path+"/"+self.node.name+'_collisionmapping.json') if self.visual is not None: serialization.exportLinearMapping(self.visual.mapping, path+"/"+self.node.name+'_visualmapping.json') if self.behavior is not None: serialization.exportLinearMapping(self.behavior.mapping, path+"/"+self.node.name+'_behaviormapping.json')
def exportGaussPoints(self, path="./generated"): if self.behavior is not None: serialization.exportGaussPoints(self.behavior.sampler, path+"/"+self.node.name+'_gauss.json')
def exportShapeFunction(self, path="./generated"): serialization.exportImageShapeFunction(self.affineNode, self.shapeFunction.shapeFunction, path+"/"+self.node.name+"_SF_indices.raw", path+"/"+self.node.name+"_SF_weights.raw")
def exportRigidDofs(self, path="./generated"): serialization.exportRigidDofs( self.rigidDofs, path + "/" + self.node.name + '_dofs.json')
def exportGaussPoints(self, path="./generated"): if self.behavior is not None: serialization.exportGaussPoints( self.behavior.sampler, path + "/" + self.node.name + '_gauss.json')
def exportShapeFunction(self, path="./generated"): serialization.exportImageShapeFunction( self.affineNode, self.shapeFunction.shapeFunction, path + "/" + self.node.name + "_SF_indices.raw", path + "/" + self.node.name + "_SF_weights.raw")
def setManually(self, filepath=None, offset=[[0, 0, 0, 0, 0, 0, 1]], voxelSize=0.01, density=1000, generatedDir=None): if len(offset) == 0: Sofa.msg_error( "RigidScale.API", "ShearlessAffineBody should have at least 1 ShearLessAffine") return self.framecom = Frame.Frame() self.bodyOffset = Frame.Frame([0, 0, 0, 0, 0, 0, 1]) path_affine_rigid = '@' + Tools.node_path_rel(self.affineNode, self.rigidNode) path_affine_scale = '@' + Tools.node_path_rel(self.affineNode, self.scaleNode) if len(offset) == 1: self.frame = [Frame.Frame(offset[0])] str_position = "" for p in offset: str_position = str_position + concat(p) + " " ### scene creation # rigid dof self.rigidDofs = self.rigidNode.createObject( 'MechanicalObject', template='Rigid3' + template_suffix, name='dofs', position=str_position, rest_position=str_position) # scale dofs self.scaleDofs = self.scaleNode.createObject( 'MechanicalObject', template='Vec3' + template_suffix, name='dofs', position=concat([1, 1, 1] * len(offset))) positiveNode = self.scaleNode.createChild('positive') positiveNode.createObject('MechanicalObject', template='Vec3' + template_suffix, name='positivescaleDOFs') positiveNode.createObject('DifferenceFromTargetMapping', template='Vec3' + template_suffix + ',Vec3' + template_suffix, applyRestPosition=1, targets=concat(target_scale)) positiveNode.createObject('UniformCompliance', isCompliance=1, compliance=0) positiveNode.createObject('UnilateralConstraint') positiveNode.createObject('Stabilization', name='Stabilization') # affine dofs self.affineDofs = self.affineNode.createObject('MechanicalObject', template='Affine', name='parent', showObject=0) self.affineNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3,Affine', input1=path_affine_rigid, input2=path_affine_scale, output='@.', autoInit='1', printLog='0') if filepath: self.image = SofaImage.API.Image(self.affineNode, name="image_" + self.name, imageType="ImageUC") self.shapeFunction = Flexible.API.ShapeFunction(self.affineNode) if generatedDir is None: self.image.addMeshLoader( filepath, value=1, insideValue=1 ) # TODO support multiple meshes closingValue=1, self.image.addMeshToImage(voxelSize) self.shapeFunction.addVoronoi(self.image, position='@dofs.rest_position') # mass self.affineMassNode = self.affineNode.createChild('mass') self.affineMassNode.createObject('TransferFunction', name='density', template='ImageUC,ImageD', inputImage='@../image.image', param='0 0 1 ' + str(density)) self.affineMassNode.createObject('MechanicalObject', template='Vec3' + template_suffix) self.affineMassNode.createObject('LinearMapping', template='Affine,Vec3' + template_suffix) self.affineMassNode.createObject( 'MassFromDensity', name='MassFromDensity', template='Affine,ImageD', image='@density.outputImage', transform='@../image.transform', lumping='0') self.mass = self.affineNode.createObject( 'AffineMass', massMatrix='@mass/MassFromDensity.massMatrix') else: self.image.addContainer(filename=self.node.name + "_rasterization.raw", directory=generatedDir) self.shapeFunction.shapeFunction = serialization.importImageShapeFunction( self.affineNode, generatedDir + self.node.name + "_SF_indices.raw", generatedDir + self.node.name + "_SF_weights.raw", 'dofs') self.mass = serialization.importAffineMass( self.affineNode, generatedDir + self.node.name + "_affinemass.json") # computation of the object mass center massInfo = SofaPython.mass.RigidMassInfo() massInfo.setFromMesh(filepath, density, [1, 1, 1]) # get the object mass center self.framecom.rotation = massInfo.inertia_rotation self.framecom.translation = massInfo.com else: print "You need a mesh to create an articulated system" self.frame = [] for o in offset: self.frame.append(Frame.Frame(o))
def exportAffineMass(self, path="./generated"): serialization.exportAffineMass(self.mass, path+"/"+self.node.name+'_affinemass.json')
def exportRigidDofs(self, path="./generated"): serialization.exportRigidDofs(self.rigidDofs, path+"/"+self.node.name+'_dofs.json')
def exportAffineMass(self, path="./generated"): serialization.exportAffineMass( self.mass, path + "/" + self.node.name + '_affinemass.json')