All files / core/src/cache/classes ContourSet.ts

75.55% Statements 34/45
70% Branches 7/10
62.5% Functions 15/24
76.74% Lines 33/43

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                                                    8x                               8x 8x 8x 8x 8x 8x 8x                 8x 32x   32x                       32x     8x               8x 8x   8x   1192x         8x           8x 1192x 1192x   1192x 40x   1152x       8x                 8x 32x                       2384x                                       16x                                                           632x               32x                               8x 32x                                                                
import type { Point3, IContour, ContourData } from '../../types';
import Contour from './Contour';
 
interface ContourSetProps {
  id: string;
  data: ContourData[];
  frameOfReferenceUID: string;
  segmentIndex: number;
  color?: Point3;
}
 
/**
 * Represents a set of contours in 3D space.
 * Usually contours are grouped together in a contour set to represent a meaningful shape.
 */
export class ContourSet {
  /** Unique identifier for the contour set */
  readonly id: string;
 
  /** Size of the contour set in bytes */
  readonly sizeInBytes: number;
 
  /** Frame of reference UID for the contour set */
  readonly frameOfReferenceUID: string;
 
  /** Color of the contour set */
  private _color: Point3 = [200, 0, 0]; // default color
 
  /** Index of the segment this contour set belongs to */
  private _segmentIndex: number;
 
  /** Centroid of the contour set */
  private _centroid: Point3;
 
  /** Array of contours in this set */
  private _contours: IContour[];
 
  /**
   * Creates an instance of ContourSet.
   * @param {ContourSetProps} props - The properties to initialize the ContourSet with
   */
  constructor(props: ContourSetProps) {
    this.id = props.id;
    this._contours = [];
    this._color = props.color ?? this._color;
    this.frameOfReferenceUID = props.frameOfReferenceUID;
    this._segmentIndex = props.segmentIndex;
    this._createEachContour(props.data);
    this.sizeInBytes = this._getSizeInBytes();
  }
 
  /**
   * Creates individual contours from the provided contour data array.
   * @param {ContourData[]} contourDataArray - Array of contour data to create contours from
   * @private
   */
  private _createEachContour(contourDataArray: ContourData[]): void {
    contourDataArray.forEach((contourData) => {
      const { points, type, color } = contourData;
 
      const contour = new Contour({
        id: `${this.id}-segment-${this._segmentIndex}`,
        data: {
          points,
          type,
          segmentIndex: this._segmentIndex,
          color: color ?? this._color,
        },
        segmentIndex: this._segmentIndex,
        color: color ?? this._color,
      });
 
      this._contours.push(contour);
    });
 
    this._updateContourSetCentroid();
  }
 
  /**
   * Updates the centroid of the contour set.
   * @private
   */
  private _updateContourSetCentroid(): void {
    const numberOfPoints = this.totalNumberOfPoints;
    const flatPointsArray = this.flatPointsArray;
 
    const sumOfPoints = flatPointsArray.reduce(
      (acc, point) => {
        return [acc[0] + point[0], acc[1] + point[1], acc[2] + point[2]];
      },
      [0, 0, 0]
    );
 
    const centroid = [
      sumOfPoints[0] / numberOfPoints,
      sumOfPoints[1] / numberOfPoints,
      sumOfPoints[2] / numberOfPoints,
    ] as Point3;
 
    const closestPoint = flatPointsArray.reduce((closestPoint, point) => {
      const distanceToPoint = this._getDistance(centroid, point);
      const distanceToClosestPoint = this._getDistance(centroid, closestPoint);
 
      if (distanceToPoint < distanceToClosestPoint) {
        return point;
      } else {
        return closestPoint;
      }
    }, flatPointsArray[0]);
 
    this._centroid = closestPoint;
  }
 
  /**
   * Calculates the total size of the contour set in bytes.
   * @returns {number} The size of the contour set in bytes
   * @private
   */
  private _getSizeInBytes(): number {
    return this._contours.reduce((sizeInBytes, contour) => {
      return sizeInBytes + contour.sizeInBytes;
    }, 0);
  }
 
  /**
   * Calculates the Euclidean distance between two 3D points.
   * @param {Point3} pointA - The first point
   * @param {Point3} pointB - The second point
   * @returns {number} The distance between the two points
   * @private
   */
  private _getDistance(pointA: Point3, pointB: Point3): number {
    return Math.sqrt(
      (pointA[0] - pointB[0]) ** 2 +
        (pointA[1] - pointB[1]) ** 2 +
        (pointA[2] - pointB[2]) ** 2
    );
  }
 
  /**
   * Gets the centroid of the contour set.
   * @returns {Point3} The centroid of the contour set
   */
  get centroid(): Point3 {
    return this._centroid;
  }
 
  /**
   * Gets the segment index of the contour set.
   * @returns {number} The segment index
   */
  get segmentIndex(): number {
    return this._segmentIndex;
  }
 
  /**
   * Gets the color of the contour set.
   * @returns {Point3} The color of the contour set
   */
  get color(): Point3 {
    return this._color;
  }
 
  /**
   * Sets the color of the contour set and updates all contours.
   * @param {Point3} value - The new color for the contour set
   */
  set color(value: Point3) {
    this._color = value;
    // Update color for all contours if needed
    this._contours.forEach((contour) => {
      if (contour instanceof Contour) {
        contour.color = value;
      }
    });
  }
 
  /**
   * Gets all contours in the set.
   * @returns {IContour[]} Array of contours
   */
  get contours(): IContour[] {
    return this._contours;
  }
 
  /**
   * Gets a flat array of all points from all contours.
   * @returns {Point3[]} Flat array of all points
   */
  get flatPointsArray(): Point3[] {
    return this._contours.flatMap((contour) => contour.points);
  }
 
  /**
   * Gets the number of contours in the set.
   * @returns {number} The number of contours
   */
  get numberOfContours(): number {
    return this._contours.length;
  }
 
  /**
   * Gets the total number of points across all contours.
   * @returns {number} The total number of points
   */
  get totalNumberOfPoints(): number {
    return this._contours.reduce((numberOfPoints, contour) => {
      return numberOfPoints + contour.points.length;
    }, 0);
  }
 
  /**
   * Gets an array of the number of points in each contour.
   * @returns {number[]} Array of point counts for each contour
   */
  get numberOfPointsArray(): number[] {
    return this._contours.map((contour) => contour.points.length);
  }
 
  /**
   * Gets the points of a specific contour.
   * @param {number} contourIndex - The index of the contour
   * @returns {Point3[]} Array of points for the specified contour
   */
  getPointsInContour(contourIndex: number): Point3[] {
    return this._contours[contourIndex].points;
  }
 
  /**
   * Gets the number of points in a specific contour.
   * @param {number} contourIndex - The index of the contour
   * @returns {number} The number of points in the specified contour
   */
  getNumberOfPointsInAContour(contourIndex: number): number {
    return this.getPointsInContour(contourIndex).length;
  }
}
 
export default ContourSet;