-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp3a_grid3.hpp
166 lines (160 loc) · 4.47 KB
/
p3a_grid3.hpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
#include "p3a_vector3.hpp"
#include "p3a_box3.hpp"
namespace p3a {
class grid3 {
vector3<int> m_extents;
public:
P3A_ALWAYS_INLINE grid3() = default;
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
grid3(int a, int b, int c)
:m_extents(a, b, c)
{}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
grid3(vector3<int> const& extents_in)
:m_extents(extents_in)
{}
template <class T>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto index(vector3<T> const& point) const
{
// "layout left"
return (point.z() * m_extents.y() + point.y()) * m_extents.x() + point.x();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int> coordinate(int index_arg) const
{
auto const i = index_arg % m_extents.x();
index_arg /= m_extents.x();
auto const j = index_arg % m_extents.y();
index_arg /= m_extents.y();
auto const k = index_arg;
return vector3<int>(i, j, k);
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
int size() const
{
return m_extents.volume();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int> const& extents() const
{
return m_extents;
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int>& extents()
{
return m_extents;
}
template <class T>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto contains(vector3<T> const& p) const
{
return p.x() >= T(0) &&
p.y() >= T(0) &&
p.z() >= T(0) &&
p.x() < m_extents.x() &&
p.y() < m_extents.y() &&
p.z() < m_extents.z();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
bool operator==(grid3 const& other) const
{
return m_extents == other.m_extents;
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
bool operator!=(grid3 const& other) const
{
return !operator==(other);
}
};
class subgrid3 {
box3<int> m_box;
public:
P3A_ALWAYS_INLINE subgrid3() = default;
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr explicit subgrid3(
box3<int> const& box_in)
:m_box(box_in)
{}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr subgrid3(
vector3<int> const& lower_in,
vector3<int> const& upper_in)
:subgrid3(box3<int>(lower_in, upper_in))
{}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr subgrid3(
grid3 const& grid_in)
:subgrid3(vector3<int>::zero(), grid_in.extents())
{}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int> const& lower() const
{
return m_box.lower();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int> const& upper() const
{
return m_box.upper();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int>& lower()
{
return m_box.lower();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int>& upper()
{
return m_box.upper();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
bool operator==(subgrid3 const& other) const
{
return m_box == other.m_box;
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
bool operator!=(subgrid3 const& other) const
{
return !operator==(other);
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector3<int> extents() const
{
return m_box.upper() - m_box.lower();
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
int size() const
{
return extents().volume();
}
template <class T>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto index(vector3<T> const& p) const
{
return grid3(extents()).index(p - lower());
}
template <class T>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto contains(vector3<T> const& p) const
{
return p.x() >= T(lower().x()) &&
p.y() >= T(lower().y()) &&
p.z() >= T(lower().z()) &&
p.x() < T(upper().x()) &&
p.y() < T(upper().y()) &&
p.z() < T(upper().z());
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
box3<int> const& box() const { return m_box; }
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
subgrid3 relative_to(subgrid3 const& parent) const
{
return subgrid3(
box().lower() - parent.box().lower(),
box().upper() - parent.box().lower());
}
};
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
inline subgrid3 intersect(subgrid3 const& a, subgrid3 const& b)
{
return subgrid3(intersect(a.box(), b.box()));
}
}