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 WeightedPoint(std::array<double, dim> const& coords, double const weight)
28 : weight_{weight}, dim_{dim}
29 {
30 static_assert(dim <= 3);
31 std::size_t i = 0;
32 if constexpr (dim > 0) // avoids compiler warning
33 {
34 for (; i < dim; ++i)
35 {
36 coords_[i] = coords[i];
37 }
38 }
39 for (; i < 3; ++i)
40 {
41 // fill the rest with NaN for safety reasons
42 coords_[i] = std::numeric_limits<double>::quiet_NaN();
43 }
44 }
45
47 explicit WeightedPoint(double const weight) : weight_{weight}, dim_{0}
48 {
49 // fill with NaN for safety reasons
50 coords_.fill(std::numeric_limits<double>::quiet_NaN());
51 }
52
53 bool operator==(WeightedPoint const& other) const
54 {
55 if (weight_ != other.weight_)
56 {
57 return false;
58 }
59 if (dim_ != other.dim_)
60 {
61 return false;
62 }
63 for (std::size_t comp = 0; comp < dim_; ++comp)
64 {
65 if (coords_[comp] != other.coords_[comp])
66 {
67 return false;
68 }
69 }
70 return true;
71 }
72
73 bool operator!=(WeightedPoint const& other) const
74 {
75 return !(*this == other);
76 }
77
78 const double* data() const { return coords_.data(); }
79
80 double getWeight() const { return weight_; }
81
83 std::size_t getDimension() const { return dim_; }
84
86 double operator[](std::size_t coord_idx) const
87 {
88 return coords_[coord_idx];
89 }
90
91private:
92 double weight_;
93 std::array<double, 3> coords_;
94 std::size_t dim_;
95};
96
97std::ostream& operator<<(std::ostream& os, MathLib::WeightedPoint const& wp);
98
99} // namespace MathLib
bool operator==(WeightedPoint const &other) const
std::size_t getDimension() const
The point dimension, i.e., the number of its coordinates.
bool operator!=(WeightedPoint const &other) const
WeightedPoint(double const weight)
Constructs a 0D weighted point.
std::array< double, 3 > coords_
const double * data() const
double operator[](std::size_t coord_idx) const
Access a specific coordinate.
double getWeight() const
WeightedPoint(std::array< double, dim > const &coords, double const weight)
std::ostream & operator<<(std::ostream &os, const Point3d &p)
Definition Point3d.h:92