1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
use crate::{
geometry::{Point, PointExt},
primitives::{
common::LineSide,
line::{
bresenham::{self, Bresenham, BresenhamParameters, BresenhamPoint},
Line, StrokeOffset,
},
},
};
const HORIZONTAL_LINE: Line = Line::new(Point::zero(), Point::new(1, 0));
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub(in crate::primitives::line) enum ParallelLineType {
Normal,
Extra,
}
/// Iterator over the parallel lines used to draw a thick line.
///
/// Thick lines are drawn using multiple 1px wide lines, which are parallel to
/// the original primitive line. The lines returned by the iterator are alternating
/// between the left and right side of original line to keep the resulting thick
/// line symmetric.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub(in crate::primitives::line) struct ParallelsIterator {
/// Parameters used for moves along the parallel lines.
pub parallel_parameters: BresenhamParameters,
/// Parameters used for moves perpendicular to the parallel lines.
perpendicular_parameters: BresenhamParameters,
/// Accumulated thickness.
///
/// The thickness accumulator is increased each time a parallel line is returned.
thickness_accumulator: i32,
/// Thickness threshold.
///
/// The thickness threshold is compared with the thickness accumulator to stop the iterator once
/// the desired line thickness is reached.
thickness_threshold: i32,
/// Changes the sign of initial error variables.
///
/// To keep the parallel lines in phase the sign of the error variables needs to be flipped in
/// some quadrants.
flip: bool,
/// Starting point for parallels on the left side.
left: Bresenham,
/// Initial error for parallels on the left side.
///
/// The initial error for the parallels is used to keep adjacent parallels in phase and prevent
/// overlapping pixels.
left_error: i32,
/// Starting point for parallels on the right side.
right: Bresenham,
/// Initial error for parallels on the right side.
///
/// The initial error for the parallels is used to keep adjacent parallels in phase and prevent
/// overlapping pixels.
right_error: i32,
/// The next side which will be drawn.
next_side: LineSide,
// TODO: Add tests for stroke alignment when polygons/thick triangle support is added
/// Stroke offset.
stroke_offset: StrokeOffset,
}
impl ParallelsIterator {
/// Creates a new parallels iterator.
pub fn new(mut line: &Line, thickness: i32, stroke_offset: StrokeOffset) -> Self {
let start_point = line.start;
// The lines orientation is undefined if start and end point are equal.
// To provide valid parameters a horizontal line is used to determine the
// parameters instead of the original line.
if line.start == line.end {
line = &HORIZONTAL_LINE;
}
let parallel_parameters = BresenhamParameters::new(line);
let perpendicular_parameters = BresenhamParameters::new(&line.perpendicular());
// Thickness threshold, taking into account that fewer pixels are required to draw a
// diagonal line of the same perceived width.
let thickness_threshold = (thickness * 2).pow(2) * line.delta().length_squared();
let thickness_accumulator =
(parallel_parameters.error_step.minor + parallel_parameters.error_step.major) / 2;
// Determine if the signs in the error calculation should be flipped.
let flip = perpendicular_parameters.position_step.minor
== -parallel_parameters.position_step.major;
let next_side = match stroke_offset {
StrokeOffset::None => LineSide::Right,
StrokeOffset::Left => LineSide::Left,
StrokeOffset::Right => LineSide::Right,
};
let mut self_ = Self {
parallel_parameters,
perpendicular_parameters,
thickness_accumulator,
thickness_threshold,
flip,
left: Bresenham::new(start_point),
left_error: 0,
right: Bresenham::new(start_point),
right_error: 0,
next_side,
stroke_offset,
};
// Skip center line
self_.next_parallel(next_side.swap());
self_
}
/// Returns the next parallel on the given side.
fn next_parallel(&mut self, side: LineSide) -> (BresenhamPoint, i32) {
let (error, decrease_error) = match side {
LineSide::Left => (&mut self.left_error, self.flip),
LineSide::Right => (&mut self.right_error, !self.flip),
};
loop {
let point = match side {
LineSide::Left => self.left.next_all(&self.perpendicular_parameters),
LineSide::Right => self.right.previous_all(&self.perpendicular_parameters),
};
match point {
BresenhamPoint::Normal(_) => {
return (point, *error);
}
BresenhamPoint::Extra(_) => {
if decrease_error {
let error_before_decrease = *error;
if self.parallel_parameters.decrease_error(error) {
return (point, error_before_decrease);
}
} else if self.parallel_parameters.increase_error(error) {
return (point, *error);
};
}
}
}
}
}
impl Iterator for ParallelsIterator {
/// The bresenham state (`Bresenham`) and the line type.
type Item = (Bresenham, ParallelLineType);
fn next(&mut self) -> Option<Self::Item> {
if self.thickness_accumulator.pow(2) > self.thickness_threshold {
return None;
}
let (point, error) = self.next_parallel(self.next_side);
let ret = match point {
BresenhamPoint::Normal(point) => {
self.thickness_accumulator += self.perpendicular_parameters.error_step.minor;
// Normal lines are the same length as the original primitive line.
(
Bresenham::with_initial_error(point, error),
ParallelLineType::Normal,
)
}
BresenhamPoint::Extra(point) => {
self.thickness_accumulator += self.perpendicular_parameters.error_step.major;
// Extra lines are 1 pixel shorter than normal lines.
(
Bresenham::with_initial_error(point, error),
ParallelLineType::Extra,
)
}
};
if self.stroke_offset == StrokeOffset::None {
self.next_side = self.next_side.swap();
}
Some(ret)
}
}
/// Iterator over all pixels in the stroke of a thick line.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub struct ThickPoints {
parallel: Bresenham,
parallel_length: u32,
parallel_points_remaining: u32,
iter: ParallelsIterator,
}
impl ThickPoints {
/// Creates a new iterator over the points in the stroke of a thick line.
pub(in crate::primitives) fn new(line: &Line, thickness: i32) -> Self {
Self {
parallel: Bresenham::new(line.start),
parallel_length: bresenham::major_length(line),
parallel_points_remaining: 0,
iter: ParallelsIterator::new(line, thickness, StrokeOffset::None),
}
}
}
impl Iterator for ThickPoints {
type Item = Point;
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.parallel_points_remaining > 0 {
self.parallel_points_remaining -= 1;
return Some(self.parallel.next(&self.iter.parallel_parameters));
} else {
let (parallel, line_type) = self.iter.next()?;
self.parallel = parallel;
self.parallel_points_remaining = self.parallel_length;
// Reduce the length of extra lines by one pixel
if line_type == ParallelLineType::Extra {
self.parallel_points_remaining -= 1;
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{mock_display::MockDisplay, pixelcolor::Gray8};
/// Draws the output of `ParallelsIterator` to a `MockDisplay`.
///
/// Each parallel line is drawn using a different `Gray8` color, to allow testing
/// of the drawing order. Points that are drawn multiple times are marked using
/// `Gray8::new(0xFF)`.
fn draw_parallels(line: Line, count: u8) -> MockDisplay<Gray8> {
// The maximum number of lines is 0xE, because 0xF is used to mark overdraw
assert!(count < 0xF);
let mut parallels = ParallelsIterator::new(&line, 100, StrokeOffset::None);
let mut display = MockDisplay::new();
for line_number in 0..count {
let (mut parallel, line_type) = parallels.next().unwrap();
let mut length = bresenham::major_length(&line);
// Reduce the length of extra lines by one pixel
if line_type == ParallelLineType::Extra {
length -= 1;
}
for _ in 0..length {
let point = parallel.next(¶llels.parallel_parameters);
let color = if display.get_pixel(point).is_some() {
// mark overdraw with `F`
Gray8::new(0xFF)
} else {
Gray8::new(line_number * 0x11)
};
display.draw_pixel(point, color);
}
}
display
}
#[test]
fn equal_start_and_end() {
let line = Line::new(Point::new(3, 3), Point::new(3, 3));
let display = draw_parallels(line, 3);
display.assert_pattern(&[
" ", //
" ", //
" 1 ", //
" 0 ", //
" 2 ", //
]);
}
#[test]
fn horizontal_1() {
let line = Line::new(Point::new(1, 3), Point::new(4, 3));
let display = draw_parallels(line, 3);
display.assert_pattern(&[
" ", //
" ", //
" 1111 ", //
" 0000 ", //
" 2222 ", //
]);
}
#[test]
fn horizontal_2() {
let line = Line::new(Point::new(4, 3), Point::new(1, 3));
let display = draw_parallels(line, 3);
display.assert_pattern(&[
" ", //
" ", //
" 2222 ", //
" 0000 ", //
" 1111 ", //
]);
}
#[test]
fn vertical_1() {
let line = Line::new(Point::new(3, 3), Point::new(3, 0));
let display = draw_parallels(line, 3);
display.assert_pattern(&[
" 102 ", //
" 102 ", //
" 102 ", //
" 102 ", //
]);
}
#[test]
fn vertical_2() {
let line = Line::new(Point::new(3, 0), Point::new(3, 3));
let display = draw_parallels(line, 3);
display.assert_pattern(&[
" 201 ", //
" 201 ", //
" 201 ", //
" 201 ", //
]);
}
#[test]
fn line_45_1() {
let line = Line::new(Point::new(2, 4), Point::new(5, 1));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" 3 ", //
" 310 ", //
" 31024 ", //
" 31024 ", //
" 024 ", //
" 4 ", //
" ",
]);
}
#[test]
fn line_45_2() {
let line = Line::new(Point::new(5, 1), Point::new(2, 4));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" 4 ", //
" 420 ", //
" 42013 ", //
" 42013 ", //
" 013 ", //
" 3 ", //
" ",
]);
}
#[test]
fn line_45_3() {
let line = Line::new(Point::new(2, 2), Point::new(5, 5));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" ", //
" 3 ", //
" 013 ", //
" 42013 ", //
" 42013 ", //
" 420 ", //
" 4 ",
]);
}
#[test]
fn line_45_4() {
let line = Line::new(Point::new(5, 5), Point::new(2, 2));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" ", //
" 4 ", //
" 024 ", //
" 31024 ", //
" 31024 ", //
" 310 ", //
" 3 ",
]);
}
#[test]
fn line_1() {
let line = Line::new(Point::new(2, 2), Point::new(5, 4));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" ", //
" 33 ", //
" 0113 ", //
" 420013 ", //
" 4220 ", //
" 44 ", //
" ",
]);
}
#[test]
fn line_2() {
let line = Line::new(Point::new(5, 4), Point::new(2, 2));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" ", //
" 44 ", //
" 0224 ", //
" 310024 ", //
" 3110 ", //
" 33 ", //
" ",
]);
}
#[test]
fn line_3() {
let line = Line::new(Point::new(2, 4), Point::new(5, 2));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" ", //
" 33 ", //
" 3110 ", //
" 310024 ", //
" 0224 ", //
" 44 ", //
" ",
]);
}
#[test]
fn line_4() {
let line = Line::new(Point::new(5, 2), Point::new(2, 4));
let display = draw_parallels(line, 5);
display.assert_pattern(&[
" ", //
" 44 ", //
" 4220 ", //
" 420013 ", //
" 0113 ", //
" 33 ", //
" ",
]);
}
#[test]
fn line_5() {
let line = Line::new(Point::new(3, 3), Point::new(5, 2));
let display = draw_parallels(line, 3);
display.assert_pattern(&[
" ", //
" 1 ", //
" 110 ", //
" 0022 ", //
" 2 ", //
" ", //
" ",
]);
}
}