ITK  6.0.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 * https://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
28namespace itk
29{
52template <typename TCoordRep, unsigned int VPointDimension = 3>
53class ITK_TEMPLATE_EXPORT Point : public FixedArray<TCoordRep, VPointDimension>
54{
55public:
56
58 using Self = Point;
60
63 using ValueType = TCoordRep;
64 using CoordRepType = TCoordRep;
65
67
69 static constexpr unsigned int PointDimension = VPointDimension;
70
73 using Iterator = typename BaseArray::Iterator;
75
77 static unsigned int
79 {
80 return VPointDimension;
81 }
82
85
88 Point() = default;
89
91 template <typename TPointValueType>
93 : BaseArray(r)
94 {}
95
97 template <typename TPointValueType>
98 Point(const TPointValueType r[VPointDimension])
99 : BaseArray(r)
100 {}
101 Point(const ValueType r[VPointDimension])
102 : BaseArray(r)
103 {}
106#if defined(ITK_LEGACY_REMOVE)
108 Point(std::nullptr_t) = delete;
109
111 template <typename TPointValueType>
112 explicit Point(const TPointValueType & v)
113 : BaseArray(v)
114 {}
115 explicit Point(const ValueType & v)
116 : BaseArray(v)
117 {}
118#else
119
122 template <typename TPointValueType>
123 Point(const TPointValueType & v)
124 : BaseArray(v)
125 {}
126 Point(const ValueType & v)
127 : BaseArray(v)
128 {}
129#endif
133 explicit Point(const std::array<ValueType, VPointDimension> & stdArray)
134 : BaseArray(stdArray)
135 {}
136
138 Point &
139 operator=(const ValueType r[VPointDimension]);
140
142 bool
143 operator==(const Self & pt) const
144 {
145 return this->BaseArray::operator==(pt);
146 }
147
149
151 const Self &
152 operator+=(const VectorType & vec);
153
155 const Self &
156 operator-=(const VectorType & vec);
157
160 operator-(const Self & pnt) const;
161
163 Self
164 operator+(const VectorType & vec) const;
165
167 Self
168 operator-(const VectorType & vec) const;
169
173
175 vnl_vector_ref<TCoordRep>
177
179 vnl_vector<TCoordRep>
181
193 void
194 SetToMidPoint(const Self &, const Self &);
195
222 void
223 SetToBarycentricCombination(const Self & A, const Self & B, double alpha);
241 void
242 SetToBarycentricCombination(const Self & A, const Self & B, const Self & C, double weightForA, double weightForB);
257 void
258 SetToBarycentricCombination(const Self * P, const double * weights, unsigned int N);
263 template <typename TCoordRepB>
264 void
266 {
267 for (unsigned int i = 0; i < VPointDimension; ++i)
268 {
269 (*this)[i] = static_cast<TCoordRep>(pa[i]);
270 }
271 }
278 template <typename TCoordRepB>
279 RealType
281 {
282 RealType sum{};
283
284 for (unsigned int i = 0; i < VPointDimension; ++i)
285 {
286 const auto component = static_cast<RealType>(pa[i]);
287 const RealType difference = static_cast<RealType>((*this)[i]) - component;
288 sum += difference * difference;
289 }
290 return sum;
291 }
292
296 template <typename TCoordRepB>
297 RealType
299 {
300 const double distance = std::sqrt(static_cast<double>(this->SquaredEuclideanDistanceTo(pa)));
301
302 return static_cast<RealType>(distance);
303 }
304};
305
306template <typename T, unsigned int VPointDimension>
307std::ostream &
308operator<<(std::ostream & os, const Point<T, VPointDimension> & vct);
309
310template <typename T, unsigned int VPointDimension>
311std::istream &
312operator>>(std::istream & is, Point<T, VPointDimension> & vct);
313
340template <typename TPointContainer, typename TWeightContainer>
341class ITK_TEMPLATE_EXPORT BarycentricCombination
342{
343public:
344
346 using PointContainerType = TPointContainer;
348 using PointType = typename PointContainerType::Element;
349 using WeightContainerType = TWeightContainer;
350
351 static PointType
352 Evaluate(const PointContainerPointer & points, const WeightContainerType & weights);
353};
354
355
356template <typename TCoordRep, unsigned int VPointDimension>
357inline void
359{
360 a.swap(b);
361}
362
363
365template <typename TValue, typename... TVariadic>
366auto
367MakePoint(const TValue firstValue, const TVariadic... otherValues)
368{
369 static_assert(std::conjunction_v<std::is_same<TVariadic, TValue>...>,
370 "The other values should have the same type as the first value.");
371
372 constexpr unsigned int dimension{ 1 + sizeof...(TVariadic) };
373 const std::array<TValue, dimension> stdArray{ { firstValue, otherValues... } };
374 return Point<TValue, dimension>{ stdArray };
375}
376
377} // end namespace itk
378
379#ifndef ITK_MANUAL_INSTANTIATION
380# include "itkPoint.hxx"
381#endif
382
383#endif
Computes the barycentric combination of an array of N points.
Definition: itkPoint.h:342
static PointType Evaluate(const PointContainerPointer &points, const WeightContainerType &weights)
typename PointContainerType::Element PointType
Definition: itkPoint.h:348
TWeightContainer WeightContainerType
Definition: itkPoint.h:349
TPointContainer PointContainerType
Definition: itkPoint.h:346
typename PointContainerType::Pointer PointContainerPointer
Definition: itkPoint.h:347
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
void swap(FixedArray &other)
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:54
typename NumericTraits< ValueType >::RealType RealType
Definition: itkPoint.h:66
Point()=default
typename BaseArray::Iterator Iterator
Definition: itkPoint.h:73
RealType EuclideanDistanceTo(const Point< TCoordRepB, VPointDimension > &pa) const
Definition: itkPoint.h:298
Self operator-(const VectorType &vec) const
Self operator+(const VectorType &vec) const
vnl_vector< TCoordRep > GetVnlVector() const
void SetToMidPoint(const Self &, const Self &)
Point(const TPointValueType r[VPointDimension])
Definition: itkPoint.h:98
Point(const TPointValueType &v)
Definition: itkPoint.h:112
Point(std::nullptr_t)=delete
TCoordRep ValueType
Definition: itkPoint.h:63
const Self & operator+=(const VectorType &vec)
bool operator==(const Self &pt) const
Definition: itkPoint.h:143
typename BaseArray::ConstIterator ConstIterator
Definition: itkPoint.h:74
Point(const Point< TPointValueType, VPointDimension > &r)
Definition: itkPoint.h:92
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
TCoordRep CoordRepType
Definition: itkPoint.h:64
const Self & operator-=(const VectorType &vec)
VectorType GetVectorFromOrigin() const
Point(const std::array< ValueType, VPointDimension > &stdArray)
Definition: itkPoint.h:133
VectorType operator-(const Self &pnt) const
Point & operator=(const ValueType r[VPointDimension])
vnl_vector_ref< TCoordRep > GetVnlVector()
void SetToBarycentricCombination(const Self &A, const Self &B, double alpha)
void SetToBarycentricCombination(const Self &A, const Self &B, const Self &C, double weightForA, double weightForB)
Point(const ValueType &v)
Definition: itkPoint.h:115
RealType SquaredEuclideanDistanceTo(const Point< TCoordRepB, VPointDimension > &pa) const
Definition: itkPoint.h:280
void CastFrom(const Point< TCoordRepB, VPointDimension > &pa)
Definition: itkPoint.h:265
void SetToBarycentricCombination(const Self *P, const double *weights, unsigned int N)
static unsigned int GetPointDimension()
Definition: itkPoint.h:78
Point(const ValueType r[VPointDimension])
Definition: itkPoint.h:101
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:63
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:543
auto MakePoint(const TValue firstValue, const TVariadic... otherValues)
Definition: itkPoint.h:367