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  *
20  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  *  For complete copyright, license and disclaimer of warranty information
25  *  please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkVectorConnectedComponentImageFilter_h
29 #define itkVectorConnectedComponentImageFilter_h
30 
31 #include "itkMath.h"
32 #include "itkNumericTraits.h"
33 #include "itkConnectedComponentFunctorImageFilter.h"
34 
35 namespace itk
36 {
37 namespace Functor
38 {
39 /** \class SimilarVectorsFunctor
40  *
41  *  \brief A connected components filter that labels the
42  *         objects in a vector image.  Two vectors are pointing
43  *         similar directions if one minus their dot product is less than a
44  *         threshold.  Vectors that are 180 degrees out of phase
45  *         are similar.  Assumes that vectors are normalized.
46  * \ingroup ITKConnectedComponents
47  */
48 
49 template< typename TInput >
50 class SimilarVectorsFunctor
51 {
52 public:
SimilarVectorsFunctor()53   SimilarVectorsFunctor()
54   { m_Threshold = itk::NumericTraits< typename TInput::ValueType >::ZeroValue(); }
55 
56   ~SimilarVectorsFunctor() = default;
57 
SetDistanceThreshold(const typename TInput::ValueType & thresh)58   void SetDistanceThreshold(const typename TInput::ValueType & thresh)
59   { m_Threshold = thresh; }
GetDistanceThreshold()60   typename TInput::ValueType GetDistanceThreshold() { return ( m_Threshold ); }
61 
62   bool operator!=(const SimilarVectorsFunctor &) const
63   {
64     return false;
65   }
66 
67   bool operator==(const SimilarVectorsFunctor & other) const
68   {
69     return !( *this != other );
70   }
71 
operator()72   bool operator()(const TInput & a, const TInput & b) const
73   {
74     using RealValueType = typename NumericTraits<typename TInput::ValueType>::RealType;
75     RealValueType dotProduct = NumericTraits<RealValueType>::ZeroValue();
76     for ( unsigned int i = 0; i < NumericTraits<TInput>::GetLength(a); ++i)
77       {
78       dotProduct += a[i]*b[i];
79       }
80     return ( static_cast<typename TInput::ValueType>( 1.0 - itk::Math::abs(dotProduct) ) <= m_Threshold );
81   }
82 
83 protected:
84   typename TInput::ValueType m_Threshold;
85 };
86 } // end namespace Functor
87 
88 /** \class VectorConnectedComponentImageFilter
89  *
90  *  \brief A connected components filter that labels the
91  *         objects in a vector image.  Two vectors are pointing
92  *         similar directions if one minus their dot product is less than a
93  *         threshold.  Vectors that are 180 degrees out of phase
94  *         are similar.  Assumes that vectors are normalized.
95  * \ingroup ITKConnectedComponents
96  */
97 template< typename TInputImage, typename TOutputImage, typename TMaskImage = TInputImage >
98 class VectorConnectedComponentImageFilter:
99   public ConnectedComponentFunctorImageFilter< TInputImage, TOutputImage,
100                                                Functor::SimilarVectorsFunctor< typename TInputImage::ValueType >,
101                                                TMaskImage >
102 {
103 public:
104   ITK_DISALLOW_COPY_AND_ASSIGN(VectorConnectedComponentImageFilter);
105 
106   /** Standard class type aliases. */
107   using Self = VectorConnectedComponentImageFilter;
108   using Superclass = ConnectedComponentFunctorImageFilter< TInputImage, TOutputImage,
109                                                 Functor::SimilarVectorsFunctor< typename TInputImage::ValueType >,
110                                                 TMaskImage >;
111   using Pointer = SmartPointer< Self >;
112   using ConstPointer = SmartPointer< const Self >;
113 
114   /** Method for creation through the object factory. */
115   itkNewMacro(Self);
116 
117   /** Run-time type information (and related methods). */
118   itkTypeMacro(VectorConnectedComponentImageFilter, ConnectedComponentFunctorImageFilter);
119 
120   using InputValueType = typename TInputImage::PixelType::ValueType;
121 
SetDistanceThreshold(const InputValueType & thresh)122   virtual void SetDistanceThreshold(const InputValueType & thresh)
123   { this->GetFunctor().SetDistanceThreshold(thresh); }
124 
GetDistanceThreshold()125   virtual InputValueType GetDistanceThreshold()
126   { return ( this->GetFunctor().GetDistanceThreshold() ); }
127 
128 #ifdef ITK_USE_CONCEPT_CHECKING
129   // Begin concept checking
130   itkConceptMacro( InputValueHasNumericTraitsCheck,
131                    ( Concept::HasNumericTraits< InputValueType > ) );
132   itkConceptMacro( InputValyeTypeIsFloatingCheck,
133                    ( Concept::IsFloatingPoint< InputValueType > ) );
134   // End concept checking
135 #endif
136 
137 protected:
138   VectorConnectedComponentImageFilter() = default;
139   ~VectorConnectedComponentImageFilter() override = default;
140 };
141 } // end namespace itk
142 
143 #endif
144