Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.1

0.45.1 / src / canvas / mesh_warping.lucb

#==============================================================================================
#
#   canvas/mesh_warping - Warp and Puppet Warp on a canvas: shown live, kept once
#
#   DESCRIPTION:
#       Edit › Transform › Warp and Edit › Puppet Warp bend one layer through a mesh over its
#       painted box: a Bézier patch's sixteen points (warp_patch.lucb) or pins
#       (puppet.lucb). While the points or pins move nothing is made at full size: once a
#       frame, when they moved, the mesh is laid again and the layer is shown through a new
#       generation of a stand-in that makes the cells the view draws, at its level
#       (mesh_render.lucb). Keeping the warp makes the cells the mesh came from or lands on
#       once at full size as one undo step, within the selection; cancelling leaves the
#       layer as it was, for it never changed.
#
#==============================================================================================

extend Canvas:
    ## Edit › Transform › Warp on layer `id`: a Bézier patch over its painted
    ## box, as found (`warp_point_*`, `set_warp_point`, `set_warp_preset`),
    ## shown live until `end_mesh_warp`.
    pub func begin_warp(id: i64) -> !:
        try begin_mesh_warp(&self, (u64)id, false, 0)

    ## Edit › Puppet Warp on layer `id`: a mesh over its painted box at
    ## `density` (0 fewer, 1 normal, 2 more) with no pins yet.
    pub func begin_puppet(id: i64, density: i64 = 1) -> !:
        try begin_mesh_warp(&self, (u64)id, true, density)

    ## Whether a warp or puppet warp is under way.
    pub func mesh_warping() -> bool:
        return self.mesh_warp != none

    ## The box the warp's mesh covers: the layer's painted box.
    pub func mesh_box_left() -> f64!:
        return (try mesh_session(&self)).box.left

    pub func mesh_box_top() -> f64!:
        return (try mesh_session(&self)).box.top

    pub func mesh_box_right() -> f64!:
        return (try mesh_session(&self)).box.right

    pub func mesh_box_bottom() -> f64!:
        return (try mesh_session(&self)).box.bottom

    ## Warp's control point `index` (0..15, row by row across the 4 by 4 grid).
    pub func warp_point_x(index: i64) -> f64!:
        return (try mesh_session(&self)).points[(usize)(try warp_index(index)) * 2]

    pub func warp_point_y(index: i64) -> f64!:
        return (try mesh_session(&self)).points[(usize)(try warp_index(index)) * 2 + 1]

    ## Move Warp's control point `index` to (`x`, `y`); the next frame shows it.
    pub func set_warp_point(index: i64, x: f64, y: f64) -> !:
        let shown = try mesh_session(&self)
        let at = (usize)(try warp_index(index))
        if not math.is_finite(x) or not math.is_finite(y):
            error(invalid, "warp points must be finite")
        shown.points[at * 2] = x
        shown.points[at * 2 + 1] = y
        shown.changed = true

    ## Warp's header preset `kind` (warp_patch.lucb's `warp_*`) at `bend` (-1..1):
    ## the points as the preset puts them; -1 puts them back as found.
    pub func set_warp_preset(kind: i64, bend: f64) -> !:
        let shown = try mesh_session(&self)
        if kind < 0:
            try identity_patch(shown.box, shown.points[0..])
        else:
            try bend_patch(shown.box, kind, bend, shown.points[0..])
        shown.changed = true

    ## Put a puppet pin at (`x`, `y`); its index.
    pub func puppet_add_pin(x: f64, y: f64) -> i64!:
        let puppet = try puppet_session(&self)
        let index = try puppet.add_pin(x, y)
        (try mesh_session(&self)).changed = true
        return (i64)index

    ## Drag puppet pin `index` to (`x`, `y`); the next frame shows it.
    pub func puppet_move_pin(index: i64, x: f64, y: f64) -> !:
        let puppet = try puppet_session(&self)
        try puppet.move_pin((usize)math.imax(0, index), x, y)
        (try mesh_session(&self)).changed = true

    ## Take puppet pin `index` away.
    pub func puppet_remove_pin(index: i64) -> !:
        let puppet = try puppet_session(&self)
        try puppet.remove_pin((usize)math.imax(0, index))
        (try mesh_session(&self)).changed = true

    pub func puppet_pin_count() -> i64!:
        return (i64)(try puppet_session(&self)).pin_count

    pub func puppet_pin_x(index: i64) -> f64!:
        return (try puppet_session(&self)).pin_x((usize)math.imax(0, index))

    pub func puppet_pin_y(index: i64) -> f64!:
        return (try puppet_session(&self)).pin_y((usize)math.imax(0, index))

    ## The pin within `reach` pixels of (`x`, `y`); -1 for none.
    pub func puppet_hit(x: f64, y: f64, reach: f64) -> i64!:
        return (try puppet_session(&self)).hit(x, y, reach)

    ## Where the pins take the layer's point (`x`, `y`): for drawing the mesh as it bends.
    pub func puppet_place_x(x: f64, y: f64) -> f64!:
        var px = 0.0
        var py = 0.0
        (try puppet_session(&self)).deform(x, y, &px, &py)
        return px

    pub func puppet_place_y(x: f64, y: f64) -> f64!:
        var px = 0.0
        var py = 0.0
        (try puppet_session(&self)).deform(x, y, &px, &py)
        return py

    ## Keep the warp as one undo step (`keep`), or leave the layer as it was.
    pub func end_mesh_warp(keep: bool) -> !:
        let shown = try mesh_session(&self)
        self.mesh_warp = none
        defer drop_mesh_warping(shown)
        if let composer = self.composer:
            composer.drop_stand_ins()
        if not keep:
            return
        try lay_mesh(shown)
        let mesh = shown.mesh else return
        let document = self.document else trap("warp without a document")
        let composer = self.composer else trap("warp without a compositor")
        let layer = document.find(shown.layer) else error(invalid, "the warped layer is gone")
        let tiles = layer.tiles() else error(invalid, "the warped layer has no pixels")
        try self.remember(shown.layer, try tiles.clone(), name = shown.name)
        # Every cell the pixels leave or land on, within the canvas.
        let span = (f64)tile_size
        let left = math.min(mesh.source.left, mesh.landed.left)
        let top = math.min(mesh.source.top, mesh.landed.top)
        let right = math.max(mesh.source.right, mesh.landed.right)
        let bottom = math.max(mesh.source.bottom, mesh.landed.bottom)
        let first_column = (u32)math.max(0.0, math.floor((left - 1.0) / span))
        let first_row = (u32)math.max(0.0, math.floor((top - 1.0) / span))
        let last_column = (u32)math.min((f64)tiles.columns() - 1.0, math.floor((right + 1.0) / span))
        let last_row = (u32)math.min((f64)tiles.rows() - 1.0, math.floor((bottom + 1.0) / span))
        if right >= 0.0 and bottom >= 0.0:
            var into = Landing(tiles = tiles, made = 0)
            for row in first_row..<last_row + 1:
                try shown.mesher.row_of_cells(&mesh, &shown.original, composer.levels(), row, first_column, last_column, mesh_threads, (void*)&into, land)
            self.mesh_cells_made = into.made
        layer.revision += 1
        try self.restrict_to_selection(shown.layer, &self.history[self.history_count - 1].tiles)

