1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkMetaTubeConverter_hxx
19 #define itkMetaTubeConverter_hxx
20 
21 #include "itkMetaTubeConverter.h"
22 
23 namespace itk
24 {
25 template< unsigned int NDimensions >
26 typename MetaTubeConverter< NDimensions >::MetaObjectType *
27 MetaTubeConverter< NDimensions>
CreateMetaObject()28 ::CreateMetaObject()
29 {
30   return dynamic_cast<MetaObjectType *>(new TubeMetaObjectType);
31 }
32 
33 /** Convert a metaTube into an Tube SpatialObject  */
34 template< unsigned int NDimensions >
35 typename MetaTubeConverter< NDimensions >::SpatialObjectPointer
36 MetaTubeConverter< NDimensions >
MetaObjectToSpatialObject(const MetaObjectType * mo)37 ::MetaObjectToSpatialObject(const MetaObjectType *mo)
38 {
39   const auto * tubeMO = dynamic_cast<const TubeMetaObjectType *>(mo);
40   if(tubeMO == nullptr)
41     {
42     itkExceptionMacro(<< "Can't convert MetaObject to MetaTube" );
43     }
44 
45   typename TubeSpatialObjectType::Pointer tubeSO = TubeSpatialObjectType::New();
46 
47   tubeSO->GetProperty().SetName( tubeMO->Name() );
48   tubeSO->SetParentPoint( tubeMO->ParentPoint() );
49   tubeSO->SetId( tubeMO->ID() );
50   tubeSO->SetParentId( tubeMO->ParentID() );
51   tubeSO->GetProperty().SetRed(tubeMO->Color()[0]);
52   tubeSO->GetProperty().SetGreen(tubeMO->Color()[1]);
53   tubeSO->GetProperty().SetBlue(tubeMO->Color()[2]);
54   tubeSO->GetProperty().SetAlpha(tubeMO->Color()[3]);
55 
56   using TubePointType = itk::TubeSpatialObjectPoint< NDimensions >;
57 
58   auto it2 = tubeMO->GetPoints().begin();
59 
60   itk::CovariantVector< double, NDimensions > v;
61   itk::Vector< double, NDimensions >          t;
62 
63   for ( unsigned int identifier = 0; identifier < tubeMO->GetPoints().size(); identifier++ )
64     {
65     TubePointType pnt;
66 
67     pnt.SetPositionInObjectSpace( ( *it2 )->m_X );
68     pnt.SetRadiusInObjectSpace( ( *it2 )->m_R );
69 
70     for ( unsigned int i = 0; i < NDimensions; i++ )
71       {
72       v[i] = ( *it2 )->m_V1[i];
73       }
74     pnt.SetNormal1InObjectSpace(v);
75 
76     for ( unsigned int i = 0; i < NDimensions; i++ )
77       {
78       v[i] = ( *it2 )->m_V2[i];
79       }
80     pnt.SetNormal2InObjectSpace(v);
81 
82     for ( unsigned int i = 0; i < NDimensions; i++ )
83       {
84       t[i] = ( *it2 )->m_T[i];
85       }
86     pnt.SetTangentInObjectSpace(t);
87 
88     pnt.SetRed( ( *it2 )->m_Color[0] );
89     pnt.SetGreen( ( *it2 )->m_Color[1] );
90     pnt.SetBlue( ( *it2 )->m_Color[2] );
91     pnt.SetAlpha( ( *it2 )->m_Color[3] );
92 
93     pnt.SetId( ( *it2 )->m_ID );
94 
95     tubeSO->GetPoints().push_back(pnt);
96 
97     it2++;
98     }
99 
100   return tubeSO.GetPointer();
101 }
102 
103 /** Convert a Tube SpatialObject into a metaTube */
104 template< unsigned int NDimensions >
105 typename MetaTubeConverter< NDimensions >::MetaObjectType *
106 MetaTubeConverter< NDimensions >
SpatialObjectToMetaObject(const SpatialObjectType * spatialObject)107 ::SpatialObjectToMetaObject(const SpatialObjectType *spatialObject)
108 {
109   TubeSpatialObjectConstPointer tubeSO =
110     dynamic_cast<const TubeSpatialObjectType *>(spatialObject);
111   if(tubeSO.IsNull())
112     {
113     itkExceptionMacro(<< "Can't downcast SpatialObject to TubeSpatialObject");
114     }
115 
116   auto * tubeMO = new MetaTube(NDimensions);
117 
118   // fill in the tube information
119   typename TubeSpatialObjectType::TubePointListType::const_iterator it;
120   for ( it = tubeSO->GetPoints().begin();
121         it != tubeSO->GetPoints().end();
122         it++ )
123     {
124     auto * pnt = new TubePnt(NDimensions);
125 
126     for ( unsigned int d = 0; d < NDimensions; d++ )
127       {
128       pnt->m_X[d] = ( *it ).GetPositionInObjectSpace()[d];
129       }
130 
131     pnt->m_ID = ( *it ).GetId();
132     pnt->m_R = ( *it ).GetRadiusInObjectSpace();
133 
134     for ( unsigned int d = 0; d < NDimensions; d++ )
135       {
136       pnt->m_V1[d] = ( *it ).GetNormal1InObjectSpace()[d];
137       }
138 
139     for ( unsigned int d = 0; d < NDimensions; d++ )
140       {
141       pnt->m_V2[d] = ( *it ).GetNormal2InObjectSpace()[d];
142       }
143 
144     for ( unsigned int d = 0; d < NDimensions; d++ )
145       {
146       pnt->m_T[d] = ( *it ).GetTangentInObjectSpace()[d];
147       }
148 
149     pnt->m_Color[0] = ( *it ).GetRed();
150     pnt->m_Color[1] = ( *it ).GetGreen();
151     pnt->m_Color[2] = ( *it ).GetBlue();
152     pnt->m_Color[3] = ( *it ).GetAlpha();
153 
154     tubeMO->GetPoints().push_back(pnt);
155     }
156 
157   if ( NDimensions == 2 )
158     {
159     tubeMO->PointDim("x y r v1x v1y tx ty red green blue alpha id");
160     }
161   else
162     {
163     tubeMO->PointDim("x y z r v1x v1y v1z v2x v2y v2z tx ty tz red green blue alpha id");
164     }
165 
166   float color[4];
167   for ( unsigned int i = 0; i < 4; i++ )
168     {
169     color[i] = tubeSO->GetProperty().GetColor()[i];
170     }
171 
172   tubeMO->Color(color);
173   tubeMO->ID( tubeSO->GetId() );
174 
175   if ( tubeSO->GetParent() )
176     {
177     tubeMO->ParentID( tubeSO->GetParent()->GetId() );
178     }
179   tubeMO->ParentPoint( tubeSO->GetParentPoint() );
180   tubeMO->NPoints(static_cast<int>( tubeMO->GetPoints().size() ) );
181 
182   return tubeMO;
183 }
184 
185 } // end namespace itk
186 
187 #endif
188