TIP: | 420 |

Title: | 'vexpr', a Vector Expression Command |

Version: | $Revision: 1.6 $ |

Authors: |
Sean Woods <yoda at etoyoc dot com> Andreas Kupries <andreask at activestate dot com> |

State: | Draft |

Type: | Project |

Tcl-Version: | 8.7 |

Vote: | Pending |

Created: | Thursday, 15 November 2012 |

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.

With the interest expressed in the community by TIP #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.

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 \ $A_pos $B_pos - \ affine_translate \ $A_rot radians \ affine_rotate \ 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 \ $C_pos $B_real - \ affine_translate \ load affine_multiply \ $B_rot radians \ affine_rotate \ 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.

Note that all arguments that are not one of these operation words are instead treated as values to push onto the evaluation stack.

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.

VECTOR -> AFFINE

Converts a "vector" of 3 angles (Xrotation Yrotation Zrotation) into an affine transformation. NOTE: the angles should be in radians.

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.

VECTOR -> AFFINE

Converts a displacement vector (X Y Z) into an affine transformation

VECTOR -> VECTOR

Converts a cartesian vector to cylindrical coordinates

VECTOR -> VECTOR

Converts a cartesian vector to spherical coordinates

VECTOR VECTOR -> VECTOR

Performs the cross product of two vectors

ANY -> ANY ANY

Copies the top of the stack, pushing it onto the stack.

VECTOR -> VECTOR

Converts a vector in cylindrical coordinates to cartesian coordinates

VECTOR -> VECTOR

Converts a cylindrical vector in radians to degrees.

VECTOR -> VECTOR

Converts a cylindrical vector in degrees to radians.

VECTOR -> VECTOR

Converts a vector or scalar in radians to degrees.

VECTOR VECTOR -> SCALAR

Produces the dot product of two vectors.

(None) -> SCALAR

Pushes the value of dT into the stack.

(None) -> AFFINE

Pushes the identity matrix onto the stack.

(None) -> ANY

Pushes the last value stored by STORE onto the stack.

(None) -> SCALER

Pushes the value of PI onto the stack.

VECTOR -> VECTOR

Converts a vector or scalar in degrees to radians.

SCALAR -> (None)

Pops the current stack value and stores it in the dT variable.

VECTOR -> VECTOR

Converts a vector in spherical coordinates to cartesian coordinates.

VECTOR -> VECTOR

Converts a spherical vector in radians to a spherical vector in degrees.

VECTOR -> VECTOR

Converts a spherical vector in degrees to a spherical vector in radians.

ANY -> ANY

Stores the top of the stack internally for later use. The value stored remains at the top of the stack.

VECTOR VECTOR -> VECTOR

Adds 2 vectors, which must be of the same length.

VECTOR -> SCALAR

Produces the length of a vector.

SCALAR VECTOR -> VECTOR

Scales a vector by a scalar

VECTOR VECTOR -> VECTOR

Subtracts one vector from another.

AFFINE VECTOR -> VECTOR

Transforms a vector using an affine matrix.

A test implementation for **vexpr** is available as an TEA extension, and can be downloaded [1]. At this point in time, the goal is adding **vexpr** as a standalone command.

**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.

This document has been placed in the public domain.

[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format *(experimental)*] [RTF Format *(experimental)*]