30#ifndef LSST_SPHGEOM_PYTHON_UTILS_H_
31#define LSST_SPHGEOM_PYTHON_UTILS_H_
33#include "pybind11/pybind11.h"
47inline ptrdiff_t convertIndex(ptrdiff_t len, pybind11::int_ i) {
48 auto j =
static_cast<ptrdiff_t
>(i);
49 if ((j == -1 && PyErr_Occurred()) || j < -len || j >= len) {
51 throw pybind11::index_error(
52 pybind11::str(
"Index {} not in range({}, {})")
53 .format(i, -len, len));
55 return (j < 0) ? j + len : j;
60inline pybind11::bytes encode(Region
const &self) {
61 std::vector<uint8_t> bytes = self.encode();
62 return pybind11::bytes(
reinterpret_cast<char const *
>(bytes.data()),
68std::unique_ptr<R> decode(pybind11::bytes bytes) {
69 uint8_t
const *buffer =
reinterpret_cast<uint8_t
const *
>(
70 PYBIND11_BYTES_AS_STRING(bytes.ptr()));
71 size_t n =
static_cast<size_t>(PYBIND11_BYTES_SIZE(bytes.ptr()));
72 return R::decode(buffer, n);
82inline std::vector<std::unique_ptr<Region>> convert_region_sequence(S
const & seq) {
83 std::vector<std::unique_ptr<Region>> result;
84 result.reserve(seq.size());
85 for (pybind11::handle py_region : seq) {
86 result.push_back(py_region.cast<Region
const &>().clone());
This file defines an interface for spherical regions.