1818#ifndef itkElastixWasmParameterObject_h
1919#define itkElastixWasmParameterObject_h
2020
21- #include < map>
22- #include < string>
23- #include < variant>
24- #include < vector>
21+ #include < map>
22+ #include < string>
23+ #include < variant>
24+ #include < vector>
2525
26+ #include " glaze/glaze.hpp"
27+
28+ #include " elxParameterObject.h"
29+
30+ #include " itkNumberToString.h"
2631 namespace itk
2732 {
2833 namespace wasm
2934 {
3035
3136/* *
32- * @brief Type used to represent an elastix ParameterKey in ITK-Wasm pipelines
33- * and for serializing elastix parameter files with glaze.
34- *
35- * This is the name of the parameter, e.g. "NumberOfResolutions"
36- */
37+ * @brief Type used to represent an elastix ParameterKey in ITK-Wasm pipelines
38+ * and for serializing elastix parameter files with glaze.
39+ *
40+ * This is the name of the parameter, e.g. "NumberOfResolutions"
41+ */
3742using ParameterKey = std::string;
3843
39- /* *
40- * @brief Type used to represent an elastix ParameterValue in ITK-Wasm pipelines
41- * and for serializing elastix parameter files with glaze.
42- *
43- * This is the type cast string held in a ParameterFile
44- */
44+ /* *
45+ * @brief Type used to represent an elastix ParameterValue in ITK-Wasm pipelines
46+ * and for serializing elastix parameter files with glaze.
47+ *
48+ * This is the type cast string held in a ParameterFile
49+ */
4550using ParameterValue = std::variant<std::string, bool , int64_t , double >;
4651
4752using ParameterValueVector = std::vector<ParameterValue>;
4853
49- /* *
50- * @brief Type used to represent an elastix ParameterObject in ITK-Wasm pipelines
51- * and for serializing elastix parameter files with glaze.
52- */
53- using ParameterMap = std::map<ParameterKey, ParameterValueVector>;
54+ /* *
55+ * @brief Type used to represent an elastix ParameterObject in ITK-Wasm pipelines
56+ * and for serializing elastix parameter files with glaze.
57+ */
58+ using ParameterMap = std::map<ParameterKey, ParameterValueVector>;
59+
60+ /* *
61+ * @brief Type used to represent a vector of elastix ParameterMaps in ITK-Wasm pipelines
62+ * and for serializing elastix parameter files with glaze.
63+ */
64+ using ParameterMapVector = std::vector<ParameterMap>;
65+
66+ /* *
67+ * @brief Read an itk::wasm::ParameterMapVector elastix parameter object JSON representation into an elastix::ParameterObject.
68+ *
69+ * @param parameterObjectJson JSON representation of the elastix parameter object
70+ * @param parameterObject Pointer to the elastix::ParameterObject to populate
71+ * @return std::string Error message if reading the parameter object fails, empty string otherwise.
72+ */
73+ std::string ReadParameterObject (const std::string & parameterObjectJson, elastix::ParameterObject * parameterObject)
74+ {
75+ using ParameterObjectType = elastix::ParameterObject;
76+
77+ itk::wasm::ParameterMapVector wasmParameterMaps;
78+ auto errorCode = glz::read_json<itk::wasm::ParameterMapVector>(wasmParameterMaps, parameterObjectJson);
79+ if (errorCode)
80+ {
81+ const std::string errorMessage = glz::format_error (errorCode, parameterObjectJson);
82+ return errorMessage;
83+ }
84+
85+ const auto numParameterMaps = wasmParameterMaps.size ();
86+ ParameterObjectType::ParameterMapVectorType parameterMaps;
87+ parameterMaps.reserve (numParameterMaps);
88+ for (const auto wasmParameterMap : wasmParameterMaps)
89+ {
90+ ParameterObjectType::ParameterMapType parameterMap;
91+ for (const auto & parameter : wasmParameterMap)
92+ {
93+ ParameterObjectType::ParameterValueVectorType parameterValues;
94+ for (const auto & value : parameter.second )
95+ {
96+ if (value.index () == 0 )
97+ {
98+ const auto & valueString = std::get<std::string>(value);
99+ parameterValues.push_back (valueString);
100+ }
101+ else if (value.index () == 1 )
102+ {
103+ const auto & valueBool = std::get<bool >(value);
104+ if (valueBool)
105+ {
106+ parameterValues.push_back (" true" );
107+ }
108+ else
109+ {
110+ parameterValues.push_back (" false" );
111+ }
112+ }
113+ else if (value.index () == 2 )
114+ {
115+ const auto & valueInt = std::get<int64_t >(value);
116+ parameterValues.push_back (itk::ConvertNumberToString (valueInt));
117+ }
118+ else if (value.index () == 3 )
119+ {
120+ const auto & valueDouble = std::get<double >(value);
121+ parameterValues.push_back (itk::ConvertNumberToString (valueDouble));
122+ }
123+ }
124+ parameterMap[parameter.first ] = parameterValues;
125+ }
126+
127+ parameterObject->AddParameterMap (parameterMap);
128+ }
129+
130+ return {};
131+ }
132+
133+ /* *
134+ * @brief Write an elastix::ParameterObject into an itk::wasm::ParameterMapVector JSON representation.
135+ *
136+ * @param parameterObject Pointer to the elastix::ParameterObject to serialize
137+ * @param parameterObjectJson JSON representation of the elastix parameter object (output)
138+ * @return std::string Error message if writing the parameter object fails, empty string otherwise.
139+ */
140+ std::string WriteParameterObject (const elastix::ParameterObject * parameterObject, std::string & parameterObjectJson)
141+ {
142+ using ParameterObjectType = elastix::ParameterObject;
143+
144+ itk::wasm::ParameterMapVector wasmParameterMaps;
145+
146+ const auto numParameterMaps = parameterObject->GetNumberOfParameterMaps ();
147+ wasmParameterMaps.reserve (numParameterMaps);
148+
149+ for (unsigned int i = 0 ; i < numParameterMaps; ++i)
150+ {
151+ const auto & parameterMap = parameterObject->GetParameterMap (i);
152+ itk::wasm::ParameterMap wasmParameterMap;
153+
154+ for (const auto & parameter : parameterMap)
155+ {
156+ const auto & parameterValueVector = parameter.second ;
157+ auto & wasmValues = wasmParameterMap[parameter.first ];
158+ wasmValues.reserve (parameterValueVector.size ());
159+
160+ // Convert each string into the variant
161+ for (const auto & val : parameterValueVector)
162+ {
163+ wasmValues.emplace_back (val);
164+ }
165+ }
166+ wasmParameterMaps.push_back (wasmParameterMap);
167+ }
168+
169+ auto errorCode = glz::write<glz::opts{ .prettify = true }>(wasmParameterMaps, parameterObjectJson);
170+ if (errorCode)
171+ {
172+ const std::string errorMessage = glz::format_error (errorCode, parameterObjectJson);
173+ return errorMessage;
174+ }
175+
176+ return {};
177+ }
178+
54179
55- /* *
56- * @brief Type used to represent a vector of elastix ParameterMaps in ITK-Wasm pipelines
57- * and for serializing elastix parameter files with glaze.
58- */
59- using ParameterMapVector = std::vector<ParameterMap>;
60180
61- } // namespace wasm
62- } // namespace itk
181+ } // namespace wasm
182+ } // namespace itk
63183
64184 #endif // itkElastixWasmParameterObject_h
0 commit comments