Shiokaze Framework
A research-oriented fluid solver for computer graphics
array_derivative3.h
Go to the documentation of this file.
1 /*
2 ** array_derivative3.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_DERIVATIVE3_H
26 #define SHKZ_ARRAY_DERIVATIVE3_H
27 //
28 #include "array3.h"
29 #include <cmath>
30 #include <cstdlib>
31 //
33 //
35 class array_derivative3 {
38 public:
51  static void derivative_interpolate_coef( const shape3 &shape, const vec3d &p, vec3i indices[8], double coef[DIM3][8] ) {
52  //
53  double x = std::max(0.0,std::min(shape.w-1.,p[0]));
54  double y = std::max(0.0,std::min(shape.h-1.,p[1]));
55  double z = std::max(0.0,std::min(shape.d-1.,p[2]));
56  int i = std::min(x,shape.w-2.);
57  int j = std::min(y,shape.h-2.);
58  int k = std::min(z,shape.d-2.);
59  //
60  indices[0] = vec3i(i,j,k);
61  indices[1] = vec3i(i+1,j,k);
62  indices[2] = vec3i(i,j+1,k);
63  indices[3] = vec3i(i+1,j+1,k);
64  indices[4] = vec3i(i,j,k+1);
65  indices[5] = vec3i(i+1,j,k+1);
66  indices[6] = vec3i(i,j+1,k+1);
67  indices[7] = vec3i(i+1,j+1,k+1);
68  // x
69  coef[0][0] = -(k+1-z)*(j+1-y);
70  coef[0][1] = (k+1-z)*(j+1-y);
71  coef[0][2] = -(k+1-z)*(y-j);
72  coef[0][3] = (k+1-z)*(y-j);
73  coef[0][4] = -(z-k)*(j+1-y);
74  coef[0][5] = (z-k)*(j+1-y);
75  coef[0][6] = -(z-k)*(y-j);
76  coef[0][7] = (z-k)*(y-j);
77  // y
78  coef[1][0] = -(k+1-z)*(i+1-x);
79  coef[1][1] = -(k+1-z)*(x-i);
80  coef[1][2] = (k+1-z)*(i+1-x);
81  coef[1][3] = (k+1-z)*(x-i);
82  coef[1][4] = -(z-k)*(i+1-x);
83  coef[1][5] = -(z-k)*(x-i);
84  coef[1][6] = (z-k)*(i+1-x);
85  coef[1][7] = (z-k)*(x-i);
86  // z
87  coef[2][0] = -(i+1-x)*(j+1-y);
88  coef[2][1] = -(x-i)*(j+1-y);
89  coef[2][2] = -(i+1-x)*(y-j);
90  coef[2][3] = -(x-i)*(y-j);
91  coef[2][4] = (i+1-x)*(j+1-y);
92  coef[2][5] = (x-i)*(j+1-y);
93  coef[2][6] = (i+1-x)*(y-j);
94  coef[2][7] = (x-i)*(y-j);
95  }
106  template<class T> static void derivative( const array3<T> &array, const vec3d &p, T result[DIM3] ) {
107  //
108  T values[8]; vec3i indices[8]; double coef[DIM3][8];
109  for( unsigned dim : DIMS3 ) result[dim] = T();
110  derivative_interpolate_coef(array.shape(),p,indices,coef);
111  for( unsigned dim : DIMS3 ) {
112  for( unsigned n=0; n<8; ++n ) {
113  result[dim] += coef[dim][n] * array(indices[n][0],indices[n][1],indices[n][2]);
114  }
115  }
116  }
131  template<class T> static vec3<T> derivative( const array3<T> &array, const vec3d &origin, double dx, const vec3d &p ) {
132  vec3<T> result;
133  derivative<T>(array,(p-origin)/dx,result.v);
134  return result/dx;
135  }
136 };
137 //
139 //
140 #endif
141 //
shape3::w
unsigned w
Width of the shape.
Definition: shape.h:942
shape3::h
unsigned h
Height of the shape.
Definition: shape.h:947
vec::v
T v[D]
Vector value array.
Definition: vec.h:44
shape3
Structure that defines a three dimensional shape such as width, height and depth.
Definition: shape.h:478
array_derivative3::derivative
static vec3< T > derivative(const array3< T > &array, const vec3d &origin, double dx, const vec3d &p)
Computes the the gradient of a physical quantity.
Definition: array_derivative3.h:131
shape3::d
unsigned d
Depth of the shape.
Definition: shape.h:952
array_derivative3::derivative
static void derivative(const array3< T > &array, const vec3d &p, T result[DIM3])
Computes the the gradient of a physical quantity.
Definition: array_derivative3.h:106
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
array_derivative3::derivative_interpolate_coef
static void derivative_interpolate_coef(const shape3 &shape, const vec3d &p, vec3i indices[8], double coef[DIM3][8])
Get the coefficients for the gradient.
Definition: array_derivative3.h:51
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
array_derivative3
Class that computes the derivative of physical quantities at arbitrary position.
Definition: array_derivative3.h:37
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