vmath.h File Reference

vector/matrix math More...

#include "common.h"

Include dependency graph for vmath.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define VMATH_H   seen
#define M_E   2.7182818284590452354
#define M_LOG2E   1.4426950408889634074
#define M_LOG10E   0.43429448190325182765
#define M_LN2   0.69314718055994530942
#define M_LN10   2.30258509299404568402
#define M_PI   3.14159265358979323846
#define M_PI_2   1.57079632679489661923
#define M_PI_4   0.78539816339744830962
#define M_1_PI   0.31830988618379067154
#define M_2_PI   0.63661977236758134308
#define M_2_SQRTPI   1.12837916709551257390
#define M_SQRT2   1.41421356237309504880
#define M_SQRT1_2   0.70710678118654752440
#define M_SQRT2_DIV2   0.70710678118654752440
#define PI   M_PI
#define VDIVIDE_TOL   ( 1.0e-20 )
#define VUNITIZE_TOL   ( 1.0e-15 )
#define ELEMENTS_PER_VECT   3
 # of fastf_t's per vect_t
#define ELEMENTS_PER_PT   3
#define HVECT_LEN   4
 # of fastf_t's per hvect_t
#define HPT_LEN   4
#define ELEMENTS_PER_PLANE   4
 # of fastf_t's per plane_t
#define ELEMENTS_PER_MAT   (4*4)
#define quat_t   hvect_t
 4-element quaternion
#define NEAR_ZERO(val, epsilon)   ( ((val) > -epsilon) && ((val) < epsilon) )
#define CLAMP(_v, _l, _h)   if ((_v) < (_l)) _v = _l; else if ((_v) > (_h)) _v = _h
#define DIST_PT_PLANE(_pt, _pl)   (VDOT(_pt, _pl) - (_pl)[H])
 Compute distance from a point to a plane.
#define DIST_PT_PT(a, b)
 Compute distance between two points.
#define X   0
#define Y   1
#define Z   2
#define H   3
#define W   H
#define MDX   3
#define MDY   7
#define MDZ   11
#define MAT_DELTAS(m, x, y, z)
 set translation values of 4x4 matrix with x, y, z values
#define MAT_DELTAS_VEC(_m, _v)   MAT_DELTAS(_m, (_v)[X], (_v)[Y], (_v)[Z] )
 set translation values of 4x4 matrix from a vector
#define MAT_DELTAS_VEC_NEG(_m, _v)   MAT_DELTAS(_m,-(_v)[X],-(_v)[Y],-(_v)[Z] )
 set translation values of 4x4 matrix from a reversed vector
#define MAT_DELTAS_GET(_v, _m)
 get translation values of 4x4 matrix to a vector
#define MAT_DELTAS_GET_NEG(_v, _m)
 get translation values of 4x4 matrix to a vector, reversed
#define MSX   0
#define MSY   5
#define MSZ   10
#define MSA   15
#define MAT_SCALE(_m, _x, _y, _z)
 set scale of 4x4 matrix from xyz
#define MAT_SCALE_VEC(_m, _v)
 set scale of 4x4 matrix from vector
#define MAT_SCALE_ALL(_m, _s)   (_m)[MSA] = (_s)
 set uniform scale of 4x4 matrix from scalar
#define MAT_ZERO(m)
 zero a matrix
#define MAT_IDN(m)
 set matrix to identity
#define MAT_COPY(d, s)
 copy a matrix
#define VSET(a, b, c, d)
 Set vector at `a' to have coordinates `b', `c', `d'.
#define VSETALL(a, s)   { (a)[X] = (a)[Y] = (a)[Z] = (s); }
 Set all elements of vector to same scalar value.
#define VSETALLN(v, s, n)
 Set all elements of N-vector to same scalar value.
#define VMOVE(a, b)
 Transfer vector at `b' to vector at `a'.
#define VMOVEN(a, b, n)
 Transfer vector of length `n' at `b' to vector at `a'.
#define HMOVE(a, b)
 Move a homogeneous 4-tuple.
