Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-vector

Source of release 0.6.0

0.6.0 / src / vector / render_tests.lucb

#==============================================================================================
#
#   vector/render_tests - Elements drawn cell by cell against the whole canvas at once
#
#   DESCRIPTION:
#       Drawing a window of cells at a time (windows.lucb) is checked against the
#       whole-canvas drawing it replaces, kept here as the oracle.
#
#==============================================================================================

## `spec` drawn over the whole `width` by `height` canvas at once, as shape layers
## were before: straight RGBA f32, heap storage the caller frees.
func whole_canvas_shape(spec: ShapeSpec, width: u32, height: u32) -> f32[]!:
    var text: collections.Buffer
    defer text.destroy()
    try shape_svg(&text, spec, 0, 0, width, height)
    let drawing = try svg.parse(text.text())
    defer drawing.release()
    let pixels = try new f32[(usize)width * (usize)height * 4] --- in memory.heap
    errdefer free(pixels) in memory.heap
    try svg.rasterize_rgba(drawing.value(), width, height, pixels, svg.Color(r = 0.0, g = 0.0, b = 0.0))
    return pixels

## `spec` turned by `degrees` about the middle of its box.
func turned(spec: ShapeSpec, degrees: f64) -> ShapeSpec:
    let angle = degrees * math.pi / 180.0
    let cx = spec.x + spec.width * 0.5
    let cy = spec.y + spec.height * 0.5
    let c = math.cos(angle)
    let s = math.sin(angle)
    var moved = spec
    moved.matrix = [c, s, -s, c, cx - c * cx + s * cy, cy - s * cx - c * cy]
    return moved

## `spec` alone as a `width` by `height` layer's pixels, drawn as a vector layer
## of that one element is.
func rendered_shape(device: gpu.Device, width: u32, height: u32, spec: ShapeSpec) -> Tiles!:
    var list: VectorElement[1] = [VectorElement(name = "", visible = true, spec = spec)]
    var tiles = try Tiles.create(device, width, height)
    errdefer tiles.destroy()
    try draw_elements(&tiles, list[..<1], none)
    return tiles

## Whether `spec` is drawn window by window as the whole canvas would draw it, to
## within a step of the half floats the tiles keep.
func matches_whole(device: gpu.Device, width: u32, height: u32, spec: ShapeSpec) -> !:
    let expected = try whole_canvas_shape(spec, width, height)
    defer free(expected) in memory.heap
    var tiles = try rendered_shape(device, width, height, spec)
    defer tiles.destroy()
    let found = try new f32[expected.length] --- in memory.heap
    defer free(found) in memory.heap
    try tiles.read(found)
    for index in 0..<expected.length:
        let want = from_half(to_half(expected[index]))
        assert(math.abs((f64)(want - found[index])) <= 1.5e-3)

test "a shape layer drawn cell by cell matches it drawn over the whole canvas":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    let width: u32 = 700
    let height: u32 = 600
    var no_path: f64[]
    let base = ShapeSpec(kind = 0, x = 100.0, y = 180.0, width = 500.0, height = 300.0, radius = 20.0, filled = true, fill_red = 0.8, fill_green = 0.1,
                         fill_blue = 0.05, stroked = true, stroke_red = 0.0, stroke_green = 0.2, stroke_blue = 0.9, stroke_width = 9.0, path = no_path)
    # A turned, rounded, stroked rectangle across many cell edges.
    try matches_whole(device, width, height, turned(base, 13.0))
    # An ellipse touching almost every cell, stroked.
    var ellipse = base
    ellipse.kind = 1
    ellipse.x = 20.0
    ellipse.y = 30.0
    ellipse.width = 660.0
    ellipse.height = 540.0
    ellipse.stroke_width = 5.0
    try matches_whole(device, width, height, ellipse)
    # A nearly flat line, whose edges run across whole windows in a row.
    var line = base
    line.kind = 2
    line.x = 5.0
    line.y = 250.0
    line.width = 690.0
    line.height = 12.0
    line.stroke_width = 3.0
    try matches_whole(device, width, height, line)
    # A turned heptagon, filled only.
    var polygon = base
    polygon.kind = 3
    polygon.sides = 7
    polygon.x = 300.0
    polygon.y = 100.0
    polygon.width = 380.0
    polygon.height = 420.0
    polygon.stroked = false
    try matches_whole(device, width, height, turned(polygon, 21.0))
    # A rectangle past the canvas's right and bottom edges.
    var past = base
    past.x = -100.0
    past.y = 500.0
    past.width = 900.0
    past.height = 200.0
    try matches_whole(device, width, height, past)
    # A closed pen path of three curved anchors.
    var anchors: f64[18] = [60.0, 60.0, 20.0, 200.0, 200.0, -40.0,
                            640.0, 120.0, 500.0, -50.0, 700.0, 300.0,
                            330.0, 560.0, 600.0, 600.0, 90.0, 520.0]
    var path = base
    path.kind = 4
    path.closed = true
    path.stroke_width = 4.0
    path.path = anchors[..<18]
    try matches_whole(device, width, height, path)

