All files / app/codeCharta/services/3DExports/3DPreview preview3DPrintMesh.ts

92.75% Statements 64/69
100% Branches 10/10
76.19% Functions 16/21
92.75% Lines 64/69

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 1795x 5x   5x 5x 5x 5x 5x 5x                                                     5x         11x 11x 11x 11x 11x   11x 11x       8x 8x   8x 8x   8x 8x   8x 8x   8x       6x               6x       2x 2x   2x 8x 8x 8x         2x 2x       2x 2x 4x 4x           1x                                       16x       22x 20x   2x   2x 2x       21x       10x 10x 10x 10x       14x       13x       5x             5x 5x 5x   5x 5x 5x 4x   5x 5x   5x    
import { Mesh, ShaderMaterial, Vector3 } from "three"
import { Font } from "three/examples/jsm/loaders/FontLoader"
import { ColorRange, NodeMetricData } from "../../../codeCharta.model"
import { BaseplateMesh } from "./MeshModels/baseplateMesh"
import { GeneralMesh } from "./MeshModels/generalMesh"
import { MapMesh } from "./MeshModels/mapMesh"
import { BackPrintContainerMesh } from "./MeshModels/BackMeshModels/backPrintContainerMesh"
import { FrontPrintContainerMesh } from "./MeshModels/FrontMeshModels/frontPrintContainerMesh"
import font from "three/examples/fonts/helvetiker_regular.typeface.json"
 
export interface GeometryOptions {
    originalMapMesh: Mesh
    width: number
    areaMetricTitle: string
    areaMetricData: NodeMetricData
    heightMetricTitle: string
    heightMetricData: NodeMetricData
    colorMetricTitle: string
    colorMetricData: NodeMetricData
    colorRange: ColorRange
    frontText: string
    secondRowText: string
    qrCodeText: string
    defaultMaterial: ShaderMaterial
    numberOfColors: number
    layerHeight: number
    frontTextSize: number
    secondRowTextSize: number
    secondRowVisible: boolean
    printHeight: number
    mapSideOffset: number
    baseplateHeight: number
    logoSize: number
}
 
export class Preview3DPrintMesh {
    private printMesh: Mesh
    private currentSize: Vector3
 
    constructor(
        private geometryOptions: GeometryOptions,
        private frontPrintContainerMesh: FrontPrintContainerMesh = new FrontPrintContainerMesh(new Font(font)),
        private backPrintContainerMesh: BackPrintContainerMesh = new BackPrintContainerMesh(new Font(font)),
        private baseplateMesh: BaseplateMesh = new BaseplateMesh(),
        private mapMesh: MapMesh = new MapMesh()
    ) {
        this.printMesh = new Mesh()
        this.printMesh.name = "PrintMesh" //TODO: rename to PrintPreview
    }
 
    async initialize() {
        this.baseplateMesh = await this.baseplateMesh.init(this.geometryOptions)
        this.printMesh.add(this.baseplateMesh)
 
        this.mapMesh = await this.mapMesh.init(this.geometryOptions)
        this.printMesh.add(this.mapMesh)
 
        this.frontPrintContainerMesh = await this.frontPrintContainerMesh.init(this.geometryOptions)
        this.printMesh.add(this.frontPrintContainerMesh)
 
        this.backPrintContainerMesh = await this.backPrintContainerMesh.init(this.geometryOptions)
        this.printMesh.add(this.backPrintContainerMesh)
 
        this.calculateCurrentSize()
    }
 
    getThreeMesh(): Mesh {
        return this.printMesh
    }
 
    getMapMesh(): Mesh {
        return this.mapMesh
    }
 
    getSize(): Vector3 {
        return this.currentSize
    }
 
    async updateSize(wantedWidth: number): Promise<boolean> {
        this.geometryOptions.width = wantedWidth
        const oldWidth = this.currentSize.x
 
        await Promise.all(
            [...this.printMesh.children].map(async mesh => {
                if (mesh instanceof GeneralMesh && mesh.isGeneralSizeChangeMesh()) {
                    mesh.changeSize(this.geometryOptions, oldWidth)
                }
            })
        )
 
        this.calculateCurrentSize()
        return this.backPrintContainerMesh.isQRCodeVisible()
    }
 
    updateNumberOfColors(numberOfColors: number) {
        this.geometryOptions.numberOfColors = numberOfColors
        for (const mesh of this.printMesh.children) {
            if (mesh instanceof GeneralMesh) {
                mesh.updateColor(numberOfColors)
            }
        }
    }
 
    async addCustomLogo(dataUrl: string): Promise<void> {
        this.frontPrintContainerMesh.addCustomLogo(dataUrl, this.geometryOptions)
    }
 
    rotateCustomLogo() {
        this.frontPrintContainerMesh.rotateCustomLogo()
    }
 
    flipCustomLogo() {
        this.frontPrintContainerMesh.flipCustomLogo()
    }
 
    removeCustomLogo() {
        this.frontPrintContainerMesh.removeCustomLogo()
    }
 
    updateCustomLogoColor(newColor: string) {
        this.frontPrintContainerMesh.updateCustomLogoColor(newColor)
    }
 
    updateFrontText(frontText: string) {
        this.frontPrintContainerMesh.updateFrontText(frontText, this.geometryOptions)
    }
 
    updateSecondRowVisibility(isSecondRowVisible: boolean) {
        if (this.geometryOptions.secondRowVisible === isSecondRowVisible) {
            return
        }
        this.geometryOptions.secondRowVisible = isSecondRowVisible
 
        this.frontPrintContainerMesh.updateSecondRowVisibility(this.geometryOptions)
        this.baseplateMesh.changeSize(this.geometryOptions)
    }
 
    async updateSecondRowText(secondRowText: string) {
        await this.frontPrintContainerMesh.updateSecondRowText(secondRowText, this.geometryOptions)
    }
 
    private calculateCurrentSize() {
        const currentWidth = this.baseplateMesh.getWidth()
        const currentDepth = this.baseplateMesh.getDepth()
        const currentHeight = this.baseplateMesh.getHeight() + this.mapMesh.getHeight()
        this.currentSize = new Vector3(currentWidth, currentDepth, currentHeight)
    }
 
    async updateQrCodeText(qrCodeText: string): Promise<void> {
        await this.backPrintContainerMesh.updateQrCodeText(qrCodeText, this.geometryOptions)
    }
 
    updateQrCodeVisibility(qrCodeVisible: boolean) {
        this.backPrintContainerMesh.updateQrCodeVisibility(qrCodeVisible)
    }
}
 
export function calculateMaxPossibleWidthForPreview3DPrintMesh(
    maxSize: Vector3,
    mapMesh: Mesh,
    frontTextSize: number,
    baseplateHeight: number,
    mapSideOffset: number
): number {
    const printerWidth = maxSize.x
    const printerDepth = maxSize.y
    const printerHeight = maxSize.z
 
    const widthFromWidth = printerWidth
    const widthFromDepth = printerDepth - frontTextSize
    if (!mapMesh.geometry.boundingBox) {
        mapMesh.geometry.computeBoundingBox()
    }
    const mapCurrentHeight = mapMesh.geometry.boundingBox.max.z - mapMesh.geometry.boundingBox.min.z
    const widthFromHeight = ((printerHeight - baseplateHeight) * mapMesh.geometry.boundingBox.max.x) / mapCurrentHeight + 2 * mapSideOffset
 
    return Math.min(widthFromWidth, widthFromDepth, widthFromHeight)
}