import Sofa

poissonRatio = 0.49
youngModulus = 100
pressure = 5

indentorRadius = 0.3

def createScene(node): 
    
    print 'Creation of Sofa SceneGraph'    
    
    node.gravity = [0,0,0]

    node.createObject('RequiredPlugin', pluginName="Flexible") 
    node.createObject('RequiredPlugin', pluginName="AFM_plane")

    node.createObject('VisualStyle', displayFlags = 'showVisual hideBehavior showCollisionModels')
    node.createObject('DefaultPipeline', name="DefaultCollisionPipeline",  verbose="0",  draw="0",  depth="6")
    node.createObject('BruteForceDetection', name="Detection")
    node.createObject('MinProximityIntersection', name="Proximity",  alarmDistance="0.05",  contactDistance="0.0")
    node.createObject('MyDefaultContactManager', name="Response",  response="default", responseParams="")
    node.createObject('TreeCollisionGroupManager', name="Group")

    node.createObject('CGLinearSolver', template="GraphScattered",iterations=500,threshold=1e-10,tolerance=1e-10)
    node.createObject('EulerImplicitSolver', rayleighStiffness=0.5,rayleighMass=0.5)

    node.createObject('GridMeshCreator', name='Grid', resolution="10 10", triangulate=1)
    node.createObject('MeshTopology', name='Topology',src="@Grid")
    node.createObject('MechanicalObject', name='DOFs', template="Vec3d")    
    node.createObject('UniformMass',name='mass', template='Vec3d')
    node.createObject('FixedConstraint',template="Vec3d", indices="0 9 90 99" )

    ## Pressure
    node.createObject("SurfacePressureForceField",  pressure=pressure)

    ## Deformation, Stress and Strain
    node.createObject('BarycentricShapeFunction', template="ShapeFunctiond")
    deformation = node.createChild("deformation")
    deformation.createObject('TopologyGaussPointSampler',name='sampler', inPosition='@../Topology.position' , showSamples="false", method="0", order="1" )
    deformation.createObject('MechanicalObject', name='deform', template='F321')
    deformation.createObject('LinearMapping', template='Vec3d,F321')
    stressStrain = deformation.createChild('stressStrain')
    stressStrain.createObject('MechanicalObject', name='Strain',template='E321')
    stressStrain.createObject('CorotationalStrainMapping', template="F321,E321")
    stressStrain.createObject('HookeForceField', template="E321", youngModulus=youngModulus,poissonRatio=poissonRatio, viscosity=0  )

    ## Collision
    node.createObject('TriangleModel',contactStiffness=1000,group=0)
    node.createObject('LineModel',contactStiffness=1000,group=0)
    node.createObject('PointModel',contactStiffness=1000,group=0)
    Visu = node.createChild('Visu')
    Visu.createObject('OglModel')
    Visu.createObject('IdentityMapping', template='Vec3d,ExtVec3f')

    ## indentor
    indentor = node.createChild('indentor')
    indentor.createObject('MechanicalObject',name='indentor', position="0.5 0.5 2")
    indentor.createObject('UniformMass')
    indentor.createObject('ConstantForceField', points=0, force="0 0 -1")
    indentor.createObject('TSphereModel', radius= indentorRadius, ContactStiffness=1000, group=1) 
    indentor.createObject('ProjectToLineConstraint',endTime='-1', indices=0, origin="0.5 0.5 2", direction="0 0 1") 
    indentor.createObject('PointConstraint', endTime=3, indices=0)                     
