polyhedra.tcl, before and after Calc

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.

  • 10 procs shown, the ones with the most interesting Calc changes

Colour key

proc foreach if keyword set lappend command Barycenter proc name $vtx variable { } braces [ ] brackets ( , ) = + * in Calc 0.02 number "text" string # comment : x = y * 2 Calc code

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.

Original
With CalcWith Calc + calcvec

Barycenterchanged−8 +3

Barycenterchanged−8 +3

The inner foreach becomes the tuple (x, y) = vtx, and the sums are one Calc block.
The running sum is one vector 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.
15proc Barycenter {lcoords} {
16 set X 0
17 set Y 0
18 set n [llength $lcoords]
19
20 foreach vtx $lcoords {
21 foreach {x y} $vtx {
22 set X [expr {$X + $x}]
23 set Y [expr {$Y + $y}]
24 }
25 }
26 return [list [expr {$X/$n}] [expr {$Y/$n}]]
27}
18proc Barycenter {lcoords} {
19 : { X = 0; Y = 0; n = llength(lcoords) }
20 
21 foreach vtx $lcoords {
22 : { (x, y) = vtx; X = X + x; Y = Y + y }
23 }
24 : list(X/n, Y/n)
25}
15proc Barycenter {lcoords} {
16 set X 0
17 set Y 0
18 set n [llength $lcoords]
19
20 foreach vtx $lcoords {
21 foreach {x y} $vtx {
22 set X [expr {$X + $x}]
23 set Y [expr {$Y + $y}]
24 }
25 }
26 return [list [expr {$X/$n}] [expr {$Y/$n}]]
27}
19proc Barycenter {lcoords} {
20 : { S = (0, 0); n = llength(lcoords) }
21 
22 foreach vtx $lcoords {
23 : S = S :vadd: vtx
24 }
25 : { (X, Y) = S; list(X/n, Y/n) }
26}

CrossProductchanged−3 +5

CrossProductemptied−3 +1

Three expr calls become one list(...).
Emptied. The cross product is now the × 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.
29proc CrossProduct {x1 y1 z1 x2 y2 z2} {
30 return [list [expr {$y1*$z2 - $y2*$z1}] \
31 [expr {$z1*$x2 - $z2*$x1}] \
32 [expr {$x1*$y2 - $x2*$y1}]]
33}
27proc CrossProduct {x1 y1 z1 x2 y2 z2} {
28 : {
29 list(y1*z2 - y2*z1,
30 z1*x2 - z2*x1,
31 x1*y2 - x2*y1)
32 }
33}
29proc CrossProduct {x1 y1 z1 x2 y2 z2} {
30 return [list [expr {$y1*$z2 - $y2*$z1}] \
31 [expr {$z1*$x2 - $z2*$x1}] \
32 [expr {$x1*$y2 - $x2*$y1}]]
33}
28proc CrossProduct {x1 y1 z1 x2 y2 z2} {
29 # no longer needed, vector ops used instead
30}

MatrixVectorProductchanged−6 +7

MatrixVectorProductemptied−6 +1

Tuples unpack the vector and the three rows of the matrix.
Emptied. A vector times a matrix is the matrix product ⊠, and a list of vectors is a matrix, so lnv ⊠ M turns all the normals at once. See Transformations.
39proc MatrixVectorProduct {M V} {
40 set x [lindex $V 0]
41 set y [lindex $V 1]
42 set z [lindex $V 2]
43 return [list [expr {[lindex $M 0 0]*$x+[lindex $M 1 0]*$y+[lindex $M 2 0]*$z}] \
44 [expr {[lindex $M 0 1]*$x+[lindex $M 1 1]*$y+[lindex $M 2 1]*$z}] \
45 [expr {[lindex $M 0 2]*$x+[lindex $M 1 2]*$y+[lindex $M 2 2]*$z}]]
46}
39proc MatrixVectorProduct {M V} {
40 : {
41 (x, y, z) = V;
42 ((m00,m01,m02), (m10,m11,m12), (m20,m21,m22)) = M;
43 list(m00*x + m10*y + m20*z,
44 m01*x + m11*y + m21*z,
45 m02*x + m12*y + m22*z)
46 }
47}
39proc MatrixVectorProduct {M V} {
40 set x [lindex $V 0]
41 set y [lindex $V 1]
42 set z [lindex $V 2]
43 return [list [expr {[lindex $M 0 0]*$x+[lindex $M 1 0]*$y+[lindex $M 2 0]*$z}] \
44 [expr {[lindex $M 0 1]*$x+[lindex $M 1 1]*$y+[lindex $M 2 1]*$z}] \
45 [expr {[lindex $M 0 2]*$x+[lindex $M 1 2]*$y+[lindex $M 2 2]*$z}]]
46}
36proc MatrixVectorProduct {M V} {
37 # no longer needed, vector ops used instead
38}

