-
Notifications
You must be signed in to change notification settings - Fork 8
[scripts] Add option to display reference state along the simulation to visually compare them #88
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
epernod
wants to merge
4
commits into
sofa-framework:master
Choose a base branch
from
epernod:replayRegression
base: master
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,6 @@ | |
| import pathlib | ||
|
|
||
| import Sofa | ||
| import Sofa.Gui | ||
|
|
||
| debug_info = False | ||
|
|
||
| def is_simulated(node): | ||
| if node.hasODESolver(): | ||
|
|
@@ -23,6 +20,35 @@ def is_simulated(node): | |
| return False | ||
|
|
||
|
|
||
| class ReplayState(Sofa.Core.Controller): | ||
| def __init__(self, node, slave_mo, state_filename, **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.node = node | ||
| self.slave_mo = slave_mo | ||
| self.keyframes = [] | ||
| self.frame_step = 0 | ||
| self.t_sim = 0.0 | ||
|
|
||
| with gzip.open(state_filename, 'r') as zipfile: | ||
| self.ref_data = json.loads(zipfile.read().decode('utf-8')) | ||
| for key in self.ref_data: | ||
| self.keyframes.append(float(key)) | ||
|
|
||
| if (self.keyframes[0] == 0.0): # frame 0.0 | ||
| tmp_position = np.asarray(self.ref_data[str(self.keyframes[0])]) | ||
| self.slave_mo.position = tmp_position.tolist() | ||
| self.frame_step = 1 | ||
|
|
||
| def onAnimateEndEvent(self, event): | ||
| dt = float(self.node.getRootContext().dt.value) | ||
| self.t_sim += dt | ||
|
|
||
| if abs(self.t_sim - self.keyframes[self.frame_step]) < 0.000001: | ||
| tmp_position = np.asarray(self.ref_data[str(self.keyframes[self.frame_step])]) | ||
| self.slave_mo.position = tmp_position.tolist() | ||
| self.frame_step += 1 | ||
|
|
||
|
|
||
| class NumpyArrayEncoder(JSONEncoder): | ||
| def default(self, obj): | ||
| if isinstance(obj, np.ndarray): | ||
|
|
@@ -32,7 +58,7 @@ def default(self, obj): | |
|
|
||
| class RegressionSceneData: | ||
| def __init__(self, file_scene_path: str = None, file_ref_path: str = None, steps = 1000, | ||
| epsilon = 0.0001, meca_in_mapping = True, dump_number_step = 1, disable_progress_bar = False): | ||
| epsilon = 0.0001, meca_in_mapping = True, dump_number_step = 1, disable_progress_bar = False, verbose = False): | ||
| """ | ||
| /// Path to the file scene to test | ||
| std::string m_fileScenePath; | ||
|
|
@@ -63,6 +89,7 @@ def __init__(self, file_scene_path: str = None, file_ref_path: str = None, steps | |
| self.regression_failed = False | ||
| self.root_node = None | ||
| self.disable_progress_bar = disable_progress_bar | ||
| self.verbose = verbose | ||
|
|
||
| def print_info(self): | ||
| print("Test scene: " + self.file_scene_path + " vs " + self.file_ref_path + " using: " + str(self.steps) | ||
|
|
@@ -99,9 +126,13 @@ def parse_node(self, node, level = 0): | |
| def add_compare_state(self): | ||
| counter = 0 | ||
| for meca_obj in self.meca_objs: | ||
| _filename = self.file_ref_path + ".reference_" + str(counter) + "_" + meca_obj.name.value + "_mstate" + ".txt.gz" | ||
| # Use this filename format to be compatible with previous version | ||
| #_filename = self.file_ref_path + ".reference_" + str(counter) + "_" + meca_obj.name.value + "_mstate" + ".txt.gz" | ||
| _filename = self.file_ref_path + ".reference_mstate_" + str(counter) + "_" + meca_obj.name.value + ".json.gz" | ||
|
|
||
| meca_obj.getContext().addObject('CompareState', filename=_filename) | ||
| compareNode = meca_obj.getContext().addChild("CompareStateNode_"+str(counter)) | ||
| cloudPoint = compareNode.addObject('VisualPointCloud', pointSize=10, drawMode="Point", color="green") | ||
| compareNode.addObject(ReplayState(node=compareNode, slave_mo=cloudPoint, state_filename=_filename)) | ||
| counter = counter+1 | ||
|
|
||
|
|
||
|
|
@@ -120,7 +151,7 @@ def load_scene(self): | |
| print(f'Error while trying to load {self.file_scene_path}') | ||
| raise RuntimeError | ||
| else: | ||
| Sofa.Simulation.init(self.root_node) | ||
| Sofa.Simulation.initRoot(self.root_node) | ||
|
|
||
| # prepare ref files per mecaObjs: | ||
| self.parse_node(self.root_node, 0) | ||
|
|
@@ -217,7 +248,7 @@ def compare_references(self): | |
| full_dist = np.linalg.norm(data_diff) | ||
| error_by_dof = full_dist / float(data_diff.size) | ||
|
|
||
| if debug_info: | ||
| if self.verbose: | ||
| print (str(step) + "| " + self.meca_objs[meca_id].name.value + " | full_dist: " + str(full_dist) + " | error_by_dof: " + str(error_by_dof) + " | nbrDofs: " + str(data_ref.size)) | ||
|
|
||
| self.total_error[meca_id] = self.total_error[meca_id] + full_dist | ||
|
|
@@ -243,10 +274,14 @@ def compare_references(self): | |
| return True | ||
|
|
||
|
|
||
| def replayReferences(self): | ||
| Sofa.Gui.GUIManager.Init("myscene", "qglviewer") | ||
| def replay_references(self): | ||
|
|
||
| # Import the GUI package | ||
| import SofaImGui | ||
| import Sofa.Gui | ||
| Sofa.Gui.GUIManager.Init("myscene", "imgui") | ||
| Sofa.Gui.GUIManager.createGUI(self.root_node, __file__) | ||
| Sofa.Gui.GUIManager.SetDimension(1080, 1080) | ||
| Sofa.Gui.GUIManager.SetDimension(1920, 1080) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no idea it is a copy past from other projects |
||
| Sofa.Gui.GUIManager.MainLoop(self.root_node) | ||
| Sofa.Gui.GUIManager.closeGUI() | ||
|
|
||
|
|
||
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
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.
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.
I would like to keep this one, I added a comment above why