Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/classes/civil/cogo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@
//!
//! ```text
//! BS 0x04BE, BL 34 (its key in the root tree node), BL 0, BL 0, 3 bits (010)
//! BL count, count × { BL 1, 3BD position, BL number, BS 0x0420 }
//! BL count, count × { BL 1, 3BD position, BL number, BS 0x0420 marker }
//! (the marker is `0x0420` in this crate's records; the application
//! writes `0x0421`, and `0x0621` for points a survey created)
//! BL n, n × BL number points with their own point style
//! BL m, m × BL number points with their own label style
//! BL 0
//! BL jA, jA × { BL number, BL tag } the leading tagged list (see
//! `tagged_leading` below; empty in this crate's records)
//! BL j, j × { BL number, BL name tag } named points
//! BL j, j × { BL name tag, BL number } the same, keyed by name
//! BL k, k × { BL number, BL description tag }
//! 13 × slot: BL e, e × { BL key | 0x40000000, BL c, c × { BL number, value } }
//! handles: n × point style, m × label style, the AeccDbCogoPoint class node
//! strings: "", the names of the j points, the same names again, the raw
//! description of each of the k points in order, then for each
//! slot entry its property's key text and its text values
//! strings: "", the texts of the leading tagged list in order, the names of
//! the j points, the same names again, the raw description of each
//! of the k points in order, then for each slot entry its
//! property's key text and its text values
//! ```
//!
//! The leading tagged list follows the styles in every record the
//! application writes (the crate reads it and keeps nothing from it yet);
//! this crate's own records place an empty one there.
//!
//! The slots hold the values of user-defined properties by kind: slot 4
//! doubles (BD), 5 booleans (B; RC in the definitions), 6 texts (none inline: each text follows
//! the key text), 7 integers (BL); the others are empty in every sample.
Expand Down Expand Up @@ -231,11 +239,17 @@ pub(super) fn entity_number(data: &[u8], start: usize, end: usize) -> Option<u32
u32::try_from(bits.bit_long()?).ok().filter(|n| *n > 0)
}

/// Flag bits a point record's marker short carries beyond its fixed
/// `0x0420` base: `0x0001` the application sets on its points, `0x0200`
/// points a survey created (both observed over the whole tutorial
/// corpus; the record's layout does not change with them).
const RECORD_MARKER_FLAGS: i16 = 0x0201;

fn record(bits: &mut Bits) -> Option<(u32, Vector3)> {
(bits.bit_long()? == 1).then_some(())?;
let position = Vector3::new(bits.bit_double()?, bits.bit_double()?, bits.bit_double()?);
let number = u32::try_from(bits.bit_long()?).ok().filter(|n| *n > 0)?;
(bits.bit_short()? == 0x0420).then_some(())?;
(bits.bit_short()? & !RECORD_MARKER_FLAGS == 0x0420).then_some(())?;
Some((number, position))
}

Expand All @@ -249,6 +263,8 @@ struct PointDatabase {
records: Vec<(u32, Vector3)>,
styled: Vec<u32>,
labelled: Vec<u32>,
/// The leading tagged list (numbers; its texts precede the names).
tagged_leading: Vec<u32>,
named: Vec<u32>,
/// Length of the list after the names (the same names keyed by text).
again: usize,
Expand Down Expand Up @@ -277,7 +293,7 @@ fn database(bits: &mut Bits) -> Option<PointDatabase> {
let records = (0..count).map(|_| record(bits)).collect::<Option<Vec<_>>>()?;
let styled = numbers(bits)?;
let labelled = numbers(bits)?;
(bits.bit_long()? == 0).then_some(())?;
let tagged_leading = tagged(bits)?;
let named = tagged(bits)?;
// The same names keyed by text: { BL tag, BL number }.
let again = usize::try_from(bits.bit_long()?).ok().filter(|n| *n <= bits.remaining() / 4)?;
Expand Down Expand Up @@ -306,7 +322,7 @@ fn database(bits: &mut Bits) -> Option<PointDatabase> {
values.push((key, list));
}
}
Some(PointDatabase { records, styled, labelled, named, again, described, values })
Some(PointDatabase { records, styled, labelled, tagged_leading, named, again, described, values })
}

