strandify 0.4.3

A string art generation library.
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
444
445
446
447
448
449
450
451
452
453
454
use log::warn;
use log::{debug, info};
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;

use image::GrayImage;
#[cfg(feature = "parallel")]
use indicatif::ParallelProgressIterator;
#[cfg(not(feature = "parallel"))]
use indicatif::ProgressIterator;
use itertools::Itertools;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
#[cfg(feature = "parallel")]
use rayon::ThreadPoolBuilder;

use crate::blueprint::Blueprint;
use crate::line::Line;
use crate::peg::{Peg, Yarn};
use crate::utils;

#[derive(Debug, Clone)]
/// Pathing algorithm early stopping configuration.
pub struct EarlyStopConfig {
    pub loss_threshold: Option<f64>,
    pub max_count: u32,
}

impl Default for EarlyStopConfig {
    fn default() -> Self {
        Self {
            loss_threshold: None,
            max_count: 100,
        }
    }
}

#[derive(Debug, Clone)]
/// Pathing algorithm configuration.
pub struct PatherConfig {
    /// Number of [`Peg`] connections.
    pub iterations: usize,
    /// The [`Yarn`] to use when computing the path. When using a high [`Yarn::opacity`] the lines
    /// will overlap less.
    pub yarn: Yarn,
    /// [`EarlyStopConfig`].
    pub early_stop: EarlyStopConfig,
    /// Radius around [`Pegs`](Peg), in pixels, to use to determine the starting [`Peg`].
    pub start_peg_radius: u32,
    /// Don't connect [`Pegs`](Peg) within distance, in pixels.
    pub skip_peg_within: u32,
    /// Beam search width, larger values will be lead to more accurate paths at the expense of
    /// compute time.
    pub beam_width: usize,
    /// Display progress bar.
    pub progress_bar: bool,
}

impl PatherConfig {
    /// Creates a new [`PatherConfig`].
    pub fn new(
        iterations: usize,
        yarn: Yarn,
        early_stop: EarlyStopConfig,
        start_peg_radius: u32,
        skip_peg_within: u32,
        beam_width: usize,
        progress_bar: bool,
    ) -> Self {
        Self {
            iterations,
            yarn,
            early_stop,
            start_peg_radius,
            skip_peg_within,
            progress_bar,
            beam_width,
        }
    }
}

impl Default for PatherConfig {
    fn default() -> Self {
        Self {
            iterations: 4000,
            yarn: Yarn::default(),
            early_stop: EarlyStopConfig::default(),
            start_peg_radius: 5,
            skip_peg_within: 0,
            progress_bar: false,
            beam_width: 1,
        }
    }
}

#[derive(Debug, Clone)]
struct BeamState {
    peg_order: Vec<usize>,
    loss: f64,
    image: image::ImageBuffer<image::Luma<u8>, Vec<u8>>,
}

impl Eq for BeamState {}

impl PartialEq for BeamState {
    fn eq(&self, other: &Self) -> bool {
        self.loss == other.loss
    }
}

impl PartialOrd for BeamState {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for BeamState {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // We want the lowest loss to be considered "greater" for purposes of sorting
        // so that the smallest loss comes first.
        self.loss
            .partial_cmp(&other.loss)
            .unwrap_or(std::cmp::Ordering::Equal)
    }
}

#[derive(Debug)]
/// The line pathing algorithm.
pub struct Pather {
    /// Input grayscale image.
    pub image: GrayImage,
    /// Array of [`Pegs`](Peg) to use to compute the path.
    pub pegs: Vec<Peg>,
    /// Pathing algorithm configuration.
    pub config: PatherConfig,
    /// Holds the pixel coords of all the lines, run [Pather::populate_line_cache] to populate the
    /// cache.
    pub line_cache: HashMap<(usize, usize), Line>,
}

impl Pather {
    /// Creates a new [`Pather`].
    pub fn new(img: GrayImage, pegs: Vec<Peg>, config: PatherConfig) -> Self {
        let line_cache = HashMap::new();
        Self {
            image: img,
            pegs,
            config,
            line_cache,
        }
    }

    /// Creates a nes [`Pather`] from an image file.
    ///
    /// # Errors
    ///
    /// This function will return an error if [`image::open`] fails to open the image file.
    pub fn from_image_file(
        image_path: PathBuf,
        pegs: Vec<Peg>,
        config: PatherConfig,
    ) -> Result<Self, Box<dyn Error>> {
        let img = image::open(image_path)?.into_luma8();
        Ok(Self::new(img, pegs, config))
    }

