diff --git a/crates/prc/README.md b/crates/prc/README.md index 614231cf64..2a21bc779d 100644 --- a/crates/prc/README.md +++ b/crates/prc/README.md @@ -36,7 +36,7 @@ BitReader: bit, bits, character, unsigned_integer, integer, integer_vbn, double_ A prototype's part is coloured by its own file structure's styles. Each `ModelNode` lists its name, where the name came from, its visibility and the range of placements it drew. - Occurrence and prototype cycles are `Malformed`; the walk is capped at depth 64, 2^20 visits and `MAX_PLACEMENTS`. -- Trees carrying markups (PMI), views, scene lights or clipping planes are `Unsupported`; textures are read past. +- A tree's markups (PMI), views, scene lights and clipping planes are read past, not modelled; so are textures. - Not yet in the crate: rebuilding compressed meshes, textures. - The `Double` code table's rows come from [prc-rs](https://github.com/ralovich/prc-rs) (MIT); its notice is in `src/acof/table.rs`. A few reading choices also follow prc-rs; each is marked `// prc-rs:` in the source. diff --git a/crates/prc/src/tree/markup.rs b/crates/prc/src/tree/markup.rs new file mode 100644 index 0000000000..193c4fa219 --- /dev/null +++ b/crates/prc/src/tree/markup.rs @@ -0,0 +1,199 @@ +//! What a tree carries beside its geometry, read past and not modelled: markup data (PMI) +//! and views [WD 7.3.10.4, 7.7], and a scene's lights and clipping planes [WD 7.5.15-7.5.18, +//! 7.11.13]. The tree section has no length to skip by, so each is read field by field. + +use super::{ + AMBIENT_LIGHT, ANNOTATION_ITEM, ANNOTATION_REFERENCE, ANNOTATION_SET, DIRECTIONAL_LIGHT, LIGHT_INTENSITY_FROM, MARKUP_LINKED_ITEM, MAX_DEPTH, + MKP_LEADER, MKP_MARKUP, MKP_VIEW, POINT_LIGHT, SPOT_LIGHT, SURF_PLANE, VIEW_FLAGS_FROM, +}; +use crate::PrcError; +use crate::entity::{Ctx, malformed}; + +impl Ctx<'_, '_> { + /// `MarkupData` [WD 7.3.10.4]: linked items, leaders, markups, then annotation entities. + pub(super) fn markup_data(&mut self) -> Result<(), PrcError> { + let n = self.count(1, "markup linked items")?; + for _ in 0..n { + self.markup_linked_item()?; + } + let n = self.count(1, "leaders")?; + for _ in 0..n { + self.leader()?; + } + let n = self.count(1, "markups")?; + for _ in 0..n { + self.mkp_markup()?; + } + let n = self.count(1, "annotation entities")?; + for _ in 0..n { + self.annotation_entity(0)?; + } + Ok(()) + } + + /// A counted list of `MKP_View`s. + pub(super) fn views(&mut self) -> Result<(), PrcError> { + let n = self.count(1, "views")?; + for _ in 0..n { + self.view()?; + } + Ok(()) + } + + /// A counted list of `ReferenceOnPRCBase`s. + fn references(&mut self, what: &'static str) -> Result<(), PrcError> { + let n = self.count(1, what)?; + for _ in 0..n { + self.reference_on_prc_base()?; + } + Ok(()) + } + + /// `MISC_MarkupLinkedItem` (204) [WD 7.4.5]. + // prc-rs: a `has_reference_data` flag and its reference data follow the content entity + // reference (pdf-issues #722). + fn markup_linked_item(&mut self) -> Result<(), PrcError> { + self.expect_type(MARKUP_LINKED_ITEM)?; + self.base_with_graphics()?; + self.r.unsigned_integer()?; // local coordinate system + 1 + if self.r.bit()? { + self.reference_data()?; + } + if self.r.bit()? { + self.reference_data()?; + } + for _ in 0..4 { + self.r.bit()?; // show and delete, markup and leader + } + self.schema.skip_added_fields(MARKUP_LINKED_ITEM, &mut self.r)?; + self.user_data() + } + + /// `MKP_Leader` (503) [WD 7.7.5; pdf-issues #698]. + // prc-rs: the leader's field order. + fn leader(&mut self) -> Result<(), PrcError> { + self.expect_type(MKP_LEADER)?; + self.base_with_graphics()?; + for _ in 0..2 { + if self.r.bit()? { + self.reference_on_prc_base()?; // first, second linked item + } + } + self.r.unsigned_integer()?; // tessellation + 1 + self.schema.skip_added_fields(MKP_LEADER, &mut self.r)?; + self.user_data() + } + + /// `MKP_Markup` (502) [WD 7.7.4]. + // prc-rs: the markup's field order. + fn mkp_markup(&mut self) -> Result<(), PrcError> { + self.expect_type(MKP_MARKUP)?; + self.base_with_graphics()?; + self.r.unsigned_integer()?; // type + self.r.unsigned_integer()?; // subtype + self.references("markup linked items")?; + self.references("markup leaders")?; + self.r.unsigned_integer()?; // tessellation + 1 + self.schema.skip_added_fields(MKP_MARKUP, &mut self.r)?; + self.user_data() + } + + /// An annotation item (504), set (505) or reference (506) [WD 7.7.6-7.7.8]; sets nest. + // prc-rs: the annotation entities' field orders. + fn annotation_entity(&mut self, depth: usize) -> Result<(), PrcError> { + if depth > MAX_DEPTH { + return Err(malformed("annotation sets nest too deep".into())); + } + let t = self.r.unsigned_integer()?; + if !matches!(t, ANNOTATION_ITEM | ANNOTATION_SET | ANNOTATION_REFERENCE) { + return Err(malformed(format!("entity type {t} as an annotation"))); + } + self.base_with_graphics()?; + match t { + ANNOTATION_ITEM => self.reference_on_prc_base()?, + ANNOTATION_SET => { + let n = self.count(1, "annotation set")?; + for _ in 0..n { + self.annotation_entity(depth + 1)?; + } + } + _ => self.references("annotation reference")?, + } + self.schema.skip_added_fields(t, &mut self.r)?; + self.user_data() + } + + /// `MKP_View` (501) [WD 7.7.3]. + // prc-rs: the view's field order, and its flags, linked items and filters from version + // 8137. + fn view(&mut self) -> Result<(), PrcError> { + self.expect_type(MKP_VIEW)?; + self.base_with_graphics()?; + self.references("view annotations")?; + self.plane()?; + if self.r.bit()? { + self.scene_display_parameters()?; + } + if self.version >= VIEW_FLAGS_FROM { + for _ in 0..3 { + self.r.bit()?; // annotation view, default view, direction + } + self.references("view linked items")?; + let n = self.count(1, "view filters")?; + for _ in 0..n { + self.filter()?; + } + } + self.schema.skip_added_fields(MKP_VIEW, &mut self.r)?; + self.user_data() + } + + /// `SURF_Plane` (86) [WD 7.11.13; pdf-issues #658]: `ContentSurface` [WD 7.11.3.2], a + /// `Transformation3D` with no presence flag, the `Domain` [WD 8.2.4] and the + /// parameterisation's four coefficients. + // prc-rs: the plane's field order. + pub(super) fn plane(&mut self) -> Result<(), PrcError> { + self.expect_type(SURF_PLANE)?; + if self.r.bit()? { + self.content_prc_base()?; // base geometry: attributes and name + self.r.unsigned_integer()?; // identifier + } + self.r.unsigned_integer()?; // extend type + self.transformation_3d()?; + for _ in 0..8 { + self.r.double()?; // domain (4), coefficients (4) + } + self.schema.skip_added_fields(SURF_PLANE, &mut self.r) + } + + /// A light (731-734) [WD 7.5.15-7.5.18]. + // prc-rs: the lights' field orders, and a directional light's intensity from version 8030 + // (pdf-issues #745). + pub(super) fn light(&mut self) -> Result<(), PrcError> { + let t = self.r.unsigned_integer()?; + if !(AMBIENT_LIGHT..=SPOT_LIGHT).contains(&t) { + return Err(malformed(format!("entity type {t} as a light"))); + } + self.content_prc_ref_base()?; + for _ in 0..3 { + self.r.unsigned_integer()?; // ambient, diffuse, specular colour + 1 + } + if matches!(t, POINT_LIGHT | SPOT_LIGHT) { + self.vector3()?; // location + for _ in 0..3 { + self.r.double()?; // attenuation + } + } + if matches!(t, DIRECTIONAL_LIGHT | SPOT_LIGHT) { + self.vector3()?; // direction + } + if t == DIRECTIONAL_LIGHT && self.version >= LIGHT_INTENSITY_FROM { + self.r.double()?; + } + if t == SPOT_LIGHT { + self.r.double()?; // fall-off angle + self.r.double()?; // fall-off exponent + } + self.schema.skip_added_fields(t, &mut self.r) + } +} diff --git a/crates/prc/src/tree/markup_tests.rs b/crates/prc/src/tree/markup_tests.rs new file mode 100644 index 0000000000..dcd658bb62 --- /dev/null +++ b/crates/prc/src/tree/markup_tests.rs @@ -0,0 +1,280 @@ +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use super::tests::{Occ, PLAIN, S_RED, base, graphics, part_with, plate, ref_base, single}; +use crate::PrcError; +use crate::bits::BitReader; +use crate::entity::Ctx; +use crate::testw::W; +use crate::{Schema, Tessellation}; + +/// Read after each entity, to show it was read to its last bit and no further. +const SENTINEL: u32 = 0x5A5A; + +/// Runs `read` over `w` then the sentinel, at authoring version `version`. +fn read_at(w: &W, version: u32, read: impl FnOnce(&mut Ctx<'_, '_>) -> Result<(), PrcError>) -> Result<(), PrcError> { + let mut all = W::default(); + all.append(w).uint(SENTINEL); + let bytes = all.bytes(); + let schema = Schema::default(); + let mut ctx = Ctx::new(BitReader::new(&bytes), &schema, version); + read(&mut ctx)?; + assert_eq!(ctx.r.unsigned_integer()?, SENTINEL, "read past the wrong number of bits"); + Ok(()) +} + +fn read(w: &W, read: impl FnOnce(&mut Ctx<'_, '_>) -> Result<(), PrcError>) -> Result<(), PrcError> { + read_at(w, 8137, read) +} + +/// Each truncation of `w` is an error, never a panic. +fn truncations_fail(w: &W, read: impl Fn(&mut Ctx<'_, '_>) -> Result<(), PrcError>) { + let bytes = w.bytes(); + let schema = Schema::default(); + for end in 0..bytes.len().saturating_sub(1) { + let mut ctx = Ctx::new(BitReader::new(&bytes[..end]), &schema, 8137); + assert!(read(&mut ctx).is_err(), "cut at {end}"); + } +} + +fn reference(w: &mut W, uid: u32) { + w.uint(205).uint(237).bit(true).uint(uid); +} + +fn markup_data(linked: &[W], leaders: &[W], markups: &[W], annotations: &[W]) -> W { + let mut w = W::default(); + for list in [linked, leaders, markups, annotations] { + w.uint(u32::try_from(list.len()).unwrap()); + for e in list { + w.append(e); + } + } + w +} + +fn linked_item() -> W { + let mut w = W::default(); + w.uint(204); + graphics(&mut w, None, 1, PLAIN); + w.uint(0).bit(true); + reference(&mut w, 7); + w.bit(true).uint(206).uint(149).bit(true).bit(true).uint(1).uint(1).uint(1).uint(0); + w.bit(true).bit(false).bit(true).bit(false).uint(0); + w +} + +fn leader() -> W { + let mut w = W::default(); + w.uint(503); + graphics(&mut w, None, 2, PLAIN); + w.bit(true); + reference(&mut w, 3); + w.bit(false).uint(4).uint(0); + w +} + +fn markup() -> W { + let mut w = W::default(); + w.uint(502); + graphics(&mut w, Some("Note"), 3, PLAIN); + w.uint(1).uint(2).uint(1); + reference(&mut w, 7); + w.uint(2); + reference(&mut w, 2); + reference(&mut w, 2); + w.uint(3).uint(0); + w +} + +fn annotation(t: u32, inner: impl FnOnce(&mut W)) -> W { + let mut w = W::default(); + w.uint(t); + graphics(&mut w, None, 4, PLAIN); + inner(&mut w); + w.uint(0); + w +} + +fn annotation_item() -> W { + annotation(504, |w| reference(w, 3)) +} + +fn annotation_set(members: &[W]) -> W { + annotation(505, |w| { + w.uint(u32::try_from(members.len()).unwrap()); + for m in members { + w.append(m); + } + }) +} + +fn annotation_reference() -> W { + annotation(506, |w| { + w.uint(1); + reference(w, 3); + }) +} + +fn plane(w: &mut W) { + w.uint(86).bit(true); + base(w, Some("Front")); + w.uint(9).uint(0).put(0x01, 8); + for v in [1., 2., 3., 0., 0., 1., 1., 1., 1., 0., 0.] { + w.double(v); + } +} + +fn light(t: u32) -> W { + let mut w = W::default(); + w.uint(t); + ref_base(&mut w, None, 5); + w.uint(1).uint(2).uint(3); + let doubles = match t { + 732 => 6, + 733 => 4, + 734 => 11, + _ => 0, + }; + for _ in 0..doubles { + w.double(0.5); + } + w +} + +/// A scene with `lights` and `planes` clipping planes; `is_absolute` is written from 8137. +fn scene(lights: &[W], planes: usize, version: u32) -> W { + let mut w = W::default(); + w.uint(741); + ref_base(&mut w, None, 6); + w.bit(true).uint(u32::try_from(lights.len()).unwrap()); + for l in lights { + w.append(l); + } + w.bit(false).bit(false).uint(u32::try_from(planes).unwrap()); + for _ in 0..planes { + plane(&mut w); + } + w.uint(0).uint(0).uint(0); + if version >= 8137 { + w.bit(true); + } + w +} + +fn filter(w: &mut W) { + w.uint(320); + ref_base(w, None, 8); + w.bit(true).bit(true).uint(0).bit(true).uint(0).uint(0); +} + +fn view(version: u32) -> W { + let mut w = W::default(); + w.uint(501); + graphics(&mut w, Some("Top"), 7, PLAIN); + w.uint(1); + reference(&mut w, 3); + plane(&mut w); + w.bit(true).append(&scene(&[light(731)], 1, version)); + if version >= 8137 { + w.bit(true).bit(false).bit(true).uint(1); + reference(&mut w, 7); + w.uint(1); + filter(&mut w); + } + w.uint(0); + w +} + +fn views(vs: &[W]) -> W { + let mut w = W::default(); + w.uint(u32::try_from(vs.len()).unwrap()); + for v in vs { + w.append(v); + } + w +} + +#[test] +fn a_markup_linked_item_is_read_past() { + let w = markup_data(&[linked_item()], &[], &[], &[]); + read(&w, |c| c.markup_data()).unwrap(); + truncations_fail(&w, |c| c.markup_data()); +} + +#[test] +fn a_leader_is_read_past() { + let w = markup_data(&[], &[leader()], &[], &[]); + read(&w, |c| c.markup_data()).unwrap(); + truncations_fail(&w, |c| c.markup_data()); +} + +#[test] +fn a_markup_is_read_past() { + let w = markup_data(&[], &[], &[markup()], &[]); + read(&w, |c| c.markup_data()).unwrap(); + truncations_fail(&w, |c| c.markup_data()); +} + +#[test] +fn annotation_items_sets_and_references_are_read_past() { + let set = annotation_set(&[annotation_item(), annotation_set(&[annotation_reference()])]); + let w = markup_data(&[], &[], &[], &[annotation_item(), set, annotation_reference()]); + read(&w, |c| c.markup_data()).unwrap(); + truncations_fail(&w, |c| c.markup_data()); +} + +#[test] +fn annotation_sets_nested_past_the_ceiling_are_an_error() { + let mut e = annotation_item(); + for _ in 0..=super::MAX_DEPTH { + e = annotation_set(&[e]); + } + let w = markup_data(&[], &[], &[], &[e]); + assert!(matches!(read(&w, |c| c.markup_data()), Err(PrcError::Malformed(_)))); +} + +#[test] +fn an_annotation_of_another_type_is_an_error() { + let w = markup_data(&[], &[], &[], &[annotation(502, |_| {})]); + assert!(matches!(read(&w, |c| c.markup_data()), Err(PrcError::Malformed(_)))); +} + +#[test] +fn a_view_is_read_past_with_its_flags_only_from_8137() { + let w = views(&[view(8137)]); + read(&w, |c| c.views()).unwrap(); + truncations_fail(&w, |c| c.views()); + read_at(&views(&[view(8030)]), 8030, |c| c.views()).unwrap(); +} + +#[test] +fn scene_lights_of_each_kind_are_read_past() { + let w = scene(&[light(731), light(732), light(733), light(734)], 0, 8137); + read(&w, |c| c.scene_display_parameters()).unwrap(); + truncations_fail(&w, |c| c.scene_display_parameters()); + let mut old = W::default(); + old.uint(733); + ref_base(&mut old, None, 5); + old.uint(1).uint(2).uint(3).double(0.).double(0.).double(1.); + read_at(&old, 8020, |c| c.light()).unwrap(); +} + +#[test] +fn scene_clipping_planes_are_read_past() { + let w = scene(&[], 2, 8137); + read(&w, |c| c.scene_display_parameters()).unwrap(); + truncations_fail(&w, |c| c.scene_display_parameters()); +} + +#[test] +fn a_tree_with_markups_and_views_still_places_its_parts() { + let mut tail = markup_data(&[linked_item()], &[leader()], &[markup()], &[annotation_set(&[annotation_item()])]); + tail.append(&views(&[view(8137)])); + let part = part_with(Some("Plate"), PLAIN, 1, |w| super::tests::poly_item(w, 7, 0, 1, (S_RED, super::SHOW)), &tail); + let occs = [Occ { name: Some("Asm"), sons: vec![1], markups: Some(tail.clone()), ..Occ::default() }, Occ { part: 1, ..Occ::default() }]; + let f = single(&[part, plate(S_RED)], &occs); + let nodes = f.model_tree().unwrap(); + assert_eq!(nodes.len(), 2); + let p = f.placements().unwrap(); + assert_eq!(p.len(), 1); + assert!(matches!(f.file_structures[0].tessellations().unwrap().first(), Some(Tessellation::Mesh(_)))); +} diff --git a/crates/prc/src/tree/mod.rs b/crates/prc/src/tree/mod.rs index 6967ba40ea..eae6b269a2 100644 --- a/crates/prc/src/tree/mod.rs +++ b/crates/prc/src/tree/mod.rs @@ -2,8 +2,7 @@ //! coordinate systems (303) and the model file (301), walked into placed tessellations //! [WD 7.3.3-7.3.11, 7.5, 7.6]. //! -//! Markups, views, scene lights and clipping planes are not read past; a tree carrying any -//! of them is [`PrcError::Unsupported`]. +//! Markups (PMI), views, scene lights and clipping planes are read past and not modelled. // prc-rs: where the working draft leaves an entity's field order open, the order here is // prc-rs's format description's; each such entity's reader carries its own marker. @@ -16,6 +15,7 @@ use crate::entity::{Ctx, malformed}; use crate::geometry::{Matrix, Vec3}; use crate::{PrcError, PrcFile, Schema, SectionKind, UniqueId}; +mod markup; mod node; mod overrides; mod read; @@ -38,8 +38,10 @@ const FILE_STRUCTURE_TREE: u32 = 304; const PRODUCT_OCCURRENCE: u32 = 310; const PART_DEFINITION: u32 = 311; const FILTER: u32 = 320; +const SURF_PLANE: u32 = 86; const CARTESIAN_TRANSFORMATION: u32 = 202; const ENTITY_REFERENCE: u32 = 203; +const MARKUP_LINKED_ITEM: u32 = 204; const REFERENCE_ON_PRC_BASE: u32 = 205; const REFERENCE_ON_TOPOLOGY: u32 = 206; const GENERAL_TRANSFORMATION: u32 = 207; @@ -52,6 +54,12 @@ const RI_POLY_BREP_MODEL: u32 = 237; const RI_POLY_WIRE: u32 = 238; const RI_SET: u32 = 239; const RI_COORDINATE_SYSTEM: u32 = 240; +const MKP_VIEW: u32 = 501; +const MKP_MARKUP: u32 = 502; +const MKP_LEADER: u32 = 503; +const ANNOTATION_ITEM: u32 = 504; +const ANNOTATION_SET: u32 = 505; +const ANNOTATION_REFERENCE: u32 = 506; const STYLE: u32 = 701; const MATERIAL: u32 = 702; const PICTURE: u32 = 703; @@ -59,10 +67,18 @@ const TEXTURE_APPLICATION: u32 = 711; const TEXTURE_DEFINITION: u32 = 712; const TEXTURE_TRANSFORMATION: u32 = 713; const LINE_PATTERN: u32 = 721; +const AMBIENT_LIGHT: u32 = 731; +const POINT_LIGHT: u32 = 732; +const DIRECTIONAL_LIGHT: u32 = 733; +const SPOT_LIGHT: u32 = 734; const SCENE_DISPLAY_PARAMETERS: u32 = 741; const CAMERA: u32 = 742; /// prc-rs: a scene's `is_absolute` exists from this authoring version. const ABSOLUTE_SCENE_FROM: u32 = 8137; +/// prc-rs: a view's flags, linked items and filters exist from this authoring version. +const VIEW_FLAGS_FROM: u32 = 8137; +/// prc-rs: a directional light's intensity exists from this authoring version. +const LIGHT_INTENSITY_FROM: u32 = 8030; /// `TextureDefinition` mapping type "operator" and function "blend", 0-based /// (pdf-issues #485). const TEXTURE_MAPPING_OPERATOR: i32 = 3; @@ -215,9 +231,8 @@ impl PrcFile { /// the file stores no tree ([`Self::stores_tree`]). /// /// # Errors - /// [`PrcError::Unsupported`] for a tree carrying markups (PMI), views, scene lights or - /// clipping planes, or, when an item is placed by a reference coordinate system, globals - /// carrying fonts or fill patterns; [`PrcError::Malformed`] for an index naming no entity, + /// [`PrcError::Unsupported`] when an item is placed by a reference coordinate system and + /// the globals carry fonts or fill patterns; [`PrcError::Malformed`] for an index naming no entity, /// a cycle of occurrences or prototypes, or a tree past [`MAX_PLACEMENTS`] or the depth /// ceiling; read errors as [`crate::FileStructure::tessellations`]. pub fn placements(&self) -> Result, PrcError> { @@ -289,5 +304,7 @@ impl PrcFile { } } +#[cfg(test)] +mod markup_tests; #[cfg(test)] mod tests; diff --git a/crates/prc/src/tree/read.rs b/crates/prc/src/tree/read.rs index 6c8047b989..ff3affaeee 100644 --- a/crates/prc/src/tree/read.rs +++ b/crates/prc/src/tree/read.rs @@ -80,7 +80,7 @@ impl Ctx<'_, '_> { /// The embedded `Transformation3D` [WD 7.4.11.1]: a behaviour byte, then each field its /// bits name. - fn transformation_3d(&mut self) -> Result { + pub(super) fn transformation_3d(&mut self) -> Result { let b = self.r.character()?; let mut m = Matrix::IDENTITY; let set_col = |m: &mut Matrix, col: usize, v: Vec3| { @@ -216,15 +216,16 @@ impl Ctx<'_, '_> { Ok(None) } - /// `SceneDisplayParameters` (741) [WD 7.5.19]: read past. Lights and clipping planes are - /// [`PrcError::Unsupported`]. + /// `SceneDisplayParameters` (741) [WD 7.5.19]: read past, lights and clipping planes + /// included. // prc-rs: the scene's field order, and `is_absolute` from version 8137. - fn scene_display_parameters(&mut self) -> Result<(), PrcError> { + pub(super) fn scene_display_parameters(&mut self) -> Result<(), PrcError> { self.expect_type(SCENE_DISPLAY_PARAMETERS)?; self.content_prc_ref_base()?; self.r.bit()?; // is_active - if self.r.unsigned_integer()? != 0 { - return Err(PrcError::Unsupported("scene lights")); + let n = self.count(1, "scene lights")?; + for _ in 0..n { + self.light()?; } if self.r.bit()? { self.camera()?; @@ -232,8 +233,9 @@ impl Ctx<'_, '_> { if self.r.bit()? { self.vector3()?; // rotation centre } - if self.r.unsigned_integer()? != 0 { - return Err(PrcError::Unsupported("scene clipping planes")); + let n = self.count(1, "scene clipping planes")?; + for _ in 0..n { + self.plane()?; } self.r.unsigned_integer()?; // background style + 1 self.r.unsigned_integer()?; // default style + 1 @@ -263,47 +265,46 @@ impl Ctx<'_, '_> { self.schema.skip_added_fields(CAMERA, &mut self.r) } - /// `MarkupData` [WD 7.3.10.4]: only the empty form reads past. - fn markup_data(&mut self) -> Result<(), PrcError> { - for _ in 0..4 { - if self.r.unsigned_integer()? != 0 { - return Err(PrcError::Unsupported("tree markups (PMI)")); - } - } - Ok(()) - } - - fn views(&mut self) -> Result<(), PrcError> { - if self.r.unsigned_integer()? != 0 { - return Err(PrcError::Unsupported("tree views")); - } - Ok(()) - } - /// `MISC_EntityReference` (203) [WD 7.4.4, 7.4.10]; `None` when it names no target. // prc-rs: the local coordinate system precedes the optional reference data. fn entity_reference(&mut self) -> Result, PrcError> { self.expect_type(ENTITY_REFERENCE)?; let (hidden, graphics) = self.base_with_graphics()?; let local = self.r.unsigned_integer()?; - let mut target = None; - if self.r.bit()? { - match self.r.unsigned_integer()? { - REFERENCE_ON_PRC_BASE => { - let kind = self.r.unsigned_integer()?; - let fs = self.file_identifier()?; - let uid = self.r.unsigned_integer()?; - target = Some(Target::Entity { kind, fs, uid }); - } - REFERENCE_ON_TOPOLOGY => target = self.reference_on_topology()?, - t => return Err(malformed(format!("entity type {t} as reference data"))), - } - } + let target = if self.r.bit()? { self.reference_data()? } else { None }; self.schema.skip_added_fields(ENTITY_REFERENCE, &mut self.r)?; self.user_data()?; Ok(target.map(|target| Override { graphics, hidden, local, target })) } + /// `ReferenceData` [WD 7.4.10.2]: a `ReferenceOnPRCBase` (205) or a + /// `ReferenceOnTopology` (206), type code included; `None` when it names no target. + pub(super) fn reference_data(&mut self) -> Result, PrcError> { + let t = self.r.unsigned_integer()?; + let target = match t { + REFERENCE_ON_PRC_BASE => Some(self.reference_on_prc_base_body()?), + REFERENCE_ON_TOPOLOGY => self.reference_on_topology()?, + t => return Err(malformed(format!("entity type {t} as reference data"))), + }; + self.schema.skip_added_fields(t, &mut self.r)?; + Ok(target) + } + + /// `ReferenceOnPRCBase` (205) [WD 7.4.6] after its type code. + fn reference_on_prc_base_body(&mut self) -> Result { + let kind = self.r.unsigned_integer()?; + let fs = self.file_identifier()?; + let uid = self.r.unsigned_integer()?; + Ok(Target::Entity { kind, fs, uid }) + } + + /// `ReferenceOnPRCBase` (205) [WD 7.4.6], type code included: read past. + pub(super) fn reference_on_prc_base(&mut self) -> Result<(), PrcError> { + self.expect_type(REFERENCE_ON_PRC_BASE)?; + self.reference_on_prc_base_body()?; + self.schema.skip_added_fields(REFERENCE_ON_PRC_BASE, &mut self.r) + } + /// `ReferenceOnTopology` (206) after its type code; `None` without a body. fn reference_on_topology(&mut self) -> Result, PrcError> { let kind = self.r.unsigned_integer()?; @@ -318,7 +319,7 @@ impl Ctx<'_, '_> { /// `ASM_Filter` (320) [WD 7.3.12]: read past. // prc-rs: the filter's field order. - fn filter(&mut self) -> Result<(), PrcError> { + pub(super) fn filter(&mut self) -> Result<(), PrcError> { self.expect_type(FILTER)?; self.content_prc_ref_base()?; self.r.bit()?; // is_active diff --git a/crates/prc/src/tree/tests.rs b/crates/prc/src/tree/tests.rs index bf30a03cbe..7f395c0f3b 100644 --- a/crates/prc/src/tree/tests.rs +++ b/crates/prc/src/tree/tests.rs @@ -12,9 +12,9 @@ const RED: Rgba = Rgba { r: 1., g: 0., b: 0., a: 1. }; const GREEN: Rgba = Rgba { r: 0., g: 1., b: 0., a: 1. }; const BLUE: Rgba = Rgba { r: 0., g: 0., b: 1., a: 1. }; /// Visible graphics with no style. -const PLAIN: (u32, u16) = (0, SHOW); +pub(super) const PLAIN: (u32, u16) = (0, SHOW); /// The biased style indices of [`palette`]: red, green, a blue material. -const S_RED: u32 = 1; +pub(super) const S_RED: u32 = 1; const S_GREEN: u32 = 2; const S_BLUE: u32 = 3; const RI_POLY_BREP: u32 = 237; @@ -28,17 +28,17 @@ fn len(n: usize) -> u32 { } /// `ContentPRCBase` with no attributes and its own name. -fn base(w: &mut W, name: Option<&str>) { +pub(super) fn base(w: &mut W, name: Option<&str>) { w.uint(0).bit(false).string(name); } -fn ref_base(w: &mut W, name: Option<&str>, uid: u32) { +pub(super) fn ref_base(w: &mut W, name: Option<&str>, uid: u32) { base(w, name); w.uint(0).uint(0).uint(uid); } /// `PRCBaseWithGraphics` with its own `(style, behaviour)`. -fn graphics(w: &mut W, name: Option<&str>, uid: u32, (style, bits): (u32, u16)) { +pub(super) fn graphics(w: &mut W, name: Option<&str>, uid: u32, (style, bits): (u32, u16)) { ref_base(w, name, uid); w.bit(false).uint(0).uint(style).put(u64::from(bits & 0xff), 8).put(u64::from(bits >> 8), 8); } @@ -58,7 +58,7 @@ fn file_id(w: &mut W, fs: Option) { } /// An `RI_PolyBrepModel` drawing biased tessellation `tess` under local system `cs`. -fn poly_item(w: &mut W, uid: u32, cs: u32, tess: u32, g: (u32, u16)) { +pub(super) fn poly_item(w: &mut W, uid: u32, cs: u32, tess: u32, g: (u32, u16)) { w.uint(RI_POLY_BREP); graphics(w, None, uid, g); w.uint(cs).uint(tess).bit(false).uint(0); @@ -71,8 +71,17 @@ fn brep_item(w: &mut W, uid: u32, g: (u32, u16), brep: (u32, u32)) { w.uint(0).uint(1).bit(true).uint(brep.0).uint(brep.1).bit(false).uint(0); } -/// A `PartDefinition` holding the `n` items `items` writes. +/// A `PartDefinition` holding the `n` items `items` writes, and no markups or views. fn part(name: Option<&str>, g: (u32, u16), n: usize, items: impl FnOnce(&mut W)) -> W { + let mut none = W::default(); + for _ in 0..5 { + none.uint(0); + } + part_with(name, g, n, items, &none) +} + +/// [`part`] with `markups` (its markup data and views) written after the items. +pub(super) fn part_with(name: Option<&str>, g: (u32, u16), n: usize, items: impl FnOnce(&mut W), markups: &W) -> W { let mut w = W::default(); w.uint(311); graphics(&mut w, name, 0, g); @@ -81,28 +90,28 @@ fn part(name: Option<&str>, g: (u32, u16), n: usize, items: impl FnOnce(&mut W)) } w.uint(len(n)); items(&mut w); - for _ in 0..6 { - w.uint(0); // markups, views, user data - } + w.append(markups).uint(0); // user data w } /// One poly-B-rep item (uid 7) drawing tessellation 1 in style `style`. -fn plate(style: u32) -> W { +pub(super) fn plate(style: u32) -> W { part(Some("Plate"), PLAIN, 1, |w| poly_item(w, 7, 0, 1, (style, SHOW))) } #[derive(Default)] -struct Occ<'a> { - name: Option<&'a str>, - g: Option<(u32, u16)>, - part: u32, - prototype: u32, - prototype_fs: Option, - sons: Vec, - suppressed: bool, - location: Option, - refs: Vec, +pub(super) struct Occ<'a> { + pub(super) name: Option<&'a str>, + pub(super) g: Option<(u32, u16)>, + pub(super) part: u32, + pub(super) prototype: u32, + pub(super) prototype_fs: Option, + pub(super) sons: Vec, + pub(super) suppressed: bool, + pub(super) location: Option, + pub(super) refs: Vec, + /// Its markup data and views; none when `None`. + pub(super) markups: Option, } fn occ(w: &mut W, o: &Occ<'_>) { @@ -126,13 +135,20 @@ fn occ(w: &mut W, o: &Occ<'_>) { for r in &o.refs { w.append(r); } - for _ in 0..5 { - w.uint(0); // markups, views + match &o.markups { + Some(m) => { + w.append(m); + } + None => { + for _ in 0..5 { + w.uint(0); + } + } } w.bit(false).uint(0).uint(0).uint(0); } -fn tree(parts: &[W], occs: &[Occ<'_>]) -> W { +pub(super) fn tree(parts: &[W], occs: &[Occ<'_>]) -> W { let mut w = W::default(); w.uint(304); base(&mut w, None); @@ -310,7 +326,7 @@ fn prc(structures: &[Fs], model: &W) -> Vec { } /// One file structure with [`palette`], `parts` and `occs`, whose root is occurrence 0. -fn single(parts: &[W], occs: &[Occ<'_>]) -> PrcFile { +pub(super) fn single(parts: &[W], occs: &[Occ<'_>]) -> PrcFile { PrcFile::parse(&prc(&[Fs { globals: palette(), tree: tree(parts, occs), tess: two_face_square() }], &model(&[(0, 1)]))).unwrap() }