Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

11.4 KB
Raw Blame
/** Path topology adapted from the v6 spline implementation (c4c0594261). */
import type { CanvasBezierPathContract, CanvasBezierPointContract } from '@workbench/canvas-engine/contracts';
import type { Vec2 } from '@workbench/canvas-engine/types';

import { setBezierPointType } from './bezierPath';

type TopologyResult = {
  path: CanvasBezierPathContract;
  activePointIndex: number;
};

type VectorPathPointRef = { pathId: string; pointIndex: number };

type SplitVectorPathResult = {
  paths: CanvasBezierPathContract[];
  activePathId: string;
  activePointIndex: number;
};

type DeleteVectorPathPointsResult = {
  paths: CanvasBezierPathContract[];
  didDelete: boolean;
};

const translateHandle = (handle: Vec2 | null, from: Vec2, to: Vec2): Vec2 | null =>
  handle ? { x: handle.x + to.x - from.x, y: handle.y + to.y - from.y } : null;

const reversePoints = (path: CanvasBezierPathContract): CanvasBezierPathContract['points'] =>
  [...path.points].reverse().map((point) => ({
    ...point,
    inHandle: point.outHandle,
    outHandle: point.inHandle,
  }));

const prepareJoinedEndpoint = (point: CanvasBezierPointContract, connectingHandle: 'inHandle' | 'outHandle') => {
  if (point.type === 'corner') {
    point[connectingHandle] = null;
    return;
  }
  // Keep the existing segment's tangent and restore the handle used by the new segment.
  setBezierPointType(point, point.type, connectingHandle === 'inHandle' ? 'outHandle' : 'inHandle');
};

export const deleteVectorPathPoints = (
  sourcePaths: CanvasBezierPathContract[],
  pointRefs: VectorPathPointRef[]
): DeleteVectorPathPointsResult => {
  const selectedPointIndices = new Map<string, Set<number>>();
  for (const pointRef of pointRefs) {
    const indices = selectedPointIndices.get(pointRef.pathId) ?? new Set<number>();
    indices.add(pointRef.pointIndex);
    selectedPointIndices.set(pointRef.pathId, indices);
  }

  let didDelete = false;
  const paths = structuredClone(sourcePaths).filter((path) => {
    const pointIndices = [...(selectedPointIndices.get(path.id) ?? [])]
      .filter((pointIndex) => path.points[pointIndex] !== undefined)
      .sort((a, b) => b - a);
    if (pointIndices.length === 0) {
      return true;
    }

    didDelete = true;
    const minPointCount = path.isClosed ? 3 : 2;
    if (path.points.length - pointIndices.length < minPointCount) {
      return false;
    }

    const deletedIndices = new Set(pointIndices);
    path.points = path.points.filter((point, pointIndex, points) => {
      if (deletedIndices.has(pointIndex)) {
        return false;
      }
      const previousIndex = path.isClosed ? (pointIndex - 1 + points.length) % points.length : pointIndex - 1;
      const nextIndex = path.isClosed ? (pointIndex + 1) % points.length : pointIndex + 1;
      const previousDeleted = deletedIndices.has(previousIndex);
      const nextDeleted = deletedIndices.has(nextIndex);
      if (previousDeleted || nextDeleted) {
        // Preserve the tangent on the unchanged side of the surviving point.
        setBezierPointType(point, point.type, nextDeleted && !previousDeleted ? 'inHandle' : 'outHandle');
      }
      return true;
    });
    return true;
  });

  return { paths, didDelete };
};

export const canSplitVectorPathAtPoints = (
  paths: CanvasBezierPathContract[],
  pointRefs: VectorPathPointRef[]
): boolean => {
  return pointRefs.some((pointRef) => {
    const path = paths.find((candidate) => candidate.id === pointRef.pathId);
    return Boolean(
      path?.points[pointRef.pointIndex] &&
      (path.isClosed || (pointRef.pointIndex > 0 && pointRef.pointIndex < path.points.length - 1))
    );
  });
};

