TIP #420: 'VEXPR', A VECTOR EXPRESSION COMMAND ================================================ Version: $Revision: 1.6 $ Author: Sean Woods Andreas Kupries State: Draft Type: Project Tcl-Version: 8.7 Vote: Pending Created: Thursday, 15 November 2012 URL: https://tip.tcl-lang.org420.html Post-History: ------------------------------------------------------------------------- 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 [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. 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 \ $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. 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 []. 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. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows