Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CoordRangeTransform.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
19#include "CoordRange.hpp"
20#include "shambackends/math.hpp"
21#include "shambackends/vec.hpp"
22
23namespace shammath {
24
25 template<class Tsource, class Tdest>
27
30
31 using component_source_t = typename SourceProp::component_type;
32 using component_dest_t = typename DestProp::component_type;
33
34 static constexpr bool source_is_int = SourceProp::is_uint_based;
35
36 static constexpr bool dest_is_int = SourceProp::is_uint_based;
37
38 enum TransformFactMode { multiply, divide };
39
40 // written as Patch->Coord transform
41 Tdest fact;
42
43 Tdest dest_coord_min;
44 Tsource source_coord_min;
45
46 TransformFactMode mode;
47
48 public:
49 static_assert(
50 SourceProp::dimension == DestProp::dimension,
51 "input and output dimensions should be the same");
52
54
55 CoordRange<Tdest> transform(CoordRange<Tsource> rnge) const;
56 CoordRange<Tsource> reverse_transform(CoordRange<Tdest> rnge) const;
57
58 Tdest transform(Tsource coord) const;
59 Tsource reverse_transform(Tdest rnge) const;
60
61 void print_transform() const;
62 };
63
65 // out of line impl
67
68 template<class Tsource, class Tdest>
70 CoordRange<Tsource> rnge) const {
71
72 Tsource pmin = rnge.lower;
73 Tsource pmax = rnge.upper;
74
75 if (mode == multiply) {
76 return {
77 sham::convert<Tdest>(pmin - source_coord_min) * fact + dest_coord_min,
78 sham::convert<Tdest>(pmax - source_coord_min) * fact + dest_coord_min};
79 } else {
80 return {
81 sham::convert<Tdest>(pmin - source_coord_min) / fact + dest_coord_min,
82 sham::convert<Tdest>(pmax - source_coord_min) / fact + dest_coord_min};
83 }
84 }
85
86 template<class Tsource, class Tdest>
87 inline CoordRange<Tsource> CoordRangeTransform<Tsource, Tdest>::reverse_transform(
88 CoordRange<Tdest> rnge) const {
89
90 Tsource pmin;
91 Tsource pmax;
92
93 if (mode == multiply) {
94 return {
95 sham::convert<Tsource>((rnge.lower - dest_coord_min) / fact) + source_coord_min,
96 sham::convert<Tsource>((rnge.upper - dest_coord_min) / fact) + source_coord_min};
97 } else {
98 return {
99 sham::convert<Tsource>((rnge.lower - dest_coord_min) * fact) + source_coord_min,
100 sham::convert<Tsource>((rnge.upper - dest_coord_min) * fact) + source_coord_min};
101 }
102 }
103
104 template<class Tsource, class Tdest>
105 inline Tdest CoordRangeTransform<Tsource, Tdest>::transform(Tsource coord) const {
106
107 if (mode == multiply) {
108 return sham::convert<Tdest>(coord - source_coord_min) * fact + dest_coord_min;
109 } else {
110 return sham::convert<Tdest>(coord - source_coord_min) / fact + dest_coord_min;
111 }
112 }
113
114 template<class Tsource, class Tdest>
115 inline Tsource CoordRangeTransform<Tsource, Tdest>::reverse_transform(Tdest coord) const {
116
117 if (mode == multiply) {
118 return sham::convert<Tsource>((coord - dest_coord_min) / fact) + source_coord_min;
119 } else {
120 return sham::convert<Tsource>((coord - dest_coord_min) * fact) + source_coord_min;
121 }
122 }
123
124} // namespace shammath
namespace for math utility
Definition AABB.hpp:26