📄 vec.h
字号:
/* * vec.h -- Vector macros for 2,3, and 4 dimensions, * for any combination of C scalar types. * * Author: Don Hatch (hatch@sgi.com) * Last modified: Fri Sep 30 03:23:02 PDT 1994 * * General description: * * The macro name describes its arguments; e.g. * MXS3 is "matrix times scalar in 3 dimensions"; * VMV2 is "vector minus vector in 2 dimensions". * * If the result of an operation is a scalar, then the macro "returns" * the value; e.g. * result = DOT3(v,w); * result = DET4(m); * * If the result of an operation is a vector or matrix, then * the first argument is the destination; e.g. * SET2(tovec, fromvec); * MXM3(result, m1, m2); * * WARNING: For the operations that are not done "componentwise" * (e.g. vector cross products and matrix multiplies) * the destination should not be either of the arguments, * for obvious reasons. For example, the following is wrong: * VXM2(v,v,m); * For such "unsafe" macros, there are safe versions provided, * but you have to specify a type for the temporary * result vector or matrix. For example, the safe versions * of VXM2 are: * VXM2d(v,v,m) if v's scalar type is double or float * VXM2i(v,v,m) if v's scalar type is int or char * VXM2l(v,v,m) if v's scalar type is long * VXM2r(v,v,m) if v's scalar type is real * VXM2safe(type,v,v,m) for other scalar types. * These "safe" macros do not evaluate to C expressions * (so, for example, they can't be used inside the parentheses of * a for(...)). * * Specific descriptions: * * The "?"'s in the following can be 2, 3, or 4. * * SET?(to,from) to = from * SETMAT?(to,from) to = from * ROUNDVEC?(to,from) to = from with entries rounded * to nearest integer * ROUNDMAT?(to,from) to = from with entries rounded * to nearest integer * FILLVEC?(v,s) set each entry of vector v to be s * FILLMAT?(m,s) set each entry of matrix m to be s * ZEROVEC?(v) v = 0 * ISZEROVEC?(v) v == 0 * EQVEC?(v,w) v == w * EQMAT?(m1,m2) m1 == m2 * ZEROMAT?(m) m = 0 * IDENTMAT?(m) m = 1 * TRANSPOSE?(to,from) (matrix to) = (transpose of matrix from) * ADJOINT?(to,from) (matrix to) = (adjoint of matrix from) * i.e. its determinant times its inverse * * V{P,M}V?(to,v,w) to = v {+,-} w * M{P,M}M?(to,m1,m2) to = m1 {+,-} m2 * SX{V,M}?(to,s,from) to = s * from * M{V,M}?(to,from) to = -from * {V,M}{X,D}S?(to,from,s) to = from {*,/} s * MXM?(to,m1,m2) to = m1 * m2 * VXM?(to,v,m) (row vec to) = (row vec v) * m * MXV?(to,m,v) (column vec to) = m * (column vec v) * LERP?(to,v0,v1,t) to = v0 + t*(v1-v0) * * DET?(m) determinant of m * TRACE?(m) trace (sum of diagonal entries) of m * DOT?(v,w) dot (scalar) product of v and w * NORMSQRD?(v) square of |v| * DISTSQRD?(v,w) square of |v-w| * * XV2(to,v) to = v rotated by 90 degrees * VXV3(to,v1,v2) to = cross (vector) product of v1 and v2 * VXVXV4(to,v1,v2,v3) to = 4-dimensional vector cross product * of v1,v2,v3 (a vector orthogonal to * v1,v2,v3 whose length equals the * volume of the spanned parallelotope) * VXV2(v0,v1) determinant of matrix with rows v0,v1 * VXVXV3(v0,v1,v2) determinant of matrix with rows v0,v1,v2 * VXVXVXV4(v0,v1,v2,v3) determinant of matrix with rows v0,..,v3 * * The following macros mix objects from different dimensions. * For example, V3XM4 would be used to apply a composite * 4x4 rotation-and-translation matrix to a 3d vector. * * SET3from2(to,from,pad) (3d vec to) = (2d vec from) with pad * SET4from3(to,from,pad) (4d vec to) = (3d vec from) with pad * SETMAT3from2(to,from,pad0,pad1) (3x3 mat to) = (2x2 mat from) * padded with pad0 on the sides * and pad1 in the corner * SETMAT4from3(to,from,pad0,pad1) (4x4 mat to) = (3x3 mat from) * padded with pad0 on the sides * and pad1 in the corner * V2XM3(to2,v2,m3) (2d row vec to2) = (2d row vec v2) * (3x3 mat m3) * V3XM4(to3,v3,m4) (3d row vec to3) = (3d row vec v2) * (4x4 mat m4) * M3XV2(to2,m3,v2) (2d col vec to2) = (3x3 mat m3) * (2d col vec v2) * M4XV3(to3,m4,v3) (3d col vec to3) = (4x4 mat m4) * (3d col vec v3) * M2XM3(to3,m2,m3) (3x3 mat to3) = (2x2 mat m2) * (3x3 mat m3) * M3XM4(to4,m3,m4) (4x4 mat to4) = (3x3 mat m3) * (4x4 mat m4) * M3XM2(to3,m3,m2) (3x3 mat to3) = (3x3 mat m3) * (2x2 mat m2) * M4XM3(to4,m4,m3) (4x4 mat to4) = (4x4 mat m4) * (3x3 mat m3) * * * This file is machine-generated and can be regenerated * for any number of dimensions. * The program that generated it is available upon request. */#ifndef VEC_H#define VEC_H 4#include <math.h> /* for definition of floor() */#define SET2(to,from) \ ((to)[0] = (from)[0], \ (to)[1] = (from)[1])#define SETMAT2(to,from) \ (SET2((to)[0], (from)[0]), \ SET2((to)[1], (from)[1]))#define ROUNDVEC2(to,from) \ ((to)[0] = floor((from)[0]+.5), \ (to)[1] = floor((from)[1]+.5))#define ROUNDMAT2(to,from) \ (ROUNDVEC2((to)[0], (from)[0]), \ ROUNDVEC2((to)[1], (from)[1]))#define FILLVEC2(v,s) \ ((v)[0] = (s), \ (v)[1] = (s))#define FILLMAT2(m,s) \ (FILLVEC2((m)[0], s), \ FILLVEC2((m)[1], s))#define ZEROVEC2(v) \ ((v)[0] = 0, \ (v)[1] = 0)#define ISZEROVEC2(v) \ ((v)[0] == 0 && \ (v)[1] == 0)#define EQVEC2(v,w) \ ((v)[0] == (w)[0] && \ (v)[1] == (w)[1])#define EQMAT2(m1,m2) \ (EQVEC2((m1)[0], (m2)[0]) && \ EQVEC2((m1)[1], (m2)[1]))#define ZEROMAT2(m) \ (ZEROVEC2((m)[0]), \ ZEROVEC2((m)[1]))#define IDENTMAT2(m) \ (ZEROVEC2((m)[0]), (m)[0][0]=1, \ ZEROVEC2((m)[1]), (m)[1][1]=1)#define TRANSPOSE2(to,from) \ (_SETcol2((to)[0], from, 0), \ _SETcol2((to)[1], from, 1))#define VPV2(to,v,w) \ ((to)[0] = (v)[0] + (w)[0], \ (to)[1] = (v)[1] + (w)[1])#define VMV2(to,v,w) \ ((to)[0] = (v)[0] - (w)[0], \ (to)[1] = (v)[1] - (w)[1])#define MPM2(to,m1,m2) \ (VPV2((to)[0], (m1)[0], (m2)[0]), \ VPV2((to)[1], (m1)[1], (m2)[1]))#define MMM2(to,m1,m2) \ (VMV2((to)[0], (m1)[0], (m2)[0]), \ VMV2((to)[1], (m1)[1], (m2)[1]))#define SXV2(to,s,from) \ ((to)[0] = (s) * (from)[0], \ (to)[1] = (s) * (from)[1])#define SXM2(to,s,from) \ (SXV2((to)[0], s, (from)[0]), \ SXV2((to)[1], s, (from)[1]))#define MV2(to,from) \ ((to)[0] = -(from)[0], \ (to)[1] = -(from)[1])#define MM2(to,from) \ (MV2((to)[0], (from)[0]), \ MV2((to)[1], (from)[1]))#define VXS2(to,from,s) \ ((to)[0] = (from)[0] * (s), \ (to)[1] = (from)[1] * (s))#define VDS2(to,from,s) \ ((to)[0] = (from)[0] / (s), \ (to)[1] = (from)[1] / (s))#define MXS2(to,from,s) \ (VXS2((to)[0], (from)[0], s), \ VXS2((to)[1], (from)[1], s))#define MDS2(to,from,s) \ (VDS2((to)[0], (from)[0], s), \ VDS2((to)[1], (from)[1], s))#define MXM2(to,m1,m2) \ (VXM2((to)[0], (m1)[0], m2), \ VXM2((to)[1], (m1)[1], m2))#define VXM2(to,v,m) \ ((to)[0] = _DOTcol2(v, m, 0), \ (to)[1] = _DOTcol2(v, m, 1))#define MXV2(to,m,v) \ ((to)[0] = DOT2((m)[0], v), \ (to)[1] = DOT2((m)[1], v))#define LERP2(to,v0,v1,t) \ ((to)[0]=(v0)[0]+(t)*((v1)[0]-(v0)[0]), \ (to)[1]=(v0)[1]+(t)*((v1)[1]-(v0)[1]))#define TRACE2(m) \ ((m)[0][0] + \ (m)[1][1])#define DOT2(v,w) \ ((v)[0] * (w)[0] + \ (v)[1] * (w)[1])#define NORMSQRD2(v) \ ((v)[0] * (v)[0] + \ (v)[1] * (v)[1])#define DISTSQRD2(v,w) \ (((v)[0]-(w)[0])*((v)[0]-(w)[0]) + \ ((v)[1]-(w)[1])*((v)[1]-(w)[1]))#define _DOTcol2(v,m,j) \ ((v)[0] * (m)[0][j] + \ (v)[1] * (m)[1][j])#define _SETcol2(v,m,j) \ ((v)[0] = (m)[0][j], \ (v)[1] = (m)[1][j])#define _MXVcol2(to,m,M,j) \ ((to)[0][j] = _DOTcol2((m)[0],M,j), \ (to)[1][j] = _DOTcol2((m)[1],M,j))#define _DET2(v0,v1,i0,i1) \ ((v0)[i0]* _DET1(v1,i1) + \ (v0)[i1]*-_DET1(v1,i0))#define XV2(to,v1) \ ((to)[0] = -_DET1(v1, 1), \ (to)[1] = _DET1(v1, 0))#define V2XM3(to2,v2,m3) \ ((to2)[0] = _DOTcol2(v2,m3,0) + (m3)[2][0], \ (to2)[1] = _DOTcol2(v2,m3,1) + (m3)[2][1])#define M3XV2(to2,m3,v2) \ ((to2)[0] = DOT2((m3)[0],v2) + (m3)[0][2], \ (to2)[1] = DOT2((m3)[1],v2) + (m3)[1][2])#define _DET1(v0,i0) \ ((v0)[i0])#define VXV2(v0,v1) \ (_DET2(v0,v1,0,1))#define DET2(m) \ (VXV2((m)[0],(m)[1]))#define ADJOINT2(to,m) \ ( _ADJOINTcol2(to,0,m,1), \ __ADJOINTcol2(to,1,m,0))#define _ADJOINTcol2(to,col,m,i1) \ ((to)[0][col] = _DET1(m[i1], 1), \ (to)[1][col] = -_DET1(m[i1], 0))#define __ADJOINTcol2(to,col,m,i1) \ ((to)[0][col] = -_DET1(m[i1], 1), \ (to)[1][col] = _DET1(m[i1], 0))#define SET3(to,from) \ ((to)[0] = (from)[0], \ (to)[1] = (from)[1], \ (to)[2] = (from)[2])#define SETMAT3(to,from) \ (SET3((to)[0], (from)[0]), \ SET3((to)[1], (from)[1]), \ SET3((to)[2], (from)[2]))#define ROUNDVEC3(to,from) \ ((to)[0] = floor((from)[0]+.5), \ (to)[1] = floor((from)[1]+.5), \ (to)[2] = floor((from)[2]+.5))#define ROUNDMAT3(to,from) \ (ROUNDVEC3((to)[0], (from)[0]), \ ROUNDVEC3((to)[1], (from)[1]), \ ROUNDVEC3((to)[2], (from)[2]))#define FILLVEC3(v,s) \ ((v)[0] = (s), \ (v)[1] = (s), \ (v)[2] = (s))#define FILLMAT3(m,s) \ (FILLVEC3((m)[0], s), \ FILLVEC3((m)[1], s), \ FILLVEC3((m)[2], s))#define ZEROVEC3(v) \ ((v)[0] = 0, \ (v)[1] = 0, \ (v)[2] = 0)#define ISZEROVEC3(v) \ ((v)[0] == 0 && \ (v)[1] == 0 && \ (v)[2] == 0)#define EQVEC3(v,w) \ ((v)[0] == (w)[0] && \ (v)[1] == (w)[1] && \ (v)[2] == (w)[2])#define EQMAT3(m1,m2) \ (EQVEC3((m1)[0], (m2)[0]) && \ EQVEC3((m1)[1], (m2)[1]) && \ EQVEC3((m1)[2], (m2)[2]))#define ZEROMAT3(m) \ (ZEROVEC3((m)[0]), \ ZEROVEC3((m)[1]), \ ZEROVEC3((m)[2]))#define IDENTMAT3(m) \ (ZEROVEC3((m)[0]), (m)[0][0]=1, \ ZEROVEC3((m)[1]), (m)[1][1]=1, \ ZEROVEC3((m)[2]), (m)[2][2]=1)#define TRANSPOSE3(to,from) \ (_SETcol3((to)[0], from, 0), \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -