modules/shared/measurement.mjs

// module: True

/* Classes to represent a measurement extracted from an entity */

export { Measurement, ComponentMeasurementCurves };

class Measurement {
    /**
     * Class to tag a measurement with a name and the dialogue command component
     * to extract the data for the measurement
     * @param {string} name Name of the measurement
     * @param {string} component Component to use in dialogue command
     * @example
     * let m = new Measurement(Measurement.X_ACCELERATION, "AX");
     */
    constructor(name, component) {
        this.name = name;
        this.component = component;
    }

    /**
     * Measurement name
     * @type {string}
     */
    get name() {
        return this._name;
    }
    set name(new_name) {
        if (Measurement.GetAllMeasurementNames().indexOf(new_name) == -1) {
            throw new Error(`Invalid name ${new_name} in Measurement constructor`);
        }

        this._name = new_name;
    }

    /** Measurement dialogue command component
     * @type {string} */
    get component() {
        return this._component;
    }
    set component(new_component) {
        if (typeof new_component != "string") {
            throw new Error(`component must be a string in Measurement constructor`);
        }

        this._component = new_component;
    }

    /** X acceleration
     * @type {string} */
    static get X_ACCELERATION() {
        return "x_acceleration";
    }
    /** Y acceleration
     * @type {string} */
    static get Y_ACCELERATION() {
        return "y_acceleration";
    }
    /** Z acceleration
     * @type {string} */
    static get Z_ACCELERATION() {
        return "z_acceleration";
    }
    /** Left acceleration X
     * @type {string} */
    static get LEFT_ACCELERATION_X() {
        return "left_acceleration_x";
    }
    /** Left acceleration Y
     * @type {string} */
    static get LEFT_ACCELERATION_Y() {
        return "left_acceleration_y";
    }
    /** Left acceleration Z
     * @type {string} */
    static get LEFT_ACCELERATION_Z() {
        return "left_acceleration_z";
    }
    /** Left acceleration X
     * @type {string} */
    static get RIGHT_ACCELERATION_X() {
        return "left_acceleration_x";
    }
    /** Left acceleration Y
     * @type {string} */
    static get RIGHT_ACCELERATION_Y() {
        return "left_acceleration_y";
    }
    /** Left acceleration Z
     * @type {string} */
    static get RIGHT_ACCELERATION_Z() {
        return "left_acceleration_z";
    }

    /** Shear
     * @type {string} */
    static get SHEAR() {
        return "shear";
    }

    /** Axial
     * @type {string} */
    static get AXIAL() {
        return "axial";
    }
    /** Upper axial
     * @type {string} */
    static get UPPER_AXIAL() {
        return "upper_axial";
    }
    /** Lower axial
     * @type {string} */
    static get LOWER_AXIAL() {
        return "lower_axial";
    }

    /** Bending
     * @type {string} */
    static get BENDING() {
        return "bending";
    }
    /** Torsion
     * @type {string} */
    static get TORSION() {
        return "torsion";
    }
    /** Upper flexion
     * @type {string} */
    static get UPPER_FLEXION() {
        return "upper_flexion";
    }
    /** Lower flexion
     * @type {string} */
    static get LOWER_FLEXION() {
        return "lower_flexion";
    }
    /** Upper extension
     * @type {string} */
    static get UPPER_EXTENSION() {
        return "upper_extension";
    }
    /** Lower extension
     * @type {string} */
    static get LOWER_EXTENSION() {
        return "lower_extension";
    }

    /** Upper translation
     * @type {string} */
    static get UPPER_TRANSLATION() {
        return "upper_translation";
    }
    /** Mid translation
     * @type {string} */
    static get MID_TRANSLATION() {
        return "mid_translation";
    }
    /** Lower translation
     * @type {string} */
    static get LOWER_TRANSLATION() {
        return "lower_translation";
    }

