Skip to main content

Python bindings for the mcubes library

Project description

Mcubes: Marching Cubes Isosurface library

Crate Docs PyPI

Uses the Marching Cubes algorithm to create isosurfaces in Rust or Python, from volume data. Designed to be easy to integrate in applications.

Electron density demo

Based loosely on PyMarchingCubes.

Outputs a native Mesh, which contains native Vertexs. In practice, you will convert these to whatever mesh struct your application uses. For example, graphics::Mesh.

Uses lin-alg for its Vec3 type; this is the library's only dependency.

Surface demo

Used by the Daedalus molecule viewer to view experimentally-derived electron density from protein crystals.

The grid must be regularly spaced, along 3 orthogonal axes. Values are either a Vec<f32>, or points which impl mcubes::GridPoint. This trait contains a single method: To get the value at that point.

Example creating a solvent-accessible-surface mesh by setting the ISO level to 0, and rendering only the vertices. Surface mesh

Example use:

use mcubes::{GridPoint, MarchingCubes, MeshSide};

/// An example data struct from your application. If you use something like this 
/// to represent a point, you may with to use the `from_gridpoints` constructor.
pub struct ElectronDensity {
    pub coords: Vec3,
    pub density: f64,
}

impl GridPoint for ElectronDensity {
    fn value(&self) -> f64 { self.density }
}

fn create_mesh(hdr: &MapHeader, density: &[ElectronDensity], iso_level: f32) {
    let mc = MarchingCubes::from_gridpoints(
        // Number of grid points along each axis
        (hdr.nx as usize, hdr.ny as usize, hdr.nz as usize),
        // Grid dimensions per unit cell along each axis
        (hdr.cell[0], hdr.cell[1], hdr.cell[2]),
        // Sampling interval along each axis. (Usually the same as grid point number of grid points.)
        (hdr.mx as f32, hdr.my as f32, hdr.mz as f32),
        density,
        // The value to draw the isosurface at.
        iso_level,
    );
    
    // If you are using a `Vec<f32` for data instead if `GridPoint`, use this constructor:
    let mc = MarchingCubes::new(
        (hdr.nx as usize, hdr.ny as usize, hdr.nz as usize),
        (hdr.cell[0], hdr.cell[1], hdr.cell[2]),
        (hdr.mx as f32, hdr.my as f32, hdr.mz as f32),
        values,  // Vec<f32>
        iso_level,
    );


    // Use MeshSide::Inside or MeshSide::Outside as required.
    let mesh = mc.generate(MeshSide::Both);
    
    // Example of converting the generic output mesh to a graphic engine's:
    let vertices = mesh
        .vertices
        .iter()
        .map(|v| graphics::Vertex::new(v.posit.to_arr(), v.normal))
        .collect();

    scene.meshes[MESH_DENSITY_SURFACE] = graphics::Mesh {
        vertices,
        indices: mesh.indices,
        material: 0,
    };
}

Why another Marching Cubes library? I couldn't figure out how to use the existing ones.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcubes-0.1.4.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcubes-0.1.4-cp310-abi3-win_amd64.whl (138.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

File details

Details for the file mcubes-0.1.4.tar.gz.

File metadata

  • Download URL: mcubes-0.1.4.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for mcubes-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8851dfd02cd7b45d0708aa4f3bb5ad78d0bbe47090fba14107c2ba80957edb9f
MD5 bf8af6bf922424946a57356b59b43c72
BLAKE2b-256 5e2c0b45184e3aaecdf004f6a6681d4157585158c51dcd6f54112ecfdd9dc753

See more details on using hashes here.

File details

Details for the file mcubes-0.1.4-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: mcubes-0.1.4-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 138.0 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for mcubes-0.1.4-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e09d215814b45fd076e975be6698c090e536270849671b5b53102da9fe71314a
MD5 2aaf9ec42dfcdbaaccde962382cfab3b
BLAKE2b-256 ec695b81da2f290abd60033b30911dcb53fd9f9cc1d3daba5184b9929572b5a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page