use crate::wrappers::mj_visualization::MjvScene;
use crate::error::MjSceneError;
use std::fs::File;
use std::io::{self, BufWriter};
use std::path::Path;
pub fn sync_geoms(src: &MjvScene, dst: &mut MjvScene) -> Result<(), MjSceneError> {
let ffi_src = src.ffi();
let ffi_dst = unsafe { dst.ffi_mut() };
let new_len_i64 = (ffi_dst.ngeom as i64) + (ffi_src.ngeom as i64);
if new_len_i64 > ffi_dst.maxgeom as i64 {
return Err(MjSceneError::SceneFull { capacity: ffi_dst.maxgeom })
}
let new_len = new_len_i64 as i32;
if ffi_src.ngeom == 0 {
return Ok(());
}
unsafe { std::ptr::copy_nonoverlapping(
ffi_src.geoms,
ffi_dst.geoms.add(ffi_dst.ngeom as usize),
ffi_src.ngeom as usize
) };
ffi_dst.ngeom = new_len;
Ok(())
}
pub fn flip_image_vertically<T>(buffer: &mut [T], height: usize, row_len: usize) {
for i in 0..(height / 2) {
let top_idx = i * row_len;
let bottom_idx = (height - 1 - i) * row_len;
let (top_split, bottom_split) = buffer.split_at_mut(bottom_idx);
top_split[top_idx..top_idx + row_len].swap_with_slice(&mut bottom_split[0..row_len]);
}
}
pub fn write_png<P: AsRef<Path>>(
path: P,
data: &[u8],
width: u32,
height: u32,
color_type: png::ColorType,
bit_depth: png::BitDepth,
compression: png::Compression
) -> io::Result<()> {
let file = File::create(path)?;
let w = BufWriter::new(file);
let mut encoder = png::Encoder::new(w, width, height);
encoder.set_color(color_type);
encoder.set_depth(bit_depth);
encoder.set_compression(compression);
let mut writer = encoder
.write_header()
.map_err(io::Error::other)?;
writer
.write_image_data(data)
.map_err(io::Error::other)?;
Ok(())
}