ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkPoint.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkPoint_h
00019 #define __itkPoint_h
00020 
00021 
00022 #include "itkNumericTraits.h"
00023 #include "itkVector.h"
00024 
00025 #include "vnl/vnl_vector_ref.h"
00026 
00027 namespace itk
00028 {
00050 template< class TCoordRep, unsigned int NPointDimension = 3 >
00051 class Point:public FixedArray< TCoordRep, NPointDimension >
00052 {
00053 public:
00055   typedef Point                                    Self;
00056   typedef FixedArray< TCoordRep, NPointDimension > Superclass;
00057 
00060   typedef TCoordRep ValueType;
00061   typedef TCoordRep CoordRepType;
00062 
00063   typedef typename NumericTraits< ValueType >::RealType RealType;
00064 
00066   itkStaticConstMacro(PointDimension, unsigned int, NPointDimension);
00067 
00069   typedef FixedArray< TCoordRep, NPointDimension > BaseArray;
00070   typedef typename BaseArray::Iterator             Iterator;
00071   typedef typename BaseArray::ConstIterator        ConstIterator;
00072 
00074   static unsigned int GetPointDimension()
00075   { return NPointDimension; }
00076 
00078   typedef Vector< ValueType, NPointDimension > VectorType;
00079 
00081   Point() {}
00082 
00084   template< class TPointValueType >
00085   Point(const Point< TPointValueType, NPointDimension > & r):BaseArray(r) {}
00086   Point(const ValueType r[NPointDimension]):BaseArray(r) {}
00087   Point(const ValueType & v):BaseArray(v) {}
00089 
00091   Point & operator=(const Self & r);
00092 
00093   Point & operator=(const ValueType r[NPointDimension]);
00094 
00096   bool
00097   operator==(const Self & pt) const
00098   {
00099     bool same = true;
00100 
00101     for ( unsigned int i = 0; i < NPointDimension && same; i++ )
00102           { same = ( ( *this )[i] == pt[i] ); }
00103     return same;
00104   }
00105 
00107   bool
00108   operator!=(const Self & pt) const
00109   {
00110     bool same = true;
00111 
00112     for ( unsigned int i = 0; i < NPointDimension && same; i++ )
00113           { same = ( ( *this )[i] == pt[i] ); }
00114     return !same;
00115   }
00116 
00118   const Self & operator+=(const VectorType & vec);
00119 
00121   const Self & operator-=(const VectorType & vec);
00122 
00124   VectorType operator-(const Self & pnt) const;
00125 
00127   Self operator+(const VectorType & vec) const;
00128 
00130   Self operator-(const VectorType & vec) const;
00131 
00133   VectorType GetVectorFromOrigin() const;
00134 
00136   vnl_vector_ref< TCoordRep > GetVnlVector(void);
00137 
00139   vnl_vector< TCoordRep > GetVnlVector(void) const;
00140 
00143   vnl_vector_ref< TCoordRep > Get_vnl_vector(void);
00144 
00147   vnl_vector< TCoordRep > Get_vnl_vector(void) const;
00148 
00160   void SetToMidPoint(const Self &, const Self &);
00161 
00188   void SetToBarycentricCombination(const Self & A, const Self & B, double alpha);
00190 
00206   void SetToBarycentricCombination(const Self & A, const Self & B, const Self & C,
00207                                    double weightA,  double weightB);
00209 
00222   void SetToBarycentricCombination(const Self *P, const double *weights, unsigned int N);
00224 
00227   template< typename TCoordRepB >
00228   void CastFrom(const Point< TCoordRepB, NPointDimension > & pa)
00229   {
00230     for ( unsigned int i = 0; i < NPointDimension; i++ )
00231       {
00232       ( *this )[i] = static_cast< TCoordRep >( pa[i] );
00233       }
00234   }
00236 
00241   template< typename TCoordRepB >
00242   RealType SquaredEuclideanDistanceTo(const Point< TCoordRepB, NPointDimension > & pa) const
00243   {
00244     RealType sum = NumericTraits< RealType >::Zero;
00245 
00246     for ( unsigned int i = 0; i < NPointDimension; i++ )
00247       {
00248       const RealType component =  static_cast< RealType >( pa[i] );
00249       const RealType difference = static_cast< RealType >( ( *this )[i] ) - component;
00250       sum += difference * difference;
00251       }
00252     return sum;
00253   }
00254 
00258   template< typename TCoordRepB >
00259   RealType EuclideanDistanceTo(const Point< TCoordRepB, NPointDimension > & pa) const
00260   {
00261     const double distance = vcl_sqrt(
00262       static_cast< double >( this->SquaredEuclideanDistanceTo(pa) ) );
00263 
00264     return static_cast< RealType >( distance );
00265   }
00266 };
00267 
00268 template< class T, unsigned int NPointDimension >
00269 ITK_EXPORT std::ostream & operator<<(std::ostream & os,
00270                                      const Point< T, NPointDimension > & v);
00271 
00272 template< class T, unsigned int NPointDimension >
00273 ITK_EXPORT std::istream & operator>>(std::istream & is,
00274                                      Point< T, NPointDimension > & v);
00275 
00301 template< class TPointContainer, class TWeightContainer >
00302 ITK_EXPORT class BarycentricCombination
00303 {
00304 public:
00306   typedef TPointContainer                      PointContainerType;
00307   typedef typename PointContainerType::Pointer PointContainerPointer;
00308   typedef typename PointContainerType::Element PointType;
00309   typedef TWeightContainer                     WeightContainerType;
00310 
00311   BarycentricCombination() {}
00312   ~BarycentricCombination() {}
00313 
00314   static PointType Evaluate(
00315     const PointContainerPointer & points,
00316     const WeightContainerType & weights);
00317 };
00318 }  // end namespace itk
00319 
00320 // Define instantiation macro for this template.
00321 #define ITK_TEMPLATE_Point(_, EXPORT, TypeX, TypeY)                                  \
00322   namespace itk                                                                      \
00323   {                                                                                  \
00324   _( 2 ( class EXPORT Point< ITK_TEMPLATE_2 TypeX > ) )                              \
00325   _( 1 ( EXPORT std::ostream & operator<<(std::ostream &,                            \
00326                                           const Point< ITK_TEMPLATE_2 TypeX > &) ) ) \
00327   _( 1 ( EXPORT std::istream & operator>>(std::istream &,                            \
00328                                           Point< ITK_TEMPLATE_2 TypeX > &) ) )       \
00329   namespace Templates                                                                \
00330   {                                                                                  \
00331   typedef Point< ITK_TEMPLATE_2 TypeX > Point##TypeY;                              \
00332   }                                                                                  \
00333   }
00334 
00335 #if ITK_TEMPLATE_EXPLICIT
00336 #include "Templates/itkPoint+-.h"
00337 #endif
00338 
00339 #if ITK_TEMPLATE_TXX
00340 #include "itkPoint.hxx"
00341 #endif
00342 
00343 #endif
00344