The clipFrame command takes an undistort flag, honoured only for full-frame reads to mirror need_undistortion_postprocess, so the engine applies BKE_tracking_distortion_exec on the fly. The reader exports every distortion model's coefficients and the Camera Data panel edits focal, sensor, model and the active model's fields (display rounded past the DNA float32 noise). CLIP_OT_track_copy_color gets its panel button, and plane use_auto_keying round-trips with an off-path that re-derives the plane from point-track motion instead of keying. The headed suite grows to 26 Clip cases with [clip-undistort] (border- concentrated pixel difference plus a zeroed-coefficient identity check) and the extended copy-colour/auto-keying assertions: 28/28 PASS, receipt tests/golden/CLIP-HEADED-20261204-124651, wasm 3c81eb06. Acceptance stays NOT_CLAIMED. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
691 lines
42 KiB
TypeScript
691 lines
42 KiB
TypeScript
import { normalizeProjectAssetPath } from "./asset-path";
|
|
import { blockedGate, capabilityIssue, readyGate, type CapabilityGateResult } from "./capability-gates";
|
|
import type { ErrorCode } from "./error";
|
|
|
|
export const TRACKING_MASK_SCHEMA = 1 as const;
|
|
export const TRACKING_MASK_BUDGET = {
|
|
maxClips: 256,
|
|
maxTracks: 100_000,
|
|
maxMarkers: 1_000_000,
|
|
maxPlaneTracks: 10_000,
|
|
maxMaskLayers: 1_024,
|
|
maxSplines: 100_000,
|
|
maxMaskPoints: 1_000_000,
|
|
maxBindings: 4_096,
|
|
maxFrame: 1_000_000,
|
|
} as const;
|
|
|
|
type Vec2 = [number, number];
|
|
type Quad2 = [Vec2, Vec2, Vec2, Vec2];
|
|
|
|
export interface TrackingMarkerIR {
|
|
frame: number;
|
|
position: Vec2;
|
|
patternMin: Vec2;
|
|
patternMax: Vec2;
|
|
searchMin: Vec2;
|
|
searchMax: Vec2;
|
|
/** marker->pattern_corners added to position: the oriented pattern quad. */
|
|
patternCorners?: Quad2;
|
|
keyframe: boolean;
|
|
muted: boolean;
|
|
selected: boolean;
|
|
}
|
|
|
|
export type MovieClipSourceStatus = "AVAILABLE" | "MISSING" | "UNREADABLE";
|
|
|
|
export interface TrackingTrackSettingsIR {
|
|
/** TrackingMotionModel: 0 translation .. 5 homography. */
|
|
motionModel: number;
|
|
/** Bitmask: 1 brute force, 4 normalization, 8 mask. */
|
|
algorithm: number;
|
|
minimumCorrelation: number;
|
|
framesLimit: number;
|
|
/** 0 = keyframe match, 1 = previous frame match. */
|
|
patternMatch: number;
|
|
margin: number;
|
|
weight: number;
|
|
weightStab: number;
|
|
/** TRACK_CUSTOMCOLOR: draw the track with its own color instead of the default. */
|
|
useCustomColor?: boolean;
|
|
/** RGBA 0..1, drawn when useCustomColor is set. */
|
|
color?: [number, number, number, number];
|
|
/** TRACK_DISABLE_RED/GREEN/BLUE inverted: channels used for tracking. */
|
|
useRedChannel?: boolean;
|
|
useGreenChannel?: boolean;
|
|
useBlueChannel?: boolean;
|
|
/** TRACK_PREVIEW_GRAYSCALE / TRACK_PREVIEW_ALPHA preview toggles. */
|
|
useGrayscalePreview?: boolean;
|
|
useAlphaPreview?: boolean;
|
|
}
|
|
|
|
export interface TrackingTrackIR {
|
|
id: string;
|
|
name: string;
|
|
object?: string;
|
|
selected: boolean;
|
|
locked: boolean;
|
|
/** TRACK_HIDDEN: hidden from the tracking view. */
|
|
hidden?: boolean;
|
|
/** TRACK_USE_2D_STAB: the track drives 2D stabilization translation. */
|
|
useStab?: boolean;
|
|
/** TRACK_USE_2D_STAB_ROT: the track drives rotation stabilization. */
|
|
useStabRot?: boolean;
|
|
/** TRACK_HAS_BUNDLE: the solve produced bundle data, so error is meaningful. */
|
|
hasBundle?: boolean;
|
|
/** RNA average_error (DNA track.error): average reprojection error in px. */
|
|
averageError?: number;
|
|
settings?: TrackingTrackSettingsIR;
|
|
markers: TrackingMarkerIR[];
|
|
}
|
|
|
|
export interface PlaneTrackKeyframeIR { frame: number; corners: Quad2 }
|
|
|
|
export interface PlaneTrackIR {
|
|
id: string;
|
|
name: string;
|
|
selected: boolean;
|
|
/** PLANE_TRACK_HIDDEN: the plane overlay is hidden in the editor. */
|
|
hidden?: boolean;
|
|
/** PLANE_TRACK_AUTOKEY: corner moves insert keyframes. */
|
|
useAutoKeying?: boolean;
|
|
pointTrackIds: string[];
|
|
keyframes: PlaneTrackKeyframeIR[];
|
|
}
|
|
|
|
export interface CameraSolveIR {
|
|
status: "NONE" | "SOLVED" | "FAILED";
|
|
sourceSha256?: string;
|
|
settingsHash?: string;
|
|
reprojectionError?: number;
|
|
focalLength?: number;
|
|
principalPoint?: Vec2;
|
|
solvedFrames?: number[];
|
|
/** Clip-local solve keyframes (MovieTrackingObject.keyframe1/2). */
|
|
keyframeA?: number;
|
|
keyframeB?: number;
|
|
/** MovieTrackingObject.scale used by the set/apply scale tools. */
|
|
scale?: number;
|
|
}
|
|
|
|
/** MovieTrackingStabilization: 2D stabilization settings. */
|
|
export interface ClipStabilizationIR {
|
|
/** TRACKING_2D_STABILIZATION: stabilization display is enabled. */
|
|
enabled: boolean;
|
|
/** TRACKING_AUTOSCALE. */
|
|
autoscale: boolean;
|
|
/** TRACKING_STABILIZE_ROTATION. */
|
|
useRotation: boolean;
|
|
/** TRACKING_STABILIZE_SCALE. */
|
|
useScale: boolean;
|
|
/** TRACKING_SHOW_STAB_TRACKS. */
|
|
showStabTracks?: boolean;
|
|
/** Clip-local anchor frame. */
|
|
anchorFrame: number;
|
|
targetPos: Vec2;
|
|
targetRot: number;
|
|
/** Zoom factor known to be present on the footage. */
|
|
scale: number;
|
|
locinf: number;
|
|
scaleinf: number;
|
|
rotinf: number;
|
|
/** 0 = nearest, 1 = bilinear, 2 = bicubic. */
|
|
filter: number;
|
|
maxscale: number;
|
|
totalTracks?: number;
|
|
totalRotationTracks?: number;
|
|
}
|
|
|
|
export interface MovieClipIR {
|
|
id: string;
|
|
name: string;
|
|
/** Name of the active tracking object (MovieTracking.objectnr). */
|
|
activeObject?: string;
|
|
/** Project-relative media path. Required for AVAILABLE clips. */
|
|
sourcePath?: string;
|
|
sourceStatus: MovieClipSourceStatus;
|
|
/** Content digest of the resolved source file. Required for AVAILABLE clips. */
|
|
sourceSha256?: string;
|
|
/** Decoded frame size; 0 means the source could not be decoded. */
|
|
width: number;
|
|
height: number;
|
|
frameStart: number;
|
|
frameEnd: number;
|
|
fpsNumerator?: number;
|
|
fpsDenominator?: number;
|
|
tracks: TrackingTrackIR[];
|
|
planeTracks: PlaneTrackIR[];
|
|
cameraSolve?: CameraSolveIR;
|
|
/** Desktop proxy settings stored in the MovieClip DNA. */
|
|
proxy?: MovieClipProxyIR;
|
|
/** MovieTrackingSettings defaults and the tracking camera intrinsics. */
|
|
settings?: MovieClipTrackingSettingsIR;
|
|
/** MovieTrackingStabilization 2D stabilization settings. */
|
|
stabilization?: ClipStabilizationIR;
|
|
}
|
|
|
|
export interface MovieClipTrackingSettingsIR {
|
|
defaultMotionModel: number;
|
|
defaultAlgorithm: number;
|
|
defaultMinimumCorrelation: number;
|
|
defaultPatternSize: number;
|
|
defaultSearchSize: number;
|
|
defaultFramesLimit: number;
|
|
defaultMargin: number;
|
|
defaultPatternMatch: number;
|
|
defaultWeight: number;
|
|
/** TRACKING_MOTION_TRIPOD: tripod solve. */
|
|
motionFlag: number;
|
|
/** TrackingRefineCameraFlag bitmask. */
|
|
refine: number;
|
|
focal?: number;
|
|
sensorWidth?: number;
|
|
pixelAspect?: number;
|
|
/** TrackingCameraUnits: 0 px, 1 in, 2 cm. */
|
|
units?: number;
|
|
/** TrackingDistortionModel. */
|
|
distortionModel?: number;
|
|
principal?: Vec2;
|
|
/** Polynomial model coefficients (TRACKING_DISTORTION_MODEL_POLYNOMIAL). */
|
|
k1?: number;
|
|
k2?: number;
|
|
k3?: number;
|
|
/** Division model coefficients. */
|
|
divisionK1?: number;
|
|
divisionK2?: number;
|
|
/** Nuke model coefficients. */
|
|
nukeK1?: number;
|
|
nukeK2?: number;
|
|
/** Brown-Conrady model coefficients. */
|
|
brownK1?: number;
|
|
brownK2?: number;
|
|
brownK3?: number;
|
|
brownK4?: number;
|
|
}
|
|
|
|
export interface MovieClipProxyIR {
|
|
/** MCLIP_USE_PROXY: the editor decodes from proxies when they exist. */
|
|
enabled: boolean;
|
|
/** Bitmask of proxy sizes: 1 = 25%, 2 = 50%, 4 = 75%, 8 = 100%. */
|
|
buildSizes: number;
|
|
quality: number;
|
|
}
|
|
|
|
const CLIP_SOURCE_STATUSES = new Set<MovieClipSourceStatus>(["AVAILABLE", "MISSING", "UNREADABLE"]);
|
|
|
|
export interface MaskPointIR {
|
|
id: string;
|
|
co: Vec2;
|
|
handleLeft: Vec2;
|
|
handleRight: Vec2;
|
|
handleType: "AUTO" | "VECTOR" | "ALIGNED" | "FREE";
|
|
feather: number;
|
|
selected: boolean;
|
|
}
|
|
|
|
export interface MaskSplineIR {
|
|
id: string;
|
|
cyclic: boolean;
|
|
fill: boolean;
|
|
points: MaskPointIR[];
|
|
}
|
|
|
|
export interface MaskLayerIR {
|
|
id: string;
|
|
name: string;
|
|
visible: boolean;
|
|
locked: boolean;
|
|
opacity: number;
|
|
splines: MaskSplineIR[];
|
|
}
|
|
|
|
export interface MaskIR { id: string; name: string; layers: MaskLayerIR[] }
|
|
|
|
export interface TrackingMaskBindingIR {
|
|
id: string;
|
|
target: "COMPOSITOR" | "SCENE";
|
|
ownerId: string;
|
|
clipId?: string;
|
|
maskId?: string;
|
|
}
|
|
|
|
export interface TrackingMaskProjectIR {
|
|
schemaVersion: typeof TRACKING_MASK_SCHEMA;
|
|
revision: number;
|
|
clips: MovieClipIR[];
|
|
masks: MaskIR[];
|
|
bindings: TrackingMaskBindingIR[];
|
|
}
|
|
|
|
export interface MaskRaycastHitIR {
|
|
maskId: string;
|
|
layerId: string;
|
|
splineId: string;
|
|
kind: "POINT" | "SEGMENT";
|
|
pointId: string;
|
|
nextPointId?: string;
|
|
distance: number;
|
|
parameter?: number;
|
|
}
|
|
|
|
export interface MaskPointSelectionIR {
|
|
maskId: string;
|
|
layerId: string;
|
|
splineId: string;
|
|
pointId: string;
|
|
}
|
|
|
|
export type TrackingMaskEditIR =
|
|
| { type: "SET_MARKER"; revision: number; clipId: string; trackId: string; marker: TrackingMarkerIR }
|
|
| { type: "DELETE_MARKER"; revision: number; clipId: string; trackId: string; frame: number }
|
|
| { type: "SET_TRACK_SELECTION"; revision: number; clipId: string; trackId: string; selected: boolean }
|
|
| { type: "SET_MASK_POINT"; revision: number; maskId: string; layerId: string; splineId: string; point: MaskPointIR }
|
|
| { type: "SET_SPLINE_CYCLIC"; revision: number; maskId: string; layerId: string; splineId: string; cyclic: boolean };
|
|
|
|
export class TrackingMaskValidationError extends Error {
|
|
readonly code: ErrorCode;
|
|
|
|
constructor(code: ErrorCode, message: string) {
|
|
super(`${code}: ${message}`);
|
|
this.name = "TrackingMaskValidationError";
|
|
this.code = code;
|
|
}
|
|
}
|
|
|
|
const SHA256 = /^[a-f0-9]{64}$/;
|
|
const HANDLE_TYPES = new Set<MaskPointIR["handleType"]>(["AUTO", "VECTOR", "ALIGNED", "FREE"]);
|
|
|
|
function record(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
function text(value: unknown, name: string, maximum = 256): string {
|
|
if (typeof value !== "string" || value.length === 0 || value.length > maximum) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is invalid`);
|
|
return value;
|
|
}
|
|
|
|
function integer(value: unknown, name: string, minimum: number, maximum: number): number {
|
|
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < minimum || value > maximum) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is outside the bounded range`);
|
|
return value;
|
|
}
|
|
|
|
function finite(value: unknown, name: string, minimum: number, maximum: number): number {
|
|
if (typeof value !== "number" || !Number.isFinite(value) || value < minimum || value > maximum) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is outside the bounded range`);
|
|
return value;
|
|
}
|
|
|
|
function boolean(value: unknown, name: string): boolean {
|
|
if (typeof value !== "boolean") throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is invalid`);
|
|
return value;
|
|
}
|
|
|
|
function digest(value: unknown, name: string): string {
|
|
if (typeof value !== "string" || !SHA256.test(value)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} must be a lowercase SHA-256 digest`);
|
|
return value;
|
|
}
|
|
|
|
function vec2(value: unknown, name: string, minimum = -16, maximum = 16): Vec2 {
|
|
if (!Array.isArray(value) || value.length !== 2) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} must be vec2`);
|
|
return [finite(value[0], `${name}[0]`, minimum, maximum), finite(value[1], `${name}[1]`, minimum, maximum)];
|
|
}
|
|
|
|
function marker(value: unknown, name: string): TrackingMarkerIR {
|
|
if (!record(value)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is invalid`);
|
|
const patternMin = vec2(value.patternMin, `${name}.patternMin`);
|
|
const patternMax = vec2(value.patternMax, `${name}.patternMax`);
|
|
const searchMin = vec2(value.searchMin, `${name}.searchMin`);
|
|
const searchMax = vec2(value.searchMax, `${name}.searchMax`);
|
|
if (patternMin[0] >= patternMax[0] || patternMin[1] >= patternMax[1] || searchMin[0] >= searchMax[0] || searchMin[1] >= searchMax[1]) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} bounds are invalid`);
|
|
const parsed: TrackingMarkerIR = {
|
|
frame: integer(value.frame, `${name}.frame`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame),
|
|
position: vec2(value.position, `${name}.position`, 0, 1),
|
|
patternMin, patternMax, searchMin, searchMax,
|
|
keyframe: value.keyframe === true,
|
|
muted: value.muted === true,
|
|
selected: value.selected === true,
|
|
};
|
|
if (value.patternCorners !== undefined) {
|
|
if (!Array.isArray(value.patternCorners) || value.patternCorners.length !== 4) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name}.patternCorners must be four vec2`);
|
|
/* Corners are the track position plus a relative pattern offset in unified
|
|
* 0..1 space, so a pattern near the frame edge legitimately reaches past
|
|
* the image bounds; the engine clamps the same values to -4..4. */
|
|
parsed.patternCorners = value.patternCorners.map((item, index) => vec2(item, `${name}.patternCorners[${index}]`, -4, 4)) as Quad2;
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
function sortedFrames<T extends { frame: number }>(items: T[], name: string): T[] {
|
|
items.sort((a, b) => a.frame - b.frame);
|
|
if (items.some((item, index) => index > 0 && item.frame === items[index - 1].frame)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} has duplicate frames`);
|
|
return items;
|
|
}
|
|
|
|
function maskPoint(value: unknown, name: string): MaskPointIR {
|
|
if (!record(value) || !HANDLE_TYPES.has(value.handleType as MaskPointIR["handleType"])) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `${name} is invalid`);
|
|
return {
|
|
id: text(value.id, `${name}.id`),
|
|
co: vec2(value.co, `${name}.co`, -4, 4),
|
|
handleLeft: vec2(value.handleLeft, `${name}.handleLeft`, -4, 4),
|
|
handleRight: vec2(value.handleRight, `${name}.handleRight`, -4, 4),
|
|
handleType: value.handleType as MaskPointIR["handleType"],
|
|
feather: finite(value.feather, `${name}.feather`, 0, 100),
|
|
selected: value.selected === true,
|
|
};
|
|
}
|
|
|
|
function cameraSolve(value: unknown, sourceSha256: string | undefined, name: string): CameraSolveIR | undefined {
|
|
if (value === undefined) return undefined;
|
|
if (!record(value) || !["NONE", "SOLVED", "FAILED"].includes(value.status as string)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is invalid`);
|
|
const solve: CameraSolveIR = { status: value.status as CameraSolveIR["status"] };
|
|
if (value.keyframeA !== undefined) solve.keyframeA = integer(value.keyframeA, `${name}.keyframeA`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame);
|
|
if (value.keyframeB !== undefined) solve.keyframeB = integer(value.keyframeB, `${name}.keyframeB`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame);
|
|
if (value.scale !== undefined) solve.scale = finite(value.scale, `${name}.scale`, 1e-6, 1_000_000);
|
|
if (solve.status === "SOLVED") {
|
|
if (sourceSha256 === undefined) throw new TrackingMaskValidationError("TRACKING_SOURCE_HASH_MISMATCH", `${name} is solved but the clip source is unresolved`);
|
|
solve.sourceSha256 = digest(value.sourceSha256, `${name}.sourceSha256`);
|
|
if (solve.sourceSha256 !== sourceSha256) throw new TrackingMaskValidationError("TRACKING_SOURCE_HASH_MISMATCH", `${name} source hash is stale`);
|
|
solve.settingsHash = digest(value.settingsHash, `${name}.settingsHash`);
|
|
solve.reprojectionError = finite(value.reprojectionError, `${name}.reprojectionError`, 0, 1_000_000);
|
|
solve.focalLength = finite(value.focalLength, `${name}.focalLength`, 0.001, 100_000);
|
|
solve.principalPoint = vec2(value.principalPoint, `${name}.principalPoint`, -4, 4);
|
|
if (!Array.isArray(value.solvedFrames) || value.solvedFrames.length === 0 || value.solvedFrames.length > TRACKING_MASK_BUDGET.maxMarkers) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", `${name}.solvedFrames exceeds the budget`);
|
|
solve.solvedFrames = sortedFrames(value.solvedFrames.map((item, index) => ({ frame: integer(item, `${name}.solvedFrames[${index}]`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame) })), `${name}.solvedFrames`).map((item) => item.frame);
|
|
}
|
|
return solve;
|
|
}
|
|
|
|
export function parseTrackingMaskProject(value: unknown): TrackingMaskProjectIR {
|
|
if (!record(value) || value.schemaVersion !== TRACKING_MASK_SCHEMA || !Array.isArray(value.clips) || !Array.isArray(value.masks) || !Array.isArray(value.bindings)) throw new TrackingMaskValidationError("PROTOCOL_MISMATCH", "Unsupported TrackingMask project schema");
|
|
if (value.clips.length > TRACKING_MASK_BUDGET.maxClips || value.bindings.length > TRACKING_MASK_BUDGET.maxBindings) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", "Tracking project exceeds the clip or binding budget");
|
|
let trackCount = 0; let markerCount = 0; let planeTrackCount = 0; let layerCount = 0; let splineCount = 0; let pointCount = 0;
|
|
const clipIds = new Set<string>();
|
|
const clips = value.clips.map((clipValue, clipIndex): MovieClipIR => {
|
|
const name = `clips[${clipIndex}]`;
|
|
if (!record(clipValue) || !Array.isArray(clipValue.tracks) || !Array.isArray(clipValue.planeTracks)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is invalid`);
|
|
const id = text(clipValue.id, `${name}.id`); if (clipIds.has(id)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `Duplicate clip ${id}`); clipIds.add(id);
|
|
const frameStart = integer(clipValue.frameStart, `${name}.frameStart`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame);
|
|
const frameEnd = integer(clipValue.frameEnd, `${name}.frameEnd`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame);
|
|
if (frameEnd < frameStart) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} frame range is invalid`);
|
|
trackCount += clipValue.tracks.length; planeTrackCount += clipValue.planeTracks.length;
|
|
if (trackCount > TRACKING_MASK_BUDGET.maxTracks || planeTrackCount > TRACKING_MASK_BUDGET.maxPlaneTracks) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", "Tracking tracks exceed the budget");
|
|
const trackIds = new Set<string>();
|
|
const tracks = clipValue.tracks.map((trackValue, trackIndex): TrackingTrackIR => {
|
|
const trackName = `${name}.tracks[${trackIndex}]`;
|
|
if (!record(trackValue) || !Array.isArray(trackValue.markers)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${trackName} is invalid`);
|
|
const trackId = text(trackValue.id, `${trackName}.id`); if (trackIds.has(trackId)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `Duplicate track ${trackId}`); trackIds.add(trackId);
|
|
markerCount += trackValue.markers.length; if (markerCount > TRACKING_MASK_BUDGET.maxMarkers) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", "Tracking markers exceed the budget");
|
|
if (trackValue.hidden !== undefined && typeof trackValue.hidden !== "boolean") throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${trackName}.hidden is invalid`);
|
|
if (trackValue.useStab !== undefined && typeof trackValue.useStab !== "boolean") throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${trackName}.useStab is invalid`);
|
|
if (trackValue.useStabRot !== undefined && typeof trackValue.useStabRot !== "boolean") throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${trackName}.useStabRot is invalid`);
|
|
const track: TrackingTrackIR = { id: trackId, name: text(trackValue.name, `${trackName}.name`), selected: trackValue.selected === true, locked: trackValue.locked === true, markers: sortedFrames(trackValue.markers.map((item, markerIndex) => marker(item, `${trackName}.markers[${markerIndex}]`)), `${trackName}.markers`) };
|
|
if (trackValue.object !== undefined) track.object = text(trackValue.object, `${trackName}.object`, 128);
|
|
if (trackValue.hidden !== undefined) track.hidden = trackValue.hidden;
|
|
if (trackValue.useStab !== undefined) track.useStab = trackValue.useStab;
|
|
if (trackValue.useStabRot !== undefined) track.useStabRot = trackValue.useStabRot;
|
|
if (trackValue.hasBundle !== undefined) {
|
|
track.hasBundle = boolean(trackValue.hasBundle, `${trackName}.hasBundle`);
|
|
}
|
|
if (trackValue.averageError !== undefined) {
|
|
track.averageError = finite(trackValue.averageError, `${trackName}.averageError`, 0, 1_000_000);
|
|
}
|
|
if (trackValue.settings !== undefined) {
|
|
if (!record(trackValue.settings)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${trackName}.settings is invalid`);
|
|
const settings = trackValue.settings;
|
|
track.settings = {
|
|
motionModel: integer(settings.motionModel, `${trackName}.settings.motionModel`, 0, 5),
|
|
algorithm: integer(settings.algorithm, `${trackName}.settings.algorithm`, 0, 15),
|
|
minimumCorrelation: finite(settings.minimumCorrelation, `${trackName}.settings.minimumCorrelation`, 0, 1),
|
|
framesLimit: integer(settings.framesLimit, `${trackName}.settings.framesLimit`, 0, 10000),
|
|
patternMatch: integer(settings.patternMatch, `${trackName}.settings.patternMatch`, 0, 1),
|
|
margin: integer(settings.margin, `${trackName}.settings.margin`, 0, 10000),
|
|
weight: finite(settings.weight, `${trackName}.settings.weight`, 0, 1000),
|
|
weightStab: finite(settings.weightStab, `${trackName}.settings.weightStab`, 0, 1000),
|
|
};
|
|
for (const key of ["useCustomColor", "useRedChannel", "useGreenChannel", "useBlueChannel", "useGrayscalePreview", "useAlphaPreview"] as const) {
|
|
const value = settings[key];
|
|
if (value !== undefined) track.settings[key] = boolean(value, `${trackName}.settings.${key}`);
|
|
}
|
|
if (settings.color !== undefined) {
|
|
if (!Array.isArray(settings.color) || settings.color.length !== 4) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${trackName}.settings.color is invalid`);
|
|
track.settings.color = settings.color.map((item, index) => finite(item, `${trackName}.settings.color[${index}]`, 0, 1)) as [number, number, number, number];
|
|
}
|
|
}
|
|
return track;
|
|
});
|
|
const planeIds = new Set<string>();
|
|
const planeTracks = clipValue.planeTracks.map((planeValue, planeIndex): PlaneTrackIR => {
|
|
const planeName = `${name}.planeTracks[${planeIndex}]`;
|
|
if (!record(planeValue) || !Array.isArray(planeValue.pointTrackIds) || planeValue.pointTrackIds.length < 4 || planeValue.pointTrackIds.length > 64 || !Array.isArray(planeValue.keyframes)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${planeName} is invalid`);
|
|
const planeId = text(planeValue.id, `${planeName}.id`); if (planeIds.has(planeId)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `Duplicate plane track ${planeId}`); planeIds.add(planeId);
|
|
const pointTrackIds = planeValue.pointTrackIds.map((item, index) => text(item, `${planeName}.pointTrackIds[${index}]`));
|
|
if (new Set(pointTrackIds).size !== pointTrackIds.length || pointTrackIds.some((trackId) => !trackIds.has(trackId))) throw new TrackingMaskValidationError("TRACKING_BINDING_MISSING", `${planeName} references missing point tracks`);
|
|
const keyframes = sortedFrames(planeValue.keyframes.map((item, keyIndex): PlaneTrackKeyframeIR => {
|
|
if (!record(item) || !Array.isArray(item.corners) || item.corners.length !== 4) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${planeName}.keyframes[${keyIndex}] is invalid`);
|
|
return { frame: integer(item.frame, `${planeName}.keyframes[${keyIndex}].frame`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame), corners: item.corners.map((corner, cornerIndex) => vec2(corner, `${planeName}.keyframes[${keyIndex}].corners[${cornerIndex}]`, -4, 4)) as Quad2 };
|
|
}), `${planeName}.keyframes`);
|
|
return { id: planeId, name: text(planeValue.name, `${planeName}.name`), selected: planeValue.selected === true, hidden: planeValue.hidden === true, useAutoKeying: planeValue.useAutoKeying === true, pointTrackIds, keyframes };
|
|
});
|
|
const sourceStatus = clipValue.sourceStatus as MovieClipSourceStatus;
|
|
if (!CLIP_SOURCE_STATUSES.has(sourceStatus)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name}.sourceStatus is invalid`);
|
|
let sourcePath: string | undefined;
|
|
if (clipValue.sourcePath !== undefined || sourceStatus === "AVAILABLE") {
|
|
try { sourcePath = normalizeProjectAssetPath(text(clipValue.sourcePath, `${name}.sourcePath`, 2048)); } catch { throw new TrackingMaskValidationError("TRACKING_RESOURCE_OUTSIDE_PROJECT", `${name}.sourcePath is outside the project`); }
|
|
}
|
|
let sourceSha256: string | undefined;
|
|
if (sourceStatus === "AVAILABLE") sourceSha256 = digest(clipValue.sourceSha256, `${name}.sourceSha256`);
|
|
const hasFps = clipValue.fpsNumerator !== undefined || clipValue.fpsDenominator !== undefined;
|
|
if (hasFps && (clipValue.fpsNumerator === undefined || clipValue.fpsDenominator === undefined)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} fps numerator and denominator must be paired`);
|
|
const clip: MovieClipIR = { id, name: text(clipValue.name, `${name}.name`), sourceStatus, width: integer(clipValue.width, `${name}.width`, 0, 32768), height: integer(clipValue.height, `${name}.height`, 0, 32768), frameStart, frameEnd, tracks, planeTracks };
|
|
if (clipValue.activeObject !== undefined) clip.activeObject = text(clipValue.activeObject, `${name}.activeObject`, 128);
|
|
if (sourcePath !== undefined) clip.sourcePath = sourcePath;
|
|
if (sourceSha256 !== undefined) clip.sourceSha256 = sourceSha256;
|
|
if (hasFps) { clip.fpsNumerator = integer(clipValue.fpsNumerator, `${name}.fpsNumerator`, 1, 1_000_000); clip.fpsDenominator = integer(clipValue.fpsDenominator, `${name}.fpsDenominator`, 1, 1_000_000); }
|
|
const solve = cameraSolve(clipValue.cameraSolve, sourceSha256, `${name}.cameraSolve`);
|
|
if (solve !== undefined) clip.cameraSolve = solve;
|
|
if (clipValue.proxy !== undefined) {
|
|
if (!record(clipValue.proxy)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name}.proxy is invalid`);
|
|
if (typeof clipValue.proxy.enabled !== "boolean") throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name}.proxy.enabled is invalid`);
|
|
clip.proxy = { enabled: clipValue.proxy.enabled, buildSizes: integer(clipValue.proxy.buildSizes, `${name}.proxy.buildSizes`, 0, 255), quality: integer(clipValue.proxy.quality, `${name}.proxy.quality`, 0, 100) };
|
|
}
|
|
if (clipValue.settings !== undefined) {
|
|
if (!record(clipValue.settings)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name}.settings is invalid`);
|
|
const settings = clipValue.settings;
|
|
clip.settings = {
|
|
defaultMotionModel: integer(settings.defaultMotionModel, `${name}.settings.defaultMotionModel`, 0, 5),
|
|
defaultAlgorithm: integer(settings.defaultAlgorithm, `${name}.settings.defaultAlgorithm`, 0, 15),
|
|
defaultMinimumCorrelation: finite(settings.defaultMinimumCorrelation, `${name}.settings.defaultMinimumCorrelation`, 0, 1),
|
|
defaultPatternSize: integer(settings.defaultPatternSize, `${name}.settings.defaultPatternSize`, 0, 10000),
|
|
defaultSearchSize: integer(settings.defaultSearchSize, `${name}.settings.defaultSearchSize`, 0, 10000),
|
|
defaultFramesLimit: integer(settings.defaultFramesLimit, `${name}.settings.defaultFramesLimit`, 0, 10000),
|
|
defaultMargin: integer(settings.defaultMargin, `${name}.settings.defaultMargin`, 0, 10000),
|
|
defaultPatternMatch: integer(settings.defaultPatternMatch, `${name}.settings.defaultPatternMatch`, 0, 1),
|
|
defaultWeight: finite(settings.defaultWeight, `${name}.settings.defaultWeight`, 0, 1000),
|
|
motionFlag: integer(settings.motionFlag, `${name}.settings.motionFlag`, 0, 1),
|
|
refine: integer(settings.refine, `${name}.settings.refine`, 0, 15),
|
|
};
|
|
if (settings.focal !== undefined) clip.settings.focal = finite(settings.focal, `${name}.settings.focal`, 0, 10_000_000);
|
|
if (settings.sensorWidth !== undefined) clip.settings.sensorWidth = finite(settings.sensorWidth, `${name}.settings.sensorWidth`, 0, 100_000);
|
|
if (settings.pixelAspect !== undefined) clip.settings.pixelAspect = finite(settings.pixelAspect, `${name}.settings.pixelAspect`, 0.0001, 1000);
|
|
if (settings.units !== undefined) clip.settings.units = integer(settings.units, `${name}.settings.units`, 0, 2);
|
|
if (settings.distortionModel !== undefined) clip.settings.distortionModel = integer(settings.distortionModel, `${name}.settings.distortionModel`, 0, 5);
|
|
if (settings.principal !== undefined) clip.settings.principal = vec2(settings.principal, `${name}.settings.principal`, -2, 2);
|
|
for (const key of ["k1", "k2", "k3", "divisionK1", "divisionK2", "nukeK1", "nukeK2", "brownK1", "brownK2", "brownK3", "brownK4"] as const) {
|
|
if (settings[key] !== undefined) clip.settings[key] = finite(settings[key] as number, `${name}.settings.${key}`, -1000, 1000);
|
|
}
|
|
}
|
|
if (clipValue.stabilization !== undefined) {
|
|
if (!record(clipValue.stabilization)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name}.stabilization is invalid`);
|
|
const stab = clipValue.stabilization;
|
|
clip.stabilization = {
|
|
enabled: stab.enabled === true,
|
|
autoscale: stab.autoscale === true,
|
|
useRotation: stab.useRotation === true,
|
|
useScale: stab.useScale === true,
|
|
anchorFrame: integer(stab.anchorFrame, `${name}.stabilization.anchorFrame`, -TRACKING_MASK_BUDGET.maxFrame, TRACKING_MASK_BUDGET.maxFrame),
|
|
targetPos: vec2(stab.targetPos, `${name}.stabilization.targetPos`, -1, 1),
|
|
targetRot: finite(stab.targetRot, `${name}.stabilization.targetRot`, -1e6, 1e6),
|
|
scale: finite(stab.scale, `${name}.stabilization.scale`, 0.0001, 1000),
|
|
locinf: finite(stab.locinf, `${name}.stabilization.locinf`, 0, 1),
|
|
scaleinf: finite(stab.scaleinf, `${name}.stabilization.scaleinf`, 0, 1),
|
|
rotinf: finite(stab.rotinf, `${name}.stabilization.rotinf`, 0, 1),
|
|
filter: integer(stab.filter, `${name}.stabilization.filter`, 0, 2),
|
|
maxscale: finite(stab.maxscale, `${name}.stabilization.maxscale`, 0, 100),
|
|
};
|
|
if (stab.showStabTracks !== undefined) clip.stabilization.showStabTracks = stab.showStabTracks === true;
|
|
if (stab.totalTracks !== undefined) clip.stabilization.totalTracks = integer(stab.totalTracks, `${name}.stabilization.totalTracks`, 0, TRACKING_MASK_BUDGET.maxTracks);
|
|
if (stab.totalRotationTracks !== undefined) clip.stabilization.totalRotationTracks = integer(stab.totalRotationTracks, `${name}.stabilization.totalRotationTracks`, 0, TRACKING_MASK_BUDGET.maxTracks);
|
|
}
|
|
return clip;
|
|
});
|
|
const maskIds = new Set<string>();
|
|
const masks = value.masks.map((maskValue, maskIndex): MaskIR => {
|
|
const name = `masks[${maskIndex}]`; if (!record(maskValue) || !Array.isArray(maskValue.layers)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `${name} is invalid`);
|
|
const id = text(maskValue.id, `${name}.id`); if (maskIds.has(id)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `Duplicate mask ${id}`); maskIds.add(id);
|
|
layerCount += maskValue.layers.length; if (layerCount > TRACKING_MASK_BUDGET.maxMaskLayers) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", "Mask layers exceed the budget");
|
|
const layerIds = new Set<string>();
|
|
const layers = maskValue.layers.map((layerValue, layerIndex): MaskLayerIR => {
|
|
const layerName = `${name}.layers[${layerIndex}]`; if (!record(layerValue) || !Array.isArray(layerValue.splines)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `${layerName} is invalid`);
|
|
const layerId = text(layerValue.id, `${layerName}.id`); if (layerIds.has(layerId)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `Duplicate mask layer ${layerId}`); layerIds.add(layerId);
|
|
splineCount += layerValue.splines.length; if (splineCount > TRACKING_MASK_BUDGET.maxSplines) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", "Mask splines exceed the budget");
|
|
const splineIds = new Set<string>();
|
|
const splines = layerValue.splines.map((splineValue, splineIndex): MaskSplineIR => {
|
|
const splineName = `${layerName}.splines[${splineIndex}]`; if (!record(splineValue) || !Array.isArray(splineValue.points)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `${splineName} is invalid`);
|
|
const splineId = text(splineValue.id, `${splineName}.id`); if (splineIds.has(splineId)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `Duplicate mask spline ${splineId}`); splineIds.add(splineId);
|
|
pointCount += splineValue.points.length; if (pointCount > TRACKING_MASK_BUDGET.maxMaskPoints) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", "Mask points exceed the budget");
|
|
const pointIds = new Set<string>(); const points = splineValue.points.map((item, pointIndex) => { const point = maskPoint(item, `${splineName}.points[${pointIndex}]`); if (pointIds.has(point.id)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `Duplicate mask point ${point.id}`); pointIds.add(point.id); return point; });
|
|
if (points.length < 2) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `${splineName} needs at least two points`);
|
|
return { id: splineId, cyclic: splineValue.cyclic === true, fill: splineValue.fill !== false, points };
|
|
});
|
|
return { id: layerId, name: text(layerValue.name, `${layerName}.name`), visible: layerValue.visible !== false, locked: layerValue.locked === true, opacity: finite(layerValue.opacity, `${layerName}.opacity`, 0, 1), splines };
|
|
});
|
|
return { id, name: text(maskValue.name, `${name}.name`), layers };
|
|
});
|
|
const bindingIds = new Set<string>();
|
|
const bindings = value.bindings.map((bindingValue, index): TrackingMaskBindingIR => {
|
|
const name = `bindings[${index}]`; if (!record(bindingValue) || !["COMPOSITOR", "SCENE"].includes(bindingValue.target as string)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${name} is invalid`);
|
|
const id = text(bindingValue.id, `${name}.id`); if (bindingIds.has(id)) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `Duplicate binding ${id}`); bindingIds.add(id);
|
|
const binding: TrackingMaskBindingIR = { id, target: bindingValue.target as TrackingMaskBindingIR["target"], ownerId: text(bindingValue.ownerId, `${name}.ownerId`) };
|
|
if (bindingValue.clipId !== undefined) binding.clipId = text(bindingValue.clipId, `${name}.clipId`);
|
|
if (bindingValue.maskId !== undefined) binding.maskId = text(bindingValue.maskId, `${name}.maskId`);
|
|
if ((!binding.clipId && !binding.maskId) || (binding.clipId && !clipIds.has(binding.clipId)) || (binding.maskId && !maskIds.has(binding.maskId))) throw new TrackingMaskValidationError("TRACKING_BINDING_MISSING", `${name} references missing resources`);
|
|
return binding;
|
|
});
|
|
return { schemaVersion: TRACKING_MASK_SCHEMA, revision: integer(value.revision, "revision", 0, Number.MAX_SAFE_INTEGER), clips, masks, bindings };
|
|
}
|
|
|
|
export function applyTrackingMaskEdit(value: unknown, edit: TrackingMaskEditIR): TrackingMaskProjectIR {
|
|
const project = parseTrackingMaskProject(value);
|
|
if (edit.revision !== project.revision) throw new TrackingMaskValidationError("REVISION_CONFLICT", "Tracking/Mask edit revision is stale");
|
|
const clone = structuredClone(project);
|
|
if (edit.type === "SET_MARKER" || edit.type === "DELETE_MARKER" || edit.type === "SET_TRACK_SELECTION") {
|
|
const clip = clone.clips.find((item) => item.id === edit.clipId); const track = clip?.tracks.find((item) => item.id === edit.trackId);
|
|
if (!track) throw new TrackingMaskValidationError("TRACKING_BINDING_MISSING", `Unknown track ${edit.trackId}`);
|
|
if (track.locked) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `${track.id} is locked`);
|
|
if (edit.type === "SET_TRACK_SELECTION") track.selected = edit.selected;
|
|
else if (edit.type === "DELETE_MARKER") {
|
|
const index = track.markers.findIndex((item) => item.frame === edit.frame); if (index < 0) throw new TrackingMaskValidationError("TRACKING_SCHEMA_INVALID", `Marker frame ${edit.frame} is missing`); track.markers.splice(index, 1);
|
|
}
|
|
else {
|
|
const next = marker(edit.marker, "edit.marker"); const index = track.markers.findIndex((item) => item.frame === next.frame); if (index < 0) track.markers.push(next); else track.markers[index] = next;
|
|
}
|
|
}
|
|
else {
|
|
const maskValue = clone.masks.find((item) => item.id === edit.maskId); const layer = maskValue?.layers.find((item) => item.id === edit.layerId); const spline = layer?.splines.find((item) => item.id === edit.splineId);
|
|
if (!spline || !layer) throw new TrackingMaskValidationError("TRACKING_BINDING_MISSING", `Unknown mask spline ${edit.splineId}`);
|
|
if (layer.locked) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `${layer.id} is locked`);
|
|
if (edit.type === "SET_SPLINE_CYCLIC") spline.cyclic = edit.cyclic;
|
|
else { const next = maskPoint(edit.point, "edit.point"); const index = spline.points.findIndex((item) => item.id === next.id); if (index < 0) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `Unknown mask point ${next.id}`); spline.points[index] = next; }
|
|
}
|
|
clone.revision += 1;
|
|
return parseTrackingMaskProject(clone);
|
|
}
|
|
|
|
export function gateTrackingOperation(operation: "MARKER_EDIT" | "MASK_EDIT" | "BROWSER_TRACKING" | "CAMERA_SOLVE", browserProbe: "VERIFIED" | "UNAVAILABLE" | "UNVERIFIED" = "UNVERIFIED"): CapabilityGateResult {
|
|
if (operation === "MARKER_EDIT" || operation === "MASK_EDIT" || operation === "CAMERA_SOLVE") return readyGate("N-022", operation);
|
|
if (operation === "BROWSER_TRACKING" && browserProbe === "VERIFIED") return readyGate("N-022", operation);
|
|
return blockedGate("N-022", operation, [capabilityIssue("TRACKING_SOLVE_UNAVAILABLE", "Browser tracking requires an explicit feature probe")]);
|
|
}
|
|
|
|
function bezierPoint(a: Vec2, b: Vec2, c: Vec2, d: Vec2, t: number): Vec2 {
|
|
const inverse = 1 - t;
|
|
return [inverse ** 3 * a[0] + 3 * inverse ** 2 * t * b[0] + 3 * inverse * t ** 2 * c[0] + t ** 3 * d[0], inverse ** 3 * a[1] + 3 * inverse ** 2 * t * b[1] + 3 * inverse * t ** 2 * c[1] + t ** 3 * d[1]];
|
|
}
|
|
|
|
export function raycastMaskProject(value: unknown, positionValue: unknown, thresholdValue = 0.02, segmentSamples = 24): MaskRaycastHitIR | null {
|
|
const project = parseTrackingMaskProject(value);
|
|
const position = vec2(positionValue, "position", -4, 4);
|
|
const threshold = finite(thresholdValue, "threshold", 0.000001, 1);
|
|
const samples = integer(segmentSamples, "segmentSamples", 2, 128);
|
|
let best: MaskRaycastHitIR | null = null;
|
|
const consider = (hit: MaskRaycastHitIR): void => { if (hit.distance <= threshold && (!best || hit.distance < best.distance || (hit.distance === best.distance && hit.kind === "POINT" && best.kind === "SEGMENT"))) best = hit; };
|
|
for (const mask of project.masks) for (const layer of mask.layers) {
|
|
if (!layer.visible || layer.locked || layer.opacity <= 0) continue;
|
|
for (const spline of layer.splines) {
|
|
for (const point of spline.points) consider({ maskId: mask.id, layerId: layer.id, splineId: spline.id, kind: "POINT", pointId: point.id, distance: Math.hypot(point.co[0] - position[0], point.co[1] - position[1]) });
|
|
const segmentCount = spline.cyclic ? spline.points.length : spline.points.length - 1;
|
|
for (let segment = 0; segment < segmentCount; segment++) {
|
|
const first = spline.points[segment]; const next = spline.points[(segment + 1) % spline.points.length];
|
|
for (let sample = 0; sample <= samples; sample++) {
|
|
const parameter = sample / samples;
|
|
const point = bezierPoint(first.co, first.handleRight, next.handleLeft, next.co, parameter);
|
|
consider({ maskId: mask.id, layerId: layer.id, splineId: spline.id, kind: "SEGMENT", pointId: first.id, nextPointId: next.id, distance: Math.hypot(point[0] - position[0], point[1] - position[1]), parameter });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
function maskSelectionKey(selection: MaskPointSelectionIR): string {
|
|
return `${selection.maskId}\0${selection.layerId}\0${selection.splineId}\0${selection.pointId}`;
|
|
}
|
|
|
|
/** Applies deterministic replace/add/toggle marquee selection to editable Mask control points. */
|
|
export function selectMaskPointsInBounds(
|
|
value: unknown,
|
|
minimumValue: unknown,
|
|
maximumValue: unknown,
|
|
currentValue: unknown = [],
|
|
mode: "REPLACE" | "ADD" | "TOGGLE" = "REPLACE",
|
|
): MaskPointSelectionIR[] {
|
|
const project = parseTrackingMaskProject(value);
|
|
if (!(["REPLACE", "ADD", "TOGGLE"] as const).includes(mode)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", "Mask selection mode is invalid");
|
|
const minimum = vec2(minimumValue, "minimum", -4, 4);
|
|
const maximum = vec2(maximumValue, "maximum", -4, 4);
|
|
if (minimum[0] > maximum[0] || minimum[1] > maximum[1]) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", "Mask selection bounds are inverted");
|
|
if (!Array.isArray(currentValue) || currentValue.length > TRACKING_MASK_BUDGET.maxMaskPoints) throw new TrackingMaskValidationError("TRACKING_BUDGET_EXCEEDED", "Mask selection exceeds the point budget");
|
|
const all: MaskPointSelectionIR[] = [];
|
|
const editable = new Set<string>();
|
|
for (const mask of project.masks) for (const layer of mask.layers) for (const spline of layer.splines) for (const point of spline.points) {
|
|
const selection = { maskId: mask.id, layerId: layer.id, splineId: spline.id, pointId: point.id };
|
|
all.push(selection);
|
|
if (layer.visible && !layer.locked && layer.opacity > 0) editable.add(maskSelectionKey(selection));
|
|
}
|
|
const allKeys = new Set(all.map(maskSelectionKey));
|
|
const current = new Set<string>();
|
|
currentValue.forEach((item, index) => {
|
|
if (!record(item)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `current[${index}] is invalid`);
|
|
const selection = { maskId: text(item.maskId, `current[${index}].maskId`), layerId: text(item.layerId, `current[${index}].layerId`), splineId: text(item.splineId, `current[${index}].splineId`), pointId: text(item.pointId, `current[${index}].pointId`) };
|
|
const key = maskSelectionKey(selection);
|
|
if (!allKeys.has(key)) throw new TrackingMaskValidationError("TRACKING_BINDING_MISSING", `current[${index}] references a missing Mask point`);
|
|
if (current.has(key)) throw new TrackingMaskValidationError("MASK_SCHEMA_INVALID", `current[${index}] is duplicated`);
|
|
current.add(key);
|
|
});
|
|
const hits = new Set<string>();
|
|
for (const mask of project.masks) for (const layer of mask.layers) {
|
|
if (!layer.visible || layer.locked || layer.opacity <= 0) continue;
|
|
for (const spline of layer.splines) for (const point of spline.points) {
|
|
if (point.co[0] >= minimum[0] && point.co[0] <= maximum[0] && point.co[1] >= minimum[1] && point.co[1] <= maximum[1]) {
|
|
hits.add(maskSelectionKey({ maskId: mask.id, layerId: layer.id, splineId: spline.id, pointId: point.id }));
|
|
}
|
|
}
|
|
}
|
|
const selected = mode === "REPLACE" ? new Set<string>() : new Set(current);
|
|
for (const key of hits) {
|
|
if (!editable.has(key)) continue;
|
|
if (mode === "TOGGLE" && selected.has(key)) selected.delete(key);
|
|
else selected.add(key);
|
|
}
|
|
return all.filter((selection) => selected.has(maskSelectionKey(selection)));
|
|
}
|