plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


82c4078 — Peter John 2026-11-16T20:16:03+05:30
U4b: paint each kind's own step, no closure in the paint walks, unturned fast paths, raw null-gfx stubs
benchmarks/screen_views/clockwork.plum ADDED
@@ -0,0 +1,53 @@
1
+ # Clockwork, timed per tick: 3600 ticks of its orrery with no input (the planets, moons and
2
+ # 60 walkers move; the orrery culls), from a start with no textures.
3
+ #
4
+ # update each tick through the game (`Game.update`)
5
+ # paint each tick, then the frame (`Game.draw`). Run with PLUM_NULL_GFX=1: there is no
6
+ # host, and the draw calls reach nothing.
7
+ #
8
+ # It prints `us <total> ticks <n> checksum <c>`, the checksum over each walker's place and
9
+ # each planet's turn at the end.
10
+ #
11
+ # plum build benchmarks/screen_views/clockwork.plum -o clockwork
12
+ # PLUM_NULL_GFX=1 ./clockwork update
13
+ # PLUM_NULL_GFX=1 ./clockwork paint
14
+ module clockwork_bench
15
+
16
+ import std/Int
17
+ import std/Float
18
+ import std/Bool
19
+ import std/Str
20
+ import std/Io
21
+ import std/collections/List
22
+ import std/game/Input
23
+ import std/game/Screen
24
+ import std/game/Game
25
+ import std/App
26
+ import benchmarks/screen_views/timing
27
+ import examples/games/clockwork/Clockwork
28
+ import examples/games/clockwork/screens/OrreryScreen
29
+
30
+ primitive ClockworkBench[App] =
31
+ fun orreryOf(g: Game) -> OrreryScreen = match g.screen()
32
+ OrreryScreen as o => o
33
+ _ => OrreryScreen.start(Assets.none())
34
+
35
+ fun state(o: OrreryScreen) -> Int =
36
+ walkers := o.walkers.map(|w| w.place().y.toBits())
37
+ orbits := o.orbits.map(|b| b.hub.angle.toBits())
38
+ Timing.hash(walkers.append(orbits))
39
+
40
+ fun tickOnce(io: Io, g: Game, paint: Bool) -> Game =
41
+ next := g.update(io, Input(), Ticker.Tick)
42
+ if paint
43
+ next.draw(io)
44
+ next
45
+
46
+ fun main(io: Io) =
47
+ args := io.args()
48
+ paint := args.length() > 0 && args.at(0) == "paint"
49
+ sky: Screen := OrreryScreen.start(Assets.none())
50
+ t0 := io.nowMicros()
51
+ done := Range.until(0, 3600).fold(Game(screens: [sky]), |g, t| ClockworkBench.tickOnce(io, g, paint))
52
+ total := io.nowMicros() - t0
53
+ io.printLn("us {Int(total)} ticks 3600 checksum {Timing.hash([ClockworkBench.state(ClockworkBench.orreryOf(done))])}")
compiler/host/src/lib.rs CHANGED
@@ -572,18 +572,25 @@ pub fn hostImports(
572
572
  })?;
573
573
  let name = n.to_string();
574
574
  if caps.null_gfx {
575
- let defaults: Vec<wasmtime::Val> = func_ty
575
+ if let Some(t) = func_ty.results().find(|t| t.default_value().is_none()) {
576
- .results()
577
- .map(|t| t.default_value().ok_or_else(|| anyhow::anyhow!("plum::{n} returns a type with no default")))
576
+ anyhow::bail!("plum::{n} returns a type with no default ({t})");
577
+ }
578
- .collect::<Result<_>>()?;
578
+ let n_results = func_ty.results().len();
579
- return Ok(wasmtime::Extern::Func(wasmtime::Func::new(
579
+ // Raw, so a call reads none of its arguments: a typed `Func::new` loads
580
+ // each into a `Val` (rooting each reference), which cost about a quarter
581
+ // of a timed paint (benchmarks/screen_views) for nothing.
582
+ // SAFETY: the closure writes exactly the function's results, each a
583
+ // zeroed `ValRaw`: 0 for a number, null for a reference (each result
584
+ // type was checked above to have a default, so a nullable one).
580
- &mut *store,
585
+ let f = unsafe {
581
- func_ty,
586
+ wasmtime::Func::new_unchecked(&mut *store, func_ty, move |_caller, vals| {
582
- move |_caller, _params, results| {
587
+ for v in vals.iter_mut().take(n_results) {
583
- results.clone_from_slice(&defaults);
588
+ v.write(wasmtime::ValRaw::v128(0));
589
+ }
584
590
  Ok(())
585
- },
591
+ })
586
- )));
592
+ };
593
+ return Ok(wasmtime::Extern::Func(f));
587
594
  }
