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 
19 #ifndef itkLevelSetSparseImage_h
20 #define itkLevelSetSparseImage_h
21 
22 #include "itkDiscreteLevelSetImage.h"
23 #include "itkObjectFactory.h"
24 
25 #include "itkLabelObject.h"
26 #include "itkLabelMap.h"
27 #include "itkLexicographicCompare.h"
28 
29 namespace itk
30 {
31 
32 /**
33  *  \class LevelSetSparseImage
34  *  \brief Base class for the sparse representation of a level-set function on one Image.
35  *
36  *  \tparam TImage Input image type of the level set function
37  *  \todo Think about using image iterators instead of GetPixel()
38  *
39  *  \ingroup ITKLevelSetsv4
40  */
41 template< typename TOutput, unsigned int VDimension >
42 class ITK_TEMPLATE_EXPORT LevelSetSparseImage :
43   public DiscreteLevelSetImage< TOutput, VDimension >
44 {
45 public:
46   ITK_DISALLOW_COPY_AND_ASSIGN(LevelSetSparseImage);
47 
48   using Self = LevelSetSparseImage;
49   using Pointer = SmartPointer< Self >;
50   using ConstPointer = SmartPointer< const Self >;
51   using Superclass = DiscreteLevelSetImage< TOutput, VDimension >;
52 
53   /** Run-time type information */
54   itkTypeMacro ( LevelSetSparseImage, DiscreteLevelSetImage );
55 
56   static constexpr unsigned int Dimension = Superclass::Dimension;
57 
58   using InputType = typename Superclass::InputType;
59   using OutputType = typename Superclass::OutputType;
60   using OutputRealType = typename Superclass::OutputRealType;
61   using GradientType = typename Superclass::GradientType;
62   using HessianType = typename Superclass::HessianType;
63   using LevelSetDataType = typename Superclass::LevelSetDataType;
64 
65   using LayerIdType = int8_t;
66   using LayerIdListType = std::list< LayerIdType >;
67 
68   using LabelObjectType = LabelObject< LayerIdType, VDimension >;
69   using LabelObjectPointer = typename LabelObjectType::Pointer;
70   using LabelObjectLengthType = typename LabelObjectType::LengthType;
71   using LabelObjectLineType = typename LabelObjectType::LineType;
72 
73   using LabelMapType = LabelMap< LabelObjectType >;
74   using LabelMapPointer = typename LabelMapType::Pointer;
75   using LabelMapConstPointer = typename LabelMapType::ConstPointer;
76   using RegionType = typename LabelMapType::RegionType;
77 
78   using LayerType = std::map< InputType, OutputType,
79                     Functor::LexicographicCompare >;
80   using LayerIterator = typename LayerType::iterator;
81   using LayerConstIterator = typename LayerType::const_iterator;
82 
83   using LayerMapType = std::map< LayerIdType, LayerType >;
84   using LayerMapIterator = typename LayerMapType::iterator;
85   using LayerMapConstIterator = typename LayerMapType::const_iterator;
86 
87   /** Returns the layer affiliation of a given location inputIndex */
88   virtual LayerIdType Status( const InputType& inputIndex ) const;
89 
90   /** Return the const reference to a layer map with given id  */
91   const LayerType& GetLayer( LayerIdType value ) const;
92 
93   /** Return the pointer to a layer map with given id  */
94   LayerType& GetLayer( LayerIdType value );
95 
96   /** Set a layer map with id to the given layer pointer */
97   void SetLayer( LayerIdType value, const LayerType& layer );
98 
99   /** Set/Get the label map for computing the sparse representation */
100   virtual void SetLabelMap( LabelMapType* labelMap );
101   itkGetModifiableObjectMacro(LabelMap, LabelMapType );
102 
103   /** Graft data object as level set object */
104   void Graft( const DataObject* data ) override;
105 
106   /** Return the label object pointer with a given id */
107   template< typename TLabel >
108   typename LabelObject< TLabel, VDimension >::Pointer GetAsLabelObject();
109 
110 protected:
111   LevelSetSparseImage() = default;
112   ~LevelSetSparseImage() override = default;
113 
114   LayerMapType      m_Layers;
115   LabelMapPointer   m_LabelMap;
116   LayerIdListType   m_InternalLabelList;
117 
118   /** Initialize the sparse field layers */
119   virtual void InitializeLayers() = 0;
120 
121   virtual void InitializeInternalLabelList() = 0;
122 
123   bool IsInsideDomain( const InputType& inputIndex ) const override;
124 
125   /** Initialize the label map point and the sparse-field layers */
126   void Initialize() override;
127 
128   /** Copy level set information from data object */
129   void CopyInformation( const DataObject* data ) override;
130 };
131 
132 }
133 
134 #ifndef ITK_MANUAL_INSTANTIATION
135 #include "itkLevelSetSparseImage.hxx"
136 #endif
137 
138 #endif // itkLevelSetSparseImage_h
139