Ollin

TrajectoryOfArches

A mast, slanted bars, and straight thread laced between them until curves appear, after Saloua Raouda Choucair's Trajectory of Arches (1969-71).

Run
swift run --package-path Examples Example-Recreations-SalouaRaoudaChoucair-TrajectoryOfArches
Source
Examples/Recreations/SalouaRaoudaChoucair/TrajectoryOfArches/Sketch.swift
//  Recreation after Saloua Raouda Choucair - Trajectory of Arches (1969-71;
//  aluminum and nylon thread, 205 x 55 x 25 cm). The Saloua Raouda Choucair
//  Foundation lists it beside her Tension series. It is an upright mast,
//  bars that cross it at a slant, and thread laced from each bar to the
//  mast. A homage, not a reproduction, and not affiliated with or endorsed
//  by the artist or her estate.
//  https://salouaraoudachoucair.com/artwork/sculptures/tension/
//  https://www.tate.org.uk/art/artists/saloua-raouda-choucair-14735/life-and-work-saloua-raouda-choucair
//
//  An original Ollin interpretation, read off the foundation's photograph of
//  the work. The title, the date, the materials and the size are on record.
//  No drawing of the piece is published. So the lacing here is the simplest
//  one that gives the curves the photograph shows. Where the bars cross the
//  mast, how they slant and how long they are is dealt from the seed, inside
//  what the photograph measures. Nothing was ported: the work is aluminum
//  and thread.

import Foundation
import Ollin

/// *Trajectory of Arches* (Saloua Raouda Choucair, 1969-71): a mast, three
/// slanted bars, and webs of thread between them. **Press** to deal another
/// set of bars.
///
/// Every thread is straight. The mast and the bars carry holes one `pitch`
/// apart. A web is one thread laced back and forth between an arm of a bar
/// and the mast, a hole further along each time. Lace it so that the arm's
/// nearest hole goes to the mast's farthest and the threads cross one
/// another: the eye then reads a curve along their crossings, an arch that
/// no thread follows. Lace the same holes the other way and the threads run
/// side by side as a flat sail. Both can be read in the photograph, and
/// `arches` is the share of webs laced the first way.
///
/// The record gives the work 205 centimeters of height and 55 of width. The
/// split into a mast of 200 and a base of 5 is measured off the photograph.
/// So is the reach of the bars, which stay inside the width. The photograph
/// shows the bars from the front, and whether they all stand in one plane is
/// not on record. So `fan` turns each bar about the mast from the one above
/// it, and at 0 they stand as the photograph shows them.
///
/// One lap strings the piece and takes it down. The thread goes up web by
/// web from the top, each run from one hole to the next. Then the piece
/// holds while the viewer steps to one side, and the thread comes out again
/// the way it went in. `sway` is how far the viewer steps, in degrees.
///
/// Try it: `bars` sets how many bars cross the mast and `pitch` the distance
/// between holes in centimeters. `strung` is how much of the thread is up at
/// the hold. `--export-svg` gives the mast, the bars and every thread as
/// straight lines a pen can draw.
@main
final class TrajectoryOfArches: Sketch {
    /// How many bars cross the mast. The photograph shows three.
    @Param(1 ... 4, icon: "line.diagonal") var bars = 3
    /// The distance from one hole to the next, in centimeters.
    @Param(0.8 ... 4, icon: "ruler") var pitch = 1.2
    /// The share of webs laced nearest hole to farthest, which makes an arch.
    /// The rest are laced nearest to nearest and make a flat sail.
    @Param(0 ... 1, icon: "point.topleft.down.to.point.bottomright.curvepath") var arches = 0.7
    /// How far each bar is turned about the mast from the bar above it, in
    /// degrees. At 0 all the bars stand in one plane.
    @Param(0 ... 90, icon: "rotate.3d") var fan = 0.0
    /// How far to each side the viewer steps during a lap, in degrees.
    @Param(0 ... 70, icon: "figure.walk") var sway = 28.0
    /// How much of the thread is up when the piece holds.
    @Param(0 ... 1, icon: "arrow.up.and.down") var strung = 1.0
    /// Seconds for one lap: the thread laced, held, and taken out.
    @Param(12 ... 30, icon: "timer") var seconds = 24.0

