Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,10 +1617,13 @@ impl Graphics {
h: f32,
start: f32,
stop: f32,
mode: &str,
mode: Option<&str>,
) -> PyResult<()> {
let mode = ArcMode::parse(mode)
.ok_or_else(|| PyValueError::new_err(format!("unknown arc mode: {mode:?}")))?;
let mode = match mode {
Some(mode) => ArcMode::parse(mode)
.ok_or_else(|| PyValueError::new_err(format!("unknown arc mode: {mode:?}")))?,
None => ArcMode::default(),
};
graphics_record_command(
self.entity,
DrawCommand::Arc {
Expand Down
4 changes: 2 additions & 2 deletions crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ pub mod processing_py {
}

#[pyfunction]
#[pyo3(pass_module, signature = (cx, cy, w, h, start, stop, mode=processing::prelude::constants::OPEN))]
#[pyo3(pass_module, signature = (cx, cy, w, h, start, stop, mode=None))]
fn arc(
module: &Bound<'_, PyModule>,
cx: f32,
Expand All @@ -2555,7 +2555,7 @@ pub mod processing_py {
h: f32,
start: f32,
stop: f32,
mode: &str,
mode: Option<&str>,
) -> PyResult<()> {
graphics!(module).arc(cx, cy, w, h, start, stop, mode)
}
Expand Down
14 changes: 8 additions & 6 deletions crates/processing_render/src/render/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,20 @@ impl StrokeJoinMode {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[repr(u8)]
pub enum ArcMode {
/// filled as a pie, stroked along the arc only: `arc()` with no mode
#[default]
Open = 0,
Chord = 1,
Pie = 2,
Default = 0,
Open = 1,
Chord = 2,
Pie = 3,
}

impl From<u8> for ArcMode {
fn from(v: u8) -> Self {
match v {
0 => Self::Open,
1 => Self::Chord,
2 => Self::Pie,
1 => Self::Open,
2 => Self::Chord,
3 => Self::Pie,
_ => Self::default(),
}
}
Expand Down
52 changes: 46 additions & 6 deletions crates/processing_render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ pub fn flush_draw_commands(
}
}
DrawCommand::Fill(color) => {
state.style.fill = Fill::Color(color);
state.style.fill = Fill::Color(in_range(color));
}
DrawCommand::FillBuffer(buf_entity) => {
state.style.fill = Fill::Buffer(buf_entity);
Expand All @@ -337,7 +337,7 @@ pub fn flush_draw_commands(
state.style.fill = Fill::None;
}
DrawCommand::StrokeColor(color) => {
state.style.stroke_color = Some(color);
state.style.stroke_color = Some(in_range(color));
}
DrawCommand::NoStroke => {
state.style.stroke_color = None;
Expand Down Expand Up @@ -681,6 +681,9 @@ pub fn flush_draw_commands(
stop,
mode,
} => {
let Some((start, stop)) = arc_angles(start, stop) else {
continue;
};
let (x, y, w, h) = apply_shape_mode(state.style.ellipse_mode, cx, cy, w, h);
let cx = x + w / 2.0;
let cy = y + h / 2.0;
Expand Down Expand Up @@ -868,7 +871,7 @@ pub fn flush_draw_commands(
}
}
DrawCommand::Tint(color) => {
state.style.tint_color = Some(color);
state.style.tint_color = Some(in_range(color));
}
DrawCommand::NoTint => {
state.style.tint_color = None;
Expand Down Expand Up @@ -978,9 +981,14 @@ pub fn flush_draw_commands(
DrawCommand::PushStyle => state.style.push(),
DrawCommand::PopStyle => state.style.pop(),
DrawCommand::Translate(v) => state.transform.apply(Affine3A::from_translation(v)),
DrawCommand::Rotate { angle, axis } => state
.transform
.apply(Affine3A::from_axis_angle(axis.normalize(), angle)),
DrawCommand::Rotate { angle, axis } => {
// a zero axis has no rotation
if let Some(axis) = axis.try_normalize() {
state
.transform
.apply(Affine3A::from_axis_angle(axis, angle));
}
}
DrawCommand::Scale(v) => state.transform.apply(Affine3A::from_scale(v)),
DrawCommand::ShearX { angle } => state.transform.apply(transform::shear_x(angle)),
DrawCommand::ShearY { angle } => state.transform.apply(transform::shear_y(angle)),
Expand Down Expand Up @@ -1511,6 +1519,38 @@ fn start_batch(
batch.current_mesh = Some(empty_mesh());
}

/// A color with its alpha in 0..=1 and no negative channel.
fn in_range(color: Color) -> Color {
let color = color.with_alpha(color.alpha().clamp(0.0, 1.0));
let linear = color.to_linear();
if linear.red < 0.0 || linear.green < 0.0 || linear.blue < 0.0 {
return LinearRgba::new(
linear.red.max(0.0),
linear.green.max(0.0),
linear.blue.max(0.0),
linear.alpha,
)
.into();
}
color
}

/// The angles an arc sweeps, from `start` forwards; `None` when it has no extent.
fn arc_angles(start: f32, stop: f32) -> Option<(f32, f32)> {
use std::f32::consts::TAU;
let sweep = stop - start;
if sweep == 0.0 || !sweep.is_finite() {
return None;
}
// a stop before the start wraps around; more than a turn is one turn
let sweep = if sweep >= TAU {
TAU
} else {
sweep.rem_euclid(TAU)
};
(sweep > 0.0).then_some((start, start + sweep))
}

fn apply_shape_mode(mode: ShapeMode, a: f32, b: f32, c: f32, d: f32) -> (f32, f32, f32, f32) {
match mode {
ShapeMode::Corner => (a, b, c, d),
Expand Down
4 changes: 2 additions & 2 deletions crates/processing_render/src/render/primitive/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn arc_path(
let first_point = Point::new(cx + rx * start.cos(), cy + ry * start.sin());

match mode {
ArcMode::Pie if for_fill => {
ArcMode::Pie | ArcMode::Default if for_fill => {
builder.begin(Point::new(cx, cy));
builder.line_to(first_point);
}
Expand All @@ -47,7 +47,7 @@ fn arc_path(
let should_close = match mode {
ArcMode::Pie => true,
ArcMode::Chord => true,
ArcMode::Open => for_fill,
ArcMode::Open | ArcMode::Default => for_fill,
};

builder.end(should_close);
Expand Down
2 changes: 1 addition & 1 deletion crates/processing_render/src/render/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Default for StrokeConfig {
fn default() -> Self {
Self {
line_cap: StrokeCapMode::Round,
line_join: StrokeJoinMode::Round,
line_join: StrokeJoinMode::Miter,
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/processing_render/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ pub fn rotate_axis(
let mut transform = transforms
.get_mut(entity)
.map_err(|_| ProcessingError::TransformNotFound)?;
transform.rotate(Quat::from_axis_angle(axis.normalize(), angle));
// a zero axis has no rotation
if let Some(axis) = axis.try_normalize() {
transform.rotate(Quat::from_axis_angle(axis, angle));
}
Ok(())
}

Expand Down
Loading