    /// Populate the [Pather::line_cache] with the pixel coords of all the lines between the [`Peg`] pairs.
    pub fn populate_line_cache(&mut self) -> Result<(), Box<dyn Error>> {
        info!("Populating line cache");

        let peg_combinations = self
            .pegs
            .iter()
            .tuple_combinations()
            .filter(|(peg_a, peg_b)| peg_a.dist_to(peg_b) >= self.config.skip_peg_within)
            .collect_vec();

        let pbar = utils::pbar(peg_combinations.len() as u64, !self.config.progress_bar)?
            .with_message("Populating line cache");

        let key_line_pixels = utils::iter_or_par_iter!(peg_combinations)
            .progress_with(pbar)
            .map(|(peg_a, peg_b)| {
                (
                    utils::hash_key(peg_a, peg_b),
                    peg_a.line_to(
                        peg_b,
                        self.config.yarn.width.round() as u32,
                        Some((0, self.image.width() - 1, 0, self.image.height() - 1)),
                    ),
                )
            })
            .collect::<Vec<((usize, usize), Line)>>();

        for (key, line) in key_line_pixels {
            self.line_cache.insert(key, line);
        }
        debug!("# line cache entries: {}", self.line_cache.len());
        Ok(())
    }

    /// Get starting peg by taking the [`Peg`] located on the darkest pixel.
    fn get_start_peg(&self, radius: u32) -> usize {
        let peg_avgs: Vec<u32> = self
            .pegs
            .iter()
            .map(|peg| {
                // get the average pixel value around peg
                let (x_coords, y_coords) = peg.around(radius);
                let pixels: Vec<u8> = x_coords
                    .into_iter()
                    .zip(y_coords)
                    .map(|(x, y)| match self.image.get_pixel_checked(x, y) {
                        Some(pixel) => pixel[0],
                        None => 0,
                    })
                    .collect();
                pixels.iter().fold(0, |acc, pixel| acc + *pixel as u32) / pixels.len() as u32
            })
            .collect();
        debug!("peg_avgs: {peg_avgs:?}");

        peg_avgs.iter().position_min().unwrap_or(0)
    }

    fn early_stop(&self, count: &mut u32, loss: f64) -> bool {
        match self.config.early_stop.loss_threshold {
            Some(early_stop_count) => {
                if loss > early_stop_count {
                    *count += 1;
                    debug!(
                        "Early stop count to {}/{}",
                        count, self.config.early_stop.max_count
                    );
                    *count >= self.config.early_stop.max_count
                } else {
                    *count = 0;
                    false
                }
            }
            None => false,
        }
    }