MatrixProductchanged−5 +5

MatrixProductemptied−11 +1

The matrix starts as a list of lists in one Calc block, and the inner 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.
Emptied. The matrix product is the ⊠ operator, which does the loops in compiled code, so the 4x4 version with its two loops and sixteen unrolled cells is gone. See MatrixRotation.
48proc MatrixProduct {M1 M2} {
49 set M {{0 0 0 0} {0 0 0 0} {0 0 0 0} {0 0 0 0}}
50
51 for {set i 0} {$i<4} {incr i} {
52 for {set j 0} {$j<4} {incr j} {
53 lset M $i $j 0
54 for {set k 0} {$k<4} {incr k} {
55 lset M $i $j [expr {[lindex $M $i $j]+[lindex $M1 $i $k]*[lindex $M2 $k $j]}]
56 }
57 }
58 }
59 return $M
60}
49proc MatrixProduct {M1 M2} {
50 : { L = (0, 0, 0, 0) ; M = (L, L, L, L) }
51 
52 for {set i 0} {$i<4} {incr i} {
53 for {set j 0} {$j<4} {incr j} {
54 lset M $i $j [: lindex(M1, i, 0) * lindex(M2, 0, j) + \
55 lindex(M1, i, 1) * lindex(M2, 1, j) + \
56 lindex(M1, i, 2) * lindex(M2, 2, j) + \
57 lindex(M1, i, 3) * lindex(M2, 3, j)]
58 }
59 }
60 return $M
61}
48proc MatrixProduct {M1 M2} {
49 set M {{0 0 0 0} {0 0 0 0} {0 0 0 0} {0 0 0 0}}
50
51 for {set i 0} {$i<4} {incr i} {
52 for {set j 0} {$j<4} {incr j} {
53 lset M $i $j 0
54 for {set k 0} {$k<4} {incr k} {
55 lset M $i $j [expr {[lindex $M $i $j]+[lindex $M1 $i $k]*[lindex $M2 $k $j]}]
56 }
57 }
58 }
59 return $M
60}
40proc MatrixProduct {M1 M2} {
41 # no longer needed, vector ops used instead
42}

MatrixRotationchanged−26 +20

MatrixRotationchanged−26 +20

