# zeux/meshoptimizer architecture

> Architecture diagram of the GitHub repository zeux/meshoptimizer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zeux/meshoptimizer
- Repository: https://github.com/zeux/meshoptimizer

## Overview

- Core Library Algorithms (“src”): This is the heart of the project and contains the implementations of the algorithms (indexing, cache optimization, overdraw reduction, vertex fetch optimization, quantization, compression, simplification, and more).
- Demos and Usage Examples (“demo”): A set of demo applications showing how to use the library (both native C++ demos and HTML/JavaScript demos using WebAssembly).
- glTF Optimization Tool (“gltf”): Contains gltfpack and related submodules used to take the core algorithms and apply them to glTF assets.
- JavaScript and WebAssembly Bindings (“js”): Provides a JS interface so that the optimization routines (especially the decoders) can be used from JavaScript in a web environment.
- External Headers/Libraries (“extern”): These are dependency files or headers (for example, libraries used for parsing or providing utility functions).
- Build & Configuration Infrastructure (e.g. CMakeLists.txt, Makefile, GitHub workflows): Although not strictly runtime components, these are part of the project’s system organization to build, test, and deploy the library.
- Tools (“tools”): Additional executable/test code for benchmarking, adjacency generation, tessellation index generation, meshlet building, and other specialized processing.

##### Relationships and Interactions
- The Core Library sits at the center. Its algorithms are called directly by demo applications, the glTF tool, and also wrapped by the JavaScript layer for online applications.
- The glTF tool (gltfpack) uses the core library to read mesh data, run the optimization pipeline, and then produce optimized glTF assets.
- The JavaScript and WASM layer is built on top of the core library (or decoders) so that browser-based demos and applications can leverage similar performance.
- The “tools” folder contains command-line programs and test harnesses that “drive” the core algorithms for benchmarking or specialized tasks (like generating adjacency or provoking index buffers).
- The external folder makes sure that any third-party or shared header-only libraries are kept isolated, which helps in maintaining consistency and configuration across different build outputs.

##### Architectural Patterns and Design Principles
- Layered Modular Design – The project clearly separates core mesh optimization algorithms from interfaces and demo applications.
- Reusability and Extensibility – The algorithms are written in modular C/C++ code so that only the necessary parts need to be compiled into a given project (or wrapped for WebAssembly), and additional binding layers are provided to extend the library to other languages.
- Cross-platform and Multi-context Integration – With support for native builds, web (JS + WASM) usage and integration in different asset pipelines (like glTF), the design demonstrates flexible integration points.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Input
    RawMesh["Raw Mesh Data"]:::input

    %% Optimization Pipeline Subgraph
    subgraph "Optimization Pipeline"
        p1["Indexing/Vertex Remap"]:::pipeline
        p2["Cache Optimization"]:::pipeline
        p3["Overdraw Optimization"]:::pipeline
        p4["Vertex Fetch Optimization"]:::pipeline
        p5["Quantization & Compression"]:::pipeline
        p6["Simplification"]:::pipeline
    end

    %% Core Library Algorithms Node (src)
    CoreLib["Core Library Algorithms (src)"]:::core

    %% Other Components
    gltfTool["glTF Optimization Tool (gltf)"]:::gltf
    jsBindings["JavaScript & WASM Bindings (js)"]:::jsbinding
    demos["Demos and Usage Examples (demo)"]:::demo
    toolsComp["Tools (tools)"]:::tools
    externComp["External Headers/Libraries (extern)"]:::extern
    buildInfra["Build & Configuration Infrastructure"]:::build

    %% Data Flow Connections
    RawMesh --> p1
    p1 --> p2
    p2 --> p3
    p3 --> p4
    p4 --> p5
    p5 --> p6
    p6 --> CoreLib

    %% Core Library distributes optimized mesh data to consumers
    CoreLib --> gltfTool
    CoreLib --> jsBindings
    CoreLib --> demos
    CoreLib --> toolsComp

    %% External Dependencies & Build System
    externComp --- CoreLib
    buildInfra --- CoreLib
    buildInfra --- gltfTool
    buildInfra --- jsBindings
    buildInfra --- demos
    buildInfra --- toolsComp

    %% Click Events for Component Mapping
    click CoreLib "https://github.com/zeux/meshoptimizer/tree/master/src/"
    click demos "https://github.com/zeux/meshoptimizer/tree/master/demo/"
    click gltfTool "https://github.com/zeux/meshoptimizer/tree/master/gltf/"
    click jsBindings "https://github.com/zeux/meshoptimizer/tree/master/js/"
    click externComp "https://github.com/zeux/meshoptimizer/tree/master/extern/"
    click toolsComp "https://github.com/zeux/meshoptimizer/tree/master/tools/"
    click buildInfra "https://github.com/zeux/meshoptimizer/blob/master/CMakeLists.txt"

    %% Styles
    classDef input fill:#a2d9ff,stroke:#000,stroke-width:1px;
    classDef pipeline fill:#ffffcc,stroke:#000,stroke-width:1px;
    classDef core fill:#ffcccc,stroke:#000,stroke-width:1px;
    classDef gltf fill:#ccffcc,stroke:#000,stroke-width:1px;
    classDef jsbinding fill:#cce5ff,stroke:#000,stroke-width:1px;
    classDef demo fill:#d5f5e3,stroke:#000,stroke-width:1px;
    classDef tools fill:#f9e79f,stroke:#000,stroke-width:1px;
    classDef extern fill:#fadbd8,stroke:#000,stroke-width:1px;
    classDef build fill:#d6eaf8,stroke:#000,stroke-width:1px;
```
