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