forked from ufz/ogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeightedPoint.h
99 lines (86 loc) · 2.45 KB
/
WeightedPoint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* \file
* \copyright
* Copyright (c) 2012-2023, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*/
#pragma once
#include <array>
#include <iosfwd>
#include <limits>
namespace MathLib
{
//! Represents a point of a certain dimension that has a weight attached.
//!
//! Used, e.g., in numerical quadrature.
class WeightedPoint
{
public:
//! Constructs a 0...3D weighted point depending on the passed coordinates
//! array.
template <std::size_t dim>
WeightedPoint(std::array<double, dim> const& coords, double const weight)
: weight_{weight}, dim_{dim}
{
static_assert(dim <= 3);
std::size_t i = 0;
if constexpr (dim > 0) // avoids compiler warning
{
for (; i < dim; ++i)
{
coords_[i] = coords[i];
}
}
for (; i < 3; ++i)
{
// fill the rest with NaN for safety reasons
coords_[i] = std::numeric_limits<double>::quiet_NaN();
}
}
//! Constructs a 0D weighted point.
explicit WeightedPoint(double const weight) : weight_{weight}, dim_{0}
{
// fill with NaN for safety reasons
coords_.fill(std::numeric_limits<double>::quiet_NaN());
}
bool operator==(WeightedPoint const& other) const
{
if (weight_ != other.weight_)
{
return false;
}
if (dim_ != other.dim_)
{
return false;
}
for (std::size_t comp = 0; comp < dim_; ++comp)
{
if (coords_[comp] != other.coords_[comp])
{
return false;
}
}
return true;
}
bool operator!=(WeightedPoint const& other) const
{
return !(*this == other);
}
const double* data() const { return coords_.data(); }
double getWeight() const { return weight_; }
//! The point dimension, i.e., the number of its coordinates.
std::size_t getDimension() const { return dim_; }
//! Access a specific coordinate.
double operator[](std::size_t coord_idx) const
{
return coords_[coord_idx];
}
private:
double weight_;
std::array<double, 3> coords_;
std::size_t dim_;
};
std::ostream& operator<<(std::ostream& os, MathLib::WeightedPoint const& wp);
} // namespace MathLib