All files / packages/tools/src/tools/annotation/splines CubicSpline.ts

1.03% Statements 1/97
0% Branches 0/51
0% Functions 0/8
1.07% Lines 1/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289                  1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
import { vec4, mat4 } from 'gl-matrix';
import { Types } from '@cornerstonejs/core';
import { Spline } from './Spline';
import * as math from '../../../utilities/math';
import type { SplineCurveSegment, SplineLineSegment } from '../../../types';
 
// The `u` in Parameter Space used when spliting a curve segment into line segments must
// be greater than or equal to `curveSegmentIndex` and smaller than `curveSegmentIndex + 1`.
// In this case we are using `curveSegmentIndex + 1 - MAX_U_ERROR`
const MAX_U_ERROR = 1e-8;
 
/**
 * Base class for all cubic splines
 */
abstract class CubicSpline extends Spline {
  protected getPreviewCurveSegments(
    controlPointPreview: Types.Point2,
    closeSpline: boolean
  ): SplineCurveSegment[] {
    const previewNumCurveSegments = this._getNumCurveSegments() + 1;
    const startCurveSegIndex = Math.max(0, previewNumCurveSegments - 2);
    const endCurveSegIndex = closeSpline
      ? previewNumCurveSegments
      : previewNumCurveSegments - 1;
    const transformMatrix = this.getTransformMatrix();
    const controlPoints = [...this.controlPoints];
    const curveSegments: SplineCurveSegment[] = [];
 
    if (!closeSpline) {
      controlPoints.push(controlPointPreview);
    }
 
    for (let i = startCurveSegIndex; i <= endCurveSegIndex; i++) {
      const curveSegment = this._getCurveSegment(
        i,
        transformMatrix,
        controlPoints,
        closeSpline
      );
 
      curveSegments.push(curveSegment);
    }
 
    return curveSegments;
  }
 
  protected getSplineCurves(): SplineCurveSegment[] {
    const numCurveSegments = this._getNumCurveSegments();
    const curveSegments: SplineCurveSegment[] = new Array(numCurveSegments);
 
    if (numCurveSegments <= 0) {
      return [];
    }
 
    const transformMatrix = this.getTransformMatrix();
    let previousCurveSegmentsLength = 0;
 
    for (let i = 0; i < numCurveSegments; i++) {
      const curveSegment = this._getCurveSegment(i, transformMatrix);
 
      curveSegment.previousCurveSegmentsLength = previousCurveSegmentsLength;
      curveSegments[i] = curveSegment;
 
      previousCurveSegmentsLength += curveSegment.length;
    }
 
    return curveSegments;
  }
 
  private _getNumCurveSegments(
    controlPoints: Types.Point2[] = this.controlPoints,
    closed: boolean = this.closed
  ): number {
    return closed
      ? controlPoints.length
      : Math.max(0, controlPoints.length - 1);
  }
 
  /**
   * Get a point on a spline curve given `u` value
   *
   * @param u - `u` value in Parameter Space that must be between 0 and N where N is the number of
   *   curve segments for opened splines or any negative/positive number for closed splines
   * @returns - Point (x, y) on the spline. It may return `undefined` when `u` is smaller than 0
   *   or greater than N for opened splines
   */
  private _getPoint(
    u: number,
    transformMatrix: number[],
    controlPoints: Types.Point2[] = this.controlPoints,
    closed: boolean = this.closed
  ): Types.Point2 {
    const numCurveSegments = this._getNumCurveSegments(controlPoints, closed);
    const uInt = Math.floor(u);
    let curveSegmentIndex = uInt % numCurveSegments;
 
    // `t` must be between 0 and 1
    const t = u - uInt;
 
    const curveSegmentIndexOutOfBounds =
      curveSegmentIndex < 0 || curveSegmentIndex >= numCurveSegments;
 
    if (curveSegmentIndexOutOfBounds) {
      if (this.closed) {
        // Wraps around when the index is negative or greater than or equal to `numSegments`
        curveSegmentIndex =
          (numCurveSegments + curveSegmentIndex) % numCurveSegments;
      } else {
        // Point is not on the spline curve
        return;
      }
    }
 
    const { p0, p1, p2, p3 } = this._getCurveSegmentPoints(
      curveSegmentIndex,
      controlPoints,
      closed
    );
 
    // Formula to find any point on a cubic spline curve given a `t` value
    //
    // P(t) = [1  t  t2  t3] | m00 m01 m02 m03 |  | P0 |
    //                       | m10 m11 m12 m13 |  | P1 |
    //                       | m20 m21 m22 m23 |  | P2 |
    //                       | m30 m31 m32 m33 |  | P3 |
 
    const tt = t * t;
    const ttt = tt * t;
    const tValues = vec4.fromValues(1, t, tt, ttt);
 
    // Influential field values which tell us how much P0, P1, P2 and P3 influence
    // each point of the curve
    const qValues = vec4.transformMat4(
      vec4.create(),
      tValues,
      transformMatrix as mat4
    );
 
    return [
      vec4.dot(qValues, vec4.fromValues(p0[0], p1[0], p2[0], p3[0])),
      vec4.dot(qValues, vec4.fromValues(p0[1], p1[1], p2[1], p3[1])),
    ] as Types.Point2;
  }
 
