Skip to content

Commit 016668a

Browse files
committed
Find input sequence files using WalkDir and regex
1 parent 21a36c7 commit 016668a

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

splashsurf/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ chrono = "0.4.20"
2121
anyhow = "1.0"
2222
rayon = "1.7"
2323
bytemuck = "1.9"
24+
regex = "1"
25+
walkdir = "2"

splashsurf/src/reconstruction.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ mod arguments {
270270
use std::convert::TryFrom;
271271
use std::fs;
272272
use std::path::{Path, PathBuf};
273+
use walkdir::WalkDir;
274+
use regex::{Regex, escape};
273275

274276
/// All arguments that can be supplied to the surface reconstruction tool converted to useful types
275277
pub struct ReconstructionRunnerArgs {
@@ -465,17 +467,34 @@ mod arguments {
465467
let input_dir = input_file.parent().unwrap();
466468
let output_dir = output_file.parent().unwrap();
467469

468-
let input_filename = input_file.file_name().unwrap().to_string_lossy();
469-
let output_filename = output_file.file_name().unwrap().to_string_lossy();
470+
let input_pattern = input_file.file_name().unwrap().to_string_lossy();
471+
let output_pattern = output_file.file_name().unwrap().to_string_lossy();
472+
473+
let (input_prefix, input_suffix) = input_pattern
474+
.split_once("{}")
475+
.expect("sequence input filename has to include pattern");
476+
477+
let input_re_str = format!(r"{}(\d+){}", escape(input_prefix), escape(input_suffix));
478+
let input_re = Regex::new(&input_re_str).expect("expected a valid regex");
470479

471480
let mut paths = Vec::new();
472-
let mut i: usize = 1;
473-
loop {
474-
let input_filename_i = input_filename.replace("{}", &i.to_string());
475-
let input_file_i = input_dir.join(input_filename_i);
476481

477-
if input_file_i.is_file() {
478-
let output_filename_i = output_filename.replace("{}", &i.to_string());
482+
for entry in WalkDir::new(input_dir)
483+
.max_depth(1)
484+
.contents_first(true)
485+
.sort_by_file_name()
486+
.into_iter()
487+
.filter_map(|e| e.ok())
488+
.filter(|e| e.file_type().is_file())
489+
{
490+
let entry_name = entry.file_name().to_string_lossy();
491+
if input_re.is_match(&entry_name) {
492+
let index = &input_re.captures(&entry_name).expect("there should be a match")[1];
493+
494+
let input_filename_i = entry_name.as_ref();
495+
let input_file_i = input_dir.join(input_filename_i);
496+
497+
let output_filename_i = output_pattern.replace("{}", index);
479498
let output_file_i = output_dir.join(output_filename_i);
480499

481500
paths.push(ReconstructionRunnerPaths::new(
@@ -489,13 +508,14 @@ mod arguments {
489508
self.sph_normals,
490509
self.attributes.clone(),
491510
));
492-
} else {
493-
break;
494511
}
495-
496-
i += 1;
497512
}
498513

514+
info!(
515+
"Found {} input files matching the pattern \"{}\"",
516+
paths.len(),
517+
input_re_str
518+
);
499519
paths
500520
} else {
501521
vec![
@@ -586,7 +606,10 @@ mod arguments {
586606
} else {
587607
let input_stem = input_pattern.file_stem().unwrap().to_string_lossy();
588608
// Use VTK format as default fallback
589-
format!("{}.vtk", input_stem.replace("{}", &format!("{}_{{}}", output_suffix)))
609+
format!(
610+
"{}.vtk",
611+
input_stem.replace("{}", &format!("{}_{{}}", output_suffix))
612+
)
590613
};
591614

592615
Self::try_new(

0 commit comments

Comments
 (0)