23 releases (15 stable)

1.3.0 Apr 14, 2024
1.2.11 Mar 24, 2024
1.2.9 May 11, 2023
1.2.8 Apr 28, 2023
0.1.6 Jan 21, 2023

#399 in Parser implementations

Download history 5/week @ 2024-07-28 5/week @ 2024-09-22 9/week @ 2024-09-29

1,250 downloads per month

MIT license

430KB
9K SLoC

C++ 4.5K SLoC // 0.0% comments Rust 4K SLoC // 0.0% comments C 469 SLoC // 0.0% comments Batch 5 SLoC

gvox-rs

Safe, high-level Rust API for the GVOX voxel data library

Crates.io Docs.rs

This library supplies an idiomatic Rust abstraction over the GVOX C API. It provides type safety, memory safety, and thread safety without any significant deviation from the C library's design. For more information on the API's design, see the GVOX Wiki.

Below is a simple example which demonstrates how to create adapter contexts and utilize them to convert a .gvox file to colored text console output. For additional examples, see the tests in src/tests.rs.

const BYTES: &[u8] = include_bytes!("palette.gvox");
let mut o_buffer = Box::default();

{
    let gvox_ctx = gvox_rs::Context::new();

    let o_config = gvox_rs::adapters::ByteBufferOutputAdapterConfig::from(&mut o_buffer);

    let s_config = gvox_rs::adapters::ColoredTextSerializeAdapterConfig {
        downscale_factor: 1,
        downscale_mode: gvox_rs::adapters::ColoredTextSerializeAdapterDownscaleMode::Nearest,
        non_color_max_value: 5,
    };

    let mut i_ctx = gvox_ctx.get_adapter::<gvox_rs::Input, gvox_rs::adapters::ByteBuffer>()
        .expect("Failed to get byte buffer input adapter.").create_adapter_context(BYTES)
        .expect("Failed to create adapter context.");

    let mut o_ctx = gvox_ctx.get_adapter::<gvox_rs::Output, gvox_rs::adapters::ByteBuffer>()
        .expect("Failed to get byte buffer input adapter.").create_adapter_context(o_config)
        .expect("Failed to create adapter context.");
    
    let mut p_ctx = gvox_ctx.get_adapter::<gvox_rs::Parse, gvox_rs::adapters::GvoxPalette>()
        .expect("Failed to get byte buffer input adapter.").create_adapter_context(())
        .expect("Failed to create adapter context.");

    let mut s_ctx = gvox_ctx.get_adapter::<gvox_rs::Serialize, gvox_rs::adapters::ColoredText>()
        .expect("Failed to get byte buffer input adapter.").create_adapter_context(s_config)
        .expect("Failed to create adapter context.");

    let region = gvox_rs::RegionRange {
        offset: gvox_rs::Offset3D {
            x: -4,
            y: -4,
            z: -4,
        },
        extent: gvox_rs::Extent3D { x: 8, y: 8, z: 8 },
    };

    gvox_rs::blit_region(
        &mut i_ctx,
        &mut o_ctx,
        &mut p_ctx,
        &mut s_ctx,
        &region,
        gvox_rs::ChannelId::COLOR | gvox_rs::ChannelId::NORMAL | gvox_rs::ChannelId::MATERIAL_ID,
    ).expect("Error while translating.");
}

assert_eq!(22228, o_buffer.len(), "Buffer output length did not match expected.");
println!("{}", std::str::from_utf8(&o_buffer).expect("Bad string slice."));

Building

For now, you must have the following things installed to build the repository.

  • A C++ compiler
  • CMake (3.21 or higher)
  • Ninja build
  • vcpkg (plus the VCPKG_ROOT environment variable)
  • The latest WASI_SDK (if you are building for WASM) These are necessary because the gvox-rs library is built on the gvox-sys library (subfolder), which is generated via bindgen in order to create language bindings to the C API of Gvox. See the Gvox README for more info!

Dependencies

~175–560KB
~12K SLoC