Skip to content

Instantly share code, notes, and snippets.

@zbounik
Created May 5, 2019 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbounik/1e9979479dc1654d299b56872c537e7e to your computer and use it in GitHub Desktop.
Save zbounik/1e9979479dc1654d299b56872c537e7e to your computer and use it in GitHub Desktop.
import Sofa
#### deleting the contents of the saved mesh folders from previous run
#
#impo
def createScene(rootNode):
rootNode.createObject('RequiredPlugin', name='SofaPython')
mySimulation = liverVolumeSurface(rootNode,[])
return 0;
class liverVolumeSurface (Sofa.PythonScriptController):
def initGraph(self,rootNode):
# rootNode
rootNode.createObject('DefaultPipeline', name='CollisionPipeline', verbose='0')
rootNode.createObject('BruteForceDetection', name='N2')
rootNode.createObject('DefaultContactManager', name='collision response', response='default')
rootNode.createObject('DiscreteIntersection', name='discreteIntersection1')
# rootNode.createObject('PythonScriptController', classname="controller", filename='controller.py')
# rootNode/Liver
Liver = rootNode.createChild('Liver')
Liver.depend = 'topo dofs'
Liver.createObject('EulerImplicitSolver', name='cg_odesolver', printLog='0')
Liver.createObject('CGLinearSolver', threshold='1e-09', tolerance='1e-09', name='linear solver', iterations='25', template='GraphScattered')
Liver.createObject('MeshGmshLoader', tetrahedraGroups=' -1 0 38144', name='loader', filename='/home/zahra/sofa/Work/Exported/undeformed_meshes_editAbovePlaneForceField/InputLiverVolumeFine.gmsh')
Liver.createObject('TetrahedronSetTopologyContainer', triangles='@loader.triangles', position='@loader.position', edges='@loader.edges', name='topo', tetrahedra='@loader.tetras')
Liver.createObject('MechanicalObject', force='0 0 0', name='dofs', template='Vec3d', position='@loader.position', velocity='0 0 0', externalForce='0 0 0', restScale='1')
Liver.createObject('TetrahedronSetGeometryAlgorithms', drawColorEdges='0.4 1 0.298039 1', showIndicesScale='0.01', name='GeomAlgo', showPointIndices='0', template='Vec3d')
Liver.createObject('DiagonalMass', massDensity='1', name='computed using mass density', template='Vec3d')
Liver.createObject('TetrahedralCorotationalFEMForceField', poissonRatio='0.3', name='FEM', computeGlobalMatrix='0', method='large', template='Vec3d', youngModulus='120')
Liver.createObject('PlaneForceField', name='planeFF0', template='Vec3d', normal='0 1 0', draw='1', drawSize='8')
Liver.createObject('ConstantForceField', indices='111', name='constantFF0', template='Vec3d', forces='0 50 0')
Liver.createObject('FixedConstraint', indices='0 11 17 16 15 14 4 3 45 95 42 40 38 39 53 62 61 60 4 63 59 125 24 20 19 9', showObject='0', drawSize='0', name='fixedConstraint0', template='Vec3d')
Liver.createObject('MeshExporter', exportEveryNumberOfSteps='1', quads='0', name='meshExporter0', format='gmsh', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Fine_volume/LiverVolumeFine', exportAtBegin='0', tetras='1', hexas='0', edges='0', triangles='0', listening='0')
Liver.createObject('TetrahedronSetTopologyModifier', name='tetrahedronSetTopologyModifier9')
Liver.createObject('TetrahedronSetTopologyAlgorithms', name='tetrahedronSetTopologyAlgorithms10', template='Vec3d')
# rootNode/Liver/surface_fine
surface_fine = Liver.createChild('surface_fine')
surface_fine.createObject('TriangleSetTopologyContainer', name='triangleSetTopologyContainer0')
surface_fine.createObject('TriangleSetTopologyModifier', name='triangleSetTopologyModifier1')
surface_fine.createObject('TriangleSetTopologyAlgorithms', name='triangleSetTopologyAlgorithms2', template='Vec3d')
surface_fine.createObject('TriangleSetGeometryAlgorithms', name='triangleSetGeometryAlgorithms3', template='Vec3d')
surface_fine.createObject('Tetra2TriangleTopologicalMapping', input='@../', name='tetra2TriangleTopologicalMapping4', output='@./')
# surface_fine.createObject('TriangularFEMForceField', name='triangularFEMFF5', template='Vec3d', poissonRatio='0.3', youngModulus='60')
surface_fine.createObject('TriangularBendingSprings', name='triangularBendingSprings6', template='Vec3d', stiffness="30", damping="1")
surface_fine.createObject('TTriangleModel', name='tTriangleModel15', template='Vec3d')
# surface_fine.createObject('TrianglePressureForceField', name='trianglePressureFF22', template='Vec3d')
surface_fine.createObject('MeshExporter', exportEveryNumberOfSteps='1', quads='0', name='meshExporter1', format='gmsh', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Fine_surface/LiverSurfaceFine', exportAtBegin='0', tetras='0', hexas='0', edges='0', triangles='1', listening='0')
# rootNode/Liver/surface_fine/visu_surface_fine
visu_surface_fine = surface_fine.createChild('visu_surface_fine')
visu_surface_fine.createObject('OglModel', fileMesh='/home/zahra/sofa/Work/Exported/undeformed_meshes_editAbovePlaneForceField/InputLiverVisuSurfaceFine.obj', sfactor='GL_SRC_ALPHA', blendEquation='GL_FUNC_ADD', name='oglModel18', template='ExtVec3f', dfactor='GL_ONE_MINUS_SRC_ALPHA', material='Default Diffuse 1 0.1 0.0 0.9 1 Ambient 1 0.2 0.2 0.2 1 Specular 0 1 1 1 1 Emissive 0 0 0 0 0 Shininess 0 45 ', primitiveType='DEFAULT')
visu_surface_fine.createObject('BarycentricMapping', input='@../../dofs', name='BarycentricMap1', template='Vec3d,ExtVec3f', output='@oglModel18/')
##### I have tried this: When I use OBJExporter to export the surface in OBJ format, the number of the nodes are the same as the number of the nodes of the volumetric mesh. So, instead of this way, I follow these steps:
# 1. export GMSH 2. Using Liver_GMSH_Compare_noClass, Function: "writeEfficientSurfaceGmshWithoutUnconnectedNodes" correct the number of nodes 3. Convert GMSH to OBJ
# SO, I disabled the "visu_surface_fine.createObject('OBJExporter..." in below
##update to the explanation in the above row: When I did that,at runtime the visual obj meshes didn't correspond to the forcefield meshes, so I returned back to the previous method and used the OBJExporter as below also for the undeformed mesh,
## so again I enabled the following row:
# visu_surface_fine.createObject('OBJExporter', exportEveryNumberOfSteps='0', exportAtBegin='0', name='oBJExporter1', listening='0', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Fine_surface/LiverVisuSurfaceFine')
visu_surface_fine.createObject('MeshExporter', exportEveryNumberOfSteps='0', quads='0', name='meshExporter4', format='gmsh', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Fine_surface/LiverVisuSurfaceFine_zzzzzz', exportAtBegin='0', tetras='0', hexas='0', edges='0', triangles='1', listening='0')
# rootNode/Liver/coarse
coarse = Liver.createChild('coarse')
coarse.createObject('MeshGmshLoader', tetrahedraGroups=' -1 0 596', name='meshGmshLoader1', filename='/home/zahra/sofa/Work/Exported/undeformed_meshes_editAbovePlaneForceField/InputLiverVolumeCoarse.gmsh')
coarse.createObject('TetrahedronSetTopologyContainer', triangles='@meshGmshLoader1.triangles', position='@meshGmshLoader1.position', edges='@meshGmshLoader1.edges', name='tetrahedronSetTopologyContainer2', tetrahedra='@meshGmshLoader1.tetras')
coarse.createObject('MechanicalObject', force='0 0 0', name='mObject0', template='Vec3d', position='@meshGmshLoader1.position', velocity='0 0 0', externalForce='0 0 0', restScale='1')
coarse.createObject('TetrahedronSetTopologyModifier', name='tetrahedronSetTopologyModifier3')
coarse.createObject('TetrahedronSetGeometryAlgorithms', drawColorEdges='0.4 1 0.298039 1', recomputeTrianglesOrientation='0', name='tetrahedronSetGeometryAlgorithms0', showPointIndices='1', flipNormals='1', showIndicesScale='0.01', template='Vec3d')
coarse.createObject('TetrahedronSetTopologyAlgorithms', name='tetrahedronSetTopologyAlgorithms1', template='Vec3d')
coarse.createObject('MeshExporter', exportEveryNumberOfSteps='1', quads='0', name='meshExporter2', format='gmsh', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Coarse_volume/LiverVolumeCoarse', exportAtBegin='0', tetras='1', hexas='0', edges='0', triangles='0', listening='0')
# coarse.createObject('EulerImplicitSolver', name='eulerImplicitSolver0')
# coarse.createObject('CGLinearSolver', name='cGLinearSolver1', template='GraphScattered')
# coarse.createObject('DiagonalMass', name='diagonalMass2', template='Vec3d')
# coarse.createObject('FixedConstraint', indices='0', name='fixedConstraint3', template='Vec3d')
# coarse.createObject('PlaneForceField', name='planeFF4', template='Vec3d', normal='0 1 0')
# coarse.createObject('TetrahedralCorotationalFEMForceField', youngModulus='12000', name='tetrahedralCorotationalFEMFF5', poissonRatio='0.3', template='Vec3d')
coarse.createObject('BarycentricMapping', input='@../', name='mapping', template='Vec3d,Vec3d', output='@./')
# rootNode/Liver/coarse/surface_coarse
surface_coarse = coarse.createChild('surface_coarse')
surface_coarse.createObject('TriangleSetTopologyContainer', name='triangleSetTopologyContainer7')
surface_coarse.createObject('TriangleSetTopologyModifier', name='triangleSetTopologyModifier8')
surface_coarse.createObject('TriangleSetTopologyAlgorithms', name='triangleSetTopologyAlgorithms9', template='Vec3d')
surface_coarse.createObject('TriangleSetGeometryAlgorithms', name='triangleSetGeometryAlgorithms10', template='Vec3d')
surface_coarse.createObject('Tetra2TriangleTopologicalMapping', input='@../', name='tetra2TriangleTopologicalMapping11', output='@./')
# surface_coarse.createObject('TriangularFEMForceField', name='triangularFEMFF12', template='Vec3d', poissonRatio='0.3', youngModulus='60')
surface_coarse.createObject('TriangularBendingSprings', name='triangularBendingSprings13', template='Vec3d', stiffness="30", damping="1")
surface_coarse.createObject('TTriangleModel', name='tTriangleModel14', template='Vec3d')
# surface_coarse.createObject('TrianglePressureForceField', name='trianglePressureFF21', template='Vec3d')
surface_coarse.createObject('MeshExporter', exportEveryNumberOfSteps='1', quads='0', name='meshExporter3', format='gmsh', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Coarse_surface/LiverSurfaceCoarse', exportAtBegin='0', tetras='0', hexas='0', edges='0', triangles='1', listening='0')
# rootNode/Liver/coarse/surface_coarse/visu_surface_coarse
visu_surface_coarse = surface_coarse.createChild('visu_surface_coarse')
#visu_surface_coarse.createObject('OglModel', fileMesh='/home/zahra/sofa/Work/Exported/undeformed_meshes/CleanSurfaceMeshes/liver-smooth.obj' ,sfactor='GL_SRC_ALPHA', blendEquation='GL_FUNC_ADD', name='oglModel17', template='ExtVec3f', dfactor='GL_ONE_MINUS_SRC_ALPHA', material='Default Diffuse 1 0.74902 0.74902 0.74902 1 Ambient 1 0.2 0.2 0.2 1 Specular 0 1 1 1 1 Emissive 0 0 0 0 0 Shininess 0 45 ', primitiveType='DEFAULT')
#### the surface mesh of the coarse volume is the same as the surface mesh of the fine one
visu_surface_coarse.createObject('OglModel', fileMesh='/home/zahra/sofa/Sofa17.06/src/share/mesh/liver-smooth.obj', sfactor='GL_SRC_ALPHA', blendEquation='GL_FUNC_ADD', name='oglModel17', template='ExtVec3f', dfactor='GL_ONE_MINUS_SRC_ALPHA', material='Default Diffuse 1 0.7 0.2 0.2 1 Ambient 1 0.2 0.2 0.2 1 Specular 0 1 1 1 1 Emissive 0 0 0 0 0 Shininess 0 45 ', primitiveType='DEFAULT')
visu_surface_coarse.createObject('BarycentricMapping', input='@../../mObject0', name='BarycentricMap2', template='Vec3d,ExtVec3f', output='@oglModel17/')
##### I have tried this: When I use OBJExporter to export the surface in OBJ format, the number of the nodes are the same as the number of the nodes of the volumetric mesh. So, instead of this way, I follow these steps:
# 1. export GMSH 2. Using Liver_GMSH_Compare_noClass, Function: "writeEfficientSurfaceGmshWithoutUnconnectedNodes" correct the number of nodes 3. Convert GMSH to OBJ
# SO, I disabled the "visu_surface_coarse.createObject('OBJExporter..." in below
## so again I enabled the following row:
# visu_surface_coarse.createObject('OBJExporter', exportEveryNumberOfSteps='0', exportAtBegin='0', name='oBJExporter2', listening='0', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Coarse_surface/LiverVisuSurfaceCoarse')
#mesh exporter for visual model doesn't work properly, it exports the surface_coarse, not the visu_surface_coarse
visu_surface_coarse.createObject('MeshExporter', exportEveryNumberOfSteps='0', quads='0', name='meshExporter5', format='gmsh', exportAtEnd='1', filename='/home/zahra/sofa/Work/Exported/Coarse_surface/LiverVisuSurfaceCoarse_zzzz', exportAtBegin='0', tetras='0', hexas='0', edges='0', triangles='1', listening='0')
# rootNode/Floor
# Floor = rootNode.createChild('Floor')
# Floor.createObject('MeshTopology', name='Topology Floor', filename='/home/zahra/sofa/src/share/mesh/floor.obj')
# Floor.createObject('MechanicalObject', name='Particles Floor')
# Floor.createObject('Triangle', moving='0', name='Triangle For Collision Floor', simulated='0')
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment