File: itkMetaContourConverter.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 (251 lines) | stat: -rw-r--r-- 7,692 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
/*=========================================================================
 *
 *  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 itkMetaContourConverter_hxx
#define itkMetaContourConverter_hxx


namespace itk
{

template <unsigned int VDimension>
auto
MetaContourConverter<VDimension>::CreateMetaObject() -> MetaObjectType *
{
  return dynamic_cast<MetaObjectType *>(new ContourMetaObjectType);
}

/** Convert a metaContour into an Contour SpatialObject  */
template <unsigned int VDimension>
auto
MetaContourConverter<VDimension>::MetaObjectToSpatialObject(const MetaObjectType * mo) -> SpatialObjectPointer
{
  const auto * contourMO = dynamic_cast<const MetaContour *>(mo);
  if (contourMO == nullptr)
  {
    itkExceptionMacro("Can't downcast MetaObject to MetaContour");
  }

  ContourSpatialObjectPointer contourSO = ContourSpatialObjectType::New();

  contourSO->GetProperty().SetName(contourMO->Name());
  contourSO->SetId(contourMO->ID());
  contourSO->SetParentId(contourMO->ParentID());
  contourSO->GetProperty().SetRed(contourMO->Color()[0]);
  contourSO->GetProperty().SetGreen(contourMO->Color()[1]);
  contourSO->GetProperty().SetBlue(contourMO->Color()[2]);
  contourSO->GetProperty().SetAlpha(contourMO->Color()[3]);
  contourSO->SetIsClosed(const_cast<ContourMetaObjectType *>(contourMO)->Closed());
  contourSO->SetAttachedToSlice(const_cast<ContourMetaObjectType *>(contourMO)->AttachedToSlice());

  // First the control points
  using ControlPointType = typename ContourSpatialObjectType::ContourPointType;

  auto itCP = contourMO->GetControlPoints().begin();

  for (unsigned int identifier = 0; identifier < contourMO->GetControlPoints().size(); ++identifier)
  {
    ControlPointType pnt;

    using PointType = typename ControlPointType::PointType;
    PointType point;
    PointType pickedPoint;

    using CovariantVectorType = typename ControlPointType::CovariantVectorType;
    CovariantVectorType normal;

    for (unsigned int i = 0; i < VDimension; ++i)
    {
      point[i] = (*itCP)->m_X[i] * contourMO->ElementSpacing(i);
    }

    for (unsigned int i = 0; i < VDimension; ++i)
    {
      pickedPoint[i] = (*itCP)->m_XPicked[i] * contourMO->ElementSpacing(i);
    }

    for (unsigned int i = 0; i < VDimension; ++i)
    {
      normal[i] = (*itCP)->m_V[i];
    }

    pnt.SetId((*itCP)->m_Id);
    pnt.SetRed((*itCP)->m_Color[0]);
    pnt.SetGreen((*itCP)->m_Color[1]);
    pnt.SetBlue((*itCP)->m_Color[2]);
    pnt.SetAlpha((*itCP)->m_Color[3]);

    pnt.SetPositionInObjectSpace(point);
    pnt.SetPickedPointInObjectSpace(pickedPoint);
    pnt.SetNormalInObjectSpace(normal);

    contourSO->GetControlPoints().push_back(pnt);
    ++itCP;
  }

  // Then the interpolated points
  using InterpolatedPointType = typename ContourSpatialObjectType::ContourPointType;
  auto itI = contourMO->GetInterpolatedPoints().begin();

  for (unsigned int identifier = 0; identifier < contourMO->GetInterpolatedPoints().size(); ++identifier)
  {
    InterpolatedPointType pnt;

    using PointType = typename ControlPointType::PointType;
    PointType point;

    for (unsigned int i = 0; i < VDimension; ++i)
    {
      point[i] = (*itI)->m_X[i];
    }

    pnt.SetId((*itI)->m_Id);
    pnt.SetRed((*itI)->m_Color[0]);
    pnt.SetGreen((*itI)->m_Color[1]);
    pnt.SetBlue((*itI)->m_Color[2]);
    pnt.SetAlpha((*itI)->m_Color[3]);

    pnt.SetPositionInObjectSpace(point);
    contourSO->AddPoint(pnt);
    ++itI;
  }

  return contourSO.GetPointer();
}

