mesa: replace FABSF with fabsf
[mesa.git] / src / mesa / math / m_matrix.c
index e81be8a7e0016b85c1f78c01baf6b2d9cff1df35..9d51021feda88cc88c20ac9c099936aa574e6a7e 100644 (file)
@@ -1,18 +1,7 @@
-/**
- * \file m_matrix.c
- * Matrix operations.
- *
- * \note
- * -# 4x4 transformation matrices are stored in memory in column major order.
- * -# Points/vertices are to be thought of as column vectors.
- * -# Transformation of a point p by a matrix M is: p' = M * p
- */
-
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
  *
- * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+/**
+ * \file m_matrix.c
+ * Matrix operations.
+ *
+ * \note
+ * -# 4x4 transformation matrices are stored in memory in column major order.
+ * -# Points/vertices are to be thought of as column vectors.
+ * -# Transformation of a point p by a matrix M is: p' = M * p
  */
 
 
-#include "glheader.h"
-#include "imports.h"
-#include "macros.h"
-#include "imports.h"
+#include "c99_math.h"
+#include "main/glheader.h"
+#include "main/imports.h"
+#include "main/macros.h"
 
 #include "m_matrix.h"
 
 
+/**
+ * \defgroup MatFlags MAT_FLAG_XXX-flags
+ *
+ * Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags
+ */
+/*@{*/
+#define MAT_FLAG_IDENTITY       0     /**< is an identity matrix flag.
+                                       *   (Not actually used - the identity
+                                       *   matrix is identified by the absense
+                                       *   of all other flags.)
+                                       */
+#define MAT_FLAG_GENERAL        0x1   /**< is a general matrix flag */
+#define MAT_FLAG_ROTATION       0x2   /**< is a rotation matrix flag */
+#define MAT_FLAG_TRANSLATION    0x4   /**< is a translation matrix flag */
+#define MAT_FLAG_UNIFORM_SCALE  0x8   /**< is an uniform scaling matrix flag */
+#define MAT_FLAG_GENERAL_SCALE  0x10  /**< is a general scaling matrix flag */
+#define MAT_FLAG_GENERAL_3D     0x20  /**< general 3D matrix flag */
+#define MAT_FLAG_PERSPECTIVE    0x40  /**< is a perspective proj matrix flag */
+#define MAT_FLAG_SINGULAR       0x80  /**< is a singular matrix flag */
+#define MAT_DIRTY_TYPE          0x100  /**< matrix type is dirty */
+#define MAT_DIRTY_FLAGS         0x200  /**< matrix flags are dirty */
+#define MAT_DIRTY_INVERSE       0x400  /**< matrix inverse is dirty */
+
+/** angle preserving matrix flags mask */
+#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \
+                                   MAT_FLAG_TRANSLATION | \
+                                   MAT_FLAG_UNIFORM_SCALE)
+
+/** geometry related matrix flags mask */
+#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \
+                           MAT_FLAG_ROTATION | \
+                           MAT_FLAG_TRANSLATION | \
+                           MAT_FLAG_UNIFORM_SCALE | \
+                           MAT_FLAG_GENERAL_SCALE | \
+                           MAT_FLAG_GENERAL_3D | \
+                           MAT_FLAG_PERSPECTIVE | \
+                           MAT_FLAG_SINGULAR)
+
+/** length preserving matrix flags mask */
+#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \
+                                    MAT_FLAG_TRANSLATION)
+
+
+/** 3D (non-perspective) matrix flags mask */
+#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \
+                     MAT_FLAG_TRANSLATION | \
+                     MAT_FLAG_UNIFORM_SCALE | \
+                     MAT_FLAG_GENERAL_SCALE | \
+                     MAT_FLAG_GENERAL_3D)
+
+/** dirty matrix flags mask */
+#define MAT_DIRTY          (MAT_DIRTY_TYPE | \
+                           MAT_DIRTY_FLAGS | \
+                           MAT_DIRTY_INVERSE)
+
+/*@}*/
+
+
+/** 
+ * Test geometry related matrix flags.
+ * 
+ * \param mat a pointer to a GLmatrix structure.
+ * \param a flags mask.
+ *
+ * \returns non-zero if all geometry related matrix flags are contained within
+ * the mask, or zero otherwise.
+ */ 
+#define TEST_MAT_FLAGS(mat, a)  \
+    ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)
+
+
+
 /**
  * Names of the corresponding GLmatrixtype values.
  */