#define V2MOVE(a, b)
 move a 2D vector. This naming convention seems better than the VMOVE_2D version below
#define VREVERSE(a, b)
 Reverse the direction of vector b and store it in a.
#define HREVERSE(a, b)
 Same as VREVERSE, but for a 4-tuple. Also useful on plane_t objects.
#define VADD2(a, b, c)
 Add vectors at `b' and `c', store result at `a'.
#define VADD2N(a, b, c, n)
 Add vectors of length `n' at `b' and `c', store result at `a'.
#define V2ADD2(a, b, c)
#define VSUB2(a, b, c)
 Subtract vector at `c' from vector at `b', store result at `a'.
#define VSUB2N(a, b, c, n)
 Subtract `n' length vector at `c' from vector at `b', store result at `a'.
#define V2SUB2(a, b, c)
#define VSUB3(a, b, c, d)
 Vectors: A = B - C - D.
#define VSUB3N(a, b, c, d, n)
 Vectors: A = B - C - D for vectors of length `n'.
#define VADD3(a, b, c, d)
 Add 3 vectors at `b', `c', and `d', store result at `a'.
#define VADD3N(a, b, c, d, n)
 Add 3 vectors of length `n' at `b', `c', and `d', store result at `a'.
#define VADD4(a, b, c, d, e)
 Add 4 vectors at `b', `c', `d', and `e', store result at `a'.
#define VADD4N(a, b, c, d, e, n)
 Add 4 `n' length vectors at `b', `c', `d', and `e', store result at `a'.
#define VSCALE(a, b, c)
 Scale vector at `b' by scalar `c', store result at `a'.
#define HSCALE(a, b, c)
#define VSCALEN(a, b, c, n)
 Scale vector of length `n' at `b' by scalar `c', store result at `a'.
#define V2SCALE(a, b, c)
#define VUNITIZE(a)
 Normalize vector `a' to be a unit vector.
#define VUNITIZE_RET(a, ret)
 If vector magnitude is too small, return an error code.
#define VADD2SCALE(o, a, b, s)
 Find the sum of two points, and scale the result. Often used to find the midpoint.
#define VADD2SCALEN(o, a, b, n)
#define VSUB2SCALE(o, a, b, s)
 Find the difference between two points, and scale result. Often used to compute bounding sphere radius given rpp points.
#define VSUB2SCALEN(o, a, b, n)
#define VCOMB3(o, a, b, c, d, e, f)
 Combine together several vectors, scaled by a scalar.
#define VCOMB3N(o, a, b, c, d, e, f, n)
#define VCOMB2(o, a, b, c, d)
#define VCOMB2N(o, a, b, c, d, n)
#define VJOIN4(a, b, c, d, e, f, g, h, i, j)
#define VJOIN3(a, b, c, d, e, f, g, h)
#define VJOIN2(a, b, c, d, e, f)
 Compose vector at `a' of: Vector at `b' plus scalar `c' times vector at `d' plus scalar `e' times vector at `f'.
#define VJOIN2N(a, b, c, d, e, f, n)
#define VJOIN1(a, b, c, d)
#define VJOIN1N(a, b, c, d, n)
#define HJOIN1(a, b, c, d)
#define V2JOIN1(a, b, c, d)
#define VBLEND2(a, b, c, d, e)
 Blend into vector `a' scalar `b' times vector at `c' plus scalar `d' times vector at `e'.
#define VBLEND2N(a, b, c, d, e, n)
#define MAGSQ(a)   ( (a)[X]*(a)[X] + (a)[Y]*(a)[Y] + (a)[Z]*(a)[Z] )
 Return scalar magnitude squared of vector at `a'.
#define MAG2SQ(a)   ( (a)[X]*(a)[X] + (a)[Y]*(a)[Y] )
#define MAGNITUDE(a)   sqrt( MAGSQ( a ) )
 Return scalar magnitude of vector at `a'.
#define VCROSS(a, b, c)
 Store cross product of vectors at `b' and `c' in vector at `a'. Note that the "right hand rule" applies: If closing your right hand goes from `b' to `c', then your thumb points in the direction of the cross product.
