cadmpeg-codec-sat 0.5.2

Inspect and decode bare ASM .sat/.sab/.smt/.smb B-rep streams.
Documentation
// SPDX-License-Identifier: Apache-2.0
//! Text and binary decode paths for bare ASM streams.

use cadmpeg_asm::acis_header;
use cadmpeg_asm::asm_header;
use cadmpeg_asm::brep::transfer::{transfer_into_ir, AsmTransferRemainder};
use cadmpeg_asm::brep::{decode_with_header, AsmBrep, DecodePurpose};
use cadmpeg_asm::ids::IdFormat;
use cadmpeg_asm::kernel_header::KernelHeader;
use cadmpeg_asm::{sab, sat};
use cadmpeg_core::decode::DecodeContext;
use cadmpeg_core::CodecError;
use cadmpeg_ir::codec::DecodeResult;
use cadmpeg_ir::document::{CadIr, SourceMeta};
use cadmpeg_ir::report::DecodeReport;
use cadmpeg_ir::units::{Tolerances, Units};
use std::collections::BTreeMap;

use crate::detect::{classify, header_attributes, StreamKind};
use crate::loss::SatLossCode;
use crate::FORMAT;

pub(crate) fn decode(ctx: &DecodeContext<'_>, bytes: &[u8]) -> Result<DecodeResult, CodecError> {
    match classify(bytes) {
        StreamKind::AsmBinary => decode_asm_binary(ctx, bytes),
        StreamKind::Text => decode_text(ctx, bytes),
        StreamKind::AcisBinary => decode_acis_binary(ctx, bytes),
        StreamKind::Unknown => Err(CodecError::Malformed(
            "not an ASM stream: no binary magic and no text header lines".to_string(),
        )),
    }
}

pub(crate) fn decode_asm_binary(
    ctx: &DecodeContext<'_>,
    bytes: &[u8],
) -> Result<DecodeResult, CodecError> {
    let header = asm_header::parse(bytes).ok_or_else(|| {
        CodecError::Malformed("ASM binary magic without a parseable header".to_string())
    })?;
    if let Some(count) = header.entity_count {
        ctx.charge_entities(count, "admit SAT header entities")?;
    }
    let width = usize::from(header.width);
    let start = asm_header::record_stream_start(bytes).ok_or_else(|| {
        CodecError::Malformed("ASM binary header without a record stream".to_string())
    })?;
    // A history-bearing stream ends its solved partition at the delta-state
    // boundary; a history-less stream ends at EOF without a terminator tag.
    let framed = match asm_header::solved_record_limit(bytes) {
        Some(limit) => sab::frame(bytes, start, limit, width),
        None => sab::frame_history(bytes, start, bytes.len(), width),
    };
    let records =
        framed.map_err(|error| CodecError::Malformed(format!("SAB framing failed: {error}")))?;
    let brep = decode_with_header(
        &records,
        bytes,
        Some(header.clone()),
        "stream",
        IdFormat(FORMAT),
        DecodePurpose::Model,
    );
    let mut attributes = BTreeMap::new();
    header_attributes(&header, "asm", &mut attributes);
    attributes.insert("encoding".to_string(), "binary".to_string());
    build_result(ctx, brep, attributes, &header, None)
}

pub(crate) fn decode_acis_binary(
    ctx: &DecodeContext<'_>,
    bytes: &[u8],
) -> Result<DecodeResult, CodecError> {
    let header = acis_header::parse(bytes).ok_or_else(|| {
        CodecError::Malformed("ACIS binary magic without a parseable header".to_string())
    })?;
    if let Some(count) = header.entity_count {
        ctx.charge_entities(count, "admit SAT header entities")?;
    }
    if !matches!(header.save_format_major(), Some(217 | 218)) {
        let mut attributes = BTreeMap::new();
        header_attributes(&header, "acis", &mut attributes);
        attributes.insert("encoding".to_string(), "binary".to_string());
        return Ok(unsupported_result(
            "Spatial ACIS binary stream: this save-format band is not decoded",
            attributes,
        ));
    }
    let start = acis_header::record_stream_start(bytes).ok_or_else(|| {
        CodecError::Malformed("ACIS binary header without a record stream".to_string())
    })?;
    let framed = match acis_header::solved_record_limit(bytes) {
        Some(limit) => sab::frame(bytes, start, limit, 4),
        None => sab::frame_history(bytes, start, bytes.len(), 4),
    };
    let records = framed
        .map_err(|error| CodecError::Malformed(format!("ACIS SAB framing failed: {error}")))?;
    let brep = decode_with_header(
        &records,
        bytes,
        Some(header.clone()),
        "stream",
        IdFormat(FORMAT),
        DecodePurpose::Model,
    );
    let mut attributes = BTreeMap::new();
    header_attributes(&header, "acis", &mut attributes);
    attributes.insert("encoding".to_string(), "binary".to_string());
    build_result(ctx, brep, attributes, &header, None)
}

