From 471c67caeb07f24c9a4f5010b831146cd48b0497 Mon Sep 17 00:00:00 2001 From: aymanhab Date: Tue, 9 Jul 2019 15:38:46 -0700 Subject: [PATCH 1/5] Add no_visualization Property to Model class and use it to bypass mesh loading --- OpenSim/Common/ModelDisplayHints.h | 6 ++++++ OpenSim/Simulation/Model/Geometry.cpp | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/OpenSim/Common/ModelDisplayHints.h b/OpenSim/Common/ModelDisplayHints.h index e103bca78b..6eceeb951c 100644 --- a/OpenSim/Common/ModelDisplayHints.h +++ b/OpenSim/Common/ModelDisplayHints.h @@ -53,6 +53,7 @@ your component produces. The currently-supported flags are: - show frames - show labels - show debug geometry + - no visualization This class is intended to provide some minimal user control over generated geometry in a form that is easy for a ModelComponent author to deal with, since @@ -102,6 +103,10 @@ class OSIMCOMMON_API ModelDisplayHints : public Object { OpenSim_DECLARE_PROPERTY(show_debug_geometry, bool, "Flag to indicate whether or not to show debug geometry, default to false."); + OpenSim_DECLARE_PROPERTY(no_visualization, bool, + "Flag to indicate whether or not to show geometry from mesh files, default to false."); + + /** Default construction creates a valid display hints object with all hints set to their default values. **/ ModelDisplayHints() { constructProperties(); } @@ -120,6 +125,7 @@ class OSIMCOMMON_API ModelDisplayHints : public Object { constructProperty_show_labels(false); constructProperty_show_forces(true); constructProperty_show_debug_geometry(false); + constructProperty_no_visualization(false); } }; diff --git a/OpenSim/Simulation/Model/Geometry.cpp b/OpenSim/Simulation/Model/Geometry.cpp index 244ac99b35..5777874f52 100644 --- a/OpenSim/Simulation/Model/Geometry.cpp +++ b/OpenSim/Simulation/Model/Geometry.cpp @@ -237,6 +237,11 @@ void Mesh::extendFinalizeFromProperties() { std::cout << "Mesh " << get_mesh_file() << " not connected to model..ignoring" << std::endl; return; // Orphan Mesh not descendant of a model } + const Model* ownerModel = dynamic_cast(rootModel); + + //No visualization don't try to load meshes + if (ownerModel->get_ModelVisualPreferences().get_ModelDisplayHints().get_no_visualization()) + return; // Current interface to Visualizer calls generateDecorations on every // frame. On first time through, load file and create DecorativeMeshFile From 74dd41f83dbe31b0a80a0644b5d839e1bc4d4938 Mon Sep 17 00:00:00 2001 From: aymanhab Date: Wed, 10 Jul 2019 14:22:35 -0700 Subject: [PATCH 2/5] Create no_visualization Property in ModelVisualPreferences and provide convenience methods to set it and query it. Use it in Mesh::extendFinalizeFromProperties to avoid file search for mesh files --- OpenSim/Common/ModelDisplayHints.h | 5 ----- OpenSim/Simulation/Model/Geometry.cpp | 2 +- OpenSim/Simulation/Model/Model.h | 11 ++++++++++- OpenSim/Simulation/Model/ModelVisualPreferences.h | 4 ++++ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/OpenSim/Common/ModelDisplayHints.h b/OpenSim/Common/ModelDisplayHints.h index 6eceeb951c..afbc904057 100644 --- a/OpenSim/Common/ModelDisplayHints.h +++ b/OpenSim/Common/ModelDisplayHints.h @@ -103,10 +103,6 @@ class OSIMCOMMON_API ModelDisplayHints : public Object { OpenSim_DECLARE_PROPERTY(show_debug_geometry, bool, "Flag to indicate whether or not to show debug geometry, default to false."); - OpenSim_DECLARE_PROPERTY(no_visualization, bool, - "Flag to indicate whether or not to show geometry from mesh files, default to false."); - - /** Default construction creates a valid display hints object with all hints set to their default values. **/ ModelDisplayHints() { constructProperties(); } @@ -125,7 +121,6 @@ class OSIMCOMMON_API ModelDisplayHints : public Object { constructProperty_show_labels(false); constructProperty_show_forces(true); constructProperty_show_debug_geometry(false); - constructProperty_no_visualization(false); } }; diff --git a/OpenSim/Simulation/Model/Geometry.cpp b/OpenSim/Simulation/Model/Geometry.cpp index 5777874f52..0b8ced3886 100644 --- a/OpenSim/Simulation/Model/Geometry.cpp +++ b/OpenSim/Simulation/Model/Geometry.cpp @@ -240,7 +240,7 @@ void Mesh::extendFinalizeFromProperties() { const Model* ownerModel = dynamic_cast(rootModel); //No visualization don't try to load meshes - if (ownerModel->get_ModelVisualPreferences().get_ModelDisplayHints().get_no_visualization()) + if (ownerModel->isVisualizationDisabled()) return; // Current interface to Visualizer calls generateDecorations on every diff --git a/OpenSim/Simulation/Model/Model.h b/OpenSim/Simulation/Model/Model.h index cefb73258b..bf713780ce 100644 --- a/OpenSim/Simulation/Model/Model.h +++ b/OpenSim/Simulation/Model/Model.h @@ -1064,7 +1064,16 @@ OpenSim_OBJECT_NONTEMPLATE_DEFS(Model, ModelComponent); SimTK::Array_& appendToThis) const override; /**@}**/ - + // Turn off visualization/loading of meshes for this model + // initSystem must be invoked after this call for it to take effect. + void disableVisualization() { + upd_ModelVisualPreferences().set_no_visualization(true); + } + // Return flag indicating if visualization has been turned off + bool isVisualizationDisabled() const { + return get_ModelVisualPreferences().get_no_visualization(); + } + //-------------------------------------------------------------------------- private: diff --git a/OpenSim/Simulation/Model/ModelVisualPreferences.h b/OpenSim/Simulation/Model/ModelVisualPreferences.h index 156ae6f99f..ea5f893a11 100644 --- a/OpenSim/Simulation/Model/ModelVisualPreferences.h +++ b/OpenSim/Simulation/Model/ModelVisualPreferences.h @@ -54,6 +54,9 @@ class OSIMSIMULATION_API ModelVisualPreferences : public Object { OpenSim_DECLARE_UNNAMED_PROPERTY(ModelDisplayHints, "Model display preferences"); + OpenSim_DECLARE_PROPERTY(no_visualization, bool, + "Flag to indicate whether or not to show geometry from mesh files, default to false."); + //-------------------------------------------------------------------------- // CONSTRUCTION //-------------------------------------------------------------------------- @@ -66,6 +69,7 @@ class OSIMSIMULATION_API ModelVisualPreferences : public Object { private: void constructProperties() { constructProperty_ModelDisplayHints(ModelDisplayHints()); + constructProperty_no_visualization(false); } //============================================================================= }; // END of class ModelVisualPreferences From 1e6278fd1615ee26daf74adf2f18a0374397d1e3 Mon Sep 17 00:00:00 2001 From: aymanhab Date: Thu, 11 Jul 2019 11:58:30 -0700 Subject: [PATCH 3/5] Change property name to load_mesh_files to reflect current functionality, flip logic and rename methods to reflect feedback on PR. --- OpenSim/Simulation/Model/Geometry.cpp | 2 +- OpenSim/Simulation/Model/Model.h | 10 +++++----- OpenSim/Simulation/Model/ModelVisualPreferences.h | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/OpenSim/Simulation/Model/Geometry.cpp b/OpenSim/Simulation/Model/Geometry.cpp index 0b8ced3886..5c82b9b56b 100644 --- a/OpenSim/Simulation/Model/Geometry.cpp +++ b/OpenSim/Simulation/Model/Geometry.cpp @@ -240,7 +240,7 @@ void Mesh::extendFinalizeFromProperties() { const Model* ownerModel = dynamic_cast(rootModel); //No visualization don't try to load meshes - if (ownerModel->isVisualizationDisabled()) + if (!ownerModel->isMeshVisualizationEnabled()) return; // Current interface to Visualizer calls generateDecorations on every diff --git a/OpenSim/Simulation/Model/Model.h b/OpenSim/Simulation/Model/Model.h index bf713780ce..df3ca0cf92 100644 --- a/OpenSim/Simulation/Model/Model.h +++ b/OpenSim/Simulation/Model/Model.h @@ -1066,12 +1066,12 @@ OpenSim_OBJECT_NONTEMPLATE_DEFS(Model, ModelComponent); /**@}**/ // Turn off visualization/loading of meshes for this model // initSystem must be invoked after this call for it to take effect. - void disableVisualization() { - upd_ModelVisualPreferences().set_no_visualization(true); + void disableMeshVisualization() { + upd_ModelVisualPreferences().set_load_mesh_files(false); } - // Return flag indicating if visualization has been turned off - bool isVisualizationDisabled() const { - return get_ModelVisualPreferences().get_no_visualization(); + // Return flag indicating visualization status for meshes + bool isMeshVisualizationEnabled() const { + return get_ModelVisualPreferences().get_load_mesh_files(); } //-------------------------------------------------------------------------- diff --git a/OpenSim/Simulation/Model/ModelVisualPreferences.h b/OpenSim/Simulation/Model/ModelVisualPreferences.h index ea5f893a11..1084c2b47b 100644 --- a/OpenSim/Simulation/Model/ModelVisualPreferences.h +++ b/OpenSim/Simulation/Model/ModelVisualPreferences.h @@ -54,8 +54,8 @@ class OSIMSIMULATION_API ModelVisualPreferences : public Object { OpenSim_DECLARE_UNNAMED_PROPERTY(ModelDisplayHints, "Model display preferences"); - OpenSim_DECLARE_PROPERTY(no_visualization, bool, - "Flag to indicate whether or not to show geometry from mesh files, default to false."); + OpenSim_DECLARE_PROPERTY(load_mesh_files, bool, + "Flag to indicate whether or not to show geometry from mesh files, default to true."); //-------------------------------------------------------------------------- // CONSTRUCTION @@ -69,7 +69,7 @@ class OSIMSIMULATION_API ModelVisualPreferences : public Object { private: void constructProperties() { constructProperty_ModelDisplayHints(ModelDisplayHints()); - constructProperty_no_visualization(false); + constructProperty_load_mesh_files(true); } //============================================================================= }; // END of class ModelVisualPreferences From 7127e6f8b7b737bcb6a4e8713b7787151a1795e9 Mon Sep 17 00:00:00 2001 From: aymanhab Date: Tue, 16 Jul 2019 15:32:55 -0700 Subject: [PATCH 4/5] Remove property and introduce flag to enable/disable visualization, use it before loading mesh files and test it in testVisualization . --- OpenSim/Common/ModelDisplayHints.h | 1 - OpenSim/Simulation/Model/Geometry.cpp | 2 +- OpenSim/Simulation/Model/Model.h | 20 +++++++++---------- .../Simulation/Model/ModelVisualPreferences.h | 12 ++++++----- OpenSim/Tools/Test/testVisualization.cpp | 7 ++++++- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/OpenSim/Common/ModelDisplayHints.h b/OpenSim/Common/ModelDisplayHints.h index afbc904057..e103bca78b 100644 --- a/OpenSim/Common/ModelDisplayHints.h +++ b/OpenSim/Common/ModelDisplayHints.h @@ -53,7 +53,6 @@ your component produces. The currently-supported flags are: - show frames - show labels - show debug geometry - - no visualization This class is intended to provide some minimal user control over generated geometry in a form that is easy for a ModelComponent author to deal with, since diff --git a/OpenSim/Simulation/Model/Geometry.cpp b/OpenSim/Simulation/Model/Geometry.cpp index 5c82b9b56b..f136aad850 100644 --- a/OpenSim/Simulation/Model/Geometry.cpp +++ b/OpenSim/Simulation/Model/Geometry.cpp @@ -240,7 +240,7 @@ void Mesh::extendFinalizeFromProperties() { const Model* ownerModel = dynamic_cast(rootModel); //No visualization don't try to load meshes - if (!ownerModel->isMeshVisualizationEnabled()) + if (!ownerModel->getUseVisualizer()) return; // Current interface to Visualizer calls generateDecorations on every diff --git a/OpenSim/Simulation/Model/Model.h b/OpenSim/Simulation/Model/Model.h index df3ca0cf92..00bf6a7fc0 100644 --- a/OpenSim/Simulation/Model/Model.h +++ b/OpenSim/Simulation/Model/Model.h @@ -328,7 +328,16 @@ OpenSim_OBJECT_NONTEMPLATE_DEFS(Model, ModelComponent); ModelVisualizer that provides visualization and interaction with the %Model as it is executing. The default is no visualization. @see getModelVisualizer() **/ - void setUseVisualizer(bool visualize) {_useVisualizer=visualize;} + void setUseVisualizer(bool visualize) { + _useVisualizer=visualize; + enableVisualization(visualize); + } + + void enableVisualization(bool enable) { + // Propagate flag so that meshes are loaded (or not) + upd_ModelVisualPreferences().setVisualize(enable); + finalizeFromProperties(); + } /** Return the current setting of the "use visualizer" flag, which will take effect at the next call to initSystem() on this %Model. **/ bool getUseVisualizer() const {return _useVisualizer;} @@ -1064,15 +1073,6 @@ OpenSim_OBJECT_NONTEMPLATE_DEFS(Model, ModelComponent); SimTK::Array_& appendToThis) const override; /**@}**/ - // Turn off visualization/loading of meshes for this model - // initSystem must be invoked after this call for it to take effect. - void disableMeshVisualization() { - upd_ModelVisualPreferences().set_load_mesh_files(false); - } - // Return flag indicating visualization status for meshes - bool isMeshVisualizationEnabled() const { - return get_ModelVisualPreferences().get_load_mesh_files(); - } //-------------------------------------------------------------------------- diff --git a/OpenSim/Simulation/Model/ModelVisualPreferences.h b/OpenSim/Simulation/Model/ModelVisualPreferences.h index 1084c2b47b..9ed6724da2 100644 --- a/OpenSim/Simulation/Model/ModelVisualPreferences.h +++ b/OpenSim/Simulation/Model/ModelVisualPreferences.h @@ -53,10 +53,6 @@ class OSIMSIMULATION_API ModelVisualPreferences : public Object { //========================================================================== OpenSim_DECLARE_UNNAMED_PROPERTY(ModelDisplayHints, "Model display preferences"); - - OpenSim_DECLARE_PROPERTY(load_mesh_files, bool, - "Flag to indicate whether or not to show geometry from mesh files, default to true."); - //-------------------------------------------------------------------------- // CONSTRUCTION //-------------------------------------------------------------------------- @@ -66,11 +62,17 @@ class OSIMSIMULATION_API ModelVisualPreferences : public Object { } virtual ~ModelVisualPreferences() {}; + bool visualize() const { + return _visualize; + } + void setVisualize(bool visualizationStatus) { + _visualize = visualizationStatus; + } private: void constructProperties() { constructProperty_ModelDisplayHints(ModelDisplayHints()); - constructProperty_load_mesh_files(true); } + bool _visualize; //============================================================================= }; // END of class ModelVisualPreferences //============================================================================= diff --git a/OpenSim/Tools/Test/testVisualization.cpp b/OpenSim/Tools/Test/testVisualization.cpp index 4d16832c00..c706ea541f 100644 --- a/OpenSim/Tools/Test/testVisualization.cpp +++ b/OpenSim/Tools/Test/testVisualization.cpp @@ -121,14 +121,16 @@ int main() LoadOpenSimLibrary("osimActuators"); Model testModel("BuiltinGeometry.osim"); + testModel.enableVisualization(true); testVisModel(testModel, "vis_BuiltinGeometry.txt"); std::cout << "BuiltinGeometry Passed" << std::endl; Model testModel2 = createModel4AppearanceTest(); + testModel2.enableVisualization(true); testVisModel(testModel2, "vis_AppearanceTest.txt"); std::cout << "Appearance test Passed" << std::endl; // Load Model in 3.3 format that had transforms attached to Geometry Model testModel3("double_pendulum33.osim"); - + testModel3.enableVisualization(true); SimTK::Array_ standard; testModel3.updDisplayHints().set_show_frames(true); populate_doublePendulumPrimitives(standard); @@ -138,17 +140,20 @@ int main() // Now a model from 3.3 where both GeometrySet and individual DisplayGeometry // have a non-trivial transform. Model composedTransformsModel("doubletransform33.osim"); + composedTransformsModel.enableVisualization(true); composedTransformsModel.updDisplayHints().set_show_frames(true); populate_composedTransformPrimitives(standard); testVisModelAgainstStandard(composedTransformsModel, standard); // Model with contacts Model modelWithContacts("visualize_contacts.osim"); + modelWithContacts.enableVisualization(true); modelWithContacts.updDisplayHints().set_show_frames(true); populate_contactModelPrimitives(standard); testVisModelAgainstStandard(modelWithContacts, standard); // Model with WrapObjects Model modelWithWrap("test_wrapAllVis.osim"); + modelWithWrap.enableVisualization(true); modelWithWrap.updDisplayHints().set_show_frames(true); populate_wrapModelPrimitives(standard); modelWithWrap.updDisplayHints().set_show_frames(false); From f1bba44574a43014c735f46c7b9fc126eb842d9b Mon Sep 17 00:00:00 2001 From: aymanhab Date: Wed, 17 Jul 2019 11:37:07 -0700 Subject: [PATCH 5/5] Key off Model's new isVisualizationEnabled method to load mesh files --- OpenSim/Simulation/Model/Geometry.cpp | 2 +- OpenSim/Simulation/Model/Model.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/OpenSim/Simulation/Model/Geometry.cpp b/OpenSim/Simulation/Model/Geometry.cpp index f136aad850..137546f57b 100644 --- a/OpenSim/Simulation/Model/Geometry.cpp +++ b/OpenSim/Simulation/Model/Geometry.cpp @@ -240,7 +240,7 @@ void Mesh::extendFinalizeFromProperties() { const Model* ownerModel = dynamic_cast(rootModel); //No visualization don't try to load meshes - if (!ownerModel->getUseVisualizer()) + if (!ownerModel->visualizationIsEnabled()) return; // Current interface to Visualizer calls generateDecorations on every diff --git a/OpenSim/Simulation/Model/Model.h b/OpenSim/Simulation/Model/Model.h index 00bf6a7fc0..b468c89d9e 100644 --- a/OpenSim/Simulation/Model/Model.h +++ b/OpenSim/Simulation/Model/Model.h @@ -332,12 +332,16 @@ OpenSim_OBJECT_NONTEMPLATE_DEFS(Model, ModelComponent); _useVisualizer=visualize; enableVisualization(visualize); } - + // Enable visualization overall, loading mesh files etc. void enableVisualization(bool enable) { // Propagate flag so that meshes are loaded (or not) upd_ModelVisualPreferences().setVisualize(enable); finalizeFromProperties(); } + // Return status if visualize flag (different from useVisualizer) + bool visualizationIsEnabled() const { + return get_ModelVisualPreferences().visualize(); + } /** Return the current setting of the "use visualizer" flag, which will take effect at the next call to initSystem() on this %Model. **/ bool getUseVisualizer() const {return _useVisualizer;}