dotmax 0.1.9

High-performance terminal braille rendering for images, animations, and graphics
Documentation
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
//! Benchmarks for drawing primitives (lines, circles, shapes).
//!
//! Performance targets (from Story 4.1 AC5 and Story 4.2 AC6):
//! - 1000-pixel line: <1ms
//! - Thick line (thickness=5, 1000px): <5ms
//! - Circle outline (radius 100): <2ms
//! - Filled circle (radius 100): <10ms
//! - Thick circle (thickness 5, radius 100): <10ms
//!
//! Run with: `cargo bench primitives`

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use dotmax::{
    primitives::{
        draw_circle, draw_circle_filled, draw_circle_thick, draw_line, draw_line_thick,
        draw_polygon, draw_polygon_filled, draw_rectangle, draw_rectangle_filled,
        draw_rectangle_thick,
    },
    BrailleGrid,
};
use std::hint::black_box;

/// Benchmark draw_line for various line lengths
fn bench_line_drawing(c: &mut Criterion) {
    let mut group = c.benchmark_group("draw_line");

    // Test different line lengths: small (10px), medium (100px), large (1000px)
    for length in &[10, 100, 1000] {
        group.bench_with_input(BenchmarkId::from_parameter(length), length, |b, &length| {
            // Grid large enough to hold the line
            let mut grid =
                BrailleGrid::new(((length / 2) + 10) as usize, ((length / 4) + 10) as usize)
                    .unwrap();

            b.iter(|| {
                // Draw diagonal line of given length
                draw_line(
                    black_box(&mut grid),
                    black_box(0),
                    black_box(0),
                    black_box(length),
                    black_box(length),
                )
                .unwrap();
            });
        });
    }

    group.finish();
}

/// Benchmark draw_line for different octants (angles)
fn bench_line_octants(c: &mut Criterion) {
    let mut group = c.benchmark_group("draw_line_octants");

    // Grid: 100×100 cells = 200×400 dots
    let mut grid = BrailleGrid::new(100, 100).unwrap();

    let test_cases = [
        ("horizontal", 0, 100, 199, 100),
        ("vertical", 100, 0, 100, 399),
        ("diagonal_45deg", 0, 0, 199, 199),
        ("steep_positive", 100, 0, 120, 199),
        ("shallow_positive", 0, 100, 199, 120),
    ];

    for (name, x0, y0, x1, y1) in &test_cases {
        group.bench_function(*name, |b| {
            b.iter(|| {
                draw_line(
                    black_box(&mut grid),
                    black_box(*x0),
                    black_box(*y0),
                    black_box(*x1),
                    black_box(*y1),
                )
                .unwrap();
            });
        });
    }

    group.finish();
}

/// Benchmark draw_line_thick for various thicknesses
fn bench_thick_line_drawing(c: &mut Criterion) {
    let mut group = c.benchmark_group("draw_line_thick");

    // Grid: 100×100 cells = 200×400 dots
    let mut grid = BrailleGrid::new(100, 100).unwrap();

    // Test different thicknesses on 1000px line
    for thickness in &[1, 3, 5, 7, 10] {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("thickness_{}", thickness)),
            thickness,
            |b, &thickness| {
                b.iter(|| {
                    draw_line_thick(
                        black_box(&mut grid),
                        black_box(0),
                        black_box(0),
                        black_box(199),
                        black_box(199),
                        black_box(thickness),
                    )
                    .unwrap();
                });
            },
        );
    }

    group.finish();
}

/// Benchmark realistic use case: drawing multiple lines (grid pattern)
fn bench_grid_pattern(c: &mut Criterion) {
    c.bench_function("grid_pattern_10x10", |b| {
        // Grid: 80×24 cells = 160×96 dots (standard terminal)
        let mut grid = BrailleGrid::new(80, 24).unwrap();

        b.iter(|| {
            // Draw 10 vertical lines
            for x in (0i32..=160).step_by(16) {
                draw_line(
                    black_box(&mut grid),
                    black_box(x),
                    black_box(0),
                    black_box(x),
                    black_box(95),
                )
                .unwrap();
            }

            // Draw 10 horizontal lines
            for y in (0i32..=96).step_by(9) {
                draw_line(
                    black_box(&mut grid),
                    black_box(0),
                    black_box(y),
                    black_box(159),
                    black_box(y),
                )
                .unwrap();
            }
        });
    });
}

