Skip to content

Commit

Permalink
Shape::{union, subtraction, intersection, smooth_min};
Browse files Browse the repository at this point in the history
Added more shapes
  • Loading branch information
FredericaBernkastel committed Sep 24, 2021
1 parent 5779afb commit a6b3d47
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "space-filling"
version = "0.3.0"
version = "0.3.1"
description = "Generalized 2D space filling"
readme = "readme.md"
authors = ["Frederica Bernkastel <bernkastel.frederica@protonmail.com>"]
Expand Down
6 changes: 3 additions & 3 deletions src/argmax2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ fn offset_to_xy(offset: u64, width: u64) -> Point2D<u64, PixelSpace> {
].into()
}

fn xy_to_offset(xy: Point2D<i64, PixelSpace>, width: u64) -> u64 {
xy.y as u64 * width + xy.x as u64
fn xy_to_offset(xy: Point2D<u64, PixelSpace>, width: u64) -> u64 {
xy.y * width + xy.x
}

impl Argmax2D {
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Argmax2D {
) * self.resolution as f32;
let chunk_span = (domain / self.chunk_size as f32)
.round_out()
.to_i64();
.cast::<u64>();

(chunk_span.min.y .. chunk_span.max.y)
.into_par_iter()
Expand Down
6 changes: 3 additions & 3 deletions src/drawing/impl_draw_rgbaimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ fn rescale_texture(texture: &DynamicImage, size: Size2D<u32, PixelSpace>) -> Dyn
).resize_exact(size.width, size.height, FilterType::Triangle)
}

fn sdf_overlay_aa(sdf: f32, Δp: f32, col1: Rgba<u8>, mut col2: Rgba<u8>) -> Rgba<u8> {
fn sdf_overlay_aa(sdf: f32, Δp: f32, mut col1: Rgba<u8>, mut col2: Rgba<u8>) -> Rgba<u8> {
let Δf = (0.5 * Δp - sdf) // antialias
.clamp(0.0, Δp);
let alpha = Δf / Δp;
// overlay blending with premultiplied alpha
col2.0[3] = ((col2.0[3] as f32) * alpha) as u8;
col2.blend(&col1);
col2
col1.blend(&col2);
col1
}

/// Draw shapes, parallel.
Expand Down
33 changes: 28 additions & 5 deletions src/geometry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//! .
//!
//! The origin of coordinate system is in top-left corner. All shapes are represented in the
//! interval [-1, 1], and center in the origin.
//! The origin of coordinate system is in top-left corner. Most of shapes are represented in the
//! interval `[-1, 1]`, and center in the origin.

use {
std::ops::Mul,
euclid::{Point2D, Box2D, Vector2D as V2, Size2D, Rotation2D, Angle},
num_traits::NumCast,
crate::sdf::SDF
crate::sdf::{SDF, Union, Subtraction, Intersection, SmoothMin}
};

pub mod shapes;
pub use shapes::*;

/// Pixel coordinate system
/// Pixel coordinate basis
#[derive(Debug, Copy, Clone)]
pub struct PixelSpace;
/// Normalized coordinate system
/// Normalized coordinate basis
#[derive(Debug, Copy, Clone)]
pub struct WorldSpace;

Expand All @@ -29,12 +29,33 @@ pub trait Shape: SDF<f32> + BoundingBox<f32, WorldSpace> {
fn translate<T>(self, offset: V2<T, WorldSpace>) -> Translation<Self, T> where Self: Sized {
Translation { shape: self, offset }
}
/// Rotate around the center of shape's bounding box
fn rotate<T>(self, angle: Angle<T>) -> Rotation<Self, T> where Self: Sized {
Rotation { shape: self, angle }
}
/// Scale around the center of shape's bounding box
fn scale<T>(self, scale: V2<T, WorldSpace>) -> Scale<Self, T> where Self: Sized {
Scale { shape: self, scale }
}
/// Union of two SDFs.
fn union<U>(self, other: U) -> Union<Self, U> where Self: Sized {
Union { s1: self, s2: other }
}
/// Subtracion of two SDFs. Note that this operation is *not* commutative,
/// i.e. `Subtraction {a, b} =/= Subtraction {b, a}`.
fn subtraction<U>(self, other: U) -> Subtraction<Self, U> where Self: Sized {
Subtraction { s1: self, s2: other }
}
/// Intersection of two SDFs.
fn intersection<U>(self, other: U) -> Intersection<Self, U> where Self: Sized {
Intersection { s1: self, s2: other }
}
/// Takes the minimum of two SDFs, smoothing between them when they are close.
///
/// `k` controls the radius/distance of the smoothing. 32 is a good default value.
fn smooth_min<T, U>(self, other: U, k: T) -> SmoothMin<T, Self, U> where Self: Sized {
SmoothMin { s1: self, s2: other, k }
}
#[cfg(feature = "drawing")]
#[cfg_attr(doc, doc(cfg(feature = "drawing")))]
fn texture<T>(self, texture: T) -> crate::drawing::Texture<Self, T> where Self: Sized {
Expand All @@ -55,6 +76,7 @@ impl <S> BoundingBox<f32, WorldSpace> for Translation<S, f32>
}
}

/// Rotate around the center of shape's bounding box
#[derive(Debug, Copy, Clone)]
pub struct Rotation<S, T> {
pub shape: S,
Expand All @@ -72,6 +94,7 @@ impl <S> BoundingBox<f32, WorldSpace> for Rotation<S, f32>
}
}

/// Scale around the center of shape's bounding box
#[derive(Debug, Copy, Clone)]
pub struct Scale<S, T> {
pub shape: S,
Expand Down
Loading

0 comments on commit a6b3d47

Please sign in to comment.