-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,37 @@ 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 | ||||||||||||||||
| #print('nbr Frames', len(self.keyframes)) | ||||||||||||||||
|
|
||||||||||||||||
| 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() | ||||||||||||||||
| #print('tmp_position: ', tmp_position.shape) | ||||||||||||||||
|
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.
Suggested change
|
||||||||||||||||
| self.frame_step += 1 | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| class NumpyArrayEncoder(JSONEncoder): | ||||||||||||||||
| def default(self, obj): | ||||||||||||||||
| if isinstance(obj, np.ndarray): | ||||||||||||||||
|
|
@@ -32,7 +60,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 +91,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 +128,12 @@ 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" | ||||||||||||||||
| #_filename = self.file_ref_path + ".reference_" + str(counter) + "_" + meca_obj.name.value + "_mstate" + ".txt.gz" | ||||||||||||||||
|
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.
Suggested change
|
||||||||||||||||
| _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 | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -217,7 +249,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 +275,17 @@ 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 | ||||||||||||||||
| #supported_gui = Sofa.Gui.GUIManager.ListSupportedGUI(",") | ||||||||||||||||
| self.root_node.bbox = "-10 -10 -10 10 10 10" | ||||||||||||||||
| #print ("Supported GUIs are " + supported_gui) | ||||||||||||||||
|
Comment on lines
+283
to
+285
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.
Suggested change
|
||||||||||||||||
| 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 ? |
||||||||||||||||
| Sofa.Gui.GUIManager.MainLoop(self.root_node) | ||||||||||||||||
| Sofa.Gui.GUIManager.closeGUI() | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
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.