export const canJoinVectorPathEndpoints = (
  paths: CanvasBezierPathContract[],
  pointRefs: VectorPathPointRef[],
  weldRadius: number,
  project: (point: Vec2) => Vec2 = (point) => point
): boolean => {
  if (pointRefs.length !== 2) {
    return false;
  }
  const [firstRef, secondRef] = pointRefs;
  if (
    !firstRef ||
    !secondRef ||
    (firstRef.pathId === secondRef.pathId && firstRef.pointIndex === secondRef.pointIndex)
  ) {
    return false;
  }
  const firstPath = paths.find((path) => path.id === firstRef.pathId);
  const secondPath = paths.find((path) => path.id === secondRef.pathId);
  if (!firstPath || !secondPath || firstPath.isClosed || secondPath.isClosed) {
    return false;
  }
  const isEndpoint = (path: CanvasBezierPathContract, pointIndex: number) =>
    pointIndex === 0 || pointIndex === path.points.length - 1;
  if (!isEndpoint(firstPath, firstRef.pointIndex) || !isEndpoint(secondPath, secondRef.pointIndex)) {
    return false;
  }
  if (firstPath.id !== secondPath.id) {
    return true;
  }
  const firstPoint = firstPath.points[firstRef.pointIndex];
  const secondPoint = secondPath.points[secondRef.pointIndex];
  if (!firstPoint || !secondPoint) {
    return false;
  }
  const first = project(firstPoint.anchor),
    second = project(secondPoint.anchor);
  const shouldWeld = Math.hypot(first.x - second.x, first.y - second.y) <= weldRadius;
  return !shouldWeld || firstPath.points.length >= 4;
};

export const splitVectorPathAtPoint = (
  sourcePath: CanvasBezierPathContract,
  pointIndex: number,
  newPathId: string
): SplitVectorPathResult | null => splitVectorPathAtPoints(sourcePath, [pointIndex], () => newPathId);

export const splitVectorPathAtPoints = (
  sourcePath: CanvasBezierPathContract,
  pointIndices: number[],
  getNewPathId: () => string
): SplitVectorPathResult | null => {
  const path = structuredClone(sourcePath);
  const validPointIndices = pointIndices.filter((pointIndex) => path.points[pointIndex] !== undefined);
  const uniquePointIndices = [...new Set(validPointIndices)].sort((a, b) => a - b);
  if (uniquePointIndices.length === 0) {
    return null;
  }

  if (path.isClosed) {
    const splitPaths = uniquePointIndices.map((startIndex, splitIndex) => {
      const endIndex = uniquePointIndices[(splitIndex + 1) % uniquePointIndices.length] ?? startIndex;
      const points: CanvasBezierPathContract['points'] = [];
      let pointIndex = startIndex;
      do {
        const point = path.points[pointIndex];
        if (point) {
          points.push(structuredClone(point));
        }
        pointIndex = (pointIndex + 1) % path.points.length;
      } while (pointIndex !== endIndex);
      const endPoint = path.points[endIndex];
      if (endPoint) {
        points.push(structuredClone(endPoint));
      }
      const firstPoint = points[0];
      const lastPoint = points.at(-1);
      if (firstPoint) {
        firstPoint.inHandle = null;
      }
      if (lastPoint) {
        lastPoint.outHandle = null;
      }
      return {
        ...path,
        id: splitIndex === 0 ? path.id : getNewPathId(),
        isClosed: false,
        points,
      };
    });
    const activeSplitPointIndex = validPointIndices.at(-1);
    const activePath = splitPaths[uniquePointIndices.indexOf(activeSplitPointIndex ?? -1)];
    if (!activePath) {
      return null;
    }
    return { paths: splitPaths, activePathId: activePath.id, activePointIndex: 0 };
  }

  const internalPointIndices = uniquePointIndices.filter(
    (pointIndex) => pointIndex > 0 && pointIndex < path.points.length - 1
  );
  if (internalPointIndices.length === 0) {
    return null;
  }

  const boundaries = [0, ...internalPointIndices, path.points.length - 1];
  const splitPaths = boundaries.slice(0, -1).map((startIndex, splitIndex) => {
    const endIndex = boundaries[splitIndex + 1] ?? startIndex;
    const points = structuredClone(path.points.slice(startIndex, endIndex + 1));
    const firstPoint = points[0];
    const lastPoint = points.at(-1);
    if (splitIndex > 0 && firstPoint) {
      firstPoint.inHandle = null;
    }
    if (splitIndex < boundaries.length - 2 && lastPoint) {
      lastPoint.outHandle = null;
    }
    return {
      ...path,
      id: splitIndex === 0 ? path.id : getNewPathId(),
      points,
    };
  });
  const activeInternalPointIndex = [...validPointIndices]
    .reverse()
    .find((pointIndex) => internalPointIndices.includes(pointIndex));
  if (activeInternalPointIndex === undefined) {
    return null;
  }
  const activePath = splitPaths[internalPointIndices.indexOf(activeInternalPointIndex) + 1];
  if (!activePath) {
    return null;
  }
  return { paths: splitPaths, activePathId: activePath.id, activePointIndex: 0 };
};