## A warp under way on layer `layer`: its pixels as it began, what bends them,
## and the mesh laid for the last frame.
struct MeshWarping:
    var layer: u64
    var original: Tiles
    var mesher: Mesher
    ## The box the mesh covers: the layer's painted box.
    var box: Footprint
    ## Warp's patch, or Puppet Warp's pins.
    var points: f64[32]
    var puppet: Puppet*?
    var mesh: MeshMap?
    var generation: u64
    ## The points or pins moved since the last frame showed them.
    var changed: bool
    var name: str

## `Canvas.begin_warp` and `Canvas.begin_puppet`: a warp of layer `id`'s pixels.
func begin_mesh_warp(canvas: Canvas*, id: u64, puppet: bool, density: i64) -> !:
    try canvas.require_idle()
    let document = canvas.document else error(invalid, "draw the document once before warping")
    let device = canvas.device else error(invalid, "draw the document once before warping")
    let layer = document.find(id) else error(invalid, "no such layer")
    let tiles = layer.tiles() else error(invalid, "a group has no pixels to warp")
    let found = try tiles.content_bounds() else error(invalid, "the layer has no pixels to warp")
    # A pixel wider all round, so the painted pixels are inside the mesh's soft rim.
    let box = Footprint(left = (f64)found.left - 1.0, top = (f64)found.top - 1.0, right = (f64)found.right + 1.0, bottom = (f64)found.bottom + 1.0)
    if canvas.mesher == none:
        canvas.mesher = try Mesher.create(device)
    var mesher = canvas.mesher else trap("made")
    mesher.srgb = document.mixes_encoded()
    var original = try tiles.clone()
    errdefer original.destroy()
    var points: f64[32] = ---
    try identity_patch(box, points[0..])
    var shaped: Puppet*? = none
    if puppet:
        shaped = try Puppet.create_boxed(box, density)
    canvas.mesh_warp = try new MeshWarping(layer = id, original = original, mesher = mesher, box = box, points = points, puppet = shaped,
                                           mesh = none, generation = 0, changed = true, name = "Puppet Warp" if puppet else "Warp") in memory.heap

