OGS
WeightedPoint.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <array>
13#include <iosfwd>
14#include <limits>
15
16namespace MathLib
17{
22{
23public:
26 template <std::size_t dim>
27 constexpr WeightedPoint(std::array<double, dim> const& coords,
28 double const weight)
29 : weight_{weight}, dim_{dim}
30 {
31 static_assert(dim <= 3);
32 std::size_t i = 0;
33 if constexpr (dim > 0) // avoids compiler warning
34 {
35 for (; i < dim; ++i)
36 {
37 coords_[i] = coords[i];
38 }
39 }
40 for (; i < 3; ++i)
41 {
42 // fill the rest with NaN for safety reasons
43 coords_[i] = std::numeric_limits<double>::quiet_NaN();
44 }
45 }
46
48 explicit constexpr WeightedPoint(double const weight)
49 : weight_{weight}, dim_{0}
50 {
51 // fill with NaN for safety reasons
52 coords_.fill(std::numeric_limits<double>::quiet_NaN());
53 }
54
55 constexpr bool operator==(WeightedPoint const& other) const
56 {
57 if (weight_ != other.weight_)
58 {
59 return false;
60 }
61 if (dim_ != other.dim_)
62 {
63 return false;
64 }
65 for (std::size_t comp = 0; comp < dim_; ++comp)
66 {
67 if (coords_[comp] != other.coords_[comp])
68 {
69 return false;
70 }
71 }
72 return true;
73 }
74
75 constexpr bool operator!=(WeightedPoint const& other) const
76 {
77 return !(*this == other);
78 }
79
80 constexpr const double* data() const { return coords_.data(); }
81
82 constexpr double getWeight() const { return weight_; }
83
85 constexpr std::size_t getDimension() const { return dim_; }
86
88 constexpr double operator[](std::size_t coord_idx) const
89 {
90 return coords_[coord_idx];
91 }
92
93private:
94 double weight_;
95 std::array<double, 3> coords_;
96 std::size_t dim_;
97};
98
99std::ostream& operator<<(std::ostream& os, MathLib::WeightedPoint const& wp);
100
101} // namespace MathLib
constexpr bool operator!=(WeightedPoint const &other) const
constexpr const double * data() const
constexpr double operator[](std::size_t coord_idx) const
Access a specific coordinate.
constexpr WeightedPoint(std::array< double, dim > const &coords, double const weight)
constexpr double getWeight() const
constexpr bool operator==(WeightedPoint const &other) const
constexpr std::size_t getDimension() const
The point dimension, i.e., the number of its coordinates.
std::array< double, 3 > coords_
constexpr WeightedPoint(double const weight)
Constructs a 0D weighted point.
std::ostream & operator<<(std::ostream &os, const Point3d &p)
Definition Point3d.h:92