From 6bd8a1a4b6464ac09b670b772a8018abe0414151 Mon Sep 17 00:00:00 2001 From: sreekanth arikatla Date: Tue, 18 Feb 2020 08:33:11 -0500 Subject: [PATCH 1/5] Add multiple scenes example --- Examples/CMakeLists.txt | 4 +- .../DeformableBody/DeformableBodyExample.cpp | 43 +++- Examples/MultipleScenes/CMakeLists.txt | 41 ++++ Examples/MultipleScenes/multipleScenes.cpp | 223 ++++++++++++++++++ .../imstkSimulationManager.cpp | 32 ++- .../imstkSimulationManager.h | 10 + Source/apiUtilities/imstkAPIUtilities.h | 12 + 7 files changed, 346 insertions(+), 19 deletions(-) create mode 100644 Examples/MultipleScenes/CMakeLists.txt create mode 100644 Examples/MultipleScenes/multipleScenes.cpp diff --git a/Examples/CMakeLists.txt b/Examples/CMakeLists.txt index 269b47bb8..665481060 100644 --- a/Examples/CMakeLists.txt +++ b/Examples/CMakeLists.txt @@ -15,6 +15,4 @@ endmacro() listOfSubDir(subDirs ${CMAKE_CURRENT_SOURCE_DIR}) foreach(subdir ${subDirs}) add_subdirectory(${subdir}) -endforeach() - - +endforeach() \ No newline at end of file diff --git a/Examples/DeformableBody/DeformableBodyExample.cpp b/Examples/DeformableBody/DeformableBodyExample.cpp index 364386e16..629764e1d 100644 --- a/Examples/DeformableBody/DeformableBodyExample.cpp +++ b/Examples/DeformableBody/DeformableBodyExample.cpp @@ -43,7 +43,7 @@ int main() { // simManager and Scene - auto simManager = std::make_shared(); + auto simManager = std::make_shared(SimulationManager::Mode::runInBackground); auto scene = simManager->createNewScene("DeformableBodyFEM"); scene->getCamera()->setPosition(0, 2.0, 15.0); @@ -65,20 +65,11 @@ main() } volTetMesh->extractSurfaceMesh(surfMesh, true); - StopWatch wct; - CpuTimer cput; - - wct.start(); - cput.start(); - // Construct a map // Construct one to one nodal map based on the above meshes auto oneToOneNodalMap = std::make_shared(tetMesh, surfMesh); - LOG(INFO) << "wall clock time: " << wct.getTimeElapsed() << " ms."; - LOG(INFO) << "CPU time: " << cput.getTimeElapsed() << " ms."; - // Scene object 1: Dragon // Configure dynamic model @@ -151,10 +142,38 @@ main() light->setIntensity(1); scene->addLight(light); + + auto scene2 = simManager->createNewScene("DeformableBodyFEM2"); + // Run the simulation - simManager->setActiveScene(scene); - simManager->getViewer()->setBackgroundColors(Vec3d(0.3285, 0.3285, 0.6525), Vec3d(0.13836, 0.13836, 0.2748), true); + simManager->setActiveScene(scene2); simManager->startSimulation(); + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + simManager->setActiveScene(scene); + + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + simManager->setActiveScene(scene2); + + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + simManager->setActiveScene(scene); + + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + simManager->endSimulation(); + + getchar(); + return 0; } diff --git a/Examples/MultipleScenes/CMakeLists.txt b/Examples/MultipleScenes/CMakeLists.txt new file mode 100644 index 000000000..a5b1b847b --- /dev/null +++ b/Examples/MultipleScenes/CMakeLists.txt @@ -0,0 +1,41 @@ +########################################################################### +# +# Copyright (c) Kitware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0.txt +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +########################################################################### + +project(Example-MultipleScenes) + +#----------------------------------------------------------------------------- +# Create executable +#----------------------------------------------------------------------------- +imstk_add_executable(${PROJECT_NAME} multipleScenes.cpp) + +#----------------------------------------------------------------------------- +# Add the target to Examples folder +#----------------------------------------------------------------------------- +SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES FOLDER Examples) + +#----------------------------------------------------------------------------- +# Link libraries to executable +#----------------------------------------------------------------------------- +target_link_libraries(${PROJECT_NAME} SimulationManager apiUtilities) + +#----------------------------------------------------------------------------- +# Set MSVC working directory to the install/bin directory +#----------------------------------------------------------------------------- +if(MSVC) # Configure running executable out of MSVC + set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${iMSTK_INSTALL_BIN_DIR}") +endif() diff --git a/Examples/MultipleScenes/multipleScenes.cpp b/Examples/MultipleScenes/multipleScenes.cpp new file mode 100644 index 000000000..3ebe56b1d --- /dev/null +++ b/Examples/MultipleScenes/multipleScenes.cpp @@ -0,0 +1,223 @@ +/*========================================================================= + + Library: iMSTK + + Copyright (c) Kitware, Inc. & Center for Modeling, Simulation, + & Imaging in Medicine, Rensselaer Polytechnic Institute. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0.txt + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +=========================================================================*/ + +#include "imstkSimulationManager.h" +#include "imstkPbdModel.h" +#include "imstkPbdObject.h" +#include "imstkPbdSolver.h" +#include "imstkAPIUtilities.h" +#include "imstkOneToOneMap.h" + +using namespace imstk; + +std::shared_ptr +createClothScene(std::shared_ptr simManager, const char* sceneName, const bool translate=false) +{ + auto scene = simManager->createNewScene(sceneName); + scene->getCamera()->setPosition(0, 2.0, 15.0); + + // Load a sample mesh + auto tetMesh = MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.veg"); + + if (translate) + { + tetMesh->translate(Vec3d(0., 10., 0.), Geometry::TransformType::ApplyToData); + } + + // Extract the surface mesh + auto surfMesh = std::make_shared(); + auto volTetMesh = std::dynamic_pointer_cast(tetMesh); + volTetMesh->extractSurfaceMesh(surfMesh, true); + + auto material = std::make_shared(); + material->setDisplayMode(RenderMaterial::DisplayMode::WIREFRAME_SURFACE); + auto surfMeshModel = std::make_shared(surfMesh); + surfMeshModel->setRenderMaterial(material); + + // Construct one to one nodal map based on the above meshes + auto oneToOneNodalMap = std::make_shared(tetMesh, surfMesh); + + auto deformableObj = std::make_shared("Dragon"); + auto pbdModel = std::make_shared(); + pbdModel->setModelGeometry(volTetMesh); + + // configure model + auto pbdParams = std::make_shared(); + + // FEM constraint + pbdParams->m_YoungModulus = 100.0; + pbdParams->m_PoissonRatio = 0.3; + pbdParams->m_fixedNodeIds = { 51, 127, 178 }; + pbdParams->enableFEMConstraint(PbdConstraint::Type::FEMTet, PbdFEMConstraint::MaterialType::StVK); + + // Other parameters + pbdParams->m_uniformMassValue = 1.0; + pbdParams->m_gravity = Vec3d(0, -9.8, 0); + pbdParams->m_maxIter = 45; + + // Set the parameters + pbdModel->configure(pbdParams); + pbdModel->setTimeStepSizeType(imstk::TimeSteppingType::realTime); + deformableObj->setDynamicalModel(pbdModel); + deformableObj->addVisualModel(surfMeshModel); + deformableObj->setPhysicsGeometry(volTetMesh); + deformableObj->setPhysicsToVisualMap(oneToOneNodalMap); //assign the computed map + + deformableObj->setPbdModel(pbdModel); + auto pbdSolver = std::make_shared(); + pbdSolver->setPbdObject(deformableObj); + scene->addNonlinearSolver(pbdSolver); + + scene->addSceneObject(deformableObj); + + auto planeGeom = std::make_shared(); + planeGeom->setWidth(40); + planeGeom->setTranslation(0, -6, 0); + auto planeObj = std::make_shared("Plane"); + planeObj->setVisualGeometry(planeGeom); + planeObj->setCollidingGeometry(planeGeom); + scene->addSceneObject(planeObj); + + // print UPS + /*auto ups = std::make_shared(); + apiutils::printUPS(simManager->getSceneManager(scene), ups);*/ + + return scene; +} + +void testBackendMode() +{ + auto simManager = std::make_shared(SimulationManager::Mode::runInBackground); + + auto scene1 = createClothScene(simManager, "scene1"); + auto scene2 = createClothScene(simManager, "scene2"); + + // set to scene 1 + simManager->setActiveScene(scene1); + + simManager->initialize(); + + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + // set to scene 2 + simManager->setActiveScene(scene2); + + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + // set to scene 1 + simManager->setActiveScene(scene1); + + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + // set to scene 2 + simManager->setActiveScene(scene2); + + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + simManager->advanceFrame(); + + simManager->endSimulation(); + + std::cout << "Press any key to exit..." << std::endl; + + getchar(); +} + +void testRenderMode() +{ + auto simManager = std::make_shared(SimulationManager::Mode::rendering); + auto scene1 = createClothScene(simManager, "scene1"); + auto scene2 = createClothScene(simManager, "scene2", true); + + // set to scene 1 + simManager->setActiveScene(scene1); + + // Create a call back on key press of 's' to switch scenes + auto viewer = simManager->getViewer(); + if (viewer) + { + std::cout << "Press 's/S' to switch scenes" << std::endl; + + viewer->setOnCharFunction('s', [&](InteractorStyle* c) -> bool + { + if (simManager->getActiveScene() == scene1) + { + simManager->setActiveScene(scene2); + } + else + { + simManager->setActiveScene(scene1); + } + return true; + }); + } + + simManager->startSimulation(); +} + +void testbackgroundMode() +{ + auto simManager = std::make_shared(SimulationManager::Mode::runInBackground); + auto scene1 = createClothScene(simManager, "scene1"); + auto scene2 = createClothScene(simManager, "scene2", true); + + simManager->setActiveScene(scene1); + + std::cout << "Press 's/S' to switch scenes" << std::endl; + auto keyPressFunc = [&]() + { + if (simManager->getActiveScene()->getName() == scene1->getName()) + { + simManager->setActiveScene(scene2); + } + else + { + simManager->setActiveScene(scene1); + } + }; + simManager->setCallbackBgModel(keyPressFunc, 's'); + + simManager->startSimulation(); +} + + +/// +/// \brief Test multiple scenes +/// +int +main() +{ + //testBackendMode(); + //testbackgroundMode(); + testRenderMode(); + + return 0; +} diff --git a/Source/SimulationManager/imstkSimulationManager.cpp b/Source/SimulationManager/imstkSimulationManager.cpp index 2aaa5cace..c2dea37da 100644 --- a/Source/SimulationManager/imstkSimulationManager.cpp +++ b/Source/SimulationManager/imstkSimulationManager.cpp @@ -152,7 +152,7 @@ SimulationManager::createNewScene(const std::string& newSceneName) { m_sceneManagerMap[newScene->getName()] = std::make_shared(newScene); } - else + //else { m_sceneMap[newScene->getName()] = newScene; } @@ -386,8 +386,19 @@ SimulationManager::initialize() return; } + // Initialize all the scenes + for (auto it = m_sceneMap.begin(); it != m_sceneMap.end(); ++it) + { + if (!it->second->initialize()) + { + LOG(WARNING) << "SimulationManager::startSimulation - Unable to initialize the scene - " + << it->second->getName() << std::endl; + return; + } + } + // Initialize the active scene - if (!this->getActiveScene()->isInitialized()) + /*if (!this->getActiveScene()->isInitialized()) { if (!this->getActiveScene()->initialize()) { @@ -395,7 +406,7 @@ SimulationManager::initialize() << this->getActiveScene()->getName() << std::endl; return; } - } + }*/ m_initialized = true; @@ -495,7 +506,7 @@ SimulationManager::infiniteLoopNoRenderingMode() while (this->getStatus() == SimulationStatus::RUNNING || this->getStatus() == SimulationStatus::PAUSED) { - auto c = getchar(); + const auto c = getchar(); if (c == 'e' || c == 'E') { break; @@ -521,6 +532,11 @@ SimulationManager::infiniteLoopNoRenderingMode() continue; } } + + if (c == m_character) + { + m_func(); + } } } @@ -724,4 +740,12 @@ SimulationManager::startModuleInNewThread(std::shared_ptr module) { m_threadMap[module->getName()] = std::thread([module] { module->start(); }); } + +void +SimulationManager::setCallbackBgModel(keyPressCallback func, const int c) +{ + m_func = func; + m_character = c; +} + } // imstk diff --git a/Source/SimulationManager/imstkSimulationManager.h b/Source/SimulationManager/imstkSimulationManager.h index 508be5b60..c8214af67 100644 --- a/Source/SimulationManager/imstkSimulationManager.h +++ b/Source/SimulationManager/imstkSimulationManager.h @@ -51,6 +51,8 @@ using SimulationStatus = ModuleStatus; /// class SimulationManager { + using keyPressCallback = std::function; + public: /// /// \brief Type of the collision detection @@ -222,6 +224,11 @@ public: /// SimulationManager::Mode getMode() const { return m_simulationMode; } + /// + /// \brief Set key press callback to be used in background mode only + /// + void setCallbackBgModel(keyPressCallback func, const int c); + private: /// @@ -262,6 +269,9 @@ private: std::shared_ptr m_viewer = nullptr; std::shared_ptr m_logUtil = std::make_shared(); + keyPressCallback m_func; + int m_character; + bool m_simThreadLaunched = false; Mode m_simulationMode = Mode::rendering; diff --git a/Source/apiUtilities/imstkAPIUtilities.h b/Source/apiUtilities/imstkAPIUtilities.h index 8214cb492..49fb9cae4 100644 --- a/Source/apiUtilities/imstkAPIUtilities.h +++ b/Source/apiUtilities/imstkAPIUtilities.h @@ -229,6 +229,18 @@ createNonLinearSystem(std::shared_ptr dynaModel) void printUPS(std::shared_ptr sceneManager, std::shared_ptr& ups) { + if (!sceneManager) + { + LOG(WARNING) << "APIUtilities::printUPS - scene manager is not valid! Unable to set UPS counter"; + return; + } + + if (!ups) + { + LOG(WARNING) << "APIUtilities::printUPS - UPS Counter is not valid! Unable to set UPS counter"; + return; + } + sceneManager->setPreInitCallback([](Module* module) { LOG(INFO) << "Pre initialization of " << module->getName() << " module"; -- GitLab From 48aa4c376ab121e4afb3b4a99620c780e9b8da68 Mon Sep 17 00:00:00 2001 From: sreekanth arikatla Date: Tue, 18 Feb 2020 21:58:12 -0500 Subject: [PATCH 2/5] ENH: Add simulation config Fix other bugs --- .../DeformableBody/DeformableBodyExample.cpp | 3 +- Examples/MultipleScenes/multipleScenes.cpp | 77 ++-- Examples/PBDCloth/pbdClothExample.cpp | 2 +- Source/Core/imstkLogUtility.cpp | 2 +- .../VTKRenderer/imstkVTKRenderer.cpp | 2 +- Source/Scene/imstkScene.cpp | 1 + Source/Scene/imstkScene.h | 25 +- .../VTKRenderer/imstkVTKViewer.cpp | 10 + .../VTKRenderer/imstkVTKViewer.h | 2 + .../imstkSimulationManager.cpp | 370 +++++++++++------- .../imstkSimulationManager.h | 68 +++- Source/SimulationManager/imstkViewer.cpp | 9 +- Source/SimulationManager/imstkViewer.h | 2 + 13 files changed, 378 insertions(+), 195 deletions(-) diff --git a/Examples/DeformableBody/DeformableBodyExample.cpp b/Examples/DeformableBody/DeformableBodyExample.cpp index 629764e1d..82db67bfd 100644 --- a/Examples/DeformableBody/DeformableBodyExample.cpp +++ b/Examples/DeformableBody/DeformableBodyExample.cpp @@ -142,9 +142,8 @@ main() light->setIntensity(1); scene->addLight(light); - auto scene2 = simManager->createNewScene("DeformableBodyFEM2"); - + // Run the simulation simManager->setActiveScene(scene2); simManager->startSimulation(); diff --git a/Examples/MultipleScenes/multipleScenes.cpp b/Examples/MultipleScenes/multipleScenes.cpp index 3ebe56b1d..8422a20d3 100644 --- a/Examples/MultipleScenes/multipleScenes.cpp +++ b/Examples/MultipleScenes/multipleScenes.cpp @@ -28,10 +28,12 @@ using namespace imstk; -std::shared_ptr -createClothScene(std::shared_ptr simManager, const char* sceneName, const bool translate=false) +std::shared_ptr +createClothScene(std::shared_ptr simManager, const char* sceneName, const bool translate = false) { - auto scene = simManager->createNewScene(sceneName); + auto sceneConfig = std::make_shared(); + sceneConfig->lazyInitialization = true; + auto scene = simManager->createNewScene(sceneName, sceneConfig); scene->getCamera()->setPosition(0, 2.0, 15.0); // Load a sample mesh @@ -43,7 +45,7 @@ createClothScene(std::shared_ptr simManager, const char* scen } // Extract the surface mesh - auto surfMesh = std::make_shared(); + auto surfMesh = std::make_shared(); auto volTetMesh = std::dynamic_pointer_cast(tetMesh); volTetMesh->extractSurfaceMesh(surfMesh, true); @@ -56,7 +58,7 @@ createClothScene(std::shared_ptr simManager, const char* scen auto oneToOneNodalMap = std::make_shared(tetMesh, surfMesh); auto deformableObj = std::make_shared("Dragon"); - auto pbdModel = std::make_shared(); + auto pbdModel = std::make_shared(); pbdModel->setModelGeometry(volTetMesh); // configure model @@ -70,8 +72,8 @@ createClothScene(std::shared_ptr simManager, const char* scen // Other parameters pbdParams->m_uniformMassValue = 1.0; - pbdParams->m_gravity = Vec3d(0, -9.8, 0); - pbdParams->m_maxIter = 45; + pbdParams->m_gravity = Vec3d(0, -9.8, 0); + pbdParams->m_maxIter = 45; // Set the parameters pbdModel->configure(pbdParams); @@ -98,14 +100,16 @@ createClothScene(std::shared_ptr simManager, const char* scen // print UPS /*auto ups = std::make_shared(); - apiutils::printUPS(simManager->getSceneManager(scene), ups);*/ - + ////apiutils::printUPS(simManager->getSceneManager(scene), ups);*/ return scene; } -void testBackendMode() +void +testBackendMode() { - auto simManager = std::make_shared(SimulationManager::Mode::runInBackground); + auto simConfig = std::make_shared(); + simConfig->simulationMode = SimulationManager::Mode::backend; + auto simManager = std::make_shared(simConfig); auto scene1 = createClothScene(simManager, "scene1"); auto scene2 = createClothScene(simManager, "scene2"); @@ -151,11 +155,12 @@ void testBackendMode() getchar(); } -void testRenderMode() +void +testRenderMode() { - auto simManager = std::make_shared(SimulationManager::Mode::rendering); - auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createClothScene(simManager, "scene2", true); + auto simManager = std::make_shared(); + auto scene1 = createClothScene(simManager, "scene1"); + auto scene2 = createClothScene(simManager, "scene2", true); // set to scene 1 simManager->setActiveScene(scene1); @@ -176,39 +181,47 @@ void testRenderMode() { simManager->setActiveScene(scene1); } + + if (!simManager->getActiveScene()) + { + simManager->setActiveScene(scene1); + } + return true; }); } - simManager->startSimulation(); + simManager->startSimulation(SimulationStatus::RUNNING); } -void testbackgroundMode() +void +testbackgroundMode() { - auto simManager = std::make_shared(SimulationManager::Mode::runInBackground); - auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createClothScene(simManager, "scene2", true); + auto simConfig = std::make_shared(); + simConfig->simulationMode = SimulationManager::Mode::runInBackground; + auto simManager = std::make_shared(simConfig); + auto scene1 = createClothScene(simManager, "scene1"); + auto scene2 = createClothScene(simManager, "scene2", true); simManager->setActiveScene(scene1); - + std::cout << "Press 's/S' to switch scenes" << std::endl; auto keyPressFunc = [&]() - { - if (simManager->getActiveScene()->getName() == scene1->getName()) - { - simManager->setActiveScene(scene2); - } - else - { - simManager->setActiveScene(scene1); - } - }; + { + if (simManager->getActiveScene()->getName() == scene1->getName()) + { + simManager->setActiveScene(scene2); + } + else + { + simManager->setActiveScene(scene1); + } + }; simManager->setCallbackBgModel(keyPressFunc, 's'); simManager->startSimulation(); } - /// /// \brief Test multiple scenes /// diff --git a/Examples/PBDCloth/pbdClothExample.cpp b/Examples/PBDCloth/pbdClothExample.cpp index 5d5fe6df5..f17a9e8c8 100644 --- a/Examples/PBDCloth/pbdClothExample.cpp +++ b/Examples/PBDCloth/pbdClothExample.cpp @@ -138,7 +138,7 @@ main() // Start simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::RUNNING); + simManager->startSimulation(SimulationStatus::PAUSED); return 0; } diff --git a/Source/Core/imstkLogUtility.cpp b/Source/Core/imstkLogUtility.cpp index f0c4bbc5a..e62ca262d 100644 --- a/Source/Core/imstkLogUtility.cpp +++ b/Source/Core/imstkLogUtility.cpp @@ -95,7 +95,7 @@ void LogUtility::createLogger(std::string name, std::string path) { m_g3logWorker = g3::LogWorker::createLogWorker(); - m_fileSinkHandle = m_g3logWorker->addDefaultLogger(name, path); + m_fileSinkHandle = m_g3logWorker->addDefaultLogger(name, path, "imstk"); m_stdSinkHandle = m_g3logWorker->addSink( std2::make_unique(), &stdSink::ReceiveLogMessage); g3::initializeLogging(m_g3logWorker.get()); diff --git a/Source/Rendering/VTKRenderer/imstkVTKRenderer.cpp b/Source/Rendering/VTKRenderer/imstkVTKRenderer.cpp index 0c3a9bc35..9c1a7f8f5 100644 --- a/Source/Rendering/VTKRenderer/imstkVTKRenderer.cpp +++ b/Source/Rendering/VTKRenderer/imstkVTKRenderer.cpp @@ -50,8 +50,8 @@ VTKRenderer::VTKRenderer(std::shared_ptr scene, const bool enableVR) { m_vtkRenderer = vtkSmartPointer::New(); } - #endif + this->updateRenderDelegates(); // Initialize textures for surface mesh render delegates diff --git a/Source/Scene/imstkScene.cpp b/Source/Scene/imstkScene.cpp index 72a03ab38..05a8f08ad 100644 --- a/Source/Scene/imstkScene.cpp +++ b/Source/Scene/imstkScene.cpp @@ -56,6 +56,7 @@ Scene::initialize() } } m_isInitialized = true; + LOG(INFO) << "Scene '" << this->getName() << "' initialized!"; return true; } diff --git a/Source/Scene/imstkScene.h b/Source/Scene/imstkScene.h index 97805ba8c..3ab574108 100644 --- a/Source/Scene/imstkScene.h +++ b/Source/Scene/imstkScene.h @@ -47,12 +47,29 @@ template using NamedMap = std::unordered_map>; public: + enum class TimeSteppingPolicy + { + asFastAsPossible, + fixedFrameRate, + realTime + }; + + struct SceneConfig + { + // Initializes the scene only when it needs to frame + // Note: May cause delays to run the first frame of the scene due to scene initialization + bool lazyInitialization = false; + + bool measureFameTimes = false; + + TimeSteppingPolicy timeStepping = TimeSteppingPolicy::asFastAsPossible; + }; /// /// \brief Constructor /// - Scene(const std::string& name) : m_name(name) {} - Scene(std::string&& name) : m_name(std::move(name)) {} + Scene(const std::string& name, std::shared_ptr config = std::make_shared()) : m_name(name), m_config(config) {} + Scene(std::string&& name, std::shared_ptr config = std::make_shared()) : m_name(std::move(name)), m_config(config) {} /// /// \brief Destructor @@ -181,8 +198,10 @@ public: void setFPS(const double fps) { m_fps = fps; } double getFPS() { return m_fps; } -protected: +public: + std::shared_ptr m_config; +protected: std::string m_name; ///> Name of the scene NamedMap m_sceneObjectsMap; NamedMap m_DebugRenderGeometryMap; diff --git a/Source/SimulationManager/VTKRenderer/imstkVTKViewer.cpp b/Source/SimulationManager/VTKRenderer/imstkVTKViewer.cpp index af02fac44..ab225b224 100644 --- a/Source/SimulationManager/VTKRenderer/imstkVTKViewer.cpp +++ b/Source/SimulationManager/VTKRenderer/imstkVTKViewer.cpp @@ -46,6 +46,7 @@ VTKViewer::VTKViewer(SimulationManager* manager /*= nullptr*/, bool enableVR /*= m_vtkRenderWindow = vtkSmartPointer::New(); m_vtkRenderWindow->SetInteractor(vtkInteractor); m_vtkRenderWindow->SetSize(1000, 800); + m_vtkRenderWindow->SetSize(1000, 800); // Screen capture m_screenCapturer = std::make_shared(m_vtkRenderWindow); @@ -207,6 +208,15 @@ VTKViewer::setBackgroundColors(const Vec3d color1, const Vec3d color2 /*= Vec3d: this->getActiveRenderer()->updateBackground(color1, color2, gradientBackground); } +void +VTKViewer::setWindowTitle(const std::string& title) +{ + if (m_vtkRenderWindow) + { + m_vtkRenderWindow->SetWindowName(title.c_str()); + } +} + const std::shared_ptr& VTKViewer::getTextStatusManager() { diff --git a/Source/SimulationManager/VTKRenderer/imstkVTKViewer.h b/Source/SimulationManager/VTKRenderer/imstkVTKViewer.h index 22f87f4e6..4b6cd557c 100644 --- a/Source/SimulationManager/VTKRenderer/imstkVTKViewer.h +++ b/Source/SimulationManager/VTKRenderer/imstkVTKViewer.h @@ -105,6 +105,8 @@ public: virtual void setBackgroundColors(const Vec3d color1, const Vec3d color2 = Vec3d::Zero(), const bool gradientBackground = false) override; + virtual void setWindowTitle(const std::string& title); + /// /// \brief Return the window status handler /// diff --git a/Source/SimulationManager/imstkSimulationManager.cpp b/Source/SimulationManager/imstkSimulationManager.cpp index c2dea37da..987bf06fa 100644 --- a/Source/SimulationManager/imstkSimulationManager.cpp +++ b/Source/SimulationManager/imstkSimulationManager.cpp @@ -28,31 +28,53 @@ namespace imstk { -SimulationManager::SimulationManager(const SimulationManager::Mode mode, const bool enableVR) -{ - m_simulationMode = mode; +//SimulationManager::SimulationManager(const SimulationManager::Mode mode, const bool enableVR) +//{ +// m_config = std::make_shared(); +// m_config->simulationMode = mode; +// m_config->VR_Enabled = enableVR; +// +// // Init g3logger +// m_logUtil->createLogger(m_config->logFilePrefix, m_config->logPath); +// +// if (mode == Mode::rendering) +// { +// createViewer(m_config->VR_Enabled); +// } +//} + +SimulationManager::SimulationManager(const std::shared_ptr config) +{ + m_config = config; // Init g3logger - m_logUtil->createLogger("simulation", "./"); + m_logUtil->createLogger(m_config->logFilePrefix, m_config->logPath); - if (mode == Mode::rendering) + if (m_config->simulationMode == Mode::rendering) { + createViewer(m_config->VR_Enabled); + } +} + +void +SimulationManager::createViewer(const bool enableVR) +{ #ifdef iMSTK_USE_Vulkan - m_viewer = std::make_shared(this, enableVR); + m_viewer = std::make_shared(this, enableVR); #else #ifdef iMSTK_ENABLE_VR - m_viewer = std::make_shared(this, enableVR); + m_viewer = std::make_shared(this, enableVR); #else - if (enableVR) - { - LOG(FATAL) << "Can not run VR simulation without iMSTK_ENABLE_VR"; - } - m_viewer = std::make_shared(this, false); + if (enableVR) + { + LOG(FATAL) << "Can not run VR simulation without iMSTK_ENABLE_VR"; + } + m_viewer = std::make_shared(this, false); + m_viewer->setWindowTitle(m_config->simulationName); #endif #endif - } } void @@ -77,22 +99,22 @@ SimulationManager::setOptimalThreadPoolSize() bool SimulationManager::isSceneRegistered(const std::string& sceneName) const { - if (m_simulationMode != Mode::backend) - { - return m_sceneManagerMap.find(sceneName) != m_sceneManagerMap.end(); - } - else - { - return m_sceneMap.find(sceneName) != m_sceneMap.end(); - } + /* if (m_simulationMode != Mode::backend) + { + return m_sceneManagerMap.find(sceneName) != m_sceneManagerMap.end(); + } + else + {*/ + return m_sceneMap.find(sceneName) != m_sceneMap.end(); + //} } std::shared_ptr SimulationManager::getSceneManager(const std::string& sceneName) const { - if (m_simulationMode == Mode::backend) + if (m_config->simulationMode == Mode::backend) { - LOG(WARNING) << "The simulation manager is in backend mode. No scene managers are created!"; + LOG(INFO) << "The simulation manager is in backend mode. No scene managers were created!"; return nullptr; } @@ -119,7 +141,7 @@ SimulationManager::getSceneManager(std::shared_ptr scene) const std::shared_ptr SimulationManager::getScene(const std::string& sceneName) const { - if (m_simulationMode != Mode::backend) + if (m_config->simulationMode != Mode::backend) { auto sceneManager = this->getSceneManager(sceneName); return sceneManager ? sceneManager->getScene() : nullptr; @@ -137,8 +159,9 @@ SimulationManager::getActiveScene() const } std::shared_ptr -SimulationManager::createNewScene(const std::string& newSceneName) +SimulationManager::createNewScene(const std::string& newSceneName, std::shared_ptr config) { + // check if there is alread a scene by that name if (this->isSceneRegistered(newSceneName)) { LOG(WARNING) << "Can not create new scene: '" << newSceneName @@ -147,31 +170,37 @@ SimulationManager::createNewScene(const std::string& newSceneName) return nullptr; } - auto newScene = std::make_shared(newSceneName); - if (m_simulationMode != Mode::backend) + auto newScene = std::make_shared(newSceneName, config); + + m_mutex.lock(); + m_sceneMap[newScene->getName()] = newScene; + if (m_config->simulationMode != Mode::backend) { m_sceneManagerMap[newScene->getName()] = std::make_shared(newScene); } - //else - { - m_sceneMap[newScene->getName()] = newScene; - } + m_mutex.unlock(); + LOG(INFO) << "New scene added: " << newScene->getName(); + return newScene; } std::shared_ptr SimulationManager::createNewScene() { + m_mutex.lock(); int id = 0; - if (m_simulationMode != Mode::backend) - { - id = (int)m_sceneMap.size() + 1; - } - else + //if (m_simulationMode != Mode::backend) + //{ + id = (int)m_sceneMap.size() + 1; + //} + /*else { id = (int)m_sceneManagerMap.size() + 1; - } + }*/ + + m_mutex.lock(); + std::string newSceneName = "Scene_" + std::to_string(id); return this->createNewScene(newSceneName); @@ -180,7 +209,7 @@ SimulationManager::createNewScene() void SimulationManager::addScene(std::shared_ptr newScene) { - std::string newSceneName = newScene->getName(); + const std::string newSceneName = newScene->getName(); if (this->isSceneRegistered(newSceneName)) { @@ -189,14 +218,19 @@ SimulationManager::addScene(std::shared_ptr newScene) << "Set this scene name to a unique name first"; return; } - if (m_simulationMode != Mode::backend) + + m_mutex.lock(); + + if (m_config->simulationMode != Mode::backend) { m_sceneManagerMap[newSceneName] = std::make_shared(newScene); } - else - { - m_sceneMap[newSceneName] = newScene; - } + //else + //{ + m_sceneMap[newSceneName] = newScene; + //} + + m_mutex.unlock(); LOG(INFO) << "Scene added: " << newSceneName; } @@ -211,16 +245,27 @@ SimulationManager::removeScene(const std::string& sceneName) return; } - if (m_simulationMode != Mode::backend) + // If the scene to be removed is the currently active in the rendering mode, return with warning + if (m_activeSceneName == sceneName && m_config->simulationMode == Mode::rendering) { - m_sceneManagerMap.erase(sceneName); + LOG(WARNING) << "Cannot remove the active scene that is currently rendered"; + return; } - else + + m_mutex.lock(); + + if (m_config->simulationMode != Mode::backend) { - m_sceneMap.erase(sceneName); + m_sceneManagerMap.erase(sceneName); } + //else + //{ + m_sceneMap.erase(sceneName); + //} + + m_mutex.unlock(); - m_sceneManagerMap.erase(sceneName); + //m_sceneManagerMap.erase(sceneName); LOG(INFO) << "Scene removed: " << sceneName; } @@ -255,7 +300,9 @@ SimulationManager::addModule(std::shared_ptr newModule) return; } + m_mutex.lock(); m_modulesMap[newModuleName] = newModule; + m_mutex.unlock(); LOG(INFO) << "Module added: " << newModuleName; } @@ -268,15 +315,16 @@ SimulationManager::removeModule(const std::string& moduleName) << "' was registered in this simulation"; return; } - + m_mutex.lock(); m_modulesMap.erase(moduleName); + m_mutex.unlock(); LOG(INFO) << "Module removed: " << moduleName; } std::shared_ptr SimulationManager::getViewer() const { - if (m_simulationMode != Mode::rendering) + if (m_config->simulationMode != Mode::rendering) { LOG(WARNING) << "The simulation is not in rendering mode!"; } @@ -294,11 +342,11 @@ void SimulationManager::setActiveScene(const std::string& newSceneName, const bool unloadCurrentScene /*= false*/) { - LOG(INFO) << "SimulationManager::setActiveScene - Setting " << newSceneName << " as active"; + LOG(INFO) << "Setting " << newSceneName << " as active"; if (newSceneName == m_activeSceneName) { - LOG(WARNING) << "\tScene '" << newSceneName << "' is already active"; + LOG(INFO) << "Scene '" << newSceneName << "' is already active!"; return; } @@ -309,6 +357,12 @@ SimulationManager::setActiveScene(const std::string& newSceneName, return; } + // Initialize the scene (if not done so already) + if (!newScene->isInitialized()) + { + newScene->initialize(); + } + if (m_viewer) { // Update viewer scene @@ -341,7 +395,7 @@ SimulationManager::setActiveScene(const std::string& newSceneName, } // Stop/Pause running scene - if (m_simulationMode != Mode::backend) + if (m_config->simulationMode != Mode::backend) { auto oldSceneManager = m_sceneManagerMap.at(m_activeSceneName); if (unloadCurrentScene) @@ -372,28 +426,37 @@ SimulationManager::setActiveScene(const std::string& newSceneName, void SimulationManager::initialize() { + if (m_initialized) + { + return; + } + + LOG(INFO) << "Initializing simulation"; + // Do some checks if (m_status == SimulationStatus::RUNNING) { - LOG(WARNING) << "SimulationManager::launchSimulation() - Simulation already running!"; + LOG(WARNING) << "Simulation already running!"; return; } // check if there is an active scene if (!this->getActiveScene()) { - LOG(WARNING) << "SimulationManager::launchSimulation - No valid active scene! Simulation canceled"; - return; + LOG(WARNING) << "No valid active scene! Simulation canceled"; } // Initialize all the scenes for (auto it = m_sceneMap.begin(); it != m_sceneMap.end(); ++it) { - if (!it->second->initialize()) + // do not initialize of not active scene + if (this->getActiveScene() != it->second && !it->second->m_config->lazyInitialization) { - LOG(WARNING) << "SimulationManager::startSimulation - Unable to initialize the scene - " - << it->second->getName() << std::endl; - return; + if (!it->second->initialize()) + { + LOG(WARNING) << "Unable to initialize the scene - " << it->first; + return; + } } } @@ -411,88 +474,94 @@ SimulationManager::initialize() m_initialized = true; // launches modules other than the simulation module at the initialization time in the backend mode - if (m_simulationMode == Mode::backend) + if (m_config->simulationMode == Mode::backend) { this->launchSimulation(); } + + LOG(INFO) << "Initialization Done"; } void SimulationManager::launchSimulation() { - if (!m_initialized) - { - this->initialize(); - } + this->initialize(); + this->startModules(); + m_status = SimulationStatus::RUNNING; + m_simThreadLaunched = true; +} +void +SimulationManager::startModules() +{ // Start modules (except the scene manager module) in separate threads for (const auto& pair : m_modulesMap) { this->startModuleInNewThread(pair.second); } - // Start simulation manager for the active scene in a separate thread - if (m_simulationMode != Mode::backend) - { - // Start scene - this->startModuleInNewThread(m_sceneManagerMap.at(m_activeSceneName)); - } - else + if (m_config->simulationMode == Mode::backend) { return; } - m_status = SimulationStatus::RUNNING; - - m_simThreadLaunched = true; + // Start scene manager in a new thread + this->startModuleInNewThread(m_sceneManagerMap.at(m_activeSceneName)); } void SimulationManager::startSimulation(const SimulationStatus simStatus /*= SimulationStatus::PAUSED*/, const Renderer::Mode renderMode /*= Renderer::Mode::SIMULATION*/) { - if (!m_initialized) - { - this->initialize(); - } + this->initialize(); - if (m_simulationMode == Mode::backend) // returns in backend mode + if (m_config->simulationMode == Mode::backend) // returns in backend mode { - LOG(WARNING) << "SimulationManager::startSimulation() - Simulation manager is in backend mode and hence use advanceFrame to step simulation!"; return; } if (m_status != SimulationStatus::INACTIVE) { - LOG(WARNING) << "Simulation already active"; + LOG(INFO) << "Simulation already active"; return; } - if (m_simulationMode != Mode::backend) + //if (m_simulationMode != Mode::backend) { - auto startingSceneManager = m_sceneManagerMap.at(m_activeSceneName); - if (startingSceneManager->getStatus() != ModuleStatus::INACTIVE) + if (m_activeSceneName != "") { - LOG(WARNING) << "Scene '" << m_activeSceneName << "' is already active"; - return; + auto startingSceneManager = m_sceneManagerMap.at(m_activeSceneName); + if (startingSceneManager->getStatus() != ModuleStatus::INACTIVE) + { + return; + } } } - // Launch simulation right away if the simulator starts in running mode - this->launchSimulation(); + if (simStatus != SimulationStatus::PAUSED) + { + // Launch simulation right away if the simulator starts in running mode + this->launchSimulation(); + } + else + { + m_status = SimulationStatus::PAUSED; + } - if (simStatus == SimulationStatus::PAUSED) + /*if (simStatus == SimulationStatus::PAUSED) { this->pauseSimulation(); - } + }*/ - if (m_simulationMode == Mode::rendering) // never returns + // Note: This never returns until the viewer is terminated + if (m_config->simulationMode == Mode::rendering) { // start the viewer this->startViewer(renderMode); } - if (m_simulationMode == Mode::runInBackground) // never returns + // Note: This never returns until the user triggers exit + if (m_config->simulationMode == Mode::runInBackground) { this->printUserControlsInfo(false); this->infiniteLoopNoRenderingMode(); @@ -533,9 +602,13 @@ SimulationManager::infiniteLoopNoRenderingMode() } } - if (c == m_character) + // conditionally execute callback functions + for (auto list : m_kepPressCallbacks) { - m_func(); + if (c == list.key) + { + list.func(); + } } } } @@ -559,6 +632,7 @@ SimulationManager::startViewer(const Renderer::Mode renderMode /*= Renderer::Mod // End simulation if active when loop exits if (m_status != SimulationStatus::INACTIVE) { + LOG(INFO) << "Ending simulation"; this->endSimulation(); } } @@ -595,34 +669,32 @@ SimulationManager::printUserControlsInfo(const bool isRendering) const void SimulationManager::runSimulation() { - if (m_simulationMode == Mode::backend) + if (m_config->simulationMode == Mode::backend) { - LOG(WARNING) << "SimulationManager::runSimulation() - Simulation cannot be run in backend mode"; + LOG(INFO) << "Simulation cannot be run in backend mode"; return; } if (m_status != SimulationStatus::PAUSED) { - LOG(WARNING) << "SimulationManager::runSimulation() - Simulation is not paused! cannot run (un-pause) simulation"; + LOG(INFO) << "Simulation is not paused! cannot run (un-pause) simulation"; return; } - else - { - LOG(INFO) << "Running simulation"; - } + + LOG(INFO) << "Running simulation"; if (!m_simThreadLaunched) { this->launchSimulation(); } - // Run scene + // Run scene manager m_sceneManagerMap.at(m_activeSceneName)->run(); // Run modules - for (const auto& pair : m_modulesMap) + for (const auto& mod : m_modulesMap) { - (pair.second)->run(); + mod.second->run(); } // Update simulation status @@ -632,26 +704,35 @@ SimulationManager::runSimulation() void SimulationManager::pauseSimulation() { - if (m_simulationMode == Mode::backend) + if (m_config->simulationMode == Mode::backend) { - LOG(WARNING) << "SimulationManager::pauseSimulation() - Simulation manager is in backend mode and hence pause doesn't make sense!"; + LOG(INFO) << "Simulation manager is in backend mode and hence pause doesn't make sense!"; return; } if (m_status != SimulationStatus::RUNNING) { - LOG(WARNING) << "SimulationManager::pauseSimulation(): - Simulation not running, can not pause"; + LOG(WARNING) << "Simulation not running, can not pause"; return; } - else - { - LOG(INFO) << "Pausing simulation"; - } + LOG(INFO) << "Pausing simulation"; m_status = SimulationStatus::PAUSING; - // Pause scene manager module - if (m_simulationMode != Mode::backend) + // Pause scene managers + this->pauseModules(); + + // Update simulation status + m_status = SimulationStatus::PAUSED; + + LOG(INFO) << "Simulation is paused"; +} + +void +SimulationManager::pauseModules() +{ + // Pause scene managers + if (m_config->simulationMode != Mode::backend) { m_sceneManagerMap.at(m_activeSceneName)->pause(); } @@ -661,9 +742,6 @@ SimulationManager::pauseSimulation() { (pair.second)->pause(); } - - // Update simulation status - m_status = SimulationStatus::PAUSED; } void @@ -678,7 +756,7 @@ SimulationManager::resetSimulation() void SimulationManager::endSimulation() { - if ((m_status != SimulationStatus::RUNNING) + /*if ((m_status != SimulationStatus::RUNNING) && (m_status != SimulationStatus::PAUSED)) { LOG(WARNING) << "SimulationManager::endSimulation() - Simulation already terminated!"; @@ -687,7 +765,7 @@ SimulationManager::endSimulation() else { LOG(INFO) << "Ending simulation"; - } + }*/ if (m_viewer) { @@ -695,31 +773,37 @@ SimulationManager::endSimulation() m_viewer->setRenderingMode(Renderer::Mode::DEBUG); } - // End modules + endModules(); + + // Update simulation status + m_status = SimulationStatus::INACTIVE; +} + +void +SimulationManager::endModules() +{ + // End modules other than scene managers for (const auto& pair : m_modulesMap) { (pair.second)->end(); m_threadMap.at(pair.first).join(); } - // End all scenes - if (m_simulationMode != Mode::backend) + // End all scene managers (if any) + /*if (m_simulationMode != Mode::backend) + {*/ + for (auto pair : m_sceneManagerMap) { - for (auto pair : m_sceneManagerMap) - { - std::string sceneName = pair.first; - ModuleStatus sceneStatus = pair.second->getStatus(); + std::string sceneName = pair.first; + ModuleStatus sceneStatus = pair.second->getStatus(); - if (sceneStatus != ModuleStatus::INACTIVE) - { - m_sceneManagerMap.at(sceneName)->end(); - m_threadMap.at(sceneName).join(); - } + if (sceneStatus != ModuleStatus::INACTIVE) + { + m_sceneManagerMap.at(sceneName)->end(); + m_threadMap.at(sceneName).join(); } } - - // Update simulation status - m_status = SimulationStatus::INACTIVE; + //} } void @@ -727,11 +811,15 @@ SimulationManager::advanceFrame() { if (m_initialized) { - this->getActiveScene()->advance(); + auto activeSce = this->getActiveScene(); + if (activeSce) + { + activeSce->advance(); + } } else { - LOG(WARNING) << "SimulationManager::advanceFrame(): - Simulation manager not initialized! call initialize before advancing frame"; + LOG(WARNING) << "Simulation manager not initialized! call initialize before advancing frame"; } } @@ -741,11 +829,9 @@ SimulationManager::startModuleInNewThread(std::shared_ptr module) m_threadMap[module->getName()] = std::thread([module] { module->start(); }); } -void -SimulationManager::setCallbackBgModel(keyPressCallback func, const int c) -{ - m_func = func; - m_character = c; +void +SimulationManager::setCallbackBgModel(keyPressCallback func, const int c) +{ + m_kepPressCallbacks.push_back(callbackKeyPair { c, func }); } - } // imstk diff --git a/Source/SimulationManager/imstkSimulationManager.h b/Source/SimulationManager/imstkSimulationManager.h index c8214af67..406b5c5d9 100644 --- a/Source/SimulationManager/imstkSimulationManager.h +++ b/Source/SimulationManager/imstkSimulationManager.h @@ -51,7 +51,7 @@ using SimulationStatus = ModuleStatus; /// class SimulationManager { - using keyPressCallback = std::function; +using keyPressCallback = std::function; public: /// @@ -72,10 +72,30 @@ public: backend }; + struct simManagerConfig + { + // logger + std::string logPath = "./"; + std::string logFilePrefix = "simulation"; + + // Name + std::string simulationName = "imstk"; + + // states + Mode simulationMode = Mode::rendering; + SimulationStatus startingStatus = SimulationStatus::RUNNING; + bool VR_Enabled = false; + bool startInPausedState = false; + + // 0 indicates that an optimal size will be used + unsigned int threadPoolSize = 0; + }; + /// /// \brief Constructor /// - SimulationManager(const SimulationManager::Mode mode = Mode::rendering, const bool enableVR = false); + //SimulationManager(const SimulationManager::Mode mode = Mode::rendering, const bool enableVR = false); + SimulationManager(const std::shared_ptr config = std::make_shared()); /// /// \brief Default destructor @@ -126,10 +146,11 @@ public: /// /// \brief Create a new scene with a given name /// - std::shared_ptr createNewScene(const std::string& newSceneName); + std::shared_ptr createNewScene(const std::string& newSceneName, std::shared_ptr config = std::make_shared()); /// - /// \brief Create a new scene with default name + /// \brief Create a new scene + /// \note Scene name is automatically assigmed /// std::shared_ptr createNewScene(); @@ -222,13 +243,19 @@ public: /// /// \brief Return the mode of the simulation manager /// - SimulationManager::Mode getMode() const { return m_simulationMode; } + SimulationManager::Mode getMode() const { return m_config->simulationMode; } /// /// \brief Set key press callback to be used in background mode only /// void setCallbackBgModel(keyPressCallback func, const int c); +protected: + /// + /// \brief Create a viewer + /// + void createViewer(const bool enableVR); + private: /// @@ -250,14 +277,27 @@ private: void startModuleInNewThread(std::shared_ptr module); + /// + /// \brief Start modules + /// + void startModules(); + + /// + /// \brief Pause modules + /// + void pauseModules(); + + /// + /// \brief End modules + /// + void endModules(); + /// /// \brief Keeps things in an infinite loop if rendering is disabled /// The same keys can be used to trigger PAUSE, RUNNING, RESET of the simulation /// void infiniteLoopNoRenderingMode(); - SimulationStatus m_status = SimulationStatus::INACTIVE; - std::string m_activeSceneName = ""; std::unordered_map> m_sceneManagerMap; std::unordered_map> m_sceneMap; // used in backend mode where m_sceneManagerMap is not used @@ -269,12 +309,16 @@ private: std::shared_ptr m_viewer = nullptr; std::shared_ptr m_logUtil = std::make_shared(); - keyPressCallback m_func; - int m_character; + struct callbackKeyPair { int key; keyPressCallback func; }; + std::vector m_kepPressCallbacks; + + // states + SimulationStatus m_status = SimulationStatus::INACTIVE; + bool m_simThreadLaunched = false; + bool m_initialized = false; - bool m_simThreadLaunched = false; + std::shared_ptr m_config; - Mode m_simulationMode = Mode::rendering; - bool m_initialized = false; + std::mutex m_mutex; }; } // imstk diff --git a/Source/SimulationManager/imstkViewer.cpp b/Source/SimulationManager/imstkViewer.cpp index 427f8e59e..18c7be272 100644 --- a/Source/SimulationManager/imstkViewer.cpp +++ b/Source/SimulationManager/imstkViewer.cpp @@ -32,7 +32,14 @@ Viewer::getActiveScene() const const std::shared_ptr& Viewer::getActiveRenderer() const { - return m_rendererMap.at(m_activeScene); + if (m_activeScene) + { + return m_rendererMap.at(m_activeScene); + } + else + { + return nullptr; + } } const bool& diff --git a/Source/SimulationManager/imstkViewer.h b/Source/SimulationManager/imstkViewer.h index c1b20c3d9..cc3eeccc9 100644 --- a/Source/SimulationManager/imstkViewer.h +++ b/Source/SimulationManager/imstkViewer.h @@ -87,6 +87,8 @@ public: /// const std::shared_ptr& getActiveRenderer() const; + virtual void setWindowTitle(const std::string& title) = 0; + /// /// \brief access screen shot utility /// -- GitLab From 923f8844525c01cd51b3ce73de1515bdadf5a357 Mon Sep 17 00:00:00 2001 From: sreekanth-arikatla Date: Mon, 24 Feb 2020 18:31:58 -0500 Subject: [PATCH 3/5] ENH: Add frame rate counter to scene --- .../DeformableBody/DeformableBodyExample.cpp | 4 +- Examples/MultipleScenes/multipleScenes.cpp | 112 ++++++++++++++++-- Examples/NoRendering/noRenderingExample.cpp | 4 +- Examples/PBDCloth/pbdClothExample.cpp | 3 +- Source/Core/imstkModule.cpp | 26 ++++ Source/Core/imstkModule.h | 29 ++++- Source/Scene/imstkScene.cpp | 1 + Source/Scene/imstkScene.h | 2 + .../imstkSimulationManager.cpp | 12 +- .../imstkSimulationManager.h | 4 +- 10 files changed, 177 insertions(+), 20 deletions(-) diff --git a/Examples/DeformableBody/DeformableBodyExample.cpp b/Examples/DeformableBody/DeformableBodyExample.cpp index 82db67bfd..b93207da0 100644 --- a/Examples/DeformableBody/DeformableBodyExample.cpp +++ b/Examples/DeformableBody/DeformableBodyExample.cpp @@ -43,7 +43,9 @@ int main() { // simManager and Scene - auto simManager = std::make_shared(SimulationManager::Mode::runInBackground); + auto simConfig = std::make_shared(); + simConfig->simulationMode = SimulationManager::Mode::runInBackground; + auto simManager = std::make_shared(simConfig); auto scene = simManager->createNewScene("DeformableBodyFEM"); scene->getCamera()->setPosition(0, 2.0, 15.0); diff --git a/Examples/MultipleScenes/multipleScenes.cpp b/Examples/MultipleScenes/multipleScenes.cpp index 8422a20d3..88de0d1bc 100644 --- a/Examples/MultipleScenes/multipleScenes.cpp +++ b/Examples/MultipleScenes/multipleScenes.cpp @@ -29,7 +29,7 @@ using namespace imstk; std::shared_ptr -createClothScene(std::shared_ptr simManager, const char* sceneName, const bool translate = false) +createSoftBodyScene(std::shared_ptr simManager, const char* sceneName) { auto sceneConfig = std::make_shared(); sceneConfig->lazyInitialization = true; @@ -39,11 +39,6 @@ createClothScene(std::shared_ptr simManager, const char* scen // Load a sample mesh auto tetMesh = MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.veg"); - if (translate) - { - tetMesh->translate(Vec3d(0., 10., 0.), Geometry::TransformType::ApplyToData); - } - // Extract the surface mesh auto surfMesh = std::make_shared(); auto volTetMesh = std::dynamic_pointer_cast(tetMesh); @@ -104,6 +99,99 @@ createClothScene(std::shared_ptr simManager, const char* scen return scene; } +std::shared_ptr +createClothScene(std::shared_ptr simManager, const char* sceneName) +{ + auto sceneConfig = std::make_shared(); + sceneConfig->lazyInitialization = true; + auto scene = simManager->createNewScene(sceneName, sceneConfig); + + // Create surface mesh + auto surfMesh = std::make_shared(); + StdVectorOfVec3d vertList; + const double width = 10.0; + const double height = 10.0; + const int nRows = 11; + const int nCols = 11; + vertList.resize(nRows * nCols); + const double dy = width / (double)(nCols - 1); + const double dx = height / (double)(nRows - 1); + for (int i = 0; i < nRows; ++i) + { + for (int j = 0; j < nCols; j++) + { + vertList[i * nCols + j] = Vec3d((double)dx * i, 1.0, (double)dy * j); + } + } + surfMesh->setInitialVertexPositions(vertList); + surfMesh->setVertexPositions(vertList); + + // Add connectivity data + std::vector triangles; + for (std::size_t i = 0; i < nRows - 1; ++i) + { + for (std::size_t j = 0; j < nCols - 1; j++) + { + SurfaceMesh::TriangleArray tri[2]; + tri[0] = { { i * nCols + j, (i + 1) * nCols + j, i * nCols + j + 1 } }; + tri[1] = { { (i + 1) * nCols + j + 1, i * nCols + j + 1, (i + 1) * nCols + j } }; + triangles.push_back(tri[0]); + triangles.push_back(tri[1]); + } + } + + surfMesh->setTrianglesVertices(triangles); + + // Create Object & Model + auto deformableObj = std::make_shared("Cloth"); + auto pbdModel = std::make_shared(); + pbdModel->setModelGeometry(surfMesh); + + // configure model + auto pbdParams = std::make_shared(); + + // Constraints + pbdParams->enableConstraint(PbdConstraint::Type::Distance, 0.1); + pbdParams->enableConstraint(PbdConstraint::Type::Dihedral, 0.001); + std::vector fixedNodes(nCols); + for (size_t i = 0; i < fixedNodes.size(); i++) + { + fixedNodes[i] = i; + } + pbdParams->m_fixedNodeIds = fixedNodes; + + // Other parameters + pbdParams->m_uniformMassValue = 1.0; + pbdParams->m_gravity = Vec3d(0, -9.8, 0); + pbdParams->m_dt = 0.03; + pbdParams->m_maxIter = 5; + + // Set the parameters + pbdModel->configure(pbdParams); + deformableObj->setDynamicalModel(pbdModel); + deformableObj->setPhysicsGeometry(surfMesh); + + auto material = std::make_shared(); + material->setBackFaceCulling(false); + material->setColor(Color::LightGray); + material->setDisplayMode(RenderMaterial::DisplayMode::WIREFRAME_SURFACE); + auto surfMeshModel = std::make_shared(surfMesh); + surfMeshModel->setRenderMaterial(material); + deformableObj->addVisualModel(surfMeshModel); + + // Solver + auto pbdSolver = std::make_shared(); + pbdSolver->setPbdObject(deformableObj); + scene->addNonlinearSolver(pbdSolver); + + scene->addSceneObject(deformableObj); + + scene->getCamera()->setFocalPoint(0, -5, 5); + scene->getCamera()->setPosition(-15., -5.0, 15.0); + + return scene; +} + void testBackendMode() { @@ -112,7 +200,7 @@ testBackendMode() auto simManager = std::make_shared(simConfig); auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createClothScene(simManager, "scene2"); + auto scene2 = createSoftBodyScene(simManager, "scene2"); // set to scene 1 simManager->setActiveScene(scene1); @@ -159,8 +247,8 @@ void testRenderMode() { auto simManager = std::make_shared(); - auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createClothScene(simManager, "scene2", true); + auto scene1 = createClothScene(simManager, "scene1"); + auto scene2 = createSoftBodyScene(simManager, "scene2"); // set to scene 1 simManager->setActiveScene(scene1); @@ -201,7 +289,7 @@ testbackgroundMode() simConfig->simulationMode = SimulationManager::Mode::runInBackground; auto simManager = std::make_shared(simConfig); auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createClothScene(simManager, "scene2", true); + auto scene2 = createSoftBodyScene(simManager, "scene2"); simManager->setActiveScene(scene1); @@ -228,8 +316,8 @@ testbackgroundMode() int main() { - //testBackendMode(); - //testbackgroundMode(); + testBackendMode(); + testbackgroundMode(); testRenderMode(); return 0; diff --git a/Examples/NoRendering/noRenderingExample.cpp b/Examples/NoRendering/noRenderingExample.cpp index f1392d5c4..d11d13b03 100644 --- a/Examples/NoRendering/noRenderingExample.cpp +++ b/Examples/NoRendering/noRenderingExample.cpp @@ -40,7 +40,9 @@ using namespace imstk; int main() { - auto simManager = std::make_shared(SimulationManager::Mode::runInBackground); + auto simConfig = std::make_shared(); + simConfig->simulationMode = SimulationManager::Mode::runInBackground; + auto simManager = std::make_shared(simConfig); auto scene = simManager->createNewScene("NoRendering"); // Create surface mesh diff --git a/Examples/PBDCloth/pbdClothExample.cpp b/Examples/PBDCloth/pbdClothExample.cpp index f17a9e8c8..52283eaf8 100644 --- a/Examples/PBDCloth/pbdClothExample.cpp +++ b/Examples/PBDCloth/pbdClothExample.cpp @@ -134,7 +134,8 @@ main() scene->addSceneObject(deformableObj); scene->getCamera()->setFocalPoint(0, -5, 5); - scene->getCamera()->setPosition(-15., -5.0, 15.0); + scene->getCamera()->setPosition(-15., -5.0, 15.0); + //scene->m_config->trackFPS = true; // Start simManager->setActiveScene(scene); diff --git a/Source/Core/imstkModule.cpp b/Source/Core/imstkModule.cpp index 946fc698a..c7dbd06b0 100644 --- a/Source/Core/imstkModule.cpp +++ b/Source/Core/imstkModule.cpp @@ -67,6 +67,11 @@ Module::start() // (updating as fast as possible) if (m_loopDelay < VERY_SMALL_EPSILON_D) { + if (m_trackFPS) + { + m_frameCounter->setStartPointOfUpdate(); + } + if (m_preUpdateCallback) { m_preUpdateCallback(this); @@ -76,6 +81,14 @@ Module::start() { m_postUpdateCallback(this); } + + if (m_trackFPS) + { + m_frameCounter->setEndPointOfUpdate(); + + std::cout << "\r" << this->getName() << " running at " + << m_frameCounter->getUPS() << " ups " << std::flush; + } continue; } @@ -84,6 +97,11 @@ Module::start() elapsed = std::chrono::duration_cast(current_t - previous_t).count(); if (elapsed >= m_loopDelay) { + if (m_trackFPS) + { + m_frameCounter->setStartPointOfUpdate(); + } + if (m_preUpdateCallback) { m_preUpdateCallback(this); @@ -93,6 +111,14 @@ Module::start() { m_postUpdateCallback(this); } + + if (m_trackFPS) + { + m_frameCounter->setEndPointOfUpdate(); + } + std::cout << "\r" << this->getName() << " running at " + << m_frameCounter->getUPS() << " ups " << std::flush; + previous_t = current_t; } } diff --git a/Source/Core/imstkModule.h b/Source/Core/imstkModule.h index 1542f7c9c..c320cf10b 100644 --- a/Source/Core/imstkModule.h +++ b/Source/Core/imstkModule.h @@ -24,6 +24,7 @@ #include #include #include +#include "imstkTimer.h" namespace imstk { @@ -56,7 +57,9 @@ public: Module(std::string name, int loopDelay = 0) : m_name(name), m_loopDelay(loopDelay) - {} + { + m_frameCounter = std::make_shared(); + } /// /// \brief Destructor @@ -120,6 +123,25 @@ public: /// void setFrequency(const double f); + + void enableFrameCount() { m_trackFPS = true; }; + void disableFrameCount() { m_trackFPS = false; }; + bool isFrameCountEnabled() const {return m_trackFPS;}; + + + unsigned int getUPS() + { + if (m_trackFPS) + { + return m_frameCounter->getUPS(); + } + else + { + LOG(WARNING) << "Frame counter is not enabled!"; + return 0; + } + }; + protected: /// @@ -144,7 +166,10 @@ protected: CallbackFunction m_preCleanUpCallback; ///> function callback preceding module cleanup CallbackFunction m_postCleanUpCallback; ///> function callback following module cleanup - std::atomic m_status { ModuleStatus::INACTIVE }; ///> Module status + std::atomic m_status{ ModuleStatus::INACTIVE }; ///> Module status + + bool m_trackFPS = false; + std::shared_ptr m_frameCounter; std::string m_name; ///> Name of the module double m_loopDelay = 0; ///> Loop delay diff --git a/Source/Scene/imstkScene.cpp b/Source/Scene/imstkScene.cpp index 05a8f08ad..a1b54b441 100644 --- a/Source/Scene/imstkScene.cpp +++ b/Source/Scene/imstkScene.cpp @@ -55,6 +55,7 @@ Scene::initialize() return false; } } + m_isInitialized = true; LOG(INFO) << "Scene '" << this->getName() << "' initialized!"; return true; diff --git a/Source/Scene/imstkScene.h b/Source/Scene/imstkScene.h index 3ab574108..a30740d66 100644 --- a/Source/Scene/imstkScene.h +++ b/Source/Scene/imstkScene.h @@ -63,6 +63,8 @@ public: bool measureFameTimes = false; TimeSteppingPolicy timeStepping = TimeSteppingPolicy::asFastAsPossible; + + bool trackFPS = false; }; /// diff --git a/Source/SimulationManager/imstkSimulationManager.cpp b/Source/SimulationManager/imstkSimulationManager.cpp index 987bf06fa..6089930e7 100644 --- a/Source/SimulationManager/imstkSimulationManager.cpp +++ b/Source/SimulationManager/imstkSimulationManager.cpp @@ -410,7 +410,12 @@ SimulationManager::setActiveScene(const std::string& newSceneName, } // Start/Run new scene - auto newSceneManager = m_sceneManagerMap.at(newSceneName); + auto newSceneManager = m_sceneManagerMap.at(newSceneName + std::string("_sceneManager")); + if (newScene->m_config->trackFPS) + { + newSceneManager->enableFrameCount(); + } + if (newSceneManager->getStatus() == ModuleStatus::INACTIVE) { this->startModuleInNewThread(newSceneManager); @@ -506,6 +511,11 @@ SimulationManager::startModules() } // Start scene manager in a new thread + if (m_sceneMap.at(m_activeSceneName)->m_config->trackFPS) + { + m_sceneManagerMap.at(m_activeSceneName)->enableFrameCount(); + } + this->startModuleInNewThread(m_sceneManagerMap.at(m_activeSceneName)); } diff --git a/Source/SimulationManager/imstkSimulationManager.h b/Source/SimulationManager/imstkSimulationManager.h index 406b5c5d9..b0145e188 100644 --- a/Source/SimulationManager/imstkSimulationManager.h +++ b/Source/SimulationManager/imstkSimulationManager.h @@ -150,7 +150,7 @@ public: /// /// \brief Create a new scene - /// \note Scene name is automatically assigmed + /// \note Scene name is automatically assigned /// std::shared_ptr createNewScene(); @@ -216,7 +216,7 @@ public: /// /// \brief Run the simulation from a paused state - /// In Mode::backend mode, the simulation manager is initialized if not and retrned + /// In Mode::backend mode, the simulation manager is initialized if not and returned /// void runSimulation(); -- GitLab From fd9aaa16212761fbb90adb65901a13a8d8c99898 Mon Sep 17 00:00:00 2001 From: sreekanth-arikatla Date: Tue, 25 Feb 2020 15:24:10 -0500 Subject: [PATCH 4/5] tmp --- .../ManualCDWithOctreeExample.cpp | 2 +- .../DebugRendering/DebugRenderingExample.cpp | 2 +- .../DeformableBody/DeformableBodyExample.cpp | 8 +- .../GeometryTransformsExample.cpp | 2 +- Examples/LineMesh/LineMeshExample.cpp | 2 +- Examples/MeshIO/MeshIOExample.cpp | 2 +- Examples/MshVegaIO/MshVegaIOExample.cpp | 2 +- Examples/MultipleScenes/multipleScenes.cpp | 59 ++++--- Examples/NoRendering/noRenderingExample.cpp | 8 +- .../ObjectCtrlDummyClientExample.cpp | 2 +- Examples/Octree/OctreeExample.cpp | 2 +- Examples/PBDCloth/pbdClothExample.cpp | 5 +- .../PBDCollisionManyDragonsExample.cpp | 2 +- .../PBDCollisionOneDragonExample.cpp | 2 +- Examples/PBDFluids/PBDFluidsExample.cpp | 6 +- Examples/PBDString/pbdStringExample.cpp | 2 +- Examples/PBDVolume/PBDVolumeExample.cpp | 2 +- Examples/Rendering/RenderingExample.cpp | 2 +- .../RigidBodyDynamicsExample.cpp | 2 +- Examples/SPHFluid/SPHFluidExample.hpp | 2 +- .../SceneManagementExample.cpp | 14 +- Examples/Screenshot/ScreenshotExample.cpp | 2 +- Examples/Viewer/ViewerExample.cpp | 2 +- .../VolumeRenderingExample.cpp | 2 +- Source/Core/imstkModule.cpp | 30 +++- Source/Core/imstkModule.h | 31 +--- Source/Scene/imstkScene.h | 12 +- .../VTKRenderer/imstkOpenVRCommand.cpp | 14 +- .../VTKRenderer/imstkVTKInteractorStyle.cpp | 8 +- .../imstkSimulationManager.cpp | 158 +++++++----------- .../imstkSimulationManager.h | 40 ++--- Source/apiUtilities/imstkAPIUtilities.h | 30 +--- 32 files changed, 199 insertions(+), 260 deletions(-) diff --git a/Examples/CollisionDetection/ManualCDWithOctree/ManualCDWithOctreeExample.cpp b/Examples/CollisionDetection/ManualCDWithOctree/ManualCDWithOctreeExample.cpp index acf807a6c..3c16d6760 100644 --- a/Examples/CollisionDetection/ManualCDWithOctree/ManualCDWithOctreeExample.cpp +++ b/Examples/CollisionDetection/ManualCDWithOctree/ManualCDWithOctreeExample.cpp @@ -469,7 +469,7 @@ main() } // Run - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/DebugRendering/DebugRenderingExample.cpp b/Examples/DebugRendering/DebugRenderingExample.cpp index 36cbedf74..f44f3769c 100644 --- a/Examples/DebugRendering/DebugRenderingExample.cpp +++ b/Examples/DebugRendering/DebugRenderingExample.cpp @@ -182,7 +182,7 @@ main() } // Run - simManager->startSimulation(SimulationStatus::RUNNING); + simManager->start(SimulationStatus::RUNNING); return 0; } diff --git a/Examples/DeformableBody/DeformableBodyExample.cpp b/Examples/DeformableBody/DeformableBodyExample.cpp index b93207da0..e04f64f34 100644 --- a/Examples/DeformableBody/DeformableBodyExample.cpp +++ b/Examples/DeformableBody/DeformableBodyExample.cpp @@ -134,10 +134,6 @@ main() nlSolver->setSystem(nlSystem); scene->addNonlinearSolver(nlSolver); - // print UPS - auto ups = std::make_shared(); - apiutils::printUPS(simManager->getSceneManager(scene), ups); - // Light auto light = std::make_shared("light"); light->setFocalPoint(Vec3d(5, -8, -5)); @@ -148,7 +144,7 @@ main() // Run the simulation simManager->setActiveScene(scene2); - simManager->startSimulation(); + simManager->start(); simManager->advanceFrame(); simManager->advanceFrame(); @@ -172,7 +168,7 @@ main() simManager->advanceFrame(); simManager->advanceFrame(); - simManager->endSimulation(); + simManager->end(); getchar(); diff --git a/Examples/GeometryTransforms/GeometryTransformsExample.cpp b/Examples/GeometryTransforms/GeometryTransformsExample.cpp index a037d0191..439187736 100644 --- a/Examples/GeometryTransforms/GeometryTransformsExample.cpp +++ b/Examples/GeometryTransforms/GeometryTransformsExample.cpp @@ -101,7 +101,7 @@ main() // Run simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::RUNNING); + simManager->start(SimulationStatus::RUNNING); return 0; } diff --git a/Examples/LineMesh/LineMeshExample.cpp b/Examples/LineMesh/LineMeshExample.cpp index 4987afa5a..a09ff7ad5 100644 --- a/Examples/LineMesh/LineMeshExample.cpp +++ b/Examples/LineMesh/LineMeshExample.cpp @@ -133,5 +133,5 @@ main() // Start simulation simManager->setActiveScene(scene); - simManager->startSimulation(); + simManager->start(); } diff --git a/Examples/MeshIO/MeshIOExample.cpp b/Examples/MeshIO/MeshIOExample.cpp index 9c975d95f..36bd2524d 100644 --- a/Examples/MeshIO/MeshIOExample.cpp +++ b/Examples/MeshIO/MeshIOExample.cpp @@ -65,7 +65,7 @@ main() // Run simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/MshVegaIO/MshVegaIOExample.cpp b/Examples/MshVegaIO/MshVegaIOExample.cpp index 1798cd2e5..4d9e4d1e3 100644 --- a/Examples/MshVegaIO/MshVegaIOExample.cpp +++ b/Examples/MshVegaIO/MshVegaIOExample.cpp @@ -85,7 +85,7 @@ main() // Run simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/MultipleScenes/multipleScenes.cpp b/Examples/MultipleScenes/multipleScenes.cpp index 88de0d1bc..e4e4bbb21 100644 --- a/Examples/MultipleScenes/multipleScenes.cpp +++ b/Examples/MultipleScenes/multipleScenes.cpp @@ -93,9 +93,6 @@ createSoftBodyScene(std::shared_ptr simManager, const char* s planeObj->setCollidingGeometry(planeGeom); scene->addSceneObject(planeObj); - // print UPS - /*auto ups = std::make_shared(); - ////apiutils::printUPS(simManager->getSceneManager(scene), ups);*/ return scene; } @@ -109,10 +106,10 @@ createClothScene(std::shared_ptr simManager, const char* scen // Create surface mesh auto surfMesh = std::make_shared(); StdVectorOfVec3d vertList; - const double width = 10.0; + const double width = 10.0; const double height = 10.0; - const int nRows = 11; - const int nCols = 11; + const int nRows = 11; + const int nCols = 11; vertList.resize(nRows * nCols); const double dy = width / (double)(nCols - 1); const double dx = height / (double)(nRows - 1); @@ -133,7 +130,7 @@ createClothScene(std::shared_ptr simManager, const char* scen for (std::size_t j = 0; j < nCols - 1; j++) { SurfaceMesh::TriangleArray tri[2]; - tri[0] = { { i * nCols + j, (i + 1) * nCols + j, i * nCols + j + 1 } }; + tri[0] = { { i* nCols + j, (i + 1) * nCols + j, i * nCols + j + 1 } }; tri[1] = { { (i + 1) * nCols + j + 1, i * nCols + j + 1, (i + 1) * nCols + j } }; triangles.push_back(tri[0]); triangles.push_back(tri[1]); @@ -144,7 +141,7 @@ createClothScene(std::shared_ptr simManager, const char* scen // Create Object & Model auto deformableObj = std::make_shared("Cloth"); - auto pbdModel = std::make_shared(); + auto pbdModel = std::make_shared(); pbdModel->setModelGeometry(surfMesh); // configure model @@ -162,9 +159,9 @@ createClothScene(std::shared_ptr simManager, const char* scen // Other parameters pbdParams->m_uniformMassValue = 1.0; - pbdParams->m_gravity = Vec3d(0, -9.8, 0); - pbdParams->m_dt = 0.03; - pbdParams->m_maxIter = 5; + pbdParams->m_gravity = Vec3d(0, -9.8, 0); + pbdParams->m_dt = 0.03; + pbdParams->m_maxIter = 5; // Set the parameters pbdModel->configure(pbdParams); @@ -193,14 +190,14 @@ createClothScene(std::shared_ptr simManager, const char* scen } void -testBackendMode() +testMultipleScenesInBackendMode() { auto simConfig = std::make_shared(); simConfig->simulationMode = SimulationManager::Mode::backend; auto simManager = std::make_shared(simConfig); - auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createSoftBodyScene(simManager, "scene2"); + auto scene1 = createClothScene(simManager, "clothScene"); + auto scene2 = createSoftBodyScene(simManager, "deformableBodyScene"); // set to scene 1 simManager->setActiveScene(scene1); @@ -236,7 +233,7 @@ testBackendMode() simManager->advanceFrame(); simManager->advanceFrame(); - simManager->endSimulation(); + simManager->end(); std::cout << "Press any key to exit..." << std::endl; @@ -244,11 +241,14 @@ testBackendMode() } void -testRenderMode() +testMultipleScenesInRenderMode() { + // Simulation manager defaults to rendering mode auto simManager = std::make_shared(); - auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createSoftBodyScene(simManager, "scene2"); + auto scene1 = createClothScene(simManager, "clothScene"); + auto scene2 = createSoftBodyScene(simManager, "deformableBodyScene"); + + scene1->getConfig()->trackFPS = true; // set to scene 1 simManager->setActiveScene(scene1); @@ -279,17 +279,22 @@ testRenderMode() }); } - simManager->startSimulation(SimulationStatus::RUNNING); + if (scene1->getConfig()->trackFPS) + { + apiutils::printUPS(simManager->getSceneManager(scene1)); + } + + simManager->start(SimulationStatus::PAUSED); } void -testbackgroundMode() +testMultipleScenesInBackgroundMode() { auto simConfig = std::make_shared(); simConfig->simulationMode = SimulationManager::Mode::runInBackground; auto simManager = std::make_shared(simConfig); - auto scene1 = createClothScene(simManager, "scene1"); - auto scene2 = createSoftBodyScene(simManager, "scene2"); + auto scene1 = createClothScene(simManager, "clothScene"); + auto scene2 = createSoftBodyScene(simManager, "deformableBodyScene"); simManager->setActiveScene(scene1); @@ -305,9 +310,9 @@ testbackgroundMode() simManager->setActiveScene(scene1); } }; - simManager->setCallbackBgModel(keyPressFunc, 's'); + simManager->addKeyPressCallback(keyPressFunc, 's'); - simManager->startSimulation(); + simManager->start(); } /// @@ -316,9 +321,9 @@ testbackgroundMode() int main() { - testBackendMode(); - testbackgroundMode(); - testRenderMode(); + testMultipleScenesInBackendMode(); + testMultipleScenesInBackgroundMode(); + testMultipleScenesInRenderMode(); return 0; } diff --git a/Examples/NoRendering/noRenderingExample.cpp b/Examples/NoRendering/noRenderingExample.cpp index d11d13b03..712f2fc17 100644 --- a/Examples/NoRendering/noRenderingExample.cpp +++ b/Examples/NoRendering/noRenderingExample.cpp @@ -128,8 +128,8 @@ main() scene->addSceneObject(deformableObj); // print UPS - auto ups = std::make_shared(); - apiutils::printUPS(simManager->getSceneManager(scene), ups); + scene->getConfig()->trackFPS = true; + apiutils::printUPS(simManager->getSceneManager(scene)); // Method to call after the simulation is done running static StdVectorOfVec3d lastPositions; // Vertex positions at the last iteration @@ -154,13 +154,13 @@ main() // Start simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::RUNNING); + simManager->start(SimulationStatus::RUNNING); // Sleep std::this_thread::sleep_for(std::chrono::seconds(300)); // End - simManager->endSimulation(); + simManager->end(); const std::vector expectedFinalPositions = { Vec3d(0, 1, 0), diff --git a/Examples/ObjectControllerDummyClient/ObjectCtrlDummyClientExample.cpp b/Examples/ObjectControllerDummyClient/ObjectCtrlDummyClientExample.cpp index da147e301..8611b0ff3 100644 --- a/Examples/ObjectControllerDummyClient/ObjectCtrlDummyClientExample.cpp +++ b/Examples/ObjectControllerDummyClient/ObjectCtrlDummyClientExample.cpp @@ -81,7 +81,7 @@ main() // Run simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::RUNNING); + simManager->start(SimulationStatus::RUNNING); return 0; } diff --git a/Examples/Octree/OctreeExample.cpp b/Examples/Octree/OctreeExample.cpp index 503a42bbd..ddbb258e7 100644 --- a/Examples/Octree/OctreeExample.cpp +++ b/Examples/Octree/OctreeExample.cpp @@ -256,7 +256,7 @@ main() } // Run - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/PBDCloth/pbdClothExample.cpp b/Examples/PBDCloth/pbdClothExample.cpp index 52283eaf8..5932f5d9c 100644 --- a/Examples/PBDCloth/pbdClothExample.cpp +++ b/Examples/PBDCloth/pbdClothExample.cpp @@ -134,12 +134,11 @@ main() scene->addSceneObject(deformableObj); scene->getCamera()->setFocalPoint(0, -5, 5); - scene->getCamera()->setPosition(-15., -5.0, 15.0); - //scene->m_config->trackFPS = true; + scene->getCamera()->setPosition(-15., -5.0, 15.0); // Start simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/PBDCollision/ManyDragons/PBDCollisionManyDragonsExample.cpp b/Examples/PBDCollision/ManyDragons/PBDCollisionManyDragonsExample.cpp index 6691400f3..ca38def0f 100644 --- a/Examples/PBDCollision/ManyDragons/PBDCollisionManyDragonsExample.cpp +++ b/Examples/PBDCollision/ManyDragons/PBDCollisionManyDragonsExample.cpp @@ -326,7 +326,7 @@ main() cam->setFocalPoint(Vec3d(0, 0, 0)); simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/PBDCollision/OneDragon/PBDCollisionOneDragonExample.cpp b/Examples/PBDCollision/OneDragon/PBDCollisionOneDragonExample.cpp index 776c0e05f..ae7d52d20 100644 --- a/Examples/PBDCollision/OneDragon/PBDCollisionOneDragonExample.cpp +++ b/Examples/PBDCollision/OneDragon/PBDCollisionOneDragonExample.cpp @@ -179,7 +179,7 @@ main() scene->addLight(light); simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::RUNNING); + simManager->start(SimulationStatus::RUNNING); return 0; } diff --git a/Examples/PBDFluids/PBDFluidsExample.cpp b/Examples/PBDFluids/PBDFluidsExample.cpp index 68ddb87b1..3167a18d5 100644 --- a/Examples/PBDFluids/PBDFluidsExample.cpp +++ b/Examples/PBDFluids/PBDFluidsExample.cpp @@ -236,11 +236,11 @@ main() scene->addLight(whiteLight); // print UPS - auto ups = std::make_shared(); - apiutils::printUPS(simManager->getSceneManager(scene), ups); + scene->getConfig()->trackFPS = true; + apiutils::printUPS(simManager->getSceneManager(scene)); simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/PBDString/pbdStringExample.cpp b/Examples/PBDString/pbdStringExample.cpp index 3c03eaa94..a321a9f6a 100644 --- a/Examples/PBDString/pbdStringExample.cpp +++ b/Examples/PBDString/pbdStringExample.cpp @@ -146,7 +146,7 @@ main() // Start simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::RUNNING); + simManager->start(SimulationStatus::RUNNING); return 0; } diff --git a/Examples/PBDVolume/PBDVolumeExample.cpp b/Examples/PBDVolume/PBDVolumeExample.cpp index 02cfa899e..f7baf82e0 100644 --- a/Examples/PBDVolume/PBDVolumeExample.cpp +++ b/Examples/PBDVolume/PBDVolumeExample.cpp @@ -111,7 +111,7 @@ main() simManager->setActiveScene(scene); simManager->getViewer()->setBackgroundColors(Vec3d(0.3285, 0.3285, 0.6525), Vec3d(0.13836, 0.13836, 0.2748), true); - simManager->startSimulation(); + simManager->start(); return 0; } diff --git a/Examples/Rendering/RenderingExample.cpp b/Examples/Rendering/RenderingExample.cpp index 170c8851c..49422d78a 100644 --- a/Examples/Rendering/RenderingExample.cpp +++ b/Examples/Rendering/RenderingExample.cpp @@ -105,7 +105,7 @@ main() //viewer->enableFullscreen(); #endif - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/RigidBodyDynamics/RigidBodyDynamicsExample.cpp b/Examples/RigidBodyDynamics/RigidBodyDynamicsExample.cpp index ac7945cb6..1a7736776 100644 --- a/Examples/RigidBodyDynamics/RigidBodyDynamicsExample.cpp +++ b/Examples/RigidBodyDynamics/RigidBodyDynamicsExample.cpp @@ -200,7 +200,7 @@ main() // Run simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/SPHFluid/SPHFluidExample.hpp b/Examples/SPHFluid/SPHFluidExample.hpp index 7545ed5a8..0ac5b2957 100644 --- a/Examples/SPHFluid/SPHFluidExample.hpp +++ b/Examples/SPHFluid/SPHFluidExample.hpp @@ -131,7 +131,7 @@ main(int argc, char* argv[]) scene->addLight(whiteLight); simManager->setActiveScene(scene); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/SceneManagement/SceneManagementExample.cpp b/Examples/SceneManagement/SceneManagementExample.cpp index c48d6e489..d12585238 100644 --- a/Examples/SceneManagement/SceneManagementExample.cpp +++ b/Examples/SceneManagement/SceneManagementExample.cpp @@ -46,26 +46,26 @@ main() LOG(INFO) << "-- Test scene switch"; int delay = 5; simManager->setActiveScene(scene1); - simManager->startSimulation(); + simManager->start(); std::this_thread::sleep_for(std::chrono::seconds(delay)); simManager->setActiveScene(scene2, false); std::this_thread::sleep_for(std::chrono::seconds(delay)); simManager->setActiveScene(scene1, true); std::this_thread::sleep_for(std::chrono::seconds(delay)); - simManager->endSimulation(); + simManager->end(); // pause/run LOG(INFO) << "-- Test simulation pause/run"; simManager->setActiveScene(scene2); - simManager->startSimulation(); + simManager->start(); std::this_thread::sleep_for(std::chrono::seconds(delay)); - simManager->pauseSimulation(); + simManager->pause(); std::this_thread::sleep_for(std::chrono::seconds(delay)); - simManager->runSimulation(); + simManager->run(); std::this_thread::sleep_for(std::chrono::seconds(delay)); - simManager->pauseSimulation(); + simManager->pause(); std::this_thread::sleep_for(std::chrono::seconds(delay)); - simManager->endSimulation(); + simManager->end(); // Quit while (simManager->getStatus() != SimulationStatus::INACTIVE) {} diff --git a/Examples/Screenshot/ScreenshotExample.cpp b/Examples/Screenshot/ScreenshotExample.cpp index 96258652f..055ad8260 100644 --- a/Examples/Screenshot/ScreenshotExample.cpp +++ b/Examples/Screenshot/ScreenshotExample.cpp @@ -102,7 +102,7 @@ main() // Run simManager->setActiveScene(sceneTest); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/Viewer/ViewerExample.cpp b/Examples/Viewer/ViewerExample.cpp index 332c93de2..addc79555 100644 --- a/Examples/Viewer/ViewerExample.cpp +++ b/Examples/Viewer/ViewerExample.cpp @@ -63,7 +63,7 @@ main() // Run simManager->setActiveScene(sceneTest); - simManager->startSimulation(SimulationStatus::PAUSED); + simManager->start(SimulationStatus::PAUSED); return 0; } diff --git a/Examples/VolumeRendering/VolumeRenderingExample.cpp b/Examples/VolumeRendering/VolumeRenderingExample.cpp index 16d9f0a65..b69fadc07 100644 --- a/Examples/VolumeRendering/VolumeRenderingExample.cpp +++ b/Examples/VolumeRendering/VolumeRenderingExample.cpp @@ -99,7 +99,7 @@ main() }; simManager->getSceneManager(scene)->setPreUpdateCallback(updateFunc); // Run - simManager->startSimulation(); + simManager->start(); return 0; } diff --git a/Source/Core/imstkModule.cpp b/Source/Core/imstkModule.cpp index c7dbd06b0..a65aec042 100644 --- a/Source/Core/imstkModule.cpp +++ b/Source/Core/imstkModule.cpp @@ -21,7 +21,6 @@ #include "imstkModule.h" #include "imstkMath.h" -#include "g3log/g3log.hpp" namespace imstk { @@ -81,13 +80,13 @@ Module::start() { m_postUpdateCallback(this); } - + if (m_trackFPS) { - m_frameCounter->setEndPointOfUpdate(); - + m_frameCounter->setEndPointOfUpdate(); + std::cout << "\r" << this->getName() << " running at " - << m_frameCounter->getUPS() << " ups " << std::flush; + << m_frameCounter->getUPS() << " ups " << std::flush; } continue; } @@ -116,8 +115,6 @@ Module::start() { m_frameCounter->setEndPointOfUpdate(); } - std::cout << "\r" << this->getName() << " running at " - << m_frameCounter->getUPS() << " ups " << std::flush; previous_t = current_t; } @@ -241,4 +238,23 @@ Module::setFrequency(const double f) } m_loopDelay = 1000.0 / f; } + +unsigned int +Module::getUPS() +{ + if (m_status != ModuleStatus::RUNNING) + { + return 0; + } + + if (m_trackFPS) + { + return m_frameCounter->getUPS(); + } + else + { + LOG(WARNING) << "Frame counter is not enabled!"; + return 0; + } +} } diff --git a/Source/Core/imstkModule.h b/Source/Core/imstkModule.h index c320cf10b..f0bbd3b78 100644 --- a/Source/Core/imstkModule.h +++ b/Source/Core/imstkModule.h @@ -24,6 +24,7 @@ #include #include #include +#include "g3log/g3log.hpp" #include "imstkTimer.h" namespace imstk @@ -56,10 +57,7 @@ public: /// Module(std::string name, int loopDelay = 0) : m_name(name), - m_loopDelay(loopDelay) - { - m_frameCounter = std::make_shared(); - } + m_loopDelay(loopDelay) {} /// /// \brief Destructor @@ -123,24 +121,11 @@ public: /// void setFrequency(const double f); - void enableFrameCount() { m_trackFPS = true; }; - void disableFrameCount() { m_trackFPS = false; }; - bool isFrameCountEnabled() const {return m_trackFPS;}; - - - unsigned int getUPS() - { - if (m_trackFPS) - { - return m_frameCounter->getUPS(); - } - else - { - LOG(WARNING) << "Frame counter is not enabled!"; - return 0; - } - }; + void disableFrameCount() { m_trackFPS = false; }; + bool isFrameCountEnabled() const { return m_trackFPS; }; + + unsigned int getUPS(); protected: @@ -166,10 +151,10 @@ protected: CallbackFunction m_preCleanUpCallback; ///> function callback preceding module cleanup CallbackFunction m_postCleanUpCallback; ///> function callback following module cleanup - std::atomic m_status{ ModuleStatus::INACTIVE }; ///> Module status + std::atomic m_status { ModuleStatus::INACTIVE }; ///> Module status bool m_trackFPS = false; - std::shared_ptr m_frameCounter; + std::shared_ptr m_frameCounter = std::make_shared(); std::string m_name; ///> Name of the module double m_loopDelay = 0; ///> Loop delay diff --git a/Source/Scene/imstkScene.h b/Source/Scene/imstkScene.h index a30740d66..e95625398 100644 --- a/Source/Scene/imstkScene.h +++ b/Source/Scene/imstkScene.h @@ -60,10 +60,9 @@ public: // Note: May cause delays to run the first frame of the scene due to scene initialization bool lazyInitialization = false; - bool measureFameTimes = false; - TimeSteppingPolicy timeStepping = TimeSteppingPolicy::asFastAsPossible; + // Keep track of the fps for the scene bool trackFPS = false; }; @@ -200,10 +199,15 @@ public: void setFPS(const double fps) { m_fps = fps; } double getFPS() { return m_fps; } -public: - std::shared_ptr m_config; + /// + /// \brief Get the configuration + /// + std::shared_ptr getConfig() const { return m_config; }; + const std::shared_ptr getConfig() { return m_config; }; protected: + std::shared_ptr m_config; + std::string m_name; ///> Name of the scene NamedMap m_sceneObjectsMap; NamedMap m_DebugRenderGeometryMap; diff --git a/Source/SimulationManager/VTKRenderer/imstkOpenVRCommand.cpp b/Source/SimulationManager/VTKRenderer/imstkOpenVRCommand.cpp index 1d59f0c2d..f7ff44b75 100644 --- a/Source/SimulationManager/VTKRenderer/imstkOpenVRCommand.cpp +++ b/Source/SimulationManager/VTKRenderer/imstkOpenVRCommand.cpp @@ -47,19 +47,19 @@ OpenVRCommand::Execute( if (status == SimulationStatus::RUNNING) { - m_simManager->pauseSimulation(); + m_simManager->pause(); } // pause simulation if (status == SimulationStatus::INACTIVE) { - m_simManager->startSimulation(); + m_simManager->start(); } // continue simulation if (status == SimulationStatus::PAUSED) { - m_simManager->runSimulation(); + m_simManager->run(); } this->AbortFlagOn(); @@ -72,13 +72,13 @@ OpenVRCommand::Execute( // pause simulation if (status == SimulationStatus::RUNNING) { - m_simManager->pauseSimulation(); + m_simManager->pause(); } // continue simulation if (status == SimulationStatus::PAUSED) { - m_simManager->runSimulation(); + m_simManager->run(); } this->AbortFlagOn(); @@ -91,11 +91,11 @@ OpenVRCommand::Execute( if (status == SimulationStatus::INACTIVE) { - m_simManager->startSimulation(); + m_simManager->start(); } else { - m_simManager->endSimulation(); + m_simManager->end(); } this->AbortFlagOn(); diff --git a/Source/SimulationManager/VTKRenderer/imstkVTKInteractorStyle.cpp b/Source/SimulationManager/VTKRenderer/imstkVTKInteractorStyle.cpp index 057fa1a39..13e6534cc 100644 --- a/Source/SimulationManager/VTKRenderer/imstkVTKInteractorStyle.cpp +++ b/Source/SimulationManager/VTKRenderer/imstkVTKInteractorStyle.cpp @@ -145,18 +145,18 @@ VTKInteractorStyle::OnChar() // pause simulation if (status == SimulationStatus::RUNNING) { - m_simManager->pauseSimulation(); + m_simManager->pause(); } // play simulation else if (status == SimulationStatus::PAUSED) { - m_simManager->runSimulation(); + m_simManager->run(); } // Launch simulation if inactive if (status == SimulationStatus::INACTIVE) { m_textStatusManager->setStatusVisibility(VTKTextStatusManager::FPS, m_displayFps); - m_simManager->startSimulation(SimulationStatus::RUNNING); + m_simManager->start(SimulationStatus::RUNNING); } } else if (status != SimulationStatus::INACTIVE @@ -188,7 +188,7 @@ VTKInteractorStyle::OnChar() } else if (key == 'r' || key == 'R') { - m_simManager->resetSimulation(); + m_simManager->reset(); } } diff --git a/Source/SimulationManager/imstkSimulationManager.cpp b/Source/SimulationManager/imstkSimulationManager.cpp index 6089930e7..de240365b 100644 --- a/Source/SimulationManager/imstkSimulationManager.cpp +++ b/Source/SimulationManager/imstkSimulationManager.cpp @@ -28,28 +28,15 @@ namespace imstk { -//SimulationManager::SimulationManager(const SimulationManager::Mode mode, const bool enableVR) -//{ -// m_config = std::make_shared(); -// m_config->simulationMode = mode; -// m_config->VR_Enabled = enableVR; -// -// // Init g3logger -// m_logUtil->createLogger(m_config->logFilePrefix, m_config->logPath); -// -// if (mode == Mode::rendering) -// { -// createViewer(m_config->VR_Enabled); -// } -//} - SimulationManager::SimulationManager(const std::shared_ptr config) { + // set the config m_config = config; - // Init g3logger + // Initialize the logger m_logUtil->createLogger(m_config->logFilePrefix, m_config->logPath); + // create viewer if the selected mode is 'rendering' if (m_config->simulationMode == Mode::rendering) { createViewer(m_config->VR_Enabled); @@ -99,14 +86,7 @@ SimulationManager::setOptimalThreadPoolSize() bool SimulationManager::isSceneRegistered(const std::string& sceneName) const { - /* if (m_simulationMode != Mode::backend) - { - return m_sceneManagerMap.find(sceneName) != m_sceneManagerMap.end(); - } - else - {*/ return m_sceneMap.find(sceneName) != m_sceneMap.end(); - //} } std::shared_ptr @@ -114,7 +94,7 @@ SimulationManager::getSceneManager(const std::string& sceneName) const { if (m_config->simulationMode == Mode::backend) { - LOG(INFO) << "The simulation manager is in backend mode. No scene managers were created!"; + LOG(WARNING) << "The simulation manager is in backend mode. No scene managers were created!"; return nullptr; } @@ -141,15 +121,7 @@ SimulationManager::getSceneManager(std::shared_ptr scene) const std::shared_ptr SimulationManager::getScene(const std::string& sceneName) const { - if (m_config->simulationMode != Mode::backend) - { - auto sceneManager = this->getSceneManager(sceneName); - return sceneManager ? sceneManager->getScene() : nullptr; - } - else - { - return m_sceneMap.find(sceneName) != m_sceneMap.end() ? m_sceneMap.at(sceneName) : nullptr; - } + return m_sceneMap.find(sceneName) != m_sceneMap.end() ? m_sceneMap.at(sceneName) : nullptr; } std::shared_ptr @@ -161,23 +133,24 @@ SimulationManager::getActiveScene() const std::shared_ptr SimulationManager::createNewScene(const std::string& newSceneName, std::shared_ptr config) { - // check if there is alread a scene by that name + // check if there is already a scene by that name if (this->isSceneRegistered(newSceneName)) { - LOG(WARNING) << "Can not create new scene: '" << newSceneName - << "' is already registered in this simulation\n" - << "You can create a new scene using an unique name"; - return nullptr; + LOG(FATAL) << "Can not create new scene: '" << newSceneName + << "' is already registered in this simulation\n" + << "You can create a new scene using an unique name"; } auto newScene = std::make_shared(newSceneName, config); m_mutex.lock(); + m_sceneMap[newScene->getName()] = newScene; if (m_config->simulationMode != Mode::backend) { m_sceneManagerMap[newScene->getName()] = std::make_shared(newScene); } + m_mutex.unlock(); LOG(INFO) << "New scene added: " << newScene->getName(); @@ -189,17 +162,11 @@ std::shared_ptr SimulationManager::createNewScene() { m_mutex.lock(); + int id = 0; - //if (m_simulationMode != Mode::backend) - //{ id = (int)m_sceneMap.size() + 1; - //} - /*else - { - id = (int)m_sceneManagerMap.size() + 1; - }*/ - m_mutex.lock(); + m_mutex.unlock(); std::string newSceneName = "Scene_" + std::to_string(id); @@ -221,14 +188,14 @@ SimulationManager::addScene(std::shared_ptr newScene) m_mutex.lock(); + // create a scene manager if not 'backend' mode if (m_config->simulationMode != Mode::backend) { m_sceneManagerMap[newSceneName] = std::make_shared(newScene); } - //else - //{ + + // add new scene to the unordered map m_sceneMap[newSceneName] = newScene; - //} m_mutex.unlock(); @@ -254,18 +221,15 @@ SimulationManager::removeScene(const std::string& sceneName) m_mutex.lock(); + // remove the scene manager and the scene if (m_config->simulationMode != Mode::backend) { m_sceneManagerMap.erase(sceneName); } - //else - //{ m_sceneMap.erase(sceneName); - //} m_mutex.unlock(); - //m_sceneManagerMap.erase(sceneName); LOG(INFO) << "Scene removed: " << sceneName; } @@ -344,20 +308,23 @@ SimulationManager::setActiveScene(const std::string& newSceneName, { LOG(INFO) << "Setting " << newSceneName << " as active"; - if (newSceneName == m_activeSceneName) + // check if the scene is registered + if (!this->isSceneRegistered(newSceneName)) { - LOG(INFO) << "Scene '" << newSceneName << "' is already active!"; + LOG(WARNING) << "Scene by the name '" << newSceneName << "' not registered! Please register before setting active"; return; } - auto newScene = this->getScene(newSceneName); - if (!newScene) + // check if the scene is already active + if (newSceneName == m_activeSceneName) { - LOG(WARNING) << "\tCan not find scene"; + LOG(INFO) << "Scene '" << newSceneName << "' is already active!"; return; } - // Initialize the scene (if not done so already) + auto newScene = this->getScene(newSceneName); + + // Initialize the scene if not done so already if (!newScene->isInitialized()) { newScene->initialize(); @@ -365,7 +332,7 @@ SimulationManager::setActiveScene(const std::string& newSceneName, if (m_viewer) { - // Update viewer scene + // Update viewer with the new scene m_viewer->setActiveScene(newScene); // If not yet rendering: update current scene and return @@ -375,6 +342,7 @@ SimulationManager::setActiveScene(const std::string& newSceneName, return; } } + // If rendering and simulation not active: // render scene in debug, update current scene, and return if (m_status == SimulationStatus::INACTIVE) @@ -394,7 +362,7 @@ SimulationManager::setActiveScene(const std::string& newSceneName, m_viewer->setRenderingMode(Renderer::Mode::SIMULATION); } - // Stop/Pause running scene + // Stop/Pause currently running scene if (m_config->simulationMode != Mode::backend) { auto oldSceneManager = m_sceneManagerMap.at(m_activeSceneName); @@ -410,12 +378,12 @@ SimulationManager::setActiveScene(const std::string& newSceneName, } // Start/Run new scene - auto newSceneManager = m_sceneManagerMap.at(newSceneName + std::string("_sceneManager")); - if (newScene->m_config->trackFPS) + auto newSceneManager = m_sceneManagerMap.at(newSceneName); + if (newScene->getConfig()->trackFPS) { newSceneManager->enableFrameCount(); } - + if (newSceneManager->getStatus() == ModuleStatus::INACTIVE) { this->startModuleInNewThread(newSceneManager); @@ -455,7 +423,7 @@ SimulationManager::initialize() for (auto it = m_sceneMap.begin(); it != m_sceneMap.end(); ++it) { // do not initialize of not active scene - if (this->getActiveScene() != it->second && !it->second->m_config->lazyInitialization) + if (this->getActiveScene() != it->second && !it->second->getConfig()->lazyInitialization) { if (!it->second->initialize()) { @@ -465,17 +433,6 @@ SimulationManager::initialize() } } - // Initialize the active scene - /*if (!this->getActiveScene()->isInitialized()) - { - if (!this->getActiveScene()->initialize()) - { - LOG(WARNING) << "SimulationManager::startSimulation - Unable to initialize the active scene - " - << this->getActiveScene()->getName() << std::endl; - return; - } - }*/ - m_initialized = true; // launches modules other than the simulation module at the initialization time in the backend mode @@ -511,7 +468,7 @@ SimulationManager::startModules() } // Start scene manager in a new thread - if (m_sceneMap.at(m_activeSceneName)->m_config->trackFPS) + if (m_sceneMap.at(m_activeSceneName)->getConfig()->trackFPS) { m_sceneManagerMap.at(m_activeSceneName)->enableFrameCount(); } @@ -520,8 +477,8 @@ SimulationManager::startModules() } void -SimulationManager::startSimulation(const SimulationStatus simStatus /*= SimulationStatus::PAUSED*/, - const Renderer::Mode renderMode /*= Renderer::Mode::SIMULATION*/) +SimulationManager::start(const SimulationStatus simStatus /*= SimulationStatus::PAUSED*/, + const Renderer::Mode renderMode /*= Renderer::Mode::SIMULATION*/) { this->initialize(); @@ -529,6 +486,13 @@ SimulationManager::startSimulation(const SimulationStatus simStatus /*= Simulati { return; } + else + { + if (m_activeSceneName == "") + { + LOG(FATAL) << "Cannot start simulation without active scene in rendering and background modes!"; + } + } if (m_status != SimulationStatus::INACTIVE) { @@ -536,15 +500,12 @@ SimulationManager::startSimulation(const SimulationStatus simStatus /*= Simulati return; } - //if (m_simulationMode != Mode::backend) + if (m_activeSceneName != "") { - if (m_activeSceneName != "") + auto startingSceneManager = m_sceneManagerMap.at(m_activeSceneName); + if (startingSceneManager->getStatus() != ModuleStatus::INACTIVE) { - auto startingSceneManager = m_sceneManagerMap.at(m_activeSceneName); - if (startingSceneManager->getStatus() != ModuleStatus::INACTIVE) - { - return; - } + return; } } @@ -558,11 +519,6 @@ SimulationManager::startSimulation(const SimulationStatus simStatus /*= Simulati m_status = SimulationStatus::PAUSED; } - /*if (simStatus == SimulationStatus::PAUSED) - { - this->pauseSimulation(); - }*/ - // Note: This never returns until the viewer is terminated if (m_config->simulationMode == Mode::rendering) { @@ -575,7 +531,7 @@ SimulationManager::startSimulation(const SimulationStatus simStatus /*= Simulati { this->printUserControlsInfo(false); this->infiniteLoopNoRenderingMode(); - this->endSimulation(); + this->end(); } } @@ -593,7 +549,7 @@ SimulationManager::infiniteLoopNoRenderingMode() if (c == 'r' || c == 'R') { - this->resetSimulation(); + this->reset(); continue; } @@ -601,13 +557,13 @@ SimulationManager::infiniteLoopNoRenderingMode() { if (this->getStatus() == SimulationStatus::RUNNING) { - this->pauseSimulation(); + this->pause(); continue; } if (this->getStatus() == SimulationStatus::PAUSED) { - this->runSimulation(); + this->run(); continue; } } @@ -643,7 +599,7 @@ SimulationManager::startViewer(const Renderer::Mode renderMode /*= Renderer::Mod if (m_status != SimulationStatus::INACTIVE) { LOG(INFO) << "Ending simulation"; - this->endSimulation(); + this->end(); } } } @@ -677,7 +633,7 @@ SimulationManager::printUserControlsInfo(const bool isRendering) const } void -SimulationManager::runSimulation() +SimulationManager::run() { if (m_config->simulationMode == Mode::backend) { @@ -712,7 +668,7 @@ SimulationManager::runSimulation() } void -SimulationManager::pauseSimulation() +SimulationManager::pause() { if (m_config->simulationMode == Mode::backend) { @@ -755,7 +711,7 @@ SimulationManager::pauseModules() } void -SimulationManager::resetSimulation() +SimulationManager::reset() { LOG(INFO) << "Resetting simulation"; @@ -764,7 +720,7 @@ SimulationManager::resetSimulation() } void -SimulationManager::endSimulation() +SimulationManager::end() { /*if ((m_status != SimulationStatus::RUNNING) && (m_status != SimulationStatus::PAUSED)) @@ -840,7 +796,7 @@ SimulationManager::startModuleInNewThread(std::shared_ptr module) } void -SimulationManager::setCallbackBgModel(keyPressCallback func, const int c) +SimulationManager::addKeyPressCallback(keyPressCallback func, const int c) { m_kepPressCallbacks.push_back(callbackKeyPair { c, func }); } diff --git a/Source/SimulationManager/imstkSimulationManager.h b/Source/SimulationManager/imstkSimulationManager.h index b0145e188..45c7b2cdb 100644 --- a/Source/SimulationManager/imstkSimulationManager.h +++ b/Source/SimulationManager/imstkSimulationManager.h @@ -55,7 +55,8 @@ using keyPressCallback = std::function; public: /// - /// \brief Type of the collision detection + /// \brief Simulation manager mode + /// \note The mode is set at the time of initialization /// enum class Mode { @@ -72,6 +73,9 @@ public: backend }; + /// + /// \brief Simulation manager configuraion + /// struct simManagerConfig { // logger @@ -84,8 +88,8 @@ public: // states Mode simulationMode = Mode::rendering; SimulationStatus startingStatus = SimulationStatus::RUNNING; - bool VR_Enabled = false; - bool startInPausedState = false; + bool VR_Enabled = false; + bool startInPausedState = false; // 0 indicates that an optimal size will be used unsigned int threadPoolSize = 0; @@ -100,7 +104,7 @@ public: /// /// \brief Default destructor /// - ~SimulationManager() { this->endSimulation(); }; + ~SimulationManager() { this->end(); }; /// /// \brief Returns the simulation status @@ -120,8 +124,6 @@ public: /// void setOptimalThreadPoolSize(); - // Scene - /// /// \brief Returns true if the scene is registered, else false /// @@ -146,7 +148,8 @@ public: /// /// \brief Create a new scene with a given name /// - std::shared_ptr createNewScene(const std::string& newSceneName, std::shared_ptr config = std::make_shared()); + std::shared_ptr createNewScene(const std::string& newSceneName, + std::shared_ptr config = std::make_shared()); /// /// \brief Create a new scene @@ -189,8 +192,6 @@ public: // Viewer std::shared_ptr getViewer() const; - // Simulation - /// /// \brief Set the current scene to the one with the supplied name /// @@ -206,8 +207,8 @@ public: /// and returned. In rendering and runInBackground modes the simulation manager /// module gets launched and an never returns /// - void startSimulation(const SimulationStatus simStatus = SimulationStatus::PAUSED, - const Renderer::Mode renderMode = Renderer::Mode::SIMULATION); + void start(const SimulationStatus simStatus = SimulationStatus::PAUSED, + const Renderer::Mode renderMode = Renderer::Mode::SIMULATION); /// /// \brief Initialize the modules and the active scene @@ -218,22 +219,22 @@ public: /// \brief Run the simulation from a paused state /// In Mode::backend mode, the simulation manager is initialized if not and returned /// - void runSimulation(); + void run(); /// /// \brief Pause the simulation /// - void pauseSimulation(); + void pause(); /// /// \brief Reset the simulation to initial state /// - void resetSimulation(); + void reset(); /// /// \brief End the simulation /// - void endSimulation(); + void end(); /// /// \brief Advance to next frame @@ -246,18 +247,17 @@ public: SimulationManager::Mode getMode() const { return m_config->simulationMode; } /// - /// \brief Set key press callback to be used in background mode only + /// \brief Add key press callback to be used in background mode only /// - void setCallbackBgModel(keyPressCallback func, const int c); + void addKeyPressCallback(keyPressCallback func, const int c); + //void removeKeyPressCallback(keyPressCallback func); -protected: +private: /// /// \brief Create a viewer /// void createViewer(const bool enableVR); -private: - /// /// \brief Launch simulation for the first time. /// 1. Initialize the active scene if not initialized already. diff --git a/Source/apiUtilities/imstkAPIUtilities.h b/Source/apiUtilities/imstkAPIUtilities.h index 49fb9cae4..732e04c43 100644 --- a/Source/apiUtilities/imstkAPIUtilities.h +++ b/Source/apiUtilities/imstkAPIUtilities.h @@ -227,40 +227,18 @@ createNonLinearSystem(std::shared_ptr dynaModel) /// \brief Print number of updates for second for a given scene /// void -printUPS(std::shared_ptr sceneManager, std::shared_ptr& ups) +printUPS(std::shared_ptr sceneManager) { if (!sceneManager) { - LOG(WARNING) << "APIUtilities::printUPS - scene manager is not valid! Unable to set UPS counter"; + LOG(WARNING) << "APIUtilities::printUPS - scene manager is not valid! Unable to print UPS"; return; } - if (!ups) - { - LOG(WARNING) << "APIUtilities::printUPS - UPS Counter is not valid! Unable to set UPS counter"; - return; - } - - sceneManager->setPreInitCallback([](Module* module) - { - LOG(INFO) << "Pre initialization of " << module->getName() << " module"; - }); - - sceneManager->setPreUpdateCallback([&ups](Module* module) + sceneManager->setPostUpdateCallback([&sceneManager](Module* module) { - ups->setStartPointOfUpdate(); - }); - - sceneManager->setPostUpdateCallback([&ups](Module* module) - { - ups->setEndPointOfUpdate(); std::cout << "\r" << module->getName() << " running at " - << ups->getUPS() << " ups " << std::flush; - }); - - sceneManager->setPostCleanUpCallback([](Module* module) - { - LOG(INFO) << "\nPost cleanup of " << module->getName() << " module"; + << sceneManager->getUPS() << " ups " << std::flush; }); } -- GitLab From 2f9e6ac173facc4b5eedb7b38c514047df3aee35 Mon Sep 17 00:00:00 2001 From: sreekanth-arikatla Date: Thu, 27 Feb 2020 16:48:07 -0500 Subject: [PATCH 5/5] temp2 --- Examples/MultipleScenes/multipleScenes.cpp | 2 +- Source/Core/imstkModule.cpp | 14 +- .../imstkSimulationManager.cpp | 163 +++++++++--------- .../imstkSimulationManager.h | 34 ++-- 4 files changed, 104 insertions(+), 109 deletions(-) diff --git a/Examples/MultipleScenes/multipleScenes.cpp b/Examples/MultipleScenes/multipleScenes.cpp index e4e4bbb21..d6b424cc7 100644 --- a/Examples/MultipleScenes/multipleScenes.cpp +++ b/Examples/MultipleScenes/multipleScenes.cpp @@ -251,7 +251,7 @@ testMultipleScenesInRenderMode() scene1->getConfig()->trackFPS = true; // set to scene 1 - simManager->setActiveScene(scene1); + simManager->setActiveScene(scene2); // Create a call back on key press of 's' to switch scenes auto viewer = simManager->getViewer(); diff --git a/Source/Core/imstkModule.cpp b/Source/Core/imstkModule.cpp index a65aec042..38efed5b0 100644 --- a/Source/Core/imstkModule.cpp +++ b/Source/Core/imstkModule.cpp @@ -129,8 +129,6 @@ Module::run() { if (m_status != ModuleStatus::PAUSED) { - LOG(WARNING) << "Can not run '" << m_name << "'.\n" - << "Module not paused."; return; } @@ -140,16 +138,12 @@ Module::run() void Module::pause() { - if (m_status != ModuleStatus::RUNNING) + if (m_status == ModuleStatus::RUNNING) { - LOG(WARNING) << "Can not pause '" << m_name << "'.\n" - << "Module not running."; - return; - } + m_status = ModuleStatus::PAUSING; - m_status = ModuleStatus::PAUSING; - - while (m_status != ModuleStatus::PAUSED) {} + while (m_status != ModuleStatus::PAUSED) {} + } } void diff --git a/Source/SimulationManager/imstkSimulationManager.cpp b/Source/SimulationManager/imstkSimulationManager.cpp index de240365b..3e1307d62 100644 --- a/Source/SimulationManager/imstkSimulationManager.cpp +++ b/Source/SimulationManager/imstkSimulationManager.cpp @@ -30,7 +30,7 @@ namespace imstk { SimulationManager::SimulationManager(const std::shared_ptr config) { - // set the config + // set the configuration m_config = config; // Initialize the logger @@ -54,10 +54,8 @@ SimulationManager::createViewer(const bool enableVR) m_viewer = std::make_shared(this, enableVR); #else - if (enableVR) - { - LOG(FATAL) << "Can not run VR simulation without iMSTK_ENABLE_VR"; - } + LOG_IF(FATAL, enableVR) << "Can not run VR simulation without iMSTK_ENABLE_VR"; + m_viewer = std::make_shared(this, false); m_viewer->setWindowTitle(m_config->simulationName); #endif @@ -111,10 +109,8 @@ SimulationManager::getSceneManager(const std::string& sceneName) const std::shared_ptr SimulationManager::getSceneManager(std::shared_ptr scene) const { - if (!scene) - { - LOG(FATAL) << "SimulationManager::getSceneManager - Scene supplied is not valid!"; - } + LOG_IF(FATAL, !scene) << "SimulationManager::getSceneManager - Scene supplied is not valid!"; + return this->getSceneManager(scene->getName()); } @@ -134,12 +130,10 @@ std::shared_ptr SimulationManager::createNewScene(const std::string& newSceneName, std::shared_ptr config) { // check if there is already a scene by that name - if (this->isSceneRegistered(newSceneName)) - { - LOG(FATAL) << "Can not create new scene: '" << newSceneName - << "' is already registered in this simulation\n" - << "You can create a new scene using an unique name"; - } + LOG_IF(FATAL, this->isSceneRegistered(newSceneName)) + << "Can not create new scene: '" << newSceneName + << "' is already registered in this simulation\n" + << "You can create a new scene using an unique name"; auto newScene = std::make_shared(newSceneName, config); @@ -288,10 +282,8 @@ SimulationManager::removeModule(const std::string& moduleName) std::shared_ptr SimulationManager::getViewer() const { - if (m_config->simulationMode != Mode::rendering) - { - LOG(WARNING) << "The simulation is not in rendering mode!"; - } + LOG_IF(WARNING, (m_config->simulationMode != Mode::rendering)) << "The simulation is not in rendering mode!"; + return m_viewer; } @@ -306,12 +298,13 @@ void SimulationManager::setActiveScene(const std::string& newSceneName, const bool unloadCurrentScene /*= false*/) { - LOG(INFO) << "Setting " << newSceneName << " as active"; + LOG(INFO) << "Setting scene '" << newSceneName << "' as active"; // check if the scene is registered if (!this->isSceneRegistered(newSceneName)) { - LOG(WARNING) << "Scene by the name '" << newSceneName << "' not registered! Please register before setting active"; + LOG(WARNING) << "Scene '" << newSceneName + << "' not registered! Please register before setting active"; return; } @@ -378,19 +371,10 @@ SimulationManager::setActiveScene(const std::string& newSceneName, } // Start/Run new scene - auto newSceneManager = m_sceneManagerMap.at(newSceneName); - if (newScene->getConfig()->trackFPS) - { - newSceneManager->enableFrameCount(); - } - - if (newSceneManager->getStatus() == ModuleStatus::INACTIVE) + if (m_status != SimulationStatus::PAUSED) { - this->startModuleInNewThread(newSceneManager); - } - else if (newSceneManager->getStatus() == ModuleStatus::PAUSED) - { - newSceneManager->run(); + this->launchSceneModule(newSceneName); + m_sceneManagerMap.at(newSceneName)->run(); } } m_activeSceneName = newSceneName; @@ -404,8 +388,6 @@ SimulationManager::initialize() return; } - LOG(INFO) << "Initializing simulation"; - // Do some checks if (m_status == SimulationStatus::RUNNING) { @@ -413,48 +395,48 @@ SimulationManager::initialize() return; } - // check if there is an active scene - if (!this->getActiveScene()) - { - LOG(WARNING) << "No valid active scene! Simulation canceled"; - } - // Initialize all the scenes - for (auto it = m_sceneMap.begin(); it != m_sceneMap.end(); ++it) + for (const auto& it : m_sceneMap) { // do not initialize of not active scene - if (this->getActiveScene() != it->second && !it->second->getConfig()->lazyInitialization) + if (!it.second->getConfig()->lazyInitialization) { - if (!it->second->initialize()) + if (!it.second->initialize()) { - LOG(WARNING) << "Unable to initialize the scene - " << it->first; + LOG(WARNING) << "Unable to initialize the scene - " << it.first; return; } + else + { + LOG(WARNING) << "Scene '" << it.first << "' initialized"; + } } } m_initialized = true; - - // launches modules other than the simulation module at the initialization time in the backend mode - if (m_config->simulationMode == Mode::backend) - { - this->launchSimulation(); - } - - LOG(INFO) << "Initialization Done"; } void -SimulationManager::launchSimulation() +SimulationManager::launchSceneModule(std::string sceneName) { - this->initialize(); - this->startModules(); - m_status = SimulationStatus::RUNNING; - m_simThreadLaunched = true; + auto scene = this->getScene(sceneName); + + LOG_IF(WARNING, !scene) << "Scene '" << sceneName << "' not found!"; + + const auto sceneMan = m_sceneManagerMap.at(sceneName); + if (scene->getConfig()->trackFPS) + { + sceneMan->enableFrameCount(); + } + + if (sceneMan->getStatus() == ModuleStatus::INACTIVE) + { + this->startModuleInNewThread(sceneMan); + } } void -SimulationManager::startModules() +SimulationManager::startNonSceneModules() { // Start modules (except the scene manager module) in separate threads for (const auto& pair : m_modulesMap) @@ -466,21 +448,22 @@ SimulationManager::startModules() { return; } - - // Start scene manager in a new thread - if (m_sceneMap.at(m_activeSceneName)->getConfig()->trackFPS) - { - m_sceneManagerMap.at(m_activeSceneName)->enableFrameCount(); - } - - this->startModuleInNewThread(m_sceneManagerMap.at(m_activeSceneName)); } void SimulationManager::start(const SimulationStatus simStatus /*= SimulationStatus::PAUSED*/, const Renderer::Mode renderMode /*= Renderer::Mode::SIMULATION*/) { - this->initialize(); + // check if there is an active scene + LOG_IF(WARNING, !this->getActiveScene()) << "No valid active scene! Simulation canceled"; + + if (!m_initialized) + { + this->initialize(); + } + + // start modules other than scene modules + this->startNonSceneModules(); if (m_config->simulationMode == Mode::backend) // returns in backend mode { @@ -488,10 +471,8 @@ SimulationManager::start(const SimulationStatus simStatus /*= SimulationStatus:: } else { - if (m_activeSceneName == "") - { - LOG(FATAL) << "Cannot start simulation without active scene in rendering and background modes!"; - } + LOG_IF(FATAL, m_activeSceneName.empty()) + << "Cannot start simulation without active scene in rendering and background modes!"; } if (m_status != SimulationStatus::INACTIVE) @@ -511,14 +492,15 @@ SimulationManager::start(const SimulationStatus simStatus /*= SimulationStatus:: if (simStatus != SimulationStatus::PAUSED) { - // Launch simulation right away if the simulator starts in running mode - this->launchSimulation(); + m_status = SimulationStatus::RUNNING; } else { m_status = SimulationStatus::PAUSED; } + m_simulationStarted = true; + // Note: This never returns until the viewer is terminated if (m_config->simulationMode == Mode::rendering) { @@ -649,13 +631,24 @@ SimulationManager::run() LOG(INFO) << "Running simulation"; - if (!m_simThreadLaunched) + if (!m_simulationStarted) + { + if (!m_initialized) + { + this->initialize(); + } + + this->startNonSceneModules(); + } + + const auto sceneMan = m_sceneManagerMap.at(m_activeSceneName); + if (sceneMan->getStatus() == ModuleStatus::INACTIVE) { - this->launchSimulation(); + launchSceneModule(m_activeSceneName); } // Run scene manager - m_sceneManagerMap.at(m_activeSceneName)->run(); + sceneMan->run(); // Run modules for (const auto& mod : m_modulesMap) @@ -682,7 +675,6 @@ SimulationManager::pause() return; } - LOG(INFO) << "Pausing simulation"; m_status = SimulationStatus::PAUSING; // Pause scene managers @@ -715,8 +707,14 @@ SimulationManager::reset() { LOG(INFO) << "Resetting simulation"; - // Reset scene - this->getScene(m_activeSceneName)->reset(); + // Reset all scenes + for (const auto s : m_sceneMap) + { + if (s.second->isInitialized()) + { + s.second->reset(); + } + } } void @@ -777,10 +775,9 @@ SimulationManager::advanceFrame() { if (m_initialized) { - auto activeSce = this->getActiveScene(); - if (activeSce) + if (auto activeScene = this->getActiveScene()) { - activeSce->advance(); + activeScene->advance(); } } else diff --git a/Source/SimulationManager/imstkSimulationManager.h b/Source/SimulationManager/imstkSimulationManager.h index 45c7b2cdb..d7f61db54 100644 --- a/Source/SimulationManager/imstkSimulationManager.h +++ b/Source/SimulationManager/imstkSimulationManager.h @@ -42,6 +42,9 @@ namespace imstk { using SimulationStatus = ModuleStatus; +template +using SceneNameMap = std::unordered_map>; + /// /// \class SimulationManager /// @@ -97,8 +100,7 @@ public: /// /// \brief Constructor - /// - //SimulationManager(const SimulationManager::Mode mode = Mode::rendering, const bool enableVR = false); + /// SimulationManager(const std::shared_ptr config = std::make_shared()); /// @@ -248,9 +250,9 @@ public: /// /// \brief Add key press callback to be used in background mode only + /// \todo add remove function as well /// void addKeyPressCallback(keyPressCallback func, const int c); - //void removeKeyPressCallback(keyPressCallback func); private: /// @@ -258,13 +260,6 @@ private: /// void createViewer(const bool enableVR); - /// - /// \brief Launch simulation for the first time. - /// 1. Initialize the active scene if not initialized already. - /// 2. Launches separate threads for each module. - /// - void launchSimulation(); - /// /// \brief Start the viewer /// @@ -275,12 +270,20 @@ private: /// void printUserControlsInfo(const bool isRendering = true) const; + /// + /// \brief Start a module (refer \link imstkModule \endlink) in new thread + /// void startModuleInNewThread(std::shared_ptr module); /// /// \brief Start modules /// - void startModules(); + void startNonSceneModules(); + + /// + /// \brief Launch scene manager modules + /// + void launchSceneModule(std::string sceneName); /// /// \brief Pause modules @@ -299,10 +302,11 @@ private: void infiniteLoopNoRenderingMode(); std::string m_activeSceneName = ""; - std::unordered_map> m_sceneManagerMap; - std::unordered_map> m_sceneMap; // used in backend mode where m_sceneManagerMap is not used - std::unordered_map> m_modulesMap; + // Maps + SceneNameMap m_sceneManagerMap; + SceneNameMap m_sceneMap; // used in backend mode where m_sceneManagerMap is not used + SceneNameMap m_modulesMap; std::unordered_map m_threadMap; @@ -314,7 +318,7 @@ private: // states SimulationStatus m_status = SimulationStatus::INACTIVE; - bool m_simThreadLaunched = false; + bool m_simulationStarted = false; bool m_initialized = false; std::shared_ptr m_config; -- GitLab