Esempio n. 1
0
def createScene(root):

    ##### global parameters
    root.createObject(
        'VisualStyle',
        displayFlags="showBehavior showWireframe showCollisionModels")
    root.dt = 0.01
    root.gravity = [0, -10, 0]

    root.createObject('RequiredPlugin', pluginName='Compliant')
    root.createObject('CompliantAttachButtonSetting')

    ##### SOLVER
    root.createObject('CompliantImplicitSolver',
                      stabilization=0,
                      neglecting_compliance_forces_in_geometric_stiffness=0)
    root.createObject('SequentialSolver', iterations=100, precision=0)
    #root.createObject('LUResponse')
    root.createObject('LDLTResponse')

    bodies = []
    points = []

    N = 10

    for i in xrange(N):
        body = StructuralAPI.RigidBody(root, "body_" + str(i))
        body.setManually([i, 0, 0, 0, 0, 0, 1], 1, [1, 1, 1])
        body.dofs.showObject = True
        body.dofs.showObjectScale = .5
        bodies.append(body)

    bodies[0].node.createObject('FixedConstraint')
    bodies[N - 1].mass.mass = 10
    bodies[N - 1].mass.inertia = "10 10 10"

    for i in xrange(N - 1):
        p0 = bodies[i].addMappedPoint("right", [0.5, 0, 0])
        p0.dofs.showObject = True
        p0.dofs.showObjectScale = .1
        p0.dofs.drawMode = 1
        p1 = bodies[i + 1].addMappedPoint("left", [-0.5, 0, 0])
        p1.dofs.showObject = True
        p1.dofs.showObjectScale = .1
        p1.dofs.drawMode = 2
        d = p0.node.createChild("d" + str(i))
        d.createObject('MechanicalObject',
                       template='Vec3' + StructuralAPI.template_suffix,
                       name='dofs',
                       position='0 0 0')
        input = []  # @internal
        input.append('@' + Tools.node_path_rel(root, p0.node) + '/dofs')
        input.append('@' + Tools.node_path_rel(root, p1.node) + '/dofs')
        d.createObject('DifferenceMultiMapping',
                       name='mapping',
                       input=Tools.cat(input),
                       output='@dofs',
                       pairs="0 0")
        p1.node.addChild(d)
        d.createObject('UniformCompliance', name='compliance', compliance="0")
Esempio n. 2
0
        def __init__(self, rigidNode, scaleNode, name, offset, arg=-1):
            # node creation
            self.node = rigidNode.createChild(name)
            scaleNode.addChild(self.node)
            # variables
            self.frame = Frame.Frame(offset)
            path_offset_rigid = '@' + Tools.node_path_rel(self.node, rigidNode)
            path_offset_scale = '@' + Tools.node_path_rel(self.node, scaleNode)
            # scene creation
            self.dofs = self.frame.insert(self.node,
                                          template='Rigid3' + template_suffix,
                                          name='dofs')

            if arg == -1:
                self.mapping = self.node.createObject(
                    'RigidScaleToRigidMultiMapping',
                    template='Rigid3' + template_suffix + ',Vec3' +
                    template_suffix + ',Rigid3' + template_suffix,
                    input1=path_offset_rigid,
                    input2=path_offset_scale,
                    output='@.',
                    useGeometricStiffness=geometric_stiffness,
                    printLog='0')
            else:
                self.mapping = self.node.createObject(
                    'RigidScaleToRigidMultiMapping',
                    template='Rigid3' + template_suffix + ',Vec3' +
                    template_suffix + ',Rigid3' + template_suffix,
                    input1=path_offset_rigid,
                    input2=path_offset_scale,
                    output='@.',
                    index='0 ' + str(arg) + ' ' + str(arg),
                    useGeometricStiffness=geometric_stiffness,
                    printLog='0')
Esempio n. 3
0
File: API.py Progetto: Ngautier/sofa
    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))
Esempio n. 4
0
File: sml.py Progetto: Sreevis/sofa
    def __insert_registration_node(self):
        """ Insert the registration node in the graph, under dofRigid node. """
        registration_node = self.createChild(self.nodes["dofRigid"], "RegistrationNode")

        param = self.param  # shortcut

        # merge topologies
        repartition = ""
        sources_component_name = []

        collision_node = [elem.collision for elem in self.rigids.values()]

        for i, elem in enumerate(collision_node):
            sources_component_name.append(Tools.node_path_rel(registration_node, elem.node) + "/topology")

            repartition += "{0} ".format(i) * len(elem.topology.position)

        registration_node.createObject('MergeMeshes', name='source_topology', nbMeshes=len(sources_component_name),
                                       **dict({'position' + str(i + 1): '@' + item + '.position' for i, item in
                                               enumerate(sources_component_name)},
                                              **{'triangles' + str(i + 1): '@' + item + '.triangles' for i, item in
                                                 enumerate(sources_component_name)}))

        registration_node.createObject('MeshTopology', name='topo', src='@./source_topology')
        registration_node.createObject('MechanicalObject', name='DOFs')
        registration_node.createObject('Triangle')

        registration_node.createObject('RigidMapping', template='Rigid3d,Vec3d',
                                       input="@" + Tools.node_path_rel(registration_node, self.nodes["dofRigid"]),
                                       output="@DOFs", rigidIndexPerPoint=repartition)

        registration_node.createObject('NormalsFromPoints', name='NormalsFromPoints',
                                       template='Vec3d', position='@DOFs.position',
                                       triangles='@topo.triangles',
                                       invertNormals=self.param.invertSourceNormals)

        # Force Field
        target_path = Tools.node_path_rel(registration_node, self.nodes["Target"])
        registration_node.createObject('ClosestPointRegistrationForceField', name='ICP',
                                       template='Vec3d',
                                       # source
                                       sourceTriangles='@topo.triangles',
                                       sourceNormals='@NormalsFromPoints.normals',
                                       # target
                                       position='@{0}/loader.position'.format(target_path),
                                       triangles='@{0}/loader.triangles'.format(target_path),
                                       normals='@{0}/NormalsFromPoints.normals'.format(target_path),
                                       # Param
                                       cacheSize='4', blendingFactor=param.blendingFactor,
                                       stiffness=param.stiffness,
                                       damping=param.damping,
                                       outlierThreshold=param.outlierThreshold,
                                       normalThreshold=param.normalThreshold,
                                       rejectOutsideBbox=param.rejectOutsideBbox,
                                       drawColorMap='0')
Esempio n. 5
0
File: API.py Progetto: Ngautier/sofa
    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))
Esempio n. 6
0
    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))
