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 itkDistanceToCentroidMembershipFunction_h
19 #define itkDistanceToCentroidMembershipFunction_h
20 
21 #include "itkMembershipFunctionBase.h"
22 #include "itkDistanceMetric.h"
23 
24 namespace itk
25 {
26 namespace Statistics
27 {
28 /** \class DistanceToCentroidMembershipFunction
29  * \brief DistanceToCentroidMembershipFunction models class membership
30  * using a distance metric.
31  *
32  * DistanceToCentroidMembershipFunction is a subclass of
33  * MembershipFunctionBase that models class membership using the
34  * distance to the centroid of the class. A choice of distance metric
35  * can be specified using the SetDistanceMetric() method. Options
36  * include EuclideanDistanceMetric, EuclideanSquaredDistanceMetric,
37  * and ManhattenDistanceMetric. The centroid of the class is specified
38  * using the SetCentroid() method. Any other parameters to control the
39  * distance function evaluation have to be set directly on the
40  * distance function itself.
41  *
42  * \ingroup ITKStatistics
43  */
44 template< typename TVector >
45 class ITK_TEMPLATE_EXPORT DistanceToCentroidMembershipFunction:
46   public MembershipFunctionBase< TVector >
47 {
48 public:
49   ITK_DISALLOW_COPY_AND_ASSIGN(DistanceToCentroidMembershipFunction);
50 
51   /** Standard class type aliases */
52   using Self = DistanceToCentroidMembershipFunction;
53   using Superclass = MembershipFunctionBase< TVector >;
54   using Pointer = SmartPointer< Self >;
55   using ConstPointer = SmartPointer< const Self >;
56 
57   /** Strandard macros */
58   itkTypeMacro(DistanceToCentroidMembershipFunction,
59                MembershipFunctionBase);
60   itkNewMacro(Self);
61 
62   /** SmartPointer class for superclass */
63   using MembershipFunctionPointer = typename Superclass::Pointer;
64 
65   /** Typedef alias for the measurement vectors */
66   using MeasurementVectorType = TVector;
67 
68   /** Typedef to represent the length of measurement vectors */
69   using MeasurementVectorSizeType = typename Superclass::MeasurementVectorSizeType;
70 
71   /**  Set the length of each measurement vector. */
72   void SetMeasurementVectorSize(MeasurementVectorSizeType) override;
73 
74   /** Type of the DistanceMetric to use */
75   using DistanceMetricType = DistanceMetric< MeasurementVectorType >;
76   using DistanceMetricPointer = typename DistanceMetricType::Pointer;
77   using CentroidType = typename DistanceMetricType::OriginType;
78 
79   /** Set the DistanceMetric to be used when calling the Evaluate() method */
80   itkSetObjectMacro(DistanceMetric, DistanceMetricType);
81 
82   /** Get the DistanceMetric used by the MembershipFunction */
83   itkGetModifiableObjectMacro(DistanceMetric, DistanceMetricType);
84 
85   /** Get the DistanceMetric used by the MembershipFunction. This is
86    * a non-const version that allows you to configure the distance
87    * function directly. */
88 
89   /** Set the centroid of the class (propagated to the DistanceMetric) */
90   void SetCentroid(const CentroidType & centroid);
91 
92   /** Get the centroid of the class (requested from the DistanceMetric */
93   const CentroidType & GetCentroid() const;
94 
95   /**
96    * Method to get probability of an instance. The return value is the
97    * value of the density function, not probability. */
98   double Evaluate(const MeasurementVectorType & measurement) const override;
99 
100 protected:
101   DistanceToCentroidMembershipFunction();
102   ~DistanceToCentroidMembershipFunction() override = default;
103   void PrintSelf(std::ostream & os, Indent indent) const override;
104 
105   /** Return a copy of the current membership function */
106   typename LightObject::Pointer InternalClone() const override;
107 
108 private:
109   DistanceMetricPointer m_DistanceMetric;
110 };
111 } // end of namespace Statistics
112 } // end namespace itk
113 
114 #ifndef ITK_MANUAL_INSTANTIATION
115 #include "itkDistanceToCentroidMembershipFunction.hxx"
116 #endif
117 
118 #endif
119