Each matrix is a nested list and the product is one call. See also the TIP 759 syntax version at the bottom.
The same 4x4 matrices as the Calc version; only the last line changes. Mx ⊠ My ⊠ Mz replaces the two MatrixProduct() calls. The Calc version is the one compared in the TIP 759 section at the bottom.
62proc MatrixRotationxxx { ax ay az } {
63 set sax [expr {sin($ax)}]
64 set cax [expr {cos($ax)}]
65 set say [expr {sin($ay)}]
66 set cay [expr {cos($ay)}]
67 set saz [expr {sin($az)}]
68 set caz [expr {cos($az)}]
69 set Mx {{1 0 0 0} {0 0 0 0} {0 0 0 0} {0 0 0 1}}
70 set My {{0 0 0 0} {0 1 0 0} {0 0 0 0} {0 0 0 1}}
71 set Mz {{0 0 0 0} {0 0 0 0} {0 0 1 0} {0 0 0 1}}
72
73 # Rotation matrix around X axis with angle ax
74 lset Mx 1 1 $cax
75 lset Mx 1 2 $sax
76 lset Mx 2 1 [expr {-1*$sax}]
77 lset Mx 2 2 $cax
78 # Rotation matrix around Y axis with angle ay
79 lset My 0 0 $cay
80 lset My 0 2 [expr {-1*$say}]
81 lset My 2 0 $say
82 lset My 2 2 $cay
83 # Rotation matrix around Z axis with angle az
84 lset Mz 0 0 $caz
85 lset Mz 0 1 $saz
86 lset Mz 1 0 [expr {-1*$saz}]
87 lset Mz 1 1 $caz
88 return [MatrixProduct [MatrixProduct $Mx $My] $Mz]
89}
63proc MatrixRotation { ax ay az } {
64 : {
65 # Rotation matrix around X axis with angle ax
66 Mx = ((1, 0 , 0 , 0),
67 (0, cos( ax) , sin( ax), 0),
68 (0, -sin( ax), cos( ax), 0),
69 (0, 0 , 0 , 1));
70 # Rotation matrix around Y axis with angle ay
71 My = ((cos( ay), 0, -sin( ay), 0),
72 (0 , 1, 0 , 0),
73 (sin( ay), 0, cos( ay) , 0),
74 (0 , 0, 0 , 1));
75 # Rotation matrix around Z axis with angle az
76 Mz = ((cos( az) , sin( az), 0, 0),
77 (-sin( az), cos( az), 0, 0),
78 (0 , 0 , 1, 0),
79 (0 , 0 , 0, 1));
80
81 
82 MatrixProduct( MatrixProduct( Mx, My), Mz)
83 }
84}
62proc MatrixRotationxxx { ax ay az } {
63 set sax [expr {sin($ax)}]
64 set cax [expr {cos($ax)}]
65 set say [expr {sin($ay)}]
66 set cay [expr {cos($ay)}]
67 set saz [expr {sin($az)}]
68 set caz [expr {cos($az)}]
69 set Mx {{1 0 0 0} {0 0 0 0} {0 0 0 0} {0 0 0 1}}
70 set My {{0 0 0 0} {0 1 0 0} {0 0 0 0} {0 0 0 1}}
71 set Mz {{0 0 0 0} {0 0 0 0} {0 0 1 0} {0 0 0 1}}
72
73 # Rotation matrix around X axis with angle ax
74 lset Mx 1 1 $cax
75 lset Mx 1 2 $sax
76 lset Mx 2 1 [expr {-1*$sax}]
77 lset Mx 2 2 $cax
78 # Rotation matrix around Y axis with angle ay
79 lset My 0 0 $cay
80 lset My 0 2 [expr {-1*$say}]
81 lset My 2 0 $say
82 lset My 2 2 $cay
83 # Rotation matrix around Z axis with angle az
84 lset Mz 0 0 $caz
85 lset Mz 0 1 $saz
86 lset Mz 1 0 [expr {-1*$saz}]
87 lset Mz 1 1 $caz
88 return [MatrixProduct [MatrixProduct $Mx $My] $Mz]
89}
44proc MatrixRotation { ax ay az } {
45 : {
46 # Rotation matrix around X axis with angle ax
47 Mx = ((1, 0 , 0 , 0),
48 (0, cos( ax) , sin( ax), 0),
49 (0, -sin( ax), cos( ax), 0),
50 (0, 0 , 0 , 1));
51 # Rotation matrix around Y axis with angle ay
52 My = ((cos( ay), 0, -sin( ay), 0),
53 (0 , 1, 0 , 0),
54 (sin( ay), 0, cos( ay) , 0),
55 (0 , 0, 0 , 1));
56 # Rotation matrix around Z axis with angle az
57 Mz = ((cos( az) , sin( az), 0, 0),
58 (-sin( az), cos( az), 0, 0),
59 (0 , 0 , 1, 0),
60 (0 , 0 , 0, 1));
61
62 
63 Mx ⊠ My ⊠ Mz
64 }
65}

NormalVectorchanged−9 +9

NormalVectorchanged−9 +9

