Skip to content

Commit

Permalink
[agx] Line allows iterating rendered points without rendering them
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Feb 18, 2023
1 parent 8d68bb3 commit de7d242
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 51 deletions.
2 changes: 2 additions & 0 deletions kernel/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void smp_init(void) {
if (processor_info->apic_id == smp_get_current_core_apic_id(smp_info)) {
continue;
}
// TODO(PT): Read a config/max_cpus.txt to decide when to stop booting APs
break;

printf("Booting core [idx %d], [APIC %d] [ID %d]\n", i, processor_info->apic_id, processor_info->processor_id);

Expand Down
16 changes: 12 additions & 4 deletions rust_programs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust_programs/agx_definitions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
axle_rt = { path = "../axle_rt" }
itertools = { version = "0.10.5", default-features = false }
bresenham = { git = "https://github.com/indubitablement2/bresenham-rs" }

# PT: For f64.sqrt() in no_std contexts
[dependencies.num-traits]
Expand Down
90 changes: 43 additions & 47 deletions rust_programs/agx_definitions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use alloc::vec::Vec;

#[cfg(target_os = "axle")]
use axle_rt::println;
use bresenham::{Bresenham, BresenhamInclusive};
#[cfg(not(target_os = "axle"))]
use std::println;

Expand Down Expand Up @@ -267,6 +268,12 @@ impl PointU32 {
}
}

impl Display for SizeF64 {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "({:.02}, {:.02})", self.width, self.height)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Ord, PartialOrd, Eq)]
pub struct Point {
pub x: isize,
Expand Down Expand Up @@ -357,8 +364,8 @@ impl From<PointU32> for Point {

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct PointF64 {
x: f64,
y: f64,
pub x: f64,
pub y: f64,
}

impl PointF64 {
Expand Down Expand Up @@ -401,7 +408,7 @@ impl Add for PointF64 {

impl Display for PointF64 {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "({:02}, {:02})", self.x, self.y)
write!(f, "({:6.02}, {:6.02})", self.x, self.y)
}
}

Expand Down Expand Up @@ -827,6 +834,12 @@ impl RectF64 {
}
}

impl Display for RectF64 {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "({}, {})", self.origin, self.size)
}
}

#[derive(PartialEq)]
struct TileSegment<'a> {
viewport_frame: Rect,
Expand Down Expand Up @@ -1510,46 +1523,31 @@ impl LineF64 {
))
}

fn draw(&self, onto: &mut Box<dyn LikeLayerSlice>, color: Color) {
// Relative distances in both directions
let mut delta_x = self.p2.x - self.p1.x;
let mut delta_y = self.p2.y - self.p1.y;

// Increment of 0 would imply either vertical or horizontal line
let inc_x = match delta_x {
_ if delta_x > 0.0 => 1.0,
_ if delta_x == 0.0 => 0.0,
_ => -1.0,
};
let inc_y = match delta_y {
_ if delta_y > 0.0 => 1.0,
_ if delta_y == 0.0 => 0.0,
_ => -1.0,
};

//let distance = max(delta_x.abs(), delta_y.abs());
delta_x = delta_x.abs();
delta_y = delta_y.abs();
let distance = delta_x.max(delta_y);

let mut cursor = PointF64::from(onto.frame().origin) + PointF64::new(self.p1.x, self.p1.y);
let mut x_err = 0.0;
let mut y_err = 0.0;
for _ in 0..((distance + 1.0) as isize) {
onto.putpixel(Point::from(cursor), color);
fn as_inclusive_bresenham_iterator(&self) -> BresenhamInclusive {
BresenhamInclusive::new(
(self.p1.x.round() as isize, self.p1.y.round() as isize),
(self.p2.x.round() as isize, self.p2.y.round() as isize),
)
}

x_err += delta_x;
y_err += delta_y;
pub fn draw(&self, onto: &mut Box<dyn LikeLayerSlice>, color: Color) {
for (x, y) in self.as_inclusive_bresenham_iterator() {
onto.putpixel(Point::new(x, y), color);
}
}

if x_err > distance {
x_err -= distance;
cursor.x += inc_x;
}
if y_err > distance {
y_err -= distance;
cursor.y += inc_y;
}
pub fn compute_rendered_pixels(&self) -> Vec<PointF64> {
let mut px_locations = vec![];
for (x, y) in self.as_inclusive_bresenham_iterator() {
px_locations.push(PointF64::new(x as _, y as _));
}
px_locations
}
}

impl Display for LineF64 {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "[{} - {}]", self.p1, self.p2)
}
}

Expand All @@ -1569,7 +1567,7 @@ impl Polygon {
let scaled_points: Vec<PointF64> = self
.points
.iter()
.map(|&p| PointF64::new(p.x as f64 * scale_x, p.y as f64 * scale_y))
.map(|&p| PointF64::new(p.x * scale_x, p.y * scale_y))
.collect();
Polygon::new(&scaled_points)
}
Expand All @@ -1596,8 +1594,7 @@ impl Polygon {
pub fn draw_outline(&self, onto: &mut Box<dyn LikeLayerSlice>, color: Color) {
let lines = self.lines();
for line in lines.iter() {
let line: Line = (*line).into();
line.draw(onto, color, StrokeThickness::Filled);
line.draw(onto, color);
}
}

Expand All @@ -1608,7 +1605,7 @@ impl Polygon {

#[derive(Debug, Clone)]
pub struct PolygonStack {
polygons: Vec<Polygon>,
pub polygons: Vec<Polygon>,
}

impl PolygonStack {
Expand All @@ -1630,7 +1627,7 @@ impl PolygonStack {
bounding_box
}

fn lines(&self) -> Vec<LineF64> {
pub fn lines(&self) -> Vec<LineF64> {
let mut lines = vec![];
for p in self.polygons.iter() {
let mut p_lines = p.lines();
Expand All @@ -1646,8 +1643,7 @@ impl PolygonStack {
pub fn draw_outline(&self, onto: &mut Box<dyn LikeLayerSlice>, color: Color) {
let lines = self.lines();
for line in lines.iter() {
let line: Line = (*line).into();
line.draw(onto, color, StrokeThickness::Filled);
line.draw(onto, color);
}
}
}
Expand Down

0 comments on commit de7d242

Please sign in to comment.