s LearnSqueak.com

SMALLTALK LAB

Build orthonormal camera axes with modified Gram–Schmidt

Turn three imperfect camera direction hints into unit axes perpendicular to each other. See what remains after each projection, and reject hints that cannot define a new axis.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Squeak Workspace and Print it for each example independently. Vectors are arrays with three coordinates. Use moderate finite values. Here the hints are forward, up, right; this is a basis construction lesson, not a complete view-matrix builder.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Measure how far an up hint leans

Use the dot product to measure alignment. A negative dot here means the up hint includes a component opposite the forward direction. Three zeros are not a valid direction.

Independent Workspace expression - select all, then Print it
| dot forward up |
dot := [:a :b |
  (a first * b first) + (a second * b second)
    + (a third * b third)].
forward := #(0 0 -3).
up := #(0 2 2).
{dot value: forward value: forward.
 dot value: forward value: up}

Expected: #(9 -6)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Measure how far an up hint leans
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The forward hint has length 3 because its squared length is 9.

Download this checkpoint

02 - Normalize the first direction

Divide each component by the length. The resulting forward axis still has the same direction but is exactly one unit long in this simple fixture.

Independent Workspace expression - select all, then Print it
| dot unit forward |
dot := [:a :b |
  (a first * b first) + (a second * b second)
    + (a third * b third)].
unit := [:v |
  | n |
  n := (dot value: v value: v) sqrt.
  v collect: [:x | x / n]].
forward := unit value: #(0 0 -3).
{forward. dot value: forward value: forward}

Expected: #(#(0 0 -1) 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Normalize the first direction
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The negative z sign is intentional: this camera looks along −z.

Download this checkpoint

03 - Remove the component along forward

For a unit axis q, the dot product q·v is the coefficient of the projection. Subtract that projection from the current residual, not from the original hint after every axis.

Independent Workspace expression - select all, then Print it
| dot remove residual |
dot := [:a :b |
  (a first * b first) + (a second * b second)
    + (a third * b third)].
remove := [:v :q |
  | weight |
  weight := dot value: q value: v.
  (1 to: 3) collect: [:i |
    (v at: i) - (weight * (q at: i))]].