test "cells wholly inside a filled shape share one tile":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    let width: u32 = 700
    let height: u32 = 600
    var no_path: f64[]
    let spec = ShapeSpec(kind = 0, x = -10.0, y = -10.0, width = 720.0, height = 620.0, filled = true, fill_red = 0.2, fill_green = 0.4,
                         fill_blue = 0.6, path = no_path)
    var tiles = try rendered_shape(device, width, height, spec)
    defer tiles.destroy()
    # The four full cells are one tile; the partial cells at the edges hold their own.
    assert(tiles.stamp(0, 0) == tiles.stamp(1, 0) and tiles.stamp(0, 0) == tiles.stamp(0, 1) and tiles.stamp(0, 0) == tiles.stamp(1, 1))
    assert(tiles.stamp(2, 0) != tiles.stamp(0, 0) and tiles.painted(2, 2))
    try matches_whole(device, width, height, spec)

## The cell plan `spec` gets on a `width` by `height` canvas, as rendered_shape makes it.
func plan_of(spec: ShapeSpec, width: u32, height: u32) -> CellPlan!:
    let reach = shape_reach(spec, width, height) else error(invalid, "the shape misses the canvas")
    var text: collections.Buffer
    defer text.destroy()
    try shape_svg(&text, spec, 0, 0, width, height)
    let parsed = try svg.parse(text.text())
    defer parsed.release()
    return try CellPlan.make(parsed.value(), spec, reach, width, height)

test "only a shape's edge cells are drawn; its inside shares one tile":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    let width: u32 = 3000
    let height: u32 = 2000
    var no_path: f64[]
    # A filled rectangle over cells 1..8 by 1..5: its edges run through the outer
    # ring of those (22 cells), and the 18 within are inside.
    let rectangle = ShapeSpec(kind = 0, x = 300.0, y = 300.0, width = 2000.0, height = 1200.0, filled = true, fill_red = 0.7, fill_green = 0.3,
                              fill_blue = 0.1, path = no_path)
    var plan = try plan_of(rectangle, 3000, 2000)
    assert(plan.count(cell_edge) == 22 and plan.count(cell_inside) == 18)
    plan.close()
    var tiles = try rendered_shape(device, width, height, rectangle)
    assert(tiles.stamp(2, 2) == tiles.stamp(7, 4) and tiles.stamp(2, 2) == tiles.stamp(5, 3) and tiles.stamp(1, 1) != tiles.stamp(2, 2))
    assert(not tiles.painted(0, 0) and not tiles.painted(10, 7))
    tiles.destroy()
    try matches_whole(device, width, height, rectangle)
    # A stroked ellipse: edge cells follow its outline, the middle shares a tile.
    var ellipse = rectangle
    ellipse.kind = 1
    ellipse.x = 100.0
    ellipse.y = 100.0
    ellipse.width = 2800.0
    ellipse.height = 1800.0
    ellipse.stroked = true
    ellipse.stroke_red = 0.0
    ellipse.stroke_green = 0.0
    ellipse.stroke_blue = 0.0
    ellipse.stroke_width = 20.0
    var oval = try plan_of(ellipse, 3000, 2000)
    assert(oval.count(cell_inside) > 0 and oval.count(cell_edge) < oval.count(cell_inside) + oval.count(cell_edge))
    oval.close()
    try matches_whole(device, width, height, ellipse)
    # A stroked, filled pen path of curves, turned.
    var anchors: f64[18] = [200.0, 200.0, 100.0, 900.0, 900.0, -100.0,
                            2700.0, 400.0, 2300.0, -200.0, 2900.0, 1100.0,
                            1300.0, 1850.0, 2400.0, 1950.0, 300.0, 1700.0]
    var path = ellipse
    path.kind = 4
    path.closed = true
    path.stroke_width = 30.0
    path.path = anchors[..<18]
    var curved = try plan_of(turned(path, 7.0), 3000, 2000)
    assert(curved.count(cell_inside) > 0)
    curved.close()
    try matches_whole(device, width, height, turned(path, 7.0))