All files / app/codeCharta/ui/codeMap/rendering geometryGenerator.ts

84.21% Statements 48/57
73.33% Branches 11/15
77.77% Functions 7/9
83.92% Lines 47/56

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  20x 20x 20x 20x 20x             20x 20x           47x   47x 47x   47x 47x 47x 47x 47x 47x   47x                           47x 130x     130x       47x             130x       130x                     130x                                                     130x 130x   130x   130x 2x   2x 1x       130x       47x 47x   47x   47x   47x 47x 47x 47x 47x 47x 47x 47x   47x   47x   47x      
import { Node, CcState } from "../../../codeCharta.model"
import { CodeMapGeometricDescription } from "./codeMapGeometricDescription"
import { addBoxToVertexData, IntermediateVertexData, BoxMeasures } from "./geometryGenerationHelper"
import { ColorConverter } from "../../../util/color/colorConverter"
import { Mesh, BufferGeometry, Material, BufferAttribute } from "three"
import { treeMapSize } from "../../../util/algorithm/treeMapLayout/treeMapHelper"
 
export interface BuildResult {
    mesh: Mesh
    desc: CodeMapGeometricDescription
}
 
export class GeometryGenerator {
    private static MINIMAL_BUILDING_HEIGHT = 1
 
    private floorGradient: string[]
    private materials: Material[]
 
    build(nodes: Node[], material: Material, state: CcState, isDeltaState: boolean): BuildResult {
        const desc = new CodeMapGeometricDescription(treeMapSize)
 
        this.floorGradient = ColorConverter.gradient("#333333", "#DDDDDD", this.getMaxNodeDepth(nodes))
        this.materials = [material]
 
        const vertices = nodes.length
        const threeDimension = 3
        const twoDimension = 2
        const numberSides = 6
        const verticesPerSide = 4
        const size = vertices * verticesPerSide * numberSides
 
        const data: IntermediateVertexData = {
            positions: new Float32Array(size * threeDimension),
            uvs: new Float32Array(size * twoDimension),
 
            normals: new Float32Array(size * threeDimension),
            colors: new Float32Array(size * threeDimension),
 
            indices: new Uint32Array(vertices * numberSides * numberSides),
            ids: new Float32Array(size),
 
            deltas: new Float32Array(size),
            isHeight: new Float32Array(size)
        }
 
        for (const [index, node] of nodes.entries()) {
            Iif (!node.isLeaf) {
                this.addFloor(data, node, index, desc)
            } else {
                this.addBuilding(data, node, index, desc, state, isDeltaState)
            }
        }
 
        return {
            mesh: this.buildMeshFromIntermediateVertexData(data),
            desc
        }
    }
 
    private getMaxNodeDepth(nodes: Node[]) {
        return nodes.reduce((max, { depth }) => Math.max(depth, max), 0)
    }
 
    private mapNodeToLocalBox(node: Node): BoxMeasures {
        return {
            x: node.x0,
            y: node.z0,
            z: node.y0,
            width: node.width,
            height: node.height,
            depth: node.length
        }
    }
 
    private ensureMinHeightUnlessDeltaIsNegative(height: number, delta: number) {
        return delta <= 0 ? height : Math.max(height, GeometryGenerator.MINIMAL_BUILDING_HEIGHT)
    }
 
    private addFloor(data: IntermediateVertexData, node: Node, index: number, desc: CodeMapGeometricDescription) {
        const color = this.getMarkingColorWithGradient(node)
        const measures = this.mapNodeToLocalBox(node)
 
        addBoxToVertexData(data, node, measures, color, index, desc, 0)
    }
 
    private getMarkingColorWithGradient(node: Node) {
        Iif (node.markingColor) {
            const markingColorAsNumber = ColorConverter.getNumber(node.markingColor)
            const markingColorWithGradient = markingColorAsNumber & (node.depth % 2 === 0 ? 0xdd_dd_dd : 0xff_ff_ff)
            return ColorConverter.convertNumberToHex(markingColorWithGradient)
        }
        return this.floorGradient[node.depth]
    }
 
    private addBuilding(
        data: IntermediateVertexData,
        node: Node,
        index: number,
        desc: CodeMapGeometricDescription,
        state: CcState,
        isDeltaState: boolean
    ) {
        const measures = this.mapNodeToLocalBox(node)
        measures.height = this.ensureMinHeightUnlessDeltaIsNegative(node.height, node.heightDelta)
 
        let renderDelta = 0
 
        if (isDeltaState && node.deltas && node.deltas[state.dynamicSettings.heightMetric] && node.heightDelta) {
            renderDelta = node.heightDelta // Set the transformed render delta
 
            if (!node.flat && renderDelta < 0) {
                measures.height += Math.abs(renderDelta)
            }
        }
 
        addBoxToVertexData(data, node, measures, node.color, index, desc, renderDelta)
    }
 
    private buildMeshFromIntermediateVertexData(data: IntermediateVertexData) {
        const threeDimensions = 3
        const twoDimensions = 2
 
        const deltaColors = new Float32Array(data.colors)
 
        const geometry = new BufferGeometry()
 
        geometry.setAttribute("position", new BufferAttribute(data.positions, threeDimensions))
        geometry.setAttribute("normal", new BufferAttribute(data.normals, threeDimensions))
        geometry.setAttribute("isHeight", new BufferAttribute(data.isHeight, 1))
        geometry.setAttribute("uv", new BufferAttribute(data.uvs, twoDimensions))
        geometry.setAttribute("color", new BufferAttribute(data.colors, threeDimensions))
        geometry.setAttribute("deltaColor", new BufferAttribute(deltaColors, threeDimensions))
        geometry.setAttribute("subGeomIdx", new BufferAttribute(data.ids, 1))
        geometry.setAttribute("delta", new BufferAttribute(data.deltas, 1))
 
        geometry.setIndex(new BufferAttribute(data.indices, 1))
 
        geometry.addGroup(0, Number.POSITIVE_INFINITY, 0)
 
        return new Mesh(geometry, this.materials)
    }
}