ArrayUtils-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: ArrayUtils-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: N-D array util functions.
6 > Created Time: 2017/04/30
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_ARRAY_UTILS_IMPL_H
10 #define CUBBYFLOW_ARRAY_UTILS_IMPL_H
11 
12 #include <Core/Array/Array2.h>
13 #include <Core/Array/Array3.h>
14 #include <Core/Utils/Parallel.h>
15 #include <Core/Utils/TypeHelpers.h>
16 
17 #include <iostream>
18 
19 namespace CubbyFlow
20 {
21  template <typename ArrayType, typename T>
22  void SetRange1(size_t size, const T& value, ArrayType* output)
23  {
24  SetRange1(ZERO_SIZE, size, value, output);
25  }
26 
27  template <typename ArrayType, typename T>
28  void SetRange1(size_t begin, size_t end, const T& value, ArrayType* output)
29  {
30  ParallelFor(begin, end, [&](size_t i)
31  {
32  (*output)[i] = value;
33  });
34  }
35 
36  template <typename ArrayType1, typename ArrayType2>
37  void CopyRange1(const ArrayType1& input, size_t size, ArrayType2* output)
38  {
39  CopyRange1(input, 0, size, output);
40  }
41 
42  template <typename ArrayType1, typename ArrayType2>
43  void CopyRange1(const ArrayType1& input, size_t begin, size_t end, ArrayType2* output)
44  {
45  ParallelFor(begin, end, [&input, &output](size_t i)
46  {
47  (*output)[i] = input[i];
48  });
49  }
50 
51  template <typename ArrayType1, typename ArrayType2>
52  void CopyRange2(const ArrayType1& input, size_t sizeX, size_t sizeY, ArrayType2* output)
53  {
54  CopyRange2(input, ZERO_SIZE, sizeX, ZERO_SIZE, sizeY, output);
55  }
56 
57  template <typename ArrayType1, typename ArrayType2>
58  void CopyRange2(const ArrayType1& input, size_t beginX, size_t endX, size_t beginY, size_t endY, ArrayType2* output)
59  {
60  ParallelFor(beginX, endX, beginY, endY, [&input, &output](size_t i, size_t j)
61  {
62  (*output)(i, j) = input(i, j);
63  });
64  }
65 
66  template <typename ArrayType1, typename ArrayType2>
67  void CopyRange3(const ArrayType1& input, size_t sizeX, size_t sizeY, size_t sizeZ, ArrayType2* output)
68  {
69  CopyRange3(input, ZERO_SIZE, sizeX, ZERO_SIZE, sizeY, ZERO_SIZE, sizeZ, output);
70  }
71 
72  template <typename ArrayType1, typename ArrayType2>
73  void CopyRange3(const ArrayType1& input, size_t beginX, size_t endX, size_t beginY, size_t endY, size_t beginZ, size_t endZ, ArrayType2* output)
74  {
75  ParallelFor(beginX, endX, beginY, endY, beginZ, endZ, [&input, &output](size_t i, size_t j, size_t k)
76  {
77  (*output)(i, j, k) = input(i, j, k);
78  });
79  }
80 
81  template <typename T>
82  void ExtrapolateToRegion(const ConstArrayAccessor2<T>& input, const ConstArrayAccessor2<char>& valid, unsigned int numberOfIterations, ArrayAccessor2<T> output)
83  {
84  const Size2 size = input.size();
85 
86  assert(size == valid.size());
87  assert(size == output.size());
88 
89  Array2<char> valid0(size);
90  Array2<char> valid1(size);
91 
92  valid0.ParallelForEachIndex([&](size_t i, size_t j)
93  {
94  valid0(i, j) = valid(i, j);
95  output(i, j) = input(i, j);
96  });
97 
98  for (unsigned int iter = 0; iter < numberOfIterations; ++iter)
99  {
100  valid0.ForEachIndex([&](size_t i, size_t j)
101  {
102  T sum = Zero<T>();
103  unsigned int count = 0;
104 
105  if (!valid0(i, j))
106  {
107  if (i + 1 < size.x && valid0(i + 1, j))
108  {
109  sum += output(i + 1, j);
110  ++count;
111  }
112 
113  if (i > 0 && valid0(i - 1, j))
114  {
115  sum += output(i - 1, j);
116  ++count;
117  }
118 
119  if (j + 1 < size.y && valid0(i, j + 1))
120  {
121  sum += output(i, j + 1);
122  ++count;
123  }
124 
125  if (j > 0 && valid0(i, j - 1))
126  {
127  sum += output(i, j - 1);
128  ++count;
129  }
130 
131  if (count > 0)
132  {
133  output(i, j) = sum / static_cast<typename ScalarType<T>::value>(count);
134  valid1(i, j) = 1;
135  }
136  }
137  else
138  {
139  valid1(i, j) = 1;
140  }
141  });
142 
143  valid0.Swap(valid1);
144  }
145  }
146 
147  template <typename T>
148  void ExtrapolateToRegion(const ConstArrayAccessor3<T>& input, const ConstArrayAccessor3<char>& valid, unsigned int numberOfIterations, ArrayAccessor3<T> output)
149  {
150  const Size3 size = input.size();
151 
152  assert(size == valid.size());
153  assert(size == output.size());
154 
155  Array3<char> valid0(size);
156  Array3<char> valid1(size);
157 
158  valid0.ParallelForEachIndex([&](size_t i, size_t j, size_t k)
159  {
160  valid0(i, j, k) = valid(i, j, k);
161  output(i, j, k) = input(i, j, k);
162  });
163 
164  for (unsigned int iter = 0; iter < numberOfIterations; ++iter)
165  {
166  valid0.ForEachIndex([&](size_t i, size_t j, size_t k)
167  {
168  T sum = Zero<T>();
169  unsigned int count = 0;
170 
171  if (!valid0(i, j, k))
172  {
173  if (i + 1 < size.x && valid0(i + 1, j, k))
174  {
175  sum += output(i + 1, j, k);
176  ++count;
177  }
178 
179  if (i > 0 && valid0(i - 1, j, k))
180  {
181  sum += output(i - 1, j, k);
182  ++count;
183  }
184 
185  if (j + 1 < size.y && valid0(i, j + 1, k))
186  {
187  sum += output(i, j + 1, k);
188  ++count;
189  }
190 
191  if (j > 0 && valid0(i, j - 1, k))
192  {
193  sum += output(i, j - 1, k);
194  ++count;
195  }
196 
197  if (k + 1 < size.z && valid0(i, j, k + 1))
198  {
199  sum += output(i, j, k + 1);
200  ++count;
201  }
202 
203  if (k > 0 && valid0(i, j, k - 1))
204  {
205  sum += output(i, j, k - 1);
206  ++count;
207  }
208 
209  if (count > 0)
210  {
211  output(i, j, k) = sum / static_cast<typename ScalarType<T>::value>(count);
212  valid1(i, j, k) = 1;
213  }
214  }
215  else {
216  valid1(i, j, k) = 1;
217  }
218  });
219 
220  valid0.Swap(valid1);
221  }
222  }
223 
224  template <typename ArrayType>
225  void ConvertToCSV(const ArrayType& data, std::ostream* stream)
226  {
227  Size2 size = data.size();
228 
229  for (size_t j = 0; j < size.y; ++j)
230  {
231  for (size_t i = 0; i < size.x; ++i)
232  {
233  auto val = data(i, j);
234 
235  // TODO: Hack to handle char and unsigned char
236  if constexpr (sizeof(decltype(val)) == 1)
237  {
238  *stream << static_cast<int>(val);
239  }
240  else
241  {
242  *stream << val;
243  }
244 
245  if (i + 1 < size.x)
246  {
247  *stream << ", ";
248  }
249  }
250 
251  *stream << std::endl;
252  }
253  }
254 }
255 
256 #endif
2-D read-only array accessor class.
Definition: ArrayAccessor2.h:261
3-D array accessor class.
Definition: ArrayAccessor3.h:31
void CopyRange3(const ArrayType1 &input, size_t sizeX, size_t sizeY, size_t sizeZ, ArrayType2 *output)
Copies 3-D input array to output array with sizeX and sizeY.
Definition: ArrayUtils-Impl.h:67
3-D read-only array accessor class.
Definition: ArrayAccessor3.h:269
T x
X (or the first) component of the point.
Definition: Point2.h:28
void CopyRange2(const ArrayType1 &input, size_t sizeX, size_t sizeY, ArrayType2 *output)
Copies 2-D input array to output array with sizeX and sizeY.
Definition: ArrayUtils-Impl.h:52
T value
Definition: TypeHelpers.h:18
void ExtrapolateToRegion(const ConstArrayAccessor2< T > &input, const ConstArrayAccessor2< char > &valid, unsigned int numberOfIterations, ArrayAccessor2< T > output)
Extrapolates 2-D input data from &#39;valid&#39; (1) to &#39;invalid&#39; (0) region.
Definition: ArrayUtils-Impl.h:82
void ConvertToCSV(const ArrayType &data, std::ostream *stream)
Converts 2-D array to Comma Separated Value (CSV) stream.
Definition: ArrayUtils-Impl.h:225
2-D point class.
Definition: Point2.h:25
3-D point class.
Definition: Point3.h:26
2-D array accessor class.
Definition: ArrayAccessor2.h:31
T y
Y (or the second) component of the point.
Definition: Point2.h:34
Definition: pybind11Utils.h:24
void ParallelFor(IndexType beginIndex, IndexType endIndex, const Function &function, ExecutionPolicy policy)
Makes a for-loop from beginIndex to endIndex in parallel.
Definition: Parallel-Impl.h:201
T z
Z (or the third) component of the point.
Definition: Point3.h:38
Size2 size() const
Returns the size of the array.
Definition: ArrayAccessor2-Impl.h:129
T y
Y (or the second) component of the point.
Definition: Point3.h:35
constexpr size_t ZERO_SIZE
Zero size_t.
Definition: Constants.h:18
T x
X (or the first) component of the point.
Definition: Point3.h:29
Size3 size() const
Returns the size of the array.
Definition: ArrayAccessor3-Impl.h:129
Size3 size() const
Returns the size of the array.
Definition: ArrayAccessor3-Impl.h:347
3-D array class.
Definition: Array3.h:45
2-D array class.
Definition: Array2.h:42
void SetRange1(size_t size, const T &value, ArrayType *output)
Assigns value to 1-D array output with size.
Definition: ArrayUtils-Impl.h:22
Size2 size() const
Returns the size of the array.
Definition: ArrayAccessor2-Impl.h:333
void CopyRange1(const ArrayType1 &input, size_t size, ArrayType2 *output)
Copies input array to output array with size.
Definition: ArrayUtils-Impl.h:37