/** Convert a Contour SpatialObject into a metaContour */
template <unsigned int VDimension>
auto
MetaContourConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectType * so) -> MetaObjectType *
{
  ContourSpatialObjectConstPointer contourSO = dynamic_cast<const ContourSpatialObjectType *>(so);
  if (contourSO.IsNull())
  {
    itkExceptionMacro("Can't downcast SpatialObject to ContourSpatialObject");
  }
  auto * contourMO = new MetaContour(VDimension);


  // fill in the control points information
  typename ContourSpatialObjectType::ContourPointListType::const_iterator itCP;

  for (itCP = contourSO->GetControlPoints().begin(); itCP != contourSO->GetControlPoints().end(); ++itCP)
  {
    auto * pnt = new ContourControlPnt(VDimension);

    pnt->m_Id = itCP->GetId();

    for (unsigned int d = 0; d < VDimension; ++d)
    {
      pnt->m_X[d] = itCP->GetPositionInObjectSpace()[d];
    }

    for (unsigned int d = 0; d < VDimension; ++d)
    {
      pnt->m_XPicked[d] = itCP->GetPickedPointInObjectSpace()[d];
    }

    for (unsigned int d = 0; d < VDimension; ++d)
    {
      pnt->m_V[d] = itCP->GetNormalInObjectSpace()[d];
    }

    pnt->m_Color[0] = itCP->GetRed();
    pnt->m_Color[1] = itCP->GetGreen();
    pnt->m_Color[2] = itCP->GetBlue();
    pnt->m_Color[3] = itCP->GetAlpha();

    contourMO->GetControlPoints().push_back(pnt);
  }

  if (VDimension == 2)
  {
    contourMO->ControlPointDim("id x y xp yp v1 v2 r g b a");
  }
  else if (VDimension == 3)
  {
    contourMO->ControlPointDim("id x y z xp yp zp v1 v2 v3 r gn be a");
  }

  // fill in the interpolated points information
  typename ContourSpatialObjectType::ContourPointListType::const_iterator itI;
  for (itI = contourSO->GetPoints().begin(); itI != contourSO->GetPoints().end(); ++itI)
  {
    auto * pnt = new ContourInterpolatedPnt(VDimension);

    pnt->m_Id = itI->GetId();
    for (unsigned int d = 0; d < VDimension; ++d)
    {
      pnt->m_X[d] = itI->GetPositionInObjectSpace()[d];
    }

    pnt->m_Color[0] = itI->GetRed();
    pnt->m_Color[1] = itI->GetGreen();
    pnt->m_Color[2] = itI->GetBlue();
    pnt->m_Color[3] = itI->GetAlpha();

    contourMO->GetInterpolatedPoints().push_back(pnt);
  }

  if (VDimension == 2)
  {
    contourMO->InterpolatedPointDim("id x y r g b a");
  }
  else if (VDimension == 3)
  {
    contourMO->InterpolatedPointDim("id x y z r g b a");
  }

  // Set the interpolation type
  switch (contourSO->GetInterpolationMethod())
  {
    case ContourSpatialObjectType::InterpolationMethodEnum::EXPLICIT_INTERPOLATION:
      contourMO->Interpolation(MET_EXPLICIT_INTERPOLATION);
      break;
    case ContourSpatialObjectType::InterpolationMethodEnum::LINEAR_INTERPOLATION:
      contourMO->Interpolation(MET_LINEAR_INTERPOLATION);
      break;
    case ContourSpatialObjectType::InterpolationMethodEnum::BEZIER_INTERPOLATION:
      contourMO->Interpolation(MET_BEZIER_INTERPOLATION);
      break;
    default:
      contourMO->Interpolation(MET_NO_INTERPOLATION);
  }

  float color[4];
  for (unsigned int i = 0; i < 4; ++i)
  {
    color[i] = contourSO->GetProperty().GetColor()[i];
  }
  contourMO->Color(color);
  contourMO->ID(contourSO->GetId());
  contourMO->Closed(contourSO->GetIsClosed());
  contourMO->AttachedToSlice(contourSO->GetAttachedToSlice());
  contourMO->DisplayOrientation(contourSO->GetOrientationInObjectSpace());

  if (contourSO->GetParent())
  {
    contourMO->ParentID(contourSO->GetParent()->GetId());
  }

  contourMO->BinaryData(true);
  return contourMO;
}

} // end namespace itk

#endif