2026-04-12 09:09:32 +10:00
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* This file is part of IfcOpenShell. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the Lesser GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3.0 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* Lesser GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the Lesser GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
2026-04-12 19:53:06 +10:00
|
|
|
// NOTE: Sidecar format v3 is being rewritten to v4 (instanced geometry layout).
|
|
|
|
|
// During the instancing rewrite (Commit A) the cache is a no-op: reads always
|
|
|
|
|
// miss and writes always succeed without producing a file. Commit B will
|
|
|
|
|
// re-introduce the on-disk format with MeshInfo + InstanceGpu sections.
|
|
|
|
|
|
2026-04-12 09:09:32 +10:00
|
|
|
#ifndef SIDECARCACHE_H
|
|
|
|
|
#define SIDECARCACHE_H
|
|
|
|
|
|
2026-04-12 19:53:06 +10:00
|
|
|
#include "InstancedGeometry.h"
|
2026-04-12 09:09:32 +10:00
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <optional>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2026-04-12 19:53:06 +10:00
|
|
|
#include <memory>
|
2026-04-12 09:09:32 +10:00
|
|
|
|
|
|
|
|
static constexpr uint32_t SIDECAR_MAGIC = 0x49465657; // "IFVW"
|
2026-04-13 18:31:43 +10:00
|
|
|
// v5 = MeshInfo extended with lod1_ebo_byte_offset + lod1_index_count (56 B).
|
|
|
|
|
// sd.indices may contain an appended LOD1 index slice for each mesh
|
|
|
|
|
// where meshoptimizer decimation produced useful output.
|
2026-04-14 21:54:15 +10:00
|
|
|
// v6 = VBO vertices quantized to 16 B/vertex (pos u16x3 + normal oct i16x2 +
|
|
|
|
|
// color u8x4). Dequant basis is per-mesh MeshInfo.local_aabb_min/max.
|
2026-04-20 12:26:29 +10:00
|
|
|
// v7 = VBO vertices shrunk to 12 B/vertex (normal oct i8x2 replaces i16x2,
|
|
|
|
|
// eliminating 2-byte pad + saving 2 bytes on normal).
|
2026-04-22 11:58:04 +10:00
|
|
|
// v8 = source_file_size field dropped from header. Sidecar is keyed purely
|
|
|
|
|
// on path stem (foo.ifc and foo.ifcdb/ both map to foo.ifcview) so the
|
|
|
|
|
// same cache serves either source format. Staleness is user-managed
|
|
|
|
|
// (delete the sidecar to force a rebuild).
|
2026-04-28 19:40:00 +10:00
|
|
|
// v9 = unused `reserved` field dropped from header (16 B -> 12 B).
|
2026-05-01 20:41:25 +10:00
|
|
|
// v10 = InstanceCpu gains placement_transformation[16] alongside transform[16]
|
|
|
|
|
// — record grew from 104 B to 168 B. placement_transformation is the
|
|
|
|
|
// raw streamer output; transform is the composed FederatedFalseOrigin ·
|
|
|
|
|
// ModelTransformation · CoordinateOperation · placement_transformation
|
|
|
|
|
// result. Sidecar serialises both; on load the transform is recomputed
|
|
|
|
|
// from placement_transformation + the ViewportWindow's current stage
|
|
|
|
|
// matrices, so v10 sidecars are reusable across .ifcfeds.
|
2026-05-02 19:08:45 +10:00
|
|
|
// v11 = SidecarData gains a per-model CoordinateOperation cache:
|
|
|
|
|
// coordinate_operation_meters[16] (column-major), unit scales, and a
|
|
|
|
|
// has_coordinate_operation flag. Lets sidecar-loaded models apply
|
|
|
|
|
// georef without re-parsing the IFC source. Edits to the IFC's
|
|
|
|
|
// IfcMapConversion do NOT invalidate the sidecar — delete the
|
|
|
|
|
// .ifcview manually if you change the source's georef parameters.
|
2026-05-19 17:31:53 +10:00
|
|
|
// v12 = InstanceCpu::placement_transformation is double[16], and
|
|
|
|
|
// InstanceChunk carries the streamer placement as double[16]. This keeps
|
|
|
|
|
// large IFC placements exact until CoordinateOperation / FederatedFalseOrigin
|
|
|
|
|
// composition has reduced them to viewport-local float-sized values.
|
2026-05-19 18:58:17 +10:00
|
|
|
// v13 = Map unit scale in cached ModelGeoref is derived from
|
|
|
|
|
// IfcMapConversion.Scale, not IfcProjectedCRS.MapUnit.
|
2026-06-30 21:22:47 +10:00
|
|
|
// v14 = Geometry is laid out in streaming-chunk order (SidecarLayout) and a
|
|
|
|
|
// chunk table-of-contents (`chunks`) is appended. The loader builds its
|
|
|
|
|
// chunks from the TOC instead of re-deriving the Morton/greedy plan, so
|
|
|
|
|
// each chunk is one CONTIGUOUS byte range — fixing network read
|
|
|
|
|
// amplification. The plan can't be re-derived at load because the float
|
|
|
|
|
// Morton quantisation isn't bit-identical across toolchains (x86 baker vs
|
|
|
|
|
// wasm loader), so it must be baked in. No back-compat: v13 sidecars are
|
|
|
|
|
// rejected (regenerate them).
|
2026-07-01 08:01:15 +10:00
|
|
|
// v15 = The post-index metadata is split into a render-CRITICAL block (meshes,
|
|
|
|
|
// instances, georef, chunk TOC) followed by a DEFERRED block (elements +
|
|
|
|
|
// string_table — the IFC element tree, used for UI/picking, never for
|
|
|
|
|
// rendering), with the critical block's byte length written just after
|
|
|
|
|
// the index section. The web loader reads only the critical block before
|
|
|
|
|
// painting, so first geometry no longer waits on the property data; the
|
|
|
|
|
// deferred block is fetched lazily (or skipped where unused). Desktop
|
|
|
|
|
// reads both. No back-compat: regenerate sidecars.
|
|
|
|
|
static constexpr uint32_t SIDECAR_VERSION = 15;
|
2026-04-12 09:09:32 +10:00
|
|
|
static constexpr uint32_t SIDECAR_ENDIAN = 0x01020304;
|
|
|
|
|
|
2026-06-30 21:22:47 +10:00
|
|
|
// Chunk table-of-contents entry (v14+). A chunk is a CONTIGUOUS range of
|
|
|
|
|
// meshes in the (reordered) meshes array — and therefore a contiguous span of
|
|
|
|
|
// vertex + index bytes, since the geometry is laid out in chunk order. The
|
|
|
|
|
// loader builds chunk `i` from meshes [first_mesh, first_mesh + mesh_count).
|
|
|
|
|
struct SidecarChunk {
|
|
|
|
|
uint32_t first_mesh;
|
|
|
|
|
uint32_t mesh_count;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-12 19:53:06 +10:00
|
|
|
// Fixed-size element record. Strings are stored as (offset, length) pairs
|
|
|
|
|
// into a separate string table.
|
2026-04-12 09:09:32 +10:00
|
|
|
struct PackedElementInfo {
|
|
|
|
|
uint32_t object_id;
|
|
|
|
|
uint32_t model_id;
|
|
|
|
|
int32_t ifc_id;
|
|
|
|
|
int32_t parent_id;
|
|
|
|
|
uint32_t guid_offset;
|
|
|
|
|
uint32_t guid_length;
|
|
|
|
|
uint32_t name_offset;
|
|
|
|
|
uint32_t name_length;
|
|
|
|
|
uint32_t type_offset;
|
|
|
|
|
uint32_t type_length;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-12 19:53:06 +10:00
|
|
|
// Everything needed to display an already-tessellated model without
|
2026-04-14 21:54:15 +10:00
|
|
|
// re-running the iterator. v6 schema: instanced + quantized geometry.
|
2026-04-12 09:09:32 +10:00
|
|
|
struct SidecarData {
|
2026-04-14 21:54:15 +10:00
|
|
|
// Per-model GPU geometry (local coords). Raw VBO bytes at the
|
2026-04-20 12:26:29 +10:00
|
|
|
// INSTANCED_VERTEX_STRIDE_BYTES layout (12 B/vertex as of v7).
|
2026-04-14 21:54:15 +10:00
|
|
|
std::vector<uint8_t> vertices;
|
2026-04-12 19:53:06 +10:00
|
|
|
std::vector<uint32_t> indices;
|
2026-04-12 09:09:32 +10:00
|
|
|
|
2026-04-12 19:53:06 +10:00
|
|
|
// Mesh dictionary and per-instance data.
|
|
|
|
|
std::vector<MeshInfo> meshes; // indexed by local_mesh_id
|
|
|
|
|
std::vector<InstanceCpu> instances; // sorted by mesh_id
|
2026-04-12 09:09:32 +10:00
|
|
|
|
2026-05-02 19:08:45 +10:00
|
|
|
// CoordinateOperation cache (v11+). Mirrors ModelGeoref so a sidecar
|
|
|
|
|
// load can apply georef without re-parsing the IFC source.
|
|
|
|
|
// has_coordinate_operation == 0 means the model has no
|
|
|
|
|
// IfcMapConversion; the matrix is then the identity placeholder.
|
|
|
|
|
double coordinate_operation_meters[16] = {
|
|
|
|
|
1, 0, 0, 0,
|
|
|
|
|
0, 1, 0, 0,
|
|
|
|
|
0, 0, 1, 0,
|
|
|
|
|
0, 0, 0, 1 };
|
|
|
|
|
double project_length_to_meters = 1.0;
|
|
|
|
|
double map_unit_to_meters = 1.0;
|
|
|
|
|
uint32_t has_coordinate_operation = 0;
|
|
|
|
|
|
2026-04-12 19:53:06 +10:00
|
|
|
// Element tree metadata.
|
2026-04-12 09:09:32 +10:00
|
|
|
std::vector<PackedElementInfo> elements;
|
2026-04-12 19:53:06 +10:00
|
|
|
std::string string_table;
|
2026-06-30 21:22:47 +10:00
|
|
|
|
|
|
|
|
// Streaming chunk TOC. Always written on disk (v14); geometry is laid out
|
|
|
|
|
// in this chunk order (see SidecarLayout) so each chunk is one contiguous
|
|
|
|
|
// range and the loader builds chunks directly from it. Stays empty only for
|
|
|
|
|
// in-memory direct loads (finalizeModel), which don't stream and fall back
|
|
|
|
|
// to deriving the plan.
|
|
|
|
|
std::vector<SidecarChunk> chunks;
|
2026-04-12 09:09:32 +10:00
|
|
|
};
|
|
|
|
|
|
2026-04-22 11:58:04 +10:00
|
|
|
// Sidecar is keyed on the path stem: foo.ifc and foo.ifcdb/ both resolve to
|
|
|
|
|
// foo.ifcview alongside the source. No staleness check — callers delete the
|
|
|
|
|
// file to invalidate.
|
|
|
|
|
bool writeSidecar(const std::string& ifc_path, const SidecarData& data);
|
2026-04-12 09:09:32 +10:00
|
|
|
|
2026-04-22 11:58:04 +10:00
|
|
|
std::optional<SidecarData> readSidecar(const std::string& ifc_path);
|
2026-04-12 09:09:32 +10:00
|
|
|
|
|
|
|
|
#endif // SIDECARCACHE_H
|