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

97.72% Statements 43/44
84% Branches 21/25
100% Functions 6/6
97.67% Lines 42/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 836x   6x 6x   6x       15x       8x 8x 8x 8x 8x 8x 8x 8x 8x   8x       8x   8x 8x 8x   8x   8x   8x               10x 10x 370x 370x 370x     370x   5x 365x   240x 125x     125x   120x   5x 5x   370x   10x       3x 3x 3x     2x      
import { CustomVisibilityMesh } from "./customVisibilityMesh"
import { GeometryOptions } from "../preview3DPrintMesh"
import { BackPrintColorChangeStrategy } from "../ColorChangeStrategies/backPrintColorChangeStrategy"
import { BufferAttribute, BufferGeometry, Float32BufferAttribute, InterleavedBufferAttribute, Mesh } from "three"
 
export class MapMesh extends CustomVisibilityMesh {
    private originalColors: BufferAttribute | InterleavedBufferAttribute
 
    constructor() {
        super("Map", new BackPrintColorChangeStrategy(), 1, false)
    }
 
    async init(geometryOptions: GeometryOptions): Promise<MapMesh> {
        this.material = (geometryOptions.originalMapMesh.clone() as Mesh).material
        this.originalColors = geometryOptions.originalMapMesh.geometry.attributes.color
        const newMapGeometry = geometryOptions.originalMapMesh.geometry.clone()
        newMapGeometry.computeBoundingBox()
        newMapGeometry.rotateX(Math.PI / 2)
        this.updateMapGeometry(geometryOptions, newMapGeometry)
        newMapGeometry.computeBoundingBox() // Ensure the bounding box is computed again after transformations
        newMapGeometry.rotateZ(-Math.PI / 2)
        this.geometry = newMapGeometry
 
        return this
    }
 
    private updateMapGeometry(geometryOptions: GeometryOptions, map: BufferGeometry): BufferGeometry {
        const width = geometryOptions.width - 2 * geometryOptions.mapSideOffset
 
        const normalizeFactor = map.boundingBox.max.x
        const scale = width / normalizeFactor
        map.scale(scale, scale, scale)
 
        map.translate(-width / 2, width / 2, 0)
 
        this.updateMapColors(this.originalColors, map, geometryOptions.numberOfColors)
 
        return map
    }
 
    private updateMapColors(
        originalColors: BufferAttribute | InterleavedBufferAttribute,
        previewMap: BufferGeometry,
        numberOfColors: number
    ) {
        const newColors = []
        for (let index = 0; index < originalColors.count; index++) {
            const colorR = originalColors.getX(index)
            const colorG = originalColors.getY(index)
            const colorB = originalColors.getZ(index)
            let newColor: number[]
 
            if (colorR === colorB && colorR === colorG && colorG === colorB) {
                //all grey values
                newColor = numberOfColors === 1 ? [1, 1, 1] : [0.5, 0.5, 0.5]
            } else if (colorR > 0.75 && colorG > 0.75) {
                //yellow
                newColor = numberOfColors < 4 ? [1, 1, 1] : [1, 1, 0]
            } else Iif (colorR > 0.45 && colorG < 0.1) {
                //red
                newColor = numberOfColors < 4 ? [1, 1, 1] : [1, 0, 0]
            } else if (colorR < 5 && colorG > 0.6) {
                //green
                newColor = numberOfColors < 4 ? [1, 1, 1] : [0, 1, 0]
            } else {
                console.error("Unknown color")
                newColor = [1, 1, 1]
            }
            newColors.push(...newColor)
        }
        previewMap.setAttribute("color", new Float32BufferAttribute(newColors, 3))
    }
 
    async changeSize(geometryOptions: GeometryOptions, oldWidth: number): Promise<void> {
        const scale = (geometryOptions.width - 2 * geometryOptions.mapSideOffset) / (oldWidth - 2 * geometryOptions.mapSideOffset)
        this.geometry.scale(scale, scale, scale)
        return
    }
    updateColor(numberOfColors: number) {
        this.updateMapColors(this.originalColors, this.geometry, numberOfColors)
    }
}