Loading...
Searching...
No Matches
ArrayUtils-Impl.hpp
Go to the documentation of this file.
1// This code is based on Jet framework.
2// Copyright (c) 2018 Doyub Kim
3// CubbyFlow is voxel-based fluid simulation engine for computer games.
4// Copyright (c) 2020 CubbyFlow Team
5// Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6// AI Part: Dongheon Cho, Minseo Kim
7// We are making my contributions/submissions to this project solely in our
8// personal capacity and are not conveying any rights to any intellectual
9// property of any third parties.
10
11#ifndef CUBBYFLOW_ARRAY_UTILS_IMPL_HPP
12#define CUBBYFLOW_ARRAY_UTILS_IMPL_HPP
13
14#include <Core/Array/Array.hpp>
15
16namespace CubbyFlow
17{
18namespace Internal
19{
20template <typename T, typename U>
23 size_t j)
24{
25 if (valid(i, j))
26 {
27 (*next)(i, j) = 1;
28 return;
29 }
30
31 T sum{};
32 unsigned int count = 0;
33
34 const auto add = [&](size_t x, size_t y) {
35 if (valid(x, y))
36 {
37 sum += output(x, y);
38 ++count;
39 }
40 };
41
42 if (i + 1 < size.x)
43 {
44 add(i + 1, j);
45 }
46
47 if (i > 0)
48 {
49 add(i - 1, j);
50 }
51
52 if (j + 1 < size.y)
53 {
54 add(i, j + 1);
55 }
56
57 if (j > 0)
58 {
59 add(i, j - 1);
60 }
61
62 if (count > 0)
63 {
64 output(i, j) =
66 (*next)(i, j) = 1;
67 }
68}
69
70template <typename T, typename U>
73 size_t j, size_t k)
74{
75 if (valid(i, j, k))
76 {
77 (*next)(i, j, k) = 1;
78 return;
79 }
80
81 T sum{};
82 unsigned int count = 0;
83
84 const auto add = [&](size_t x, size_t y, size_t z) {
85 if (valid(x, y, z))
86 {
87 sum += output(x, y, z);
88 ++count;
89 }
90 };
91
92 if (i + 1 < size.x)
93 {
94 add(i + 1, j, k);
95 }
96
97 if (i > 0)
98 {
99 add(i - 1, j, k);
100 }
101
102 if (j + 1 < size.y)
103 {
104 add(i, j + 1, k);
105 }
106
107 if (j > 0)
108 {
109 add(i, j - 1, k);
110 }
111
112 if (k + 1 < size.z)
113 {
114 add(i, j, k + 1);
115 }
116
117 if (k > 0)
118 {
119 add(i, j, k - 1);
120 }
121
122 if (count > 0)
123 {
124 output(i, j, k) =
126 (*next)(i, j, k) = 1;
127 }
128}
129} // namespace Internal
130
131template <typename T, size_t N>
133 const Vector<size_t, N>& end, const T& val)
134{
135 ForEachIndex(begin, end, [&a, &val](auto... idx) { a(idx...) = val; });
136}
137
138template <typename T, size_t N>
140{
142}
143
144template <typename T>
145void Fill(ArrayView<T, 1> a, size_t begin, size_t end, const T& val)
146{
147 Fill(a, Vector1UZ{ begin }, Vector1UZ{ end }, val);
148}
149
150template <typename T, typename U, size_t N>
153{
154 ForEachIndex(begin, end,
155 [&dst, &src](auto... idx) { dst(idx...) = src(idx...); });
156}
157
158template <typename T, typename U, size_t N>
163
164template <typename T, typename U>
165void Copy(ArrayView<T, 1> src, size_t begin, size_t end, ArrayView<U, 1> dst)
166{
167 Copy(src, Vector1UZ{ begin }, Vector1UZ{ end }, dst);
168}
169
170template <typename T, typename U>
173{
174 const Vector2UZ size = input.Size();
175
176 assert(size == valid.Size());
177 assert(size == output.Size());
178
181
183 valid0.Size(), [&valid0, &valid, &output, &input](size_t i, size_t j) {
184 valid0(i, j) = valid(i, j);
185 output(i, j) = input(i, j);
186 });
187
188 for (unsigned int iter = 0; iter < numberOfIterations; ++iter)
189 {
190 ForEachIndex(valid0.Size(), [&valid0, &size, &output, &valid1](
191 size_t i, size_t j) {
192 Internal::ExtrapolatePoint<T>(valid0, size, output, &valid1, i, j);
193 });
194
196 }
197}
198
199template <typename T, typename U>
202{
203 const Vector3UZ size = input.Size();
204
205 assert(size == valid.Size());
206 assert(size == output.Size());
207
210
212 size_t i, size_t j, size_t k) {
213 valid0(i, j, k) = valid(i, j, k);
214 output(i, j, k) = input(i, j, k);
215 });
216
217 for (unsigned int iter = 0; iter < numberOfIterations; ++iter)
218 {
219 ForEachIndex(valid0.Size(), [&valid0, &size, &output, &valid1](
220 size_t i, size_t j, size_t k) {
221 Internal::ExtrapolatePoint<T>(valid0, size, output, &valid1, i, j,
222 k);
223 });
224
226 }
227}
228} // namespace CubbyFlow
229
230#endif
Definition Matrix.hpp:30
void Swap(Matrix &other)
Definition Matrix-Impl.hpp:254
void ExtrapolatePoint(const Array2< char > &valid, const Vector2UZ &size, ArrayView2< U > output, Array2< char > *next, size_t i, size_t j)
Definition ArrayUtils-Impl.hpp:21
Definition pybind11Utils.hpp:22
void ForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func)
Definition IterationUtils-Impl.hpp:51
void Fill(ArrayView< T, N > a, const Vector< size_t, N > &begin, const Vector< size_t, N > &end, const T &val)
Definition ArrayUtils-Impl.hpp:132
void Copy(ArrayView< T, N > src, const Vector< size_t, N > &begin, const Vector< size_t, N > &end, ArrayView< U, N > dst)
Definition ArrayUtils-Impl.hpp:151
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:719
void ParallelForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func, ExecutionPolicy policy)
Definition IterationUtils-Impl.hpp:98
void ExtrapolateToRegion(ArrayView2< T > input, ArrayView2< char > valid, unsigned int numberOfIterations, ArrayView2< U > output)
Extrapolates 2-D input data from 'valid' (1) to 'invalid' (0) region.
Definition ArrayUtils-Impl.hpp:171