Shiokaze Framework
A research-oriented fluid solver for computer graphics
array_gaussianblur2.h
Go to the documentation of this file.
1 /*
2 ** array_gaussianblur2.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_GAUSSIANBLUR2_H
26 #define SHKZ_ARRAY_GAUSSIANBLUR2_H
27 //
28 #include "shared_array2.h"
29 #include <cmath>
30 #include <cstdlib>
31 //
33 //
35 namespace array_gaussianblur2 {
48  template<class T> void gaussian_blur( const array2<T> &array, array2<T> &result, double r, const bitarray2 *mask=nullptr ) {
49  int rs = std::ceil(r * 2.57);
50  std::vector<double> exp_w(2*rs+1);
51  for(int q=-rs; q<rs+1; q++) {
52  exp_w[q+rs] = std::exp(-(q*q)/(2.0*r*r))/std::sqrt(M_PI*2.0*r*r);
53  }
54  auto valid = [&]( int i, int j ) {
55  if( ! mask ) return true;
56  else {
57  return (*mask)(mask->shape().clamp(i,j)) != 0;
58  }
59  };
60  shared_array2<T> save (array);
61  //
62  for( int dim : DIMS2 ) {
63  result.parallel_all([&](int i, int j, auto &it) {
64  if( valid(i,j)) {
65  T val (0.0);
66  double wsum (0.0);
67  for(int q=-rs; q<rs+1; q++) {
68  int ni = i+(dim==0)*q;
69  int nj = j+(dim==1)*q;
70  const double &wght = exp_w[q+rs];
71  if( valid(ni,nj)) {
72  T value = save()(save->shape().clamp(ni,nj));
73  val += value * wght; wsum += wght;
74  }
75  }
76  if( wsum ) result.set(i,j, val / wsum );
77  else it.set(T(0.0));
78  }
79  });
80  save->copy(result);
81  }
82  }
83 };
84 //
86 //
87 #endif
88 //
array_gaussianblur2::gaussian_blur
void gaussian_blur(const array2< T > &array, array2< T > &result, double r, const bitarray2 *mask=nullptr)
Perform gaussian blur on grids.
Definition: array_gaussianblur2.h:48
shared_array2.h
array2::set
void set(int i, int j, const T &value)
Set value on grid.
Definition: array2.h:518
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
array2::parallel_all
void parallel_all(std::function< void(iterator &it)> func)
Loop over all the cells in parallel.
Definition: array2.h:1130
array_gaussianblur2
Namespace that performs gaussian blur.
Definition: array_gaussianblur2.h:37
shared_array2
Storage class that enables sharing pre-allocated arrays.
Definition: shared_array2.h:40
array2
Two dimensional array class designed to be defined as instance member in recursive_configurable class...
Definition: array2.h:42
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
bitarray2
Two dimensional bit grid class designed to be defined as instance member in recursive_configurable cl...
Definition: bitarray2.h:43