13 #ifndef GeometryHelpers_h
14 #define GeometryHelpers_h
19 #include <pybind11/numpy.h>
24 namespace py = pybind11;
89 template <
typename PyClass,
typename CppClass,
bool BindNdim = true>
93 cls.def(
"get_spacing",
95 py::arg(
"time_step") = 0,
96 R
"(Return the voxel spacing at the given time step.
99 time_step: Time-step index (default 0).
102 A 3-tuple ``(sx, sy, sz)`` in world units.
104 cls.def("set_spacing",
106 py::arg(
"spacing"), py::arg(
"time_step") = 0,
107 R
"(Set the voxel spacing at the given time step.
110 spacing: 3-element sequence ``(sx, sy, sz)`` in world units.
111 time_step: Time-step index (default 0).
113 cls.def("get_origin",
115 py::arg(
"time_step") = 0,
116 R
"(Return the origin at the given time step.
119 time_step: Time-step index (default 0).
122 A 3-tuple ``(ox, oy, oz)`` in world coordinates.
124 cls.def("set_origin",
126 py::arg(
"origin"), py::arg(
"time_step") = 0,
127 R
"(Set the origin at the given time step.
130 origin: 3-element sequence ``(ox, oy, oz)`` in world coordinates.
131 time_step: Time-step index (default 0).
133 cls.def("get_direction",
135 py::arg(
"time_step") = 0,
136 R
"(Return the direction cosine matrix at the given time step.
139 time_step: Time-step index (default 0).
142 A 3x3 NumPy array of doubles.
144 cls.def("set_direction",
145 [](CppClass& obj, py::array_t<double, py::array::c_style | py::array::forcecast> d,
mitk::TimeStepType t) {
148 py::arg(
"direction"), py::arg(
"time_step") = 0,
149 R
"(Set the direction cosine matrix at the given time step.
152 direction: 3x3 array-like (NumPy array, nested list, etc.).
153 time_step: Time-step index (default 0).
155 cls.def("get_direction_cosines",
157 py::arg(
"time_step") = 0,
158 R
"(Return the direction cosines as a flat 9-element tuple.
161 time_step: Time-step index (default 0).
164 A 9-element sequence in row-major order.
166 cls.def("get_geometry",
168 py::arg(
"time_step") = 0,
169 R
"(Return the full :py:class:`BaseGeometry` for the given time step.
172 time_step: Time-step index (default 0).
175 The spatial geometry at the requested time step.
178 cls.def_property("spacing",
179 [](
const CppClass& obj) {
return GetSpacing(obj.GetTimeGeometry(), 0); },
180 [](CppClass& obj,
const std::array<double, 3>& s) {
SetSpacing(obj.GetTimeGeometry(), s, 0); },
181 R
"(Voxel spacing at time step 0 as a 3-tuple ``(sx, sy, sz)``.)");
182 cls.def_property("origin",
183 [](
const CppClass& obj) {
return GetOrigin(obj.GetTimeGeometry(), 0); },
184 [](CppClass& obj,
const std::array<double, 3>& o) {
SetOrigin(obj.GetTimeGeometry(), o, 0); },
185 R
"(Origin at time step 0 as a 3-tuple ``(ox, oy, oz)``.)");
186 cls.def_property("direction",
187 [](
const CppClass& obj) {
return GetDirection(obj.GetTimeGeometry(), 0); },
188 [](CppClass& obj, py::array_t<double, py::array::c_style | py::array::forcecast> d) {
SetDirection(obj.GetTimeGeometry(), d, 0); },
189 R
"(Direction cosine matrix at time step 0 as a 3x3 NumPy array.)");
190 cls.def_property_readonly("direction_cosines",
192 "Direction cosines at time step 0 as a flat 9-element tuple.");
193 if constexpr (BindNdim)
195 cls.def_property_readonly(
"ndim",
196 [](
const CppClass& obj) {
return obj.GetDimension(); },
197 "Number of dimensions of the object.");
199 cls.def_property_readonly(
"time_steps",
200 [](
const CppClass& obj) {
return obj.GetTimeGeometry()->CountTimeSteps(); },
201 "Number of time steps.");
202 cls.def_property_readonly(
"time_geometry",
203 [](CppClass& obj) {
return mitk::TimeGeometry::Pointer(obj.GetTimeGeometry()); },
204 "The full :py:class:`TimeGeometry` of this object.");
py::array_t< double > GetDirection(const mitk::TimeGeometry *tg, mitk::TimeStepType t)
Returns the direction cosine matrix (3x3) at the given time step.
void SetOrigin(mitk::TimeGeometry *tg, const std::array< double, 3 > &origin, mitk::TimeStepType t)
Sets the origin at the given time step.
mitk::BaseGeometry::Pointer GetGeometryForTimeStep(mitk::TimeGeometry *tg, mitk::TimeStepType timeStep)
Returns the mutable BaseGeometry for the given time step.
void SetSpacing(mitk::TimeGeometry *tg, const std::array< double, 3 > &spacing, mitk::TimeStepType t)
Sets the voxel spacing at the given time step.
std::tuple< double, double, double > GetSpacing(const mitk::TimeGeometry *tg, mitk::TimeStepType t)
Returns the voxel spacing (x, y, z) at the given time step.
std::tuple< double, double, double > GetOrigin(const mitk::TimeGeometry *tg, mitk::TimeStepType t)
Returns the origin (x, y, z) at the given time step.
std::array< double, 9 > GetDirectionCosines(const mitk::TimeGeometry *tg, mitk::TimeStepType t)
Returns the direction cosines as a flat 9-element array at the given time step.
mitk::BaseGeometry::ConstPointer GetConstGeometryForTimeStep(const mitk::TimeGeometry *tg, mitk::TimeStepType timeStep)
Returns the const BaseGeometry for the given time step.
py::array_t< double > ExtractDirection(const mitk::BaseGeometry &geom)
Extracts the direction cosine matrix (3x3) from a BaseGeometry.
void BindGeometryAccessors(PyClass &cls)
Binds geometry accessors (per-time-step methods and convenience properties) onto an existing pybind11...
void SetDirection(mitk::TimeGeometry *tg, py::array_t< double, py::array::c_style|py::array::forcecast > direction, mitk::TimeStepType t)
Sets the direction cosine matrix at the given time step.
Abstract base class describing the geometry of a data object.
std::size_t TimeStepType
Type for time step indices (non-negative integers starting from 0).