    /** Rotation
     * @type {string} */
    static get ROTATION() {
        return "rotation";
    }
    /** Upper rotation
     * @type {string} */
    static get UPPER_ROTATION() {
        return "upper_rotation";
    }
    /** Mid rotation
     * @type {string} */
    static get MID_ROTATION() {
        return "mid_rotation";
    }
    /** Lower rotation
     * @type {string} */
    static get LOWER_ROTATION() {
        return "lower_rotation";
    }
    /** Lower left compression x1
     * @type {string} */
    static get LOWER_LEFT_COMPRESSION_X_1() {
        return "lower_left_compression_x_1";
    }
    /** Lower left compression y1
     * @type {string} */
    static get LOWER_LEFT_COMPRESSION_Y_1() {
        return "lower_left_compression_y_1";
    }
    /** Lower left compression z1
     * @type {string} */
    static get LOWER_LEFT_COMPRESSION_Z_1() {
        return "lower_left_compression_z_1";
    }
    /** Lower left compression x2
     * @type {string} */
    static get LOWER_LEFT_COMPRESSION_X_2() {
        return "lower_left_compression_x_2";
    }
    /** Lower left compression y2
     * @type {string} */
    static get LOWER_LEFT_COMPRESSION_Y_2() {
        return "lower_left_compression_y_2";
    }
    /** Lower left compression z2
     * @type {string} */
    static get LOWER_LEFT_COMPRESSION_Z_2() {
        return "lower_left_compression_z_2";
    }
    /** Upper left compression x1
     * @type {string} */
    static get UPPER_LEFT_COMPRESSION_X_1() {
        return "upper_left_compression_x_1";
    }
    /** Upper left compression y1
     * @type {string} */
    static get UPPER_LEFT_COMPRESSION_Y_1() {
        return "upper_left_compression_y_1";
    }
    /** Upper left compression z1
     * @type {string} */
    static get UPPER_LEFT_COMPRESSION_Z_1() {
        return "upper_left_compression_z_1";
    }
    /** Upper left compression x2
     * @type {string} */
    static get UPPER_LEFT_COMPRESSION_X_2() {
        return "upper_left_compression_x_2";
    }
    /** Upper left compression y2
     * @type {string} */
    static get UPPER_LEFT_COMPRESSION_Y_2() {
        return "upper_left_compression_y_2";
    }
    /** Upper left compression z2
     * @type {string} */
    static get UPPER_LEFT_COMPRESSION_Z_2() {
        return "upper_left_compression_z_2";
    }
    /** Lower right compression x1
     * @type {string} */
    static get LOWER_RIGHT_COMPRESSION_X_1() {
        return "lower_right_compression_x_1";
    }
    /** Lower right compression y1
     * @type {string} */
    static get LOWER_RIGHT_COMPRESSION_Y_1() {
        return "lower_right_compression_y_1";
    }
    /** Lower right compression z1
     * @type {string} */
    static get LOWER_RIGHT_COMPRESSION_Z_1() {
        return "lower_right_compression_z_1";
    }
    /** Lower right compression x2
     * @type {string} */
    static get LOWER_RIGHT_COMPRESSION_X_2() {
        return "lower_right_compression_x_2";
    }
    /** Lower right compression y2
     * @type {string} */
    static get LOWER_RIGHT_COMPRESSION_Y_2() {
        return "lower_right_compression_y_2";
    }
    /** Lower right compression z2
     * @type {string} */
    static get LOWER_RIGHT_COMPRESSION_Z_2() {
        return "lower_right_compression_z_2";
    }
    /** Upper right compression x1
     * @type {string} */
    static get UPPER_RIGHT_COMPRESSION_X_1() {
        return "upper_right_compression_x_1";
    }
    /** Upper right compression y1
     * @type {string} */
    static get UPPER_RIGHT_COMPRESSION_Y_1() {
        return "upper_right_compression_y_1";
    }
    /** Upper right compression z1
     * @type {string} */
    static get UPPER_RIGHT_COMPRESSION_Z_1() {
        return "upper_right_compression_z_1";
    }
    /** Upper right compression x2
     * @type {string} */
    static get UPPER_RIGHT_COMPRESSION_X_2() {
        return "upper_right_compression_x_2";
    }
    /** Upper right compression y2
     * @type {string} */
    static get UPPER_RIGHT_COMPRESSION_Y_2() {
        return "upper_right_compression_y_2";
    }
    /** Upper right compression z2
     * @type {string} */
    static get UPPER_RIGHT_COMPRESSION_Z_2() {
        return "upper_right_compression_z_2";
    }
    /** Left force
     * @type {string} */
    static get LEFT_FORCE() {
        return "left_force";
    }
    /** Right force
     * @type {string} */
    static get RIGHT_FORCE() {
        return "right_force";
    }
    /** Deflection
     * @type {string} */
    static get DEFLECTION() {
        return "deflection";
    }
    /** Left compression x1
     * @type {string} */
    static get LEFT_COMPRESSION_X_1() {
        return "left_compression_x_1";
    }
    /** Left compression y1
     * @type {string} */
    static get LEFT_COMPRESSION_Y_1() {
        return "left_compression_y_1";
    }
    /** Left compression z1
     * @type {string} */
    static get LEFT_COMPRESSION_Z_1() {
        return "left_compression_z_1";
    }
    /** Left compression x2
     * @type {string} */
    static get LEFT_COMPRESSION_X_2() {
        return "left_compression_x_2";
    }
    /** Left compression y2
     * @type {string} */
    static get LEFT_COMPRESSION_Y_2() {
        return "left_compression_y_2";
    }
    /** Left compression z2
     * @type {string} */
    static get LEFT_COMPRESSION_Z_2() {
        return "left_compression_z_2";
    }
    /** Right compression x1
     * @type {string} */
    static get RIGHT_COMPRESSION_X_1() {
        return "right_compression_x_1";
    }
    /** Right compression y1
     * @type {string} */
    static get RIGHT_COMPRESSION_Y_1() {
        return "right_compression_y_1";
    }
    /** Right compression z1
     * @type {string} */
    static get RIGHT_COMPRESSION_Z_1() {
        return "right_compression_z_1";
    }
    /** Right compression x2
     * @type {string} */
    static get RIGHT_COMPRESSION_X_2() {
        return "right_compression_x_2";
    }
    /** Right compression y2
     * @type {string} */
    static get RIGHT_COMPRESSION_Y_2() {
        return "right_compression_y_2";
    }
    /** Right compression z2
     * @type {string} */
    static get RIGHT_COMPRESSION_Z_2() {
        return "right_compression_z_2";
    }
    /** Front force
     * @type {string} */
    static get FRONT_FORCE() {
        return "front_force";
    }
    /** Mid force
     * @type {string} */
    static get MID_FORCE() {
        return "mid_force";
    }
    /** Back force
     * @type {string} */
    static get BACK_FORCE() {
        return "back_force";
    }