fn decode_text(ctx: &DecodeContext<'_>, bytes: &[u8]) -> Result<DecodeResult, CodecError> {
    let stream = sat::parse(bytes)
        .map_err(|error| CodecError::Malformed(format!("text stream parse failed: {error}")))?;
    let header = stream.header.as_kernel_header();
    let brep = decode_with_header(
        &stream.records,
        bytes,
        Some(header.clone()),
        "stream",
        IdFormat(FORMAT),
        DecodePurpose::Model,
    );
    let mut attributes = BTreeMap::new();
    let family = match stream.dialect {
        sat::Dialect::Asm => "asm",
        sat::Dialect::Acis => "acis",
    };
    header_attributes(&header, family, &mut attributes);
    attributes.insert("encoding".to_string(), "text".to_string());
    attributes.insert("scale".to_string(), format!("{}", stream.header.scale));
    let dialect = match stream.dialect {
        sat::Dialect::Asm => "End-of-ASM-data",
        sat::Dialect::Acis => "End-of-ACIS-data",
    };
    attributes.insert("terminator".to_string(), dialect.to_string());
    build_result(ctx, brep, attributes, &header, Some(dialect))
}

fn build_result(
    ctx: &DecodeContext<'_>,
    brep: AsmBrep,
    attributes: BTreeMap<String, String>,
    header: &KernelHeader,
    text_dialect: Option<&str>,
) -> Result<DecodeResult, CodecError> {
    let mut ir = CadIr::empty(Units::default());
    ir.source = Some(SourceMeta {
        format: FORMAT.to_string(),
        attributes,
    });
    if let (Some(linear), Some(angular)) = (header.linear, header.angular) {
        ir.tolerances = Tolerances { linear, angular };
    }

    let AsmTransferRemainder {
        body_keys: _,
        face_keys: _,
        unknowns,
        stats,
        annotation_records: _,
    } = transfer_into_ir(ctx, &mut ir, FORMAT, 1, brep)?;

    let geometry_transferred =
        !(ir.model.surfaces.is_empty() && ir.model.points.is_empty() && ir.model.faces.is_empty());
    let mut losses = Vec::new();
    if !geometry_transferred {
        let branch = text_dialect.map_or(String::new(), |dialect| {
            format!(" The stream ends with `{dialect}`.")
        });
        losses.push(SatLossCode::GeometryFramedWithoutCarriers.note(format!(
            "the stream framed but its records decoded no surfaces, points, or faces; its \
             version or branch is outside the ASM decoders' coverage.{branch}"
        )));
    }
    if stats.unknown_surface_faces > 0 {
        losses.push(SatLossCode::GeometryProceduralSurfaceUntyped.note(format!(
            "{} face(s) rest on procedural surface constructions without a decoded carrier",
            stats.unknown_surface_faces
        )));
    }
    let mut coverage = BTreeMap::new();
    coverage.insert("unknown_records".to_string(), unknowns.len());
    coverage.insert(
        "unknown_surface_faces".to_string(),
        stats.unknown_surface_faces,
    );
    let report = DecodeReport {
        format: FORMAT.to_string(),
        container_only: false,
        geometry_transferred,
        coverage,
        transfer_ledger: cadmpeg_ir::report::TransferLedger::default(),
        losses,
        notes: Vec::new(),
    };

    let mut source_fidelity = cadmpeg_ir::SourceFidelity::default();
    source_fidelity
        .attach_native_unknown_records(&mut ir, FORMAT, unknowns)
        .map_err(|error| {
            CodecError::Malformed(format!("unknown-record retention failed: {error}"))
        })?;
    Ok(DecodeResult::new(ir, report, source_fidelity))
}

/// A result for an identified stream the decoders do not cover.
fn unsupported_result(message: &str, attributes: BTreeMap<String, String>) -> DecodeResult {
    let mut ir = CadIr::empty(Units::default());
    ir.source = Some(SourceMeta {
        format: FORMAT.to_string(),
        attributes,
    });
    let report = DecodeReport {
        format: FORMAT.to_string(),
        container_only: false,
        geometry_transferred: false,
        coverage: BTreeMap::new(),
        transfer_ledger: cadmpeg_ir::report::TransferLedger::default(),
        losses: vec![SatLossCode::ContainerAcisSaveFormatUnsupported.note(message)],
        notes: Vec::new(),
    };
    DecodeResult::new(ir, report, cadmpeg_ir::SourceFidelity::default())
}

#[cfg(test)]
mod tests;