Esempio n. 7
0
def createScene(root):
  
    ##### global parameters
    root.createObject('VisualStyle', displayFlags="showBehavior showWireframe showCollisionModels" )
    root.dt = 0.01
    root.gravity = [0, -10, 0]
    
    root.createObject('RequiredPlugin', pluginName = 'Compliant')
    root.createObject('CompliantAttachButtonSetting')
    
    ##### SOLVER
    root.createObject('CompliantImplicitSolver', stabilization=0, neglecting_compliance_forces_in_geometric_stiffness=0)
    root.createObject('SequentialSolver', iterations=100, precision=0)
    #root.createObject('LUResponse')
    root.createObject('LDLTResponse')
    
    
    bodies = []
    points = []
    
    N = 10
   
    for i in xrange(N):
        body = StructuralAPI.RigidBody( root, "body_"+str(i) )
        body.setManually( [i,0,0,0,0,0,1], 1, [1,1,1] )
        body.dofs.showObject = True
        body.dofs.showObjectScale = .5
        bodies.append( body )
        
    bodies[0].node.createObject('FixedConstraint')
    bodies[N-1].mass.mass = 10
    bodies[N-1].mass.inertia = "10 10 10"
    
        
    for i in xrange(N-1):
        p0 = bodies[i].addMappedPoint( "right", [0.5, 0, 0] )
        p0.dofs.showObject = True
        p0.dofs.showObjectScale = .1
        p0.dofs.drawMode=1
        p1 = bodies[i+1].addMappedPoint( "left", [-0.5, 0, 0] )
        p1.dofs.showObject = True
        p1.dofs.showObjectScale = .1
        p1.dofs.drawMode=2
        d = p0.node.createChild( "d"+str(i) )
        d.createObject('MechanicalObject', template = 'Vec3'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
        input = [] # @internal
        input.append( '@' + Tools.node_path_rel(root,p0.node) + '/dofs' )
        input.append( '@' + Tools.node_path_rel(root,p1.node) + '/dofs' )
        d.createObject('DifferenceMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 0" )
        p1.node.addChild( d )
        d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
Esempio n. 8
0
        def __init__(self, rigidNode, scaleNode, name, offset, arg=-1):
            # node creation
            self.node = rigidNode.createChild(name)
            scaleNode.addChild(self.node)
            # variables
            self.frame = Frame.Frame(offset)
            path_offset_rigid = '@' + Tools.node_path_rel(self.node, rigidNode)
            path_offset_scale = '@' + Tools.node_path_rel(self.node, scaleNode)
            # scene creation
            self.dofs = self.frame.insert(self.node, template='Rigid3'+template_suffix, name='dofs')

            if arg==-1:
                self.mapping = self.node.createObject('RigidScaleToRigidMultiMapping', template='Rigid3'+template_suffix+',Vec3'+template_suffix+',Rigid3'+template_suffix
                                                                                     , input1=path_offset_rigid, input2=path_offset_scale, output='@.'
                                                                                     , useGeometricStiffness=geometric_stiffness, printLog='0')
            else:
                self.mapping = self.node.createObject('RigidScaleToRigidMultiMapping', template='Rigid3'+template_suffix+',Vec3'+template_suffix+',Rigid3'+template_suffix
                                                                                     , input1=path_offset_rigid, input2=path_offset_scale, output='@.'
                                                                                     , index='0 '+ str(arg) + ' ' + str(arg), useGeometricStiffness=geometric_stiffness, printLog='0')
Esempio n. 9
0
    def setMeshLess(self, offset=[[0,0,0,0,0,0,1]], mass=1, rayleigh=0.1, 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)
        self.rigidNode.createObject('UniformMass', totalMass=mass, rayleighStiffness=rayleigh);

        # scale dofs

        self.scaleDofs = self.scaleNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='dofs', position= concat([1,1,1]*len(offset)))
        self.scaleNode.createObject('UniformMass', totalMass=mass, rayleighStiffness=rayleigh);
        #positiveNode = self.scaleNode.createChild('positive')
        #positiveNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='positivescaleDOFs')
        #target_scale = [0.5,0.5,0.5]
        #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='parent')
        self.affineNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3,Affine', input1=path_affine_rigid, input2=path_affine_scale, output='@.', autoInit='1', printLog='0')
        
        self.frame = []
        for o in offset:
            self.frame.append(Frame.Frame(o))
Esempio n. 10
0
    def setMeshLess(self, offset=[[0,0,0,0,0,0,1]], mass=1, rayleigh=0.1, 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)
        self.rigidNode.createObject('UniformMass', totalMass=mass, rayleighStiffness=rayleigh);

        # scale dofs

        self.scaleDofs = self.scaleNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='dofs', position= concat([1,1,1]*len(offset)))
        self.scaleNode.createObject('UniformMass', totalMass=mass, rayleighStiffness=rayleigh);
        #positiveNode = self.scaleNode.createChild('positive')
        #positiveNode.createObject('MechanicalObject', template='Vec3'+template_suffix, name='positivescaleDOFs')
        #target_scale = [0.5,0.5,0.5]
        #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='parent')
        self.affineNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3,Affine', input1=path_affine_rigid, input2=path_affine_scale, output='@.', autoInit='1', printLog='0')
        
        self.frame = []
        for o in offset:
            self.frame.append(Frame.Frame(o))
Esempio n. 11
0
    def __init__(self, parent, name, dofs, dim):

        self.node = parent.createChild(name)

        self.compliance = 0
        self.damping = 0
        
        input = []
        dofs_dim = 0
        
        for n in dofs:
            input.append( Tools.node_path_rel(self.node, n) )
            dofs_dim += len( n.velocity ) * len(n.velocity[0] )

        self.matrix = np.zeros( (dim, dofs_dim) )
        self.value = np.zeros( dim )


        
        self.dofs = self.node.createObject('MechanicalObject',
                                           name = 'dofs',
                                           template = 'Vec1d',
                                           position = concat( [0] * dofs_dim ) )

        template = dofs[0].template
        self.map = self.node.createObject('AffineMultiMapping',
                                          name = 'map',
                                          template = '{0}, Vec1d'.format( template ),
                                          input = concat( input ),
                                          output = '@dofs',
                                          matrix = concat( self.matrix.reshape( self.matrix.size ).tolist() ),
                                          value = concat( -self.value ) )

        self.ff = self.node.createObject('UniformCompliance',
                                         name = 'ff',
                                         template = 'Vec1d',
                                         compliance = self.compliance,
                                         damping = self.damping )
