Wayverb
indexing.h
1 #pragma once
2 
3 #include "glm/glm.hpp"
4 
5 namespace wayverb {
6 namespace core {
7 namespace indexing {
8 
10 
11 template <size_t dimensions>
12 struct index;
13 
14 template <>
15 struct index<1> {
16  using type = unsigned;
17  static constexpr auto numbered_dimensions = type{0};
18 };
19 template <>
20 struct index<2> {
21  using type = glm::uvec2;
22  static constexpr auto numbered_dimensions = type{0, 1};
23 };
24 template <>
25 struct index<3> {
26  using type = glm::uvec3;
27  static constexpr auto numbered_dimensions = type{0, 1, 2};
28 };
29 
30 template <size_t n>
31 using index_t = typename index<n>::type;
32 
33 template <size_t n>
34 constexpr auto numbered_dimensions_t = index<n>::numbered_dimensions;
35 
37 
38 template <size_t n>
39 inline index_t<n> relative_position(size_t i) {
40  const auto mask = index_t<n>(1) << numbered_dimensions_t<n>;
41  return (mask & static_cast<unsigned>(i)) / mask;
42 }
43 
45 
46 template <size_t n>
47 inline auto front(index_t<n> i) {
48  return i[0];
49 }
50 
51 template <size_t n>
52 inline auto back(index_t<n> i) {
53  return i[n - 1];
54 }
55 
57 
58 template <size_t n>
59 index_t<n - 1> reduce(index_t<n> i);
60 
61 template <>
62 inline index_t<1> reduce<2>(index_t<2> i) {
63  return front<2>(i);
64 }
65 template <>
66 inline index_t<2> reduce<3>(index_t<3> i) {
67  return i;
68 }
69 
71 
72 template <size_t n>
73 index_t<n - 1> tail(index_t<n> i);
74 
75 template<>
76 inline index_t<1> tail<2>(index_t<2> i) {
77  return back<2>(i);
78 }
79 
80 template<>
81 inline index_t<2> tail<3>(index_t<3> i) {
82  return index_t<2>{i[1], i[2]};
83 }
84 
86 
87 template <size_t n>
88 inline unsigned product(index_t<n> i) {
89  return front<n>(i) * product<n - 1>(tail<n>(i));
90 }
91 
92 template<>
93 inline unsigned product<1>(index_t<1> i) {
94  return i;
95 }
96 
98 
100 template <size_t n>
101 inline unsigned flatten(index_t<n> i, index_t<n> size) {
102  auto to_find_prod = size;
103  to_find_prod[0] = i[0];
104  return product<n>(to_find_prod) + flatten<n - 1>(tail<n>(i), tail<n>(size));
105 }
106 
107 template <>
108 inline unsigned flatten<1>(index_t<1> i, index_t<1>) {
109  return i;
110 }
111 
112 } // namespace indexing
113 } // namespace core
114 } // namespace wayverb
Definition: traits.cpp:2
types for indexing into n-dimensional data
Definition: indexing.h:12
Definition: capsule_base.h:9