> ## Documentation Index
> Fetch the complete documentation index at: https://khaos-ml.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 3D geometry

> Parts and scans: duplicates, auto-tagging, retrieval.

## Find duplicate and near-duplicate parts

**Problem:** a CAD library has grown over the years and holds many near-copies.
**Uses:** Khaos and scikit-learn.

```python theme={null}
import khaos
import numpy as np
from pathlib import Path
from sklearn.neighbors import NearestNeighbors

embed = khaos.load("geo_mesh_embedding")

paths = sorted(Path("library").glob("*.stl"))
vectors = np.stack([embed(p).vector for p in paths])

nn = NearestNeighbors(n_neighbors=2, metric="cosine").fit(vectors)
dist, idx = nn.kneighbors(vectors)

for i, (d, j) in enumerate(zip(dist[:, 1], idx[:, 1])):
    if d < 0.02:
        print(paths[i].name, "~", paths[j].name, round(d, 3))
# bracket_v2.stl ~ bracket_v2_copy.stl 0.004
# flange_80.stl ~ flange_80_rev.stl 0.011
```

## Auto-tag incoming parts

**Problem:** sort uploaded CAD files into folders by part type. **Uses:** Khaos and
the standard library.

```python theme={null}
import khaos
import shutil
from pathlib import Path

classify = khaos.load("geo_part_classifier")

for path in Path("incoming").glob("*.step"):
    part = classify(path)
    if max(part.scores.values()) < 0.6:
        part.label = "needs_review"
    dest = Path("sorted") / part.label
    dest.mkdir(parents=True, exist_ok=True)
    shutil.move(path, dest / path.name)
    print(path.name, "->", part.label)
# housing_a.step -> housing
# gear_44t.step -> gear
# odd_part.step -> needs_review
```

## Check that a retrieved part fits

**Problem:** retrieve similar parts, then filter by real geometry. **Uses:** Khaos
and trimesh.

```python theme={null}
import khaos
import trimesh

embed = khaos.load("geo_mesh_embedding")
query = embed("target.stl").vector

candidates = nearest_parts(query, k=20)   # from your vector index

target = trimesh.load("target.stl")
fits = []
for path in candidates:
    mesh = trimesh.load(path)
    if mesh.volume < 1.2 * target.volume and mesh.bounds[1].max() <= 80:
        fits.append(path)
print(len(candidates), "->", len(fits))
# 20 -> 6
```

## Match a site scan to a known site

**Problem:** work out which known location a new point cloud belongs to. **Uses:**
Khaos and laspy.

```python theme={null}
import khaos
import laspy

embed = khaos.load("geo_pointcloud_embedding")

scan = embed("new_scan.las").vector
las = laspy.read("new_scan.las")
print(len(las.points), "points")

best = max(known_sites, key=lambda s: scan @ s.vector)   # your site registry
print(best.name)
# 412880 points
# north_yard
```

## Inputs and outputs for each model

These are real outputs of the v1 models, run through their exported ONNX files on held-out examples. The shapes listed are the exact ONNX inputs and outputs; `batch` and other named sizes are dynamic.

### `geo_part_classifier`

512 points sampled from a CAD model, centered and scaled to the unit sphere, shape (1, 3, 512), in. Class scores out; the top three are shown with softmax probabilities.

**Model inputs:** `points float32 [batch, 3, 512]`\
**Model outputs:** `logits float32 [batch, 40]`

**Input**

```python theme={null}
points.shape = (1, 3, 512)   # xyz of a CAD model
```

**Output**

```json theme={null}
{
  "top_3": [
    {
      "class": "cone",
      "probability": 0.7
    },
    {
      "class": "tent",
      "probability": 0.164
    },
    {
      "class": "bowl",
      "probability": 0.067
    }
  ],
  "true_class": "cone"
}
```

### `geo_mesh_embedding`

A point set in, one unit-length 256-dimensional vector out, so the cosine similarity of two shapes is a dot product.

**Model inputs:** `points float32 [batch, 6, 512]`\
**Model outputs:** `vector float32 [batch, 256]`

**Input**

```python theme={null}
points.shape = (1, 6, 512)   # xyz and surface normals sampled from a mesh
```

**Output**

```json theme={null}
{
  "vector": [
    -0.048,
    0.094,
    0.053,
    -0.041,
    0.025,
    0.087,
    "..."
  ],
  "vector_norm": 1.0,
  "query_class": "monitor",
  "cosine_to_same_class (monitor)": 0.388,
  "cosine_to_different_class (keyboard)": 0.201,
  "over_150_held_out_shapes": {
    "mean_cosine_same_class": 0.639,
    "mean_cosine_different_class": 0.132
  }
}
```

### `geo_pointcloud_embedding`

A point set in, one unit-length 256-dimensional vector out, so the cosine similarity of two shapes is a dot product.

**Model inputs:** `points float32 [batch, 3, 512]`\
**Model outputs:** `vector float32 [batch, 256]`

**Input**

```python theme={null}
points.shape = (1, 3, 512)   # xyz of a scanned object
```

**Output**

```json theme={null}
{
  "vector": [
    -0.05,
    -0.064,
    0.073,
    0.077,
    -0.029,
    0.023,
    "..."
  ],
  "vector_norm": 1.0,
  "query_class": "desk",
  "cosine_to_same_class (desk)": 0.971,
  "cosine_to_different_class (cabinet)": 0.953,
  "over_150_held_out_shapes": {
    "mean_cosine_same_class": 0.724,
    "mean_cosine_different_class": 0.608
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.