diff --git a/utilities.cpp b/utilities.cpp new file mode 100644 index 0000000..8592f80 --- /dev/null +++ b/utilities.cpp @@ -0,0 +1,108 @@ +#include "utilities.hpp" +#include "matplot/matplot.h" + +void Utilities::createPlot(const std::string &xLabel, + const std::string &yLabel, + const std::vector &x, + const std::vector &y, + const std::vector &markerSizes, + const std::vector &c, + const std::string &saveTo) +{ + // matplot::figure(true); + matplot::xlabel(xLabel); + matplot::ylabel(yLabel); + matplot::grid(matplot::on); + matplot::scatter(x, y, markerSizes, c)->marker_face(true); + if (!saveTo.empty()) { + matplot::save(saveTo); + } +} +#ifdef POLYSCOPE_DEFINED +#include "polyscope/curve_network.h" +#include "polyscope/pick.h" +#include "polyscope/polyscope.h" +#include + +void PolyscopeInterface::mainCallback() +{ + ImGui::PushItemWidth(100); // Make ui elements 100 pixels wide, + // instead of full width. Must have + // matching PopItemWidth() below. + + for (std::function &userCallback : globalPolyscopeData.userCallbacks) { + userCallback(); + } + + ImGui::PopItemWidth(); +} + +void PolyscopeInterface::addUserCallback(const std::function &userCallback) +{ + globalPolyscopeData.userCallbacks.push_back(userCallback); +} + +void PolyscopeInterface::deinitPolyscope() +{ + if (!polyscope::state::initialized) { + return; + } + + polyscope::render::engine->shutdownImGui(); +} + +void PolyscopeInterface::init() +{ + if (polyscope::state::initialized) { + return; + } + polyscope::init(); + polyscope::options::groundPlaneEnabled = false; + polyscope::view::upDir = polyscope::view::UpDir::ZUp; + + polyscope::state::userCallback = &mainCallback; + polyscope::options::autocenterStructures = false; + polyscope::options::autoscaleStructures = false; +} + +std::pair PolyscopeInterface::getSelection() +{ + std::pair selection = polyscope::pick::getSelection(); + if (selection.first == nullptr) { + return std::make_pair(std::string(), 0); + } + return std::make_pair(selection.first->name, selection.second); +} + +void PolyscopeInterface::registerWorldAxes() +{ + PolyscopeInterface::init(); + + Eigen::MatrixX3d axesPositions(4, 3); + axesPositions.row(0) = Eigen::Vector3d(0, 0, 0); + // axesPositions.row(1) = Eigen::Vector3d(polyscope::state::lengthScale, 0, 0); + // axesPositions.row(2) = Eigen::Vector3d(0, polyscope::state::lengthScale, 0); + // axesPositions.row(3) = Eigen::Vector3d(0, 0, polyscope::state::lengthScale); + axesPositions.row(1) = Eigen::Vector3d(1, 0, 0); + axesPositions.row(2) = Eigen::Vector3d(0, 1, 0); + axesPositions.row(3) = Eigen::Vector3d(0, 0, 1); + + Eigen::MatrixX2i axesEdges(3, 2); + axesEdges.row(0) = Eigen::Vector2i(0, 1); + axesEdges.row(1) = Eigen::Vector2i(0, 2); + axesEdges.row(2) = Eigen::Vector2i(0, 3); + Eigen::MatrixX3d axesColors(3, 3); + axesColors.row(0) = Eigen::Vector3d(1, 0, 0); + axesColors.row(1) = Eigen::Vector3d(0, 1, 0); + axesColors.row(2) = Eigen::Vector3d(0, 0, 1); + + const std::string worldAxesName = "World Axes"; + polyscope::registerCurveNetwork(worldAxesName, axesPositions, axesEdges); + polyscope::getCurveNetwork(worldAxesName)->setRadius(0.0001, false); + const std::string worldAxesColorName = worldAxesName + " Color"; + polyscope::getCurveNetwork(worldAxesName) + ->addEdgeColorQuantity(worldAxesColorName, axesColors) + ->setEnabled(true); +} + +#endif diff --git a/utilities.hpp b/utilities.hpp index 6e752ff..4966135 100755 --- a/utilities.hpp +++ b/utilities.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #define GET_VARIABLE_NAME(Variable) (#Variable) @@ -323,6 +324,7 @@ inline std::vector fromEigenMatrix(const Eigen::MatrixXd &m) inline std::filesystem::path getFilepathWithExtension(const std::filesystem::path &folderPath, const std::string &extension) { + assert(std::filesystem::exists(folderPath)); for (const std::filesystem::directory_entry &dirEntry : std::filesystem::directory_iterator(folderPath)) { if (dirEntry.is_regular_file() && std::filesystem::path(dirEntry).extension() == extension) { @@ -333,13 +335,17 @@ inline std::filesystem::path getFilepathWithExtension(const std::filesystem::pat return ""; } +void createPlot(const std::string &xLabel, + const std::string &yLabel, + const std::vector &x, + const std::vector &y, + const std::vector &markerSizes, + const std::vector &c, + const std::string &saveTo = {}); + } // namespace Utilities #ifdef POLYSCOPE_DEFINED -#include "polyscope/curve_network.h" -#include "polyscope/pick.h" -#include "polyscope/polyscope.h" -#include namespace PolyscopeInterface { inline struct GlobalPolyscopeData @@ -347,86 +353,17 @@ inline struct GlobalPolyscopeData std::vector> userCallbacks; } globalPolyscopeData; -inline void mainCallback() -{ - ImGui::PushItemWidth(100); // Make ui elements 100 pixels wide, - // instead of full width. Must have - // matching PopItemWidth() below. +void mainCallback(); - for (std::function &userCallback : globalPolyscopeData.userCallbacks) { - userCallback(); - } +void addUserCallback(const std::function &userCallback); - ImGui::PopItemWidth(); -} +void deinitPolyscope(); -inline void addUserCallback(const std::function &userCallback) -{ - globalPolyscopeData.userCallbacks.push_back(userCallback); -} - -inline void deinitPolyscope() -{ - if (!polyscope::state::initialized) { - return; - } - - polyscope::render::engine->shutdownImGui(); -} - -inline void init() -{ - if (polyscope::state::initialized) { - return; - } - polyscope::init(); - polyscope::options::groundPlaneEnabled = false; - polyscope::view::upDir = polyscope::view::UpDir::ZUp; - - polyscope::state::userCallback = &mainCallback; - polyscope::options::autocenterStructures = false; - polyscope::options::autoscaleStructures = false; -} +void init(); using PolyscopeLabel = std::string; -inline std::pair getSelection() -{ - std::pair selection = polyscope::pick::getSelection(); - if (selection.first == nullptr) { - return std::make_pair(std::string(), 0); - } - return std::make_pair(selection.first->name, selection.second); -} +std::pair getSelection(); -inline void registerWorldAxes() -{ - PolyscopeInterface::init(); - - Eigen::MatrixX3d axesPositions(4, 3); - axesPositions.row(0) = Eigen::Vector3d(0, 0, 0); - // axesPositions.row(1) = Eigen::Vector3d(polyscope::state::lengthScale, 0, 0); - // axesPositions.row(2) = Eigen::Vector3d(0, polyscope::state::lengthScale, 0); - // axesPositions.row(3) = Eigen::Vector3d(0, 0, polyscope::state::lengthScale); - axesPositions.row(1) = Eigen::Vector3d(1, 0, 0); - axesPositions.row(2) = Eigen::Vector3d(0, 1, 0); - axesPositions.row(3) = Eigen::Vector3d(0, 0, 1); - - Eigen::MatrixX2i axesEdges(3, 2); - axesEdges.row(0) = Eigen::Vector2i(0, 1); - axesEdges.row(1) = Eigen::Vector2i(0, 2); - axesEdges.row(2) = Eigen::Vector2i(0, 3); - Eigen::MatrixX3d axesColors(3, 3); - axesColors.row(0) = Eigen::Vector3d(1, 0, 0); - axesColors.row(1) = Eigen::Vector3d(0, 1, 0); - axesColors.row(2) = Eigen::Vector3d(0, 0, 1); - - const std::string worldAxesName = "World Axes"; - polyscope::registerCurveNetwork(worldAxesName, axesPositions, axesEdges); - polyscope::getCurveNetwork(worldAxesName)->setRadius(0.0001, false); - const std::string worldAxesColorName = worldAxesName + " Color"; - polyscope::getCurveNetwork(worldAxesName) - ->addEdgeColorQuantity(worldAxesColorName, axesColors) - ->setEnabled(true); -} +void registerWorldAxes(); } // namespace PolyscopeInterface #endif