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 itkSphereSpatialFunction_hxx
19 #define itkSphereSpatialFunction_hxx
20 
21 #include "itkSphereSpatialFunction.h"
22 
23 namespace itk
24 {
25 template< unsigned int VImageDimension, typename TInput >
26 SphereSpatialFunction< VImageDimension, TInput >
SphereSpatialFunction()27 ::SphereSpatialFunction()
28 {
29   m_Radius = 1.0;
30 
31   m_Center.Fill(0.0f);
32 }
33 
34 template< unsigned int VImageDimension, typename TInput >
35 typename SphereSpatialFunction< VImageDimension, TInput >::OutputType
36 SphereSpatialFunction< VImageDimension, TInput >
Evaluate(const InputType & position) const37 ::Evaluate(const InputType & position) const
38 {
39   double acc = 0;
40 
41   for ( unsigned int i = 0; i < VImageDimension; i++ )
42     {
43     acc += ( position[i] - m_Center[i] ) * ( position[i] - m_Center[i] );
44     }
45 
46   acc -= m_Radius * m_Radius;
47 
48   if ( acc <= 0 ) // inside the sphere
49     {
50     return 1;
51     }
52   else
53     {
54     return 0; // outside the sphere
55     }
56 }
57 
58 template< unsigned int VImageDimension, typename TInput >
59 void
60 SphereSpatialFunction< VImageDimension, TInput >
PrintSelf(std::ostream & os,Indent indent) const61 ::PrintSelf(std::ostream & os, Indent indent) const
62 {
63   Superclass::PrintSelf(os, indent);
64 
65   unsigned int i;
66   os << indent << "Center: [";
67   for ( i = 0; i < VImageDimension - 1; i++ )
68     {
69     os << m_Center[i] << ", ";
70     }
71   os << "]" << std::endl;
72 
73   os << indent << "Radius: " << m_Radius << std::endl;
74 }
75 } // end namespace itk
76 
77 #endif
78