119 lines
3.9 KiB
TypeScript
119 lines
3.9 KiB
TypeScript
import type { Geometry, Point } from '@faunosteo/core-wasm/types'
|
|
import { round2 } from './frame'
|
|
|
|
/** The geometry group as typed: numbers stay text until the group is applied whole. */
|
|
export type GroupFields =
|
|
| { type: 'circle'; cx: string; cy: string; r: string }
|
|
| { type: 'polygon'; rows: { x: string; y: string }[] }
|
|
|
|
export type EditableGeometry = Extract<Geometry, { type: 'circle' | 'polygon' }>
|
|
|
|
export type GroupParse =
|
|
| { ok: true; geometry: EditableGeometry }
|
|
| { ok: false }
|
|
|
|
/** The stored numbers exactly as JSON prints them (no display rounding). */
|
|
export function fieldsOf(geometry: EditableGeometry): GroupFields {
|
|
if (geometry.type === 'circle') {
|
|
return {
|
|
type: 'circle',
|
|
cx: String(geometry.cx),
|
|
cy: String(geometry.cy),
|
|
r: String(geometry.r),
|
|
}
|
|
}
|
|
return {
|
|
type: 'polygon',
|
|
rows: geometry.points.map(([x, y]) => ({ x: String(x), y: String(y) })),
|
|
}
|
|
}
|
|
|
|
export function isEditable(
|
|
geometry: Geometry | null,
|
|
): geometry is EditableGeometry {
|
|
return geometry?.type === 'circle' || geometry?.type === 'polygon'
|
|
}
|
|
|
|
/** Input shape only: a blank or non-numeric field is no number. The core judges the rest. */
|
|
function numberOf(text: string): number | null {
|
|
if (text.trim() === '') return null
|
|
const value = Number(text)
|
|
return Number.isFinite(value) ? value : null
|
|
}
|
|
|
|
export function parseFields(fields: GroupFields): GroupParse {
|
|
if (fields.type === 'circle') {
|
|
const [cx, cy, r] = [fields.cx, fields.cy, fields.r].map(numberOf)
|
|
if (cx === null || cy === null || r === null) return { ok: false }
|
|
return { ok: true, geometry: { type: 'circle', cx, cy, r } }
|
|
}
|
|
const points: Point[] = []
|
|
for (const row of fields.rows) {
|
|
const [x, y] = [numberOf(row.x), numberOf(row.y)]
|
|
if (x === null || y === null) return { ok: false }
|
|
points.push([x, y])
|
|
}
|
|
return { ok: true, geometry: { type: 'polygon', points } }
|
|
}
|
|
|
|
/** True when every typed number equals the stored one: nothing to propose. */
|
|
export function sameAsStored(
|
|
fields: GroupFields,
|
|
stored: EditableGeometry,
|
|
): boolean {
|
|
const parsed = parseFields(fields)
|
|
if (!parsed.ok) return false
|
|
return JSON.stringify(parsed.geometry) === JSON.stringify(stored)
|
|
}
|
|
|
|
/** The rows whose text differs from the stored value: the ones marked « non appliqué ». */
|
|
export function changedFieldNames(
|
|
fields: GroupFields,
|
|
stored: EditableGeometry,
|
|
): Set<string> {
|
|
const base = fieldsOf(stored)
|
|
const changed = new Set<string>()
|
|
if (fields.type === 'circle' && base.type === 'circle') {
|
|
for (const key of ['cx', 'cy', 'r'] as const) {
|
|
if (fields[key] !== base[key]) changed.add(key)
|
|
}
|
|
return changed
|
|
}
|
|
if (fields.type === 'polygon' && base.type === 'polygon') {
|
|
fields.rows.forEach((row, index) => {
|
|
if (row.x !== base.rows[index]?.x) changed.add(`x${index}`)
|
|
if (row.y !== base.rows[index]?.y) changed.add(`y${index}`)
|
|
})
|
|
}
|
|
return changed
|
|
}
|
|
|
|
/** A new vertex halfway between the selected row and the next (wrapping), 2 decimals. */
|
|
export function withVertexAfter(
|
|
fields: Extract<GroupFields, { type: 'polygon' }>,
|
|
after: number,
|
|
): Extract<GroupFields, { type: 'polygon' }> {
|
|
const rows = fields.rows
|
|
const from = rows[after] ?? rows[rows.length - 1]
|
|
const to = rows[(after + 1) % rows.length] ?? from
|
|
const middle = (a: string, b: string) => {
|
|
const value = round2((Number(a) + Number(b)) / 2)
|
|
return Number.isFinite(value) && a.trim() !== '' && b.trim() !== ''
|
|
? String(value)
|
|
: ''
|
|
}
|
|
const added = { x: middle(from.x, to.x), y: middle(from.y, to.y) }
|
|
const at = Math.min(after, rows.length - 1) + 1
|
|
return {
|
|
type: 'polygon',
|
|
rows: [...rows.slice(0, at), added, ...rows.slice(at)],
|
|
}
|
|
}
|
|
|
|
export function withoutVertex(
|
|
fields: Extract<GroupFields, { type: 'polygon' }>,
|
|
index: number,
|
|
): Extract<GroupFields, { type: 'polygon' }> {
|
|
return { type: 'polygon', rows: fields.rows.filter((_, i) => i !== index) }
|
|
}
|