Ollin

Extension

A floor field of 19,200 painted rods, after Jesús Rafael Soto's Extension (1989).

Run
swift run --package-path Examples Example-Recreations-JesusRafaelSoto-Extension
Source
Examples/Recreations/JesusRafaelSoto/Extension/Sketch.swift
//  Recreation after Jesús Rafael Soto - Extension (1989; Formica and painted
//  metal, 50 x 400 x 300 cm, Centre Pompidou, Paris, AM 2012-117), with Ariel
//  Jiménez's account of it on the museum's page. A homage, not a
//  reproduction, and not affiliated with or endorsed by the artist, his
//  estate, or the museum.
//  https://www.centrepompidou.fr/en/ressources/oeuvre/MZDquYU
//
//  An original Ollin interpretation, written from the record, the account,
//  and the museum's two photographs of the work. Nothing was ported: the
//  work is metal rods set in a white base. The record gives the size and the
//  materials. The count of rods, their height, and where the paint changes
//  were read off the photographs, and are this sketch's reading.

import Foundation
import Ollin

/// *Extension* (Jesús Rafael Soto, Paris, 1989). **Move the pointer** to
/// choose where you stand and how high your eyes are.
///
/// A white base lies on the floor, three meters by four. Thousands of thin
/// metal rods stand in it on a regular grid. Each is painted white at the
/// foot, red above that, and black to the tip. Ariel Jiménez read the rods
/// as the dots of Soto's early paintings pushed straight out of the picture.
/// Rods that hang from above make a Penetrable. Rods set in the floor make
/// an Extension. A few rods are only a few rods, he wrote. Over several
/// square meters they turn into one field of color, where floating masses
/// seem to follow your body as you move.
///
/// Those masses are what this sketch is for, and nothing in it draws them.
/// Every rod is drawn where it stands, at its own width. Wherever your line
/// of sight runs straight down a row of the grid, the rods line up one
/// behind another. You see between them to the white base: a pale aisle
/// that runs to the spot you stand on. Everywhere else the rods overlap
/// into a dark mass. Walk, and each aisle moves across the field to stay
/// with you, while others open along the diagonals of the grid.
///
/// While the pointer rests you stroll past the field and back, `stroll`
/// degrees to each side of where you start, once in thirty seconds. You
/// start `around` degrees from the front, `distance` meters out from the
/// field's edge, your eyes `eyeHeight` meters up. Crouch and the black tips
/// close into one plane. Stand tall and the aisles open to the base.
///
/// Try it: `foot` and `band` are how far up the white and the red reach, in
/// centimeters, and `bandColor` and `tipColor` are the two paints. `lean` is
/// how far off plumb a hand-set rod may stand (`handSeed` deals those), and
/// `thickness` is the rod, in millimeters.
///
/// `--export-svg` draws every rod as three lines through the camera, one for
/// each paint. The grid and the aisles can be read back from the file.
@main
final class Extension: Sketch {
    /// How far you stroll to each side of where you start, in degrees
    /// around the field. Zero stands still.
    @Param(0 ... 60, icon: "figure.walk") var stroll = 20.0
    /// Where you start, turned around the field from the front, in degrees.
    @Param(-180 ... 180, icon: "camera.rotate") var around = 0.0
    /// How far out from the edge of the field you walk, in meters.
    @Param(1.5 ... 6, icon: "arrow.left.and.right") var distance = 3.2
    /// How high your eyes are, in meters.
    @Param(0.3 ... 2.6, icon: "eye") var eyeHeight = 1.6
    /// How far up from the base a rod is white, in centimeters.
    @Param(0 ... 20, icon: "ruler") var foot = 8.5
    /// How far the second paint reaches above the white, in centimeters.
    @Param(0 ... 25, icon: "ruler.fill") var band = 12.0
    /// The second paint: red in 1989.
    @Param(icon: "paintpalette") var bandColor = Color(hex: 0xC92A1E)
    /// The paint from there to the tip: black in 1989.
    @Param(icon: "paintpalette.fill") var tipColor = Color(hex: 0x17171A)
    /// How far off plumb a hand-set rod may stand, in degrees.
    @Param(0 ... 3, icon: "angle") var lean = 0.6
    /// Which way each rod leans. Every number is another hand.
    @Param(1 ... 9999, icon: "dice") var handSeed = 1
    /// How thick a rod is, in millimeters.
    @Param(1 ... 6, icon: "line.diagonal") var thickness = 2.5

