23#ifndef LSST_SPHGEOM_PYTHON_UTILS_H_
24#define LSST_SPHGEOM_PYTHON_UTILS_H_
26#include "pybind11/pybind11.h"
40inline ptrdiff_t convertIndex(ptrdiff_t len, pybind11::int_ i) {
41 auto j =
static_cast<ptrdiff_t
>(i);
42 if ((j == -1 && PyErr_Occurred()) || j < -len || j >= len) {
44 throw pybind11::index_error(
45 pybind11::str(
"Index {} not in range({}, {})")
46 .format(i, -len, len));
48 return (j < 0) ? j + len : j;
53inline pybind11::bytes encode(Region
const &self) {
54 std::vector<uint8_t> bytes = self.encode();
55 return pybind11::bytes(
reinterpret_cast<char const *
>(bytes.data()),
61std::unique_ptr<R> decode(pybind11::bytes bytes) {
62 uint8_t
const *buffer =
reinterpret_cast<uint8_t
const *
>(
63 PYBIND11_BYTES_AS_STRING(bytes.ptr()));
64 size_t n =
static_cast<size_t>(PYBIND11_BYTES_SIZE(bytes.ptr()));
65 return R::decode(buffer, n);
75inline std::vector<std::unique_ptr<Region>> convert_region_sequence(S
const & seq) {
76 std::vector<std::unique_ptr<Region>> result;
77 result.reserve(seq.size());
78 for (pybind11::handle py_region : seq) {
79 result.push_back(py_region.cast<Region
const &>().clone());
This file defines an interface for spherical regions.