Orfeo Toolbox  3.16
itkMetaDTITubeConverter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMetaDTITubeConverter.txx,v $
5  Language: C++
6  Date: $Date: 2008-01-10 05:47:59 $
7  Version: $Revision: 1.13 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkMetaDTITubeConverter_txx
18 #define __itkMetaDTITubeConverter_txx
19 
21 
22 namespace itk
23 {
24 
25 
27 template <unsigned int NDimensions>
30 {
31 
32 }
33 
34 
36 template <unsigned int NDimensions>
40 {
41  typedef itk::DTITubeSpatialObject<NDimensions> DTITubeSpatialObjectType;
42  typename DTITubeSpatialObjectType::Pointer tub =
43  DTITubeSpatialObjectType::New();
44  double spacing[NDimensions];
45 
46  unsigned int ndims = tube->NDims();
47  for(unsigned int ii=0;ii<ndims;ii++)
48  {
49  spacing[ii]=tube->ElementSpacing()[ii];
50  }
51 
52  tub->GetIndexToObjectTransform()->SetScaleComponent(spacing);
53  tub->GetProperty()->SetName(tube->Name());
54  tub->SetParentPoint(tube->ParentPoint());
55  tub->SetId(tube->ID());
56  tub->SetParentId(tube->ParentID());
57  tub->GetProperty()->SetRed(tube->Color()[0]);
58  tub->GetProperty()->SetGreen(tube->Color()[1]);
59  tub->GetProperty()->SetBlue(tube->Color()[2]);
60  tub->GetProperty()->SetAlpha(tube->Color()[3]);
61 
62  typedef itk::DTITubeSpatialObjectPoint<NDimensions> TubePointType;
63  typedef TubePointType* TubePointPointer;
64 
65  typedef MetaDTITube::PointListType ListType;
66  ListType::iterator it2 = tube->GetPoints().begin();
67 
69  v.Fill(0.0);
71  t.Fill(0.0);
72 
73  for(unsigned int identifier=0;identifier< tube->GetPoints().size();identifier++)
74  {
75  TubePointType pnt;
76 
77  typedef typename DTITubeSpatialObjectType::PointType PointType;
78  PointType point;
79 
80  for(unsigned int ii=0;ii<ndims;ii++)
81  {
82  point[ii]=(*it2)->m_X[ii];
83  }
84 
85  // Get the fields from the metaIO
86  const DTITubePnt::FieldListType & metaFields = (*it2)->GetExtraFields();
87  DTITubePnt::FieldListType::const_iterator extraIt = metaFields.begin();
88  while(extraIt != metaFields.end())
89  {
90  // Do not add the optional fields
91  if( ((*extraIt).first != "r")
92  && ((*extraIt).first != "v1x")
93  && ((*extraIt).first != "v1y")
94  && ((*extraIt).first != "v1z")
95  && ((*extraIt).first != "v2x")
96  && ((*extraIt).first != "v2y")
97  && ((*extraIt).first != "v2z")
98  && ((*extraIt).first != "tx")
99  && ((*extraIt).first != "ty")
100  && ((*extraIt).first != "tz")
101  && ((*extraIt).first != "red")
102  && ((*extraIt).first != "green")
103  && ((*extraIt).first != "blue")
104  && ((*extraIt).first != "alpha")
105  && ((*extraIt).first != "id")
106  )
107  {
108  pnt.AddField((*extraIt).first.c_str(),(*extraIt).second);
109  }
110  extraIt++;
111  }
112 
113  pnt.SetPosition(point);
114 
115  float* tensor = new float[6];
116 
117  for(unsigned int ii=0;ii<6;ii++)
118  {
119  tensor[ii]=(*it2)->m_TensorMatrix[ii];
120  }
121  pnt.SetTensorMatrix(tensor);
122 
123  delete []tensor;
124 
125 
126  // This attribute are optional
127  if((*it2)->GetField("r") != -1)
128  {
129  pnt.SetRadius((*it2)->GetField("r"));
130  }
131 
132  if((*it2)->GetField("v1x") != -1)
133  {
134  v[0]= (*it2)->GetField("v1x");
135  v[1]= (*it2)->GetField("v1y");
136  if(ndims == 3)
137  {
138  v[2]= (*it2)->GetField("v1z");
139  }
140  pnt.SetNormal1(v);
141  }
142 
143 
144  if((*it2)->GetField("v2x") != -1)
145  {
146  v[0]= (*it2)->GetField("v2x");
147  v[1]= (*it2)->GetField("v2y");
148  if(ndims == 3)
149  {
150  v[2]= (*it2)->GetField("v2z");
151  }
152  pnt.SetNormal1(v);
153  }
154 
155  if((*it2)->GetField("tx") != -1)
156  {
157  t[0]= (*it2)->GetField("tx");
158  t[1]= (*it2)->GetField("ty");
159  if(ndims == 3)
160  {
161  t[2]= (*it2)->GetField("tz");
162  }
163  pnt.SetTangent(t);
164  }
165 
166 
167  if((*it2)->GetField("red") != -1)
168  {
169  pnt.SetRed((*it2)->GetField("red"));
170  }
171 
172  if((*it2)->GetField("green") != -1)
173  {
174  pnt.SetGreen((*it2)->GetField("green"));
175  }
176 
177  if((*it2)->GetField("blue") != -1)
178  {
179  pnt.SetBlue((*it2)->GetField("blue"));
180  }
181 
182  if((*it2)->GetField("alpha") != -1)
183  {
184  pnt.SetAlpha((*it2)->GetField("alpha"));
185  }
186 
187  if((*it2)->GetField("id") != -1)
188  {
189  pnt.SetID((int)((*it2)->GetField("id")));
190  }
191 
192  tub->GetPoints().push_back(pnt);
193 
194  it2++;
195  }
196  return tub;
197 }
198 
200 template <unsigned int NDimensions>
201 MetaDTITube*
204 {
205  MetaDTITube* tube = new MetaDTITube(NDimensions);
206 
207  // Check what are the fields to be written
208  bool writeNormal1 = false;
209  bool writeNormal2 = false;
210  bool writeTangent = false;
211  bool writeRadius = false;
212  bool writeColor = false;
213  bool writeAlpha = false;
214  bool writeID = false;
215 
216  typename SpatialObjectType::PointListType::const_iterator i;
217  for(i = dynamic_cast<SpatialObjectType*>(spatialObject)->GetPoints().begin();
218  i != dynamic_cast<SpatialObjectType*>(spatialObject)->GetPoints().end();
219  i++)
220  {
221  // Optional fields (written only if not default values)
222  if((*i).GetID() != -1)
223  {
224  writeID = true;
225  }
226 
227  if((*i).GetRadius() != 0)
228  {
229  writeRadius = true;
230  }
231 
232  unsigned int d;
233  for(d=0;d<NDimensions;d++)
234  {
235  if((*i).GetNormal1()[d] != 0)
236  {
237  writeNormal1 = true;
238  }
239  if((*i).GetNormal2()[d] != 0)
240  {
241  writeNormal2 = true;
242  }
243  if((*i).GetTangent()[d] != 0)
244  {
245  writeTangent = true;
246  }
247  }
248 
249  // write the color if changed
250  if( ((*i).GetRed() != 1.0)
251  || ((*i).GetGreen() != 0.0)
252  || ((*i).GetBlue() != 0.0)
253  )
254  {
255  writeColor = true;
256  }
257 
258  if((*i).GetAlpha() != 1.0)
259  {
260  writeAlpha = true;
261  }
262  }
263 
264  // fill in the tube information
265  for(i = dynamic_cast<SpatialObjectType*>(spatialObject)->GetPoints().begin();
266  i != dynamic_cast<SpatialObjectType*>(spatialObject)->GetPoints().end();
267  i++)
268  {
269  DTITubePnt* pnt = new DTITubePnt(NDimensions);
270 
271  for(unsigned int d=0;d<NDimensions;d++)
272  {
273  pnt->m_X[d] = (*i).GetPosition()[d];
274  }
275 
276  const DTITubePnt::FieldListType & metaFields = (*i).GetFields();
277  DTITubePnt::FieldListType::const_iterator extraIt = metaFields.begin();
278  while(extraIt != metaFields.end())
279  {
280  pnt->AddField((*extraIt).first.c_str(),(*extraIt).second);
281  extraIt++;
282  }
283 
284  for(unsigned int d=0;d<6;d++)
285  {
286  pnt->m_TensorMatrix[d]=(*i).GetTensorMatrix()[d];
287  }
288 
289  // Optional fields (written only if not default values)
290  if(writeID)
291  {
292  pnt->AddField("id",(*i).GetID());
293  }
294 
295  if(writeRadius)
296  {
297  pnt->AddField("r",(*i).GetRadius());
298  }
299 
300  if(writeNormal1)
301  {
302  pnt->AddField("v1x",(*i).GetNormal1()[0]);
303  pnt->AddField("v1y",(*i).GetNormal1()[1]);
304  if(NDimensions == 3)
305  {
306  pnt->AddField("v1z",(*i).GetNormal1()[2]);
307  }
308  }
309 
310  if(writeNormal2)
311  {
312  pnt->AddField("v2x",(*i).GetNormal2()[0]);
313  pnt->AddField("v2y",(*i).GetNormal2()[1]);
314  if(NDimensions == 3)
315  {
316  pnt->AddField("v2z",(*i).GetNormal2()[2]);
317  }
318  }
319 
320  if(writeTangent)
321  {
322  pnt->AddField("tx",(*i).GetTangent()[0]);
323  pnt->AddField("ty",(*i).GetTangent()[1]);
324  if(NDimensions == 3)
325  {
326  pnt->AddField("tz",(*i).GetTangent()[2]);
327  }
328  }
329 
330  // write the color if changed
331  if(writeColor)
332  {
333  pnt->AddField("red",(*i).GetRed());
334  pnt->AddField("green",(*i).GetGreen());
335  pnt->AddField("blue",(*i).GetBlue());
336  }
337 
338  if(writeAlpha)
339  {
340  pnt->AddField("alpha",(*i).GetAlpha());
341  }
342 
343  tube->GetPoints().push_back(pnt);
344  }
345 
346  tube->PointDim("x y z tensor1 tensor2 tensor3 tensor4 tensor5 tensor6");
347 
348  float color[4];
349  for(unsigned int ii=0;ii<4;ii++)
350  {
351  color[ii]=spatialObject->GetProperty()->GetColor()[ii];
352  }
353 
354  tube->Color(color);
355  tube->ID( spatialObject->GetId());
356 
357  if(spatialObject->GetParent())
358  {
359  tube->ParentID(spatialObject->GetParent()->GetId());
360  }
361  tube->ParentPoint(spatialObject->GetParentPoint());
362  tube->NPoints(tube->GetPoints().size());
363 
364  for(unsigned int ii=0;ii<NDimensions;ii++)
365  {
366  tube->ElementSpacing(ii, spatialObject->GetIndexToObjectTransform()
367  ->GetScaleComponent()[ii]);
368  }
369  return tube;
370 }
371 
372 
374 template <unsigned int NDimensions>
377 ::ReadMeta(const char* name)
378 {
379  SpatialObjectPointer spatialObject;
380  MetaDTITube* Tube = new MetaDTITube();
381  Tube->Read(name);
382  spatialObject = MetaDTITubeToDTITubeSpatialObject(Tube);
383  delete Tube;
384  return spatialObject;
385 }
386 
387 
389 template <unsigned int NDimensions>
390 bool
392 ::WriteMeta(SpatialObjectType* spatialObject,const char* name)
393 {
394  MetaDTITube* Tube = DTITubeSpatialObjectToMetaDTITube(spatialObject);
395  Tube->Write(name);
396  delete Tube;
397  return true;
398 }
399 
400 } // end namespace itk
401 
402 #endif

Generated at Sat Feb 2 2013 23:52:44 for Orfeo Toolbox with doxygen 1.8.1.1