Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
GeometryHelpers.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef GeometryHelpers_h
14 #define GeometryHelpers_h
15 
16 #include <mitkBaseGeometry.h>
17 #include <mitkTimeGeometry.h>
18 
19 #include <pybind11/numpy.h>
20 
21 #include <array>
22 #include <tuple>
23 
24 namespace py = pybind11;
25 
26 // ---------------------------------------------------------------------------
27 // Shared geometry accessors for Image and MultiLabelSegmentation bindings
28 // ---------------------------------------------------------------------------
29 
33 mitk::BaseGeometry::Pointer GetGeometryForTimeStep(mitk::TimeGeometry* tg, mitk::TimeStepType timeStep);
34 
38 mitk::BaseGeometry::ConstPointer GetConstGeometryForTimeStep(const mitk::TimeGeometry* tg, mitk::TimeStepType timeStep);
39 
43 std::tuple<double, double, double> GetSpacing(const mitk::TimeGeometry* tg, mitk::TimeStepType t);
44 
49 void SetSpacing(mitk::TimeGeometry* tg, const std::array<double, 3>& spacing, mitk::TimeStepType t);
50 
54 std::tuple<double, double, double> GetOrigin(const mitk::TimeGeometry* tg, mitk::TimeStepType t);
55 
59 void SetOrigin(mitk::TimeGeometry* tg, const std::array<double, 3>& origin, mitk::TimeStepType t);
60 
63 py::array_t<double> ExtractDirection(const mitk::BaseGeometry& geom);
64 
68 py::array_t<double> GetDirection(const mitk::TimeGeometry* tg, mitk::TimeStepType t);
69 
74 void SetDirection(mitk::TimeGeometry* tg, py::array_t<double, py::array::c_style | py::array::forcecast> direction, mitk::TimeStepType t);
75 
79 std::array<double, 9> GetDirectionCosines(const mitk::TimeGeometry* tg, mitk::TimeStepType t);
80 
89 template <typename PyClass, typename CppClass, bool BindNdim = true>
90 void BindGeometryAccessors(PyClass& cls)
91 {
92  // Per-time-step accessors
93  cls.def("get_spacing",
94  [](const CppClass& obj, mitk::TimeStepType t) { return GetSpacing(obj.GetTimeGeometry(), t); },
95  py::arg("time_step") = 0,
96  R"(Return the voxel spacing at the given time step.
97 
98 Args:
99  time_step: Time-step index (default 0).
100 
101 Returns:
102  A 3-tuple ``(sx, sy, sz)`` in world units.
103 )");
104  cls.def("set_spacing",
105  [](CppClass& obj, const std::array<double, 3>& s, mitk::TimeStepType t) { SetSpacing(obj.GetTimeGeometry(), s, t); },
106  py::arg("spacing"), py::arg("time_step") = 0,
107  R"(Set the voxel spacing at the given time step.
108 
109 Args:
110  spacing: 3-element sequence ``(sx, sy, sz)`` in world units.
111  time_step: Time-step index (default 0).
112 )");
113  cls.def("get_origin",
114  [](const CppClass& obj, mitk::TimeStepType t) { return GetOrigin(obj.GetTimeGeometry(), t); },
115  py::arg("time_step") = 0,
116  R"(Return the origin at the given time step.
117 
118 Args:
119  time_step: Time-step index (default 0).
120 
121 Returns:
122  A 3-tuple ``(ox, oy, oz)`` in world coordinates.
123 )");
124  cls.def("set_origin",
125  [](CppClass& obj, const std::array<double, 3>& o, mitk::TimeStepType t) { SetOrigin(obj.GetTimeGeometry(), o, t); },
126  py::arg("origin"), py::arg("time_step") = 0,
127  R"(Set the origin at the given time step.
128 
129 Args:
130  origin: 3-element sequence ``(ox, oy, oz)`` in world coordinates.
131  time_step: Time-step index (default 0).
132 )");
133  cls.def("get_direction",
134  [](const CppClass& obj, mitk::TimeStepType t) { return GetDirection(obj.GetTimeGeometry(), t); },
135  py::arg("time_step") = 0,
136  R"(Return the direction cosine matrix at the given time step.
137 
138 Args:
139  time_step: Time-step index (default 0).
140 
141 Returns:
142  A 3x3 NumPy array of doubles.
143 )");
144  cls.def("set_direction",
145  [](CppClass& obj, py::array_t<double, py::array::c_style | py::array::forcecast> d, mitk::TimeStepType t) {
146  SetDirection(obj.GetTimeGeometry(), d, t);
147  },
148  py::arg("direction"), py::arg("time_step") = 0,
149  R"(Set the direction cosine matrix at the given time step.
150 
151 Args:
152  direction: 3x3 array-like (NumPy array, nested list, etc.).
153  time_step: Time-step index (default 0).
154 )");
155  cls.def("get_direction_cosines",
156  [](const CppClass& obj, mitk::TimeStepType t) { return GetDirectionCosines(obj.GetTimeGeometry(), t); },
157  py::arg("time_step") = 0,
158  R"(Return the direction cosines as a flat 9-element tuple.
159 
160 Args:
161  time_step: Time-step index (default 0).
162 
163 Returns:
164  A 9-element sequence in row-major order.
165 )");
166  cls.def("get_geometry",
167  [](CppClass& obj, mitk::TimeStepType t) { return GetGeometryForTimeStep(obj.GetTimeGeometry(), t); },
168  py::arg("time_step") = 0,
169  R"(Return the full :py:class:`BaseGeometry` for the given time step.
170 
171 Args:
172  time_step: Time-step index (default 0).
173 
174 Returns:
175  The spatial geometry at the requested time step.
176 )");
177 
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",
191  [](const CppClass& obj) { return GetDirectionCosines(obj.GetTimeGeometry(), 0); },
192  "Direction cosines at time step 0 as a flat 9-element tuple.");
193  if constexpr (BindNdim)
194  {
195  cls.def_property_readonly("ndim",
196  [](const CppClass& obj) { return obj.GetDimension(); },
197  "Number of dimensions of the object.");
198  }
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.");
205 }
206 
207 #endif
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).