/// Benchmark worst case: line with boundary clipping
fn bench_boundary_clipping(c: &mut Criterion) {
    c.bench_function("line_with_clipping", |b| {
        // Small grid: 20×10 cells = 40×40 dots
        let mut grid = BrailleGrid::new(20, 10).unwrap();

        b.iter(|| {
            // Line extends far beyond grid bounds (tests clipping performance)
            draw_line(
                black_box(&mut grid),
                black_box(-1000),
                black_box(-1000),
                black_box(1000),
                black_box(1000),
            )
            .unwrap();
        });
    });
}

/// Benchmark draw_circle for various radii
fn bench_circle_drawing(c: &mut Criterion) {
    let mut group = c.benchmark_group("draw_circle");

    // Test different radii: small (10), medium (50), large (100), very large (500)
    for radius in &[10, 50, 100, 500] {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("radius_{}", radius)),
            radius,
            |b, &radius| {
                // Grid large enough to hold the circle
                let grid_size = ((radius * 2 + 20) / 2) as usize;
                let mut grid = BrailleGrid::new(grid_size, (grid_size * 2) / 4).unwrap();
                let center_x = (grid_size) as i32;
                let center_y = (grid_size * 2) as i32;

                b.iter(|| {
                    draw_circle(
                        black_box(&mut grid),
                        black_box(center_x),
                        black_box(center_y),
                        black_box(radius),
                    )
                    .unwrap();
                });
            },
        );
    }

    group.finish();
}

/// Benchmark draw_circle_filled for various radii
fn bench_circle_filled(c: &mut Criterion) {
    let mut group = c.benchmark_group("draw_circle_filled");

    // Test different radii: small (10), medium (50), large (100)
    for radius in &[10, 50, 100] {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("radius_{}", radius)),
            radius,
            |b, &radius| {
                // Grid large enough to hold the circle
                let grid_size = ((radius * 2 + 20) / 2) as usize;
                let mut grid = BrailleGrid::new(grid_size, (grid_size * 2) / 4).unwrap();
                let center_x = (grid_size) as i32;
                let center_y = (grid_size * 2) as i32;

                b.iter(|| {
                    draw_circle_filled(
                        black_box(&mut grid),
                        black_box(center_x),
                        black_box(center_y),
                        black_box(radius),
                    )
                    .unwrap();
                });
            },
        );
    }

    group.finish();
}

/// Benchmark draw_circle_thick for various thicknesses
fn bench_circle_thick(c: &mut Criterion) {
    let mut group = c.benchmark_group("draw_circle_thick");

    let radius = 100;
    // Test different thicknesses
    for thickness in &[1, 3, 5, 7, 10] {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("thickness_{}", thickness)),
            thickness,
            |b, &thickness| {
                // Grid large enough to hold the circle
                let grid_size = ((radius * 2 + 20) / 2) as usize;
                let mut grid = BrailleGrid::new(grid_size, (grid_size * 2) / 4).unwrap();
                let center_x = (grid_size) as i32;
                let center_y = (grid_size * 2) as i32;

                b.iter(|| {
                    draw_circle_thick(
                        black_box(&mut grid),
                        black_box(center_x),
                        black_box(center_y),
                        black_box(radius),
                        black_box(thickness),
                    )
                    .unwrap();
                });
            },
        );
    }

    group.finish();
}

/// Benchmark realistic use case: concentric circles pattern
fn bench_concentric_circles(c: &mut Criterion) {
    c.bench_function("concentric_circles_10", |b| {
        // Grid: 80×24 cells = 160×96 dots (standard terminal)
        let mut grid = BrailleGrid::new(80, 24).unwrap();

        b.iter(|| {
            // Draw 10 concentric circles with radii 5, 10, 15, ..., 50
            for i in 1..=10 {
                let radius = i * 5;
                draw_circle(
                    black_box(&mut grid),
                    black_box(80),
                    black_box(48),
                    black_box(radius),
                )
                .unwrap();
            }
        });
    });
}