    override var canvasSize: CanvasSize { .size(1620, 1080) }
    override var loopDuration: Double? { Extension.loop }

    /// The stroll comes back to its start after this many seconds. It is
    /// slow on purpose: at a walking pace the rods would cross several
    /// pixels a frame, and a row of them would seem to run backward.
    static let loop = 30.0

    // The record's measurements, in meters: 400 x 300 on the floor and 50
    // high in all. The rest is read off the photographs.
    static let fieldWidth = 3.0
    static let fieldDepth = 4.0
    static let overallHeight = 0.5
    static let baseHeight = 0.07
    static var rodLength: Double { overallHeight - baseHeight }
    /// One rod every 2.5 cm: 120 across the short side, 160 along the long.
    static let spacing = 0.025
    static let columns = 120
    static let rows = 160
    static var count: Int { columns * rows }

    private static let white = Color(hex: 0xF3F3F0)
    private static let backdrop = Color(hex: 0xC9C8C4)
    private static let floorColor = Color(hex: 0xA9A69F)

    /// Where rod `i` stands on the base.
    static func foot(_ i: Int) -> Vector3 {
        let column = i % columns, row = i / columns
        return Vector3(-fieldWidth / 2 + (Double(column) + 0.5) * spacing, baseHeight,
                       -fieldDepth / 2 + (Double(row) + 0.5) * spacing)
    }

    /// The way each rod points, up and a little off plumb.
    private var along: [Vector3] = []
    private var built: [Double] = []
    /// One rod a meter wide, lying along z from its foot, in its three
    /// paints, and the paints it was made for.
    private var rod = Mesh.plane(width: 1, depth: 1, segments: 1)
    private var painted: [Double] = []
    private var order: [Int] = Array(0 ..< Extension.count)
    /// How far the pointer has pulled you from the walk: degrees around, and
    /// meters up.
    private var pullAround = 0.0
    private var pullHeight = 0.0
    private var lastMoved = -100.0

    /// The base is three panels side by side, each a meter wide.
    private let panelMesh = Mesh.box(width: Extension.fieldWidth / 3 - 0.004, height: Extension.baseHeight,
                                     depth: Extension.fieldDepth)
    private let floorMesh = Mesh.plane(width: 400, depth: 400)
    private let shadeMesh = Mesh.plane(width: 1, depth: 1, segments: 1)

    // MARK: - The run

    override func draw() {
        let signature = [Double(handSeed), lean]
        if signature != built {
            along = makeRods()
            built = signature
        }
        let eye = whereYouStand()
        let view = Double.pi / 180 * 46
        camera(.perspective(eye: eye, target: Vector3(0, 0.2, 0), fieldOfView: view, near: 0.05, far: 600))

        background(Extension.backdrop)
        if isVectorExporting {
            drawAsLines()
            return
        }
        ambientLight(Color(white: 0.78))
        directionalLight(Color(white: 1), direction: Vector3(-0.3, -0.85, -0.4), intensity: 0.5)

        noStroke()
        fill(Extension.floorColor)
        drawMesh(floorMesh)
        // The base darkens the floor a little all around it.
        withoutLights {
            for (i, margin) in [0.2, 0.15, 0.1, 0.06, 0.03].enumerated() {
                fill(Color(white: 0, alpha: 0.035))
                withState {
                    translate(0, 0.004 + 0.002 * Double(i), 0)
                    scale(Extension.fieldWidth + 2 * margin, 1, Extension.fieldDepth + 2 * margin)
                    drawMesh(shadeMesh)
                }
            }
        }
        fill(Extension.white)
        drawMesh(panelMesh, instances: [-1, 0, 1].map {
            MeshInstance(position: Vector3(Double($0) * Extension.fieldWidth / 3, Extension.baseHeight / 2, 0))
        })
        drawRods(from: eye, pixelsPerRadian: height / 2 / tan(view / 2))
        drawText("move the pointer: across is where you stand, up and down is your eyes", width / 2, height - 26,
                 size: 14, color: Color(white: 0, alpha: 0.4), align: .center, .bottom)
    }

