diff --git a/.claude/launch.json b/.claude/launch.json index 55a220fb..ef306c52 100644 --- a/.claude/launch.json +++ b/.claude/launch.json @@ -2,14 +2,22 @@ "version": "0.0.1", "configurations": [ { - "name": "backend-migrated", - "runtimeExecutable": "C:\\Users\\EFESTWIN\\miniconda3\\envs\\csc\\python.exe", + "name": "backend-local", + "runtimeExecutable": "cmd", "runtimeArgs": [ - "-m", - "invoke", - "dev-migrated" + "/c", + "set CSC_PUBLIC_API_URL=http://127.0.0.1:8791&& scripts\\dev\\run_csc_python.cmd -u scripts/dev/serve_dump.py --dump latest --assets .dev/assets-261012 --port 8791 --mongo-port 27791" ], - "port": 8000 + "port": 8791 + }, + { + "name": "frontend-local", + "runtimeExecutable": "cmd", + "runtimeArgs": [ + "/c", + "set FASTAPI_URL=http://127.0.0.1:8791&& set NEXTAUTH_URL=http://localhost:3000&& npm --prefix src/frontend run dev" + ], + "port": 3000 }, { "name": "frontend", @@ -23,58 +31,29 @@ "port": 3000 }, { - "name": "frontend-migrated", - "runtimeExecutable": "cmd", - "runtimeArgs": [ - "/c", - "set FASTAPI_URL=http://127.0.0.1:8000&& npm --prefix src/frontend run dev" - ], - "port": 3000 - }, - { - "name": "frontend-migrated-3100", + "name": "backend-migrated", "runtimeExecutable": "cmd", "runtimeArgs": [ "/c", - "set FASTAPI_URL=http://127.0.0.1:8000&& set NEXTAUTH_URL=http://127.0.0.1:3100&& npm --prefix src/frontend run dev -- -p 3100" + "scripts\\dev\\run_csc_python.cmd -m invoke dev-local --migrate --dump 261001" ], - "port": 3100, - "url": "http://127.0.0.1:3100" + "port": 8000 }, { - "name": "frontend-migrated-8010", + "name": "frontend-migrated", "runtimeExecutable": "cmd", "runtimeArgs": [ "/c", - "set FASTAPI_URL=http://127.0.0.1:8010&& set NEXTAUTH_URL=http://127.0.0.1:3100&& set NEXT_DIST_DIR=.next-3100&& npm --prefix src/frontend run dev -- -p 3100" - ], - "port": 3100, - "url": "http://127.0.0.1:3100" - }, - { - "name": "backend-261012", - "runtimeExecutable": "C:/Users/EFESTWIN/miniconda3/envs/csc/python.exe", - "runtimeArgs": [ - "-u", - "scripts/dev/serve_migrated.py", - "--dump", - "261012", - "--no-migrate", - "--assets", - ".dev/assets-261012", - "--port", - "8791", - "--mongo-port", - "27791" + "set FASTAPI_URL=http://127.0.0.1:8000&& npm --prefix src/frontend run dev" ], - "port": 8791 + "port": 3000 }, { - "name": "frontend-261012", + "name": "frontend-migrated-3100", "runtimeExecutable": "cmd", "runtimeArgs": [ "/c", - "set FASTAPI_URL=http://127.0.0.1:8791&& set NEXTAUTH_URL=http://127.0.0.1:3100&& set NEXT_DIST_DIR=.next-3100&& npm --prefix src/frontend run dev -- -p 3100" + "set FASTAPI_URL=http://127.0.0.1:8000&& set NEXTAUTH_URL=http://127.0.0.1:3100&& npm --prefix src/frontend run dev -- -p 3100" ], "port": 3100, "url": "http://127.0.0.1:3100" diff --git a/.gitattributes b/.gitattributes index 17a66083..0cc2affe 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,3 +2,4 @@ *.sh text eol=lf uberspaceconfig/crontab/crontab.example text eol=lf uberspaceconfig/html/** text eol=lf +*.cmd text eol=crlf diff --git a/.zenodo.json b/.zenodo.json index 2b6c65fa..d9896f3a 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,8 +1,8 @@ { - "title": "Catalog of Second Chances (CSC) - Digital Database and Corresponding Interfaces for ReUse of Architectural Components (v0.6.3.0)", - "version": "0.6.3.0", + "title": "Catalog of Second Chances (CSC) - Digital Database and Corresponding Interfaces for ReUse of Architectural Components (v0.6.3.1)", + "version": "0.6.3.1", "upload_type": "software", - "description": "
The Catalog of Second Chances (CSC) is a prototypical digital component database. It catalogs digital representations of uniquely identifiable architectural components for reuse. Furthermore, it provides corresponding interfaces and tools to leverage them via a backend REST-API, a web-frontend as well as a interface for direct interaction within Rhino Grasshopper.
GET /auth/token-check tests one. The ground for the MCP layer and the Grasshopper device sign-in of this release.https://<api host>/mcp with nine read-only tools (search, components, versions, evidence, datasets, timeline, similar components). Add it as a custom connector in Claude; a browser window opens the consent page on the website, where an admin allows read access to the catalog as they see it in the web app. The connection renews itself for up to 90 days and can be revoked in the token list. Answers carry no e-mail addresses and mark everything that is not public.SignIn button (Windows). It opens the browser, you allow \"Grasshopper on this computer\" on the website, and the sign-in is stored for your Windows user (encrypted with DPAPI, one per server), outside every Grasshopper file. Later runs sign in by themselves and renew their token; SignOut ends the sign-in on the computer and the server. Each sign-in appears in the token list in Settings, where it can be revoked.SignIn and SignOut; Refresh is gone, because the sign-in renews itself. Connected Username and Password still win and stay the way to sign in on macOS; with them the component signs in again by itself when its 6-hour token runs out. Update the components with CSC_Update.SignIn again.CSC_PAT_ROLES (optional, default admin) names the roles that may create access tokens.access_tokens, oauth_clients, oauth_requests, oauth_codes and oauth_used_refresh need the same database rights as the others; their indexes are created at the start.CSC_PUBLIC_API_URL=https://<api host> (no path) in the backend environment: it turns on /mcp and the OAuth server and is the address the connector is given. It also becomes the base of the links in the JSON-LD export (if it was unset so far, those links took the request's own address). After the restart, check that https://<api host>/.well-known/oauth-protected-resource/mcp answers, then add the connector once in claude.ai.mcp) and its dependencies (cryptography among them)./mcp and the token renewal keep working; a code issued just before the switch can still be exchanged once. New consents are refused until the flag is gone.Full history: CHANGELOG.md
", + "description": "The Catalog of Second Chances (CSC) is a prototypical digital component database. It catalogs digital representations of uniquely identifiable architectural components for reuse. Furthermore, it provides corresponding interfaces and tools to leverage them via a backend REST-API, a web-frontend as well as a interface for direct interaction within Rhino Grasshopper.
A hardening release after 0.6.3.0.
invoke release-prep sets the CHANGELOG date, writes .zenodo.json and runs the version checks in one step; bump-version --gh leaves the Session component alone when its client version already matches. The local launch entries and the old scripts work without a user path; the local server script is now scripts/dev/serve_dump.py (invoke dev-local; invoke dev-migrated still works and says so), migrating an old 0.5 dump only with --migrate; it writes .dev/dev-local-credentials-<port>.txt and .dev/local-<port>.json.Retry-After); valid tokens are never counted.Full history: CHANGELOG.md
", "creators": [ { "name": "Eschenbach, Max Benjamin", diff --git a/CHANGELOG.md b/CHANGELOG.md index ee2f04fc..c682bc0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,39 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.6.3.1] - 2026-10-21 + +A hardening release after 0.6.3.0. + +### Changed + +- **Viewer:** mesh edges stay crisp over the faces instead of breaking up at some angles; close + zooms no longer cut a piece open, and small pieces open filling the view. A version switch + while the mesh or the point cloud is Off loads nothing hidden. +- **Evidence:** a core record links to its paired rebound record; a piece with several + reinforcement layouts shows all of them. +- **Geometry:** replacing the meshes or clouds of a draft also removes the files and markers + of the dropped ones; a frame-only cache entry no longer carries descriptor data; the outline + vertex limit counts holes. +- **Release tooling:** `invoke release-prep` sets the CHANGELOG date, writes `.zenodo.json` and + runs the version checks in one step; `bump-version --gh` leaves the Session component alone + when its client version already matches. The local launch entries and the old scripts work + without a user path; the local server script is now `scripts/dev/serve_dump.py` (`invoke + dev-local`; `invoke dev-migrated` still works and says so), migrating an old 0.5 dump only + with `--migrate`; it writes `.dev/dev-local-credentials-
Record id
+ Paired rebound record: + {paired.onPage ? ( + + {paired.id.slice(0, 8)} + + ) : ( + {paired.id.slice(0, 8)} + )} +
+ )} {(record.derived ?? []).map((d, i) => (
{quantityLabel(d.quantity)} ({d.model.kind}):
diff --git a/src/frontend/components/viewer/ProxySolid.tsx b/src/frontend/components/viewer/ProxySolid.tsx
index ce1ea6b8..af5009e5 100644
--- a/src/frontend/components/viewer/ProxySolid.tsx
+++ b/src/frontend/components/viewer/ProxySolid.tsx
@@ -4,6 +4,7 @@ import { useEffect, useLayoutEffect, useMemo } from 'react'
import * as THREE from 'three'
import { solidGeometry, type ProxyShape } from '@/lib/proxyShape'
+import { FACE_POLYGON_OFFSET, edgeLineMaterial } from '@/lib/viewerDepth'
import { OPACITY_DEFAULT, applyOpacity, materialSettings } from '@/lib/viewerOpacity'
/**
@@ -27,7 +28,7 @@ export default function ProxySolid({
opacity?: number
}) {
const solid = useMemo(() => solidGeometry(shape), [shape])
- const edgeMaterial = useMemo(() => new THREE.LineBasicMaterial({ color: 0x000000 }), [])
+ const edgeMaterial = useMemo(() => edgeLineMaterial(), [])
useEffect(() => () => solid.dispose(), [solid])
useEffect(() => () => edgeMaterial.dispose(), [edgeMaterial])
useLayoutEffect(() => applyOpacity(edgeMaterial, opacity), [edgeMaterial, opacity])
@@ -46,6 +47,9 @@ export default function ProxySolid({
transparent={fade.transparent}
opacity={fade.opacity}
depthWrite={fade.depthWrite}
+ polygonOffset={FACE_POLYGON_OFFSET.polygonOffset}
+ polygonOffsetFactor={FACE_POLYGON_OFFSET.polygonOffsetFactor}
+ polygonOffsetUnits={FACE_POLYGON_OFFSET.polygonOffsetUnits}
/>
)}
diff --git a/src/frontend/lib/evidence/paired.test.ts b/src/frontend/lib/evidence/paired.test.ts
new file mode 100644
index 00000000..65cadae2
--- /dev/null
+++ b/src/frontend/lib/evidence/paired.test.ts
@@ -0,0 +1,68 @@
+/**
+ * The paired rebound record of a core record: the link on the core's card
+ * (0.6.3.1).
+ *
+ * Run: npm test
+ */
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+
+import {
+ PAIRED_OFF_PAGE_TITLE,
+ isOnPage,
+ pairedReboundId,
+ pairedReboundLink,
+ recordAnchor,
+} from '@/lib/evidence/paired'
+
+test('a core record names its paired rebound record', () => {
+ const payload = { sampling: { paired_rebound_id: 'abc-123' } }
+ assert.equal(pairedReboundId('core_compression', payload), 'abc-123')
+})
+
+test('only a core record has one', () => {
+ const payload = { sampling: { paired_rebound_id: 'abc-123' } }
+ assert.equal(pairedReboundId('rebound_hammer', payload), null)
+ assert.equal(pairedReboundId('reinforcement_layout', payload), null)
+})
+
+test('a core without a pairing, or a value that is no id, has none', () => {
+ assert.equal(pairedReboundId('core_compression', { sampling: {} }), null)
+ assert.equal(pairedReboundId('core_compression', { sampling: { paired_rebound_id: null } }), null)
+ assert.equal(pairedReboundId('core_compression', { sampling: { paired_rebound_id: 5 } }), null)
+ assert.equal(pairedReboundId('core_compression', { sampling: { paired_rebound_id: ' ' } }), null)
+ assert.equal(pairedReboundId('core_compression', {}), null)
+ assert.equal(pairedReboundId('core_compression', null), null)
+ assert.equal(pairedReboundId('core_compression', undefined), null)
+})
+
+test('the link points at the card of the record', () => {
+ assert.equal(recordAnchor('abc-123'), '#record-abc-123')
+})
+
+test('the link is shown for a record on the page', () => {
+ assert.equal(isOnPage('a', ['a', 'b']), true)
+ assert.equal(isOnPage('c', ['a', 'b']), false)
+})
+
+test('the core links to its rebound record when that record is on the page', () => {
+ const payload = { sampling: { paired_rebound_id: 'reb-1' } }
+ assert.deepEqual(pairedReboundLink('core_compression', payload, ['core-1', 'reb-1']), {
+ id: 'reb-1', onPage: true, href: '#record-reb-1',
+ })
+})
+
+test('the id is plain text with a title when the record is not on the page', () => {
+ const payload = { sampling: { paired_rebound_id: 'reb-1' } }
+ const off = { id: 'reb-1', onPage: false, title: PAIRED_OFF_PAGE_TITLE }
+ assert.deepEqual(pairedReboundLink('core_compression', payload, ['core-1']), off)
+ assert.deepEqual(pairedReboundLink('core_compression', payload, []), off)
+ // no list at all (a queue, the review step of the form): nothing is on the page
+ assert.deepEqual(pairedReboundLink('core_compression', payload), off)
+ assert.match(PAIRED_OFF_PAGE_TITLE, /another version|filtered out/)
+})
+
+test('a record without a pairing has no link and no text', () => {
+ assert.equal(pairedReboundLink('core_compression', { sampling: {} }, ['a']), null)
+ assert.equal(pairedReboundLink('rebound_hammer', { sampling: { paired_rebound_id: 'x' } }, ['x']), null)
+})
diff --git a/src/frontend/lib/evidence/paired.ts b/src/frontend/lib/evidence/paired.ts
new file mode 100644
index 00000000..c3e2e81d
--- /dev/null
+++ b/src/frontend/lib/evidence/paired.ts
@@ -0,0 +1,51 @@
+/**
+ * The rebound record a core record is paired with (EN 13791 pairs, 0.6.3.1):
+ * the card of the core links to it. The id is read from the payload of a
+ * `core_compression` record; any other record, or a value that is no text,
+ * has none.
+ *
+ * Run: npm test
+ */
+
+/** The `paired_rebound_id` of a core record's payload, or null. */
+export function pairedReboundId(method: string, payload: unknown): string | null {
+ if (method !== 'core_compression') return null
+ const sampling = (payload as { sampling?: { paired_rebound_id?: unknown } } | null | undefined)?.sampling
+ const id = sampling?.paired_rebound_id
+ return typeof id === 'string' && id.trim() ? id.trim() : null
+}
+
+/** The anchor of a record's card on the component page (`EvidenceCard` ids). */
+export function recordAnchor(recordId: string): string {
+ return `#record-${recordId}`
+}
+
+/** The record is on this page: the link can point at its card. */
+export function isOnPage(recordId: string, shownIds: readonly string[]): boolean {
+ return shownIds.includes(recordId)
+}
+
+export const PAIRED_OFF_PAGE_TITLE =
+ 'This record is on another version or filtered out of this list'
+
+export type PairedReboundLink =
+ | { id: string; onPage: true; href: string }
+ | { id: string; onPage: false; title: string }
+
+/**
+ * What the core's card shows for its paired rebound record: a link to the card
+ * when that record is on the page (`shownIds`: the cards the page lists), else
+ * the id as plain text with a short title saying why there is no link. Null for
+ * a record that has no pairing. Without `shownIds` (a list that is no
+ * component page: a queue, the form's review) nothing is on the page.
+ */
+export function pairedReboundLink(
+ method: string,
+ payload: unknown,
+ shownIds?: readonly string[],
+): PairedReboundLink | null {
+ const id = pairedReboundId(method, payload)
+ if (!id) return null
+ if (shownIds && isOnPage(id, shownIds)) return { id, onPage: true, href: recordAnchor(id) }
+ return { id, onPage: false, title: PAIRED_OFF_PAGE_TITLE }
+}
diff --git a/src/frontend/lib/reinforcementColors.test.ts b/src/frontend/lib/reinforcementColors.test.ts
index b7c3f0f6..740763aa 100644
--- a/src/frontend/lib/reinforcementColors.test.ts
+++ b/src/frontend/lib/reinforcementColors.test.ts
@@ -9,6 +9,7 @@ import {
diameterRange,
layoutColour,
layoutLabels,
+ layoutsOnSnapshot,
LAYOUT_COLOURS,
rampColour,
} from '@/lib/reinforcementColors'
@@ -78,3 +79,58 @@ test('the legend lists the layouts or the diameter range', () => {
assert.equal(byDiameter?.kind === 'diameter' && byDiameter.max, 16)
assert.equal(barLegend('steel', layouts, bars), null)
})
+
+
+// TWO LAYOUTS ON ONE PIECE (RL1, 0.6.3.1) -----------------------------------------
+const bar = (diameter: number, x: number) => ({
+ spec: `B${diameter}`, diameter_mm: diameter, points: [[x, 0, 0], [x, 0, 100]],
+})
+
+test('two layouts on one snapshot are two layouts with their own bars', () => {
+ const records = [
+ { _id: 'a', observed_at: '2026-01-05', position: { snapshot_id: 's1' },
+ payload: { bars: [bar(12, 0), bar(12, 50)], document: { title: 'Drawing A' } } },
+ { _id: 'b', observed_at: '2026-02-09', position: { snapshot_id: 's1' },
+ payload: { bars: [bar(8, 10), bar(8, 20), bar(8, 30)], document: { title: 'Drawing B' } } },
+ ]
+ const { bars, layouts } = layoutsOnSnapshot(records, 's1')
+ assert.equal(bars.length, 5)
+ assert.deepEqual(layouts, [
+ { id: 'a', label: 'Drawing A', count: 2 },
+ { id: 'b', label: 'Drawing B', count: 3 },
+ ])
+ assert.deepEqual(bars.map((b) => b.layout), [0, 0, 1, 1, 1])
+ assert.equal(defaultColourMode(layouts.length), 'layout')
+ // each layout has its own colour in the legend
+ assert.notEqual(layoutColour(0), layoutColour(1))
+ const legend = barLegend('layout', layouts, bars)
+ assert.equal(legend?.kind, 'layout')
+})
+
+test('a layout of another snapshot, or without a drawable bar, is not a layout here', () => {
+ const records = [
+ { _id: 'a', position: { snapshot_id: 's1' }, payload: { bars: [bar(12, 0)] } },
+ { _id: 'other', position: { snapshot_id: 's2' }, payload: { bars: [bar(12, 0)] } },
+ { _id: 'empty', position: { snapshot_id: 's1' }, payload: { bars: [] } },
+ { _id: 'flat', position: { snapshot_id: 's1' },
+ payload: { bars: [{ spec: 'x', diameter_mm: 0, points: [[0, 0, 0], [1, 0, 0]] }] } },
+ { _id: 'none', position: null, payload: { bars: [bar(12, 0)] } },
+ ]
+ const { bars, layouts } = layoutsOnSnapshot(records, 's1')
+ assert.deepEqual(layouts, [{ id: 'a', label: 'Layout 1', count: 1 }])
+ assert.equal(bars.length, 1)
+})
+
+test('two layouts with the same title are told apart in the legend', () => {
+ const records = ['a', 'b'].map((id) => ({
+ _id: id, position: { snapshot_id: 's1' },
+ payload: { bars: [bar(10, 0)], document: { title: 'Ground floor' } } }))
+ const { layouts } = layoutsOnSnapshot(records, 's1')
+ assert.deepEqual(layouts.map((l) => l.label), ['Ground floor (1)', 'Ground floor (2)'])
+})
+
+test('a piece without a layout has no bars and starts steel', () => {
+ const { bars, layouts } = layoutsOnSnapshot([], 's1')
+ assert.deepEqual([bars, layouts], [[], []])
+ assert.equal(defaultColourMode(layouts.length), 'steel')
+})
diff --git a/src/frontend/lib/reinforcementColors.ts b/src/frontend/lib/reinforcementColors.ts
index ad6e5609..a99e2c62 100644
--- a/src/frontend/lib/reinforcementColors.ts
+++ b/src/frontend/lib/reinforcementColors.ts
@@ -94,6 +94,42 @@ export function layoutLabels(records: RecordLike[]): string[] {
export type ColouredBar = { diameter_mm: number; layout: number }
+/** A bar the viewer can draw: a centreline of two points or more and a diameter. */
+export type DrawableBar = { diameter_mm: number; points: number[][] }
+
+export function isDrawableBar(bar: DrawableBar): boolean {
+ return Array.isArray(bar.points) && bar.points.length >= 2 && bar.diameter_mm > 0
+}
+
+export type LayoutRecord = RecordLike & {
+ position?: { snapshot_id?: string | null } | null
+ payload?: { bars?: B[]; document?: { title?: string | null } | null } | null
+}
+
+/**
+ * The layouts of one snapshot and their bars (RL1, 0.6.3.1): every record that
+ * is positioned on the snapshot and has a drawable bar is one layout, in the
+ * order given, and every bar of every layout is drawn, each with the index of
+ * its layout. Two layouts on one piece are two cards and two sets of bars.
+ */
+export function layoutsOnSnapshot(
+ records: LayoutRecord[],
+ snapshotId: string,
+): { bars: (B & { layout: number })[]; layouts: BarLayout[] } {
+ const here = records
+ .filter((record) => record.position?.snapshot_id === snapshotId)
+ .filter((record) => (record.payload?.bars ?? []).some(isDrawableBar))
+ const labels = layoutLabels(here)
+ const bars = here.flatMap((record, layout) =>
+ (record.payload?.bars ?? []).filter(isDrawableBar).map((bar) => ({ ...bar, layout })))
+ const layouts = here.map((record, layout) => ({
+ id: String(record._id ?? layout),
+ label: labels[layout],
+ count: bars.filter((bar) => bar.layout === layout).length,
+ }))
+ return { bars, layouts }
+}
+
/** The colour of a bar, or null for the steel material. */
export function barColour(
mode: BarColourMode,
diff --git a/src/frontend/lib/viewerDepth.test.ts b/src/frontend/lib/viewerDepth.test.ts
new file mode 100644
index 00000000..d20626d4
--- /dev/null
+++ b/src/frontend/lib/viewerDepth.test.ts
@@ -0,0 +1,150 @@
+/**
+ * Faces behind their edges (0.6.3.1): the face materials carry a polygon
+ * offset, also in the faded copies of the opacity slider; the edge lines keep
+ * their depth test and have no offset.
+ *
+ * Run: npm test
+ */
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import * as THREE from 'three'
+
+import {
+ FACE_POLYGON_OFFSET,
+ NEAR_FLOOR,
+ bodyFaceMaterial,
+ depthChanged,
+ depthRange,
+ edgeLineMaterial,
+ pushFacesBack,
+ sizeChanged,
+} from '@/lib/viewerDepth'
+import { applyOpacity, applyOpacityToObject, copyForViewer } from '@/lib/viewerOpacity'
+
+function offsetOf(material: THREE.Material) {
+ return [material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits]
+}
+
+test('the offset is small and positive: faces go back, not forward', () => {
+ assert.equal(FACE_POLYGON_OFFSET.polygonOffset, true)
+ assert.ok(FACE_POLYGON_OFFSET.polygonOffsetFactor > 0 && FACE_POLYGON_OFFSET.polygonOffsetFactor <= 2)
+ assert.ok(FACE_POLYGON_OFFSET.polygonOffsetUnits > 0 && FACE_POLYGON_OFFSET.polygonOffsetUnits <= 2)
+})
+
+test('the face material of a mesh has the offset, with vertex colours or grey', () => {
+ for (const hasColors of [true, false]) {
+ const material = bodyFaceMaterial(hasColors)
+ assert.deepEqual(offsetOf(material), [true, 1, 1])
+ assert.equal(material.side, THREE.DoubleSide)
+ assert.equal(material.vertexColors, hasColors)
+ // opaque and writing depth, as before the offset
+ assert.equal(material.transparent, false)
+ assert.equal(material.depthWrite, true)
+ assert.equal(material.depthTest, true)
+ }
+ assert.equal(bodyFaceMaterial(false).color.getHex(), 0x888888)
+})
+
+test('the edge lines have no offset and keep their depth test', () => {
+ const lines = edgeLineMaterial()
+ assert.deepEqual(offsetOf(lines), [false, 0, 0])
+ assert.equal(lines.depthTest, true)
+ assert.equal(lines.color.getHex(), 0)
+})
+
+test('an existing material can be pushed back', () => {
+ const standard = pushFacesBack(new THREE.MeshStandardMaterial({ color: 0x336699 }))
+ assert.deepEqual(offsetOf(standard), [true, 1, 1])
+ assert.equal(standard.color.getHex(), 0x336699)
+})
+
+test('the faded copy of the opacity slider keeps the offset, the original is not changed', () => {
+ const faces = bodyFaceMaterial(true)
+ const edges = edgeLineMaterial()
+ const group = new THREE.Group()
+ group.add(new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), faces))
+ group.add(new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.BoxGeometry(1, 1, 1)), edges))
+ const copy = copyForViewer(group)
+ applyOpacityToObject(copy, 40)
+ const [copiedFaces, copiedEdges] = copy.children.map(
+ (child) => (child as THREE.Mesh).material as THREE.Material)
+ assert.deepEqual(offsetOf(copiedFaces), [true, 1, 1])
+ assert.deepEqual(offsetOf(copiedEdges), [false, 0, 0])
+ assert.equal(copiedFaces.opacity, 0.4)
+ assert.deepEqual(offsetOf(faces), [true, 1, 1])
+ assert.equal(faces.opacity, 1)
+})
+
+test('fading and restoring a face material leaves its offset', () => {
+ const faces = bodyFaceMaterial(false)
+ applyOpacity(faces, 30)
+ applyOpacity(faces, 100)
+ assert.deepEqual(offsetOf(faces), [true, 1, 1])
+})
+
+
+// THE CAMERA PLANES (0.6.3.1) -----------------------------------------------------
+test('near is a hundredth of the distance and far lies past the piece', () => {
+ const { near, far } = depthRange(6, 2.3) // a 4.6 m beam, seen from the fit distance
+ assert.equal(near, 0.06)
+ assert.ok(far > 6 + 2.3, 'the far side of the piece and the grid are inside')
+ assert.ok(far / near < 5000)
+})
+
+test('close to the piece the near plane follows, so a face is not cut away', () => {
+ const radius = 2.3
+ for (const distance of [1, 0.2, 0.05, 0.01]) {
+ const { near } = depthRange(distance, radius)
+ assert.ok(near <= distance / 100 + 1e-12 || near === radius * NEAR_FLOOR, String(distance))
+ assert.ok(near < distance, 'a face at the target is in front of the near plane: ' + distance)
+ }
+ // a camera right at the target: still a positive, tiny near plane
+ assert.ok(depthRange(0, radius).near > 0)
+ assert.equal(depthRange(1e-9, radius).near, radius * NEAR_FLOOR)
+})
+
+test('the planes keep the ratio the depth buffer can resolve', () => {
+ for (const radius of [0.09, 0.5, 2.3, 40]) {
+ for (const distance of [radius * 1e-3, radius * 0.1, radius, radius * 3, radius * 10]) {
+ const { near, far } = depthRange(distance, radius)
+ assert.ok(near > 0 && far > near)
+ assert.ok(far / near <= 1e6, `radius ${radius}, distance ${distance}: ${far / near}`)
+ }
+ }
+})
+
+test('a small piece and a large piece get planes of their own size', () => {
+ const small = depthRange(0.3, 0.09) // 185 mm
+ const large = depthRange(6, 2.3) // 4.6 m
+ assert.ok(small.near < large.near && small.far < large.far)
+ assert.ok(small.far > 0.3 + 0.09)
+})
+
+test('a distance or radius that is no number falls back to something sane', () => {
+ for (const [distance, radius] of [[Number.NaN, 1], [1, Number.NaN], [-3, 1], [1, 0], [Infinity, 1]]) {
+ const { near, far } = depthRange(distance, radius)
+ assert.ok(Number.isFinite(near) && Number.isFinite(far) && near > 0 && far > near)
+ }
+})
+
+test('the camera is only touched when a plane moved by more than a hundredth', () => {
+ const now = { near: 1, far: 100 }
+ assert.equal(depthChanged(now, { near: 1.005, far: 100.4 }), false)
+ assert.equal(depthChanged(now, { near: 1.2, far: 100 }), true)
+ assert.equal(depthChanged(now, { near: 1, far: 150 }), true)
+})
+
+test('the view is fitted again when the piece changes size, not for a small wobble', () => {
+ assert.equal(sizeChanged(0, 0.3), true) // the first size
+ assert.equal(sizeChanged(0.3, 0.3), false)
+ assert.equal(sizeChanged(0.3, 0.31), false)
+ assert.equal(sizeChanged(0.3, 4.6), true) // a version of another size
+ assert.equal(sizeChanged(4.6, 0.3), true)
+})
+
+test('a size that is no number never asks for a fit', () => {
+ for (const bad of [0, -1, Number.NaN, Infinity]) {
+ assert.equal(sizeChanged(0.3, bad), false)
+ assert.equal(sizeChanged(0, bad), false)
+ }
+})
diff --git a/src/frontend/lib/viewerDepth.ts b/src/frontend/lib/viewerDepth.ts
new file mode 100644
index 00000000..ac8deb35
--- /dev/null
+++ b/src/frontend/lib/viewerDepth.ts
@@ -0,0 +1,79 @@
+/**
+ * Faces sit a hair behind their edges (0.6.3.1): a mesh and the lines of its
+ * edges lie in the same surface, so at some angles the depth test let the
+ * faces cut the lines. The face materials carry a polygon offset, which moves
+ * the faces a little back in depth while the edges stay where they are. The
+ * depth test of the edges stays on: back edges are hidden by the faces as
+ * before, only the edges of the visible side win.
+ *
+ * Run: npm test
+ */
+import * as THREE from 'three'
+
+/** Offset of the faces: the slope factor handles steep faces, the units the flat ones. */
+export const FACE_POLYGON_OFFSET = {
+ polygonOffset: true,
+ polygonOffsetFactor: 1,
+ polygonOffsetUnits: 1,
+} as const
+
+/** Move the faces of a material back (an existing one, e.g. a built-in mesh). */
+export function pushFacesBack