From 284b0d815b69178cf4eb67321b7cc6c03202553c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:15:02 +0000 Subject: [PATCH 1/3] Expose compatible OBJ read options --- crates/mesh/src/obj.rs | 28 +++++++++++++++++++++++++--- crates/mesh/src/tests/obj.rs | 12 ++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index d1354a66d..0a4cc3b68 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -167,8 +167,20 @@ struct Object { slots: HashMap<(String, Option, u32), usize>, } +/// Options for callers that need strict parsing or original face-run order. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct ReadOptions { + /// Return an error when a vertex or face line cannot be read. + pub strict: bool, + /// Keep separate runs when groups, materials or smoothing change. + pub preserve_runs: bool, + /// Do not resolve or report material libraries; `usemtl` names remain available. + pub ignore_material_libraries: bool, +} + struct Reader<'a> { limits: &'a Limits, + options: ReadOptions, v: Vec<[f64; 3]>, colors: Vec<[f32; 4]>, vt: Vec<[f32; 2]>, @@ -314,7 +326,7 @@ impl Reader<'_> { let Some(object) = self.objects.last_mut() else { return Ok(None) }; let slot = match self.slot { Some(s) => s, - None => match object.slots.get(&(self.group.clone(), self.material, self.smoothing)) { + None => match (!self.options.preserve_runs).then(|| object.slots.get(&(self.group.clone(), self.material, self.smoothing))).flatten() { Some(s) => *s, None => { if self.parts >= self.limits.max_parts { @@ -400,7 +412,7 @@ impl Reader<'_> { self.group = after.to_string(); } "usemtl" => self.use_material(after)?, - "mtllib" => self.library(after, resolve)?, + "mtllib" if !self.options.ignore_material_libraries => self.library(after, resolve)?, "f" => self.face(it)?, _ => {} } @@ -456,6 +468,11 @@ impl Reader<'_> { /// writes; every name asked, and every image a library names, is listed in /// [`Scene::external`]. pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { + read_with_options(bytes, limits, resolve, &ReadOptions::default()) +} + +/// Read an OBJ using the selected compatibility behavior. +pub fn read_with_options(bytes: &[u8], limits: &Limits, resolve: Resolver, options: &ReadOptions) -> Result { // Lossy UTF-8 conversion can allocate, so bound it before copying invalid input. if std::str::from_utf8(bytes).is_err() && bytes.len() > limits.max_data_bytes { return Err(too_large(KIND, "invalid UTF-8 text exceeds the memory limit")); @@ -463,6 +480,7 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { let text = String::from_utf8_lossy(bytes); let mut r = Reader { limits, + options: *options, v: vec![], colors: vec![], vt: vec![], @@ -482,7 +500,8 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { }; // A line ending in `\` continues on the next. let mut joined = String::new(); - for raw in text.lines() { + for (i, raw) in text.lines().enumerate() { + let raw = if i == 0 { raw.trim_start_matches('\u{feff}') } else { raw }; let line = raw.split('#').next().unwrap_or(""); if line.len() > limits.max_data_bytes || joined.len().checked_add(line.len()).is_none_or(|n| n > limits.max_data_bytes) { return Err(too_large(KIND, "a continued line exceeds the memory limit")); @@ -501,6 +520,9 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { } } r.line(joined.trim(), resolve)?; + if options.strict && r.bad_lines > 0 { + return Err(malformed(KIND, "invalid vertex or face line")); + } r.finish() } diff --git a/crates/mesh/src/tests/obj.rs b/crates/mesh/src/tests/obj.rs index 5283012c4..583fcb4a0 100644 --- a/crates/mesh/src/tests/obj.rs +++ b/crates/mesh/src/tests/obj.rs @@ -352,3 +352,15 @@ fn face_tokens_and_continued_lines_are_bounded_before_collection() { let invalid = b"v 0 0 0\nf 1 1 1\n\xff"; assert!(matches!(read(invalid, &Limits { max_data_bytes: 4, ..Limits::default() }, &mut no_external), Err(Error::TooLarge(_)))); } + +#[test] +fn compatibility_options_keep_runs_and_skip_material_libraries() { + let input = b"\xef\xbb\xbf v 0 0 0\nv 1 0 0\nv 0 1 0\nmtllib missing.mtl\nusemtl Red\nf 1 2 3\nusemtl Blue\nf 1 2 3\nusemtl Red\nf 1 2 3\n"; + let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true }; + let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); + let parts = &scene.meshes[0].parts; + assert_eq!(parts.len(), 3); + assert_eq!(parts[0].material, parts[2].material); + assert!(scene.warnings.is_empty()); + assert!(crate::obj::read_with_options(b"v NaN 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n", &Limits::default(), &mut no_external, &options).is_err()); +} From 2ee29d87b59f13de6a629df25b1151922303869a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:18:54 +0000 Subject: [PATCH 2/3] Preserve repeated OBJ group declarations --- crates/mesh/src/obj.rs | 6 ++++++ crates/mesh/src/tests/obj.rs | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index 0a4cc3b68..b59f903d5 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -176,6 +176,8 @@ pub struct ReadOptions { pub preserve_runs: bool, /// Do not resolve or report material libraries; `usemtl` names remain available. pub ignore_material_libraries: bool, + /// Treat each group declaration as an object boundary, retaining repeated names. + pub groups_as_objects: bool, } struct Reader<'a> { @@ -407,6 +409,10 @@ impl Reader<'_> { }; } "o" => self.start_object(after.to_string())?, + "g" if self.options.groups_as_objects => { + self.group.clear(); + self.start_object(after.to_string())?; + } "g" => { self.slot = None; self.group = after.to_string(); diff --git a/crates/mesh/src/tests/obj.rs b/crates/mesh/src/tests/obj.rs index 583fcb4a0..ed8a89755 100644 --- a/crates/mesh/src/tests/obj.rs +++ b/crates/mesh/src/tests/obj.rs @@ -356,7 +356,7 @@ fn face_tokens_and_continued_lines_are_bounded_before_collection() { #[test] fn compatibility_options_keep_runs_and_skip_material_libraries() { let input = b"\xef\xbb\xbf v 0 0 0\nv 1 0 0\nv 0 1 0\nmtllib missing.mtl\nusemtl Red\nf 1 2 3\nusemtl Blue\nf 1 2 3\nusemtl Red\nf 1 2 3\n"; - let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true }; + let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true, ..Default::default() }; let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); let parts = &scene.meshes[0].parts; assert_eq!(parts.len(), 3); @@ -364,3 +364,12 @@ fn compatibility_options_keep_runs_and_skip_material_libraries() { assert!(scene.warnings.is_empty()); assert!(crate::obj::read_with_options(b"v NaN 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n", &Limits::default(), &mut no_external, &options).is_err()); } + +#[test] +fn group_boundaries_keep_repeated_names_separate() { + let input = b"v 0 0 0\nv 1 0 0\nv 0 1 0\ng Same\nusemtl M\nf 1 2 3\ng Same\nf 1 3 2\n"; + let options = crate::obj::ReadOptions { groups_as_objects: true, ..Default::default() }; + let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); + assert_eq!(scene.meshes.len(), 2); + assert!(scene.meshes.iter().all(|m| m.name == "Same")); +} From 316d40376e0e4a8b4991ab428c5e0730688a7219 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:19:15 +0000 Subject: [PATCH 3/3] Keep OBJ declaration groups across smoothing changes --- crates/mesh/src/obj.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index b59f903d5..69428eb25 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -400,6 +400,7 @@ impl Reader<'_> { self.vn.push(n.unwrap_or(NAN3)); self.bad_lines += usize::from(n.is_none()); } + "s" if self.options.groups_as_objects => {} "s" => { self.slot = None; self.smoothing = match it.next() {