From cee74b95fd11a8627fddf11476b03b33092f1406 Mon Sep 17 00:00:00 2001 From: Thomas Lindemeier Date: Tue, 9 Nov 2021 11:22:58 +0100 Subject: [PATCH 1/4] updates gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 1c4a1b0..c654447 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,5 @@ # vim temp files *.sw* + +build/* From c2e1946ddd81bc4310f95bad61a23c826c3e2ea0 Mon Sep 17 00:00:00 2001 From: Thomas Lindemeier Date: Tue, 9 Nov 2021 11:23:16 +0100 Subject: [PATCH 2/4] fix warning for certain compilers --- matplotlibcpp.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46a..a929020 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -350,9 +350,9 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY // Sanity checks; comment them out or change the numpy type below if you're compiling on // a platform where they don't apply -static_assert(sizeof(long long) == 8); +static_assert(sizeof(long long) == 8, "size check failed"); template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; -static_assert(sizeof(unsigned long long) == 8); +static_assert(sizeof(unsigned long long) == 8, "size check failed"); template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; template @@ -1057,8 +1057,7 @@ template if(res) Py_DECREF(res); return res; - } - +} template bool scatter(const std::vector& x, @@ -1069,9 +1068,9 @@ bool scatter(const std::vector& x, const long fig_number=0) { detail::_interpreter::get(); - // Same as with plot_surface: We lazily load the modules here the first time - // this function is called because I'm not sure that we can assume "matplotlib - // installed" implies "mpl_toolkits installed" on all platforms, and we don't + // Same as with plot_surface: We lazily load the modules here the first time + // this function is called because I'm not sure that we can assume "matplotlib + // installed" implies "mpl_toolkits installed" on all platforms, and we don't // want to require it for people who don't need 3d plots. static PyObject *mpl_toolkitsmod = nullptr, *axis3dmod = nullptr; if (!mpl_toolkitsmod) { @@ -1468,7 +1467,7 @@ bool quiver(const std::vector& x, const std::vector& y, cons Py_DECREF(axis3d); if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); } } - + //assert sizes match up assert(x.size() == y.size() && x.size() == u.size() && u.size() == w.size() && x.size() == z.size() && x.size() == v.size() && u.size() == v.size()); @@ -1496,7 +1495,7 @@ bool quiver(const std::vector& x, const std::vector& y, cons { PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); } - + //get figure gca to enable 3d projection PyObject *fig = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, @@ -1516,7 +1515,7 @@ bool quiver(const std::vector& x, const std::vector& y, cons Py_INCREF(axis); Py_DECREF(gca); Py_DECREF(gca_kwargs); - + //plot our boys bravely, plot them strongly, plot them with a wink and clap PyObject *plot3 = PyObject_GetAttrString(axis, "quiver"); if (!plot3) throw std::runtime_error("No 3D line plot"); @@ -2655,7 +2654,7 @@ inline void rcparams(const std::map& keywords = {}) { PyDict_SetItemString(kwargs, it->first.c_str(), PyLong_FromLong(std::stoi(it->second.c_str()))); else PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str())); } - + PyObject * update = PyObject_GetAttrString(detail::_interpreter::get().s_python_function_rcparams, "update"); PyObject * res = PyObject_Call(update, args, kwargs); if(!res) throw std::runtime_error("Call to rcParams.update() failed."); From 999b9c28b9a802ec11591c717b4d132a5059e6b8 Mon Sep 17 00:00:00 2001 From: Thomas Lindemeier Date: Tue, 9 Nov 2021 11:24:59 +0100 Subject: [PATCH 3/4] do not build examples if not explicitly enabled --- CMakeLists.txt | 135 +++++++++++++++++++++++++------------------------ 1 file changed, 69 insertions(+), 66 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb2decd..3f8aca6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ set(PACKAGE_NAME matplotlib_cpp) set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PACKAGE_NAME}/cmake) +option(BUILD_EXAMPLES "Build examples" OFF) + # Library target add_library(matplotlib_cpp INTERFACE) target_include_directories(matplotlib_cpp @@ -36,72 +38,73 @@ install( ) -# Examples -add_executable(minimal examples/minimal.cpp) -target_link_libraries(minimal PRIVATE matplotlib_cpp) -set_target_properties(minimal PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(basic examples/basic.cpp) -target_link_libraries(basic PRIVATE matplotlib_cpp) -set_target_properties(basic PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(modern examples/modern.cpp) -target_link_libraries(modern PRIVATE matplotlib_cpp) -set_target_properties(modern PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(animation examples/animation.cpp) -target_link_libraries(animation PRIVATE matplotlib_cpp) -set_target_properties(animation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(nonblock examples/nonblock.cpp) -target_link_libraries(nonblock PRIVATE matplotlib_cpp) -set_target_properties(nonblock PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(xkcd examples/xkcd.cpp) -target_link_libraries(xkcd PRIVATE matplotlib_cpp) -set_target_properties(xkcd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(bar examples/bar.cpp) -target_link_libraries(bar PRIVATE matplotlib_cpp) -set_target_properties(bar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(fill_inbetween examples/fill_inbetween.cpp) -target_link_libraries(fill_inbetween PRIVATE matplotlib_cpp) -set_target_properties(fill_inbetween PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(fill examples/fill.cpp) -target_link_libraries(fill PRIVATE matplotlib_cpp) -set_target_properties(fill PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(update examples/update.cpp) -target_link_libraries(update PRIVATE matplotlib_cpp) -set_target_properties(update PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(subplot2grid examples/subplot2grid.cpp) -target_link_libraries(subplot2grid PRIVATE matplotlib_cpp) -set_target_properties(subplot2grid PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(lines3d examples/lines3d.cpp) -target_link_libraries(lines3d PRIVATE matplotlib_cpp) -set_target_properties(lines3d PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -if(Python3_NumPy_FOUND) - add_executable(surface examples/surface.cpp) - target_link_libraries(surface PRIVATE matplotlib_cpp) - set_target_properties(surface PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - - add_executable(colorbar examples/colorbar.cpp) - target_link_libraries(colorbar PRIVATE matplotlib_cpp) - set_target_properties(colorbar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - add_executable(contour examples/contour.cpp) - target_link_libraries(contour PRIVATE matplotlib_cpp) - set_target_properties(contour PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - - add_executable(spy examples/spy.cpp) - target_link_libraries(spy PRIVATE matplotlib_cpp) - set_target_properties(spy PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") -endif() - +if(BUILD_EXAMPLES) + # Examples + add_executable(minimal examples/minimal.cpp) + target_link_libraries(minimal PRIVATE matplotlib_cpp) + set_target_properties(minimal PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(basic examples/basic.cpp) + target_link_libraries(basic PRIVATE matplotlib_cpp) + set_target_properties(basic PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(modern examples/modern.cpp) + target_link_libraries(modern PRIVATE matplotlib_cpp) + set_target_properties(modern PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(animation examples/animation.cpp) + target_link_libraries(animation PRIVATE matplotlib_cpp) + set_target_properties(animation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(nonblock examples/nonblock.cpp) + target_link_libraries(nonblock PRIVATE matplotlib_cpp) + set_target_properties(nonblock PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(xkcd examples/xkcd.cpp) + target_link_libraries(xkcd PRIVATE matplotlib_cpp) + set_target_properties(xkcd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(bar examples/bar.cpp) + target_link_libraries(bar PRIVATE matplotlib_cpp) + set_target_properties(bar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(fill_inbetween examples/fill_inbetween.cpp) + target_link_libraries(fill_inbetween PRIVATE matplotlib_cpp) + set_target_properties(fill_inbetween PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(fill examples/fill.cpp) + target_link_libraries(fill PRIVATE matplotlib_cpp) + set_target_properties(fill PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(update examples/update.cpp) + target_link_libraries(update PRIVATE matplotlib_cpp) + set_target_properties(update PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(subplot2grid examples/subplot2grid.cpp) + target_link_libraries(subplot2grid PRIVATE matplotlib_cpp) + set_target_properties(subplot2grid PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(lines3d examples/lines3d.cpp) + target_link_libraries(lines3d PRIVATE matplotlib_cpp) + set_target_properties(lines3d PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + if(Python3_NumPy_FOUND) + add_executable(surface examples/surface.cpp) + target_link_libraries(surface PRIVATE matplotlib_cpp) + set_target_properties(surface PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(colorbar examples/colorbar.cpp) + target_link_libraries(colorbar PRIVATE matplotlib_cpp) + set_target_properties(colorbar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + add_executable(contour examples/contour.cpp) + target_link_libraries(contour PRIVATE matplotlib_cpp) + set_target_properties(contour PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + + add_executable(spy examples/spy.cpp) + target_link_libraries(spy PRIVATE matplotlib_cpp) + set_target_properties(spy PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + endif() +endif(BUILD_EXAMPLES) # Install headers install(FILES From 7566e3dc657f2ca88b10aa7a65f9b531e0ef4ba8 Mon Sep 17 00:00:00 2001 From: Thomas Lindemeier Date: Thu, 23 Dec 2021 10:51:58 +0100 Subject: [PATCH 4/4] Update matplotlibcpp.h --- matplotlibcpp.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index a929020..64af100 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -348,13 +348,19 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY template <> struct select_npy_type { const static NPY_TYPES type = NPY_ULONG; }; template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; -// Sanity checks; comment them out or change the numpy type below if you're compiling on -// a platform where they don't apply +#ifndef DISABLE_TYPE_SIZE_SANITY_CHECK +// Sanity checks; comment them out or change the numpy type below if you're +// compiling on a platform where they don't apply static_assert(sizeof(long long) == 8, "size check failed"); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; +template <> struct select_npy_type { + const static NPY_TYPES type = NPY_INT64; +}; static_assert(sizeof(unsigned long long) == 8, "size check failed"); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; - +template <> struct select_npy_type { + const static NPY_TYPES type = NPY_UINT64; +}; +#endif + template PyObject* get_array(const std::vector& v) {