PeriDyno 1.0.0
Loading...
Searching...
No Matches
Lerp.h
Go to the documentation of this file.
1
16#include "Vector.h"
17
18#include "Array/Array.h"
19#include "Array/Array2D.h"
20#include "Array/Array3D.h"
21
22
23namespace dyno
24{
28
34
35 template<typename T, DeviceType deviceType>
36 DYN_FUNC T lerp(Array<T, deviceType>& array1d, float x, LerpMode mode = LerpMode::REPEAT)
37 {
38 const uint nx = array1d.size();
39
40 int i0 = (int)floor(x);
41
42 int i1 = i0 + 1;
43
44 const float fx = x - i0;
45
46 i0 = mode == LerpMode::REPEAT ? i0 % nx : (i0 < 0 ? 0 : (i0 >= nx ? nx - 1 : nx));
47 i1 = mode == LerpMode::REPEAT ? i1 % nx : (i1 < 0 ? 0 : (i1 >= nx ? nx - 1 : nx));
48
49 const float w0 = (1.0f - fx);
50 const float w1 = fx;
51
52 return w0 * array1d[i0] + w1 * array1d[i1];
53 };
54
55 template<typename T, DeviceType deviceType>
56 DYN_FUNC T bilinear(Array2D<T, deviceType>& array2d, float x, float y, LerpMode mode = LerpMode::REPEAT)
57 {
58 const uint nx = array2d.nx();
59 const uint ny = array2d.ny();
60
61 int i0 = (int)floor(x);
62 int j0 = (int)floor(y);
63
64 int i1 = i0 + 1;
65 int j1 = j0 + 1;
66
67 const float fx = x - i0;
68 const float fy = y - j0;
69
70 i0 = mode == LerpMode::REPEAT ? i0 % nx : (i0 < 0 ? 0 : (i0 >= nx ? nx - 1 : nx));
71 j0 = mode == LerpMode::REPEAT ? j0 % ny : (j0 < 0 ? 0 : (j0 >= ny ? ny - 1 : ny));
72
73 i1 = mode == LerpMode::REPEAT ? i1 % nx : (i1 < 0 ? 0 : (i1 >= nx ? nx - 1 : nx));
74 j1 = mode == LerpMode::REPEAT ? j1 % ny : (j1 < 0 ? 0 : (j1 >= ny ? ny - 1 : ny));
75
76 const float w00 = (1.0f - fx) * (1.0f - fy);
77 const float w10 = fx * (1.0f - fy);
78 const float w01 = (1.0f - fx) * fy;
79 const float w11 = fx * fy;
80
81 return w00 * array2d(i0, j0) + w01 * array2d(i0, j1) + w10 * array2d(i1, j0) + w11 * array2d(i1, j1);
82 };
83
84 template<typename T, DeviceType deviceType>
85 DYN_FUNC T trilinear(Array3D<T, deviceType>& array3d, float x, float y, float z, LerpMode mode = LerpMode::REPEAT)
86 {
87 const uint nx = array3d.nx();
88 const uint ny = array3d.ny();
89 const uint nz = array3d.nz();
90
91 int i0 = (int)floor(x);
92 int j0 = (int)floor(y);
93 int k0 = (int)floor(z);
94
95 int i1 = i0 + 1;
96 int j1 = j0 + 1;
97 int k1 = k0 + 1;
98
99 const float fx = x - i0;
100 const float fy = y - j0;
101 const float fz = z - k0;
102
103 i0 = mode == LerpMode::REPEAT ? i0 % nx : (i0 < 0 ? 0 : (i0 >= nx ? nx - 1 : nx));
104 j0 = mode == LerpMode::REPEAT ? j0 % ny : (j0 < 0 ? 0 : (j0 >= ny ? ny - 1 : ny));
105 k0 = mode == LerpMode::REPEAT ? k0 % nz : (k0 < 0 ? 0 : (k0 >= nz ? nz - 1 : nz));
106
107 i1 = mode == LerpMode::REPEAT ? i1 % nx : (i1 < 0 ? 0 : (i1 >= nx ? nx - 1 : nx));
108 j1 = mode == LerpMode::REPEAT ? j1 % ny : (j1 < 0 ? 0 : (j1 >= ny ? ny - 1 : ny));
109 k1 = mode == LerpMode::REPEAT ? k1 % nz : (k1 < 0 ? 0 : (k1 >= nz ? nz - 1 : nz));
110
111 const float w000 = (1.0f - fx) * (1.0f - fy) * (1.0f - fz);
112 const float w100 = fx * (1.0f - fy) * (1.0f - fz);
113 const float w010 = (1.0f - fx) * fy * (1.0f - fz);
114 const float w001 = (1.0f - fx) * (1.0f - fy) * fz;
115 const float w111 = fx * fy * fz;
116 const float w011 = (1.0f - fx) * fy * fz;
117 const float w101 = fx * (1.0f - fy) * fz;
118 const float w110 = fx * fy * (1.0f - fz);
119
120 return w000 * array3d(i0, j0, k0) + w100 * array3d(i1, j0, k0) + w010 * array3d(i0, j1, k0) + w001 * array3d(i0, j0, k1)
121 + w111 * array3d(i1, j1, k1) + w011 * array3d(i0, j1, k1) + w101 * array3d(i1, j0, k1) + w110 * array3d(i1, j1, k0);
122 };
123}
This class is designed to be elegant, so it can be directly passed to GPU as parameters.
Definition Array.h:27
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
LerpMode
Definition Lerp.h:30
@ REPEAT
Definition Lerp.h:31
@ CLAMP_TO_BORDER
Definition Lerp.h:32
DYN_FUNC T bilinear(Array2D< T, deviceType > &array2d, float x, float y, LerpMode mode=LerpMode::REPEAT)
Definition Lerp.h:56
DYN_FUNC T lerp(Array< T, deviceType > &array1d, float x, LerpMode mode=LerpMode::REPEAT)
Definition Lerp.h:36
unsigned int uint
Definition VkProgram.h:14
DYN_FUNC T trilinear(Array3D< T, deviceType > &array3d, float x, float y, float z, LerpMode mode=LerpMode::REPEAT)
Definition Lerp.h:85