residual := remove value: #(0 2 2) value: #(0 0 -1).
{residual. dot value: residual value: #(0 0 -1)}

Expected: #(#(0 2 0) 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Remove the component along forward
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The cleaned up hint has no forward component. Its magnitude is 2, so normalize it next.

Download this checkpoint

04 - Update the third hint twice

Modified Gram–Schmidt removes an accepted axis from the current residual, then uses that reduced residual for the next dot product. Here right loses its forward component first and its up component second.

Independent Workspace expression - select all, then Print it
| dot remove afterForward afterUp |
dot := [:a :b |
  (a first * b first) + (a second * b second)
    + (a third * b third)].
remove := [:v :q |
  | weight |
  weight := dot value: q value: v.
  (1 to: 3) collect: [:i |
    (v at: i) - (weight * (q at: i))]].
afterForward := remove value: #(2 2 -1) value: #(0 0 -1).
afterUp := remove value: afterForward value: #(0 1 0).
{afterForward. afterUp}

Expected: #(#(2 2 0) #(2 0 0))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Update the third hint twice
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The two residuals are different. The final right direction is +(1,0,0) after normalization.

Download this checkpoint

05 - Build the camera basis and check every dot

Feed forward, up, then right as axis hints. Each accepted unit vector joins the basis; remove every accepted axis before normalizing a new hint. The dot table is the identity here: diagonal entries are 1 and off-diagonal entries are 0.

Independent Workspace expression - select all, then Print it
| dot remove unit basis q |
dot := [:a :b |
  (a first * b first) + (a second * b second)
    + (a third * b third)].
remove := [:v :axisVector |
  | weight |
  weight := dot value: axisVector value: v.
  (1 to: 3) collect: [:i |
    (v at: i) - (weight * (axisVector at: i))]].
unit := [:v |
  | n |
  n := (dot value: v value: v) sqrt.
  v collect: [:x | x / n]].
basis := [:hints |
  | accepted |
  accepted := OrderedCollection new.
  hints do: [:hint |
    | v original residual |
    v := hint.
    original := (dot value: hint value: hint) sqrt.
    accepted do: [:axis | v := remove value: v value: axis].
    residual := (dot value: v value: v) sqrt.
    residual <= (1.0e-10 * original)
      ifTrue: [Error signal: 'Dependent axis hint'].
    accepted add: (unit value: v)].
  accepted asArray].
q := basis value: #(#(0 0 -3) #(0 2 2) #(2 2 -1)).
{q. q collect: [:a | q collect: [:b | dot value: a value: b]]}

Expected: #(#(#(0 0 -1) #(0 1 0) #(1 0 0)) #(#(1 0 0) #(0 1 0) #(0 0 1)))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Build the camera basis and check every dot
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: These are forward/up/right, in that order. For right-handed camera coordinates use right, up and backward (the negation of forward).

Download this checkpoint

06 - Reject dependent hints and validate a tilted basis

An input whose residual length is at most 10⁻¹⁰ times its input length contributes no usable new direction in this demo; reject before dividing. Also validate a tilted basis with approximate unit and orthogonality checks, without expecting binary floating-point decimals to equal exact fractions.

Independent Workspace expression - select all, then Print it
| dot remove unit basis tilted rejected near unitOK orthogonalOK |
dot := [:a :b |
  (a first * b first) + (a second * b second)
    + (a third * b third)].
remove := [:v :axisVector |
  | weight |
  weight := dot value: axisVector value: v.
  (1 to: 3) collect: [:i |
    (v at: i) - (weight * (axisVector at: i))]].
unit := [:v |
  | n |
  n := (dot value: v value: v) sqrt.
  v collect: [:x | x / n]].
basis := [:hints |
  | accepted |
  accepted := OrderedCollection new.
  hints do: [:hint |
    | v original residual |
    v := hint.
    original := (dot value: hint value: hint) sqrt.
    accepted do: [:axis | v := remove value: v value: axis].
    residual := (dot value: v value: v) sqrt.
    residual <= (1.0e-10 * original)
      ifTrue: [Error signal: 'Dependent axis hint'].
    accepted add: (unit value: v)].
  accepted asArray].
tilted := basis value: #(#(3 4 0) #(0 0 5) #(4 -3 1)).
rejected := [basis value: #(#(1 2 3) #(2 4 6)). false]
  on: Error do: [:e | e messageText = 'Dependent axis hint'].
near := [basis value: #(#(1 0 0) #(1 1.0e-12 0)). false]
  on: Error do: [:e | e messageText = 'Dependent axis hint'].
unitOK := tilted allSatisfy: [:a | ((dot value: a value: a) - 1) abs < 1.0e-9].
orthogonalOK := ((dot value: tilted first value: tilted second) abs < 1.0e-9)
  and: [((dot value: tilted first value: tilted third) abs < 1.0e-9)
  and: [(dot value: tilted second value: tilted third) abs < 1.0e-9]].
{rejected. near. unitOK. orthogonalOK}

Expected: #(true true true true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Reject dependent hints and validate a tilted basis
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Both bad inputs are rejected, and the three non-axis-aligned basis vectors have unit length and are pairwise orthogonal.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Build orthonormal camera axes with modified Gram–Schmidt native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

Order matters: the forward axis is preserved first and the later hints are corrected around it. A reversed right hint may produce a reflected frame. For a right-handed camera basis, check the orientation of right, up and backward, and deliberately correct a sign if your convention requires it.

The threshold is a demonstration policy, not a general rank test. Very large or tiny numbers may overflow or underflow in a naive norm. Modified Gram–Schmidt can still lose orthogonality on ill-conditioned data; robust QR methods or reorthogonalization may be needed.

Remove uses q·v only because q is already a unit vector. Subtracting q times that coefficient when q is not unit length generally fails to remove the projection.

Three practice challenges

Identify a redundant axis

After forward and up have been accepted, use #(0 1 -1) as the right hint. Predict the result.

Show solution

The right hint lies in the span of forward and up. Removing both projections leaves zero, so the basis block rejects the hint.

Watch the order matter

Swap the forward and up hints. Which direction is preserved exactly apart from normalization?

Show solution

The original up hint now comes first, so its direction is preserved; the original forward hint is corrected relative to it.

Check a reflected basis

Negate the right hint in the camera fixture and inspect the pairwise dot table. What does it miss?

Show solution

The dot table is still the identity, but the right axis reverses. Unit length and orthogonality do not decide handedness.

Reference

MIT OpenCourseWare: Gram–Schmidt orthogonalization

Continue learning

Affine Transforms | Gaussian Elimination

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.