Tuples unpack the face and its three vertices; CrossProduct() and DotProduct() are called from Calc.
The three vertices stay whole lists 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.
137# Compute normal vector and norm for each face
138# -------------------------------------------------------------------
139proc NormalVector {lvtx lcnx} {
140 set lnv {}
141 set lmv {}
142
143 foreach face $lcnx {
144 foreach {nx ny nz} [CrossProduct \
145 [expr {[lindex $lvtx [lindex $face 1] 0] - [lindex $lvtx [lindex $face 0] 0]}] \
146 [expr {[lindex $lvtx [lindex $face 1] 1] - [lindex $lvtx [lindex $face 0] 1]}] \
147 [expr {[lindex $lvtx [lindex $face 1] 2] - [lindex $lvtx [lindex $face 0] 2]}] \
148 [expr {[lindex $lvtx [lindex $face 2] 0] - [lindex $lvtx [lindex $face 1] 0]}] \
149 [expr {[lindex $lvtx [lindex $face 2] 1] - [lindex $lvtx [lindex $face 1] 1]}] \
150 [expr {[lindex $lvtx [lindex $face 2] 2] - [lindex $lvtx [lindex $face 1] 2]}]] {}
151 lappend lnv [list $nx $ny $nz]
152 lappend lmv [DotProduct $nx $ny $nz $nx $ny $nz]
153 }
154 return [list $lnv $lmv]
155}
86# Compute normal vector and norm for each face
87# -------------------------------------------------------------------
88proc NormalVector {lvtx lcnx} {
89 set lnv {}
90 set lmv {}
91
92 foreach face $lcnx {
93 : {
94 (i0, i1, i2) = face;
95 (x0, y0, z0) = lindex(lvtx, i0);
96 (x1, y1, z1) = lindex(lvtx, i1);
97 (x2, y2, z2) = lindex(lvtx, i2);
98 (nx, ny, nz) = CrossProduct(x1-x0, y1-y0, z1-z0, x2-x1, y2-y1, z2-z1)
99 }
100 lappend lnv [: (nx, ny, nz)]
101 lappend lmv [: DotProduct(nx, ny, nz, nx, ny, nz)]
102 }
103 return [list $lnv $lmv]
104}
137# Compute normal vector and norm for each face
138# -------------------------------------------------------------------
139proc NormalVector {lvtx lcnx} {
140 set lnv {}
141 set lmv {}
142
143 foreach face $lcnx {
144 foreach {nx ny nz} [CrossProduct \
145 [expr {[lindex $lvtx [lindex $face 1] 0] - [lindex $lvtx [lindex $face 0] 0]}] \
146 [expr {[lindex $lvtx [lindex $face 1] 1] - [lindex $lvtx [lindex $face 0] 1]}] \
147 [expr {[lindex $lvtx [lindex $face 1] 2] - [lindex $lvtx [lindex $face 0] 2]}] \
148 [expr {[lindex $lvtx [lindex $face 2] 0] - [lindex $lvtx [lindex $face 1] 0]}] \
149 [expr {[lindex $lvtx [lindex $face 2] 1] - [lindex $lvtx [lindex $face 1] 1]}] \
150 [expr {[lindex $lvtx [lindex $face 2] 2] - [lindex $lvtx [lindex $face 1] 2]}]] {}
151 lappend lnv [list $nx $ny $nz]
152 lappend lmv [DotProduct $nx $ny $nz $nx $ny $nz]
153 }
154 return [list $lnv $lmv]
155}
67# Compute normal vector and norm for each face
68# -------------------------------------------------------------------
69proc NormalVector {lvtx lcnx} {
70 set lnv {}
71 set lmv {}
72 
73 foreach face $lcnx {
74 : {
75 (i0, i1, i2) = face;
76 p0 = lindex(lvtx, i0);
77 p1 = lindex(lvtx, i1);
78 p2 = lindex(lvtx, i2);
79 n = (p1 :vsub: p0) × (p2 :vsub: p1)
80 }
81 lappend lnv $n
82 lappend lmv [: n ⊙ n]
83 }
84 return [list $lnv $lmv]
85}

Projectionchanged−4 +7

Projectionchanged−5 +6