## The warp under way; an error when there is none.
func mesh_session(canvas: Canvas*) -> MeshWarping*!:
    return canvas.mesh_warp else error(invalid, "no warp is in progress")

## The puppet warp under way; an error when there is none.
func puppet_session(canvas: Canvas*) -> Puppet*!:
    let shown = try mesh_session(canvas)
    if shown.puppet == none:
        error(invalid, "no puppet warp is in progress")
    return shown.puppet else trap("checked")

## A Warp control point's index, checked.
func warp_index(index: i64) -> i64!:
    if index < 0 or index >= (i64)patch_points:
        error(invalid, "a warp has sixteen control points, 0..15")
    return index

## Once a frame: when the points or pins moved, lay the mesh again and show
## the layer through a new generation of the stand-in.
func settle_mesh_warp(canvas: Canvas*, shown: MeshWarping*) -> !:
    if not shown.changed:
        return
    shown.changed = false
    try lay_mesh(shown)
    let composer = canvas.composer else return
    shown.generation = next_generation()
    try composer.stand_in(StandIn(layer = shown.layer, mask = false, generation = shown.generation, context = (void*)shown, make = mesh_warp_cell))

## The mesh as the points or pins have it now.
func lay_mesh(shown: MeshWarping*) -> !:
    var fresh: MeshMap = ---
    if let puppet = shown.puppet:
        fresh = try puppet.mesh()
    else:
        fresh = try patch_mesh(shown.box, shown.points[0..])
    if let old = shown.mesh:
        var owned = old
        owned.destroy()
    shown.mesh = fresh

## A stand-in's cell (`column`, `row`) of `level`: the warped layer there.
func mesh_warp_cell(context: void*?, layer_levels: Pyramid*, layer: u64, mask: bool, column: u32, row: u32, level: u32) -> Made!:
    let shown = (MeshWarping*)(context else return made_original())
    let mesh = shown.mesh else return made_original()
    let made = try shown.mesher.cell(&mesh, &shown.original, layer_levels, column, row, level)
    let texture = made else return made_nothing()
    return made_texture(texture)

## Threads making a kept warp's cells' fields at once.
let mesh_threads: usize = 8

## Where a kept warp's cells go: the layer's tiles.
struct Landing:
    var tiles: Tiles*
    var made: usize

func land(context: void*, column: u32, row: u32, made: gpu.Texture?) -> !:
    let into = (Landing*)context
    into.made += 1
    if let texture = made:
        try into.tiles.set(column, row, texture)
    else:
        try into.tiles.clear(column, row)

## Let a warp go without keeping it (the canvas closing mid-warp).
func drop_mesh_warp(canvas: Canvas*):
    let shown = canvas.mesh_warp else return
    canvas.mesh_warp = none
    if let composer = canvas.composer:
        composer.drop_stand_ins()
    drop_mesh_warping(shown)

func drop_mesh_warping(shown: MeshWarping*):
    if let mesh = shown.mesh:
        var owned = mesh
        owned.destroy()
    if let puppet = shown.puppet:
        puppet.destroy()
        free(puppet) in memory.heap
    shown.original.destroy()
    free(shown) in memory.heap