1 个稳定版本
1.0.0 | 2023年3月21日 |
---|
在 解析器实现 中排名第 2746
每月下载量 30 次
25KB
267 代码行
petra_grid
petra_grid
是一个用于读取由 Petra™ 地质解释应用程序生成的 .GRD
文件格式的网格数据的库
该库基于在十六进制编辑器中花费大量时间查看网格文件和仔细阅读公开可用的文档。由于需要,它是不完整和不正确的,并且将在文件格式得到适当和公开指定之前保持如此。
然而,该库能够成功读取矩形和三角网格以及大量其元数据。如果您在读取网格时遇到问题并且能够与开发者分享网格(GRD 和导出格式),请提交一个问题。
示例用法
此库可以用于从文件、缓冲区或实现 std::io::Read
和 std::io::Seek
的任何其他源读取 .GRD
网格数据。以下是一个用于在命令行中转储网格文件“调试”表示的简短程序
use std::{
env,
fs::File,
process::ExitCode,
};
use petra_grid::{Error, Grid};
fn process_grid_file(path: &String) -> Result<(), Error> {
let mut f = File::open(path)?;
let grid = Grid::read(&mut f)?;
println!("{}:\n{:?}", path, grid);
Ok(())
}
fn main() -> ExitCode {
let args = env::args().collect::<Vec<_>>();
match &args[..] {
[] => {
eprintln!("Usage: read_grid <grd-files>");
return ExitCode::from(2);
},
[prog] => {
eprintln!("Usage: {} <grd-files>", prog);
return ExitCode::from(2);
},
_ => {},
}
let mut any_error = false;
for path in &args[1..] {
match process_grid_file(path) {
Ok(()) => { },
Err(e) => {
eprintln!("Error reading {}: {}", path, e);
any_error = true;
},
};
}
if any_error {
ExitCode::from(1)
} else {
ExitCode::from(0)
}
}
作为另一个示例,我们可以使用 plotters
绘制矩形或三角网格的灰度渲染(轴标签等留给读者练习!)
use std::{
env,
error::Error,
fs::File,
process::ExitCode,
};
use plotters::prelude::*;
use petra_grid::{Grid, GridData};
const PIXELS_PER_XY_UNIT: f64 = 1e-3;
fn greyscale(val: f64, min: f64, span: f64) -> RGBColor {
let frac = ((val - min) / span * 255.0) as u8;
RGBColor(frac, frac, frac)
}
fn plot_grid(grid: &Grid, path: &String) -> Result<(), Box<dyn Error>> {
let xspan = grid.xmax - grid.xmin;
let yspan = grid.ymax - grid.ymin;
let zspan = grid.zmax - grid.zmin;
let xres = (xspan * PIXELS_PER_XY_UNIT) as u32;
let yres = (yspan * PIXELS_PER_XY_UNIT) as u32;
let root = BitMapBackend::new(path, (xres, yres)).into_drawing_area();
root.fill(&WHITE)?;
match &grid.data {
GridData::Rectangular(arr) => {
let (rows, cols) = arr.dim();
let mut chart = ChartBuilder::on(&root)
.build_cartesian_2d(0..cols, 0..rows)?;
chart.configure_mesh()
.disable_x_mesh()
.disable_y_mesh()
.draw()?;
chart.draw_series(
arr.indexed_iter().filter_map(|((j, i), &z)| {
if z.is_nan() {
None
} else {
Some(Rectangle::new(
[(i, j), ((i + 1), (j + 1))],
greyscale(z, grid.zmin, zspan).filled()))
}
})
)?;
},
GridData::Triangular(arr) => {
let mut chart = ChartBuilder::on(&root)
.build_cartesian_2d(grid.xmin..grid.xmax, grid.ymin..grid.ymax)?;
chart.configure_mesh()
.disable_x_mesh()
.disable_y_mesh()
.draw()?;
chart.draw_series(
arr.outer_iter().filter_map(|tri| {
let mut verts = vec![];
let mut z_avg = 0.0;
for vert in tri.outer_iter() {
verts.push((vert[0], vert[1]));
z_avg += vert[2];
}
z_avg /= 3.0;
if z_avg.is_nan() {
None
} else {
Some(Polygon::new(verts,
greyscale(z_avg, grid.zmin, zspan).filled()))
}
})
)?;
},
};
Ok(())
}
fn process_grid_file(path: &String) -> Result<(), Box<dyn Error>> {
let mut f = File::open(path)?;
let grid = Grid::read(&mut f)?;
let output_path = match path.to_lowercase().strip_suffix(".grd") {
Some(base) => format!("{}.png", base),
None => format!("{}.png", path),
};
plot_grid(&grid, &output_path)
}
fn main() -> ExitCode {
let args = env::args().collect::<Vec<_>>();
match &args[..] {
[] => {
eprintln!("Usage: read_grid <grd-files>");
return ExitCode::from(2);
},
[prog] => {
eprintln!("Usage: {} <grd-files>", prog);
return ExitCode::from(2);
},
_ => {},
}
let mut any_error = false;
for path in &args[1..] {
match process_grid_file(path) {
Ok(()) => { },
Err(e) => {
eprintln!("Error reading {}: {}", path, e);
any_error = true;
},
};
}
if any_error {
ExitCode::from(1)
} else {
ExitCode::from(0)
}
}
在 MIT 许可证 下提供
(c) 2023 dwt | terminus, LLC
依赖关系
~2MB
~40K SLoC