numerics 0.1.0
Loading...
Searching...
No Matches
fields.cpp
Go to the documentation of this file.
1/// @file src/fields/fields.cpp
2/// @brief Out-of-line implementations for ScalarField3D and VectorField3D.
3#include "fields/field3d.hpp"
4
5#include "core/vector.hpp"
6
7namespace num {
8
9float ScalarField3D::sample(float x, float y, float z) const {
10 const float gx = (x - ox()) / dx();
11 const float gy = (y - oy()) / dx();
12 const float gz = (z - oz()) / dx();
13
14 if (gx < 0 || gx >= nx() - 1 || gy < 0 || gy >= ny() - 1 || gz < 0 || gz >= nz() - 1) {
15 return 0.0f;
16 }
17
18 const int i0 = static_cast<int>(gx);
19 const int j0 = static_cast<int>(gy);
20 const int k0 = static_cast<int>(gz);
21 const float tx = gx - i0, ty = gy - j0, tz = gz - k0;
22
23 auto v = [&](int di, int dj, int dk) {
24 return static_cast<float>((*this)(i0 + di, j0 + dj, k0 + dk));
25 };
26 return ((1 - tz)
27 * (((1 - ty) * (((1 - tx) * v(0, 0, 0)) + (tx * v(1, 0, 0))))
28 + (ty * (((1 - tx) * v(0, 1, 0)) + (tx * v(1, 1, 0))))))
29 + (tz
30 * (((1 - ty) * (((1 - tx) * v(0, 0, 1)) + (tx * v(1, 0, 1))))
31 + (ty * (((1 - tx) * v(0, 1, 1)) + (tx * v(1, 1, 1))))));
32}
33
35 int ny,
36 int nz,
37 float dx,
38 float ox,
39 float oy,
40 float oz)
41 : x(nx, ny, nz, dx, ox, oy, oz),
42 y(nx, ny, nz, dx, ox, oy, oz),
43 z(nx, ny, nz, dx, ox, oy, oz) {
44}
45
46std::array<float, 3> VectorField3D::sample(float px, float py, float pz) const {
47 return {x.sample(px, py, pz), y.sample(px, py, pz), z.sample(px, py, pz)};
48}
49
50void VectorField3D::scale(float s) {
51 num::scale(x.vec(), static_cast<real>(s));
52 num::scale(y.vec(), static_cast<real>(s));
53 num::scale(z.vec(), static_cast<real>(s));
54}
55
56} // namespace num
float oy() const
Definition field3d.hpp:54
Vector & vec()
Definition field3d.hpp:78
float ox() const
Definition field3d.hpp:53
int nx() const
Definition field3d.hpp:49
float dx() const
Definition field3d.hpp:52
float sample(float x, float y, float z) const
Definition fields.cpp:9
int ny() const
Definition field3d.hpp:50
float oz() const
Definition field3d.hpp:55
int nz() const
Definition field3d.hpp:51
3D scalar and vector fields on uniform Cartesian grids.
double real
Definition types.hpp:10
void scale(Vector &v, real alpha, Backend b=default_backend)
Compute .
Definition vector.cpp:15
std::array< float, 3 > sample(float px, float py, float pz) const
Definition fields.cpp:46
ScalarField3D z
Definition field3d.hpp:92
VectorField3D(int nx, int ny, int nz, float dx, float ox=0.0f, float oy=0.0f, float oz=0.0f)
Definition fields.cpp:34
ScalarField3D x
Definition field3d.hpp:92
void scale(float s)
Definition fields.cpp:50
ScalarField3D y
Definition field3d.hpp:92
Dense vector storage and operations.