@@ -187,7 +260,8 @@ _math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
 {
    dest->flags |= (MAT_FLAG_GENERAL |
                   MAT_DIRTY_TYPE |
-                  MAT_DIRTY_INVERSE);
+                  MAT_DIRTY_INVERSE |
+                   MAT_DIRTY_FLAGS);
 
    matmul4( dest->m, dest->m, m );
 }
@@ -222,19 +296,15 @@ static void print_matrix_floats( const GLfloat m[16] )
 void
 _math_matrix_print( const GLmatrix *m )
 {
+   GLfloat prod[16];
+
    _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
    print_matrix_floats(m->m);
    _mesa_debug(NULL, "Inverse: \n");
-   if (m->inv) {
-      GLfloat prod[16];
-      print_matrix_floats(m->inv);
-      matmul4(prod, m->m, m->inv);
-      _mesa_debug(NULL, "Mat * Inverse:\n");
-      print_matrix_floats(prod);
-   }
-   else {
-      _mesa_debug(NULL, "  - not available\n");
-   }
+   print_matrix_floats(m->inv);
+   matmul4(prod, m->m, m->inv);
+   _mesa_debug(NULL, "Mat * Inverse:\n");
+   print_matrix_floats(prod);
 }
 
 /*@}*/
@@ -259,7 +329,7 @@ _math_matrix_print( const GLmatrix *m )
 /*@{*/
 
 /**
- * Swaps the values of two floating pointer variables.
+ * Swaps the values of two floating point variables.
  *
  * Used by invert_matrix_general() to swap the row pointers.
  */
@@ -307,9 +377,9 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
 
    /* choose pivot - or die */
-   if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
-   if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
-   if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
+   if (fabsf(r3[0])>fabsf(r2[0])) SWAP_ROWS(r3, r2);
+   if (fabsf(r2[0])>fabsf(r1[0])) SWAP_ROWS(r2, r1);
+   if (fabsf(r1[0])>fabsf(r0[0])) SWAP_ROWS(r1, r0);
    if (0.0 == r0[0])  return GL_FALSE;
 
    /* eliminate first variable     */
@@ -327,8 +397,8 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
 
    /* choose pivot - or die */
-   if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
-   if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
+   if (fabsf(r3[1])>fabsf(r2[1])) SWAP_ROWS(r3, r2);
+   if (fabsf(r2[1])>fabsf(r1[1])) SWAP_ROWS(r2, r1);
    if (0.0 == r1[1])  return GL_FALSE;
 
    /* eliminate second variable */
@@ -341,7 +411,7 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
 
    /* choose pivot - or die */
-   if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
+   if (fabsf(r3[2])>fabsf(r2[2])) SWAP_ROWS(r3, r2);
    if (0.0 == r2[2])  return GL_FALSE;
 
    /* eliminate third variable */
@@ -439,7 +509,7 @@ static GLboolean invert_matrix_3d_general( GLmatrix *mat )
 
    det = pos + neg;
 
-   if (det*det < 1e-25)
+   if (fabsf(det) < 1e-25)
       return GL_FALSE;
 
    det = 1.0F / det;
