diff --git a/Base/Core/imstkUtils.h b/Base/Core/imstkUtils.h new file mode 100644 index 0000000000000000000000000000000000000000..b529f9cf06995bb2a160b7bef154aad5d3aab915 --- /dev/null +++ b/Base/Core/imstkUtils.h @@ -0,0 +1,190 @@ +/*========================================================================= + +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. + +=========================================================================*/ + +#ifndef imstkUtils_h +#define imstkUtils_h + +// STL includes +#include <iostream> +#include <fstream> + +// imstk +#include "imstkMath.h" +#include "g3log/g3log.hpp" + +namespace imstk +{ + +namespace plotters +{ + +/// +/// \brief Write a Matlab script to plot a given vector +/// +static void writePlotterVectorMatlab(Vectord& x, char* fileName) +{ + std::ofstream scriptFile(fileName); + + if (!scriptFile.is_open()) + { + LOG(WARNING) << "Unable to create or open the specified file for plot script!"; + return; + } + + // print x + scriptFile << "X=[\n"; + for (auto i = 0; i < x.size(); i++) + { + scriptFile << x[i] << "\n"; + } + scriptFile << "];\n"; + + // plot x + scriptFile << "plot(X, 'r-', 'LineWidth', 2, 'MarkerSize', 10);\n"; + scriptFile.close(); +} + +/// +/// \brief Write a Matlab script to plot X vs Y where X, Y are input vectors of same size +/// +static void writePlotterVecVsVecMatlab(Vectord& x, Vectord& y, char* fileName) +{ + // check if the vectors are of the same size + if (x.size() != y.size()) + { + LOG(WARNING) << "The vectors supplied for plotting are not of same size!"; + return; + } + + std::ofstream scriptFile(fileName); + + if (!scriptFile.is_open()) + { + LOG(WARNING) << "Unable to create or open the specified file for plot script!"; + return; + } + + // print x + scriptFile << "X=[\n"; + for (auto i = 0; i < x.size(); i++) + { + scriptFile << x[i] << "\n"; + } + scriptFile << "];\n"; + + // print y + scriptFile << "Y=[\n"; + for (auto i = 0; i < y.size(); i++) + { + scriptFile << y[i] << "\n"; + } + scriptFile << "];\n"; + + // plot x vs y + scriptFile << "plot(X, Y, 'r-', 'LineWidth', 2, 'MarkerSize', 10);\n"; + scriptFile.close(); +} + +/// +/// \brief Write a MatPlotlib script to plot a given vector +/// +static void writePlotterVectorMatPlotlib(Vectord& x, char* fileName) +{ + std::ofstream scriptFile(fileName); + + if (!scriptFile.is_open()) + { + LOG(WARNING) << "Unable to create or open the specified file for plot script!"; + return; + } + + // import + scriptFile << "import numpy as np\n"; + scriptFile << "import matplotlib.pyplot as plt\n"; + + // print x + scriptFile << "X=[\n"; + for (auto i = 0; i < x.size(); i++) + { + scriptFile << x[i] << ",\n"; + } + scriptFile << "];\n"; + + // plot + scriptFile << "plt.plot(X, 'ro')\n"; + + // show the plot + scriptFile << "plt.show()\n"; +} + +/// +/// \brief Write a MatPlotlib script to plot X vs Y where X, Y are input vectors of same +/// size +/// +static void writePlotterVecVsVecMatPlotlib(Vectord& x, Vectord& y, char* fileName) +{ + // check if the vectors are of the same size + if (x.size() != y.size()) + { + LOG(WARNING) << "The vectors supplied for plotting are not of same size!"; + return; + } + + std::ofstream scriptFile(fileName); + + if (!scriptFile.is_open()) + { + LOG(WARNING) << "Unable to create or open the specified file for plot script!"; + return; + } + + // import + scriptFile << "import numpy as np\n"; + scriptFile << "import matplotlib.pyplot as plt\n"; + + // print x + scriptFile << "X=[\n"; + for (auto i = 0; i < x.size(); i++) + { + scriptFile << x[i] << ",\n"; + } + scriptFile << "];\n"; + + // print y + scriptFile << "Y=[\n"; + for (auto i = 0; i < y.size(); i++) + { + scriptFile << y[i] << ",\n"; + } + scriptFile << "];\n"; + + // plot + scriptFile << "plt.plot(X, Y, 'ro')\n"; + + // show the plot + scriptFile << "plt.show()\n"; +} + +} // plotters + +} // imstk + +#endif // ifndef imstkUtils_h diff --git a/Examples/Sandbox/main.cpp b/Examples/Sandbox/main.cpp index c35a9206badb43ee3fa6975af62ba746b08f04a2..f219359ee3d592cb97a598bcede85f5da1b00179 100644 --- a/Examples/Sandbox/main.cpp +++ b/Examples/Sandbox/main.cpp @@ -49,6 +49,7 @@ // logger #include "g3log/g3log.hpp" +#include "imstkUtils.h" using namespace imstk; @@ -68,6 +69,7 @@ void testOneToOneNodalMap(); void testExtractSurfaceMesh(); void testSurfaceMeshOptimizer(); void testDeformableBody(); +void testVectorPlotters(); int main() { @@ -90,7 +92,8 @@ int main() //testExtractSurfaceMesh(); //testOneToOneNodalMap(); //testSurfaceMeshOptimizer(); - testDeformableBody(); + //testDeformableBody(); + testVectorPlotters(); return 0; } @@ -819,6 +822,12 @@ void testDeformableBody() } volTetMesh->extractSurfaceMesh(surfMesh); + imstk::StopWatch wct; + imstk::CpuTimer cput; + + wct.start(); + cput.start(); + // d. Construct a map // d.1 Construct one to one nodal map based on the above meshes @@ -829,6 +838,9 @@ void testDeformableBody() // d.2 Compute the map oneToOneNodalMap->compute(); + LOG(INFO) << "wall clock time: " << wct.getTimeElapsed() << " ms."; + LOG(INFO) << "CPU time: " << cput.getTimeElapsed() << " ms."; + // e. Scene object 1: Dragon // Configure dynamic model @@ -862,7 +874,10 @@ void testDeformableBody() // h. Add collision handling // create a nonlinear system - auto nlSystem = std::make_shared<NonLinearSystem>(dynaModel->getFunction(), dynaModel->getFunctionGradient()); + auto nlSystem = std::make_shared<NonLinearSystem>( + dynaModel->getFunction(), + dynaModel->getFunctionGradient()); + nlSystem->setUnknownVector(dynaModel->getUnknownVec()); nlSystem->setUpdateFunction(dynaModel->getUpdateFunction()); @@ -879,3 +894,22 @@ void testDeformableBody() sdk->setCurrentScene("DeformableBodyTest"); sdk->startSimulation(true); } + +void testVectorPlotters() +{ + Vectord a; + a.resize(100); + a.setConstant(1.0001); + + Vectord b; + b.resize(100); + b.setConstant(2.0); + + plotters::writePlotterVectorMatlab(a, "plotX.m"); + plotters::writePlotterVecVsVecMatlab(a, b, "plotXvsY.m"); + + plotters::writePlotterVectorMatPlotlib(a, "plotX.py"); + plotters::writePlotterVecVsVecMatPlotlib(a, b, "plotXvsY.py"); + + getchar(); +}