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 itkMetaLineConverter_hxx
19 #define itkMetaLineConverter_hxx
20 
21 #include "itkMetaLineConverter.h"
22 
23 namespace itk
24 {
25 
26 template< unsigned int NDimensions >
27 typename MetaLineConverter< NDimensions >::MetaObjectType *
28 MetaLineConverter< NDimensions>
CreateMetaObject()29 ::CreateMetaObject()
30 {
31   return dynamic_cast<MetaObjectType *>(new LineMetaObjectType);
32 }
33 
34 /** Convert a metaLine into an Line SpatialObject  */
35 template< unsigned int NDimensions >
36 typename MetaLineConverter< NDimensions >::SpatialObjectPointer
37 MetaLineConverter< NDimensions >
MetaObjectToSpatialObject(const MetaObjectType * mo)38 ::MetaObjectToSpatialObject(const MetaObjectType *mo)
39 {
40   const auto * lineMO = dynamic_cast<const LineMetaObjectType *>(mo);
41   if(lineMO == nullptr)
42     {
43     itkExceptionMacro(<< "Can't convert MetaObject to MetaLine" );
44     }
45 
46   LineSpatialObjectPointer lineSO = LineSpatialObjectType::New();
47 
48   lineSO->GetProperty().SetName( lineMO->Name() );
49   lineSO->SetId( lineMO->ID() );
50   lineSO->SetParentId( lineMO->ParentID() );
51   lineSO->GetProperty().SetRed(lineMO->Color()[0]);
52   lineSO->GetProperty().SetGreen(lineMO->Color()[1]);
53   lineSO->GetProperty().SetBlue(lineMO->Color()[2]);
54   lineSO->GetProperty().SetAlpha(lineMO->Color()[3]);
55 
56   using LinePointType = itk::LineSpatialObjectPoint< NDimensions >;
57 
58   auto it2 = lineMO->GetPoints().begin();
59 
60   for ( unsigned int identifier = 0; identifier < lineMO->GetPoints().size(); identifier++ )
61     {
62     LinePointType pnt;
63 
64     using PointType = typename LinePointType::PointType;
65     PointType point;
66     using NormalType = typename LinePointType::CovariantVectorType;
67 
68     for ( unsigned int ii = 0; ii < NDimensions; ii++ )
69       {
70       point[ii] = ( *it2 )->m_X[ii];
71       }
72 
73     pnt.SetPositionInObjectSpace(point);
74 
75     for ( unsigned int ii = 0; ii < NDimensions - 1; ii++ )
76       {
77       NormalType normal;
78       for ( unsigned int jj = 0; jj < NDimensions; jj++ )
79         {
80         normal[jj] = ( *it2 )->m_V[ii][jj];
81         }
82       pnt.SetNormalInObjectSpace(normal, ii);
83       }
84 
85     pnt.SetRed( ( *it2 )->m_Color[0] );
86     pnt.SetGreen( ( *it2 )->m_Color[1] );
87     pnt.SetBlue( ( *it2 )->m_Color[2] );
88     pnt.SetAlpha( ( *it2 )->m_Color[3] );
89 
90     lineSO->GetPoints().push_back(pnt);
91     it2++;
92     }
93   return lineSO.GetPointer();
94 }
95 
96 /** Convert a Line SpatialObject into a metaLine */
97 template< unsigned int NDimensions >
98 typename MetaLineConverter< NDimensions >::MetaObjectType *
99 MetaLineConverter< NDimensions >
SpatialObjectToMetaObject(const SpatialObjectType * spatialObject)100 ::SpatialObjectToMetaObject(const SpatialObjectType *spatialObject)
101 {
102   LineSpatialObjectConstPointer lineSO =
103     dynamic_cast<const LineSpatialObjectType *>(spatialObject);
104   if(lineSO.IsNull())
105     {
106     itkExceptionMacro(<< "Can't downcast SpatialObject to LineSpatialObject");
107     }
108 
109   auto * lineMO = new MetaLine(NDimensions);
110 
111   // due to a Visual Studio stupidity, can't seem to define
112   // a const method to return the points list.
113   const typename LineSpatialObjectType::LinePointListType &linePoints =
114     lineSO->GetPoints();
115 
116   // fill in the Line information
117   typename LineSpatialObjectType::LinePointListType::const_iterator it;
118   for ( it = linePoints.begin();
119         it != linePoints.end();
120         ++it )
121     {
122     auto * pnt = new LinePnt(NDimensions);
123 
124     for ( unsigned int d = 0; d < NDimensions; d++ )
125       {
126       pnt->m_X[d] = ( *it ).GetPositionInObjectSpace()[d];
127       }
128 
129     for ( unsigned int n = 0; n < NDimensions - 1; n++ )
130       {
131       for ( unsigned int d = 0; d < NDimensions; d++ )
132         {
133         pnt->m_V[n][d] = ( ( *it ).GetNormalInObjectSpace(n) )[d];
134         }
135       }
136 
137     pnt->m_Color[0] = ( *it ).GetRed();
138     pnt->m_Color[1] = ( *it ).GetGreen();
139     pnt->m_Color[2] = ( *it ).GetBlue();
140     pnt->m_Color[3] = ( *it ).GetAlpha();
141 
142     lineMO->GetPoints().push_back(pnt);
143     }
144 
145   if ( NDimensions == 2 )
146     {
147     lineMO->PointDim("x y v1x v1y v2x v2y red green blue alpha");
148     }
149   else if ( NDimensions == 3 )
150     {
151     lineMO->PointDim("x y z v1x v1y v1z v2x v2y v2z red green blue alpha");
152     }
153 
154   float color[4];
155   for ( unsigned int ii = 0; ii < 4; ii++ )
156     {
157     color[ii] = lineSO->GetProperty().GetColor()[ii];
158     }
159 
160   lineMO->Color(color);
161   lineMO->ID( lineSO->GetId() );
162   if ( lineSO->GetParent() )
163     {
164     lineMO->ParentID( lineSO->GetParent()->GetId() );
165     }
166   lineMO->NPoints(static_cast<int>( linePoints.size() ) );
167   lineMO->BinaryData(true);
168   return lineMO;
169 }
170 
171 } // end namespace itk
172 
173 #endif
174