Files
excalidraw/packages/math/src/pca.ts
T
f179f7ffd2 feat(editor): draw to shape (auto-detection) (#9313)
* Initial commit

* Segments detection

* Remove aspect ratio

* Refactor

* Fix tests

* Toggle shapeSnap

* Fix linting

* debug: live preview

* Rebase fixes

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* Bind to elements (no hover background)

* Shape closed

* Fix rad calc + rename segments

* Comments

* Fix binding highlighting + lints

* Rename to convert to shape

* fix: Binding

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* chore: Refactor spline simplification

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* chore: Remove unused doBoundsIntersect

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Restore type signature

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Propose new button shapes

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* chore: Remove unused types

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* chore: Icon shapes

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Element cloning

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Element cloning

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Remove unused

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Shape settings in recognition mode

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* chore: Remove debug

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Actions

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Lint

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Arrowheads for freedraw

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Draw shape tool instead of freedraw

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Test

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Lint

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Keyboard shortcut visibility

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Unique cursor for draw shape

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Remove selection

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Shape selection

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Improve shape recognition

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Further refine shape recognition

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Preview

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Add hint

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Refined shape recognition

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Less ellipse more rectanguloid

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Refined arrow recognition

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Refinerecognition

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* chore: Lower linear threshold

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Rectangle head arrow

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: More arrowhead sizes

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Arrow remains an arrow

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Draw line between elements converts to arrow

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* remove pendingDrawShapeElement

* fix: Arrow start point binding

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Different arrow endpoint heuristic

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Point type

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Various small fixes

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Avoid recompute

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Type issues

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* test(editor): cover the drawShape (snap-to-shape) tool

The drawShape tool had no dedicated tests, which let master's toolbar /
active-tool rewrite silently break it during the merge: actionFinalize
reverted to the selection tool after every sketch, because the drawShape
flow finalizes without a `newElement` and so failed master's new
`&& element` guard.

Covers the behavior the merge had to preserve:
- sketch recognition (rectangle, ellipse) and the too-small no-op
- the tool stays active after finalizing, so shapes can be sketched
  consecutively (regression guard for the above)
- activation via the S shortcut and the extra-tools dropdown
- the tool's own cursor

Points are fed into the trail directly rather than through pointermove,
which is rAF-throttled and would drop all but one point per frame — the
same approach lasso.test.tsx uses.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: Add test

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: Breakout from App.tsx

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Shortcut is now Shift+X

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: No selecting arrows for shape recog

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* fix: Double history

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* feat: PCA + feature extraction recognition

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>

* reduce and normalize min-size gates

* never select newElement when shape recog tool active

* simplify lifecycle & remove double finalize

* make arrows bindings orbit

* funnel though actionFinalize & fix two unrelated cleanup cases

* use crosshair cursor

* show strokeWidth

* switch icons for drawShape & extraTools

* dedupe getBoundsFromPoints

* remove unused helpers

* jsdoc cleanup

* dynamic trail size

* improve rectangle recognition

* disallow linears to deviate too much from a straight line

* put under freedraw tool on tablet/mobile

* tweak label

* rename `drawShape` -> `autoshape`

---------

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
Co-authored-by: Mark Tolmacs <mark@lazycat.hu>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-23 18:42:42 +02:00

203 lines
5.6 KiB
TypeScript

import { pointFrom } from "./point";
import { vector, vectorNormal, vectorNormalize, vectorScale } from "./vector";
import type { GlobalPoint, LocalPoint, Vector } from "./types";
/**
* The principal axes of a point set, i.e. the eigen decomposition of its
* 2x2 covariance matrix.
*
* The axes are orthonormal and ordered by variance, so `major` is the
* direction along which the points spread the most. Together with `centroid`
* they define a canonical frame: expressing the points in it removes
* translation and rotation, and dividing by `sqrt(majorVariance)` removes
* scale.
*/
export type PrincipalAxes<Point extends GlobalPoint | LocalPoint> = {
centroid: Point;
/** Unit vector along the direction of largest variance. */
major: Vector;
/** Unit vector along the direction of smallest variance, normal to major. */
minor: Vector;
/** Variance along `major` (the larger eigenvalue, λ₁). */
majorVariance: number;
/** Variance along `minor` (the smaller eigenvalue, λ₂). */
minorVariance: number;
};
/**
* A point expressed in a principal axes frame: `u` along the major axis,
* `v` along the minor axis, both relative to the centroid.
*/
export type PrincipalCoords = [u: number, v: number];
/**
* Compute the centroid of a point set.
*/
export function centroid<Point extends GlobalPoint | LocalPoint>(
points: readonly Point[],
): Point {
let cx = 0;
let cy = 0;
for (const [x, y] of points) {
cx += x;
cy += y;
}
return pointFrom<Point>(cx / points.length, cy / points.length);
}
/**
* Principal component analysis of a 2D point set.
*
* Computes the centroid and the eigen decomposition of the covariance matrix
* [[μ20, μ11], [μ11, μ02]].
*
* @param points At least two points; fewer leaves the axes degenerate.
* @returns The centroid, the orthonormal axes and their variances.
*/
export function principalAxes<Point extends GlobalPoint | LocalPoint>(
points: readonly Point[],
): PrincipalAxes<Point> {
if (points.length < 2) {
console.error("Degenerate point set: at least two points are required");
}
const c = centroid(points);
let m20 = 0;
let m02 = 0;
let m11 = 0;
for (const [x, y] of points) {
const dx = x - c[0];
const dy = y - c[1];
m20 += dx * dx;
m02 += dy * dy;
m11 += dx * dy;
}
m20 /= points.length;
m02 /= points.length;
m11 /= points.length;
// Eigenvalues of [[m20, m11], [m11, m02]].
const trace = m20 + m02;
const diff = Math.hypot(m20 - m02, 2 * m11);
const majorVariance = (trace + diff) / 2;
const minorVariance = (trace - diff) / 2;
// Eigenvector for the larger eigenvalue. When m11 is 0 the covariance is
// already diagonal and the axes are the coordinate axes.
let major: Vector;
if (Math.abs(m11) > Number.EPSILON) {
major = vectorNormalize(vector(majorVariance - m02, m11));
} else {
major = m20 >= m02 ? vector(1, 0) : vector(0, 1);
}
return {
centroid: c,
major,
minor: vectorNormal(major),
majorVariance,
minorVariance,
};
}
/**
* Express points in the frame of the given principal axes, making the point
* cloud rotation, scale and translation invariant.
*/
export function principalCoords<Point extends GlobalPoint | LocalPoint>(
points: readonly Point[],
axes: PrincipalAxes<Point>,
scale: number = 1,
): PrincipalCoords[] {
const { centroid: c, major, minor } = axes;
return points.map(([x, y]) => {
const dx = x - c[0];
const dy = y - c[1];
return [
(dx * major[0] + dy * major[1]) * scale,
(dx * minor[0] + dy * minor[1]) * scale,
] as PrincipalCoords;
});
}
/**
* Flip the major axis so that it points toward the denser end of the point
* cloud, resolving the 180° sign ambiguity of the eigenvector.
*
* @returns The axes with `major`/`minor` flipped if needed. Variances are
* unchanged, as they are invariant to axis sign.
*/
export function orientPrincipalAxes<Point extends GlobalPoint | LocalPoint>(
points: readonly Point[],
axes: PrincipalAxes<Point>,
): PrincipalAxes<Point> {
const u = principalCoords(points, axes).map(([uu]) => uu);
if (skewness(u) <= 0) {
return axes;
}
const major = vectorScale(axes.major, -1);
return { ...axes, major, minor: vectorNormal(major) };
}
/**
* Ratio of the minor to the major variance in [0, 1]
*
* @returns 0 is a perfectly straight stroke, 1 is a stroke with no preferred
* direction.
*/
export function elongation<Point extends GlobalPoint | LocalPoint>(
axes: PrincipalAxes<Point>,
): number {
return axes.majorVariance > 0 ? axes.minorVariance / axes.majorVariance : 1;
}
/**
* The `order`-th standardized moment of a sample in a rotation and scale
* invariant way (sandardizing by sigma).
*
* @returns 0 for a degenerate (zero variance) sample.
*/
export function standardizedMoment(
values: readonly number[],
order: number,
): number {
const n = values.length;
let mean = 0;
for (const v of values) {
mean += v;
}
mean /= n;
let variance = 0;
let moment = 0;
for (const v of values) {
const d = v - mean;
variance += d * d;
moment += d ** order;
}
variance /= n;
moment /= n;
const sigma = Math.sqrt(variance);
return sigma > Number.EPSILON ? moment / sigma ** order : 0;
}
/**
* Third standardized moment: how lopsided a sample is.
*
*/
export function skewness(values: readonly number[]): number {
return standardizedMoment(values, 3);
}
/**
* Fourth standardized moment: how the mass is split between the tails and the
* middle of a sample. Not excess kurtosis, a normal sample gives 3.
*
*/
export function kurtosis(values: readonly number[]): number {
return standardizedMoment(values, 4);
}