    /** Pubic symphysis force
     * @type {string} */
    static get PUBIC_SYMPHYSIS_FORCE() {
        return "pubic_symphysis_force";
    }
    /** Iliac force
     * @type {string} */
    static get ILIAC_FORCE() {
        return "iliac_force";
    }
    /** Acetabulum force
     * @type {string} */
    static get ACETABULUM_FORCE() {
        return "acetabulum_force";
    }
    /** Left force X
     * @type {string} */
    static get LEFT_FORCE_X() {
        return "left_force_x";
    }
    /** Left force Y
     * @type {string} */
    static get LEFT_FORCE_Y() {
        return "left_force_y";
    }
    /** Left force Z
     * @type {string} */
    static get LEFT_FORCE_Z() {
        return "left_force_z";
    }
    /** Right force X
     * @type {string} */
    static get RIGHT_FORCE_X() {
        return "right_force_x";
    }
    /** Right force Y
     * @type {string} */
    static get RIGHT_FORCE_Y() {
        return "right_force_y";
    }
    /** Right force Z
     * @type {string} */
    static get RIGHT_FORCE_Z() {
        return "right_force_z";
    }
    /** Left axial force
     * @type {string} */
    static get LEFT_AXIAL_FORCE() {
        return "left_axial_force";
    }
    /** Right axial force
     * @type {string} */
    static get RIGHT_AXIAL_FORCE() {
        return "right_axial_force";
    }
    /** Upper force X
     * @type {string} */
    static get UPPER_FORCE_X() {
        return "upper_force_x";
    }
    /** Upper force Y
     * @type {string} */
    static get UPPER_FORCE_Y() {
        return "upper_force_Y";
    }
    /** Upper force Z
     * @type {string} */
    static get UPPER_FORCE_Z() {
        return "upper_force_z";
    }
    /** Upper moment X
     * @type {string} */
    static get UPPER_MOMENT_X() {
        return "upper_moment_x";
    }
    /** Upper moment Y
     * @type {string} */
    static get UPPER_MOMENT_Y() {
        return "upper_moment_Y";
    }
    /** Upper moment Z
     * @type {string} */
    static get UPPER_MOMENT_Z() {
        return "upper_moment_z";
    }
    /** Lower force X
     * @type {string} */
    static get LOWER_FORCE_X() {
        return "lower_force_x";
    }
    /** Lower force Y
     * @type {string} */
    static get LOWER_FORCE_Y() {
        return "lower_force_Y";
    }
    /** Lower force Z
     * @type {string} */
    static get LOWER_FORCE_Z() {
        return "lower_force_z";
    }
    /** Lower moment X
     * @type {string} */
    static get LOWER_MOMENT_X() {
        return "lower_moment_x";
    }
    /** Lower moment Y
     * @type {string} */
    static get LOWER_MOMENT_Y() {
        return "lower_moment_Y";
    }
    /** Lower moment Z
     * @type {string} */
    static get LOWER_MOMENT_Z() {
        return "lower_moment_z";
    }
    /** Left translation
     * @type {string} */
    static get LEFT_TRANSLATION() {
        return "left_translation";
    }
    /** Right translation
     * @type {string} */
    static get RIGHT_TRANSLATION() {
        return "right_translation";
    }
    /** Left upper axial force
     * @type {string} */
    static get LEFT_UPPER_AXIAL_FORCE() {
        return "left_upper_axial_force";
    }
    /** Left upper bending X
     * @type {string} */
    static get LEFT_UPPER_BENDING_X() {
        return "left_upper_bending_x";
    }
    /** Left upper bending Y
     * @type {string} */
    static get LEFT_UPPER_BENDING_Y() {
        return "left_upper_bending_y";
    }
    /** Right upper axial force
     * @type {string} */
    static get RIGHT_UPPER_AXIAL_FORCE() {
        return "right_upper_axial_force";
    }
    /** Right upper bending X
     * @type {string} */
    static get RIGHT_UPPER_BENDING_X() {
        return "right_upper_bending_x";
    }
    /** Right upper bending Y
     * @type {string} */
    static get RIGHT_UPPER_BENDING_Y() {
        return "right_upper_bending_y";
    }
    /** Left lower axial force
     * @type {string} */
    static get LEFT_LOWER_AXIAL_FORCE() {
        return "left_lower_axial_force";
    }
    /** Left lower bending X
     * @type {string} */
    static get LEFT_LOWER_BENDING_X() {
        return "left_lower_bending_x";
    }
    /** Left lower bending Y
     * @type {string} */
    static get LEFT_LOWER_BENDING_Y() {
        return "left_lower_bending_y";
    }
    /** Right lower axial force
     * @type {string} */
    static get RIGHT_LOWER_AXIAL_FORCE() {
        return "right_lower_axial_force";
    }
    /** Right lower bending X
     * @type {string} */
    static get RIGHT_LOWER_BENDING_X() {
        return "right_lower_bending_x";
    }
    /** Right lower bending Y
     * @type {string} */
    static get RIGHT_LOWER_BENDING_Y() {
        return "right_lower_bending_y";
    }
    /** Vertical intrusion
     * @type {string} */
    static get VERTICAL_INTRUSION() {
        return "vertical_intrusion";
    }
    /** Lateral intrusion
     * @type {string} */
    static get LATERAL_INTRUSION() {
        return "lateral_intrusion";
    }
    /** Vertical intrusion
     * @type {string} */
    static get FORE_AFT_INTRUSION() {
        return "fore_aft_intrusion";
    }

