File: itkMetaSceneConverter.hxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (394 lines) | stat: -rw-r--r-- 13,591 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkMetaSceneConverter_hxx
#define itkMetaSceneConverter_hxx

#include "itkMetaEllipseConverter.h"
#include "itkMetaTubeConverter.h"
#include "itkMetaDTITubeConverter.h"
#include "itkMetaVesselTubeConverter.h"
#include "itkMetaGroupConverter.h"
#include "itkMetaImageConverter.h"
#include "itkMetaImageMaskConverter.h"
#include "itkMetaBlobConverter.h"
#include "itkMetaGaussianConverter.h"
#include "itkMetaMeshConverter.h"
#include "itkMetaLandmarkConverter.h"
#include "itkMetaLineConverter.h"
#include "itkMetaSurfaceConverter.h"
#include "itkMetaArrowConverter.h"
#include "itkMetaContourConverter.h"

#include <algorithm>

namespace itk
{

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::MetaSceneConverter()
{
  // default behaviour of scene converter is not to save transform
  // with each spatial object.
  m_Event = nullptr;
  m_BinaryPoints = false;
  m_TransformPrecision = 6;
  m_WriteImagesInSeparateFile = false;
}

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
void
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::SetTransform(MetaObject * obj, const TransformType * transform)
{
  typename SpatialObjectType::TransformType::InputPointType center = transform->GetCenter();
  typename SpatialObjectType::TransformType::MatrixType     matrix = transform->GetMatrix();
  typename SpatialObjectType::TransformType::OffsetType     offset = transform->GetOffset();

  unsigned int p = 0;
  for (unsigned int row = 0; row < VDimension; ++row)
  {
    for (unsigned int col = 0; col < VDimension; ++col)
    {
      m_Orientation[p++] = matrix[row][col];
    }
  }

  for (unsigned int i = 0; i < VDimension; ++i)
  {
    m_Position[i] = offset[i];
    m_CenterOfRotation[i] = center[i];
  }

  obj->CenterOfRotation(m_CenterOfRotation);
  obj->TransformMatrix(m_Orientation);
  obj->Offset(m_Position);
  obj->SetDoublePrecision(m_TransformPrecision);
}

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
void
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::SetTransform(SpatialObjectType * so, const MetaObject * meta)
{
  typename SpatialObjectType::TransformType::InputPointType center;
  typename SpatialObjectType::TransformType::MatrixType     matrix;
  typename SpatialObjectType::TransformType::OffsetType     offset;

  unsigned int p = 0;
  for (unsigned int row = 0; row < VDimension; ++row)
  {
    for (unsigned int col = 0; col < VDimension; ++col)
    {
      matrix[row][col] = (meta->Orientation())[p++];
    }
  }

  for (unsigned int i = 0; i < VDimension; ++i)
  {
    offset[i] = (meta->Position())[i];
    center[i] = (meta->CenterOfRotation())[i];
  }

  so->GetModifiableObjectToParentTransform()->SetCenter(center);
  so->GetModifiableObjectToParentTransform()->SetMatrix(matrix);
  so->GetModifiableObjectToParentTransform()->SetOffset(offset);
}

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
auto
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::CreateSpatialObjectScene(MetaScene * mScene)
  -> SpatialObjectPointer
{
  SpatialObjectPointer soScene = nullptr;

  MetaScene::ObjectListType * list = mScene->GetObjectList();
  auto                        it = list->begin();
  auto                        itEnd = list->end();

  SpatialObjectPointer currentSO = nullptr;

  while (it != itEnd)
  {
    const std::string objectTypeName((*it)->ObjectTypeName());
    const std::string objectSubTypeName((*it)->ObjectSubTypeName());

    /** New object goes here */
    if (objectTypeName == "Tube")
    {
      // If there is the subtype is a vessel
      if (objectSubTypeName == "Vessel")
      {
        currentSO = this->MetaObjectToSpatialObject<MetaVesselTubeConverter<VDimension>>(*it);
      }
      else if (objectSubTypeName == "DTI")
      {
        currentSO = this->MetaObjectToSpatialObject<MetaDTITubeConverter<VDimension>>(*it);
      }
      else
      {
        currentSO = this->MetaObjectToSpatialObject<MetaTubeConverter<VDimension>>(*it);
      }
    }
    else if (objectTypeName == "Group")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaGroupConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Ellipse")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaEllipseConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Arrow")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaArrowConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Image")
    {
      // If there is the subtype is a mask
      if (objectSubTypeName == "Mask")
      {
        currentSO = this->MetaObjectToSpatialObject<MetaImageMaskConverter<VDimension>>(*it);
      }
      else
      {
        currentSO = this->MetaObjectToSpatialObject<MetaImageConverter<VDimension, PixelType>>(*it);
      }
    }
    else if (objectTypeName == "Blob")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaBlobConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Gaussian")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaGaussianConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Landmark")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaLandmarkConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Surface")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaSurfaceConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Line")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaLineConverter<VDimension>>(*it);
    }
    else if (objectTypeName == "Mesh")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaMeshConverter<VDimension, PixelType, TMeshTraits>>(*it);
    }
    else if (objectTypeName == "Contour")
    {
      currentSO = this->MetaObjectToSpatialObject<MetaContourConverter<VDimension>>(*it);
    }
    else
    {
      auto converterIt = this->m_ConverterMap.find(objectTypeName);

      if (converterIt == this->m_ConverterMap.end())
      {
        itkGenericExceptionMacro("Unable to find MetaObject -> SpatialObject converter for " << objectTypeName);
      }
      currentSO = converterIt->second->MetaObjectToSpatialObject(*it);
    }
    this->SetTransform(currentSO, *it);
    int tmpParentId = currentSO->GetParentId();
    if (soScene != nullptr)
    {
      soScene->AddChild(currentSO);
    }
    else
    {
      soScene = currentSO;
    }
    currentSO->SetParentId(tmpParentId);
    ++it;
  }

  if (soScene != nullptr)
  {
    soScene->FixIdValidity();
    soScene->FixParentChildHierarchyUsingParentIds();
  }

  return soScene;
}

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
auto
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::ReadMeta(const std::string & name) -> SpatialObjectPointer
{
  auto * mScene = new MetaScene;

  if (m_Event)
  {
    mScene->SetEvent(m_Event);
  }
  mScene->Read(name.c_str());
  SpatialObjectPointer soScene = CreateSpatialObjectScene(mScene);
  delete mScene;
  return soScene;
}

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
MetaScene *
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::CreateMetaScene(const SpatialObjectType * soScene,
                                                                        unsigned int              depth,
                                                                        const std::string &       name)
{
  auto * metaScene = new MetaScene(VDimension);

  metaScene->BinaryData(m_BinaryPoints);

  float spacing[VDimension];
  for (unsigned int i = 0; i < VDimension; ++i)
  {
    spacing[i] = 1;
  }
  metaScene->ElementSpacing(spacing);

  using ListType = typename SpatialObjectType::ChildrenConstListType;

  ListType * childrenList = soScene->GetConstChildren(depth, name);
  childrenList->push_front(soScene); // add the top level object to the list
                                     //   to be processed.

  auto it = childrenList->begin();
  auto itEnd = childrenList->end();

  MetaObject * currentMeta;

  while (it != itEnd)
  {
    std::string spatialObjectTypeName((*it)->GetTypeName());
    if (spatialObjectTypeName == "GroupSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaGroupConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "TubeSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaTubeConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "VesselTubeSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaVesselTubeConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "DTITubeSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaDTITubeConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "EllipseSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaEllipseConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "ArrowSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaArrowConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "ImageSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaImageConverter<VDimension, PixelType>>(*it);
    }
    else if (spatialObjectTypeName == "ImageMaskSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaImageMaskConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "BlobSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaBlobConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "GaussianSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaGaussianConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "LandmarkSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaLandmarkConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "ContourSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaContourConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "SurfaceSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaSurfaceConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "LineSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaLineConverter<VDimension>>(*it);
    }
    else if (spatialObjectTypeName == "MeshSpatialObject")
    {
      currentMeta = this->SpatialObjectToMetaObject<MetaMeshConverter<VDimension, PixelType, TMeshTraits>>(*it);
    }
    else
    {
      auto converterIt = this->m_ConverterMap.find(spatialObjectTypeName);
      if (converterIt == this->m_ConverterMap.end())
      {
        itkGenericExceptionMacro("Unable to find MetaObject -> SpatialObject converter for " << spatialObjectTypeName);
      }
      currentMeta = converterIt->second->SpatialObjectToMetaObject(*it);
    }
    if ((*it)->GetParent())
    {
      currentMeta->ParentID((*it)->GetParent()->GetId());
    }
    currentMeta->Name((*it)->GetProperty().GetName().c_str());
    currentMeta->Color((*it)->GetProperty().GetRed(),
                       (*it)->GetProperty().GetGreen(),
                       (*it)->GetProperty().GetBlue(),
                       (*it)->GetProperty().GetAlpha());

    this->SetTransform(currentMeta, (*it)->GetObjectToParentTransform());
    metaScene->AddObject(currentMeta);
    ++it;
  }

  delete childrenList;

  return metaScene;
}

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
bool
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::WriteMeta(const SpatialObjectType * soScene,
                                                                  const std::string &       fileName,
                                                                  unsigned int              depth,
                                                                  const std::string &       soName)
{
  MetaScene * metaScene = this->CreateMetaScene(soScene, depth, soName);

  metaScene->Write(fileName.c_str());

  delete metaScene;

  return true;
}

template <unsigned int VDimension, typename PixelType, typename TMeshTraits>
void
MetaSceneConverter<VDimension, PixelType, TMeshTraits>::RegisterMetaConverter(const std::string & metaTypeName,
                                                                              const std::string & spatialObjectTypeName,
                                                                              MetaConverterBaseType * converter)
{
  this->m_ConverterMap[metaTypeName] = converter;
  this->m_ConverterMap[spatialObjectTypeName] = converter;
}

} // end namespace itk

#endif