ITK  5.3.0
Insight Toolkit
itkPoint.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
52 template <typename TCoordRep, unsigned int NPointDimension = 3>
53 class ITK_TEMPLATE_EXPORT Point : public FixedArray<TCoordRep, NPointDimension>
54 {
55 public:
56 
58  using Self = Point;
60 
63  using ValueType = TCoordRep;
64  using CoordRepType = TCoordRep;
65 
67 
69  static constexpr unsigned int PointDimension = NPointDimension;
70 
73  using Iterator = typename BaseArray::Iterator;
75 
77  static unsigned int
79  {
80  return NPointDimension;
81  }
82 
85 
87  Point() = default;
88  Point(const Point &) = default;
89  Point(Point &&) = default;
90  Point &
91  operator=(const Point &) = default;
92  Point &
93  operator=(Point &&) = default;
94  ~Point() = default;
95 
97  template <typename TPointValueType>
99  : BaseArray(r)
100  {}
101 
103  template <typename TPointValueType>
104  Point(const TPointValueType r[NPointDimension])
105  : BaseArray(r)
106  {}
107  Point(const ValueType r[NPointDimension])
108  : BaseArray(r)
109  {}
110 
112  template <typename TPointValueType>
113  Point(const TPointValueType & v)
114  : BaseArray(v)
115  {}
116  Point(const ValueType & v)
117  : BaseArray(v)
118  {}
120 
122  explicit Point(const std::array<ValueType, NPointDimension> & stdArray)
123  : BaseArray(stdArray)
124  {}
125 
127  Point &
128  operator=(const ValueType r[NPointDimension]);
129 
131  bool
132  operator==(const Self & pt) const
133  {
134  bool same = true;
135 
136  for (unsigned int i = 0; i < NPointDimension && same; ++i)
137  {
138  same = (Math::ExactlyEquals((*this)[i], pt[i]));
139  }
140  return same;
141  }
142 
143  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
144 
146  const Self &
147  operator+=(const VectorType & vec);
148 
150  const Self &
151  operator-=(const VectorType & vec);
152 
154  VectorType
155  operator-(const Self & pnt) const;
156 
158  Self
159  operator+(const VectorType & vec) const;
160 
162  Self
163  operator-(const VectorType & vec) const;
164 
166  VectorType
167  GetVectorFromOrigin() const;
168 
170  vnl_vector_ref<TCoordRep>
171  GetVnlVector();
172 
174  vnl_vector<TCoordRep>
175  GetVnlVector() const;
176 
188  void
189  SetToMidPoint(const Self &, const Self &);
190 
217  void
218  SetToBarycentricCombination(const Self & A, const Self & B, double alpha);
220 
236  void
237  SetToBarycentricCombination(const Self & A, const Self & B, const Self & C, double weightForA, double weightForB);
239 
252  void
253  SetToBarycentricCombination(const Self * P, const double * weights, unsigned int N);
255 
258  template <typename TCoordRepB>
259  void
261  {
262  for (unsigned int i = 0; i < NPointDimension; ++i)
263  {
264  (*this)[i] = static_cast<TCoordRep>(pa[i]);
265  }
266  }
268 
273  template <typename TCoordRepB>
274  RealType
276  {
278 
279  for (unsigned int i = 0; i < NPointDimension; ++i)
280  {
281  const auto component = static_cast<RealType>(pa[i]);
282  const RealType difference = static_cast<RealType>((*this)[i]) - component;
283  sum += difference * difference;
284  }
285  return sum;
286  }
287 
291  template <typename TCoordRepB>
292  RealType
294  {
295  const double distance = std::sqrt(static_cast<double>(this->SquaredEuclideanDistanceTo(pa)));
296 
297  return static_cast<RealType>(distance);
298  }
299 };
300 
301 template <typename T, unsigned int NPointDimension>
302 std::ostream &
303 operator<<(std::ostream & os, const Point<T, NPointDimension> & vct);
304 
305 template <typename T, unsigned int NPointDimension>
306 std::istream &
307 operator>>(std::istream & is, Point<T, NPointDimension> & vct);
308 
335 template <typename TPointContainer, typename TWeightContainer>
336 class ITK_TEMPLATE_EXPORT BarycentricCombination
337 {
338 public:
339 
341  using PointContainerType = TPointContainer;
342  using PointContainerPointer = typename PointContainerType::Pointer;
343  using PointType = typename PointContainerType::Element;
344  using WeightContainerType = TWeightContainer;
345 
346  BarycentricCombination() = default;
347  ~BarycentricCombination() = default;
348 
349  static PointType
350  Evaluate(const PointContainerPointer & points, const WeightContainerType & weights);
351 };
352 
353 
354 template <typename TCoordRep, unsigned int NPointDimension>
355 inline void
357 {
358  a.swap(b);
359 }
360 
361 
363 template <typename TValue, typename... TVariadic>
364 auto
365 MakePoint(const TValue firstValue, const TVariadic... otherValues)
366 {
367  // Assert that the other values have the same type as the first value.
368  const auto assertSameType = [](const auto value) {
369  static_assert(std::is_same<decltype(value), const TValue>::value, "Each value must have the same type!");
370  return true;
371  };
372  const bool assertions[] = { true, assertSameType(otherValues)... };
373  (void)assertions;
374  (void)assertSameType;
376 
377  constexpr unsigned dimension{ 1 + sizeof...(TVariadic) };
378  const std::array<TValue, dimension> stdArray{ { firstValue, otherValues... } };
379  return Point<TValue, dimension>{ stdArray };
380 }
381 
382 } // end namespace itk
383 
384 #ifndef ITK_MANUAL_INSTANTIATION
385 # include "itkPoint.hxx"
386 #endif
387 
388 #endif
itk::MakePoint
auto MakePoint(const TValue firstValue, const TVariadic... otherValues)
Definition: itkPoint.h:365
itk::Point< double, Self::ImageDimension >::CoordRepType
double CoordRepType
Definition: itkPoint.h:64
itk::BarycentricCombination
Computes the barycentric combination of an array of N points.
Definition: itkPoint.h:336
itk::BarycentricCombination::WeightContainerType
TWeightContainer WeightContainerType
Definition: itkPoint.h:344
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::Point::SquaredEuclideanDistanceTo
RealType SquaredEuclideanDistanceTo(const Point< TCoordRepB, NPointDimension > &pa) const
Definition: itkPoint.h:275
itk::Point::Point
Point(const std::array< ValueType, NPointDimension > &stdArray)
Definition: itkPoint.h:122
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itk::Math::ExactlyEquals
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potentially different types.
Definition: itkMath.h:723
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:672
itk::Point::Point
Point(const TPointValueType &v)
Definition: itkPoint.h:113
itk::Point::GetPointDimension
static unsigned int GetPointDimension()
Definition: itkPoint.h:78
itk::FixedArray< double, NPointDimension >::ConstIterator
const ValueType * ConstIterator
Definition: itkFixedArray.h:71
itk::Point::Point
Point(const TPointValueType r[NPointDimension])
Definition: itkPoint.h:104
itk::Point< double, Self::ImageDimension >::RealType
typename NumericTraits< ValueType >::RealType RealType
Definition: itkPoint.h:66
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &vct)
itk::BarycentricCombination::PointType
typename PointContainerType::Element PointType
Definition: itkPoint.h:343
itk::Point::Point
Point(const Point< TPointValueType, NPointDimension > &r)
Definition: itkPoint.h:98
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::BarycentricCombination::PointContainerPointer
typename PointContainerType::Pointer PointContainerPointer
Definition: itkPoint.h:342
itk::Point::EuclideanDistanceTo
RealType EuclideanDistanceTo(const Point< TCoordRepB, NPointDimension > &pa) const
Definition: itkPoint.h:293
itkVector.h
itk::FixedArray< TCoordRep, NPointDimension >::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:422
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:654
itkNumericTraits.h
itk::Point
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:53
itk::Point::CastFrom
void CastFrom(const Point< TCoordRepB, NPointDimension > &pa)
Definition: itkPoint.h:260
itk::Point::Point
Point(const ValueType &v)
Definition: itkPoint.h:116
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:84
itk::BarycentricCombination::PointContainerType
TPointContainer PointContainerType
Definition: itkPoint.h:341
itk::Point::operator==
bool operator==(const Self &pt) const
Definition: itkPoint.h:132
itkMath.h
itk::Point::Point
Point(const ValueType r[NPointDimension])
Definition: itkPoint.h:107
itk::FixedArray< double, NPointDimension >::Iterator
ValueType * Iterator
Definition: itkFixedArray.h:68
itk::FixedArray< double, NPointDimension >::ValueType
double ValueType
Definition: itkFixedArray.h:62