All files / app/codeCharta/util/algorithm/streetLayout/strip strip.ts

96.29% Statements 26/27
50% Branches 1/2
100% Functions 12/12
95.65% Lines 22/23

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 6715x 15x   15x   15x 2x     2x                             7x       2x       3x       3x       2x 1x 1x   1x       1x           15x 15x 15x       2x 2x 2x 2x      
import Rectangle from "../rectangle"
import { Vector2 } from "three"
import { CodeMapNode } from "../../../../codeCharta.model"
import { TreeMapHelper } from "../../treeMapLayout/treeMapHelper"
 
export default abstract class Strip {
    nodes: CodeMapNode[] = []
 
    constructor(nodes: CodeMapNode[]) {
        this.nodes = nodes
    }
 
    abstract layout(
        rect: Rectangle,
        rootSize: number,
        metricName: string,
        currentTreemapDepth: number,
        margin: number,
        order?: number
    ): CodeMapNode[]
 
    protected abstract worstAspectRatio(nodes: CodeMapNode[], rect: Rectangle, rootSize: number, metricName: string): number
 
    totalScaledSize(nodes: CodeMapNode[], metricName: string, rootSize: number, rootArea: number): number {
        return nodes.reduce((total, n) => total + this.scaledSize(n, rootSize, rootArea, metricName), 0)
    }
 
    totalSize(metricName: string) {
        return this.nodes.reduce((total, n) => total + TreeMapHelper.calculateSize(n, metricName), 0)
    }
 
    protected min(nodes: CodeMapNode[], metricName: string, rootSize: number, rootArea: number): number {
        return nodes.reduce((min, n) => Math.min(min, this.scaledSize(n, rootSize, rootArea, metricName)), Number.MAX_VALUE)
    }
 
    protected max(nodes: CodeMapNode[], metricName: string, rootSize: number, rootArea: number): number {
        return nodes.reduce((max, n) => Math.max(max, this.scaledSize(n, rootSize, rootArea, metricName)), Number.MIN_VALUE)
    }
 
    populate(nodes: CodeMapNode[], rect: Rectangle, rootSize: number, metricName: string) {
        for (const node of nodes) {
            const score = this.worstAspectRatio(this.nodes, rect, rootSize, metricName)
            const newScore = this.worstAspectRatio([...this.nodes, node], rect, rootSize, metricName)
 
            Iif (newScore < score) {
                this.nodes.push(node)
            } else {
                /* Node would increase worst aspect ratio, strip is completed */
                break
            }
        }
    }
 
    protected scaledSize(node: CodeMapNode, parentSize: number, parentArea: number, metricName: string): number {
        const size = TreeMapHelper.calculateSize(node, metricName)
        const scale = parentArea / parentSize
        return scale * size
    }
 
    protected applyNodeMargin(rect: Rectangle, margin: number): Rectangle {
        const topLeft = new Vector2(rect.topLeft.x + margin, rect.topLeft.y + margin)
        const width = rect.width - 2 * margin
        const height = rect.height - 2 * margin
        return new Rectangle(topLeft, width, height)
    }
}