Skip to content

Commit

Permalink
add bindings for grid, log #15
Browse files Browse the repository at this point in the history
  • Loading branch information
timdewhirst committed Aug 12, 2022
1 parent add7a02 commit 2ef770a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ if (BUILD_PYBIND)
pybind11_add_module(
pyopenpivcore
core/module.cpp
core/grid.cpp
core/log.cpp
core/point.cpp
core/rect.cpp
core/size.cpp
core/vector.cpp
)

find_package(fmt CONFIG REQUIRED)
target_link_libraries(
pyopenpivcore
PRIVATE fmt::fmt-header-only
PRIVATE openpivcore)

if (UNIX AND APPLE)
Expand Down
33 changes: 33 additions & 0 deletions pybind/core/grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

// openpiv
#include "core/grid.h"
#include "core/rect.h"
#include "core/size.h"

using namespace openpiv;
using namespace openpiv::core;

// pybind
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

using namespace pybind11::literals;

// std
#include <sstream>

namespace py = pybind11;

void add_grid(py::module& m)
{
m.def("generate_cartesian_grid_with_relative_offset",
static_cast<std::vector<rect>(*)(const size&, const core::size&, double)>(&generate_cartesian_grid),
"image_size"_a,
"interrogation_size"_a,
"percentage_offset"_a);
m.def("generate_cartesian_grid_with_fixed_offset",
static_cast<std::vector<rect>(*)(const size&, const core::size&, std::array< uint32_t, 2 >)>(&generate_cartesian_grid),
"image_size"_a,
"interrogation_size"_a,
"offsets"_a);
}
55 changes: 55 additions & 0 deletions pybind/core/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

// openpiv
#include "core/log.h"

using namespace openpiv;
using namespace openpiv::core;
using namespace openpiv::core::logger;

// pybind
#include <pybind11/pybind11.h>
#include <pybind11/functional.h>
#include <pybind11/stl.h>

// std
#include <functional>
#include <sstream>
#include <string>

namespace py = pybind11;

Logger::sink_id_t add_sink_(Logger::sink_t sink)
{
// wrap the passed function in a GIL lock as the sink is called
// from a logging thread in C++
auto gil_sink = [sink](Level level, const std::string& str) -> bool {
pybind11::gil_scoped_acquire lock{};
return sink(level, str);
};

auto result = Logger::instance().add_sink(gil_sink);
warn("python logging sink added: ensure this is removed before this module is unloaded by calling remove_sink");

return result;
}

void add_log(py::module& m)
{
py::enum_<Level>(m, "Level")
.value("NONE", Level::NONE)
.value("FATAL", Level::FATAL)
.value("ERROR", Level::ERROR)
.value("WARN", Level::WARN)
.value("INFO", Level::INFO)
.value("DEBUG", Level::DEBUG)
.value("TEST", Level::TEST)
.export_values();

m.def("add_sink", &add_sink_);
m.def("remove_sink",
[](Logger::sink_id_t id) -> bool
{
return Logger::instance().remove_sink(id);
});
m.def("test_log", [](){ debug("hello, world! {}", 3.141); });
}
4 changes: 4 additions & 0 deletions pybind/core/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace py = pybind11;

void add_grid(py::module &);
void add_log(py::module &);
void add_point(py::module &);
void add_rect(py::module &);
void add_size(py::module &);
Expand All @@ -23,6 +25,8 @@ PYBIND11_MODULE(pyopenpivcore, m) {
)pbdoc";

// add each binding chunk here
add_grid(m);
add_log(m);
add_point(m);
add_rect(m);
add_size(m);
Expand Down

0 comments on commit 2ef770a

Please sign in to comment.