The matrix is unpacked with a tuple and the whole projection is one Calc block.
The proc takes one rotated point p and only does the perspective division. The matrix multiplication moved out, so the matrix and its nine names are gone.
157# 2D projection
158# -------------------------------------------------------------------
159proc Projection {x y z M} {
160 global scx scy vdist
161
162 set nx [expr {[lindex $M 0 0]*$x+[lindex $M 1 0]*$y+[lindex $M 2 0]*$z}]
163 set ny [expr {[lindex $M 0 1]*$x+[lindex $M 1 1]*$y+[lindex $M 2 1]*$z}]
164 set nz [expr {([lindex $M 0 2]*$x+[lindex $M 1 2]*$y+[lindex $M 2 2]*$z+10)/$vdist}]
165 return [list [expr {$nx/$nz+$scx/2.0}] [expr {$ny/$nz+$scy/2.0}]]
166}
106# 2D projection
107# -------------------------------------------------------------------
108proc Projection {x y z M} {
109 global scx scy vdist
110 
111 : {
112 ((m00,m01,m02), (m10,m11,m12), (m20,m21,m22)) = M;
113 nx = m00*x + m10*y + m20*z;
114 ny = m01*x + m11*y + m21*z;
115 nz = (m02*x + m12*y + m22*z + 10)/vdist;
116 list(nx/nz + scx/2.0, ny/nz + scy/2.0)
117 }
118}
157# 2D projection
158# -------------------------------------------------------------------
159proc Projection {x y z M} {
160 global scx scy vdist
161
162 set nx [expr {[lindex $M 0 0]*$x+[lindex $M 1 0]*$y+[lindex $M 2 0]*$z}]
163 set ny [expr {[lindex $M 0 1]*$x+[lindex $M 1 1]*$y+[lindex $M 2 1]*$z}]
164 set nz [expr {([lindex $M 0 2]*$x+[lindex $M 1 2]*$y+[lindex $M 2 2]*$z+10)/$vdist}]
165 return [list [expr {$nx/$nz+$scx/2.0}] [expr {$ny/$nz+$scy/2.0}]]
166}
87# 2D projection
88# -------------------------------------------------------------------
89proc Projection {p} {
90 global scx scy vdist
91 
92 : {
93 (nx, ny, nz) = p;
94 z = (nz + 10)/vdist;
95 list(nx/z + scx/2.0, ny/z + scy/2.0)
96 }
97}

Transformationschanged−6 +5

Transformationschanged−11 +9

Inline [: ...] 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.
168# Apply transformations to vertex coordinates
169# -------------------------------------------------------------------
170proc Transformations {lvtx lnv} {
171 global ax ay az
172 set lnew {}
173 set lvn {}
174 # Compute matrix rotation
175 set M [MatrixRotation $ax $ay $az]
176 set i 0
177 # Apply projection
178 foreach vtx $lvtx {
179 lappend lnew [Projection [lindex $vtx 0] [lindex $vtx 1] [lindex $vtx 2] $M]
180 incr i
181 }
182 # Normal vector rotation
183 foreach v $lnv {lappend lvn [MatrixVectorProduct $M $v]}
184
185 return [list $M $lnew $lvn]
186}
120# Apply transformations to vertex coordinates
121# -------------------------------------------------------------------
122proc Transformations {lvtx lnv} {
123 global ax ay az
124 set lnew {}
125 set lvn {}
126 # Compute matrix rotation
127 : M = MatrixRotation(ax, ay, az)
128 # Apply projection
129 foreach vtx $lvtx {
130 : (x, y, z) = vtx
131 lappend lnew [: Projection(x, y, z, M)]
132 }
133 # Normal vector rotation
134 foreach v $lnv {lappend lvn [: MatrixVectorProduct(M, v)]}
135
136 return [: (M, lnew, lvn)]
137}
168# Apply transformations to vertex coordinates
169# -------------------------------------------------------------------
170proc Transformations {lvtx lnv} {
171 global ax ay az
172 set lnew {}
173 set lvn {}
174 # Compute matrix rotation
175 set M [MatrixRotation $ax $ay $az]
176 set i 0
177 # Apply projection
178 foreach vtx $lvtx {
179 lappend lnew [Projection [lindex $vtx 0] [lindex $vtx 1] [lindex $vtx 2] $M]
180 incr i
181 }
182 # Normal vector rotation
183 foreach v $lnv {lappend lvn [MatrixVectorProduct $M $v]}
184
185 return [list $M $lnew $lvn]
186}
99# Apply transformations to vertex coordinates
100# -------------------------------------------------------------------
101proc Transformations {lvtx lnv} {
102 global ax ay az
103 set lnew {}
104 # The matrices are 4x4, so a point gets a fourth number w = 1 and a normal vector w = 0
105 set lp [lmap v $lvtx {list {*}$v 1}]
106 set ld [lmap v $lnv {list {*}$v 0}]
107 # Rotate all the points and all the normal vectors with one matrix product each
108 : { M = MatrixRotation(ax, ay, az); lrot = lp ⊠ M; ldir = ld ⊠ M }
109 # Apply projection
110 foreach p $lrot {lappend lnew [Projection $p]}
111 # Back to three numbers per normal vector
112 set lvn [lmap v $ldir {lrange $v 0 2}]
113 
114 return [: (M, lnew, lvn)]
115}