/// Benchmark circle with boundary clipping
fn bench_circle_boundary_clipping(c: &mut Criterion) {
    c.bench_function("circle_with_clipping", |b| {
        // Small grid: 20×10 cells = 40×40 dots
        let mut grid = BrailleGrid::new(20, 10).unwrap();

        b.iter(|| {
            // Circle extends beyond grid bounds (tests clipping performance)
            draw_circle(
                black_box(&mut grid),
                black_box(-50),
                black_box(-50),
                black_box(200),
            )
            .unwrap();
        });
    });
}

/// Benchmark rectangle outline drawing
fn bench_rectangle_drawing(c: &mut Criterion) {
    c.bench_function("rectangle_outline_100x50", |b| {
        // Grid: 80×24 cells = 160×96 dots
        let mut grid = BrailleGrid::new(80, 24).unwrap();

        b.iter(|| {
            // Rectangle 100×50 dots (target: <1ms)
            draw_rectangle(
                black_box(&mut grid),
                black_box(30),
                black_box(23),
                black_box(100),
                black_box(50),
            )
            .unwrap();
        });
    });
}

/// Benchmark filled rectangle drawing
fn bench_rectangle_filled(c: &mut Criterion) {
    c.bench_function("rectangle_filled_100x50", |b| {
        // Grid: 80×24 cells = 160×96 dots
        let mut grid = BrailleGrid::new(80, 24).unwrap();

        b.iter(|| {
            // Filled rectangle 100×50 dots (target: <5ms)
            draw_rectangle_filled(
                black_box(&mut grid),
                black_box(30),
                black_box(23),
                black_box(100),
                black_box(50),
            )
            .unwrap();
        });
    });
}

/// Benchmark thick rectangle drawing
fn bench_rectangle_thick(c: &mut Criterion) {
    c.bench_function("rectangle_thick_thickness5_100x50", |b| {
        // Grid: 80×24 cells = 160×96 dots
        let mut grid = BrailleGrid::new(80, 24).unwrap();

        b.iter(|| {
            // Thick rectangle thickness=5, 100×50 dots (target: <5ms)
            draw_rectangle_thick(
                black_box(&mut grid),
                black_box(30),
                black_box(23),
                black_box(100),
                black_box(50),
                black_box(5),
            )
            .unwrap();
        });
    });
}

/// Benchmark polygon outline drawing
fn bench_polygon_drawing(c: &mut Criterion) {
    c.bench_function("polygon_outline_10_vertices", |b| {
        // Grid: 80×24 cells = 160×96 dots
        let mut grid = BrailleGrid::new(80, 24).unwrap();

        // 10-vertex polygon
        let vertices: Vec<(i32, i32)> = vec![
            (50, 20),
            (60, 25),
            (70, 30),
            (75, 40),
            (70, 50),
            (60, 55),
            (50, 60),
            (40, 55),
            (30, 50),
            (30, 40),
        ];

        b.iter(|| {
            // Polygon with 10 vertices (target: <2ms)
            draw_polygon(black_box(&mut grid), black_box(&vertices)).unwrap();
        });
    });
}

/// Benchmark filled polygon drawing
fn bench_polygon_filled(c: &mut Criterion) {
    c.bench_function("polygon_filled_10_vertices", |b| {
        // Grid: 80×24 cells = 160×96 dots
        let mut grid = BrailleGrid::new(80, 24).unwrap();

        // 10-vertex polygon
        let vertices: Vec<(i32, i32)> = vec![
            (50, 20),
            (60, 25),
            (70, 30),
            (75, 40),
            (70, 50),
            (60, 55),
            (50, 60),
            (40, 55),
            (30, 50),
            (30, 40),
        ];

        b.iter(|| {
            // Filled polygon with 10 vertices (target: <10ms)
            draw_polygon_filled(black_box(&mut grid), black_box(&vertices)).unwrap();
        });
    });
}

criterion_group!(
    benches,
    bench_line_drawing,
    bench_line_octants,
    bench_thick_line_drawing,
    bench_grid_pattern,
    bench_boundary_clipping,
    bench_circle_drawing,
    bench_circle_filled,
    bench_circle_thick,
    bench_concentric_circles,
    bench_circle_boundary_clipping,
    bench_rectangle_drawing,
    bench_rectangle_filled,
    bench_rectangle_thick,
    bench_polygon_drawing,
    bench_polygon_filled
);

criterion_main!(benches);