    /**
     * Returns an array of all the measurement names
     * @returns {string[]}
     * @example
     * let names = Measurement.GetAllMeasurementNames();
     */
    static GetAllMeasurementNames() {
        return [
            Measurement.X_ACCELERATION,
            Measurement.Y_ACCELERATION,
            Measurement.Z_ACCELERATION,
            Measurement.SHEAR,
            Measurement.AXIAL,
            Measurement.BENDING,
            Measurement.TORSION,
            Measurement.UPPER_AXIAL,
            Measurement.LOWER_AXIAL,
            Measurement.UPPER_FLEXION,
            Measurement.LOWER_FLEXION,
            Measurement.UPPER_EXTENSION,
            Measurement.LOWER_EXTENSION,
            Measurement.UPPER_TRANSLATION,
            Measurement.MID_TRANSLATION,
            Measurement.LOWER_TRANSLATION,
            Measurement.ROTATION,
            Measurement.UPPER_ROTATION,
            Measurement.MID_ROTATION,
            Measurement.LOWER_ROTATION,
            Measurement.UPPER_LEFT_COMPRESSION_X_1,
            Measurement.UPPER_LEFT_COMPRESSION_Y_1,
            Measurement.UPPER_LEFT_COMPRESSION_Z_1,
            Measurement.UPPER_LEFT_COMPRESSION_X_2,
            Measurement.UPPER_LEFT_COMPRESSION_Y_2,
            Measurement.UPPER_LEFT_COMPRESSION_Z_2,
            Measurement.LOWER_LEFT_COMPRESSION_X_1,
            Measurement.LOWER_LEFT_COMPRESSION_Y_1,
            Measurement.LOWER_LEFT_COMPRESSION_Z_1,
            Measurement.LOWER_LEFT_COMPRESSION_X_2,
            Measurement.LOWER_LEFT_COMPRESSION_Y_2,
            Measurement.LOWER_LEFT_COMPRESSION_Z_2,
            Measurement.UPPER_RIGHT_COMPRESSION_X_1,
            Measurement.UPPER_RIGHT_COMPRESSION_Y_1,
            Measurement.UPPER_RIGHT_COMPRESSION_Z_1,
            Measurement.UPPER_RIGHT_COMPRESSION_X_2,
            Measurement.UPPER_RIGHT_COMPRESSION_Y_2,
            Measurement.UPPER_RIGHT_COMPRESSION_Z_2,
            Measurement.LOWER_RIGHT_COMPRESSION_X_1,
            Measurement.LOWER_RIGHT_COMPRESSION_Y_1,
            Measurement.LOWER_RIGHT_COMPRESSION_Z_1,
            Measurement.LOWER_RIGHT_COMPRESSION_X_2,
            Measurement.LOWER_RIGHT_COMPRESSION_Y_2,
            Measurement.LOWER_RIGHT_COMPRESSION_Z_2,
            Measurement.LEFT_FORCE,
            Measurement.RIGHT_FORCE,
            Measurement.DEFLECTION,
            Measurement.LEFT_COMPRESSION_X_1,
            Measurement.LEFT_COMPRESSION_Y_1,
            Measurement.LEFT_COMPRESSION_Z_1,
            Measurement.LEFT_COMPRESSION_X_2,
            Measurement.LEFT_COMPRESSION_Y_2,
            Measurement.LEFT_COMPRESSION_Z_2,
            Measurement.RIGHT_COMPRESSION_X_1,
            Measurement.RIGHT_COMPRESSION_Y_1,
            Measurement.RIGHT_COMPRESSION_Z_1,
            Measurement.RIGHT_COMPRESSION_X_2,
            Measurement.RIGHT_COMPRESSION_Y_2,
            Measurement.RIGHT_COMPRESSION_Z_2,
            Measurement.LOWER_ROTATION,
            Measurement.UPPER_ROTATION,
            Measurement.FRONT_FORCE,
            Measurement.MID_FORCE,
            Measurement.BACK_FORCE,
            Measurement.PUBIC_SYMPHYSIS_FORCE,
            Measurement.ILIAC_FORCE,
            Measurement.ACETABULUM_FORCE,
            Measurement.LEFT_FORCE_X,
            Measurement.LEFT_FORCE_Y,
            Measurement.LEFT_FORCE_Z,
            Measurement.RIGHT_FORCE_X,
            Measurement.RIGHT_FORCE_Y,
            Measurement.RIGHT_FORCE_Z,
            Measurement.LEFT_AXIAL_FORCE,
            Measurement.RIGHT_AXIAL_FORCE,
            Measurement.UPPER_FORCE_X,
            Measurement.UPPER_FORCE_Y,
            Measurement.UPPER_FORCE_Z,
            Measurement.LOWER_FORCE_X,
            Measurement.LOWER_FORCE_Y,
            Measurement.LOWER_FORCE_Z,
            Measurement.UPPER_MOMENT_X,
            Measurement.UPPER_MOMENT_Y,
            Measurement.UPPER_MOMENT_Z,
            Measurement.LOWER_MOMENT_X,
            Measurement.LOWER_MOMENT_Y,
            Measurement.LOWER_MOMENT_Z,
            Measurement.LEFT_TRANSLATION,
            Measurement.RIGHT_TRANSLATION,
            Measurement.LEFT_UPPER_AXIAL_FORCE,
            Measurement.LEFT_LOWER_AXIAL_FORCE,
            Measurement.RIGHT_UPPER_AXIAL_FORCE,
            Measurement.RIGHT_LOWER_AXIAL_FORCE,
            Measurement.LEFT_UPPER_BENDING_X,
            Measurement.LEFT_UPPER_BENDING_Y,
            Measurement.LEFT_LOWER_BENDING_X,
            Measurement.LEFT_LOWER_BENDING_Y,
            Measurement.RIGHT_UPPER_BENDING_X,
            Measurement.RIGHT_UPPER_BENDING_Y,
            Measurement.RIGHT_LOWER_BENDING_X,
            Measurement.RIGHT_LOWER_BENDING_Y,
            Measurement.LEFT_ACCELERATION_X,
            Measurement.LEFT_ACCELERATION_Y,
            Measurement.LEFT_ACCELERATION_Z,
            Measurement.RIGHT_ACCELERATION_X,
            Measurement.RIGHT_ACCELERATION_Y,
            Measurement.RIGHT_ACCELERATION_Z,
            Measurement.VERTICAL_INTRUSION,
            Measurement.LATERAL_INTRUSION,
            Measurement.FORE_AFT_INTRUSION
        ];
    }

