Shiokaze Framework
A research-oriented fluid solver for computer graphics
array_derivative2.h
Go to the documentation of this file.
1 /*
2 ** array_derivative2.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_DERIVATIVE2_H
26 #define SHKZ_ARRAY_DERIVATIVE2_H
27 //
28 #include "array2.h"
29 #include <cmath>
30 #include <cstdlib>
31 //
33 //
35 class array_derivative2 {
38 public:
51  static void derivative_interpolate_coef( const shape2 &shape, const vec2d &p, vec2i indices[4], double coef[DIM2][4] ) {
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  int i = std::min(x,shape.w-2.);
56  int j = std::min(y,shape.h-2.);
57  //
58  indices[0] = vec2i(i,j);
59  indices[1] = vec2i(i+1,j);
60  indices[2] = vec2i(i,j+1);
61  indices[3] = vec2i(i+1,j+1);
62  // x
63  coef[0][0] = -(j+1-y);
64  coef[0][1] = (j+1-y);
65  coef[0][2] = -(y-j);
66  coef[0][3] = (y-j);
67  // y
68  coef[1][0] = -(i+1-x);
69  coef[1][1] = -(x-i);
70  coef[1][2] = (i+1-x);
71  coef[1][3] = (x-i);
72  }
83  template<class T>
84  static void derivative( const array2<T> &array, const vec2d &p, T result[DIM2] ) {
85  //
86  T values[4]; vec2i indices[4]; double coef[DIM2][4];
87  for( unsigned dim : DIMS2 ) result[dim] = T();
88  derivative_interpolate_coef(array.shape(),p,indices,coef);
89  for( unsigned dim : DIMS2 ) {
90  for( unsigned n=0; n<4; ++n ) {
91  result[dim] += coef[dim][n] * array(indices[n][0],indices[n][1]);
92  }
93  }
94  }
109  template<class T> static vec2<T> derivative( const array2<T> &array, const vec2d &origin, double dx, const vec2d &p ) {
110  vec2<T> result;
111  derivative<T>(array,(p-origin)/dx,result.v);
112  return result/dx;
113  }
114 };
115 //
117 //
118 #endif
119 //
array_derivative2::derivative_interpolate_coef
static void derivative_interpolate_coef(const shape2 &shape, const vec2d &p, vec2i indices[4], double coef[DIM2][4])
Get the coefficients for the gradient.
Definition: array_derivative2.h:51
array_derivative2::derivative
static void derivative(const array2< T > &array, const vec2d &p, T result[DIM2])
Computes the the gradient of a physical quantity.
Definition: array_derivative2.h:84
array2.h
array_derivative2
Class that computes the derivative of physical quantities at arbitrary position.
Definition: array_derivative2.h:37
vec::v
T v[D]
Vector value array.
Definition: vec.h:44
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
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
array_derivative2::derivative
static vec2< T > derivative(const array2< T > &array, const vec2d &origin, double dx, const vec2d &p)
Computes the the gradient of a physical quantity.
Definition: array_derivative2.h:109
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