    override var canvasSize: CanvasSize { .portrait1080 }
    override var loopDuration: Double? { seconds }

    /// The work's measures, in centimeters.
    enum Work {
        /// The mast above the base. With the base it makes the 205 on record.
        static let mast = 200.0
        /// The base as the photograph shows it, a slab 42 wide and 5 high.
        /// Its depth is taken as the 25 on record.
        static let base = Vector3(42, 5, 25)
        /// Half the width on record: no bar reaches further from the mast.
        static let reach = 27.5
        /// How far behind the mast's own line a bar passes. In the photograph
        /// the mast hides each bar where they cross.
        static let behind = 1.6
    }

    /// A bar: where it crosses the mast, how it slants, and how long its two
    /// arms are.
    struct Bar {
        /// The height of the crossing above the base.
        var height: Double
        /// The rise of the right arm, in radians. Negative falls to the right.
        var slant: Double
        /// The lengths of the left and right arms, in centimeters.
        var left: Double
        var right: Double
        /// The mast above and below the crossing that this bar's webs may use.
        var roomUp: Double
        var roomDown: Double
    }

    /// A web: one thread laced between an arm and the mast. Holes are counted
    /// from the crossing, 1 being the nearest.
    struct Web {
        var bar: Int
        /// -1 for the left arm, 1 for the right.
        var side: Double
        /// Whether the web goes to the mast above the crossing or below it.
        var up: Bool
        /// A number from 0 to 1 dealt once. The web is an arch while `arches`
        /// is more than it, so raising `arches` only ever turns sails to arches.
        var chance: Double
        /// Dealt shares, turned into holes once the pitch is known: where on
        /// the arm the web starts and ends, and how far along the mast it
        /// starts.
        var armFrom: Double
        var armTo: Double
        var mastFrom: Double
    }

    /// One straight run of thread, from the hole it leaves to the hole it
    /// reaches.
    struct Run {
        var from: Vector3
        var to: Vector3
        var length: Double { (to - from).length }
    }

    private var plan: [Bar] = []
    private var webs: [Web] = []
    private var built = ""
    private var dealt = 0

    private let thread = Color(hex: 0xECE7CD)
    private let aluminum = Color(hex: 0xD6DCDE)
    private let shade = Color(hex: 0x6C7478)

    override func mousePressed() {
        dealt += 1
    }

    // MARK: - The frame

    override func draw() {
        let key = "\(variation)/\(dealt)/\(bars)"
        if key != built {
            (plan, webs) = TrajectoryOfArches.deal(seed: variation &+ dealt &* 7919, bars: bars)
            built = key
        }

        let progress = loopProgress(over: seconds)
        let angle = Double.degrees(sway) * sin(progress * .tau)
        let runs = laced(upTo: share(at: progress) * strung)

        if isVectorExporting {
            drawThePlan(runs, angle: angle)
            return
        }
        drawTheRoom()
        drawTheBase(angle: angle)
        drawTheFrame(angle: angle)
        drawTheThreads(runs, angle: angle)
    }

    /// How much of the thread is up at a point of the lap: none, then laced
    /// at a steady pace, held, and unlaced at a steady pace.
    private func share(at progress: Double) -> Double {
        let up = clamp((progress - 0.04) / 0.40, 0, 1)
        let down = clamp((progress - 0.70) / 0.26, 0, 1)
        return up * (1 - down)
    }

    // MARK: - Dealing the bars