#define VDOT(a, b)   ( (a)[X]*(b)[X] + (a)[Y]*(b)[Y] + (a)[Z]*(b)[Z] )
 Compute dot product of vectors at `a' and `b'.
#define V2DOT(a, b)   ( (a)[X]*(b)[X] + (a)[Y]*(b)[Y] )
#define VSUB2DOT(_pt2, _pt, _vec)
 Subtract two points to make a vector, dot with another vector.
#define V2ARGS(a)   (a)[X], (a)[Y]
 Turn a vector into comma-separated list of elements, for subroutine args.
#define V3ARGS(a)   (a)[X], (a)[Y], (a)[Z]
#define V4ARGS(a)   (a)[X], (a)[Y], (a)[Z], (a)[W]
#define V2INTCLAMPARGS(a)   INTCLAMP((a)[X]), INTCLAMP((a)[Y])
 integer clamped versions of the previous arg macros
#define V3INTCLAMPARGS(a)   INTCLAMP((a)[X]), INTCLAMP((a)[Y]), INTCLAMP((a)[Z])
 integer clamped versions of the previous arg macros
#define V4INTCLAMPARGS(a)   INTCLAMP((a)[X]), INTCLAMP((a)[Y]), INTCLAMP((a)[Z]), INTCLAMP((a)[W])
 integer clamped versions of the previous arg macros
#define V2PRINT(a, b)   (void)fprintf(stderr,"%s (%g, %g)\n", a, V2ARGS(b) );
 Print vector name and components on stderr.
#define VPRINT(a, b)   (void)fprintf(stderr,"%s (%g, %g, %g)\n", a, V3ARGS(b) );
#define HPRINT(a, b)   (void)fprintf(stderr,"%s (%g, %g, %g, %g)\n", a, V4ARGS(b) );
#define V2INTCLAMPPRINT(a, b)   (void)fprintf(stderr,"%s (%g, %g)\n", a, V2INTCLAMPARGS(b) );
 integer clamped versions of the previous print macros
#define VINTCLAMPPRINT(a, b)   (void)fprintf(stderr,"%s (%g, %g, %g)\n", a, V3INTCLAMPARGS(b) );
#define HINTCLAMPPRINT(a, b)   (void)fprintf(stderr,"%s (%g, %g, %g, %g)\n", a, V4INTCLAMPARGS(b) );
#define INTCLAMP(_a)   ( NEAR_ZERO((_a) - rint(_a), VUNITIZE_TOL) ? rint(_a) : (_a) )
#define VELMUL(a, b, c)
 Vector element multiplication. Really: diagonal matrix X vect.
#define VELMUL3(a, b, c, d)
#define VELDIV(a, b, c)
 Similar to VELMUL.
#define VINVDIR(_inv, _dir)
 Given a direction vector, compute the inverses of each element. When division by zero would have occured, mark inverse as INFINITY.
#define MAT3X3VEC(o, mat, vec)
 Apply the 3x3 part of a mat_t to a 3-tuple. This rotates a vector without scaling it (changing its length).
#define VEC3X3MAT(o, i, m)
 Multiply a 3-tuple by the 3x3 part of a mat_t.
#define MAT3X2VEC(o, mat, vec)
 Apply the 3x3 part of a mat_t to a 2-tuple (Z part=0).
#define VEC2X3MAT(o, i, m)
 Multiply a 2-tuple (Z=0) by the 3x3 part of a mat_t.
#define MAT4X3PNT(o, m, i)
 Apply a 4x4 matrix to a 3-tuple which is an absolute Point in space.
#define PNT3X4MAT(o, i, m)
 Multiply an Absolute 3-Point by a full 4x4 matrix.
#define MAT4X4PNT(o, m, i)
 Multiply an Absolute hvect_t 4-Point by a full 4x4 matrix.
#define MAT4X3VEC(o, m, i)
 Apply a 4x4 matrix to a 3-tuple which is a relative Vector in space This macro can scale the length of the vector if [15] != 1.0.
