TIP #420: 'vexpr', a Vector Expression Command
_____________________________________________________________________________
TIP:	420
Title:	'vexpr', a Vector Expression Command
Version:	$Revision: 1.6 $
Authors:	Sean Woods <yoda@etoyoc.com>
	Andreas Kupries <andreask@activestate.com>
State:	Draft
Type:	Project
Tcl-Version:	8.7
Vote:	Pending
Created:	Thu Nov 15 00:00:00 UTC 2012
Abstract
This TIP proposes to add a new command to Tcl for manipulating vectors and related mathematical objects. The command, vexpr, will provide C-optimized implementations of generally useful scalar, 2D, 3D and affine transforms. vexpr is a complement to expr, and expects to take in vector arguments and return vector results.
Rationale
With the interest expressed in the community by [363], I am concerned about the introduction of non-scalar results from expr (and parts of the language the use expr). As the goal of that TIP is to introduce vector math operations, a less ambitious, but arguable equally effective technique could be to introduce a dedicated command. In particular, one designed from the ground up to handle the intricacies of vector operations.
vexpr is a vector expression parser. It operates using reverse-polish notation (like an HP calculator.) Each argument is pushed onto the stack, and when a command is detected, they are popped off the stack. The result of the command is pushed onto the stack in their place.
Why? Well mostly for ease of implementation. Partly because there is no PEMDAS equivalent order of operation for matrices and vectors. Once I go through an example or two, it should be a little clearer.
Examples
To add 1 1 1 and 2 2 2 I run the following command:
vexpr 2 2 2 1 1 1 +
> 3.0 3.0 3.0
Remember though, we are working with a stack. Items are popped on the stack in a first-in first-out fashion. While for addition it doesn't matter what order we do things, subtraction does care.
vexpr 1 1 1 2 2 2 -
> 1.0 1.0 1.0
vexpr 2 2 2 1 1 1 -
> -1.0 -1.0 -1.0
While with 2 arguments and an opcode this seems silly, imagine a complex operation with several steps. Here we are going to model a robot arm with 3 joints. Each "arm" is one unit long, and when one joint bends, the rest follow suit.
unbent
(A) - (B) - (C)
bent
      (C)
        |
      (B)
     /
(A)
Code:
# Positions of the joints
set A_pos 0.0 0.0 0.0
set B_pos 1.0 0.0 0.0
set C_pos 2.0 0.0 0.0

# Rotations of the joints 
set A_rot 0 0 45
set B_rot 0 0 45

set b_transform [vexpr \par    $A_pos $B_pos - \par    affine_translate \par    $A_rot radians \par    affine_rotate \par    affine_multiply]
>  0.707  0.707 0.0  -0.707 
  -0.707  0.707 0.0   0.707
   0.0    0.0   1.0   0.0
   0.0    0.0   0.0   1.0

set b_real [vexpr $B_pos $b_transform vector_transform]

> 0.707106 0.707106 0.0

set c_transform [vexpr \par    $C_pos $B_real - \par    affine_translate \par    load affine_multiply \par    $B_rot radians \par    affine_rotate \par    affine_multiply]
>  0.0 1.0 0.0 0.707
  -1.0 0.0 0.0 2.293
  0.0  0.0 1.0 0.0
  0.0  0.0 0.0 1.0

set c_real [vexpr $C_pos $c_transform vector_transform]
> 0.0 2.0 0.0
If you aren't familiar with 3D math and affine transformations, that may look overly complicated, but as you can see each vexpr call is packed with commands. You can plainly see that after 2 45 degree bends, our "C" point comes to rest at 0.0,2,0 after completing a 90 degree bend.
Operations
Note that all arguments that are not one of these operation words are instead treated as values to push onto the evaluation stack.
affine_multiply
AFFINE AFFINE -> AFFINE
Multiplies 2 4x4 matrices. Used to combine 2 affine transformations. Note: Some affine transformations need to be performed in a particular order to make sense.
affine_rotate
VECTOR -> AFFINE
Converts a "vector" of 3 angles (Xrotation Yrotation Zrotation) into an affine transformation. NOTE: the angles should be in radians.
affine_scale
VECTOR -> AFFINE
Converts a scale vector (Xscale Yscale Zscale) into an affine transformation. Note: 1.0 1.0 1.0 = No scaling. 2.0 2.0 2.0 = Double the size. 0.5 0.5 0.5 = Half the size.
affine_translate
VECTOR -> AFFINE
Converts a displacement vector (X Y Z) into an affine transformation 
cart_to_cyl
VECTOR -> VECTOR
Converts a cartesian vector to cylindrical coordinates 
cart_to_sphere
VECTOR -> VECTOR
Converts a cartesian vector to spherical coordinates
cross
VECTOR VECTOR -> VECTOR
Performs the cross product of two vectors
copy
ANY -> ANY ANY
Copies the top of the stack, pushing it onto the stack.
cyl_to_cart
VECTOR -> VECTOR
Converts a vector in cylindrical coordinates to cartesian coordinates 
cyl_to_degrees
VECTOR -> VECTOR
Converts a cylindrical vector in radians to degrees.
cyl_to_radians
VECTOR -> VECTOR
Converts a cylindrical vector in degrees to radians.
degrees
VECTOR -> VECTOR
Converts a vector or scalar in radians to degrees.
dot
VECTOR VECTOR -> SCALAR
Produces the dot product of two vectors.
dT
(None) -> SCALAR
Pushes the value of dT into the stack.
identity
(None) -> AFFINE
Pushes the identity matrix onto the stack.
load
(None) -> ANY
Pushes the last value stored by STORE onto the stack. 
pi
(None) -> SCALER
Pushes the value of PI onto the stack.
radians
VECTOR -> VECTOR
Converts a vector or scalar in degrees to radians.
setDT
SCALAR -> (None)
Pops the current stack value and stores it in the dT variable.
sphere_to_cart
VECTOR -> VECTOR
Converts a vector in spherical coordinates to cartesian coordinates.
sphere_to_degrees
VECTOR -> VECTOR
Converts a spherical vector in radians to a spherical vector in degrees.
sphere_to_radians
VECTOR -> VECTOR
Converts a spherical vector in degrees to a spherical vector in radians.
store
ANY -> ANY
Stores the top of the stack internally for later use. The value stored remains at the top of the stack.
vector_add
VECTOR VECTOR -> VECTOR
Adds 2 vectors, which must be of the same length.
vector_length
VECTOR -> SCALAR
Produces the length of a vector.
vector_scale
SCALAR VECTOR -> VECTOR
Scales a vector by a scalar
vector_subtract
VECTOR VECTOR -> VECTOR
Subtracts one vector from another.
vector_transform
AFFINE VECTOR -> VECTOR
Transforms a vector using an affine matrix.
Implementation
A test implementation for vexpr is available as an TEA extension, and can be downloaded [http://www.etoyoc.com/tclmatrix3d]. At this point in time, the goal is adding vexpr as a standalone command.
Limits
vexpr converts all arguments to an array of 16 double precision elements; only the item left on the top of the stack is converted back into a Tcl list. The "stack" itself has a hard-coded limit of 32 elements. (It is implemented as an array.) Exceeding the stack size will cause the command to throw a Tcl error.
Copyright
This document has been placed in the public domain.
_____________________________________________________________________________
Colophon
TIP AutoGenerator - written by Donal K. Fellows