    /// Deals the bars and their webs. Everything comes from the seed, so a
    /// seed is a set of bars.
    static func deal(seed: Int, bars count: Int) -> ([Bar], [Web]) {
        var rng = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 1969)))
        func roll() -> Double { Double(rng.next() >> 11) / Double(1 << 53) }
        func roll(_ low: Double, _ high: Double) -> Double { low + (high - low) * roll() }

        // The crossings come down the mast in order. In the photograph they
        // sit between a sixth and two thirds of the way down.
        var plan: [Bar] = []
        for i in 0 ..< count {
            let down = lerp(0.15, 0.71, (Double(i) + 0.5 + roll(-0.2, 0.2)) / Double(count))
            // The photograph's bars slant by 12, 23 and 41 degrees.
            let slant = Double.degrees(roll(10, 42)) * (roll() < 0.5 ? -1 : 1)
            // An arm is 13 to 35 centimeters long there, and none passes the
            // work's width.
            let longest = Work.reach / cos(slant)
            plan.append(Bar(height: Work.mast * (1 - down), slant: slant,
                            left: min(roll(13, 35), longest), right: min(roll(13, 35), longest),
                            roomUp: 0, roomDown: 0))
        }
        // Each bar's webs keep to its own stretch of the mast: up to halfway
        // to the bar above, and down to halfway to the bar below.
        for i in plan.indices {
            let above = i == 0 ? Work.mast - 2 : (plan[i - 1].height + plan[i].height) / 2
            let below = i == plan.count - 1 ? max(plan[i].height - 40, 12) : (plan[i].height + plan[i + 1].height) / 2
            plan[i].roomUp = above - plan[i].height
            plan[i].roomDown = plan[i].height - below
        }

        var webs: [Web] = []
        for i in plan.indices {
            for side in [-1.0, 1.0] {
                let arm = side < 0 ? plan[i].left : plan[i].right
                let up = roll() < 0.5
                if arm > 18, roll() < 0.55 {
                    // A long arm can carry two webs, as the top bar does in
                    // the photograph: its outer half laced one way along the
                    // mast and its inner half the other.
                    webs.append(Web(bar: i, side: side, up: up, chance: roll(),
                                    armFrom: 0.5, armTo: 1, mastFrom: roll(0.35, 0.6)))
                    webs.append(Web(bar: i, side: side, up: !up, chance: roll(),
                                    armFrom: 0, armTo: 0.5, mastFrom: roll(0, 0.5)))
                } else {
                    webs.append(Web(bar: i, side: side, up: up, chance: roll(),
                                    armFrom: 0, armTo: 1, mastFrom: roll() < 0.35 ? roll(0.1, 0.4) : 0))
                }
            }
        }
        return (plan, webs)
    }

    // MARK: - The lacing

    /// Which way an arm points from the crossing, and where the crossing is.
    private func arm(of web: Web) -> (from: Vector3, along: Vector3) {
        let bar = plan[web.bar]
        // Each bar is turned about the mast from the one above it, the middle
        // bar facing front.
        let turn = Double.degrees(fan) * (Double(web.bar) - Double(plan.count - 1) / 2)
        let across = Vector3(cos(turn), 0, sin(turn))
        let back = Vector3(sin(turn), 0, -cos(turn))
        let along = (across * cos(bar.slant) + Vector3(0, sin(bar.slant), 0)) * web.side
        return (Vector3(0, bar.height, 0) + back * Work.behind, along)
    }

    /// The runs of one web, in the order the thread takes them. The thread
    /// goes out along one run and comes back along the next.
    private func runs(of web: Web) -> [Run] {
        let bar = plan[web.bar]
        let length = web.side < 0 ? bar.left : bar.right
        let room = web.up ? bar.roomUp : bar.roomDown
        let armHoles = Int(length / pitch), mastHoles = Int(room / pitch)
        let opposed = web.chance < arches
        let first = Int(web.armFrom * Double(armHoles)) + 1
        let last = Int(web.armTo * Double(armHoles))
        // A sail starts as far out on the mast as on the arm, which is what
        // keeps its threads parallel. An arch may start anywhere.
        let mastFirst = opposed ? Int(web.mastFrom * Double(mastHoles)) + 1 : first
        let count = min(last - first + 1, mastHoles - mastFirst + 1)
        guard count >= 3 else { return [] }

        let (crossing, along) = arm(of: web)
        let onMast = Vector3(0, web.up ? 1 : -1, 0)
        return (0 ..< count).map { i in
            let onArm = crossing + along * (Double(first + i) * pitch)
            // The whole rule: count the mast's holes the other way and the
            // threads cross into an arch, the same way and they run side by
            // side.
            let hole = opposed ? mastFirst + count - 1 - i : mastFirst + i
            let atMast = Vector3(0, bar.height, 0) + onMast * (Double(hole) * pitch)
            return i % 2 == 0 ? Run(from: onArm, to: atMast) : Run(from: atMast, to: onArm)
        }
    }

    /// Every run that is up when `share` of the thread has been laced, the
    /// last one cut short where the thread has got to.
    private func laced(upTo share: Double) -> [Run] {
        let all = webs.flatMap { runs(of: $0) }
        guard share < 1 else { return all }
        var left = share * all.reduce(0) { $0 + $1.length }
        var up: [Run] = []
        for run in all {
            guard left > 0 else { break }
            if left >= run.length {
                up.append(run)
            } else {
                up.append(Run(from: run.from, to: run.from + (run.to - run.from) * (left / run.length)))
            }
            left -= run.length
        }
        return up
    }

    // MARK: - The view

    /// A point of the work as the viewer sees it: where it lands on the
    /// canvas, how near it is, and the size of a centimeter there. The viewer
    /// stands five meters off, level with the middle of the work, and has
    /// stepped `angle` round the mast.
    private func seen(_ point: Vector3, angle: Double) -> (at: Vector2, near: Double, scale: Double) {
        let across = point.x * cos(angle) - point.z * sin(angle)
        let near = point.x * sin(angle) + point.z * cos(angle)
        let level = (Work.mast - Work.base.y) / 2
        let scale = height * 0.86 / (Work.mast + Work.base.y) * 500 / (500 - near)
        return (Vector2(center.x + across * scale, center.y - (point.y - level) * scale), near, scale)
    }

    // MARK: - Drawing

    /// The backdrop of the photograph: slate blue, lighter toward the floor.
    private func drawTheRoom() {
        background(Color(hex: 0x4E6472))
        noStroke()
        fill(Gradient.linear(from: Vector2(0, 0), to: Vector2(0, height),
                             [Color(hex: 0x536976), Color(hex: 0x516775), Color(hex: 0x4E6472),
                              Color(hex: 0x4C6270), Color(hex: 0x70869A), Color(hex: 0xC4D2D8)]))
        drawRect(0, 0, width, height)
    }

    /// The base, a low slab: the faces the viewer can see, the top lightest.
    private func drawTheBase(angle: Double) {
        let half = Work.base / 2
        // Where the viewer's eye is: five meters off, level with the middle
        // of the work.
        let toEye = Vector3(sin(angle), 0, cos(angle))
        let eye = toEye * 500 + Vector3(0, (Work.mast - Work.base.y) / 2, 0)
        let faces: [(normal: Vector3, corners: [Vector3])] = [
            (Vector3(0, 1, 0), [Vector3(-1, 0, -1), Vector3(1, 0, -1), Vector3(1, 0, 1), Vector3(-1, 0, 1)]),
            (Vector3(0, 0, 1), [Vector3(-1, 0, 1), Vector3(1, 0, 1), Vector3(1, -2, 1), Vector3(-1, -2, 1)]),
            (Vector3(0, 0, -1), [Vector3(-1, 0, -1), Vector3(1, 0, -1), Vector3(1, -2, -1), Vector3(-1, -2, -1)]),
            (Vector3(1, 0, 0), [Vector3(1, 0, -1), Vector3(1, 0, 1), Vector3(1, -2, 1), Vector3(1, -2, -1)]),
            (Vector3(-1, 0, 0), [Vector3(-1, 0, -1), Vector3(-1, 0, 1), Vector3(-1, -2, 1), Vector3(-1, -2, -1)]),
        ]
        noStroke()
        for face in faces {
            // A face shows when the eye is on its outer side.
            let onFace = Vector3(face.normal.x * half.x, face.normal.y > 0 ? 0 : -half.y, face.normal.z * half.z)
            guard face.normal.dot(eye - onFace) > 0 else { continue }
            let light = face.normal.y > 0 ? 1.0 : 0.08 + 0.3 * max(0, face.normal.dot(toEye))
            fill(Color(hex: 0x2E383D).mixed(with: Color(hex: 0xB4BEC0), light))
            drawPolygon(face.corners.map { seen(Vector3($0.x * half.x, $0.y * half.y, $0.z * half.z), angle: angle).at })
        }
    }

    /// The mast and the bars, the farthest first, with their holes.
    private func drawTheFrame(angle: Double) {
        struct Piece { var from: Vector3; var to: Vector3; var isMast: Bool; var near: Double }
        var pieces = [Piece(from: Vector3(0, 0, 0), to: Vector3(0, Work.mast, 0), isMast: true, near: 0)]
        for (i, bar) in plan.enumerated() {
            let left = arm(of: Web(bar: i, side: -1, up: true, chance: 0, armFrom: 0, armTo: 1, mastFrom: 0))
            let right = arm(of: Web(bar: i, side: 1, up: true, chance: 0, armFrom: 0, armTo: 1, mastFrom: 0))
            pieces.append(Piece(from: left.from + left.along * (bar.left + pitch / 2),
                                to: right.from + right.along * (bar.right + pitch / 2),
                                isMast: false, near: seen(left.from, angle: angle).near))
        }
        strokeCap(.butt)
        for piece in pieces.sorted(by: { $0.near < $1.near }) {
            let a = seen(piece.from, angle: angle), b = seen(piece.to, angle: angle)
            if piece.isMast {
                // A square tube 2.4 centimeters across: a face in shade and a
                // lit edge, as the photograph has it.
                stroke(shade)
                strokeWeight(2.4 * a.scale)
                drawLine(a.at, b.at)
                stroke(aluminum)
                strokeWeight(0.5 * a.scale)
                let edge = Vector2(0.95 * a.scale, 0)
                drawLine(a.at + edge, b.at + edge)
            } else {
                stroke(aluminum)
                strokeWeight(1.1 * (a.scale + b.scale) / 2)
                drawLine(a.at, b.at)
            }
        }
    }

    /// The threads, the farthest first and a little dimmer than the near ones.
    private func drawTheThreads(_ runs: [Run], angle: Double) {
        strokeCap(.round)
        strokeWeight(1.15)
        let placed = runs.map { run -> (from: Vector2, to: Vector2, near: Double) in
            let a = seen(run.from, angle: angle), b = seen(run.to, angle: angle)
            return (a.at, b.at, (a.near + b.near) / 2)
        }
        for run in placed.sorted(by: { $0.near < $1.near }) {
            stroke(thread.withAlpha(map(run.near, -Work.reach, Work.reach, 0.55, 0.92, clamped: true)))
            drawLine(run.from, run.to)
        }
        // The holes the thread has reached, as the small knots they are.
        noStroke()
        fill(thread)
        for run in placed {
            drawCircle(center: run.from, radius: 1.1)
            drawCircle(center: run.to, radius: 1.1)
        }
    }

    /// The mast, the bars, the base and every thread as plain lines, as the
    /// viewer sees them at this moment.
    private func drawThePlan(_ runs: [Run], angle: Double) {
        background(.white)
        noFill()
        stroke(.black)
        strokeCap(.round)
        strokeWeight(1.2)
        drawLine(seen(Vector3(0, 0, 0), angle: angle).at, seen(Vector3(0, Work.mast, 0), angle: angle).at)
        for (i, bar) in plan.enumerated() {
            let left = arm(of: Web(bar: i, side: -1, up: true, chance: 0, armFrom: 0, armTo: 1, mastFrom: 0))
            let right = arm(of: Web(bar: i, side: 1, up: true, chance: 0, armFrom: 0, armTo: 1, mastFrom: 0))
            drawLine(seen(left.from + left.along * (bar.left + pitch / 2), angle: angle).at,
                     seen(right.from + right.along * (bar.right + pitch / 2), angle: angle).at)
        }
        let half = Work.base / 2
        let foot = [Vector3(-half.x, 0, half.z), Vector3(half.x, 0, half.z),
                    Vector3(half.x, -Work.base.y, half.z), Vector3(-half.x, -Work.base.y, half.z)]
        drawPolyline(foot.map { seen($0, angle: angle).at }, closed: true)
        strokeWeight(0.5)
        for run in runs {
            drawLine(seen(run.from, angle: angle).at, seen(run.to, angle: angle).at)
        }
    }
}

↑↓ move ↩ open