Intensitychanged−8 +8

Intensitychanged−9 +8

Tuples for the light and normal vectors; the shading maths is one Calc block.
light ⊙ light and nv ⊙ light replace the DotProduct() calls, so the three-name tuples for the light and the normals are gone.
188# Compute color entensity for each face
189# -------------------------------------------------------------------
190proc Intensity {lnv lmv lvv} {
191 set lclr {}
192 set v [DotProduct [lindex $lvv 0] [lindex $lvv 1] [lindex $lvv 2] \
193 [lindex $lvv 0] [lindex $lvv 1] [lindex $lvv 2]]
194 set i 0
195 foreach nv $lnv {
196 set clr 31
197 set a [DotProduct [lindex $nv 0] [lindex $nv 1] [lindex $nv 2] \
198 [lindex $lvv 0] [lindex $lvv 1] [lindex $lvv 2]]
199 set b [expr {sqrt([lindex $lmv $i]*$v)}]
200 set clr [expr {round(31*($a/$b))}]
201 lappend lclr [expr {$clr < 0 ? 31 : [expr {32 - $clr}]}]
202 incr i
203 }
204 return $lclr
205}
139# Compute color entensity for each face
140# -------------------------------------------------------------------
141proc Intensity {lnv lmv lvv} {
142 set lclr {}
143 : { (lx, ly, lz) = lvv; v = DotProduct(lx, ly, lz, lx, ly, lz) }
144 set i 0
145 foreach nv $lnv {
146 : {
147 (nx, ny, nz) = nv;
148 a = DotProduct(nx, ny, nz, lx, ly, lz);
149 b = sqrt(lindex(lmv, i) * v);
150 clr = round(31*(a/b))
151 }
152 lappend lclr [: clr < 0 ? 31 : 32 - clr]
153 incr i
154 }
155 return $lclr
156}
188# Compute color entensity for each face
189# -------------------------------------------------------------------
190proc Intensity {lnv lmv lvv} {
191 set lclr {}
192 set v [DotProduct [lindex $lvv 0] [lindex $lvv 1] [lindex $lvv 2] \
193 [lindex $lvv 0] [lindex $lvv 1] [lindex $lvv 2]]
194 set i 0
195 foreach nv $lnv {
196 set clr 31
197 set a [DotProduct [lindex $nv 0] [lindex $nv 1] [lindex $nv 2] \
198 [lindex $lvv 0] [lindex $lvv 1] [lindex $lvv 2]]
199 set b [expr {sqrt([lindex $lmv $i]*$v)}]
200 set clr [expr {round(31*($a/$b))}]
201 lappend lclr [expr {$clr < 0 ? 31 : [expr {32 - $clr}]}]
202 incr i
203 }
204 return $lclr
205}
117# Compute color entensity for each face
118# -------------------------------------------------------------------
119proc Intensity {lnv lmv light} {
120 set lclr {}
121 : v = light ⊙ light
122 set i 0
123 foreach nv $lnv {
124 : {
125 a = nv ⊙ light;
126 b = sqrt(lindex(lmv, i) * v);
127 clr = round(31*(a/b))
128 }
129 lappend lclr [: clr < 0 ? 31 : 32 - clr]
130 incr i
131 }
132 return $lclr
133}