    /// The pointer stands for you while it moves: across the canvas is your
    /// place around the field, and up and down is your eyes. When it rests
    /// you go back to the stroll, which is a function of the clock alone.
    private func whereYouStand() -> Vector3 {
        if mouse != previousMouse { lastMoved = time }
        var wantAround = 0.0, wantHeight = 0.0
        if time - lastMoved < 4 {
            wantAround = (mouseX / width - 0.5) * 240
            wantHeight = (0.3 + (1 - mouseY / height) * 2.3) - eyeHeight
        }
        let ease = 1 - exp(-deltaTime * 4)
        pullAround += (wantAround - pullAround) * ease
        pullHeight += (wantHeight - pullHeight) * ease
        let turn = Double.pi / 180 * (around + pullAround + stroll * sin(2 * Double.pi * time / Extension.loop))
        // An oval, the same distance out from the short sides and the long.
        return Vector3(sin(turn) * (Extension.fieldWidth / 2 + distance), max(0.3, eyeHeight + pullHeight),
                       cos(turn) * (Extension.fieldDepth / 2 + distance))
    }

    // MARK: - The rods

    /// Each rod's direction: straight up, tipped by up to `lean` degrees
    /// whichever way `handSeed` deals it.
    private func makeRods() -> [Vector3] {
        var rng = SplitMix64(seed: UInt64(handSeed) &* 1_000_003 &+ 89)
        let most = tan(lean * Double.pi / 180)
        return (0 ..< Extension.count).map { _ in
            let angle = Double.random(in: 0 ..< 2 * Double.pi, using: &rng)
            // The square root spreads the tips evenly over a small disk.
            let tip = most * Double.random(in: 0 ... 1, using: &rng).squareRoot()
            return Vector3(cos(angle) * tip, 1, sin(angle) * tip).normalized
        }
    }

    /// The three paints of a rod, as distances along it from the base.
    private var paints: [(from: Double, to: Double, color: Color)] {
        let a = min(foot / 100, Extension.rodLength)
        let b = min(a + band / 100, Extension.rodLength)
        return [(0, a, Extension.white), (a, b, bandColor), (b, Extension.rodLength, tipColor)]
            .filter { $0.1 - $0.0 > 1e-6 }
    }

    /// One rod as a flat strip a meter wide along z, a rectangle for each
    /// paint, so a whole rod is one copy of it.
    private func makeRod() -> Mesh {
        var positions: [Vector3] = [], colors: [Color] = [], indices: [UInt32] = []
        for paint in paints {
            let first = UInt32(positions.count)
            positions += [Vector3(-0.5, 0, paint.from), Vector3(0.5, 0, paint.from),
                          Vector3(0.5, 0, paint.to), Vector3(-0.5, 0, paint.to)]
            colors += [Color](repeating: paint.color, count: 4)
            indices += [first, first + 2, first + 1, first, first + 3, first + 2]
        }
        return Mesh(positions: positions, normals: [Vector3](repeating: Vector3(0, 1, 0), count: positions.count),
                    indices: indices, colors: colors)
    }

