Skip to content
Snippets Groups Projects
Commit 9e7b7792 authored by Sreekanth Arikatla's avatar Sreekanth Arikatla
Browse files

ENH: Adds basic plot generator scripts for vectors

Adds basic plot generator scripts for vectors for Matlab and Matlibplot
parent a5f419ce
No related branches found
No related tags found
No related merge requests found
/*=========================================================================
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
......@@ -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();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment