-
Notifications
You must be signed in to change notification settings - Fork 55
Add two examples #1 and #3 #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bakpaul
wants to merge
1
commit into
sofa-framework:25_04_work_on_new_prefabs
Choose a base branch
from
bakpaul:26_03_add_modifiers
base: 25_04_work_on_new_prefabs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
|
|
||
| import splib.mechanics.fix_points | ||
| from splib.core.enum_types import ConstraintType | ||
| from stlib.core.baseNodeModifierParameters import BaseNodeModifierParameters, SingleNodeModifierParameters, PairNodeModifierParameters | ||
| from splib.core.utils import DEFAULT_VALUE | ||
| from stlib.entities import Entity | ||
| import Sofa | ||
| from Sofa.Core import Node | ||
|
|
||
| import dataclasses | ||
|
|
||
| class NodeModifier(Sofa.Core.Controller): | ||
|
|
||
| parameters : BaseNodeModifierParameters | ||
|
|
||
| def __init__(self, parameters : BaseNodeModifierParameters): | ||
| Sofa.Core.Controller.__init__(self, **parameters.toDict()) | ||
| self.parameters = parameters | ||
|
|
||
|
|
||
| class SingleNodeModifier(NodeModifier): | ||
|
|
||
| def __init__(self, parameters : BaseNodeModifierParameters): | ||
| super().__init__(parameters) | ||
|
|
||
| def modify(self, node : Node): | ||
| self.parameters.modifier(node) | ||
|
|
||
|
|
||
| class PairNodeModifier(NodeModifier): | ||
|
|
||
| def __init__(self, parameters : BaseNodeModifierParameters): | ||
| super().__init__(parameters) | ||
|
|
||
| def modify(self, node1 : Node, node2 : Node): | ||
| self.parameters.modifier(node1,node2) | ||
|
|
||
|
|
||
|
|
||
| class MultiNodeModifier(NodeModifier): | ||
|
|
||
| def __init__(self, parameters : BaseNodeModifierParameters): | ||
| super().__init__(parameters) | ||
|
|
||
| def modify(self, *nodes : Node ): | ||
| self.parameters.modifier(*nodes) | ||
|
|
||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class FixConstraintParameters(SingleNodeModifierParameters): | ||
| constraintType : ConstraintType = ConstraintType.PROJECTIVE | ||
| boxROI : list[ list[ float ] ] = dataclasses.field(default_factory= [[0,0,0], [1,1,1]]) | ||
|
|
||
| def modifier(self, node : Node): | ||
| splib.mechanics.fix_points.addFixation(node.Material, type = self.constraintType, boxROIs = self.boxROI) | ||
| pass | ||
|
|
||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class AttachmentConstraintParameters(PairNodeModifierParameters): | ||
| constraintType : ConstraintType = ConstraintType.PROJECTIVE | ||
|
|
||
| def modifier(self, node1 : Node, node2 : Node): | ||
| # splib.mechanics.fix_points.addFixation(node, type = self.constraintType, boxROI = self.boxROI) | ||
| pass | ||
|
|
||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class SimulationSolversParameters(SingleNodeModifierParameters): | ||
| constraintType : ConstraintType = ConstraintType.PROJECTIVE | ||
| boxROI : list[ list[ float ] ] = DEFAULT_VALUE | ||
|
|
||
| def modifier(self, node1 : Node): | ||
| splib.simulation.ode_solvers.addImplicitODE(node1) | ||
| splib.simulation.linear_solvers.addLinearSolver(node1, constantSparsity=False, ) | ||
| pass | ||
|
|
||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class SimulationSettingsParameters(SingleNodeModifierParameters): | ||
| useLagrangian : bool = False | ||
|
|
||
| def modifier(self, node1 : Node): | ||
| if(self.useLagrangian): | ||
| splib.simulation.headers.setupLagrangianCollision(root, displayFlags = "showVisualModels",backgroundColor=[0.8, 0.8, 0.8, 1], | ||
| parallelComputing = True,alarmDistance=0.3, contactDistance=0.02, | ||
| frictionCoef=0.5, tolerance=1.0e-4, maxIterations=20) | ||
| else: | ||
| splib.simulation.headers.setupDefaultHeader(root, displayFlags = "showVisualModels", | ||
| backgroundColor=[0.8, 0.8, 0.8, 1], | ||
| parallelComputing = True) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| from stlib.geometries.plane import PlaneParameters | ||
| from stlib.geometries.file import FileParameters | ||
| from stlib.geometries.extract import ExtractParameters | ||
| from stlib.materials.deformable import DeformableBehaviorParameters | ||
| from stlib.collision import Collision, CollisionParameters | ||
| from stlib.entities import Entity, EntityParameters | ||
| from stlib.visual import Visual, VisualParameters | ||
| from splib.core.enum_types import CollisionPrimitive, ElementType, ConstitutiveLaw | ||
| from splib.simulation.headers import setupLagrangianCollision, setupDefaultHeader | ||
| from splib.simulation.ode_solvers import addImplicitODE | ||
| from splib.simulation.linear_solvers import addLinearSolver | ||
| from stlib.core.basePrefab import BasePrefab, BasePrefabParameters | ||
|
|
||
| ### !!!!!! To be able to apply the modifier from the root, because I need to do this for the simulation settings | ||
| # for instance, I need it to be a prefab, and this ONLY to be able to add a secondary 'informative' object in it automatically | ||
| # ----> BEURK | ||
| root = BasePrefab(parameters=BasePrefabParameters(name="root")) | ||
|
|
||
|
|
||
| root.addObject("RequiredPlugin", pluginName=[ 'Sofa.Component', | ||
| 'Sofa.GL.Component.Rendering3D', | ||
| 'SofaImGui' | ||
| ]) | ||
|
|
||
|
|
||
| LogoParams = EntityParameters(name = "Logo", | ||
| geometry = FileParameters(filename="mesh/SofaScene/Logo.vtk"), | ||
| material = DeformableBehaviorParameters(), | ||
| collision = CollisionParameters(geometry = FileParameters(filename="mesh/SofaScene/LogoColli.sph")), | ||
| visual = VisualParameters(geometry = FileParameters(filename="mesh/SofaScene/LogoVisu.obj"))) | ||
|
|
||
| LogoParams.geometry.elementType = ElementType.TETRAHEDRA | ||
| LogoParams.material.constitutiveLawType = ConstitutiveLaw.ELASTIC | ||
| LogoParams.material.parameters = [200, 0.4] | ||
| LogoParams.material.massDensity = 0.003261 | ||
| LogoParams.collision.primitives = [CollisionPrimitive.SPHERES] | ||
| #TODO make this flawless with spheres. Here collisions elements are not in the topology and a link is to be made between the loader and the collision object | ||
| LogoParams.collision.kwargs = {"SphereCollision" : {"radius" : "@Geometry/loader.listRadius"}} | ||
| LogoParams.visual.color = [0.7, .35, 0, 0.8] | ||
|
|
||
| Logo = root.add(Entity, parameters = LogoParams) | ||
|
|
||
| ### Who needs to apply it ? Logo or root ? | ||
| # Logo.applyModifier(SingleNodeModifier, SimulationSolversParameters(), Logo) | ||
| root.applyModifier(SingleNodeModifier, SimulationSolversParameters(name="solvers"), Logo) | ||
| root.applyModifier(SingleNodeModifier, FixConstraintParameters(name="fixedConstraints", boxROI=[[0, -20, -20], [1, 20, 20]]), Logo) | ||
| # | ||
| root.applyModifier(SingleNodeModifier, SimulationSettingsParameters(name="settings"), root) | ||
|
|
||
|
|
||
| root.gravity=[0,0,9.81] | ||
| Sofa.Simulation.initRoot(root) | ||
|
|
||
| import Sofa.Gui | ||
|
|
||
| # Find out the supported GUIs | ||
| print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(",")) | ||
| # Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py") | ||
| Sofa.Gui.GUIManager.Init("myscene", "imgui") | ||
| Sofa.Gui.GUIManager.createGUI(root, __file__) | ||
| Sofa.Gui.GUIManager.SetDimension(1080, 1080) | ||
| # Initialization of the scene will be done here | ||
| Sofa.Gui.GUIManager.MainLoop(root) | ||
| Sofa.Gui.GUIManager.closeGUI() | ||
| print("GUI was closed") | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong copy/past TBH, thank's !