AutoPas  3.0.0
Loading...
Searching...
No Matches
ThreeDimensionalMapping.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <array>
11#include <type_traits>
12
18
28template <typename T>
29constexpr T threeToOneD(T x, T y, T z, const std::array<T, 3> &dims) {
30 static_assert(std::is_integral_v<T>, "threeToOneD requires integral types");
31 return (z * dims[1] + y) * dims[0] + x;
32}
33
41template <typename T>
42constexpr T threeToOneD(const std::array<T, 3> &index3d, const std::array<T, 3> &dims) {
43 static_assert(std::is_integral_v<T>, "threeToOneD requires integral types");
44 return (index3d[2] * dims[1] + index3d[1]) * dims[0] + index3d[0];
45}
46
54template <typename T>
55constexpr std::array<T, 3> oneToThreeD(T ind, const std::array<T, 3> &dims) {
56 static_assert(std::is_integral_v<T>, "oneToThreeD requires integral types");
57 std::array<T, 3> pos{};
58 pos[2] = ind / (dims[0] * dims[1]);
59 pos[1] = (ind - pos[2] * dims[0] * dims[1]) / dims[0];
60 pos[0] = ind - dims[0] * (pos[1] + dims[1] * pos[2]);
61 return pos;
62}
63
64} // namespace autopas::utils::ThreeDimensionalMapping
Namespace to handle the conversion between one dimensional and three dimensional indices.
Definition: namespaces.h:70
constexpr std::array< T, 3 > oneToThreeD(T ind, const std::array< T, 3 > &dims)
Convert a 1d index to a 3d index.
Definition: ThreeDimensionalMapping.h:55
constexpr T threeToOneD(T x, T y, T z, const std::array< T, 3 > &dims)
Convert a 3d index to a 1d index.
Definition: ThreeDimensionalMapping.h:29