The rotating polyhedra demo by Gerard Sookahet (2005), on the left as it was, and on the right with its maths
written in the Calc : language and compiled by Calc::transformProcs. The best examples are shown proc by proc,
with the changed lines shaded, followed by the code that compiles them. The calc+calcvec button switches the right side to the same procs written with the calcvec vector operators (⊙ dot, × cross, ⊠ matrix product, :vadd: and :vsub: vector sum and difference); the original on the left stays the same. At the bottom, MatrixRotation is also compared with the TIP 759 syntax version.
Blue tint marks Calc code: the : command, its braced block, and inline [: ...] calls.
Everything else is plain Tcl. Red shading is a changed line on the left, green its replacement on the right, hatching means no counterpart.
foreach becomes the tuple (x, y) = vtx, and the sums are one Calc block.S, added to with S :vadd: vtx, instead of the two sums X and Y and the tuple (x, y) = vtx to take each vertex apart. The division by n is still done per coordinate, since there is no operator for a vector divided by a number.expr calls become one list(...).× operator, written where the vectors are, so the three expressions and the proc call are gone. See NormalVector. The empty proc is kept only so the comparison lines up.⊠, and a list of vectors is a matrix, so lnv ⊠ M turns all the normals at once. See Transformations.k loop is unrolled into one expression per cell, so there is a single lset per cell. The i and j loops stay, since Calc has no loops.⊠ operator, which does the loops in compiled code, so the 4x4 version with its two loops and sixteen unrolled cells is gone. See MatrixRotation.Mx ⊠ My ⊠ Mz replaces the two MatrixProduct() calls. The Calc version is the one compared in the TIP 759 section at the bottom.CrossProduct() and DotProduct() are called from Calc.p0, p1, p2, with no x, y and z names. The edges are p1 :vsub: p0 and p2 :vsub: p1, the cross product is × instead of a CrossProduct() call with six arguments, and the squared length is n ⊙ n instead of DotProduct() with six. The normal stays one list, so it needs no (nx, ny, nz) to build it again.p and only does the perspective division. The matrix multiplication moved out, so the matrix and its nine names are gone.[: ...] calls feed the lappend lines, and the result is one tuple, [: (M, lnew, lvn)]. The counter i was never used, so it is gone.lp ⊠ M rotates every point in one operation and ld ⊠ M every normal vector, so the foreach over the normals is gone. The matrices are 4x4, so each point first gets a fourth number w = 1 and each normal w = 0 (the two lmap lines), and the normals are cut back to three numbers afterwards. That is more lines than with 3x3 matrices, and the cost of the general form: a translation could now be added as one more matrix in the product.light ⊙ light and nv ⊙ light replace the DotProduct() calls, so the three-name tuples for the light and the normals are gone.$ on $ax, $ay and so on, but if they are there it simply ignores them, for compatibility. The text on the left is an example, shown exactly as posted in the forum discussion of TIP 672, written in the syntax of TIP 759 to show something TIP 672 cannot do.Calc::transformProcs call listing each by name. Then : and = are removed, so any Calc code that was missed fails with an error naming the proc and line instead of silently using the slow path. If all the procs were in a namespace, one wild-card call such as Calc::transformProcs ::poly::* could compile them all, since a proc with no Calc code in it is left unchanged. Calls between those procs would keep their plain names, since Calc looks a function up in the namespace of the proc it is compiling, then in the global namespace.CrossProduct, DotProduct, MatrixVectorProduct and MatrixProduct are now empty procs with no Calc code, which Calc::transformProcs would leave alone anyway. The operators come from package require calcvec near the top of the file, which is not shown here, and are defined before this call, so the inline code is compiled into the procs.