ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkPoint.h
Go to the documentation of this file.
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 itkPoint_h
19 #define itkPoint_h
20 
21 
22 #include "itkNumericTraits.h"
23 #include "itkVector.h"
24 
25 #include "vnl/vnl_vector_ref.h"
26 #include "itkMath.h"
27 
28 namespace itk
29 {
51 template< typename TCoordRep, unsigned int NPointDimension = 3 >
52 class ITK_TEMPLATE_EXPORT Point:public FixedArray< TCoordRep, NPointDimension >
53 {
54 public:
56  using Self = Point;
58 
61  using ValueType = TCoordRep;
62  using CoordRepType = TCoordRep;
63 
65 
67  static constexpr unsigned int PointDimension = NPointDimension;
68 
71  using Iterator = typename BaseArray::Iterator;
73 
75  static unsigned int GetPointDimension()
76  { return NPointDimension; }
77 
80 
82  Point() = default;
83  Point(const Point &) = default;
84  Point(Point &&) = default;
85  Point & operator=(const Point &) = default;
86  Point & operator=(Point &&) = default;
87  ~Point() = default;
88 
90  template< typename TPointValueType >
92 
94  template< typename TPointValueType >
95  Point(const TPointValueType r[NPointDimension]):BaseArray(r) {}
96  Point(const ValueType r[NPointDimension]):BaseArray(r) {}
97 
99  template< typename TPointValueType >
100  Point(const TPointValueType & v):BaseArray(v) {}
101  Point(const ValueType & v):BaseArray(v) {}
103 
105  Point & operator=(const ValueType r[NPointDimension]);
106 
108  bool
109  operator==(const Self & pt) const
110  {
111  bool same = true;
112 
113  for ( unsigned int i = 0; i < NPointDimension && same; ++i )
114  { same = ( Math::ExactlyEquals(( *this )[i], pt[i]) ); }
115  return same;
116  }
117 
119  bool
120  operator!=(const Self & pt) const
121  {
122  bool same = true;
123 
124  for ( unsigned int i = 0; i < NPointDimension && same; ++i )
125  { same = ( Math::ExactlyEquals(( *this )[i], pt[i]) ); }
126  return !same;
127  }
128 
130  const Self & operator+=(const VectorType & vec);
131 
133  const Self & operator-=(const VectorType & vec);
134 
136  VectorType operator-(const Self & pnt) const;
137 
139  Self operator+(const VectorType & vec) const;
140 
142  Self operator-(const VectorType & vec) const;
143 
145  VectorType GetVectorFromOrigin() const;
146 
148  vnl_vector_ref< TCoordRep > GetVnlVector();
149 
151  vnl_vector< TCoordRep > GetVnlVector() const;
152 
164  void SetToMidPoint(const Self &, const Self &);
165 
192  void SetToBarycentricCombination(const Self & A, const Self & B, double alpha);
194 
210  void SetToBarycentricCombination(const Self & A, const Self & B, const Self & C,
211  double weightA, double weightB);
213 
226  void SetToBarycentricCombination(const Self *P, const double *weights, unsigned int N);
228 
231  template< typename TCoordRepB >
233  {
234  for ( unsigned int i = 0; i < NPointDimension; i++ )
235  {
236  ( *this )[i] = static_cast< TCoordRep >( pa[i] );
237  }
238  }
240 
245  template< typename TCoordRepB >
247  {
249 
250  for ( unsigned int i = 0; i < NPointDimension; i++ )
251  {
252  const auto component = static_cast< RealType >( pa[i] );
253  const RealType difference = static_cast< RealType >( ( *this )[i] ) - component;
254  sum += difference * difference;
255  }
256  return sum;
257  }
258 
262  template< typename TCoordRepB >
264  {
265  const double distance = std::sqrt(
266  static_cast< double >( this->SquaredEuclideanDistanceTo(pa) ) );
267 
268  return static_cast< RealType >( distance );
269  }
270 };
271 
272 template< typename T, unsigned int NPointDimension >
273 std::ostream & operator<<(std::ostream & os,
274  const Point< T, NPointDimension > & v);
275 
276 template< typename T, unsigned int NPointDimension >
277 std::istream & operator>>(std::istream & is,
278  Point< T, NPointDimension > & v);
279 
305 template< typename TPointContainer, typename TWeightContainer >
306 class ITK_TEMPLATE_EXPORT BarycentricCombination
307 {
308 public:
310  using PointContainerType = TPointContainer;
311  using PointContainerPointer = typename PointContainerType::Pointer;
312  using PointType = typename PointContainerType::Element;
313  using WeightContainerType = TWeightContainer;
314 
315  BarycentricCombination() = default;
316  ~BarycentricCombination() = default;
317 
318  static PointType Evaluate(
319  const PointContainerPointer & points,
320  const WeightContainerType & weights);
321 };
322 
323 
324 template<typename TCoordRep, unsigned int NPointDimension>
326 {
327  a.swap(b);
328 }
329 
330 } // end namespace itk
331 
332 #ifndef ITK_MANUAL_INSTANTIATION
333 #include "itkPoint.hxx"
334 #endif
335 
336 #endif
void CastFrom(const Point< TCoordRepB, NPointDimension > &pa)
Definition: itkPoint.h:232
bool operator==(const Self &pt) const
Definition: itkPoint.h:109
typename NumericTraits< ValueType >::RealType RealType
Definition: itkPoint.h:64
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Define numeric traits for std::vector.
Point(const TPointValueType r[NPointDimension])
Definition: itkPoint.h:95
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &v)
Point(const Point< TPointValueType, NPointDimension > &r)
Definition: itkPoint.h:91
Computes the barycentric combination of an array of N points.
Definition: itkPoint.h:306
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:188
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potetially different types...
Definition: itkMath.h:707
Point(const TPointValueType &v)
Definition: itkPoint.h:100
typename PointContainerType::Pointer PointContainerPointer
Definition: itkPoint.h:311
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:51
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
bool operator!=(const Self &pt) const
Definition: itkPoint.h:120
Point(const ValueType r[NPointDimension])
Definition: itkPoint.h:96
Point(const ValueType &v)
Definition: itkPoint.h:101
static unsigned int GetPointDimension()
Definition: itkPoint.h:75
RealType SquaredEuclideanDistanceTo(const Point< TCoordRepB, NPointDimension > &pa) const
Definition: itkPoint.h:246
RealType EuclideanDistanceTo(const Point< TCoordRepB, NPointDimension > &pa) const
Definition: itkPoint.h:263
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
TPointContainer PointContainerType
Definition: itkPoint.h:310
TWeightContainer WeightContainerType
Definition: itkPoint.h:313
typename PointContainerType::Element PointType
Definition: itkPoint.h:312