588
595
  Ok(wasmtime::Extern::Func(wasmtime::Func::new(
589
596
  &mut *store,
@@ -856,9 +863,16 @@ mod tests {
856
863
  let wat = r#"(module
857
864
  (import "plum" "screenWidth" (func $w (param anyref) (result f64)))
858
865
  (import "plum" "drawRect" (func $r (param anyref f64)))
866
+ (import "plum" "pair" (func $p (param anyref i32 f64) (result i32 anyref f64)))
859
867
  (func (export "go") (result f64)
868
+ (local $n anyref) (local $f f64)
860
869
  (call $r (ref.null any) (f64.const 1))
870
+ (call $p (ref.null any) (i32.const 7) (f64.const 2))
871
+ (local.set $f)
872
+ (local.set $n)
873
+ (if (i32.ne (i32.const 0)) (then (unreachable)))
874
+ (if (i32.eqz (ref.is_null (local.get $n))) (then (unreachable)))
861
- (call $w (ref.null any))))"#;
875
+ (f64.add (local.get $f) (call $w (ref.null any)))))"#;
862
876
  let engine = engine();
863
877
  let module = wasmtime::Module::new(&engine, wat::parse_str(wat).unwrap()).unwrap();
864
878
  for null_gfx in [false, true] {
docs/backlog.md CHANGED
@@ -28,6 +28,7 @@ The retained scene tree was replaced by master's screen view in functional form:
28
28
  - **Closure calls are untyped.** A closure is held as `anyref`: each call does two casts and an indirect call, and building one allocates two objects. A typed closure struct per signature with `call_ref` would fix it. Indirect evidence: `cons_rec` (1.94 s) and `cons` without fusion (1.0 s) did not move.
29
29
  - **A value struct inside an enum payload is boxed twice** (`Some(Pos)` allocates two objects). No measurement yet.
30
30
  - **In-place updates still miss** 36 of Orb Run's 56 spreads. Reasons, by count: a value rebound by a pattern (8), the receiver read again after a method call (5), a shared value (4), a field passed on without being written back (4), an argument passed from inside a closure (4).
31
+ - **Paint** (U4b, 2026-11-16, `sl/paint-direct`): each kind of view has its own copy of the draw's step (`View.paintAt`, `paintCulledAt`, `@final`), so an item costs one trait call in place of four; the walks recurse over the list with no closure (`List.each` with a capturing closure cost about 15% of a paint), skip the walk under an empty view, and an unturned `Place.under`, `Affine.scaleX`/`scaleY`/`rotation`, `Views.through` and a `Left` label skip the matrix product, `hypot`, `atan2` and the text width. `PLUM_NULL_GFX`'s stubs are raw host functions (a typed `Func::new` loaded every argument as a `Val`: a quarter of a timed paint). Instructions retired by the paint (paint run minus update run): Orbit Runner 2,659M to 1,087M, Clockwork (`benchmarks/screen_views/clockwork.plum`, new) 1,394M to 692M, `scale.plum` 7,750M to 2,740M; about a third of each drop is the null host. Open: wasmtime's `compiler_inlining` takes another 20% of instructions off paint and update both, but inlined frames drop out of a crash's `in f (called from g)` (`crash_in_a_loop_names_the_function_not_its_lifted_loop` fails), so it is off; still made each frame: the view's list (spreads upcast and copied) and each item's own list; `List.each`/`map` with a capturing closure is still slow everywhere else (`Views.stepIn`, `drawnIn`).
31
32
  - **Static functions are slower than free functions** (0.78 s against 0.57 s on `benchmarks/static_calls`).
32
33
  - **Which closures are inlined depends on the whole program**: closure specialisation copies a callee for at most 8 closures program-wide (`closure_spec::MAX_COPIES`), in the order it meets them, so a closure-taking walk added anywhere (std's `List.filter`, `any`) can turn another call of the same walk back into a closure call (`call_ref`, slower). In-place updates no longer depend on it (U1). A cap per caller, or copies ranked by how hot the caller is, would make it stable.
33
34
  - **Smaller codegen items**: match arms test then cast (`br_on_cast` would do one check); the inliner skips leaves that use their locals (`unbox.*`); every struct has its own empty supertype; purity treats every closure-taking function as impure, so pipelines fuse only after closure specialisation.
docs/game.md CHANGED
@@ -135,6 +135,7 @@ trait View = # what to draw
135
135
  fun view(self) -> List<View> # default: empty
136
136
  fun visible(self) -> Bool # default: true
137
137
  fun paintSelf(self, io: Io, m: Affine, alpha: Float) # default: nothing
138
+ @final fun paintAt(..), paintCulledAt(..) # the draw's step for one item: not written
138
139
 
139
140
  trait Actor[View] = # acts: state that advances each tick
140
141
  fun step(self, dt: Float) -> Self
@@ -223,7 +224,7 @@ Every kind is at the origin of what it is in unless given an `x` and a `y`: an a
223
224
 
224
225
  ### How std draws a view
225
226
 
226
- The game draws the shown screen once per frame (`Game.draw`): it clears with the screen's `background()` and draws the screen's `view()`, made for the frame, through its camera's transform (the world to the window's pixels) with `Views.paint`: each visible item through its place, its `paintSelf` and then its own view, multiplying the alphas down (after a `Blend`'s view, the blend mode goes back to the one it was drawn in). An item scaled flat (0 on an axis) is drawn as the host draws it, and never hit, nor what is under it. A screen that `culls()` is drawn with `Views.paintCulled`, which skips each `Sized` item whose bounds miss what the camera shows, with what is under it. Then the screen's `hud(window)` is drawn one unit a pixel. During a transition both screens are drawn this way, each on its own background, moved and faded. `Game.drawList()` lists what a frame draws, each item with its transform and alpha (`Drawn`), culled as the frame is, the hud's after the view's; std/game/TestRig reads it.
227
+ The game draws the shown screen once per frame (`Game.draw`): it clears with the screen's `background()` and draws the screen's `view()`, made for the frame, through its camera's transform (the world to the window's pixels) with `Views.paint`: each visible item through its place, its `paintSelf` and then its own view, multiplying the alphas down (after a `Blend`'s view, the blend mode goes back to the one it was drawn in). An item scaled flat (0 on an axis) is drawn as the host draws it, and never hit, nor what is under it. A screen that `culls()` is drawn with `Views.paintCulled`, which skips each `Sized` item whose bounds miss what the camera shows, with what is under it. Each kind of view draws its own items: `View` has two `@final` methods, `paintAt` and `paintCulledAt` (the walks' step for one item), so each kind gets its own copy, in which its `visible`, `place`, `paintSelf` and `view` are direct calls; an item drawn costs one call through `View`, and an item with an empty view starts no walk under it. Then the screen's `hud(window)` is drawn one unit a pixel. During a transition both screens are drawn this way, each on its own background, moved and faded. `Game.drawList()` lists what a frame draws, each item with its transform and alpha (`Drawn`), culled as the frame is, the hud's after the view's; std/game/TestRig reads it.
227
228
 
228
229
  Nothing is kept between frames, and there is no tree: the game keeps the screens and their state, the camera's inputs (each screen's `size()`, `viewport()` and `camera`, the window's size) and the transition, and works out the rest when it needs it. One walk goes over a view for everything (draw, cull, list, hit test, find): an item, then its `view()`. The walks are the statics of `Views` in `std/game/View` (`paint`, `paintCulled`, `drawnIn`, `hitIn`, `worldIn`), with `stepAll`, `stepSome` and `stepIn` for a derived stepping.
229
230
 
std/game/Label.plum CHANGED
@@ -43,7 +43,9 @@ struct Label[View, Sized, Touch](x: Float = 0.0, y: Float = 0.0, text: Str, size
43
43
  # How far left of its place its text starts.
44
44
  fun lead(self) -> Float = self.align.lead(Label.textWidth(self.text, self.size))
45
45
 
46
+ fun bounds(self) -> Aabb =
47
+ w := Label.textWidth(self.text, self.size)
46
- fun bounds(self) -> Aabb = Aabb.of(0.0 - self.lead(), 0.0, Label.textWidth(self.text, self.size), self.size)
48
+ Aabb.of(0.0 - self.align.lead(w), 0.0, w, self.size)
47
49
 
48
50
  fun touch(self) -> Touchable = self.touch
49
51
 
@@ -79,7 +81,10 @@ struct Label[View, Sized, Touch](x: Float = 0.0, y: Float = 0.0, text: Str, size
79
81
  # and scaled as `m` turns and scales it. Paints with `alpha` in place of the colour's
80
82
  # alpha, as `Box.draw` does. Unturned text is a `Gfx.drawText`, turned text a `Gfx.drawTextEx`.
81
83
  fun draw(io: Io, m: Affine, alpha: Float, s: Str, size: Float, color: Color, align: Align = Left) =
84
+ lead := match align
85
+ Left => 0.0
86
+ _ => align.lead(Label.textWidth(s, size))
82
- p := m.apply(Vec2(x: 0.0 - align.lead(Label.textWidth(s, size)), y: 0.0))
87
+ p := m.apply(Vec2(x: 0.0 - lead, y: 0.0))
83
88
  if m.b == 0.0 && m.a > 0.0
84
89
  Gfx.drawText(io, s, p.x, p.y, size * m.scaleY(), color.withAlpha(alpha))
85
90
  else
std/game/Place.plum CHANGED
@@ -71,7 +71,16 @@ struct Place(
71
71
  fun fade(self, a: Float) -> Place = Place(..self, alpha: a)
72
72
 
73
73
  # The transform of what is under it, `m` being its parent's: `m` times `local`.
74
+ # An unturned, unscaled place (most of them) only moves `m`: its `local` is
75
+ # (1, 0, 0, 1, e, f), and `m.mul` of it is `m`'s first four with the move, the same sums
76
+ # without the products by 1 and 0.
74
- fun under(self, m: Affine) -> Affine = m.mul(self.local())
77
+ fun under(self, m: Affine) -> Affine =
78
+ if self.rotation == 0.0 && self.scaleX == 1.0 && self.scaleY == 1.0
79
+ e := self.x + self.originX - self.originX
80
+ f := self.y + self.originY - self.originY
81
+ Affine(a: m.a, b: m.b, c: m.c, d: m.d, e: m.a * e + m.c * f + m.e, f: m.b * e + m.d * f + m.f)
82
+ else
83
+ m.mul(self.local())
75
84
 
76
85
  # Where its origin lands in the parent: the point it turns and scales about, the same
77
86
  # whatever its rotation and scale (`Place.pivoted(x, y, ox, oy)`'s is (x + ox, y + oy)).
std/game/View.plum CHANGED
@@ -43,6 +43,8 @@ import std/game/Gfx
43
43
  # Nothing by default. A game says what it draws with ready-made views in its
44
44
  # `view` (a glow is a `Blend` of `Circle`s, a string a `Line`); `paintSelf`
45
45
  # with `Gfx` through `m` is the escape hatch for what none of them draws
46
+ # paintAt the draw's step for one item (and `paintCulledAt`, the culling draw's):
47
+ # `@final`, so each kind has its own copy, which calls the four above directly
46
48
  #
47
49
  # A view whose state advances each tick (its place, its clock) is an `Actor`, which
48
50
  # extends `View` with `step(self, dt) -> Self`, its next version `dt` seconds on. A screen
@@ -100,6 +102,39 @@ trait View =
100
102
 
101
103
  fun paintSelf(self, io: Io, m: Affine, alpha: Float) = Unit
102
104
 
105
+ # `Views.paintIn`'s step for one item, its own for each kind of view: in a kind's copy of
106
+ # it `visible`, `place`, `paintSelf` and `view` are the kind's own, called directly, so
107
+ # an item drawn costs one call through `View` in place of four.
108
+ @final fun paintAt(self, io: Io, m: Affine, alpha: Float, mode: Int) =
109
+ if self.visible()
110
+ at := self.place()
111
+ mk := at.under(m)
112
+ ak := alpha * at.alpha
113
+ me: View := self
114
+ blend := Views.blendOf(me)
115
+ self.paintSelf(io, mk, ak)
116
+ under := self.view()
117
+ if !under.isEmpty()
118
+ Views.paintIn(io, under, mk, ak, if blend < 0 then mode else blend)
119
+ if blend >= 0
120
+ Gfx.blendMode(io, mode)
121
+
122
+ # `Views.paintCulledIn`'s step for one item, as `paintAt` is `paintIn`'s.
123
+ @final fun paintCulledAt(self, io: Io, m: Affine, alpha: Float, area: Aabb, mode: Int) =
124
+ if self.visible()
125
+ at := self.place()
126
+ mk := at.under(m)
127
+ me: View := self
128
+ if !Views.missesArea(me, mk, area)
129
+ ak := alpha * at.alpha
130
+ blend := Views.blendOf(me)
131
+ self.paintSelf(io, mk, ak)
132
+ under := self.view()
133
+ if !under.isEmpty()
134
+ Views.paintCulledIn(io, under, mk, ak, area, if blend < 0 then mode else blend)
135
+ if blend >= 0
136
+ Gfx.blendMode(io, mode)
137
+
103
138
  # A view with state that advances each tick: its next version `dt` seconds on. The screen
104
139
  # holding it steps it in its frame action (std/game/Screen's `stepped`).
105
140
  trait Actor[View] =
@@ -153,18 +188,10 @@ primitive Views =
153
188
  # enclosing `Blend`'s, or `Blend.Normal`).
154
189
  fun paint(io: Io, items: List<View>, m: Affine, alpha: Float) = Views.paintIn(io, items, m, alpha, Blend.Normal)
155
190
 
156
- fun paintIn(io: Io, items: List<View>, m: Affine, alpha: Float, mode: Int) = items.each(|k| Views.paintItem(io, k, m, alpha, mode))
157
-
158
- fun paintItem(io: Io, a: View, m: Affine, alpha: Float, mode: Int) =
191
+ fun paintIn(io: Io, items: List<View>, m: Affine, alpha: Float, mode: Int) =
159
- if a.visible()
160
- at := a.place()
161
- mk := at.under(m)
162
- ak := alpha * at.alpha
163
- blend := Views.blendOf(a)
192
+ if Cons(k, rest) := items
164
- a.paintSelf(io, mk, ak)
193
+ k.paintAt(io, m, alpha, mode)
165
- Views.paintIn(io, a.view(), mk, ak, if blend < 0 then mode else blend)
194
+ Views.paintIn(io, rest, m, alpha, mode)
166
- if blend >= 0
167
- Gfx.blendMode(io, mode)
168
195
 
169
196
  # The blend mode `a` draws its view in: a `Blend`'s, -1 for any other view.
170
197
  fun blendOf(a: View) -> Int = match a
@@ -176,18 +203,9 @@ primitive Views =
176
203
  fun paintCulled(io: Io, items: List<View>, m: Affine, alpha: Float, area: Aabb) = Views.paintCulledIn(io, items, m, alpha, area, Blend.Normal)
177
204
 
178
205
  fun paintCulledIn(io: Io, items: List<View>, m: Affine, alpha: Float, area: Aabb, mode: Int) =
179
- items.each(|a|
180
- if a.visible()
181
- at := a.place()
182
- mk := at.under(m)
183
- if !Views.missesArea(a, mk, area)
206
+ if Cons(k, rest) := items
184
- ak := alpha * at.alpha
185
- blend := Views.blendOf(a)
186
- a.paintSelf(io, mk, ak)
207
+ k.paintCulledAt(io, m, alpha, area, mode)
187
- Views.paintCulledIn(io, a.view(), mk, ak, area, if blend < 0 then mode else blend)
208
+ Views.paintCulledIn(io, rest, m, alpha, area, mode)
188
- if blend >= 0
189
- Gfx.blendMode(io, mode)
190
- )
191
209
 
192
210
  # Pushes onto `out` what `items` draw, in the order drawn: each visible item with its
193
211
  # transform and its alpha, then what its view draws. With `cull`, a `Sized` item whose
@@ -261,8 +279,19 @@ primitive Views =
261
279
  Sized as s => !Views.through(s.bounds(), m).overlaps(area)
262
280
  _ => false
263
281
 
264
- # The box around `b` seen through `m`.
282
+ # The box around `b` seen through `m`. Through an unturned `m` (most), the box's
283
+ # corners on each axis are its own two moved and scaled.
265
284
  fun through(b: Aabb, m: Affine) -> Aabb =
285
+ if m.b == 0.0 && m.c == 0.0
286
+ x1 := m.a * b.min.x + m.e
287
+ x2 := m.a * b.max.x + m.e
288
+ y1 := m.d * b.min.y + m.f
289
+ y2 := m.d * b.max.y + m.f
290
+ Aabb(min: Vec2(x: x1.min(x2), y: y1.min(y2)), max: Vec2(x: x1.max(x2), y: y1.max(y2)))
291
+ else
292
+ Views.throughTurned(b, m)
293
+
294
+ fun throughTurned(b: Aabb, m: Affine) -> Aabb =
266
295
  p1 := m.apply(b.min)
267
296
  p2 := m.apply(Vec2(x: b.max.x, y: b.min.y))
268
297
  p3 := m.apply(b.max)
std/math/Affine.plum CHANGED
@@ -53,11 +53,16 @@ struct Affine(a: Float, b: Float, c: Float, d: Float, e: Float, f: Float) =
53
53
  e: 0.0 - {ia * self.e + ic * self.f},
54
54
  f: 0.0 - {ib * self.e + id * self.f}))
55
55
 
56
+ # How much it stretches its x axis. Unturned (most draws), `|a|`, which `hypot` with a
57
+ # zero side is exactly, without its call.
56
58
  fun scaleX(self) -> Float =
57
- Math.hypot(self.a, self.b)
59
+ if self.b == 0.0 then self.a.abs() else Math.hypot(self.a, self.b)
58
60
 
61
+ # How much it stretches its y axis. Unturned, `|d|`.
59
62
  fun scaleY(self) -> Float =
60
- Math.hypot(self.c, self.d)
63
+ if self.c == 0.0 then self.d.abs() else Math.hypot(self.c, self.d)
61
64
 
65
+ # How far it turns its x axis, in radians. Unturned and not mirrored, `b` (a zero of
66
+ # `b`'s sign, as `atan2` gives), without the `atan2`.
62
67
  fun rotation(self) -> Float =
63
- Math.atan2(self.b, self.a)
68
+ if self.b == 0.0 && self.a > 0.0 then self.b else Math.atan2(self.b, self.a)
tests/std/game/Place.plum CHANGED
@@ -1,4 +1,5 @@
1
1
  import std/Float
2
+ import std/collections/List
2
3
  import std/math/Math
3
4
  import std/math/Vec2
4
5
  import std/math/Affine
@@ -51,3 +52,10 @@ primitive PlaceTest[UnitTest] =
51
52
  assert Place.at(1.0, 2.0).scaleTo(0.0, 1.0).toLocal(Vec2(x: 1.0, y: 2.0)).isNone()
52
53
  assert p.alpha == 0.5
53
54
  assert Place.at(1.0, 2.0).under(Affine.Identity).e == 1.0
55
+
56
+ # `under` an unturned, unscaled place is `m` times its `local`, the same numbers
57
+ fun underAnUnmovedPlaceIsMTimesItsLocal() =
58
+ m := Affine(a: 0.6, b: 0.8, c: -0.8, d: 0.6, e: 7.0, f: -3.0)
59
+ places := [Place.at(10.0, 20.0), Place.pivoted(0.1, 0.2, 24.0, 0.7), Place.at(-3.5, 0.0).fade(0.5), Place.turned(1.0, 2.0, 2.0, 0.5), Place.turned(1.0, 2.0, 1.0, 0.0).pivotAt(3.0, 4.0)]
60
+ assert places.all(|p| p.under(m) == m.mul(p.local()))
61
+ assert places.all(|p| p.under(Affine.Identity) == Affine.Identity.mul(p.local()))
tests/std/game/View.plum CHANGED
@@ -150,6 +150,12 @@ primitive ViewTest[UnitTest] =
150
150
  b := Views.through(Aabb.of(0.0, 0.0, 10.0, 20.0), Affine(a: 0.0, b: 1.0, c: -1.0, d: 0.0, e: 100.0, f: 0.0))
151
151
  assert b.min.x == 80.0 && b.max.x == 100.0 && b.min.y == 0.0 && b.max.y == 10.0
152
152
 
153
+ # an unturned transform bounds a box by its moved and scaled corners, as its four corners do
154
+ fun anUnturnedTransformBoundsABoxAsItsCornersDo() =
155
+ box := Aabb.of(-3.0, 2.0, 10.0, 20.0)
156
+ flat := [Affine(a: 2.0, b: 0.0, c: 0.0, d: 0.5, e: 100.0, f: -7.0), Affine(a: -2.0, b: 0.0, c: 0.0, d: -0.5, e: 1.0, f: 0.0), Affine.Identity]
157
+ assert flat.all(|m| Views.through(box, m) == Views.throughTurned(box, m))
158
+
153
159
  # A view that logs where it is painted (the x of its origin) and at what alpha.
154
160
 
155
161
  # a screen's derived `stepped` (it lists no `Steps`) steps each field holding actors (the
tests/std/math/Affine.plum CHANGED
@@ -2,6 +2,7 @@ import std/Float
2
2
  import std/math/Math
3
3
  import std/math/Vec2
4
4
  import std/Option
5
+ import std/collections/List
5
6
  import std/math/Affine
6
7
  import std/Test
7
8
 
@@ -20,3 +21,13 @@ primitive AffineTest[UnitTest] =
20
21
  assert {p.y - 1.0}.abs() < 1e-6
21
22
  else
22
23
  assert false
24
+
25
+ # an unturned transform's scales and rotation are those `hypot` and `atan2` give, read off it
26
+ fun anUnturnedTransformsScalesAreItsOwn() =
27
+ flat := [Affine(a: 2.0, b: 0.0, c: 0.0, d: -3.0, e: 1.0, f: 1.0), Affine(a: -0.5, b: -0.0, c: -0.0, d: 0.25, e: 0.0, f: 0.0), Affine(a: 3.0, b: -0.0, c: 0.0, d: 1.0, e: 0.0, f: 0.0), Affine.Identity]
28
+ assert flat.all(|m| m.scaleX() == Math.hypot(m.a, m.b) && m.scaleY() == Math.hypot(m.c, m.d))
29
+ assert flat.all(|m| m.rotation().toBits() == Math.atan2(m.b, m.a).toBits())
30
+ t := Affine(a: 0.6, b: 0.8, c: -0.8, d: 0.6, e: 0.0, f: 0.0)
31
+ assert Math.near(t.scaleX(), 1.0, 1e-12) && Math.near(t.scaleY(), 1.0, 1e-12)
32
+ assert t.rotation() == Math.atan2(0.8, 0.6)
33
+ assert Affine(a: -1.0, b: 0.0, c: 0.0, d: 1.0, e: 0.0, f: 0.0).rotation() == Float.Pi