export const joinVectorPathEndpoints = (
  sourcePathState: CanvasBezierPathContract,
  sourcePointIndex: number,
  targetPathState: CanvasBezierPathContract,
  targetPointIndex: number,
  shouldWeld: boolean
): TopologyResult | null => {
  if (sourcePathState.isClosed || targetPathState.isClosed) {
    return null;
  }
  const sourcePath = structuredClone(sourcePathState);
  const targetPath = sourcePath.id === targetPathState.id ? sourcePath : structuredClone(targetPathState);
  const isEndpoint = (path: CanvasBezierPathContract, pointIndex: number) =>
    pointIndex === 0 || pointIndex === path.points.length - 1;
  if (!isEndpoint(sourcePath, sourcePointIndex) || !isEndpoint(targetPath, targetPointIndex)) {
    return null;
  }

  const sourcePoint = sourcePath.points[sourcePointIndex];
  const targetPoint = targetPath.points[targetPointIndex];
  if (!sourcePoint || !targetPoint) {
    return null;
  }

  if (sourcePath.id === targetPath.id) {
    const firstPoint = sourcePath.points[0];
    const lastPoint = sourcePath.points.at(-1);
    if (!firstPoint || !lastPoint || (shouldWeld && sourcePath.points.length < 4)) {
      return null;
    }
    if (shouldWeld) {
      const joinedPoint: CanvasBezierPointContract = {
        anchor: { ...targetPoint.anchor },
        inHandle: translateHandle(lastPoint.inHandle, lastPoint.anchor, targetPoint.anchor),
        outHandle: translateHandle(firstPoint.outHandle, firstPoint.anchor, targetPoint.anchor),
        type: targetPoint.type,
      };
      setBezierPointType(joinedPoint, joinedPoint.type, targetPointIndex === 0 ? 'outHandle' : 'inHandle');
      sourcePath.points = [joinedPoint, ...sourcePath.points.slice(1, -1)];
      sourcePath.isClosed = true;
      return { path: sourcePath, activePointIndex: 0 };
    }

    prepareJoinedEndpoint(firstPoint, 'inHandle');
    prepareJoinedEndpoint(lastPoint, 'outHandle');
    sourcePath.isClosed = true;
    return { path: sourcePath, activePointIndex: targetPointIndex };
  }

  const sourcePoints = sourcePointIndex === 0 ? reversePoints(sourcePath) : sourcePath.points;
  const targetPoints =
    targetPointIndex === targetPath.points.length - 1 ? reversePoints(targetPath) : targetPath.points;
  const sourceEndPoint = sourcePoints.at(-1);
  const targetStartPoint = targetPoints[0];
  if (!sourceEndPoint || !targetStartPoint) {
    return null;
  }

  if (shouldWeld) {
    const joinedPoint: CanvasBezierPointContract = {
      anchor: { ...targetStartPoint.anchor },
      inHandle: translateHandle(sourceEndPoint.inHandle, sourceEndPoint.anchor, targetStartPoint.anchor),
      outHandle: targetStartPoint.outHandle,
      type: targetStartPoint.type,
    };
    setBezierPointType(joinedPoint, joinedPoint.type, 'outHandle');
    return {
      path: {
        ...sourcePath,
        points: [...sourcePoints.slice(0, -1), joinedPoint, ...targetPoints.slice(1)],
        isClosed: false,
      },
      activePointIndex: sourcePoints.length - 1,
    };
  }

  prepareJoinedEndpoint(sourceEndPoint, 'outHandle');
  prepareJoinedEndpoint(targetStartPoint, 'inHandle');
  return {
    path: {
      ...sourcePath,
      points: [...sourcePoints, ...targetPoints],
      isClosed: false,
    },
    activePointIndex: sourcePoints.length,
  };
};