DisplayModelchanged−8 +4

DisplayModelchanged−8 +4

The six variables become one tuple assignment. Also fixed from the original, which did not remember the current model; that is not a Calc change.
No change from the Calc version. The operators do not apply to this proc.
207# Start the display and rotation loop
208# -------------------------------------------------------------------
209proc DisplayModel {w s} {
210 global stop
211 global display
212 global ax ay az tx ty tz
213 global form
214
215 $w.c delete all
216 set stop 0
217 global iterations
218 set ax 0.2
219 set ay 0.1
220 set az 0.3
221 set tx 0
222 set ty 0
223 set tz 0
224
225 set d $display
226 foreach {t lvtx lcnx lclr} [ReadData $s] {}
227 $w.c create text 10 10 -anchor w -fill white -text $t
228 foreach {lnv lmv} [NormalVector $lvtx $lcnx] {}
229
230 set lpoly [DisplayInit $w $d $lcnx $lclr]
231 
232 after cancel $::run
233 set ::run [after 0 [list Display$d $w $lpoly $lvtx $lcnx $lnv $lmv]]
234}
158# Start the display and rotation loop
159# -------------------------------------------------------------------
160proc DisplayModel {w s} {
161 global stop
162 global display
163 global ax ay az tx ty tz
164 global form
165
166 $w.c delete all
167 set stop 0
168 global iterations
169 : (ax, ay, az, tx, ty, tz) = (0.2, 0.1, 0.3, 0, 0, 0)
170
171 : ::curmodel = s; d = display ;# curmodel is remembered so Shaded/Wireframe can restart it
172 foreach {t lvtx lcnx lclr} [ReadData $s] {}
173 $w.c create text 10 10 -anchor w -tags label -text $t \
174 -fill [expr {$::bggray < 128 ? "white" : "black"}]
175 foreach {lnv lmv} [NormalVector $lvtx $lcnx] {}
176
177 set lpoly [DisplayInit $w $d $lcnx $lclr]
178 
179 after cancel $::run
180 set ::run [after 0 [list Display$d $w $lpoly $lvtx $lcnx $lnv $lmv]]
181}
207# Start the display and rotation loop
208# -------------------------------------------------------------------
209proc DisplayModel {w s} {
210 global stop
211 global display
212 global ax ay az tx ty tz
213 global form
214
215 $w.c delete all
216 set stop 0
217 global iterations
218 set ax 0.2
219 set ay 0.1
220 set az 0.3
221 set tx 0
222 set ty 0
223 set tz 0
224
225 set d $display
226 foreach {t lvtx lcnx lclr} [ReadData $s] {}
227 $w.c create text 10 10 -anchor w -fill white -text $t
228 foreach {lnv lmv} [NormalVector $lvtx $lcnx] {}
229
230 set lpoly [DisplayInit $w $d $lcnx $lclr]
231 
232 after cancel $::run
233 set ::run [after 0 [list Display$d $w $lpoly $lvtx $lcnx $lnv $lmv]]
234}
135# Start the display and rotation loop
136# -------------------------------------------------------------------
137proc DisplayModel {w s} {
138 global stop
139 global display
140 global ax ay az tx ty tz
141 global form
142
143 $w.c delete all
144 set stop 0
145 global iterations
146 : (ax, ay, az, tx, ty, tz) = (0.2, 0.1, 0.3, 0, 0, 0)
147
148 : ::curmodel = s; d = display ;# curmodel is remembered so Shaded/Wireframe can restart it
149 foreach {t lvtx lcnx lclr} [ReadData $s] {}
150 $w.c create text 10 10 -anchor w -tags label -text $t \
151 -fill [expr {$::bggray < 128 ? "white" : "black"}]
152 foreach {lnv lmv} [NormalVector $lvtx $lcnx] {}
153
154 set lpoly [DisplayInit $w $d $lcnx $lclr]
155 
156 after cancel $::run
157 set ::run [after 0 [list Display$d $w $lpoly $lvtx $lcnx $lnv $lmv]]
158}

