# ahzs645/seamer architecture

> Architecture diagram of the GitHub repository ahzs645/seamer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ahzs645/seamer
- Repository: https://github.com/ahzs645/seamer

## Overview

1. **Overall shape**
   - This is a **SvelteKit** application with a split architecture: a **browser-based studio UI** and a small **server/API layer** for pattern persistence.
   - The distinctive path is **2D sewing pattern → reconstructed avatar → arranged garment → WebGPU cloth simulation → 3D drape render**.

2. **Primary user surface**
   - The main workspace is **`/studio`**, which combines a 2D pattern editor, property/panel UI, and a 3D simulation pane.
   - Supporting informational routes (`/about`, `/docs`, `/software`, `/support-seamer`) are separate from the studio workflow and do not drive the simulation pipeline.

3. **Pattern domain model and persistence**
   - `src/lib/types/pattern.ts` defines the core data shape: points, Bézier paths, pieces, seams, materials, body settings, and 3D arrangement metadata.
   - Pattern state is managed on the client through Svelte stores, with local persistence via `src/lib/stores/localDB.ts`.
   - Server-side persistence/CRUD is exposed under `src/routes/api/patterns/*`, with public and per-id endpoints suggesting a lightweight pattern library backend.

4. **2D pattern editing layer**
   - The 2D editor is assembled from Svelte components such as `PatternCanvas2D`, `BodyPanel`, `LayerPanel`, `PropertyPanel`, `SeamPanel`, `MaterialPanel`, and `DrawingTools`.
   - `src/lib/utils/patternGeometry.ts`, `patternMutations.ts`, `patternValidation.ts`, and `patternImport.ts` handle the transformation from editable pattern data to renderable/validated geometry.
   - This layer is the authoring boundary: it manages user edits, but the 3D simulation consumes the resolved pattern state.

5. **Avatar reconstruction pipeline**
   - The avatar subsystem in `src/lib/model/*` rebuilds a body mesh from measurements using bundled statistical model assets in `static/models/`.
   - `measurements.ts` and `measurementDefs.ts` define body inputs; `avatar.ts`, `skinnedAvatar.ts`, and `avatarController.ts` turn those inputs into a posed mesh.
   - The readme’s flow is important here: **measurements → regression coefficients → 12,302-vertex mesh → skeleton + skinning → posed avatar**.

6. **Garment arrangement and geometry prep**
   - Before simulation, garment pieces are converted into physical geometry by `src/lib/geometry/*` and `src/lib/sim/*`.
   - `boundary.ts`, `triangulate.ts`, and `arrangement.ts` prepare piece boundaries and triangulated cloth panels; `cylinders.ts` and `bodyGrid.ts` fit the garment around tapered body cylinders for initial placement.
   - This is the bridge between 2D drafting and 3D physics: it turns sewing patterns into a mesh that can be simulated around the body.

7. **Cloth simulation core**
   - The simulation stack in `src/lib/sim/` implements a **WebGPU XPBD cloth solver**.
   - `build.ts` assembles particles, stretch/bend constraints, seam constraints, and collision structures; `simulator.ts` coordinates stepping; `webgpu/device.ts`, `engine.ts`, and `shaders.ts` contain the GPU execution path.
   - The solver is intentionally hard-bound to WebGPU for live draping; the repo does not provide a WebGL fallback for cloth physics.

8. **Collision, seams, and physical constraints**
   - The sim includes **body collision**, **seam attraction**, **anisotropic stretch compliance**, and **bend constraints**.
   - The body collision path uses a grid-based broad phase and closest-point push-out/friction handling, while seam constraints keep mirrored or paired pattern edges together.
   - This makes the garment physics dependent on the reconstructed avatar shape, so measurement changes propagate all the way into the drape result.

9. **3D scene and rendering**
   - `src/lib/scene/scene3d.ts` and `materials.ts` define the 3D viewport using **three.js / WebGL** for rendering.
   - The scene renders the avatar, the arranged or simulated garment, lighting, and controls; WebGPU is only used for the physics solve, not for the main scene renderer.
   - The repo explicitly separates “display” rendering from “physics” execution.

