All files / app/codeCharta/services/3DExports/3DPreview/MeshModels generalMesh.ts

100% Statements 21/21
100% Branches 2/2
100% Functions 7/7
100% Lines 21/21

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 5525x             25x         185x   185x 185x 185x 185x         26x 26x 13x 13x           31x 31x     12x 12x     7x 7x       10x       50x 34x 34x        
import { Mesh, MeshBasicMaterial } from "three"
import { GeometryOptions } from "../preview3DPrintMesh"
import { ColorChangeStrategy } from "../ColorChangeStrategies/colorChangeStrategy"
 
export interface GeneralSizeChangeMesh {
    changeSize(geometryOptions: GeometryOptions, oldWidth: number): void
}
export abstract class GeneralMesh extends Mesh {
    boundingBoxCalculated: boolean
 
    constructor(
        name: string,
        public colorChangeStrategy: ColorChangeStrategy
    ) {
        super()
        this.name = name
        this.boundingBoxCalculated = false
        this.material = new MeshBasicMaterial()
    }
 
    abstract init(geometryOptions: GeometryOptions): Promise<GeneralMesh>
    updateColor(numberOfColors: number): void {
        this.colorChangeStrategy.execute(numberOfColors, this)
        for (const child of this.children) {
            if (child instanceof GeneralMesh) {
                child.updateColor(numberOfColors)
            }
        }
    }
 
    getWidth(): number {
        this.updateBoundingBox()
        return this.geometry.boundingBox.max.x - this.geometry.boundingBox.min.x
    }
    getHeight(): number {
        this.updateBoundingBox()
        return this.geometry.boundingBox.max.z - this.geometry.boundingBox.min.z
    }
    getDepth(): number {
        this.updateBoundingBox()
        return this.geometry.boundingBox.max.y - this.geometry.boundingBox.min.y
    }
 
    isGeneralSizeChangeMesh(): this is GeneralSizeChangeMesh {
        return "changeSize" in this
    }
 
    private updateBoundingBox() {
        if (!this.boundingBoxCalculated) {
            this.geometry.computeBoundingBox()
            this.boundingBoxCalculated = true
        }
    }
}