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

87.61% Statements 92/105
35.89% Branches 14/39
95% Functions 19/20
87.12% Lines 88/101

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207  16x 16x 16x 16x 16x 16x   16x 16x 16x     16x 1x 1x 1x         1x 1x 1x       1x 3x   1x 1x       1x 1x 1x       1x 1x 1x 1x   1x       1x 1x   1x       1x 2x 2x 2x 2x   1x       1x 1x 1x 1x 1x   1x 1x   1x                 1x 1x   1x       1x 1x 1x 1x 1x   1x       4x       3x       4x       7x   7x 7x   7x       1x 2x 1x   1x     1x       1x   1x 1x   1x 3x     1x 3x 2x     2x 2x   1x           1x 1x             5x       1x     1x                                     1x 1x 1x   1x       1x       1x      
import BoundingBox from "./boundingBox"
import Rectangle from "./rectangle"
import HorizontalStreet, { HorizontalOrientation } from "./horizontalStreet"
import Street from "./street"
import { CodeMapNode, NodeType } from "../../../codeCharta.model"
import { StreetViewHelper } from "./streetViewHelper"
import { Vector2 } from "three"
 
export enum VerticalOrientation {
    UP,
    DOWN
}
 
export default class VerticalStreet extends Street {
    protected children: BoundingBox[] = []
    protected leftRow: BoundingBox[] = []
    protected rightRow: BoundingBox[] = []
    orientation: VerticalOrientation
    private _origin: Vector2
    // biome-ignore lint/correctness/noUnusedVariables: <explanation>
    constructor(node: CodeMapNode, children: BoundingBox[], depth: number, orientation: VerticalOrientation = VerticalOrientation.UP) {
        super(node)
        this.children = children
        this.orientation = orientation
    }
 
    calculateDimension(metricName: string): void {
        for (const child of this.children) {
            child.calculateDimension(metricName)
        }
        this.splitChildrenToRows(this.children)
        this.rearrangeRows()
 
        // TODO refactor it - seems to be very similar to horizontalStreet.ts
        // TODO add a comment what the calculations are doing and why
        this.metricValue = StreetViewHelper.calculateSize(this.mapNode, metricName)
        this.width = this.getMaxWidth(this.leftRow) + this.getStreetThickness() + this.getMaxWidth(this.rightRow) + 2 * this.spacer
        this.height = Math.max(this.getLength(this.leftRow), this.getLength(this.rightRow))
    }
 
    layout(margin: number, origin: Vector2): CodeMapNode[] {
        const maxLeftWidth = this.getMaxWidth(this.leftRow)
        const leftRowNodes = this.layoutLeftRow(origin, maxLeftWidth, margin)
        const rightRowNodes = this.layoutRightRow(origin, maxLeftWidth, margin)
        const streetNode = this.layoutStreet(origin, maxLeftWidth)
 
        return [...leftRowNodes, streetNode, ...rightRowNodes]
    }
 
    private layoutLeftRow(origin: Vector2, maxLeftWidth: number, margin: number): CodeMapNode[] {
        const rowOrigin = new Vector2(origin.x, origin.y)
        const nodes: CodeMapNode[] = []
 
        Iif (this.orientation === VerticalOrientation.UP) {
            const rowHeight = this.getLength(this.leftRow)
            rowOrigin.y += this.height - rowHeight
        }
        for (let index = 0; index < this.leftRow.length; index++) {
            const childOriginX = this.calculateStreetOffsetX(rowOrigin, maxLeftWidth) - this.leftRow[index].width
            const childOriginY = this.calculateChildOriginY(rowOrigin, index, this.leftRow)
            const childOrigin = new Vector2(childOriginX, childOriginY)
            nodes.push(...this.leftRow[index].layout(margin, childOrigin))
        }
        return nodes
    }
 
    protected layoutStreet(origin: Vector2, maxLeftWidth: number): CodeMapNode {
        const streetOffsetX = this.calculateStreetOffsetX(origin, maxLeftWidth)
        const streetOrigin = new Vector2(streetOffsetX, origin.y)
        const streetOverhang = this.calculateStreetOverhang(streetOrigin)
        const streetHeight = this.height - streetOverhang
        const metricValue = this.metricValue
 
        streetOrigin.y += this.orientation === VerticalOrientation.UP ? streetOverhang : 0
        this.streetRect = new Rectangle(streetOrigin, this.getStreetThickness(), streetHeight)
 
        return {
            ...this.mapNode,
            value: metricValue,
            rect: this.streetRect,
            zOffset: 0
        }
    }
 
