Shiokaze Framework
A research-oriented fluid solver for computer graphics
array_extrapolator2.h
Go to the documentation of this file.
1 /*
2 ** array_extrapolator2.h
3 **
4 ** This is part of Shiokaze, a research-oriented fluid solver for computer graphics.
5 ** Created by Ryoichi Ando <rand@nii.ac.jp> on Feb 14, 2018.
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining a copy of
8 ** this software and associated documentation files (the "Software"), to deal in
9 ** the Software without restriction, including without limitation the rights to use,
10 ** copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
11 ** Software, and to permit persons to whom the Software is furnished to do so,
12 ** subject to the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included in all copies
15 ** or substantial portions of the Software.
16 **
17 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18 ** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19 ** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 ** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 ** CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 ** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 //
25 #ifndef SHKZ_ARRAY_EXTRAPOLATOR2_H
26 #define SHKZ_ARRAY_EXTRAPOLATOR2_H
27 //
28 #include "array2.h"
29 #include "shared_array2.h"
30 //
32 //
34 namespace array_extrapolator2 {
47  template<class T> unsigned extrapolate_if ( array2<T> &array, std::function<bool(int i, int j, int thread_index )> func, int count=1 ) {
48  //
49  std::vector<unsigned> counters(array.get_thread_num());
50  const shape2 shape(array.shape());
51  //
52  array.dilate([&](int i, int j, auto &it, int tn ) {
53  if( func(i,j,tn)) {
54  T sum (0.0);
55  int weight = 0;
56  vec2i query[] = {vec2i(i+1,j),vec2i(i-1,j),vec2i(i,j+1),vec2i(i,j-1)};
57  for( int nq=0; nq<4; nq++ ) {
58  vec2i qi (query[nq]);
59  if( ! shape.out_of_bounds(qi) ) {
60  if( array.active(qi)) {
61  sum += array(qi);
62  weight ++;
63  }
64  }
65  }
66  if( weight ) {
67  counters[tn] ++;
68  it.set( sum / weight );
69  }
70  }
71  },count);
72  //
73  unsigned sum (0);
74  for( const auto &e : counters ) sum += e;
75  return sum;
76  }
85  template<class T> unsigned extrapolate ( array2<T> &array, int count=1 ) {
86  return extrapolate_if(array,[&](int i, int j, int tn){ return true; },count);
87  }
88 };
89 //
91 //
92 #endif
93 //
shared_array2.h
array2.h
shape2::out_of_bounds
bool out_of_bounds(int i, int j) const
Get if the position is outside of the index space of this shape.
Definition: shape.h:341
array2::shape
shape2 shape() const
Get the shape of the array.
Definition: array2.h:218
array2::active
bool active(int i, int j) const
Get if a position on grid is active.
Definition: array2.h:546
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
array_extrapolator2
Namespace that implements array extrapolation.
Definition: array_extrapolator2.h:36
array2::dilate
void dilate(std::function< void(int i, int j, iterator &it, int thread_index)> func, int count=1)
Dilate cells.
Definition: array2.h:1639
array_extrapolator2::extrapolate_if
unsigned extrapolate_if(array2< T > &array, std::function< bool(int i, int j, int thread_index)> func, int count=1)
Extrapolate array where it passes the test function.
Definition: array_extrapolator2.h:47
vec
Fixed sized vector structure.
Definition: vec.h:38
array2
Two dimensional array class designed to be defined as instance member in recursive_configurable class...
Definition: array2.h:42
shape2
Structure that defines shape such as width, height.
Definition: shape.h:42
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
array2::get_thread_num
int get_thread_num() const
Get the current number of threads for parallel processing on this grid.
Definition: array2.h:932
array_extrapolator2::extrapolate
unsigned extrapolate(array2< T > &array, int count=1)
Extrapolate array where it passes the test function.
Definition: array_extrapolator2.h:85