pub trait ComputeVolume {
// Required method
fn compute_volume(&self) -> Real;
}Expand description
Trait for computing the volume of geometric shapes.
This trait provides a uniform interface for volume calculation across different geometric shape types. All shapes implement this trait, allowing for generic volume computation algorithms.
§Examples
use phys_geom::shape::{Sphere, Cuboid};
use phys_geom::volume::ComputeVolume;
use phys_geom::Real;
fn calculate_total_volume<T: ComputeVolume>(shapes: &[T]) -> Real {
shapes.iter().map(|s| s.compute_volume()).sum()
}
// Works with homogeneous collections
let spheres = vec![Sphere::new(1.0), Sphere::new(2.0)];
let sphere_total = calculate_total_volume(&spheres);
let cuboids = vec![Cuboid::new([1.0, 1.0, 1.0]), Cuboid::new([2.0, 2.0, 2.0])];
let cuboid_total = calculate_total_volume(&cuboids);§Implementation Notes
Implementations should use mathematically accurate formulas and be optimized for performance. Volume calculations should be deterministic and return positive values for valid shapes.
Required Methods§
Sourcefn compute_volume(&self) -> Real
fn compute_volume(&self) -> Real
Computes the volume of this shape.
§Returns
The volume as a Real (f32 or f64 depending on features).
Always returns a non-negative value for valid shapes.
§Examples
use phys_geom::shape::Sphere;
use phys_geom::volume::ComputeVolume;
let sphere = Sphere::new(2.0);
let volume = sphere.compute_volume();
assert!(volume > 0.0);