OGS
WeightedPoint.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <array>
7#include <iosfwd>
8#include <limits>
9
10namespace MathLib
11{
16{
17public:
20 template <std::size_t dim>
21 constexpr WeightedPoint(std::array<double, dim> const& coords,
22 double const weight)
23 : weight_{weight}, dim_{dim}
24 {
25 static_assert(dim <= 3);
26 std::size_t i = 0;
27 if constexpr (dim > 0) // avoids compiler warning
28 {
29 for (; i < dim; ++i)
30 {
31 coords_[i] = coords[i];
32 }
33 }
34 for (; i < 3; ++i)
35 {
36 // fill the rest with NaN for safety reasons
37 coords_[i] = std::numeric_limits<double>::quiet_NaN();
38 }
39 }
40
42 explicit constexpr WeightedPoint(double const weight)
43 : weight_{weight}, dim_{0}
44 {
45 // fill with NaN for safety reasons
46 coords_.fill(std::numeric_limits<double>::quiet_NaN());
47 }
48
49 constexpr bool operator==(WeightedPoint const& other) const
50 {
51 if (weight_ != other.weight_)
52 {
53 return false;
54 }
55 if (dim_ != other.dim_)
56 {
57 return false;
58 }
59 for (std::size_t comp = 0; comp < dim_; ++comp)
60 {
61 if (coords_[comp] != other.coords_[comp])
62 {
63 return false;
64 }
65 }
66 return true;
67 }
68
69 constexpr bool operator!=(WeightedPoint const& other) const
70 {
71 return !(*this == other);
72 }
73
74 constexpr const double* data() const { return coords_.data(); }
75
76 constexpr double getWeight() const { return weight_; }
77
79 constexpr std::size_t getDimension() const { return dim_; }
80
82 constexpr double operator[](std::size_t coord_idx) const
83 {
84 return coords_[coord_idx];
85 }
86
87private:
88 double weight_;
89 std::array<double, 3> coords_;
90 std::size_t dim_;
91};
92
93std::ostream& operator<<(std::ostream& os, MathLib::WeightedPoint const& wp);
94
95} // 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:83