#define MAT4XSCALOR(o, m, i)   {(o) = (i) / (m)[15];}
#define VEC3X4MAT(o, i, m)
 Multiply a Relative 3-Vector by most of a 4x4 matrix.
#define VEC2X4MAT(o, i, m)
 Multiply a Relative 2-Vector by most of a 4x4 matrix.
#define BN_VEC_NON_UNIT_LEN(_vec)   (fabs(MAGSQ(_vec)) < 0.0001 || fabs(fabs(MAGSQ(_vec))-1) > 0.0001)
 Test a vector for non-unit length.
#define VEQUAL(a, b)   ((a)[X]==(b)[X] && (a)[Y]==(b)[Y] && (a)[Z]==(b)[Z])
 Compare two vectors for EXACT equality. Use carefully.
#define VAPPROXEQUAL(a, b, tol)
 Compare two vectors for approximate equality, within the specified absolute tolerance.
#define VNEAR_ZERO(v, tol)
 Test for all elements of `v' being smaller than `tol'.
#define V_MIN(r, s)   if( (s) < (r) ) r = (s)
 Macros to update min and max X,Y,Z values to contain a point.
#define V_MAX(r, s)   if( (s) > (r) ) r = (s)
#define VMIN(r, s)   { V_MIN((r)[X],(s)[X]); V_MIN((r)[Y],(s)[Y]); V_MIN((r)[Z],(s)[Z]); }
#define VMAX(r, s)   { V_MAX((r)[X],(s)[X]); V_MAX((r)[Y],(s)[Y]); V_MAX((r)[Z],(s)[Z]); }
#define VMINMAX(min, max, pt)   { VMIN( (min), (pt) ); VMAX( (max), (pt) ); }
#define HDIVIDE(a, b)
 Divide out homogeneous parameter from hvect_t, creating vect_t.
#define VADD2_2D(a, b, c)   V2ADD2(a,b,c)
 Some 2-D versions of the 3-D macros given above.
#define VSUB2_2D(a, b, c)   V2SUB2(a,b,c)
#define MAGSQ_2D(a)   MAG2SQ(a)
#define VDOT_2D(a, b)   V2DOT(a,b)
#define VMOVE_2D(a, b)   V2MOVE(a,b)
#define VSCALE_2D(a, b, c)   V2SCALE(a,b,c)
#define VJOIN1_2D(a, b, c, d)   V2JOIN1(a,b,c,d)
#define QUAT_FROM_ROT(q, r, x, y, z)
 Quaternion math definitions.Create Quaternion from Vector and Rotation about vector.
#define QUAT_FROM_VROT(q, r, v)
#define QUAT_FROM_VROT_DEG(q, r, v)   QUAT_FROM_VROT(q, ((r)*(M_PI/180.0)), v)
#define QUAT_FROM_ROT_DEG(q, r, x, y, z)   QUAT_FROM_ROT(q, ((r)*(M_PI/180.0)), x, y, z)
#define QSET(a, b, c, d, e)
 Set quaternion at `a' to have coordinates `b', `c', `d', `e'.
#define QMOVE(a, b)
 Transfer quaternion at `b' to quaternion at `a'.
#define QADD2(a, b, c)
 Add quaternions at `b' and `c', store result at `a'.
#define QSUB2(a, b, c)
 Subtract quaternion at `c' from quaternion at `b', store result at `a'.
#define QSCALE(a, b, c)
 Scale quaternion at `b' by scalar `c', store result at `a'.
#define QUNITIZE(a)
 Normalize quaternion 'a' to be a unit quaternion.
#define QMAGSQ(a)
 Return scalar magnitude squared of quaternion at `a'.
#define QMAGNITUDE(a)   sqrt( QMAGSQ( a ) )
 Return scalar magnitude of quaternion at `a'.
#define QDOT(a, b)
 Compute dot product of quaternions at `a' and `b'.
#define QMUL(a, b, c)
 Compute quaternion product a = b * c a[W] = b[W]*c[W] - VDOT(b,c); VCROSS( temp, b, c ); VJOIN2( a, temp, b[W], c, c[W], b );.
