Shiokaze Framework
A research-oriented fluid solver for computer graphics
array_interpolator3.h
Go to the documentation of this file.
1 /*
2 ** array_interpolator3.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_INTERPOLATOR3_H
26 #define SHKZ_ARRAY_INTERPOLATOR3_H
27 //
28 #include "array3.h"
29 #include <cmath>
30 #include <cstdlib>
31 //
33 //
35 namespace array_interpolator3 {
50  static void interpolate_coef( const shape3 &shape, const vec3d &p, vec3i indices[8], double coef[8] ) {
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  double z = std::max(0.0,std::min(shape.d-1.,p[2]));
54  int i = std::min(x,shape.w-2.);
55  int j = std::min(y,shape.h-2.);
56  int k = std::min(z,shape.d-2.);
57  indices[0] = vec3i(i,j,k);
58  indices[1] = vec3i(i+1,j,k);
59  indices[2] = vec3i(i,j+1,k);
60  indices[3] = vec3i(i+1,j+1,k);
61  indices[4] = vec3i(i,j,k+1);
62  indices[5] = vec3i(i+1,j,k+1);
63  indices[6] = vec3i(i,j+1,k+1);
64  indices[7] = vec3i(i+1,j+1,k+1);
65  coef[0] = (k+1-z)*(i+1-x)*(j+1-y);
66  coef[1] = (k+1-z)*(x-i)*(j+1-y);
67  coef[2] = (k+1-z)*(i+1-x)*(y-j);
68  coef[3] = (k+1-z)*(x-i)*(y-j);
69  coef[4] = (z-k)*(i+1-x)*(j+1-y);
70  coef[5] = (z-k)*(x-i)*(j+1-y);
71  coef[6] = (z-k)*(i+1-x)*(y-j);
72  coef[7] = (z-k)*(x-i)*(y-j);
73  }
86  template<class T> static T interpolate( const array3<T> &array, const vec3d &p, bool only_actives=false ) {
87  T values[8]; vec3i indices[8]; double coef[8];
88  interpolate_coef(array.shape(),p,indices,coef);
89  T value = T();
90  if( only_actives ) {
91  double w[8];
92  double sum (0.0);
93  for( int n=0; n<8; ++n ) w[n] = array.active(indices[n]) ? coef[n] : 0.0;
94  for( int n=0; n<8; ++n ) sum += w[n];
95  if( sum ) {
96  for( int n=0; n<8; ++n ) w[n] /= sum;
97  for( unsigned n=0; n<8; ++n ) if( w[n] ) value += array(indices[n]) * w[n];
98  }
99  } else {
100  for( unsigned n=0; n<8; ++n ) if( coef[n] ) value += array(indices[n]) * coef[n];
101  }
102  return value;
103  }
120  template<class T> static T interpolate( const array3<T> &array, const vec3d &origin, double dx, const vec3d &p, bool only_actives=false ) {
121  return interpolate<T>(array,(p-origin)/dx,only_actives);
122  }
123 };
124 //
126 //
127 #endif
128 //
shape3::w
unsigned w
Width of the shape.
Definition: shape.h:942
array3::active
bool active(int i, int j, int k) const
Get if a position on grid is active.
Definition: array3.h:552
shape3::h
unsigned h
Height of the shape.
Definition: shape.h:947
shape3
Structure that defines a three dimensional shape such as width, height and depth.
Definition: shape.h:478
array_interpolator3
Namespace that implements array interpolation.
Definition: array_interpolator3.h:37
shape3::d
unsigned d
Depth of the shape.
Definition: shape.h:952
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
array3.h
vec
Fixed sized vector structure.
Definition: vec.h:38
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
array3::shape
shape3 shape() const
Get the shape of the array.
Definition: array3.h:218
array3
Three dimensional array class designed to be defined as instance member in recursive_configurable cla...
Definition: array3.h:42