    /**
     * JSON representation
     * @returns {object}
     * @example
     * let json = measurement.toJSON();
     */
    toJSON() {
        return {
            name: this.name,
            component: this.component
        };
    }
}

class ComponentMeasurementCurves {
    /**
     * Class to store and retrieve curves read from a component
     */
    constructor() {
        /* Storage for curves, initially set each curve to null */
        this.curves = {};

        for (let curve_name of Measurement.GetAllMeasurementNames()) {
            this.curves[curve_name] = null;
        }
    }

    /**
     * Add a curve
     * @param {string} measurement Measurement type
     * @param {Curve} curve Curve to add
     * @example
     * let m = new ComponentMeasurementCurves();
     * m.AddCurve(Measurement.AXIAL, c);
     */
    AddCurve(measurement, curve) {
        if (Measurement.GetAllMeasurementNames().indexOf(measurement) == -1) {
            throw new Error(`Invalid measurement: '${measurement}' in ComponentMeasurementCurves.AddCurve()`);
        }

        if (!(curve instanceof Curve)) {
            throw new Error(`curve must be a Curve instance in ComponentMeasurementCurves.AddCurve()`);
        }

        this.curves[measurement] = curve;
    }

    /**
     * Get a curve
     * @param {string} measurement Measurement type
     * @return {Curve}
     * @example
     * let c = m.GetCurve(Measurement.AXIAL);
     */
    GetCurve(measurement) {
        if (Measurement.GetAllMeasurementNames().indexOf(measurement) == -1) {
            throw new Error(`Invalid measurement: '${measurement}' in ComponentMeasurementCurves.GetCurve()`);
        }

        return this.curves[measurement];
    }
}