Skip to main content

del_msh_cpu/
grid2.rs

1pub enum Interpolation {
2    Nearest,
3    Bilinear,
4}
5
6/// coordinate (0., 0.) is the center ot the texel
7pub fn bilinear_integer_center<const NDIM: usize>(
8    pix: &[f32; 2],
9    tex_shape: &(usize, usize),
10    tex_data: &[f32],
11) -> [f32; NDIM] {
12    let rx = pix[0] - pix[0].floor();
13    let ix0 = pix[0].floor() as i64;
14    if ix0 < 0 || ix0 >= tex_shape.0 as i64 {
15        return [0f32; NDIM];
16    }
17    let ry = pix[1] - pix[1].floor();
18    let iy0 = pix[1].floor() as i64;
19    if iy0 < 0 || iy0 >= tex_shape.1 as i64 {
20        return [0f32; NDIM];
21    }
22    //
23    let ix0 = ix0 as usize;
24    let iy0 = iy0 as usize;
25    let ix1 = ix0 + 1;
26    let iy1 = iy0 + 1;
27    let i00_tex = iy0 * tex_shape.0 + ix0;
28    let i10_tex = iy0 * tex_shape.0 + ix1;
29    let i01_tex = iy1 * tex_shape.0 + ix0;
30    let i11_tex = iy1 * tex_shape.0 + ix1;
31    std::array::from_fn(|i_dim| {
32        let v00 = tex_data[i00_tex * NDIM + i_dim];
33        let v01 = tex_data[i01_tex * NDIM + i_dim];
34        let v10 = tex_data[i10_tex * NDIM + i_dim];
35        let v11 = tex_data[i11_tex * NDIM + i_dim];
36        (1. - rx) * (1. - ry) * v00 + rx * (1. - ry) * v10 + (1. - rx) * ry * v01 + rx * ry * v11
37    })
38}
39
40/// coordinate (0., 0.) is the center ot the texel
41pub fn nearest_integer_center<const NDIM: usize>(
42    pix: &[f32; 2],
43    tex_shape: &(usize, usize),
44    tex_data: &[f32],
45) -> [f32; NDIM] {
46    let ix0 = pix[0].round() as i64;
47    if ix0 < 0 || ix0 >= tex_shape.0 as i64 {
48        return [0f32; NDIM];
49    }
50    let iy0 = pix[1].round() as i64;
51    if iy0 < 0 || iy0 >= tex_shape.1 as i64 {
52        return [0f32; NDIM];
53    }
54    let i_tex = (iy0 as usize) * tex_shape.0 + (ix0 as usize);
55    // assert!(i_tex >=0 && i_tex < tex_shape.1);
56    std::array::from_fn(|i_dim| tex_data[i_tex * NDIM + i_dim])
57}
58
59// left up corner is the origin
60pub fn to_quadmesh3_hightmap(
61    grid_shape: (usize, usize),
62    height: &[f32],
63    elen: f32,
64) -> (Vec<usize>, Vec<f32>) {
65    let nw = grid_shape.0;
66    let nh = grid_shape.1;
67    let _num_vtx = nw * nh;
68    let mw = nw - 1; // quads in width
69    let mh = nh - 1; // quads in height
70    let mut quad2vtx = Vec::<usize>::with_capacity(mw * mh * 4);
71    for ih in 0..mh {
72        for iw in 0..mw {
73            let i01_vtx = ih * nw + iw;
74            let i11_vtx = ih * nw + iw + 1;
75            let i00_vtx = (ih + 1) * nw + iw;
76            let i10_vtx = (ih + 1) * nw + iw + 1;
77            quad2vtx.extend_from_slice(&[i00_vtx, i10_vtx, i11_vtx, i01_vtx]);
78        }
79    }
80    let mut vtx2xyz = Vec::<f32>::with_capacity(nw * nh * 3);
81    for ih in 0..nh {
82        for iw in 0..nw {
83            let x = iw as f32 * elen;
84            let y = ih as f32 * -elen;
85            let z = height[ih * nw + iw];
86            vtx2xyz.extend_from_slice(&[x, y, z]);
87        }
88    }
89    (quad2vtx, vtx2xyz)
90}