#define QCONJUGATE(a, b)
 Conjugate quaternion.
#define QINVERSE(a, b)
 Multiplicative inverse quaternion.
#define QBLEND2(a, b, c, d, e)
 Blend into quaternion `a' scalar `b' times quaternion at `c' plus scalar `d' times quaternion at `e'.
#define V3RPP_DISJOINT(_l1, _h1, _l2, _h2)
 Macros for dealing with 3-D "extents" represented as axis-aligned right parallelpipeds (RPPs). This is stored as two points: a min point, and a max point. RPP 1 is defined by lo1, hi1, RPP 2 by lo2, hi2. Compare two extents represented as RPPs. If they are disjoint, return true.
#define V3RPP_OVERLAP(_l1, _h1, _l2, _h2)
 Compare two extents represented as RPPs. If they overlap, return true.
#define V3RPP_OVERLAP_TOL(_l1, _h1, _l2, _h2, _t)
 If two extents overlap within distance tolerance, return true.
#define V3PT_IN_RPP(_pt, _lo, _hi)
 Is the point within or on the boundary of the RPP?
#define V3PT_IN_RPP_TOL(_pt, _lo, _hi, _t)
 Within the distance tolerance, is the point within the RPP?
#define V3RPP1_IN_RPP2(_lo1, _hi1, _lo2, _hi2)
 Determine if one bounding box is within another. Also returns true if the boxes are the same.

Typedefs

typedef fastf_t mat_t [ELEMENTS_PER_MAT]
 4x4 matrix
typedef fastf_tmatp_t
typedef fastf_t vect_t [ELEMENTS_PER_VECT]
 3-tuple vector
typedef fastf_tvectp_t
typedef fastf_t point_t [ELEMENTS_PER_PT]
 3-tuple point
typedef fastf_tpointp_t
typedef fastf_t point2d_t [2]
typedef fastf_t hvect_t [HVECT_LEN]
 4-tuple vector
typedef fastf_t hpoint_t [HPT_LEN]
 4-tuple point
typedef fastf_t plane_t [ELEMENTS_PER_PLANE]
 Definition of a plane equation.


Detailed Description

vector/matrix math

This header file defines many commonly used 3D vector math macros, and operates on vect_t, point_t, mat_t, and quat_t objects.

Note that while many people in the computer graphics field use post-multiplication with row vectors (ie, vector * matrix * matrix ...) the BRL-CAD system uses the more traditional representation of column vectors (ie, ... matrix * matrix * vector). (The matrices in these two representations are the transposes of each other). Therefore, when transforming a vector by a matrix, pre-multiplication is used, ie:

view_vec = model2view_mat * model_vec

Furthermore, additional transformations are multiplied on the left, ie:

                vec'  =  T1 * vec
                vec'' =  T2 * T1 * vec  =  T2 * vec'

The most notable implication of this is the location of the "delta" (translation) values in the matrix, ie:

        x'     ( R0   R1   R2   Dx )      x
        y' =   ( R4   R5   R6   Dy )   *  y
        z'     ( R8   R9   R10  Dz )      z
        w'     (  0    0    0   1/s)      w

Note -
vect_t objects are 3-tuples
hvect_t objects are 4-tuples
Most of these macros require that the result be in separate storage, distinct from the input parameters, except where noted.

When writing macros like this, it is very important that any variables which are declared within code blocks inside a macro start with an underscore. This prevents any name conflicts with user-provided parameters. For example: { register double _f; stuff; }

Author:
Michael John Muuss
Source
The U. S. Army Research Laboratory
Aberdeen Proving Ground, Maryland 21005
Include Sequencing -
        #include <stdio.h>
        #include <math.h>
        #include "machine.h"    /_* For fastf_t definition on this machine *_/
        #include "vmath.h"

Libraries Used
-lm -lc
Header
/cvsroot/brlcad/brlcad/include/vmath.h,v 14.14 2006/09/18 05:24:07 lbutler Exp

Definition in file vmath.h.


Generated on Mon Sep 18 01:25:02 2006 for BRL-CAD by  doxygen 1.4.6