10. **Client state, UX overlays, and interaction**
   - Shared UI concerns are encapsulated in components like `Toaster`, `ConfirmDialog`, `ContextMenu`, `FormulaDialog`, `KeyboardShortcuts`, and `WelcomeModal`.
   - This layer coordinates editing, feedback, and workflow state, but it does not own the geometry or physics; it wraps the domain subsystems above.

11. **Static assets and bundled content**
   - Most heavy assets live in `static/`: avatar model binaries/json, HDR environment maps, sample DXFs, templates, textures, and the PWA/service-worker files.
   - The presence of many prebuilt garment templates suggests the app supports both import/open flows and curated starter patterns.

12. **Operational and test boundary**
   - `src/routes/sw.js` and `static/service-worker.js` indicate offline/cache-aware behavior typical of a studio app with large static assets.
   - End-to-end coverage is done with **Playwright** in `e2e/`, including scenarios for importing samples and edge editing, which reflects the two critical workflows: pattern ingestion and interactive garment manipulation.

Main entry points: [`src/routes/studio/+page.svelte`](https://github.com/ahzs645/seamer/blob/main/src/routes/studio/+page.svelte) (Studio), [`src/lib/stores/pattern.ts`](https://github.com/ahzs645/seamer/blob/main/src/lib/stores/pattern.ts) (Pattern store), [`src/lib/utils/patternGeometry.ts`](https://github.com/ahzs645/seamer/blob/main/src/lib/utils/patternGeometry.ts) (Pattern ops), [`src/lib/scene/scene3d.ts`](https://github.com/ahzs645/seamer/blob/main/src/lib/scene/scene3d.ts) (3D scene)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_ui["Studio UI"]
  node_node_studio_page["Studio<br/>route<br/>[+page.svelte]"]
  node_node_shared_ui["UI chrome<br/>components"]
  node_node_pattern_canvas["2D editor"]
  node_node_panels["Panels"]
  node_node_support_routes["Docs routes<br/>content routes<br/>[+page.svelte]"]
end

subgraph group_group_domain["Pattern Domain"]
  node_node_pattern_store["Pattern store<br/>client state<br/>[pattern.ts]"]
  node_node_localdb[("Local DB<br/>persistence<br/>[localDB.ts]")]
  node_node_pattern_types["Pattern model<br/>domain types<br/>[pattern.ts]"]
  node_node_pattern_utils["Pattern ops<br/>geometry utils<br/>[patternGeometry.ts]"]
end

subgraph group_group_avatar["Avatar Pipeline"]
  node_node_model["Avatar model<br/>body reconstruction<br/>[avatar.ts]"]
  node_node_measurements["Measurements<br/>inputs<br/>[measurements.ts]"]
end

subgraph group_group_geometry["Garment Prep"]
  node_node_geometry["Garment prep<br/>geometry prep<br/>[arrangement.ts]"]
  node_node_bodygrid["Body grid<br/>initial fit<br/>[bodyGrid.ts]"]
end

subgraph group_group_sim["Cloth Sim"]
  node_node_simulator["Simulator<br/>orchestrator<br/>[simulator.ts]"]
  node_node_build["Solver build<br/>xpbd assembly<br/>[build.ts]"]
  node_node_webgpu["WebGPU engine<br/>gpu runtime<br/>[engine.ts]"]
end

subgraph group_group_render["3D Scene"]
  node_node_scene3d["3D scene<br/>viewport<br/>[scene3d.ts]"]
  node_node_scene_materials["Scene mats<br/>render materials<br/>[materials.ts]"]
end

subgraph group_group_api["API"]
  node_node_api_patterns["Pattern API<br/>server routes<br/>[+server.ts]"]
end

subgraph group_group_assets["Static Assets"]
  node_node_assets_model[("Model assets<br/>static model data<br/>[base_model.json]")]
  node_node_templates[("Templates<br/>starter content<br/>[black-dress.json]")]
end

node_node_studio_page -->|"hosts"| node_node_pattern_canvas
node_node_studio_page -->|"hosts"| node_node_panels
node_node_studio_page -->|"hosts"| node_node_scene3d
node_node_shared_ui -->|"wraps"| node_node_studio_page
node_node_pattern_canvas -->|"reads/writes"| node_node_pattern_store
node_node_panels -->|"reads/writes"| node_node_pattern_store
node_node_pattern_store -->|"persists"| node_node_localdb
node_node_pattern_store -->|"uses"| node_node_pattern_types
node_node_pattern_utils -->|"transforms"| node_node_pattern_types
node_node_pattern_utils -->|"imports"| node_node_templates
node_node_pattern_canvas -->|"uses"| node_node_pattern_utils
node_node_measurements -->|"feeds"| node_node_model
node_node_assets_model -->|"backs"| node_node_model
node_node_model -->|"renders"| node_node_scene3d
node_node_pattern_utils -->|"prepares"| node_node_geometry
node_node_geometry -->|"initializes"| node_node_bodygrid
node_node_geometry -->|"feeds"| node_node_simulator
node_node_build -->|"executes on"| node_node_webgpu
node_node_simulator -->|"drives"| node_node_build
node_node_simulator -->|"updates"| node_node_scene3d
node_node_scene3d -->|"uses"| node_node_scene_materials
node_node_api_patterns -->|"syncs"| node_node_pattern_store
node_node_api_patterns -->|"serializes"| node_node_pattern_types
node_node_support_routes -->|"separate"| node_node_studio_page

click node_node_studio_page "https://github.com/ahzs645/seamer/blob/main/src/routes/studio/+page.svelte"
click node_node_shared_ui "https://github.com/ahzs645/seamer/tree/main/src/lib/components"
click node_node_pattern_canvas "https://github.com/ahzs645/seamer/blob/main/src/lib/components/PatternCanvas2D.svelte"
click node_node_panels "https://github.com/ahzs645/seamer/tree/main/src/lib/components"
click node_node_pattern_store "https://github.com/ahzs645/seamer/blob/main/src/lib/stores/pattern.ts"
click node_node_localdb "https://github.com/ahzs645/seamer/blob/main/src/lib/stores/localDB.ts"
click node_node_pattern_types "https://github.com/ahzs645/seamer/blob/main/src/lib/types/pattern.ts"
click node_node_pattern_utils "https://github.com/ahzs645/seamer/blob/main/src/lib/utils/patternGeometry.ts"
click node_node_model "https://github.com/ahzs645/seamer/blob/main/src/lib/model/avatar.ts"
click node_node_measurements "https://github.com/ahzs645/seamer/blob/main/src/lib/model/measurements.ts"
click node_node_assets_model "https://github.com/ahzs645/seamer/blob/main/static/models/base_model.json"
click node_node_geometry "https://github.com/ahzs645/seamer/blob/main/src/lib/geometry/arrangement.ts"
click node_node_bodygrid "https://github.com/ahzs645/seamer/blob/main/src/lib/sim/bodyGrid.ts"
click node_node_simulator "https://github.com/ahzs645/seamer/blob/main/src/lib/sim/simulator.ts"
click node_node_build "https://github.com/ahzs645/seamer/blob/main/src/lib/sim/build.ts"
click node_node_webgpu "https://github.com/ahzs645/seamer/blob/main/src/lib/sim/webgpu/engine.ts"
click node_node_scene3d "https://github.com/ahzs645/seamer/blob/main/src/lib/scene/scene3d.ts"
click node_node_scene_materials "https://github.com/ahzs645/seamer/blob/main/src/lib/scene/materials.ts"
click node_node_api_patterns "https://github.com/ahzs645/seamer/blob/main/src/routes/api/patterns/+server.ts"
click node_node_support_routes "https://github.com/ahzs645/seamer/blob/main/src/routes/docs/+page.svelte"
click node_node_templates "https://github.com/ahzs645/seamer/blob/main/static/templates/black-dress.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_studio_page,node_node_shared_ui,node_node_pattern_canvas,node_node_panels,node_node_support_routes,node_node_api_patterns toneBlue
class node_node_pattern_store,node_node_localdb,node_node_pattern_types,node_node_pattern_utils,node_node_assets_model,node_node_templates toneAmber
class node_node_model,node_node_measurements toneMint
class node_node_geometry,node_node_bodygrid toneRose
class node_node_simulator,node_node_build,node_node_webgpu toneIndigo
class node_node_scene3d,node_node_scene_materials toneTeal
```

## Components

### Studio UI

Browser workspace for drafting, panels, and 3D preview.

- [Studio](https://github.com/ahzs645/seamer/blob/main/src/routes/studio/+page.svelte): route · Main workspace route combining editor, panels, and simulation viewport. · `src/routes/studio/+page.svelte`
- [UI chrome](https://github.com/ahzs645/seamer/tree/main/src/lib/components): components · Header, toolbar, overlays, dialogs, and workflow helpers. · `src/lib/components`
- [2D editor](https://github.com/ahzs645/seamer/blob/main/src/lib/components/PatternCanvas2D.svelte): editor · Pattern canvas and editing tools for drafting and annotation. · `src/lib/components/PatternCanvas2D.svelte`
- [Panels](https://github.com/ahzs645/seamer/tree/main/src/lib/components): editor panels · Body, layer, property, seam, material, and object panels. · `src/lib/components`
- [Docs routes](https://github.com/ahzs645/seamer/blob/main/src/routes/docs/+page.svelte): content routes · Informational routes for about, docs, software, and support pages. · `src/routes/docs/+page.svelte`

### Pattern Domain

Pattern data model, editing, validation, and persistence on the client.

- [Pattern store](https://github.com/ahzs645/seamer/blob/main/src/lib/stores/pattern.ts): client state · Svelte store state and local database persistence for patterns. · `src/lib/stores/pattern.ts`
- [Local DB](https://github.com/ahzs645/seamer/blob/main/src/lib/stores/localDB.ts): persistence · Browser-side persistence layer for saved pattern state. · `src/lib/stores/localDB.ts`
- [Pattern model](https://github.com/ahzs645/seamer/blob/main/src/lib/types/pattern.ts): domain types · Core data shapes for points, seams, materials, bodies, and arrangement metadata. · `src/lib/types/pattern.ts`
- [Pattern ops](https://github.com/ahzs645/seamer/blob/main/src/lib/utils/patternGeometry.ts): geometry utils · Mutation, validation, import, export, and geometry conversion helpers. · `src/lib/utils/patternGeometry.ts`

### Avatar Pipeline

Measurement-driven body reconstruction and posed avatar generation.

- [Avatar model](https://github.com/ahzs645/seamer/blob/main/src/lib/model/avatar.ts): body reconstruction · Measurement-driven body reconstruction and posed mesh pipeline. · `src/lib/model/avatar.ts`
- [Measurements](https://github.com/ahzs645/seamer/blob/main/src/lib/model/measurements.ts): inputs · Measurement definitions and measurement data used to drive body fitting. · `src/lib/model/measurements.ts`

### Garment Prep

Converts 2D patterns into simulation-ready cloth geometry.

- [Garment prep](https://github.com/ahzs645/seamer/blob/main/src/lib/geometry/arrangement.ts): geometry prep · Boundary extraction, triangulation, and initial garment arrangement around the body. · `src/lib/geometry/arrangement.ts`
- [Body grid](https://github.com/ahzs645/seamer/blob/main/src/lib/sim/bodyGrid.ts): initial fit · Cylindrical fitting and grid-based garment placement helpers. · `src/lib/sim/bodyGrid.ts`

### Cloth Sim

WebGPU XPBD cloth solver and its orchestration layers.

- [Simulator](https://github.com/ahzs645/seamer/blob/main/src/lib/sim/simulator.ts): orchestrator · Coordinates cloth stepping, constraints, and collision-aware simulation updates. · `src/lib/sim/simulator.ts`
- [Solver build](https://github.com/ahzs645/seamer/blob/main/src/lib/sim/build.ts): xpbd assembly · Builds particles, stretch/bend constraints, seams, and collision structures. · `src/lib/sim/build.ts`
- [WebGPU engine](https://github.com/ahzs645/seamer/blob/main/src/lib/sim/webgpu/engine.ts): gpu runtime · Device, execution engine, and shaders for cloth physics on the GPU. · `src/lib/sim/webgpu/engine.ts`

### 3D Scene

Display renderer and materials for the studio viewport.

- [3D scene](https://github.com/ahzs645/seamer/blob/main/src/lib/scene/scene3d.ts): viewport · Three.js/WebGL scene for avatar, cloth, lighting, and interaction. · `src/lib/scene/scene3d.ts`
- [Scene mats](https://github.com/ahzs645/seamer/blob/main/src/lib/scene/materials.ts): render materials · Viewport material definitions and rendering configuration. · `src/lib/scene/materials.ts`

### API

Server endpoints for pattern library CRUD and public access.

- [Pattern API](https://github.com/ahzs645/seamer/blob/main/src/routes/api/patterns/+server.ts): server routes · Pattern library endpoints for list, create, update, delete, and public access. · `src/routes/api/patterns/+server.ts`

### Static Assets

Bundled models, templates, samples, and environment maps.

- [Model assets](https://github.com/ahzs645/seamer/blob/main/static/models/base_model.json): static model data · Bundled coefficient, index, and skinning assets for avatar reconstruction. · `static/models/base_model.json`
- [Templates](https://github.com/ahzs645/seamer/blob/main/static/templates/black-dress.json): starter content · Curated pattern templates and sample DXF imports for onboarding and reuse. · `static/templates/black-dress.json`

## Connections

- Studio → 2D editor: hosts · The studio page embeds the drafting canvas.
- Studio → Panels: hosts · The studio page lays out the editing panels.
- Studio → 3D scene: hosts · The studio page includes the 3D viewport.
- UI chrome → Studio: wraps · Shared chrome and dialogs support the studio flow.
- 2D editor → Pattern store: reads/writes · Editor actions mutate the client-side pattern state.
- Panels → Pattern store: reads/writes · Panels edit the same pattern state as the canvas.
- Pattern store → Local DB: persists · Pattern state is saved locally for reload and continuity.
- Pattern store → Pattern model: uses · Store state follows the shared pattern domain model.
- Pattern ops → Pattern model: transforms · Utilities mutate, validate, and derive geometry from pattern data.
- Pattern ops → Templates: imports · Pattern utilities consume template and sample content.
- 2D editor → Pattern ops: uses · Interactive editing relies on geometry and validation helpers.
- Measurements → Avatar model: feeds · Measurement inputs drive body reconstruction.
- Model assets → Avatar model: backs · Bundled statistical assets support avatar generation.
- Avatar model → 3D scene: renders · The reconstructed avatar is shown in the viewport.
- Pattern ops → Garment prep: prepares · Resolved pattern data becomes garment geometry.
- Garment prep → Body grid: initializes · Geometry preparation uses body-fit placement helpers.
- Garment prep → Simulator: feeds · Prepared cloth geometry enters the simulation pipeline.
- Solver build → WebGPU engine: executes on · Solver assembly is run through the WebGPU path.
- Simulator → Solver build: drives · The simulator coordinates constraint and collision setup.
- Simulator → 3D scene: updates · Simulation results are visualized in the 3D scene.
- 3D scene → Scene mats: uses · Viewport rendering depends on material definitions.
- Pattern API → Pattern store: syncs · Server-side pattern CRUD complements client persistence.
- Pattern API → Pattern model: serializes · API payloads follow the shared pattern schema.
- Docs routes → Studio: separate · Informational pages are distinct from the studio workflow.