/// Every point of a point database, in record order. `handles` and
Expand All @@ -316,7 +332,8 @@ pub(super) fn points(data: &[u8], start: usize, end: usize, handles: &[u64], str
return Vec::new();
};
// Each value entry: its key text, then its text values.
let mut texts = strings.iter().skip(1 + db.named.len() + db.again + db.described.len()).cloned();
let text_base = 1 + db.tagged_leading.len() + db.named.len() + db.again + db.described.len();
let mut texts = strings.iter().skip(text_base).cloned();
let mut values: Vec<(String, Vec<(u32, PropertyValue)>)> = Vec::new();
for (_, list) in &db.values {
let Some(key) = texts.next() else { return Vec::new() };
Expand All @@ -343,9 +360,14 @@ pub(super) fn points(data: &[u8], start: usize, end: usize, handles: &[u64], str
.described
.iter()
.position(|n| *n == number)
.and_then(|i| strings.get(1 + db.named.len() + db.again + i))
.and_then(|i| strings.get(text_base - db.described.len() + i))
.cloned(),
name: db
.named
.iter()
.position(|n| *n == number)
.and_then(|i| strings.get(1 + db.tagged_leading.len() + i))
.cloned(),
name: db.named.iter().position(|n| *n == number).and_then(|i| strings.get(1 + i)).cloned(),
properties: values
.iter()
.filter_map(|(key, list)| list.iter().find(|(n, _)| *n == number).map(|(_, v)| (key.clone(), v.clone())))
Expand Down
90 changes: 90 additions & 0 deletions tests/civil_read_cogo_points_app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! The point database of drawings the application wrote.
//!
//! Two record forms the application writes and this crate's writer does
//! not (the D016 packet-1 read gap W1-5): the per-record marker short is
//! `0x0421` (and `0x0621` for points a survey created), not the `0x0420`
//! this crate writes, and the database carries a leading tagged list
//! before the named points where the crate's records keep an empty one.
//! Without both, `cogo_points` decoded no application point database at
//! all (0 of 538 in Survey-3.dwg, 0 of 2405 in Points-4a.dwg).

use acadrust::classes::civil::{cogo_points, CogoPoint};
use acadrust::DwgReader;

/// The application's tutorial drawing, from `ACADRUST_CIVIL_TUTORIALS_DIR`.
fn app_drawing(name: &str) -> Option<acadrust::CadDocument> {
let dir = std::env::var_os("ACADRUST_CIVIL_TUTORIALS_DIR")?;
let path = std::path::Path::new(&dir).join(name);
if !path.exists() {
return None;
}
Some(DwgReader::from_file(&path).unwrap().read().unwrap())
}

fn by_number<'a>(points: &'a [CogoPoint], number: u32) -> &'a CogoPoint {
points.iter().find(|p| p.number == number).unwrap_or_else(|| panic!("point {number} missing"))
}

/// Survey-3.dwg: 538 points a survey created (marker `0x0621`, a leading
/// tagged list of 0, raw descriptions of all points, names of none).
/// Positions and raw descriptions of the application's oracle reads
/// (execute_civil_code `CogoPoint.Easting/Northing/Elevation/RawDescription`,
/// 2026-11-24).
#[test]
fn survey_created_points_decode() {
let Some(doc) = app_drawing("Survey-3.dwg") else { return };
let points = cogo_points(&doc);
assert_eq!(points.len(), 538);
let p = by_number(&points, 456);
assert_eq!(p.position.x, 5675.002124308634);
assert_eq!(p.position.y, 5040.000833228553);
assert_eq!(p.position.z, 255.95998686447933);
assert_eq!(p.name, None);
let p = by_number(&points, 816);
assert_eq!(p.position.x, 5518.053784082379);
assert_eq!(p.position.y, 5445.828551114028);
assert_eq!(p.position.z, 247.7690529209087);
// every point has a raw description, none has a name
assert_eq!(points.iter().filter(|p| p.description.is_some()).count(), 538);
assert_eq!(points.iter().filter(|p| p.name.is_some()).count(), 0);
}

/// Points-4a.dwg: 2405 points of the create-points commands (marker
/// `0x0421`, a leading tagged list of 51, raw descriptions of all points,
/// names of none). The first three points exactly as the application
/// reads them (execute_civil_code, D017 packet 2, 2026-11-24:
/// E=19857.2577 N=20260.4556 Z=100.3058 name= etc.).
#[test]
fn create_points_commands_points_decode() {
let Some(doc) = app_drawing("Points-4a.dwg") else { return };
let points = cogo_points(&doc);
assert_eq!(points.len(), 2405);
for (number, x, y, z) in [
(1, 19857.2577, 20260.4556, 100.3058),
(2, 19875.4801, 20207.7198, 101.0929),
(3, 19887.3953, 20692.713, 96.4194),
] {
let p = by_number(&points, number);
assert_eq!(p.position.x, x, "point {number} easting");
assert_eq!(p.position.y, y, "point {number} northing");
assert_eq!(p.position.z, z, "point {number} elevation");
assert_eq!(p.name, None, "point {number} name");
}
}

/// The same database in the drawing the application saves the points
/// tutorial's work into keeps all 2405 points, and a drawing where the
/// points carry their own label styles (Points-4a: 51) maps them.
#[test]
fn point_label_styles_and_counts_hold_across_the_tutorial_set() {
for (name, count) in [("Points-2.dwg", 2405), ("Points-3.dwg", 2405), ("Points-4e.dwg", 2405), ("Survey-2B.dwg", 538)] {
let Some(doc) = app_drawing(name) else { continue };
let points = cogo_points(&doc);
assert_eq!(points.len(), count, "{name}");
assert!(points.iter().all(|p| p.position.x.is_finite() && p.number > 0), "{name}");
}
let Some(doc) = app_drawing("Points-4a.dwg") else { return };
let points = cogo_points(&doc);
let styled_labels = points.iter().filter(|p| p.label_style != 0).count();
assert_eq!(styled_labels, 51, "Points-4a label styles");
}
Loading