generated from cubao/cmake_example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* misc update * add hsv to rgb, random stroke * next rgb * assert randoms * clear ubodt * match, lots of work to do * more types * fix build * more bindings from nano-fmm * some code from geobuf-cpp * misc * fix * test edge case --------- Co-authored-by: TANG ZHIXIONG <[email protected]>
- Loading branch information
1 parent
052b760
commit 3844783
Showing
18 changed files
with
1,321 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#pragma once | ||
|
||
// migrated from | ||
// https://github.com/cubao/geobuf-cpp/blob/dev/src/geobuf/pybind11_helpers.hpp | ||
|
||
#include <pybind11/iostream.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/stl_bind.h> | ||
|
||
#include "nano_fmm/types.hpp" | ||
#include "nano_fmm/rapidjson_helpers.hpp" | ||
|
||
namespace nano_fmm | ||
{ | ||
namespace py = pybind11; | ||
using namespace pybind11::literals; | ||
using rvp = py::return_value_policy; | ||
|
||
inline RapidjsonValue __py_int_to_rapidjson(const py::handle &obj) | ||
{ | ||
try { | ||
auto num = obj.cast<int64_t>(); | ||
if (py::int_(num).equal(obj)) { | ||
return RapidjsonValue(num); | ||
} | ||
} catch (...) { | ||
} | ||
try { | ||
auto num = obj.cast<uint64_t>(); | ||
if (py::int_(num).equal(obj)) { | ||
return RapidjsonValue(num); | ||
} | ||
} catch (...) { | ||
} | ||
throw std::runtime_error( | ||
"failed to convert to rapidjson, invalid integer: " + | ||
py::repr(obj).cast<std::string>()); | ||
} | ||
|
||
inline RapidjsonValue to_rapidjson(const py::handle &obj, | ||
RapidjsonAllocator &allocator) | ||
{ | ||
if (obj.ptr() == nullptr || obj.is_none()) { | ||
return {}; | ||
} | ||
if (py::isinstance<py::bool_>(obj)) { | ||
return RapidjsonValue(obj.cast<bool>()); | ||
} | ||
if (py::isinstance<py::int_>(obj)) { | ||
return __py_int_to_rapidjson(obj); | ||
} | ||
if (py::isinstance<py::float_>(obj)) { | ||
return RapidjsonValue(obj.cast<double>()); | ||
} | ||
if (py::isinstance<py::bytes>(obj)) { | ||
// https://github.com/pybind/pybind11_json/blob/master/include/pybind11_json/pybind11_json.hpp#L112 | ||
py::module base64 = py::module::import("base64"); | ||
auto str = base64.attr("b64encode")(obj) | ||
.attr("decode")("utf-8") | ||
.cast<std::string>(); | ||
return RapidjsonValue(str.data(), str.size(), allocator); | ||
} | ||
if (py::isinstance<py::str>(obj)) { | ||
auto str = obj.cast<std::string>(); | ||
return RapidjsonValue(str.data(), str.size(), allocator); | ||
} | ||
if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj)) { | ||
RapidjsonValue arr(rapidjson::kArrayType); | ||
for (const py::handle &value : obj) { | ||
arr.PushBack(to_rapidjson(value, allocator), allocator); | ||
} | ||
return arr; | ||
} | ||
if (py::isinstance<py::dict>(obj)) { | ||
RapidjsonValue kv(rapidjson::kObjectType); | ||
for (const py::handle &key : obj) { | ||
auto k = py::str(key).cast<std::string>(); | ||
kv.AddMember(RapidjsonValue(k.data(), k.size(), allocator), | ||
to_rapidjson(obj[key], allocator), allocator); | ||
} | ||
return kv; | ||
} | ||
if (py::isinstance<RapidjsonValue>(obj)) { | ||
auto ptr = py::cast<const RapidjsonValue *>(obj); | ||
return deepcopy(*ptr, allocator); | ||
} | ||
throw std::runtime_error( | ||
"to_rapidjson not implemented for this type of object: " + | ||
py::repr(obj).cast<std::string>()); | ||
} | ||
|
||
inline RapidjsonValue to_rapidjson(const py::handle &obj) | ||
{ | ||
RapidjsonAllocator allocator; | ||
return to_rapidjson(obj, allocator); | ||
} | ||
|
||
inline py::object to_python(const RapidjsonValue &j) | ||
{ | ||
if (j.IsNull()) { | ||
return py::none(); | ||
} else if (j.IsBool()) { | ||
return py::bool_(j.GetBool()); | ||
} else if (j.IsNumber()) { | ||
if (j.IsUint64()) { | ||
return py::int_(j.GetUint64()); | ||
} else if (j.IsInt64()) { | ||
return py::int_(j.GetInt64()); | ||
} else { | ||
return py::float_(j.GetDouble()); | ||
} | ||
} else if (j.IsString()) { | ||
return py::str(std::string{j.GetString(), j.GetStringLength()}); | ||
} else if (j.IsArray()) { | ||
py::list ret; | ||
for (const auto &e : j.GetArray()) { | ||
ret.append(to_python(e)); | ||
} | ||
return ret; | ||
} else { | ||
py::dict ret; | ||
for (auto &m : j.GetObject()) { | ||
ret[py::str( | ||
std::string{m.name.GetString(), m.name.GetStringLength()})] = | ||
to_python(m.value); | ||
} | ||
return ret; | ||
} | ||
} | ||
} // namespace nano_fmm | ||
|
||
#ifndef BIND_PY_FLUENT_ATTRIBUTE | ||
#define BIND_PY_FLUENT_ATTRIBUTE(Klass, type, var) \ | ||
.def( \ | ||
#var, [](Klass &self) -> type & { return self.var; }, \ | ||
rvp::reference_internal) \ | ||
.def( \ | ||
#var, \ | ||
[](Klass &self, const type &v) -> Klass & { \ | ||
self.var = v; \ | ||
return self; \ | ||
}, \ | ||
rvp::reference_internal) | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <pybind11/eigen.h> | ||
#include <pybind11/iostream.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/stl_bind.h> | ||
|
||
#include "nano_fmm/randoms.hpp" | ||
|
||
namespace nano_fmm | ||
{ | ||
namespace py = pybind11; | ||
using namespace pybind11::literals; | ||
using rvp = py::return_value_policy; | ||
|
||
void bind_randoms(py::module &m) | ||
{ | ||
m // | ||
.def("hsv2rgb", &hsv_to_rgb, "h"_a, "s"_a, "v"_a) | ||
// | ||
; | ||
py::class_<RandomColor>(m, "RandomColor", py::module_local()) // | ||
.def(py::init<bool>(), py::kw_only(), "on_black"_a = true) | ||
.def(py::init<int, bool>(), "seed"_a, py::kw_only(), | ||
"on_black"_a = true) | ||
// | ||
.def("next_rgb", &RandomColor::next_rgb) | ||
.def("next_hex", &RandomColor::next_hex) | ||
// | ||
; | ||
} | ||
} // namespace nano_fmm |
Oops, something went wrong.