Shiokaze Framework
A research-oriented fluid solver for computer graphics
array_gaussianblur3.h
Go to the documentation of this file.
1 /*
2 ** array_gaussianblur3.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_GAUSSIANBLUR3_H
26 #define SHKZ_ARRAY_GAUSSIANBLUR3_H
27 //
28 #include "shared_array3.h"
29 #include <cmath>
30 #include <cstdlib>
31 //
33 //
35 namespace array_gaussianblur3 {
48  template<class T> void gaussian_blur( const array3<T> &array, array3<T> &result, double r, const bitarray3 *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, int k ) {
55  if( ! mask ) return true;
56  else {
57  return (*mask)(mask->shape().clamp(i,j,k)) != 0;
58  }
59  };
60  shared_array3<T> save (array);
61  //
62  for( int dim : DIMS3 ) {
63  result.parallel_all([&](int i, int j, int k, auto &it, int tn) {
64  if( valid(i,j,k)) {
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  int nk = k+(dim==2)*q;
71  const double &wght = exp_w[q+rs];
72  if(valid(ni,nj,nk)) {
73  T value = save()(save->shape().clamp(ni,nj,nk));
74  val += value * wght; wsum += wght;
75  }
76  }
77  if( wsum ) it.set(val/wsum);
78  else it.set(0.0);
79  }
80  });
81  //
82  save->copy(result);
83  }
84  }
85 };
86 //
88 //
89 #endif
90 //
array_gaussianblur3::gaussian_blur
void gaussian_blur(const array3< T > &array, array3< T > &result, double r, const bitarray3 *mask=nullptr)
Perform gaussian blur on grids.
Definition: array_gaussianblur3.h:48
shared_array3
Storage class that enables sharing pre-allocated arrays.
Definition: shared_array3.h:41
shared_array3.h
bitarray3
Three dimensional bit grid class designed to be defined as instance member in recursive_configurable ...
Definition: bitarray3.h:43
array_gaussianblur3
Namespace that performs gaussian blur.
Definition: array_gaussianblur3.h:37
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
array3::parallel_all
void parallel_all(std::function< void(iterator &it)> func)
Loop over all the cells in parallel.
Definition: array3.h:1150
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
array3
Three dimensional array class designed to be defined as instance member in recursive_configurable cla...
Definition: array3.h:42