Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
PropertyConversionUtils.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 
37 #pragma once
38 
39 #include <mitkBaseProperty.h>
40 #include <mitkColorProperty.h>
41 #include <mitkProperties.h>
42 #include <mitkStringProperty.h>
43 #include <pybind11/pybind11.h>
44 
45 namespace py = pybind11;
46 
47 namespace mitk
48 {
49  namespace python
50  {
51 
67  inline py::object propertyToPythonValue(const mitk::BaseProperty &prop)
68  {
69  if (const mitk::StringProperty *strProp = dynamic_cast<const mitk::StringProperty *>(&prop))
70  {
71  return py::cast(strProp->GetValue());
72  }
73  else if (const mitk::BoolProperty *boolProp = dynamic_cast<const mitk::BoolProperty *>(&prop))
74  {
75  return py::cast(boolProp->GetValue());
76  }
77  else if (const mitk::IntProperty *intProp = dynamic_cast<const mitk::IntProperty *>(&prop))
78  {
79  return py::cast(intProp->GetValue());
80  }
81  else if (const mitk::FloatProperty *floatProp = dynamic_cast<const mitk::FloatProperty *>(&prop))
82  {
83  return py::cast(floatProp->GetValue());
84  }
85  else if (const mitk::DoubleProperty *doubleProp = dynamic_cast<const mitk::DoubleProperty *>(&prop))
86  {
87  return py::cast(doubleProp->GetValue());
88  }
89  else if (const mitk::ColorProperty *colorProp = dynamic_cast<const mitk::ColorProperty *>(&prop))
90  {
91  auto color = colorProp->GetColor();
92  return py::make_tuple(color[0], color[1], color[2]);
93  }
94 
95  // For unsupported types, return the property itself
96  return py::cast(&prop, py::return_value_policy::reference);
97  }
98 
99  // Convert BaseProperty to dict for JSON serialization
100  inline py::dict propertyToDict(const mitk::BaseProperty &prop)
101  {
102  py::dict result;
103  result["type"] = prop.GetNameOfClass();
104 
105  // Add value based on property type
106  if (const mitk::StringProperty *strProp = dynamic_cast<const mitk::StringProperty *>(&prop))
107  {
108  result["value"] = strProp->GetValue();
109  }
110  else if (const mitk::BoolProperty *boolProp = dynamic_cast<const mitk::BoolProperty *>(&prop))
111  {
112  result["value"] = boolProp->GetValue();
113  }
114  else if (const mitk::IntProperty *intProp = dynamic_cast<const mitk::IntProperty *>(&prop))
115  {
116  result["value"] = intProp->GetValue();
117  }
118  else if (const mitk::FloatProperty *floatProp = dynamic_cast<const mitk::FloatProperty *>(&prop))
119  {
120  result["value"] = floatProp->GetValue();
121  }
122  else if (const mitk::DoubleProperty *doubleProp = dynamic_cast<const mitk::DoubleProperty *>(&prop))
123  {
124  result["value"] = doubleProp->GetValue();
125  }
126  else if (const mitk::ColorProperty *colorProp = dynamic_cast<const mitk::ColorProperty *>(&prop))
127  {
128  auto color = colorProp->GetColor();
129  result["value"] = py::make_tuple(color[0], color[1], color[2]);
130  }
131 
132  return result;
133  }
134 
135  // Convert dict to BaseProperty for JSON deserialization
136  inline mitk::BaseProperty::Pointer dictToProperty(const py::dict &d)
137  {
138  std::string type = d["type"].cast<std::string>();
139 
140  if (type == "StringProperty")
141  {
142  return mitk::StringProperty::New(d["value"].cast<std::string>());
143  }
144  else if (type == "BoolProperty")
145  {
146  return mitk::BoolProperty::New(d["value"].cast<bool>());
147  }
148  else if (type == "IntProperty")
149  {
150  return mitk::IntProperty::New(d["value"].cast<int>());
151  }
152  else if (type == "FloatProperty")
153  {
154  return mitk::FloatProperty::New(d["value"].cast<float>());
155  }
156  else if (type == "DoubleProperty")
157  {
158  return mitk::DoubleProperty::New(d["value"].cast<double>());
159  }
160  else if (type == "ColorProperty")
161  {
162  auto value = d["value"].cast<py::tuple>();
163  mitk::Color color;
164  color[0] = value[0].cast<float>();
165  color[1] = value[1].cast<float>();
166  color[2] = value[2].cast<float>();
167  return mitk::ColorProperty::New(color);
168  }
169 
170  throw std::runtime_error("Unsupported property type: " + type);
171  }
172 
173  } // namespace python
174 } // namespace mitk
Abstract base class for properties.
Property for storing RGB color values.
static Pointer New()
Property for storing string values.
static Pointer New()
Declares commonly used GenericProperty specializations for MITK.
py::object propertyToPythonValue(const mitk::BaseProperty &prop)
Converts a BaseProperty to its Python native value.
py::dict propertyToDict(const mitk::BaseProperty &prop)
mitk::BaseProperty::Pointer dictToProperty(const py::dict &d)
Find image slices visible on a given plane.
itk::RGBPixel< float > Color
Standard RGB color typedef using float components.