@@ -524,7 +594,7 @@ static GLboolean invert_matrix_3d( GLmatrix *mat )
    }
    else {
       /* pure translation */
-      MEMCPY( out, Identity, sizeof(Identity) );
+      memcpy( out, Identity, sizeof(Identity) );
       MAT(out,0,3) = - MAT(in,0,3);
       MAT(out,1,3) = - MAT(in,1,3);
       MAT(out,2,3) = - MAT(in,2,3);
@@ -562,7 +632,7 @@ static GLboolean invert_matrix_3d( GLmatrix *mat )
  */
 static GLboolean invert_matrix_identity( GLmatrix *mat )
 {
-   MEMCPY( mat->inv, Identity, sizeof(Identity) );
+   memcpy( mat->inv, Identity, sizeof(Identity) );
    return GL_TRUE;
 }
 
@@ -584,7 +654,7 @@ static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
    if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
       return GL_FALSE;
 
-   MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
+   memcpy( out, Identity, 16 * sizeof(GLfloat) );
    MAT(out,0,0) = 1.0F / MAT(in,0,0);
    MAT(out,1,1) = 1.0F / MAT(in,1,1);
    MAT(out,2,2) = 1.0F / MAT(in,2,2);
@@ -617,7 +687,7 @@ static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
    if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
       return GL_FALSE;
 
-   MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
+   memcpy( out, Identity, 16 * sizeof(GLfloat) );
    MAT(out,0,0) = 1.0F / MAT(in,0,0);
    MAT(out,1,1) = 1.0F / MAT(in,1,1);
 
@@ -639,7 +709,7 @@ static GLboolean invert_matrix_perspective( GLmatrix *mat )
    if (MAT(in,2,3) == 0)
       return GL_FALSE;
 
-   MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
+   memcpy( out, Identity, 16 * sizeof(GLfloat) );
 
    MAT(out,0,0) = 1.0F / MAT(in,0,0);
    MAT(out,1,1) = 1.0F / MAT(in,1,1);
@@ -701,7 +771,7 @@ static GLboolean matrix_invert( GLmatrix *mat )
       return GL_TRUE;
    } else {
       mat->flags |= MAT_FLAG_SINGULAR;
-      MEMCPY( mat->inv, Identity, sizeof(Identity) );
+      memcpy( mat->inv, Identity, sizeof(Identity) );
       return GL_FALSE;
    }
 }