Esempio n. 12
0
    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 createGraph(self, root):
        # Variable
        self.E_t = 0
        self.E_t_dt = 0
        self.root_node = root

        # Sofa parameters
        self.root_node.createObject('BackgroundSetting', color='1 1 1')
        self.root_node.createObject(
            'VisualStyle',
            displayFlags=
            'showVisual hideWireframe showBehaviorModels hideForceFields hideInteractionForceFields'
        )
        self.root_node.createObject('StaticSolver')
        self.root_node.createObject('CGLinearSolver',
                                    iterations=500,
                                    tolerance=1E-10,
                                    threshold=1E-10)
        self.root_node.findData('gravity').value = '0 0 0'

        # Object to transfer creation
        node = self.root_node.createChild('left_humerus')
        node.createObject('MeshObjLoader',
                          name='source',
                          filename=source,
                          triangulate=1,
                          translation='0 0 0',
                          rotation='0 0 0',
                          scale3d='1 1 1')
        node.createObject('MeshToImageEngine',
                          template='ImageUC',
                          name='rasterizer',
                          src='@source',
                          insideValue='1',
                          voxelSize=voxel_size,
                          padSize=2,
                          rotateImage='false')
        node.createObject('ImageContainer',
                          template='ImageUC',
                          name='image',
                          src='@rasterizer',
                          drawBB='false')
        node.createObject('ImageSampler',
                          template='ImageUC',
                          name='sampler',
                          src='@image',
                          method=1,
                          param='1 0',
                          clearData=0)
        node.createObject('MeshTopology',
                          name='frame_topo',
                          position='@sampler.position')

        #================================ Target model ===================================
        targetNode = node.createChild('target')
        targetNode.createObject('MeshObjLoader',
                                name='target',
                                filename=target,
                                triangulate=1,
                                translation='0 0 0',
                                rotation='0 0 0',
                                scale3d='3 3 3',
                                showObject=0)
        targetNode.createObject('MechanicalObject',
                                template='Vec3d',
                                name='DOFs',
                                src='@target',
                                showObject=0)
        targetNode.createObject('FixedConstraint', fixAll='1')
        targetVisuNode = targetNode.createChild('visu')
        targetVisuNode.createObject('OglModel',
                                    template='ExtVec3f',
                                    name='visual',
                                    src='@../target',
                                    color='0.5 0.5 0.5 0.75')

        #================================ Rigid frame ====================================
        rigidNode = node.createChild('rigid')
        rigidNode.createObject('MechanicalObject',
                               template='Rigid3d',
                               name='DOFs',
                               src='@../frame_topo',
                               showObject=0,
                               showObjectScale='0.1')

        #=================================== Scale =======================================
        scaleNode = node.createChild('scale')
        scaleNode.createObject('MechanicalObject',
                               template='Vec3d',
                               name='DOFs',
                               position='1 1 1',
                               showObject=0,
                               showObjectScale='0.1')

        #============================= Registration model ================================
        objMainNode = rigidNode.createChild('main')
        scaleNode.addChild(objMainNode)

        # scene creation
        loader = objMainNode.createObject('MeshObjLoader',
                                          name='source',
                                          filename=source,
                                          triangulate=1,
                                          translation='0 0 0',
                                          rotation='0 0 0',
                                          scale3d='1 1 1')
        objMainNode.createObject('MeshToImageEngine',
                                 template='ImageUC',
                                 name='rasterizer',
                                 src='@source',
                                 value=1,
                                 insideValue=1,
                                 voxelSize=voxel_size,
                                 padSize=0,
                                 rotateImage='false')
        objMainNode.createObject('ImageContainer',
                                 template='ImageUC',
                                 name='image',
                                 src='@rasterizer',
                                 drawBB='false')
        objMainNode.createObject('MechanicalObject',
                                 template='Affine',
                                 name='parent',
                                 src='@../../frame_topo',
                                 showObject=1,
                                 showObjectScale='0.1')
        objMainNode.createObject('RigidScaleToAffineMultiMapping',
                                 template='Rigid,Vec3d,Affine',
                                 input1='@../../rigid/DOFs',
                                 input2='@../../scale/DOFs',
                                 output='@.',
                                 index='0 0 0',
                                 printLog='0')
        objMainNode.createObject('VoronoiShapeFunction',
                                 template='ShapeFunctiond,ImageUC',
                                 name='SF',
                                 position='@parent.rest_position',
                                 image='@image.image',
                                 transform='@image.transform',
                                 nbRef=4,
                                 clearData=1,
                                 bias=0)
        # Contact
        objContactNode = objMainNode.createChild('registration')
        objContactNode.createObject('MeshTopology',
                                    name='topo',
                                    src='@../source')
        objContactNode.createObject('MechanicalObject', name='DOFs')
        objContactNode.createObject('UniformMass', totalMass=1)
        objContactNode.createObject('TriangleModel')
        objContactNode.createObject('LinearMapping', template='Affine,Vec3d')
        # Visual model
        objVisuNode = objContactNode.createChild('visual')
        objVisuNode.createObject('OglModel',
                                 template='ExtVec3f',
                                 name='visual',
                                 src='@../topo',
                                 color='1 0.2 0.2 0.9')
        objVisuNode.createObject('IdentityMapping', template='Vec3d,ExtVec3f')
        # Registration
        objRegistrationNode = objMainNode.createChild('force')
        objRegistrationNode.createObject('MechanicalObject',
                                         template='Vec3d',
                                         name='DOFs',
                                         src='@../source')
        objRegistrationNode.createObject('LinearMapping',
                                         template='Affine,Vec3d')
        # registration force field
        springs = ""
        for i in range(len(loader.position)):
            springs += str(i) + ' ' + str(i) + ' '
        distanceNode = objRegistrationNode.createChild(
            'registration_constraint')
        targetNode.addChild(distanceNode)
        distanceNode.createObject('MechanicalObject',
                                  template='Vec3d',
                                  name='distanceDOFs')
        distanceNode.createObject(
            'DifferenceMultiMapping',
            template='Vec3d,Vec3d',
            input='@' + Tools.node_path_rel(distanceNode, targetNode) + ' @' +
            Tools.node_path_rel(distanceNode, objRegistrationNode),
            output='@.',
            pairs=springs,
            showObjectScale="0.005")
        distanceNode.createObject('UniformCompliance',
                                  name='constraint',
                                  isCompliance=0,
                                  compliance=1E-6,
                                  damping=0.1)
    def createGraph(self, root):

        # Variable
        self.E_t = 0
        self.E_t_dt = 0
        self.root_node = root
        self.root_node.findData('gravity').value = '0 0 0'

        # Sofa parameters
        self.root_node.createObject('BackgroundSetting', color='1 1 1')
        self.root_node.createObject(
            'VisualStyle',
            displayFlags=
            'showVisual hideWireframe showBehaviorModels hideForceFields showInteractionForceFields'
        )

        self.root_node.createObject('EulerImplicit',
                                    rayleighStiffness='0.01',
                                    rayleighMass='0.01')
        self.root_node.createObject('CGLinearSolver',
                                    iterations=200,
                                    tolerance=1E-6,
                                    threshold=1E-6)

        # Object to transfer creation
        node = self.root_node.createChild('main')
        node.createObject('MeshObjLoader',
                          name='source',
                          filename='./mesh/source.obj',
                          triangulate=1,
                          translation='0 0 0',
                          rotation='0 0 0',
                          scale3d='1 1 1')
        node.createObject('MeshToImageEngine',
                          template='ImageUC',
                          name='rasterizer',
                          src='@source',
                          insideValue='1',
                          voxelSize=0.01,
                          padSize=2,
                          rotateImage='false')
        node.createObject('ImageContainer',
                          template='ImageUC',
                          name='image',
                          src='@rasterizer',
                          drawBB='false')
        node.createObject('MeshTopology',
                          name='frame_topo',
                          position=listListToStr(dofs_position))

        #================================ Target model ===================================
        targetNode = node.createChild('target')
        targetNode.createObject('MeshObjLoader',
                                name='loader',
                                filename='./mesh/target.obj',
                                rotation='0 0 0',
                                translation='0 0 0',
                                scale3d='1 1 1',
                                showObject=0)
        targetNode.createObject('MechanicalObject',
                                template='Vec3d',
                                name='DOFs',
                                src='@loader',
                                showObject=0)
        targetNode.createObject('NormalsFromPoints',
                                name='normalsFromPoints',
                                template='Vec3d',
                                position='@DOFs.position',
                                triangles='@loader.triangles',
                                invertNormals=0)
        targetNode.createObject('FixedConstraint', fixAll='1', drawSize=0.001)
        targetVisuNode = targetNode.createChild('visu')
        targetVisuNode.createObject('OglModel',
                                    template='ExtVec3f',
                                    name='visual',
                                    src='@../loader',
                                    color='0.5 0.5 0.5 0.85')

        #================================ Rigid frame ====================================
        rigidNode = node.createChild('rigid')
        rigidNode.createObject('MechanicalObject',
                               template='Rigid',
                               name='DOFs',
                               src='@../frame_topo',
                               showObject=1,
                               showObjectScale='0.1')
        rigidNode.createObject('RigidMass',
                               mass="1 1 ",
                               inertia="1 1 1  1 1 1")
        #rigidNode.createObject('FixedConstraint', name='fixed', indices='0')

        #=================================== Scale =======================================
        scaleNode = node.createChild('scale')
        scaleNode.createObject('MechanicalObject',
                               template='Vec3d',
                               name='DOFs',
                               position='1 1 1 1 1 1',
                               showObject=0,
                               showObjectScale='0.1')

        #=========================== Alignement constraint ===============================
        offsetNode = rigidNode.createChild('offset')
        offsetNode.createObject('MechanicalObject',
                                template='Rigid',
                                name='DOFs',
                                position='0 0 0 0 0 0 1  0 0 0 0 0 0 1',
                                showObject=1,
                                showObjectScale='0.1')
        offsetNode.createObject('AssembledRigidRigidMapping',
                                template='Rigid,Rigid',
                                source='0 ' + listToStr(offset_position[0]) +
                                listToStr(joint_orientation) + ' 1 ' +
                                listToStr(offset_position[1]) +
                                listToStr(joint_orientation))
        # --- old things even if they don't work well are often more stable, just for creating the prototype ...
        offsetNode.createObject(
            'JointSpringForceField',
            template='Rigid',
            name='joint',
            object1='@.',
            object2='@.',
            spring=
            ' BEGIN_SPRING 0 1 FREE_AXIS 0 1 0 0 0 0 KS_T 0 1E10 KS_R 0 1e10 KS_B 3E3 KD 0.1 R_LIM_X 0 0 R_LIM_Y 0 0 R_LIM_Z 0 0 REST_T 0 1 0 END_SPRING'
        )

        #============================= Registration model ================================
        objMainNode = rigidNode.createChild('deformable')
        scaleNode.addChild(objMainNode)
        # scene creation
        loader = objMainNode.createObject('MeshObjLoader',
                                          name='source',
                                          filename='./mesh/source.obj',
                                          triangulate=1,
                                          translation='0 0 0',
                                          rotation='0 0 0',
                                          scale3d='1 1 1')
        objMainNode.createObject('MeshToImageEngine',
                                 template='ImageUC',
                                 name='rasterizer',
                                 src='@source',
                                 value=1,
                                 insideValue=1,
                                 voxelSize=0.01,
                                 padSize=0,
                                 rotateImage='false')
        objMainNode.createObject('ImageContainer',
                                 template='ImageUC',
                                 name='image',
                                 src='@rasterizer',
                                 drawBB='false')
        objMainNode.createObject('MechanicalObject',
                                 template='Affine',
                                 name='parent',
                                 showObject=1,
                                 src='@../../frame_topo',
                                 showObjectScale='0.1')
        objMainNode.createObject('RigidScaleToAffineMultiMapping',
                                 template='Rigid,Vec3d,Affine',
                                 input1='@../../rigid/DOFs',
                                 input2='@../../scale/DOFs',
                                 output='@.',
                                 autoInit='1',
                                 printLog='0')
        objMainNode.createObject('VoronoiShapeFunction',
                                 template='ShapeFunctiond,ImageUC',
                                 name='SF',
                                 position='@parent.rest_position',
                                 image='@image.image',
                                 transform='@image.transform',
                                 nbRef=4,
                                 clearData=1,
                                 bias=0)
        # Behavior Node
        objBehaviorNode = objMainNode.createChild('behavior')
        objBehaviorNode.createObject('ImageGaussPointSampler',
                                     name='sampler',
                                     indices='@../SF.indices',
                                     weights='@../SF.weights',
                                     transform='@../SF.transform',
                                     method=2,
                                     order=1,
                                     targetNumber=40)
        objBehaviorNode.createObject('MechanicalObject', template='F331')
        objBehaviorNode.createObject('LinearMapping', template='Affine,F331')
        objBehaviorNode.createObject('ProjectiveForceField',
                                     template='F331',
                                     youngModulus=1E6,
                                     poissonRatio=0,
                                     viscosity=0,
                                     isCompliance=0)
        # Contact
        objContactNode = objMainNode.createChild('registration')
        objContactNode.createObject('MeshTopology',
                                    name='topo',
                                    src='@../source')
        objContactNode.createObject('MechanicalObject', name='DOFs')
        objContactNode.createObject('UniformMass', totalMass=1)
        objContactNode.createObject('TriangleModel')
        objContactNode.createObject('LinearMapping', template='Affine,Vec3d')
        # Visual model
        objVisuNode = objContactNode.createChild('visual')
        objVisuNode.createObject('OglModel',
                                 template='ExtVec3f',
                                 name='visual',
                                 src='@../topo',
                                 color='1 0.2 0.2 0.8')
        objVisuNode.createObject('IdentityMapping', template='Vec3d,ExtVec3f')
        # Registration : first attraction field to well positioning the source on the target
        objRegistrationNode = objMainNode.createChild('force')
        objRegistrationNode.createObject('MeshTopology',
                                         name='topo',
                                         src='@../source')
        objRegistrationNode.createObject('MechanicalObject',
                                         template='Vec3d',
                                         name='DOFs')
        objRegistrationNode.createObject('LinearMapping',
                                         template='Affine,Vec3d')
        # registration force field
        springs = ""
        for i in range(len(loader.position)):
            springs += str(i) + ' ' + str(i) + ' '

        distanceNode = objRegistrationNode.createChild(
            'registration_constraint')
        targetNode.addChild(distanceNode)
        distanceNode.createObject('MechanicalObject',
                                  template='Vec3d',
                                  name='distanceDOFs')
        distanceNode.createObject(
            'DifferenceMultiMapping',
            template='Vec3d,Vec3d',
            input='@' + Tools.node_path_rel(distanceNode, targetNode) + ' @' +
            Tools.node_path_rel(distanceNode, objRegistrationNode),
            output='@.',
            pairs=springs,
            showObjectScale="0.005")
        distanceNode.createObject('UniformCompliance',
                                  name='constraint',
                                  isCompliance=0,
                                  compliance=1E-6,
                                  damping=0.1)
        # Registration : surface to surface registration forces
        surfaceRegistrationNode = objMainNode.createChild('reg_force')
        surfaceRegistrationNode.createObject('MeshTopology',
                                             name='topo',
                                             src='@../source')
        surfaceRegistrationNode.createObject('MechanicalObject', name='DOFs')
        surfaceRegistrationNode.createObject('Triangle')
        surfaceRegistrationNode.createObject('LinearMapping',
                                             template='Affine,Vec3d',
                                             assemble=0)
        surfaceRegistrationNode.createObject('NormalsFromPoints',
                                             name='normalsFromPoints',
                                             template='Vec3d',
                                             position='@DOFs.position',
                                             triangles='@topo.triangles',
                                             invertNormals=0)
        surfaceRegistrationNode.createObject(
            'ClosestPointRegistrationForceField',
            name='ICP',
            template='Vec3d',
            sourceTriangles='@topo.triangles',
            sourceNormals='@normalsFromPoints.normals',
            position='@../../../target/loader.position',
            triangles='@../../../target/loader.triangles',
            normals='@../../../target/normalsFromPoints.normals',
            cacheSize=4,
            blendingFactor=1,
            stiffness=1E3,
            damping=1E-3,
            outlierThreshold=0,
            normalThreshold=0,
            rejectOutsideBbox=0,
            drawColorMap='0')
