OGS
TemplatePoint.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 #include "mathlib_export.h"
18 
19 // STL
20 #include <algorithm>
21 #include <array>
22 #include <cassert>
23 #include <cmath>
24 #include <istream>
25 #include <iterator>
26 #include <utility>
27 
28 namespace MathLib
29 {
36 template <typename T, std::size_t DIM = 3> class TemplatePoint
37 {
38 public:
41 
46  explicit TemplatePoint(std::array<T, DIM> x);
47 
49  virtual ~TemplatePoint() = default;
50 
51  TemplatePoint(TemplatePoint const&) = default;
53 
61  const T& operator[] (std::size_t idx) const
62  {
63  assert (idx < DIM);
64  return x_[idx];
65  }
69  T& operator[] (std::size_t idx)
70  {
71  return const_cast<T&> (static_cast<const TemplatePoint&> (*this)[idx]);
72  }
73 
75  const T* getCoords() const { return x_.data(); }
76 
77  T* getCoords() { return x_.data(); }
78 
82  virtual void write (std::ostream &os) const
83  {
84  std::copy(x_.cbegin(), x_.cend(), std::ostream_iterator<T>(os, " "));
85  }
86 
88  virtual void read (std::istream &is)
89  {
90  std::copy(std::istream_iterator<T>(is), std::istream_iterator<T>(),
91  x_.begin());
92  }
93 
94 private:
95  std::array<T, DIM> x_;
96 };
97 
98 template <typename T, std::size_t DIM>
100 {}
101 
102 template <typename T, std::size_t DIM>
103 TemplatePoint<T, DIM>::TemplatePoint(std::array<T, DIM> x) : x_(std::move(x))
104 {}
105 
108 template <typename T, std::size_t DIM>
110 {
111  T const sqr_dist(sqrDist(a,b));
112  auto const eps = std::numeric_limits<T>::epsilon();
113  return (sqr_dist < eps*eps);
114 }
115 
116 template <typename T, std::size_t DIM>
118 {
119  for (std::size_t i = 0; i < DIM; ++i)
120  {
121  if (a[i] > b[i]) {
122  return false;
123  }
124  if (a[i] < b[i])
125  {
126  return true;
127  }
128 
129  // continue with next dimension, because a[0] == b[0]
130  }
131 
132  // The values in all dimenisions are equal.
133  return false;
134 }
135 
147 template <typename T, std::size_t DIM>
149  double eps = std::numeric_limits<double>::epsilon())
150 {
151  auto coordinateIsLargerEps = [&eps](T const u, T const v) -> bool
152  {
153  return std::abs(u - v) > eps * std::min(std::abs(v), std::abs(u)) &&
154  std::abs(u - v) > eps;
155  };
156 
157  for (std::size_t i = 0; i < DIM; ++i)
158  {
159  // test a relative and an absolute criterion
160  if (coordinateIsLargerEps(a[i], b[i]))
161  {
162  return static_cast<bool>(a[i] <= b[i]);
163  }
164  // a[i] ~= b[i] up to an epsilon. Compare next dimension.
165  }
166 
167  // all coordinates are equal up to an epsilon.
168  return true;
169 }
170 
172 template <typename T, std::size_t DIM>
173 std::ostream& operator<< (std::ostream &os, const TemplatePoint<T,DIM> &p)
174 {
175  p.write (os);
176  return os;
177 }
178 
180 template <typename T, std::size_t DIM>
181 std::istream& operator>> (std::istream &is, TemplatePoint<T,DIM> &p)
182 {
183  p.read (is);
184  return is;
185 }
186 } // end namespace MathLib
class-template for points can be instantiated by a numeric type.
Definition: TemplatePoint.h:37
virtual ~TemplatePoint()=default
std::array< T, DIM > x_
Definition: TemplatePoint.h:95
virtual void write(std::ostream &os) const
Definition: TemplatePoint.h:82
TemplatePoint(TemplatePoint const &)=default
TemplatePoint(std::array< T, DIM > x)
const T * getCoords() const
Definition: TemplatePoint.h:75
TemplatePoint & operator=(TemplatePoint const &)=default
const T & operator[](std::size_t idx) const
const access operator The access to the point coordinates is like the access to a field....
Definition: TemplatePoint.h:61
virtual void read(std::istream &is)
Definition: TemplatePoint.h:88
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
bool operator==(TemplatePoint< T, DIM > const &a, TemplatePoint< T, DIM > const &b)
std::ostream & operator<<(std::ostream &os, const TemplatePoint< T, DIM > &p)
bool operator<(TemplatePoint< T, DIM > const &a, TemplatePoint< T, DIM > const &b)
std::istream & operator>>(std::istream &is, TemplatePoint< T, DIM > &p)
bool lessEq(TemplatePoint< T, DIM > const &a, TemplatePoint< T, DIM > const &b, double eps=std::numeric_limits< double >::epsilon())
static const double p
double sqrDist(MathLib::Point3d const &p0, MathLib::Point3d const &p1)
Definition: Point3d.h:48