    /// Run a greedy line pathing algorithm and construct a [`Blueprint`].
    pub fn compute_greedy(&self) -> Result<Blueprint, Box<dyn Error>> {
        if self.line_cache.is_empty() {
            return Err("Line cache is empty, run 'populate_line_cache'.".into());
        };

        let pbar = utils::pbar(self.config.iterations as u64, !self.config.progress_bar)?
            .with_message("Computing blueprint");

        let line_color = 255. * self.config.yarn.opacity;

        let compute = || {
            let start_peg = &self.pegs[self.get_start_peg(self.config.start_peg_radius)];
            debug!("Starting peg: {start_peg:?}");
            let mut peg_order = vec![start_peg];
            let mut work_img = self.image.clone();
            let mut last_peg = start_peg;
            let mut last_last_peg = last_peg;
            let mut early_stop_count: u32 = 0;

            'iter: for iter_i in pbar.wrap_iter(0..self.config.iterations) {
                let (min_loss, min_peg, min_line) = utils::iter_or_par_iter!(self.pegs)
                    .filter(|peg| peg.id != last_peg.id && peg.id != last_last_peg.id)
                    .filter_map(|peg| {
                        let line = self.line_cache.get(&utils::hash_key(last_peg, peg))?;
                        let loss = line.loss(&work_img);
                        Some((loss, peg, line))
                    })
                    .min_by(|(loss1, _, _), (loss2, _, _)| {
                        loss1
                            .partial_cmp(loss2)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .unwrap();
                if self.early_stop(&mut early_stop_count, min_loss) {
                    info!("Early stopping at iteration {iter_i}");
                    break 'iter;
                }

                debug!("line {:?} -> {:?}: {min_loss:?}", last_peg.id, min_peg.id);
                peg_order.push(min_peg);
                last_last_peg = last_peg;
                last_peg = min_peg;

                min_line.draw(&mut work_img, self.config.yarn.opacity, line_color);
            }
            peg_order
        };

        let order;
        #[cfg(feature = "parallel")]
        {
            let pool = ThreadPoolBuilder::new().build()?;
            order = pool.install(compute);
        }

        #[cfg(not(feature = "parallel"))]
        {
            order = compute();
        }

        Ok(Blueprint::from_refs(
            order,
            self.image.width(),
            self.image.height(),
            Some((255, 255, 255)),
            1.,
            self.config.progress_bar,
        ))
    }

    /// Run a beam search based line pathing algorithm and construct a [`Blueprint`].
    pub fn compute_beam(&self) -> Result<Blueprint, Box<dyn Error>> {
        if self.line_cache.is_empty() {
            return Err("Line cache is empty, run 'populate_line_cache'.".into());
        };

        let start_peg = self.get_start_peg(self.config.start_peg_radius);
        debug!("Starting peg: {start_peg:?}");

        let pbar = utils::pbar(self.config.iterations as u64, !self.config.progress_bar)?
            .with_message("Computing blueprint");

        let line_color = 255. * self.config.yarn.opacity;

        let mut beam = vec![BeamState {
            peg_order: vec![start_peg],
            loss: 0.,
            image: self.image.clone(),
        }];

        // use a ThreadPool to reduce overhead
        let beam_width_index = self.config.beam_width - 1;

        let compute = |beam: &mut Vec<BeamState>| {
            let mut early_stop_count = 0;

            'iter: for iter_i in pbar.wrap_iter(0..self.config.iterations) {
                let mut candidates: Vec<_> = utils::iter_or_par_iter!(beam)
                    .flat_map(|beam_state| {
                        let last_peg = &self.pegs[*beam_state.peg_order.last().unwrap()];
                        let last_last_peg = &self.pegs[*beam_state
                            .peg_order
                            .get(beam_state.peg_order.len().saturating_sub(2))
                            .unwrap_or(beam_state.peg_order.last().unwrap())];

                        utils::iter_or_par_iter!(self.pegs)
                            .enumerate()
                            .filter(|(_, peg)| peg.id != last_peg.id && peg.id != last_last_peg.id)
                            .filter_map(|(i, peg)| {
                                let line = self.line_cache.get(&utils::hash_key(last_peg, peg))?;
                                let loss = line.loss(&beam_state.image);
                                Some((loss, i, line, beam_state))
                            })
                            .collect::<Vec<_>>()
                    })
                    .collect::<Vec<_>>();

                // partial sort up to beam width
                candidates.select_nth_unstable_by(beam_width_index, |(loss1, ..), (loss2, ..)| {
                    loss1
                        .partial_cmp(loss2)
                        .unwrap_or(std::cmp::Ordering::Equal)
                });

                let min_loss = candidates
                    .iter()
                    .take(self.config.beam_width)
                    .min_by(|(loss1, ..), (loss2, ..)| {
                        loss1
                            .partial_cmp(loss2)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .unwrap()
                    .0;

                if self.early_stop(&mut early_stop_count, min_loss) {
                    info!("Early stopping at iteration {iter_i}");
                    break 'iter;
                }

                *beam = candidates
                    .into_iter()
                    .take(self.config.beam_width)
                    .map(|(loss, peg_i, line, beam_state)| {
                        let mut new_img = beam_state.image.clone();
                        line.draw(&mut new_img, self.config.yarn.opacity, line_color);

                        BeamState {
                            peg_order: beam_state
                                .peg_order
                                .iter()
                                .copied()
                                .chain(std::iter::once(peg_i))
                                .collect(),
                            loss: beam_state.loss + loss,
                            image: new_img,
                        }
                    })
                    .collect();
            }
        };

        #[cfg(feature = "parallel")]
        {
            let pool = ThreadPoolBuilder::new().build()?;
            pool.install(|| compute(&mut beam));
        }

        #[cfg(not(feature = "parallel"))]
        {
            compute(&mut beam);
        }

        let best_state = beam.into_iter().min().ok_or("Beam search failed.")?;

        Ok(Blueprint::new(
            best_state
                .peg_order
                .iter()
                .map(|&index| self.pegs[index])
                .collect(),
            self.image.width(),
            self.image.height(),
            Some((255, 255, 255)),
            1.,
            self.config.progress_bar,
        ))
    }

    /// Run the pathing algorithm. Will use the [greedy](Pather::compute_greedy) algorithm when
    /// [`PatherConfig::beam_width`] equals 1 and the [beam search](Pather::compute_beam) algorithm
    /// otherwise.
    ///
    /// If [`Pather::line_cache`] is empty, will [populate](Pather::populate_line_cache) it.
    pub fn compute(&mut self) -> Result<Blueprint, Box<dyn Error>> {
        if self.line_cache.is_empty() {
            warn!("Line cache is empty, populating it.");
            self.populate_line_cache()?;
        }
        if self.config.beam_width > 1 {
            info!("Using beam search algorithm.");
            self.compute_beam()
        } else {
            info!("Using greedy algorithm.");
            self.compute_greedy()
        }
    }
}