1个稳定版本
2.0.2 | 2023年7月12日 |
---|
#1342在数学
28KB
206 行
ND-Interpolate-rs
1-10维插值在Rust中实现。它支持f64和f32数据类型进行插值,并提供线性与三次样条插值,最多支持10维。
此库特别适用于Perlin Noise应用。
示例
以下是一个使用3D三次插值制作球体大理石纹理的示例
use nd_interpolate::*;
use image::*; // image = "0.23.14"
use rand::prelude::*; // rand = "0.8.4"
use std::f64::consts::PI;
fn sphere_marble_texture_example() {
println!("Making marbled sphere texture...");
let imgsize = 512; let w = 2*imgsize; let h = imgsize;
let gridsize = 16; let gridcenter = gridsize / 2;
let radius = (gridsize / 2 - 3) as f64;
let mut img: RgbImage = ImageBuffer::new(w, h);
let mut rand_grid = vec![vec![vec![0f64;gridsize];gridsize];gridsize];
for x in 0..rand_grid.len(){ for y in 0..rand_grid[x].len(){ for z in 0..rand_grid[x][y].len() {
let r: f64 = random();
rand_grid[x][y][z] = r;
} } }
for py in 0..h { for px in 0..w {
let lon = (px as f64 / w as f64) * (2.0*PI) - PI;
let lat = ((h-py) as f64 / h as f64) * PI - 0.5*PI;
let coord = [
radius * lon.sin() * lat.cos(), // X
radius * lat.sin(), // Y
radius * lon.cos() * lat.cos(), // Z
];
let grid_coord: [usize; 3] = [
(coord[0].floor() as i32 + gridcenter as i32) as usize,
(coord[1].floor() as i32 + gridcenter as i32) as usize,
(coord[2].floor() as i32 + gridcenter as i32) as usize
];
let mut local_4x4x4 = [[[0f64;4];4];4];
for ix in 0..4{for iy in 0..4{for iz in 0..4{
let gx = grid_coord[0];
let gy = grid_coord[1];
let gz = grid_coord[2];
local_4x4x4[ix][iy][iz] = rand_grid[ix+gx-1][iy+gy-1][iz+gz-1];
}}}
let v = ((cubic_3D_grid(coord, &local_4x4x4)) * 255f64) as u8;
let pixel = Rgb([v,v,v]);
img.put_pixel(px, py, pixel);
} }
img.save("marbled_sphere_texture.png").unwrap();
println!("...Done!");
}
上述代码生成了以下图像:
备注
此Rust库基于我的Java噪声库插值器
依赖项
~155KB