Esempio n. 15
0
def createScene(root):
  
    ##### global parameters
    root.createObject('VisualStyle', displayFlags="showBehavior showCollisionModels" )
    root.dt = 0.01
    root.gravity = [0, -10, 0]
    
    root.createObject('RequiredPlugin', pluginName = 'Compliant')
    root.createObject('CompliantAttachButton')
    
    
    root.createObject('DefaultPipeline', name='DefaultCollisionPipeline', depth="6")
    root.createObject('BruteForceDetection')
    root.createObject('DiscreteIntersection')
    root.createObject('DefaultContactManager', name="Response", response="CompliantContact", responseParams="compliance=0&restitution=0" )
    
    
    
    
    
    ##### SOLVER
    root.createObject('CompliantImplicitSolver', stabilization=1, neglecting_compliance_forces_in_geometric_stiffness=1)
    root.createObject('SequentialSolver', iterations=100, precision=0)
    #root.createObject('LUResponse')
    root.createObject('LDLTResponse')
    
    
    
    
    
    ##### GEAR
    
    gearNode = root.createChild( "GEAR" )
    
    r0 = 0.33
    r1 = 0.66
    
    body0 = StructuralAPI.RigidBody( gearNode, "body_0" )
    body0.setManually( [0,0,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = r0*1.1
    body0.dofs.velocity="0 0 0 0 1 0"
    body0.node.createObject('Sphere', radius=r0)
        
    body1 = StructuralAPI.RigidBody( gearNode, "body_1" )
    body1.setManually( [1,0,0,0,0,0,1], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = r1*1.1
    body1.node.createObject('Sphere', radius=r1)
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 4 0 4", ratio = -r0/r1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
    
    
    #####  driving belt / chain
    
    
    beltNode = root.createChild( "BELT" )
    
    r0 = 0.7
    r1 = 0.3
    
    body0 = StructuralAPI.RigidBody( beltNode, "body_0" )
    body0.setManually( [0,-2,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = r0*1.1
    body0.dofs.velocity="0 0 0 0 1 0"
    body0.node.createObject('Sphere', radius=r0)
        
    body1 = StructuralAPI.RigidBody( beltNode, "body_1" )
    body1.setManually( [1.5,-2,0,0,0,0,1], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = r1*1.1
    body1.node.createObject('Sphere', radius=r1)
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 4 0 4", ratio = r0/r1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
    
    
    
    
    #####  angle transmission
    
    
    angleNode = root.createChild( "ANGLE" )
    
    r0 = 0.49
    r1 = 0.49
    
    body0 = StructuralAPI.RigidBody( angleNode, "body_0" )
    body0.setManually( [0,-4,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = r0*1.1
    body0.dofs.velocity="0 0 0 1 0 0"
    body0.node.createObject('Sphere', radius=r0)
        
    body1 = StructuralAPI.RigidBody( angleNode, "body_1" )
    body1.setManually( [1,-4,0,0,0,0,1], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = r1*1.1
    body1.node.createObject('Sphere', radius=r1)
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 0 1 1")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 3 0 4", ratio = r0/r1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
    
    
    #####  rack
    
    
    rackNode = root.createChild( "RACK" )
    
    
    body0 = StructuralAPI.RigidBody( rackNode, "body_0" )
    body0.setManually( [0,-6,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = 0.55
    body0.dofs.velocity="0 0 0 0 0 1"
    body0.node.createObject('Sphere', radius=0.5)
        
    body1 = StructuralAPI.RigidBody( rackNode, "body_1" )
    body1.setManually( [-2,-6.71,0, 0,0,0.7071067811865476,0.7071067811865476], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = 0.3
    body1.node.createObject('Capsule', radii="0.2", heights="5")
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 1 0")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="0 1 1 1 1 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 5 0 0", ratio = 1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
Esempio n. 16
0
File: gear.py Progetto: bcarrez/sofa
def createScene(root):
  
    ##### global parameters
    root.createObject('VisualStyle', displayFlags="showBehavior showCollisionModels" )
    root.dt = 0.01
    root.gravity = [0, -10, 0]
    
    root.createObject('RequiredPlugin', pluginName = 'Compliant')
    root.createObject('CompliantAttachButton')
    
    
    root.createObject('DefaultPipeline', name='DefaultCollisionPipeline', depth="6")
    root.createObject('BruteForceBroadPhase', name='N2')
    root.createObject('BVHNarrowPhase')
    root.createObject('DiscreteIntersection')
    root.createObject('DefaultContactManager', name="Response", response="CompliantContact", responseParams="compliance=0&restitution=0" )
    
    
    
    
    
    ##### SOLVER
    root.createObject('CompliantImplicitSolver', stabilization=1, neglecting_compliance_forces_in_geometric_stiffness=1)
    root.createObject('SequentialSolver', iterations=100, precision=0)
    #root.createObject('LUResponse')
    root.createObject('LDLTResponse')
    
    
    
    
    
    ##### GEAR
    
    gearNode = root.createChild( "GEAR" )
    
    r0 = 0.33
    r1 = 0.66
    
    body0 = StructuralAPI.RigidBody( gearNode, "body_0" )
    body0.setManually( [0,0,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = r0*1.1
    body0.dofs.velocity="0 0 0 0 1 0"
    body0.node.createObject('Sphere', radius=r0)
        
    body1 = StructuralAPI.RigidBody( gearNode, "body_1" )
    body1.setManually( [1,0,0,0,0,0,1], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = r1*1.1
    body1.node.createObject('Sphere', radius=r1)
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 4 0 4", ratio = -r0/r1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
    
    
    #####  driving belt / chain
    
    
    beltNode = root.createChild( "BELT" )
    
    r0 = 0.7
    r1 = 0.3
    
    body0 = StructuralAPI.RigidBody( beltNode, "body_0" )
    body0.setManually( [0,-2,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = r0*1.1
    body0.dofs.velocity="0 0 0 0 1 0"
    body0.node.createObject('Sphere', radius=r0)
        
    body1 = StructuralAPI.RigidBody( beltNode, "body_1" )
    body1.setManually( [1.5,-2,0,0,0,0,1], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = r1*1.1
    body1.node.createObject('Sphere', radius=r1)
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 4 0 4", ratio = r0/r1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
    
    
    
    
    #####  angle transmission
    
    
    angleNode = root.createChild( "ANGLE" )
    
    r0 = 0.49
    r1 = 0.49
    
    body0 = StructuralAPI.RigidBody( angleNode, "body_0" )
    body0.setManually( [0,-4,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = r0*1.1
    body0.dofs.velocity="0 0 0 1 0 0"
    body0.node.createObject('Sphere', radius=r0)
        
    body1 = StructuralAPI.RigidBody( angleNode, "body_1" )
    body1.setManually( [1,-4,0,0,0,0,1], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = r1*1.1
    body1.node.createObject('Sphere', radius=r1)
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 0 1 1")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 0 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 3 0 4", ratio = r0/r1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
    
    
    #####  rack
    
    
    rackNode = root.createChild( "RACK" )
    
    
    body0 = StructuralAPI.RigidBody( rackNode, "body_0" )
    body0.setManually( [0,-6,0,0,0,0,1], 1, [1,1,1] )
    body0.dofs.showObject = True
    body0.dofs.showObjectScale = 0.55
    body0.dofs.velocity="0 0 0 0 0 1"
    body0.node.createObject('Sphere', radius=0.5)
        
    body1 = StructuralAPI.RigidBody( rackNode, "body_1" )
    body1.setManually( [-2,-6.71,0, 0,0,0.7071067811865476,0.7071067811865476], 1, [1,1,1] )
    body1.dofs.showObject = True
    body1.dofs.showObjectScale = 0.3
    body1.node.createObject('Capsule', radii="0.2", heights="5")
    
   
    body0.node.createObject('PartialFixedConstraint', fixedDirections="1 1 1 1 1 0")
    body1.node.createObject('PartialFixedConstraint', fixedDirections="0 1 1 1 1 1")
    
    
    d = body0.node.createChild( "d" )
    d.createObject('MechanicalObject', template = 'Vec1'+StructuralAPI.template_suffix, name = 'dofs', position = '0 0 0' )
    input = [] # @internal
    input.append( '@' + Tools.node_path_rel(root,body0.node) + '/dofs' )
    input.append( '@' + Tools.node_path_rel(root,body1.node) + '/dofs' )
    d.createObject('GearMultiMapping', name = 'mapping', input = Tools.cat(input), output = '@dofs', pairs = "0 5 0 0", ratio = 1 )
    body1.node.addChild( d )
    d.createObject('UniformCompliance', name = 'compliance', compliance="0" )
    
    
    def createGraph(self, root):

        # Variable
        self.E_t = 0
        self.E_t_dt = 0
        self.root_node = root
        self.root_node.findData('gravity').value = '0 0 0'
        
        # Sofa parameters
        self.root_node.createObject('BackgroundSetting',color='1 1 1')
        self.root_node.createObject('VisualStyle', displayFlags='showVisual hideWireframe showBehaviorModels hideForceFields showInteractionForceFields')

        self.root_node.createObject('EulerImplicit',rayleighStiffness='0.01',rayleighMass='0.01')
        self.root_node.createObject('CGLinearSolver', iterations=200, tolerance=1E-6, threshold=1E-6)
        
        # Object to transfer creation
        node = self.root_node.createChild('main')
        node.createObject('MeshObjLoader',name='source', filename='./mesh/source.obj', triangulate=1, translation='0 0 0', rotation='0 0 0', scale3d='1 1 1')
        node.createObject('MeshToImageEngine', template='ImageUC', name='rasterizer', src='@source', insideValue='1', voxelSize=0.01, padSize=2, rotateImage='false')
        node.createObject('ImageContainer', template='ImageUC', name='image', src='@rasterizer', drawBB='false')
        node.createObject('MeshTopology', name='frame_topo', position=listListToStr(dofs_position))

        #================================ Target model ===================================
        targetNode = node.createChild('target')
        targetNode.createObject('MeshObjLoader', name='loader', filename='./mesh/target.obj', rotation='0 0 0', translation='0 0 0', scale3d='1 1 1', showObject=0)
        targetNode.createObject('MechanicalObject', template='Vec3d', name='DOFs', src='@loader', showObject=0)
        targetNode.createObject('NormalsFromPoints', name='normalsFromPoints', template='Vec3d', position='@DOFs.position', triangles='@loader.triangles', invertNormals=0)
        targetNode.createObject('FixedConstraint', fixAll='1', drawSize=0.001)
        targetVisuNode = targetNode.createChild('visu')
        targetVisuNode.createObject('OglModel', template='ExtVec3f', name='visual', src='@../loader', color='0.5 0.5 0.5 0.85')

        #================================ Rigid frame ====================================
        rigidNode = node.createChild('rigid')           
        rigidNode.createObject('MechanicalObject', template='Rigid', name='DOFs', src='@../frame_topo', showObject=1, showObjectScale='0.1')
        rigidNode.createObject('RigidMass', mass="1 1 ", inertia="1 1 1  1 1 1")
        #rigidNode.createObject('FixedConstraint', name='fixed', indices='0')

        #=================================== Scale =======================================
        scaleNode = node.createChild('scale')
        scaleNode.createObject('MechanicalObject', template='Vec3d', name='DOFs', position='1 1 1 1 1 1', showObject=0, showObjectScale='0.1')

        #=========================== Alignement constraint ===============================
        offsetNode = rigidNode.createChild('offset')
        offsetNode.createObject('MechanicalObject', template='Rigid', name='DOFs', position='0 0 0 0 0 0 1  0 0 0 0 0 0 1', showObject=1, showObjectScale='0.1')
        offsetNode.createObject('AssembledRigidRigidMapping', template='Rigid,Rigid', source='0 '+listToStr(offset_position[0]) + listToStr(joint_orientation) + ' 1 '+listToStr(offset_position[1]) + listToStr(joint_orientation))
        # --- old things even if they don't work well are often more stable, just for creating the prototype ...
        offsetNode.createObject('JointSpringForceField', template='Rigid', name='joint', object1='@.', object2='@.', spring=' BEGIN_SPRING 0 1 FREE_AXIS 0 1 0 0 0 0 KS_T 0 1E10 KS_R 0 1e10 KS_B 3E3 KD 0.1 R_LIM_X 0 0 R_LIM_Y 0 0 R_LIM_Z 0 0 REST_T 0 1 0 END_SPRING')

        #============================= Registration model ================================
        objMainNode = rigidNode.createChild('deformable')
        scaleNode.addChild(objMainNode)
        # scene creation
        loader = objMainNode.createObject('MeshObjLoader',name='source', filename='./mesh/source.obj', triangulate=1, translation='0 0 0', rotation='0 0 0', scale3d='1 1 1')
        objMainNode.createObject('MeshToImageEngine', template='ImageUC', name='rasterizer', src='@source', value=1, insideValue=1, voxelSize=0.01, padSize=0, rotateImage='false')
        objMainNode.createObject('ImageContainer', template='ImageUC', name='image', src='@rasterizer', drawBB='false') 
        objMainNode.createObject('MechanicalObject', template='Affine', name='parent', showObject=1, src='@../../frame_topo', showObjectScale='0.1')
        objMainNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3d,Affine', input1='@../../rigid/DOFs', input2='@../../scale/DOFs', output='@.', autoInit='1', printLog='0')
        objMainNode.createObject('VoronoiShapeFunction', template='ShapeFunctiond,ImageUC', name='SF', position='@parent.rest_position', image='@image.image', transform='@image.transform', nbRef=4, clearData=1, bias=0)
        # Behavior Node
        objBehaviorNode = objMainNode.createChild('behavior')
        objBehaviorNode.createObject('ImageGaussPointSampler', name='sampler', indices='@../SF.indices', weights='@../SF.weights', transform='@../SF.transform', method=2, order=1, targetNumber=40)
        objBehaviorNode.createObject('MechanicalObject', template='F331')  
        objBehaviorNode.createObject('LinearMapping', template='Affine,F331')    
        objBehaviorNode.createObject('ProjectiveForceField', template='F331', youngModulus=1E6, poissonRatio=0, viscosity=0, isCompliance=0)
        # Contact
        objContactNode = objMainNode.createChild('registration')     
        objContactNode.createObject('MeshTopology', name='topo', src='@../source')
        objContactNode.createObject('MechanicalObject', name='DOFs')
        objContactNode.createObject('UniformMass', totalMass=1)
        objContactNode.createObject('TriangleModel')
        objContactNode.createObject('LinearMapping', template='Affine,Vec3d')
        # Visual model
        objVisuNode = objContactNode.createChild('visual')
        objVisuNode.createObject('OglModel', template='ExtVec3f', name='visual', src='@../topo', color='1 0.2 0.2 0.8')
        objVisuNode.createObject('IdentityMapping', template='Vec3d,ExtVec3f')
        # Registration : first attraction field to well positioning the source on the target
        objRegistrationNode = objMainNode.createChild('force')
        objRegistrationNode.createObject('MeshTopology', name='topo', src='@../source')
        objRegistrationNode.createObject('MechanicalObject', template='Vec3d', name='DOFs')
        objRegistrationNode.createObject('LinearMapping', template='Affine,Vec3d')
        # registration force field
        springs = ""
        for i in range(len(loader.position)):
            springs += str(i)+' '+str(i)+' '

        distanceNode = objRegistrationNode.createChild('registration_constraint')
        targetNode.addChild(distanceNode)
        distanceNode.createObject('MechanicalObject', template='Vec3d', name='distanceDOFs')
        distanceNode.createObject('DifferenceMultiMapping', template='Vec3d,Vec3d', input='@'+Tools.node_path_rel(distanceNode, targetNode)+' @'+Tools.node_path_rel(distanceNode, objRegistrationNode), output='@.', pairs=springs, showObjectScale="0.005")
        distanceNode.createObject('UniformCompliance', name='constraint', isCompliance=0, compliance=1E-6, damping=0.1)
        # Registration : surface to surface registration forces
        surfaceRegistrationNode = objMainNode.createChild('reg_force')
        surfaceRegistrationNode.createObject('MeshTopology', name='topo', src='@../source')
        surfaceRegistrationNode.createObject('MechanicalObject', name='DOFs')
        surfaceRegistrationNode.createObject('Triangle')
        surfaceRegistrationNode.createObject('LinearMapping', template='Affine,Vec3d', assemble=0)
        surfaceRegistrationNode.createObject('NormalsFromPoints', name='normalsFromPoints', template='Vec3d', position='@DOFs.position', triangles='@topo.triangles', invertNormals=0)
        surfaceRegistrationNode.createObject('ClosestPointRegistrationForceField', name='ICP', template='Vec3d'
                                                                                             , sourceTriangles='@topo.triangles', sourceNormals='@normalsFromPoints.normals'
                                                                                             , position='@../../../target/loader.position' , triangles='@../../../target/loader.triangles', normals='@../../../target/normalsFromPoints.normals'
                                                                                             , cacheSize=4, blendingFactor=1, stiffness=1E3, damping=1E-3
                                                                                             , outlierThreshold=0, normalThreshold=0, rejectOutsideBbox=0, drawColorMap='0')
Esempio n. 18
0
    def insert(self, parent, **kwargs):
        
        node = parent.createChild(self.name)

        self.offset_node = []

        geometric = kwargs.get('geometric', geometric_stiffness)
        
        # build input data for multimapping
        input = []
        for b, o in zip(self.body, self.offset):
            if o is None:
                input.append( '@' + Tools.node_path_rel(node, b) + '/dofs' )
            else:
                joint = b.createChild( self.name + '-offset' )
                self.offset_node.append( joint )
                
                joint.createObject('MechanicalObject', 
                                   template = 'Rigid', 
                                   name = 'dofs' )
                
                joint.createObject('AssembledRigidRigidMapping', 
                                   template = "Rigid,Rigid",
                                   source = '0 ' + concat( o ),
                                   geometricStiffness = geometric)
                
                input.append( '@' + Tools.node_path_rel(node, b) + '/' + joint.name + '/dofs' )
                             
        assert len(input) > 0
   
        dofs = node.createObject('MechanicalObject', 
                                 template = 'Vec6d', 
                                 name = 'dofs', 
                                 position = '0 0 0 0 0 0',
                                 velocity = '0 0 0 0 0 0')

        mapping = node.createObject('RigidJointMultiMapping',
                                name = 'mapping', 
                                template = 'Rigid,Vec6d', 
                                input = concat(input),
                                output = '@dofs',
                                pairs = "0 0",
                                geometricStiffness = geometric
        )
                
        sub = node.createChild("constraints")

        dim = sum(np.array(self.dofs) != 0)
        
        sub.createObject('MechanicalObject', 
                         template = 'Vec1d', 
                         name = 'dofs',
                         position = concat( np.zeros(dim) ),
                         velocity = concat( np.zeros(dim) ),
        )
	
        mask = [ (1 - d) for d in self.dofs ]
        mapping = sub.createObject('MaskMapping', 
                               name = 'mapping',
                               template = 'Vec6d,Vec1d',
                               input = '@../',
                               output = '@dofs',
                               dofs = concat(mask) )
		
        ff = sub.createObject('UniformCompliance',
                              name = 'compliance',
                              template = 'Vec1d',
                              compliance = self.compliance)

        if self.stabilize:
            stab = sub.createObject('Stabilization')
        
        self.ff = ff
        self.node = node
        
        return node
    def createGraph(self, root):
        # Variable
        self.E_t = 0
        self.E_t_dt = 0
        self.root_node = root
        
        # Sofa parameters
        self.root_node.createObject('BackgroundSetting',color='1 1 1')
        self.root_node.createObject('VisualStyle', displayFlags='showVisual hideWireframe showBehaviorModels hideForceFields hideInteractionForceFields')
        self.root_node.createObject('StaticSolver')
        self.root_node.createObject('CGLinearSolver', iterations=500, tolerance=1E-10, threshold=1E-10)
        self.root_node.findData('gravity').value = '0 0 0'

        # Object to transfer creation
        node = self.root_node.createChild('left_humerus')
        node.createObject('MeshObjLoader',name='source', filename=source, triangulate=1, translation='0 0 0', rotation='0 0 0', scale3d='1 1 1')
        node.createObject('MeshToImageEngine', template='ImageUC', name='rasterizer', src='@source', insideValue='1', voxelSize=voxel_size, padSize=2, rotateImage='false')
        node.createObject('ImageContainer', template='ImageUC', name='image', src='@rasterizer', drawBB='false')
        node.createObject('ImageSampler', template='ImageUC', name='sampler', src='@image', method=1, param='1 0', clearData=0)
        node.createObject('MeshTopology', name='frame_topo', position='@sampler.position') 
                
        #================================ Target model ===================================
        targetNode = node.createChild('target') 
        targetNode.createObject('MeshObjLoader', name='target', filename=target, triangulate=1, translation='0 0 0', rotation='0 0 0', scale3d='3 3 3', showObject=0)        
        targetNode.createObject('MechanicalObject', template='Vec3d', name='DOFs', src='@target', showObject=0)
        targetNode.createObject('FixedConstraint', fixAll='1' )          
        targetVisuNode = targetNode.createChild('visu')
        targetVisuNode.createObject('OglModel', template='ExtVec3f', name='visual', src='@../target', color='0.5 0.5 0.5 0.75') 
        
        #================================ Rigid frame ====================================
        rigidNode = node.createChild('rigid')
        rigidNode.createObject('MechanicalObject', template='Rigid3d', name='DOFs', src='@../frame_topo', showObject=0, showObjectScale='0.1')
                
        #=================================== Scale =======================================
        scaleNode = node.createChild('scale')
        scaleNode.createObject('MechanicalObject', template='Vec3d', name='DOFs', position='1 1 1', showObject=0, showObjectScale='0.1')

        #============================= Registration model ================================
        objMainNode = rigidNode.createChild('main')
        scaleNode.addChild(objMainNode)

        # scene creation
        loader = objMainNode.createObject('MeshObjLoader',name='source', filename=source, triangulate=1, translation='0 0 0', rotation='0 0 0', scale3d='1 1 1')
        objMainNode.createObject('MeshToImageEngine', template='ImageUC', name='rasterizer', src='@source', value=1, insideValue=1, voxelSize=voxel_size, padSize=0, rotateImage='false')
        objMainNode.createObject('ImageContainer', template='ImageUC', name='image', src='@rasterizer', drawBB='false') 
        objMainNode.createObject('MechanicalObject', template='Affine', name='parent', src='@../../frame_topo', showObject=1, showObjectScale='0.1')
        objMainNode.createObject('RigidScaleToAffineMultiMapping', template='Rigid,Vec3d,Affine', input1='@../../rigid/DOFs', input2='@../../scale/DOFs', output='@.', index='0 0 0', printLog='0')
        objMainNode.createObject('VoronoiShapeFunction', template='ShapeFunctiond,ImageUC', name='SF', position='@parent.rest_position', image='@image.image', transform='@image.transform', nbRef=4, clearData=1, bias=0)  
        # Contact        
        objContactNode = objMainNode.createChild('registration')     
        objContactNode.createObject('MeshTopology', name='topo', src='@../source')
        objContactNode.createObject('MechanicalObject', name='DOFs')
        objContactNode.createObject('UniformMass', totalMass=1)
        objContactNode.createObject('TriangleModel')
        objContactNode.createObject('LinearMapping', template='Affine,Vec3d')
        # Visual model
        objVisuNode = objContactNode.createChild('visual')
        objVisuNode.createObject('OglModel', template='ExtVec3f', name='visual', src='@../topo', color='1 0.2 0.2 0.9')
        objVisuNode.createObject('IdentityMapping', template='Vec3d,ExtVec3f') 
        # Registration
        objRegistrationNode = objMainNode.createChild('force')
        objRegistrationNode.createObject('MechanicalObject', template='Vec3d', name='DOFs', src='@../source')
        objRegistrationNode.createObject('LinearMapping', template='Affine,Vec3d')
        # registration force field
        springs = ""
        for i in range(len(loader.position)):
            springs += str(i)+' '+str(i)+' '
        distanceNode = objRegistrationNode.createChild('registration_constraint')
        targetNode.addChild(distanceNode)
        distanceNode.createObject('MechanicalObject', template='Vec3d', name='distanceDOFs')
        distanceNode.createObject('DifferenceMultiMapping', template='Vec3d,Vec3d', input='@'+Tools.node_path_rel(distanceNode, targetNode)+' @'+Tools.node_path_rel(distanceNode, objRegistrationNode), output='@.', pairs=springs, showObjectScale="0.005")
        distanceNode.createObject('UniformCompliance', name='constraint', isCompliance=0, compliance=1E-6, damping=0.1)
Esempio n. 20
0
    def __insert_registration_node(self):
        """ Insert the registration node in the graph, under dofRigid node. """
        registration_node = self.createChild(self.nodes["dofRigid"],
                                             "RegistrationNode")

        param = self.param  # shortcut

        # merge topologies
        repartition = ""
        sources_component_name = []

        collision_node = [elem.collision for elem in self.rigids.values()]

        for i, elem in enumerate(collision_node):
            sources_component_name.append(
                Tools.node_path_rel(registration_node, elem.node) +
                "/topology")

            repartition += "{0} ".format(i) * len(elem.topology.position)

        registration_node.createObject(
            'MergeMeshes',
            name='source_topology',
            nbMeshes=len(sources_component_name),
            **dict(
                {
                    'position' + str(i + 1): '@' + item + '.position'
                    for i, item in enumerate(sources_component_name)
                }, **{
                    'triangles' + str(i + 1): '@' + item + '.triangles'
                    for i, item in enumerate(sources_component_name)
                }))

        registration_node.createObject('MeshTopology',
                                       name='topo',
                                       src='@./source_topology')
        registration_node.createObject('MechanicalObject', name='DOFs')
        registration_node.createObject('Triangle')

        registration_node.createObject(
            'RigidMapping',
            template='Rigid3d,Vec3d',
            input="@" +
            Tools.node_path_rel(registration_node, self.nodes["dofRigid"]),
            output="@DOFs",
            rigidIndexPerPoint=repartition)

        registration_node.createObject(
            'NormalsFromPoints',
            name='NormalsFromPoints',
            template='Vec3d',
            position='@DOFs.position',
            triangles='@topo.triangles',
            invertNormals=self.param.invertSourceNormals)

        # Force Field
        target_path = Tools.node_path_rel(registration_node,
                                          self.nodes["Target"])
        registration_node.createObject(
            'ClosestPointRegistrationForceField',
            name='ICP',
            template='Vec3d',
            # source
            sourceTriangles='@topo.triangles',
            sourceNormals='@NormalsFromPoints.normals',
            # target
            position='@{0}/loader.position'.format(target_path),
            triangles='@{0}/loader.triangles'.format(target_path),
            normals='@{0}/NormalsFromPoints.normals'.format(target_path),
            # Param
            cacheSize='4',
            blendingFactor=param.blendingFactor,
            stiffness=param.stiffness,
            damping=param.damping,
            outlierThreshold=param.outlierThreshold,
            normalThreshold=param.normalThreshold,
            rejectOutsideBbox=param.rejectOutsideBbox,
            drawColorMap='0')