    private layoutRightRow(origin: Vector2, maxLeftWidth: number, margin: number): CodeMapNode[] {
        const rowOrigin = new Vector2(origin.x, origin.y)
        const nodes: CodeMapNode[] = []
 
        Iif (this.orientation === VerticalOrientation.UP) {
            const rowHeight = this.getLength(this.rightRow)
            rowOrigin.y += this.height - rowHeight
        }
        for (let index = 0; index < this.rightRow.length; index++) {
            const childOriginX = this.calculateStreetOffsetX(rowOrigin, maxLeftWidth) + this.getStreetThickness()
            const childOriginY = this.calculateChildOriginY(rowOrigin, index, this.rightRow)
            const childOrigin = new Vector2(childOriginX, childOriginY)
            nodes.push(...this.rightRow[index].layout(margin, childOrigin))
        }
        return nodes
    }
 
    private calculateStreetOffsetX(origin: Vector2, maxLeftWidth: number): number {
        return origin.x + this.spacer + maxLeftWidth
    }
 
    private calculateChildOriginY(origin: Vector2, index: number, row: BoundingBox[]): number {
        return origin.y + this.getLengthUntil(row, index)
    }
 
    private getLength(boxes: BoundingBox[]): number {
        return this.getLengthUntil(boxes, boxes.length)
    }
 
    private getLengthUntil(boxes: BoundingBox[], end: number): number {
        let sum = 0
 
        for (let index = 0; index < end; index++) {
            sum += boxes[index].height
        }
        return sum
    }
 
    protected sortChildrenByType(children: BoundingBox[]): void {
        children.sort((a, b) => {
            if (a.mapNode.type === b.mapNode.type) {
                return 0
            }
            Iif (a.mapNode.type === NodeType.FILE) {
                return -1
            }
            return 1
        })
    }
    protected splitChildrenToRows(children: BoundingBox[]): void {
        this.sortChildrenByType(children)
 
        let totalLength = 0
        let sum = 0
 
        for (const child of children) {
            totalLength += child.height
        }
 
        for (const child of children) {
            if (sum <= totalLength / 2) {
                Iif (child instanceof HorizontalStreet) {
                    child.orientation = HorizontalOrientation.LEFT
                }
                this.leftRow.push(child)
                sum += child.height
            } else {
                this.rightRow.push(child)
            }
        }
    }
 
    protected rearrangeRows(): void {
        if (this.orientation === VerticalOrientation.UP) {
            this.leftRow.reverse()
        } else E{
            this.rightRow.reverse()
        }
    }
 
    private getMaxWidth(boxes: BoundingBox[]): number {
        return boxes.reduce((max, n) => Math.max(max, n.width), Number.MIN_VALUE)
    }
 
    protected calculateStreetOverhang(streetOrigin: Vector2): number {
        Iif (this.orientation === VerticalOrientation.UP) {
            return this.calculateTopStreetOverhang(streetOrigin)
        }
        return this.calculateBottomStreetOverhang(streetOrigin)
    }
 
    private calculateTopStreetOverhang(streetOrigin: Vector2): number {
        const firstLeftNode = this.leftRow[0]
        const firstRightNode = this.rightRow[0]
        const leftOverhang =
            firstLeftNode instanceof HorizontalStreet && firstLeftNode.streetRect
                ? firstLeftNode.streetRect.topLeft.y - streetOrigin.y
                : this.height - this.getLength(this.leftRow)
        const rightOverhang =
            firstRightNode instanceof HorizontalStreet && firstRightNode.streetRect
                ? firstRightNode.streetRect.topLeft.y - streetOrigin.y
                : this.height - this.getLength(this.rightRow)
 
        return leftOverhang > 0 && rightOverhang > 0 ? Math.min(leftOverhang, rightOverhang) : 0
    }
 
    private calculateBottomStreetOverhang(streetOrigin: Vector2): number {
        const lastLeftNode = this.leftRow.at(-1)
        const lastRightNode = this.rightRow.at(-1)
        const streetBottomY = streetOrigin.y + this.height
        const leftOverhang =
            lastLeftNode instanceof HorizontalStreet && lastLeftNode.streetRect
                ? streetBottomY - lastLeftNode.streetRect.getBottomRight().y
                : this.height - this.getLength(this.leftRow)
        const rightOverhang =
            lastRightNode instanceof HorizontalStreet && lastRightNode.streetRect
                ? streetBottomY - lastRightNode.streetRect.getBottomRight().y
                : this.height - this.getLength(this.rightRow)
 
        return leftOverhang > 0 && rightOverhang > 0 ? Math.min(leftOverhang, rightOverhang) : 0
    }
}