@@ -732,7 +802,7 @@ _math_matrix_rotate( GLmatrix *mat,
    s = (GLfloat) sin( angle * DEG2RAD );
    c = (GLfloat) cos( angle * DEG2RAD );
 
-   MEMCPY(m, Identity, sizeof(GLfloat)*16);
+   memcpy(m, Identity, sizeof(GLfloat)*16);
    optimized = GL_FALSE;
 
 #define M(row,col)  m[col*4+row]
@@ -787,7 +857,7 @@ _math_matrix_rotate( GLmatrix *mat,
    }
 
    if (!optimized) {
-      const GLfloat mag = SQRTF(x * x + y * y + z * z);
+      const GLfloat mag = sqrtf(x * x + y * y + z * z);
 
       if (mag <= 1.0e-4) {
          /* no rotation, leave mat as-is */
@@ -814,7 +884,7 @@ _math_matrix_rotate( GLmatrix *mat,
        *  Y-axis to bring the axis vector parallel with the X-axis.  The
        *  rotation about the X-axis is then performed.  Ry and Rz are
        *  simply the respective inverse transforms to bring the arbitrary
-       *  axis back to it's original orientation.  The first transforms
+       *  axis back to its original orientation.  The first transforms
        *  Rz' and Ry' are considered inverses, since the data from the
        *  arbitrary axis gives you info on how to get to it, not how
        *  to get away from it, and an inverse must be applied.
@@ -951,22 +1021,28 @@ _math_matrix_ortho( GLmatrix *mat,
                    GLfloat bottom, GLfloat top,
                    GLfloat nearval, GLfloat farval )
 {
-   GLfloat x, y, z;
-   GLfloat tx, ty, tz;
    GLfloat m[16];
 
-   x = 2.0F / (right-left);
-   y = 2.0F / (top-bottom);
-   z = -2.0F / (farval-nearval);
-   tx = -(right+left) / (right-left);
-   ty = -(top+bottom) / (top-bottom);
-   tz = -(farval+nearval) / (farval-nearval);
-
 #define M(row,col)  m[col*4+row]
-   M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = 0.0F;  M(0,3) = tx;
-   M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = 0.0F;  M(1,3) = ty;
-   M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = z;     M(2,3) = tz;
-   M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = 0.0F;  M(3,3) = 1.0F;
+   M(0,0) = 2.0F / (right-left);
+   M(0,1) = 0.0F;
+   M(0,2) = 0.0F;
+   M(0,3) = -(right+left) / (right-left);
+
+   M(1,0) = 0.0F;
+   M(1,1) = 2.0F / (top-bottom);
+   M(1,2) = 0.0F;
+   M(1,3) = -(top+bottom) / (top-bottom);
+
+   M(2,0) = 0.0F;
+   M(2,1) = 0.0F;
+   M(2,2) = -2.0F / (farval-nearval);
+   M(2,3) = -(farval+nearval) / (farval-nearval);
+
+   M(3,0) = 0.0F;
+   M(3,1) = 0.0F;
+   M(3,2) = 0.0F;
+   M(3,3) = 1.0F;
 #undef M
 
    matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
@@ -994,7 +1070,7 @@ _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
    m[2] *= x;   m[6] *= y;   m[10] *= z;
    m[3] *= x;   m[7] *= y;   m[11] *= z;
 
-   if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
+   if (fabsf(x - y) < 1e-8 && fabsf(x - z) < 1e-8)
       mat->flags |= MAT_FLAG_UNIFORM_SCALE;
    else
       mat->flags |= MAT_FLAG_GENERAL_SCALE;
@@ -1029,6 +1105,26 @@ _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
                  MAT_DIRTY_INVERSE);
 }
 
+
+/**
+ * Set matrix to do viewport and depthrange mapping.
+ * Transforms Normalized Device Coords to window/Z values.
+ */
+void
+_math_matrix_viewport(GLmatrix *m, const double scale[3],
+                      const double translate[3], double depthMax)
+{
+   m->m[MAT_SX] = scale[0];
+   m->m[MAT_TX] = translate[0];
+   m->m[MAT_SY] = scale[1];
+   m->m[MAT_TY] = translate[1];
+   m->m[MAT_SZ] = depthMax*scale[2];
+   m->m[MAT_TZ] = depthMax*translate[2];
+   m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION;
+   m->type = MATRIX_3D_NO_ROT;
+}
+
+
 /**
  * Set a matrix to the identity matrix.
  *
@@ -1040,10 +1136,8 @@ _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
 void
 _math_matrix_set_identity( GLmatrix *mat )
 {
-   MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
-
-   if (mat->inv)
-      MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
+   memcpy( mat->m, Identity, 16*sizeof(GLfloat) );
+   memcpy( mat->inv, Identity, 16*sizeof(GLfloat) );
 
    mat->type = MATRIX_IDENTITY;
    mat->flags &= ~(MAT_DIRTY_FLAGS|
@@ -1136,7 +1230,7 @@ static void analyse_from_scratch( GLmatrix *mat )
       mat->type = MATRIX_2D_NO_ROT;
 
       if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
-        mat->flags = MAT_FLAG_GENERAL_SCALE;
+        mat->flags |= MAT_FLAG_GENERAL_SCALE;
    }
    else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
       GLfloat mm = DOT2(m, m);
@@ -1278,16 +1372,56 @@ _math_matrix_analyse( GLmatrix *mat )
 
    if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
       matrix_invert( mat );
+      mat->flags &= ~MAT_DIRTY_INVERSE;
    }
 
-   mat->flags &= ~(MAT_DIRTY_FLAGS|
-                  MAT_DIRTY_TYPE|
-                  MAT_DIRTY_INVERSE);
+   mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
 }
 
 /*@}*/
 
 
+/**
+ * Test if the given matrix preserves vector lengths.
+ */
+GLboolean
+_math_matrix_is_length_preserving( const GLmatrix *m )
+{
+   return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING);
+}
+
+
+/**
+ * Test if the given matrix does any rotation.
+ * (or perhaps if the upper-left 3x3 is non-identity)
+ */
+GLboolean
+_math_matrix_has_rotation( const GLmatrix *m )
+{
+   if (m->flags & (MAT_FLAG_GENERAL |
+                   MAT_FLAG_ROTATION |
+                   MAT_FLAG_GENERAL_3D |
+                   MAT_FLAG_PERSPECTIVE))
+      return GL_TRUE;
+   else
+      return GL_FALSE;
+}
+
+
+GLboolean
+_math_matrix_is_general_scale( const GLmatrix *m )
+{
+   return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE;
+}
+
+
+GLboolean
+_math_matrix_is_dirty( const GLmatrix *m )
+{
+   return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE;
+}
+
+
 /**********************************************************************/
 /** \name Matrix setup */
 /*@{*/
@@ -1303,18 +1437,10 @@ _math_matrix_analyse( GLmatrix *mat )
 void
 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
 {
-   MEMCPY( to->m, from->m, sizeof(Identity) );
+   memcpy( to->m, from->m, sizeof(Identity) );
+   memcpy(to->inv, from->inv, 16 * sizeof(GLfloat));
    to->flags = from->flags;
    to->type = from->type;
-
-   if (to->inv != 0) {
-      if (from->inv == 0) {
-        matrix_invert( to );
-      }
-      else {
-        MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
-      }
-   }
 }
 
 /**
@@ -1329,7 +1455,7 @@ _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
 void
 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
 {
-   MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
+   memcpy( mat->m, m, 16*sizeof(GLfloat) );
    mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
 }
 
@@ -1343,10 +1469,12 @@ _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
 void
 _math_matrix_ctr( GLmatrix *m )
 {
-   m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
+   m->m = _mesa_align_malloc( 16 * sizeof(GLfloat), 16 );
    if (m->m)
-      MEMCPY( m->m, Identity, sizeof(Identity) );
-   m->inv = NULL;
+      memcpy( m->m, Identity, sizeof(Identity) );
+   m->inv = _mesa_align_malloc( 16 * sizeof(GLfloat), 16 );
+   if (m->inv)
+      memcpy( m->inv, Identity, sizeof(Identity) );
    m->type = MATRIX_IDENTITY;
    m->flags = 0;
 }
@@ -1361,31 +1489,11 @@ _math_matrix_ctr( GLmatrix *m )
 void
 _math_matrix_dtr( GLmatrix *m )
 {
-   if (m->m) {
-      ALIGN_FREE( m->m );
-      m->m = NULL;
-   }
-   if (m->inv) {
-      ALIGN_FREE( m->inv );
-      m->inv = NULL;
-   }
-}
+   _mesa_align_free( m->m );
+   m->m = NULL;
 
-/**
- * Allocate a matrix inverse.
- *
- * \param m matrix.
- *
- * Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity.
- */
-void
-_math_matrix_alloc_inv( GLmatrix *m )
-{
-   if (!m->inv) {
-      m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
-      if (m->inv)
-         MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
-   }
+   _mesa_align_free( m->inv );
+   m->inv = NULL;
 }
 
 /*@}*/
@@ -1478,3 +1586,24 @@ _math_transposefd( GLfloat to[16], const GLdouble from[16] )
 
 /*@}*/
 
+
+/**
+ * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix.  This
+ * function is used for transforming clipping plane equations and spotlight
+ * directions.
+ * Mathematically,  u = v * m.
+ * Input:  v - input vector
+ *         m - transformation matrix
+ * Output:  u - transformed vector
+ */
+void
+_mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
+{
+   const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
+#define M(row,col)  m[row + col*4]
+   u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
+   u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
+   u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
+   u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
+#undef M
+}