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

82.35% Statements 28/34
100% Branches 2/2
66.66% Functions 10/15
81.81% Lines 27/33

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                        4x                       4x 4x 4x 4x 4x 4x 4x 4x 4x                 4x               4x 4x 4x 4x   4x 154272x 154272x 154272x     4x                       4x               12x               12x                                 12x                               32x             12x               12x                                                      
import type { SurfaceData, Point3, RGB } from '../../types';
 
type SurfaceProps = SurfaceData;
 
/**
 * Surface class for storing surface data
 * Each Surface represents a single segment in a 3D volume.
 */
export class Surface {
  readonly id: string;
  readonly sizeInBytes: number;
  readonly frameOfReferenceUID: string;
  private _color: RGB = [200, 0, 0]; // default color
  private _points: number[];
  private _polys: number[];
  private _segmentIndex: number;
  private _centroid: Point3;
  private _visible: boolean;
 
  /**
   * Creates an instance of Surface.
   * @param {SurfaceProps} props - The properties to initialize the Surface with.
   */
  constructor(props: SurfaceProps) {
    this.id = props.id;
    this._points = props.points;
    this._polys = props.polys;
    this._color = props.color ?? this._color;
    this.frameOfReferenceUID = props.frameOfReferenceUID;
    this._segmentIndex = props.segmentIndex;
    this.sizeInBytes = this._getSizeInBytes();
    this._updateCentroid();
    this._visible = true;
  }
 
  /**
   * Calculates the size of the surface in bytes.
   * @returns {number} The size of the surface in bytes.
   * @private
   */
  private _getSizeInBytes(): number {
    return this._points.length * 4 + this._polys.length * 4;
  }
 
  /**
   * Updates the centroid of the surface.
   * @private
   */
  private _updateCentroid(): void {
    const numberOfPoints = this._points.length / 3;
    let sumX = 0,
      sumY = 0,
      sumZ = 0;
 
    for (let i = 0; i < this._points.length; i += 3) {
      sumX += this._points[i];
      sumY += this._points[i + 1];
      sumZ += this._points[i + 2];
    }
 
    this._centroid = [
      sumX / numberOfPoints,
      sumY / numberOfPoints,
      sumZ / numberOfPoints,
    ];
  }
 
  /**
   * Gets the color of the surface.
   * @returns {RGB} The color of the surface.
   */
  get color(): RGB {
    return this._color;
  }
 
  /**
   * Sets the color of the surface.
   * @param {RGB} color - The new color for the surface.
   */
  set color(color: RGB) {
    this._color = color;
  }
 
  /**
   * Gets the points of the surface.
   * @returns {number[]} The points of the surface.
   */
  get points(): number[] {
    return this._points;
  }
 
  /**
   * Sets the points of the surface and updates the centroid.
   * @param {number[]} points - The new points for the surface.
   */
  set points(points: number[]) {
    this._points = points;
    this._updateCentroid();
  }
 
  /**
   * Gets the polygons of the surface.
   * @returns {number[]} The polygons of the surface.
   */
  get polys(): number[] {
    return this._polys;
  }
 
  /**
   * Sets the polygons of the surface.
   * @param {number[]} polys - The new polygons for the surface.
   */
  set polys(polys: number[]) {
    this._polys = polys;
  }
 
  /**
   * Gets the segment index of the surface.
   * @returns {number} The segment index of the surface.
   */
  get segmentIndex(): number {
    return this._segmentIndex;
  }
 
  /**
   * Gets the visibility of the surface.
   */
  get visible(): boolean {
    return this._visible;
  }
 
  /**
   * Sets the visibility of the surface.
   * @param {boolean} visible - The new visibility for the surface.
   */
  set visible(visible: boolean) {
    this._visible = visible;
  }
 
  /**
   * Gets the centroid of the surface.
   * @returns {Point3} The centroid of the surface.
   */
  get centroid(): Point3 {
    return this._centroid;
  }
 
  /**
   * Gets a flat array of all points.
   * @returns {number[]} A flat array of all points.
   */
  get flatPointsArray(): number[] {
    return this._points;
  }
 
  /**
   * Gets the total number of points in the surface.
   * @returns {number} The total number of points.
   */
  get totalNumberOfPoints(): number {
    return this._points.length / 3;
  }
}