Shiokaze Framework
A research-oriented fluid solver for computer graphics
array_interpolator2.h
Go to the documentation of this file.
1 /*
2 ** array_interpolator2.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_INTERPOLATOR2_H
26 #define SHKZ_ARRAY_INTERPOLATOR2_H
27 //
28 #include "array2.h"
29 #include <cmath>
30 #include <cstdlib>
31 //
33 //
35 namespace array_interpolator2 {
50  static void interpolate_coef( const shape2 &shape, const vec2d &p, vec2i indices[4], double coef[4] ) {
51  double x = std::max(0.0,std::min(shape.w-1.,p[0]));
52  double y = std::max(0.0,std::min(shape.h-1.,p[1]));
53  int i = std::min(x,shape.w-2.);
54  int j = std::min(y,shape.h-2.);
55  indices[0] = vec2i(i,j);
56  indices[1] = vec2i(i+1,j);
57  indices[2] = vec2i(i,j+1);
58  indices[3] = vec2i(i+1,j+1);
59  coef[0] = (i+1-x)*(j+1-y);
60  coef[1] = (x-i)*(j+1-y);
61  coef[2] = (i+1-x)*(y-j);
62  coef[3] = (x-i)*(y-j);
63  }
76  template<class T> T static interpolate( const array2<T> &array, const vec2d &p, bool only_actives=false ) {
77  T values[4]; vec2i indices[4]; double coef[4];
78  interpolate_coef(array.shape(),p,indices,coef);
79  T value = T();
80  if( only_actives ) {
81  double w[4];
82  double sum (0.0);
83  for( int n=0; n<4; ++n ) w[n] = array.active(indices[n]) ? coef[n] : 0.0;
84  for( int n=0; n<4; ++n ) sum += w[n];
85  if( sum ) {
86  for( int n=0; n<4; ++n ) w[n] /= sum;
87  for( unsigned n=0; n<4; ++n ) if( w[n] ) value += array(indices[n]) * w[n];
88  }
89  } else {
90  for( unsigned n=0; n<4; ++n ) if( coef[n] ) value += array(indices[n]) * coef[n];
91  }
92  return value;
93  }
110  template<class T> T static interpolate( const array2<T> &array, const vec2d &origin, double dx, const vec2d &p, bool only_actives=false ) {
111  return interpolate<T>(array,(p-origin)/dx,only_actives);
112  }
113 };
114 //
116 //
117 #endif
118 //
array2.h
array_interpolator2
Namespace that implements array interpolation.
Definition: array_interpolator2.h:37
array2::shape
shape2 shape() const
Get the shape of the array.
Definition: array2.h:218
shape2::h
unsigned h
Height of the shape.
Definition: shape.h:466
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
shape2::w
unsigned w
Width of the shape.
Definition: shape.h:461
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