Skip to content

Commit

Permalink
Remove duplicate render functions from public API
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Nov 15, 2024
1 parent 70daa2d commit e6fe8a5
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 62 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
can now render non-square images!
- Meshing uses just the `Camera`, to position the model within the ±1 bounds
- The previous `fidget::shape::Bounds` type is removed
- Remove `fidget::render::render2d/3d` from the public API, as they're
equivalent to the functions on `ImageRenderConfig` / `VoxelRenderConfig`

# 0.3.3
- `Function` and evaluator types now produce multiple outputs
Expand Down
14 changes: 5 additions & 9 deletions demos/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn run3d<F: fidget::eval::Function + fidget::shape::RenderHints>(
let mut depth = vec![];
let mut color = vec![];
for _ in 0..settings.n {
(depth, color) = fidget::render::render3d(shape.clone(), &cfg);
(depth, color) = cfg.run(shape.clone());
}

let out = if mode_color {
Expand Down Expand Up @@ -206,10 +206,8 @@ fn run2d<F: fidget::eval::Function + fidget::shape::RenderHints>(
if sdf {
let mut image = vec![];
for _ in 0..settings.n {
image = fidget::render::render2d::<
_,
fidget::render::SdfRenderMode,
>(shape.clone(), &cfg);
image =
cfg.run::<_, fidget::render::SdfRenderMode>(shape.clone());
}
image
.into_iter()
Expand All @@ -218,10 +216,8 @@ fn run2d<F: fidget::eval::Function + fidget::shape::RenderHints>(
} else {
let mut image = vec![];
for _ in 0..settings.n {
image = fidget::render::render2d::<
_,
fidget::render::DebugRenderMode,
>(shape.clone(), &cfg);
image = cfg
.run::<_, fidget::render::DebugRenderMode>(shape.clone());
}
image
.into_iter()
Expand Down
20 changes: 7 additions & 13 deletions demos/viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,8 @@ fn render<F: fidget::eval::Function + fidget::shape::RenderHints>(

match mode {
Mode2D::Color => {
let image = fidget::render::render2d::<
_,
fidget::render::BitRenderMode,
>(shape, &config);
let image =
config.run::<_, fidget::render::BitRenderMode>(shape);
let c = egui::Color32::from_rgba_unmultiplied(
color[0],
color[1],
Expand All @@ -190,20 +188,16 @@ fn render<F: fidget::eval::Function + fidget::shape::RenderHints>(
}

Mode2D::Sdf => {
let image = fidget::render::render2d::<
_,
fidget::render::SdfRenderMode,
>(shape, &config);
let image =
config.run::<_, fidget::render::SdfRenderMode>(shape);
for (p, i) in pixels.iter_mut().zip(&image) {
*p = egui::Color32::from_rgb(i[0], i[1], i[2]);
}
}

Mode2D::Debug => {
let image = fidget::render::render2d::<
_,
fidget::render::DebugRenderMode,
>(shape, &config);
let image =
config.run::<_, fidget::render::DebugRenderMode>(shape);
for (p, i) in pixels.iter_mut().zip(&image) {
let c = i.as_debug_color();
*p = egui::Color32::from_rgb(c[0], c[1], c[2]);
Expand All @@ -226,7 +220,7 @@ fn render<F: fidget::eval::Function + fidget::shape::RenderHints>(
),
..Default::default()
};
let (depth, color) = fidget::render::render3d(shape, &config);
let (depth, color) = config.run(shape);
match mode {
ThreeDMode::Color => {
for (p, (&d, &c)) in
Expand Down
20 changes: 4 additions & 16 deletions fidget/benches/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ pub fn prospero_size_sweep(c: &mut Criterion) {
group.bench_function(BenchmarkId::new("vm", size), move |b| {
b.iter(|| {
let tape = shape_vm.clone();
black_box(fidget::render::render2d::<
_,
fidget::render::BitRenderMode,
>(tape, cfg))
black_box(cfg.run::<_, fidget::render::BitRenderMode>(tape))
})
});

Expand All @@ -43,10 +40,7 @@ pub fn prospero_size_sweep(c: &mut Criterion) {
group.bench_function(BenchmarkId::new("jit", size), move |b| {
b.iter(|| {
let tape = shape_jit.clone();
black_box(fidget::render::render2d::<
_,
fidget::render::BitRenderMode,
>(tape, cfg))
black_box(cfg.run::<_, fidget::render::BitRenderMode>(tape))
})
});
}
Expand Down Expand Up @@ -74,10 +68,7 @@ pub fn prospero_thread_sweep(c: &mut Criterion) {
group.bench_function(BenchmarkId::new("vm", threads), move |b| {
b.iter(|| {
let tape = shape_vm.clone();
black_box(fidget::render::render2d::<
_,
fidget::render::BitRenderMode,
>(tape, cfg))
black_box(cfg.run::<_, fidget::render::BitRenderMode>(tape))
})
});
#[cfg(feature = "jit")]
Expand All @@ -91,10 +82,7 @@ pub fn prospero_thread_sweep(c: &mut Criterion) {
group.bench_function(BenchmarkId::new("jit", threads), move |b| {
b.iter(|| {
let tape = shape_jit.clone();
black_box(fidget::render::render2d::<
_,
fidget::render::BitRenderMode,
>(tape, cfg))
black_box(cfg.run::<_, fidget::render::BitRenderMode>(tape))
})
});
}
Expand Down
19 changes: 6 additions & 13 deletions fidget/src/render/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
eval::Function,
render::{RegionSize, RenderMode, View2, View3},
shape::{Shape, TileSizes},
Error,
};
use nalgebra::{Const, Matrix3, Matrix4, OPoint, Point2, Vector2};
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -97,15 +96,12 @@ impl Default for ImageRenderConfig {
}

impl ImageRenderConfig {
/// High-level API for rendering shapes in 2D
///
/// Under the hood, this delegates to
/// [`fidget::render::render2d`](crate::render::render2d())
/// Render a shape in 2D using this configuration
pub fn run<F: Function, M: RenderMode + Sync>(
&self,
shape: Shape<F>,
) -> Result<Vec<<M as RenderMode>::Output>, Error> {
Ok(crate::render::render2d::<F, M>(shape, self))
) -> Vec<<M as RenderMode>::Output> {
crate::render::render2d::<F, M>(shape, self)
}

/// Returns the combined screen-to-model transform matrix
Expand Down Expand Up @@ -150,17 +146,14 @@ impl Default for VoxelRenderConfig {
}

impl VoxelRenderConfig {
/// High-level API for rendering shapes in 2D
///
/// Under the hood, this delegates to
/// [`fidget::render::render3d`](crate::render::render3d())
/// Render a shape in 3D using this configuration
///
/// Returns a tuple of heightmap, RGB image.
pub fn run<F: Function>(
&self,
shape: Shape<F>,
) -> Result<(Vec<u32>, Vec<[u8; 3]>), Error> {
Ok(crate::render::render3d::<F>(shape, self))
) -> (Vec<u32>, Vec<[u8; 3]>) {
crate::render::render3d::<F>(shape, self)
}

/// Returns the combined screen-to-model transform matrix
Expand Down
10 changes: 4 additions & 6 deletions fidget/src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! 2D and 3D rendering
//!
//! The easiest way to render something is with
//! [`RenderConfig::run`](RenderConfig::run); you can also use the lower-level
//! functions ([`render2d`](render2d()) and [`render3d`](render3d())) for manual
//! control over the input tape.
//! To render something, build a configuration object then call its `run`
//! function, e.g. [`ImageRenderConfig::run`] and [`VoxelRenderConfig::run`].
use crate::{
eval::{BulkEvaluator, Function, Trace, TracingEvaluator},
shape::{Shape, ShapeTape},
Expand All @@ -20,8 +18,8 @@ pub use config::{ImageRenderConfig, ThreadCount, VoxelRenderConfig};
pub use region::{ImageSize, RegionSize, VoxelSize};
pub use view::{View2, View3};

pub use render2d::render as render2d;
pub use render3d::render as render3d;
use render2d::render as render2d;
use render3d::render as render3d;

pub use render2d::{
BitRenderMode, DebugRenderMode, RenderMode, SdfPixelRenderMode,
Expand Down
2 changes: 1 addition & 1 deletion fidget/src/render/render2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ mod test {
view,
..Default::default()
};
let out = cfg.run::<_, BitRenderMode>(shape).unwrap();
let out = cfg.run::<_, BitRenderMode>(shape);
let mut img_str = String::new();
for (i, b) in out.iter().enumerate() {
if i % width as usize == 0 {
Expand Down
5 changes: 3 additions & 2 deletions fidget/src/render/render3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ mod test {
image_size: VoxelSize::from(128), // very small!
..Default::default()
};
let out = cfg.run(shape);
assert!(out.is_ok());
let (depth, rgb) = cfg.run(shape);
assert_eq!(depth.len(), 128 * 128);
assert_eq!(rgb.len(), 128 * 128);
}
}
5 changes: 3 additions & 2 deletions fidget/src/render/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nalgebra::{
Matrix3, Matrix4, Point2, Vector2, Vector3,
};

/// Helper object for a camera in 2D or 3D space
/// Object providing a view-to-model transform in 2D
///
/// Rendering and meshing happen in the ±1 square or cube; these are referred to
/// as _world_ coordinates. A `Camera` generates a homogeneous transform matrix
Expand Down Expand Up @@ -98,7 +98,7 @@ impl View2 {
}
}

/// See [`View2`] for docstrings
/// Object providing a view-to-model transform in 2D
#[derive(Copy, Clone, Debug)]
pub struct View3 {
mat: Similarity3<f32>,
Expand All @@ -113,6 +113,7 @@ impl Default for View3 {
}

#[allow(missing_docs)]
/// See [`View2`] for docstrings
impl View3 {
pub fn from_center_and_scale(center: Vector3<f32>, scale: f32) -> Self {
let mat =
Expand Down

0 comments on commit e6fe8a5

Please sign in to comment.