    private func drawRods(from eye: Vector3, pixelsPerRadian: Double) {
        let recipe = [foot, band] + [bandColor, tipColor].flatMap { [$0.red, $0.green, $0.blue] }
        if recipe != painted {
            rod = makeRod()
            painted = recipe
        }
        // Farthest first, so each rod is laid over the ones behind it. The
        // order is worked out again every frame, so a frame depends on
        // where you stand and on nothing that came before.
        let keys = (0 ..< Extension.count).map { i -> Double in
            let p = Extension.foot(i)
            return (p.x - eye.x) * (p.x - eye.x) + (p.z - eye.z) * (p.z - eye.z)
        }
        order.sort { keys[$0] != keys[$1] ? keys[$0] > keys[$1] : $0 < $1 }
        let width = thickness / 1000
        var copies: [MeshInstance] = []
        copies.reserveCapacity(Extension.count)
        for i in order {
            let base = Extension.foot(i)
            let up = along[i]
            // A rod this thin reads the same from every side, so it is drawn
            // as a flat strip of its width turned toward the eye.
            let toEye = eye - base
            var facing = toEye - up * toEye.dot(up)
            let gap = facing.length
            facing = gap > 1e-9 ? facing / gap : Vector3(0, 0, 1)
            let turn = Extension.rotation(across: facing.cross(up), facing: facing, along: up)
            // A rod narrower than a pixel is drawn a pixel wide and that
            // much fainter, so far rods keep their true share of the picture.
            let pixels = width * pixelsPerRadian / max(toEye.length, 0.05)
            let shown = max(pixels, 1)
            copies.append(MeshInstance(position: base, rotation: turn,
                                       scale: Vector3(width * shown / pixels, 1, 1),
                                       color: Color(white: 1, alpha: pixels / shown)))
        }
        // A flat strip has no round side for a light to model, so the rods
        // take their colors as painted.
        withoutLights {
            fill(Color(white: 1))
            drawMesh(rod, instances: copies)
        }
    }

    /// The turn that lays a strip's width along `across`, its face along
    /// `facing`, and its length along `along`. The three are perpendicular
    /// unit vectors, and `across` x `facing` = `along`.
    static func rotation(across x: Vector3, facing y: Vector3, along z: Vector3) -> Rotation3D {
        let trace = x.x + y.y + z.z
        if trace > 0 {
            let s = (trace + 1).squareRoot() * 2
            return Rotation3D(x: (y.z - z.y) / s, y: (z.x - x.z) / s, z: (x.y - y.x) / s, w: s / 4)
        } else if x.x > y.y && x.x > z.z {
            let s = (1 + x.x - y.y - z.z).squareRoot() * 2
            return Rotation3D(x: s / 4, y: (y.x + x.y) / s, z: (z.x + x.z) / s, w: (y.z - z.y) / s)
        } else if y.y > z.z {
            let s = (1 + y.y - x.x - z.z).squareRoot() * 2
            return Rotation3D(x: (y.x + x.y) / s, y: s / 4, z: (z.y + y.z) / s, w: (z.x - x.z) / s)
        } else {
            let s = (1 + z.z - x.x - y.y).squareRoot() * 2
            return Rotation3D(x: (z.x + x.z) / s, y: (z.y + y.z) / s, z: s / 4, w: (x.y - y.x) / s)
        }
    }

    // MARK: - The plot

    /// The base's outline and every rod as three lines through the camera,
    /// one for each paint.
    private func drawAsLines() {
        let w = Extension.fieldWidth / 2, d = Extension.fieldDepth / 2, h = Extension.baseHeight
        stroke(Color(hex: 0x8C8C88))
        strokeWeight(1)
        for y in [0, h] {
            drawPolyline([Vector3(-w, y, -d), Vector3(w, y, -d), Vector3(w, y, d), Vector3(-w, y, d)], closed: true)
        }
        strokeWeight(0.35)
        for i in 0 ..< Extension.count {
            let base = Extension.foot(i)
            for paint in paints {
                stroke(paint.color == Extension.white ? Color(hex: 0xA7AEB2) : paint.color)
                drawLine(base + along[i] * paint.from, base + along[i] * paint.to)
            }
        }
    }
}

↑↓ move ↩ open