  private _getCurveSegmentPoints(
    curveSegmentIndex: number,
    controlPoints: Types.Point2[] = this.controlPoints,
    closed: boolean = this.closed
  ) {
    const numCurveSegments = this._getNumCurveSegments(controlPoints, closed);
    const p1Index = curveSegmentIndex;
    const p0Index = p1Index - 1;
    const p2Index = closed ? (p1Index + 1) % numCurveSegments : p1Index + 1;
    const p3Index = p2Index + 1;
    const p1 = controlPoints[p1Index];
    const p2 = controlPoints[p2Index];
    let p0;
    let p3;
 
    // P0 shall be negative when P1/P2 are the start/end points of the first curve segment
    if (p0Index >= 0) {
      p0 = controlPoints[p0Index];
    } else {
      p0 = closed
        ? controlPoints[controlPoints.length - 1]
        : math.point.mirror(p2, p1);
    }
 
    // P3 shall be negative when P1/P2 are the start/end points of the last curve segment
    if (p3Index < controlPoints.length) {
      p3 = controlPoints[p3Index];
    } else {
      p3 = closed ? controlPoints[0] : math.point.mirror(p1, p2);
    }
 
    return { p0, p1, p2, p3 };
  }
 
  private _getLineSegments(
    curveSegmentIndex: number,
    transformMatrix: number[],
    controlPoints: Types.Point2[] = this.controlPoints,
    closed: boolean = this.closed
  ): SplineLineSegment[] {
    const numCurveSegments = this._getNumCurveSegments(controlPoints, closed);
    const numLineSegments = this.resolution + 1;
    const inc = 1 / numLineSegments;
    const minU = curveSegmentIndex;
    let maxU = minU + 1;
 
    // 'u' must be greater than or equal to 0 and smaller than N where N is the number of segments
    // otherwise it does not find the spline segment when it is not a closed curve because it is
    // 0-based indexed. In this case `u` needs to get very close to the end point but never touch it
    if (!closed && curveSegmentIndex === numCurveSegments - 1) {
      maxU -= MAX_U_ERROR;
    }
 
    const lineSegments: SplineLineSegment[] = [];
    let startPoint: Types.Point2;
    let endPoint: Types.Point2;
    let previousLineSegmentsLength = 0;
 
    for (let i = 0, u = minU; i <= numLineSegments; i++, u += inc) {
      // `u` may be greater than maxU in the last FOR loop due to number precision issue
      u = u > maxU ? maxU : u;
 
      const point = this._getPoint(u, transformMatrix, controlPoints, closed);
 
      if (!i) {
        startPoint = point;
        continue;
      }
 
      endPoint = point;
 
      const dx = endPoint[0] - startPoint[0];
      const dy = endPoint[1] - startPoint[1];
      const length = Math.sqrt(dx ** 2 + dy ** 2);
      const aabb: Types.AABB2 = {
        minX: startPoint[0] <= endPoint[0] ? startPoint[0] : endPoint[0],
        maxX: startPoint[0] >= endPoint[0] ? startPoint[0] : endPoint[0],
        minY: startPoint[1] <= endPoint[1] ? startPoint[1] : endPoint[1],
        maxY: startPoint[1] >= endPoint[1] ? startPoint[1] : endPoint[1],
      };
 
      lineSegments.push({
        points: {
          start: startPoint,
          end: endPoint,
        },
        aabb,
        length,
        previousLineSegmentsLength,
      });
 
      startPoint = endPoint;
      previousLineSegmentsLength += length;
    }
 
    return lineSegments;
  }
 
  private _getCurveSegment(
    curveSegmentIndex: number,
    transformMatrix: number[] = this.getTransformMatrix(),
    controlPoints: Types.Point2[] = this.controlPoints,
    closed: boolean = this.closed
  ): SplineCurveSegment {
    // Cubic spline curves are mainly controlled by P1 and P2 points but
    // they are also influenced by previous (P0) and next (P3) poins. For
    // Cardinal, Linear and Catmull-Rom splines P1 and P2 are also known as
    // knots because they are the connection between two curve segments.
    const { p0, p1, p2, p3 } = this._getCurveSegmentPoints(
      curveSegmentIndex,
      controlPoints,
      closed
    );
    const lineSegments = this._getLineSegments(
      curveSegmentIndex,
      transformMatrix,
      controlPoints,
      closed
    );
    let curveSegmentLength = 0;
    let minX = Infinity;
    let minY = Infinity;
    let maxX = -Infinity;
    let maxY = -Infinity;
 
    lineSegments.forEach(({ aabb: lineSegAABB, length: lineSegLength }) => {
      minX = Math.min(minX, lineSegAABB.minX);
      minY = Math.min(minY, lineSegAABB.minY);
      maxX = Math.max(maxX, lineSegAABB.maxX);
      maxY = Math.max(maxY, lineSegAABB.maxY);
      curveSegmentLength += lineSegLength;
    });
 
    return {
      controlPoints: { p0, p1, p2, p3 },
      aabb: { minX, minY, maxX, maxY },
      length: curveSegmentLength,
      previousCurveSegmentsLength: 0,
      lineSegments,
    };
  }
}
 
export { CubicSpline as default, CubicSpline };