MatrixRotationTIP 759 syntax−9 +12

Calc can compile this code with only a little wrapping and a few changes, such as function calls instead of command substitutions. Calc does not need the $ 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.
1proc MatrixRotation { ax ay az } {(
2 # Rotation matrix around X axis with angle ax
3 Mx = ((1, 0 , 0 , 0),
4 (0, cos($ax) , sin($ax), 0),
5 (0, -sin($ax), cos($ax), 0),
6 (0, 0 , 0 , 1));
7 # Rotation matrix around Y axis with angle ay
8 My = ((cos($ay), 0, -sin($ay), 0),
9 (0 , 1, 0 , 0),
10 (sin($ay), 0, cos($ay) , 0),
11 (0 , 0, 0 , 1));
12 # Rotation matrix around Z axis with angle az
13 Mz = ((cos($az) , sin($az), 0, 0),
14 (-sin($az), cos($az), 0, 0),
15 (0 , 0 , 1, 0),
16 (0 , 0 , 0, 1));
17
18 [MatrixProduct [MatrixProduct $Mx $My] $Mz]
19)}
63proc MatrixRotation { ax ay az } {
64 : {
65 # Rotation matrix around X axis with angle ax
66 Mx = ((1, 0 , 0 , 0),
67 (0, cos( ax) , sin( ax), 0),
68 (0, -sin( ax), cos( ax), 0),
69 (0, 0 , 0 , 1));
70 # Rotation matrix around Y axis with angle ay
71 My = ((cos( ay), 0, -sin( ay), 0),
72 (0 , 1, 0 , 0),
73 (sin( ay), 0, cos( ay) , 0),
74 (0 , 0, 0 , 1));
75 # Rotation matrix around Z axis with angle az
76 Mz = ((cos( az) , sin( az), 0, 0),
77 (-sin( az), cos( az), 0, 0),
78 (0 , 0 , 1, 0),
79 (0 , 0 , 0, 1));
80
81 
82 MatrixProduct( MatrixProduct( Mx, My), Mz)
83 }
84}

End of the filechanged−0 +19

End of the filechanged−0 +18

The new procs are compiled here, once, by one 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.
Same compile step as the Calc version. The list is shorter, because 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.
468Main
493# Compile the Calc code (the : commands) in these procs, once, now that they
494# are all defined and just before Main runs. This is the same result as
495# defining each of them with proc=
496# -------------------------------------------------------------------
497Calc::transformProcs \
498 Barycenter CrossProduct DotProduct \
499 MatrixVectorProduct MatrixProduct MatrixRotation \
500 NormalVector Projection Transformations Intensity \
501 DisplayModel ReadData BlueColor \
502 DisplayShaded DisplayWireframe
503 
504# Everything that uses Calc is compiled now, so remove the interpreted : and =
505# commands. A : or = left in any code that was not compiled (a proc missing from
506# the list above, say) now fails with 'invalid command name' and a traceback
507# showing which proc and line, instead of quietly running the slow path and
508# filling Calc::cache.
509rename : {}
510rename = {}
511 
512Main
468Main
466# Compile the Calc code (the : commands) in these procs, once, now that they
467# are all defined and just before Main runs. This is the same result as
468# defining each of them with proc=
469# -------------------------------------------------------------------
470Calc::transformProcs \
471 Barycenter MatrixRotation \
472 NormalVector Projection Transformations Intensity \
473 DisplayModel ReadData BlueColor \
474 DisplayShaded DisplayWireframe
475 
476# Everything that uses Calc is compiled now, so remove the interpreted : and =
477# commands. A : or = left in any code that was not compiled (a proc missing from
478# the list above, say) now fails with 'invalid command name' and a traceback
479# showing which proc and line, instead of quietly running the slow path and
480# filling Calc::cache.
481rename : {}
482rename = {}
483 
484Main