Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CoordRange.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
20#include "intervals.hpp"
21#include "shambackends/math.hpp"
23#include "shambackends/vec.hpp"
24#include <limits>
25
26namespace shammath {
27
28 template<class T>
29 struct CoordRange {
30
32
33 T lower;
34 T upper;
35
36 inline CoordRange() = default;
37
38 inline CoordRange(T lower, T upper) : lower(lower), upper(upper) {};
39
40 inline CoordRange(std::tuple<T, T> range)
41 : lower(std::get<0>(range)), upper(std::get<1>(range)) {}
42
43 inline CoordRange(std::pair<T, T> range)
44 : lower(std::get<0>(range)), upper(std::get<1>(range)) {}
45
46 inline T delt() const { return upper - lower; }
47
48 inline CoordRange expand_all(typename T_prop::component_type value) {
49 return CoordRange{lower - value, upper + value};
50 }
51
52 inline void expand_center(T tol) {
53 T center = (lower + upper) / 2;
54 T cur_delt = upper - lower;
55 cur_delt /= 2;
56 cur_delt *= tol;
57 lower = center - cur_delt;
58 upper = center + cur_delt;
59 }
60
61 inline typename T_prop::component_type get_volume() {
62 return sham::product_accumulate(upper - lower);
63 }
64
65 static CoordRange max_range();
66
67 void check_throw_ranges(SourceLocation loc = SourceLocation{});
68
69 inline CoordRange get_intersect(CoordRange other) const {
70 return {sham::max(lower, other.lower), sham::min(upper, other.upper)};
71 }
72
73 inline CoordRange get_union(CoordRange other) const {
74 return {sham::min(lower, other.lower), sham::max(upper, other.upper)};
75 }
76
77 inline bool contain_pos(T pos) { return is_in_half_open(pos, lower, upper); }
78
79 inline bool is_not_empty() { return sham::vec_compare_geq(upper, lower); }
80
81 inline CoordRange add_offset(T off) { return CoordRange{lower + off, upper + off}; }
82
83 inline bool is_err_mode() {
84
85 auto tmp = max_range();
86
87 return sham::equals(tmp.lower, lower) && sham::equals(tmp.upper, upper);
88 }
89 };
90
91 template<>
93
95
97
99
100 return ret;
101 }
102
103 template<>
104 inline CoordRange<f64_3> CoordRange<f64_3>::max_range() {
105
106 CoordRange<f64_3> ret;
107
109
111
112 return ret;
113 }
114
115 template<>
116 inline CoordRange<u32_3> CoordRange<u32_3>::max_range() {
117
118 CoordRange<u32_3> ret;
119
121
123
124 return ret;
125 }
126
127 template<>
128 inline CoordRange<u64_3> CoordRange<u64_3>::max_range() {
129
130 CoordRange<u64_3> ret;
131
133
135
136 return ret;
137 }
138
139 template<>
140 inline CoordRange<i64_3> CoordRange<i64_3>::max_range() {
141
142 CoordRange<i64_3> ret;
143
145
147
148 return ret;
149 }
150
151} // namespace shammath
Source location utility.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for math utility
Definition AABB.hpp:26
bool is_in_half_open(T val, T min, T max)
return true if val is in [min,max[
Definition intervals.hpp:36
provide information about the source location