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 // Software Guide : BeginLatex
20 //
21 // \index{itk::Statistics::EuclideanDistanceMetric}
22 //
23 // The Euclidean distance function (\subdoxygen{Statistics}{EuclideanDistanceMetric}
24 // requires as template parameter the type of the measurement vector. We can
25 // use this function for any subclass of the \doxygen{FixedArray}. As a
26 // subclass of the \subdoxygen{Statistics}{DistanceMetric}, it has two basic
27 // methods, the \code{SetOrigin(measurement vector)} and the
28 // \code{Evaluate(measurement vector)}. The \code{Evaluate()} method returns
29 // the distance between its argument (a measurement vector) and the measurement
30 // vector set by the \code{SetOrigin()} method.
31 //
32 // In addition to the two methods, EuclideanDistanceMetric has two more
33 // methods that return the distance of two measurements ---
34 // \code{Evaluate(measurement vector, measurement vector)} and the
35 // coordinate distance between two measurements (not vectors) ---
36 // \code{Evaluate(measurement, measurement)}. The argument type of the
37 // latter method is the type of the component of the measurement vector.
38 //
39 // We include the header files for the class and the \doxygen{Vector}.
40 //
41 // Software Guide : EndLatex
42 
43 // Software Guide : BeginCodeSnippet
44 #include "itkVector.h"
45 #include "itkArray.h"
46 #include "itkEuclideanDistanceMetric.h"
47 // Software Guide : EndCodeSnippet
48 
49 // Software Guide : BeginLatex
50 //
51 // We define the type of the measurement vector that will be input of
52 // the Euclidean distance function. As a result, the measurement type
53 // is \code{float}.
54 //
55 // Software Guide : EndLatex
56 
main(int,char * [])57 int main(int, char*[])
58 {
59   // Software Guide : BeginCodeSnippet
60   using MeasurementVectorType = itk::Array< float >;
61   // Software Guide : EndCodeSnippet
62 
63   // Software Guide : BeginLatex
64   //
65   // The instantiation of the function is done through the usual
66   // \code{New()} method and a smart pointer.
67   //
68   // Software Guide : EndLatex
69 
70   // Software Guide : BeginCodeSnippet
71   using DistanceMetricType =
72     itk::Statistics::EuclideanDistanceMetric<MeasurementVectorType>;
73   DistanceMetricType::Pointer distanceMetric = DistanceMetricType::New();
74   // Software Guide : EndCodeSnippet
75 
76   // Software Guide : BeginLatex
77   //
78   // We create three measurement vectors, the \code{originPoint},
79   // the \code{queryPointA}, and the \code{queryPointB}. The type of the
80   // \code{originPoint} is fixed in the
81   // \subdoxygen{Statistics}{DistanceMetric} base class as
82   // \code{itk::Vector< double, length of the measurement vector of the
83   // each distance metric instance>}.
84   //
85   // Software Guide : EndLatex
86 
87   // Software Guide : BeginLatex
88   //
89   // The Distance metric does not know about the length of the measurement
90   // vectors.  We must set it explicitly using the
91   // \code{SetMeasurementVectorSize()} method.
92   //
93   // Software Guide : EndLatex
94   distanceMetric->SetMeasurementVectorSize( 2 );
95 
96   // Software Guide : BeginCodeSnippet
97   DistanceMetricType::OriginType originPoint( 2 );
98   MeasurementVectorType queryPointA( 2 );
99   MeasurementVectorType queryPointB( 2 );
100 
101   originPoint[0] = 0;
102   originPoint[1] = 0;
103 
104   queryPointA[0] = 2;
105   queryPointA[1] = 2;
106 
107   queryPointB[0] = 3;
108   queryPointB[1] = 3;
109   // Software Guide : EndCodeSnippet
110 
111 
112   // Software Guide : BeginLatex
113   //
114   // In the following code snippet, we show the uses of the three different
115   // \code{Evaluate()} methods.
116   //
117   // Software Guide : EndLatex
118 
119   // Software Guide : BeginCodeSnippet
120   distanceMetric->SetOrigin( originPoint );
121   std::cout << "Euclidean distance between the origin and the query point A = "
122             << distanceMetric->Evaluate( queryPointA )
123             << std::endl;
124 
125   std::cout << "Euclidean distance between the two query points (A and B) = "
126             << distanceMetric->Evaluate( queryPointA, queryPointB )
127             << std::endl;
128 
129   std::cout << "Coordinate distance between "
130             << "the first components of the two query points = "
131             << distanceMetric->Evaluate( queryPointA[0], queryPointB[0] )
132             << std::endl;
133   // Software Guide : EndCodeSnippet
134 
135   return EXIT_SUCCESS;
136 }
137