summaryrefslogtreecommitdiff
path: root/sim/src/lin-alg.h
blob: 4c8742d3c52c22fb4c18100e6a54fc750d9b2cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2005-2006 Nanorex, Inc.  See LICENSE file for details. 
#ifndef LIN_ALG_H_INCLUDED
#define LIN_ALG_H_INCLUDED

#define RCSID_LIN_ALG_H  "$Id$"

/* a 3-vector */
struct xyz {
        double x;
        double y;
        double z;
};

// cast a struct xyz into a struct xyza, and you can access the
// dimensions by an index:
struct xyza {
  double a[3];
};

struct quaternion {
  double x;
  double y;
  double z;
  double a;
};

extern struct xyz vcon(double x);

extern struct xyz vsum(struct xyz v, struct xyz w);

extern struct xyz vprod(struct xyz v, struct xyz w);

extern struct xyz vprodc(struct xyz v, double w);

extern struct xyz vdif(struct xyz v, struct xyz w);

extern double vlen(struct xyz v);

extern struct xyz uvec(struct xyz v);

extern double vang(struct xyz v, struct xyz w);

extern struct xyz vx(struct xyz v, struct xyz w);

extern void printMatrix3(FILE *f, double *m);

extern void matrixRotateX(double *m, double theta);

extern void matrixRotateY(double *m, double theta);

extern void matrixRotateZ(double *m, double theta);

extern void matrixRotateXYZ(double *rotation, double thetaX, double thetaY, double thetaZ);

extern void matrixMultiply(double *prod, double *a, double *b);

extern void matrixTransform(struct xyz *out, double *m, struct xyz *in);

extern void matrixInverseTransform(struct xyz *out, double *m, struct xyz *in);

extern int matrixInvert3(double *inverse, double *m);


/** vector addition (incremental: add src to dest) */
#define vadd(dest,src) (dest).x+=(src).x; (dest).y+=(src).y; (dest).z+=(src).z
/** vector addition (non-incremental) */
#define vadd2(dest,src1,src2) (dest).x=(src1).x+(src2).x; \
    (dest).y=(src1).y+(src2).y; (dest).z=(src1).z+(src2).z
/** vector addition (incremental, with scaling) */
#define vadd2scale(dest,src1,k) (dest).x+=(src1).x*(k); \
    (dest).y+=(src1).y*(k); (dest).z+=(src1).z*(k)
/** vector subtraction (incremental: subtract src from dest) */
#define vsub(dest,src) (dest).x-=(src).x; (dest).y-=(src).y; (dest).z-=(src).z
/** vector subtraction (non-incremental) */
#define vsub2(dest,src1,src2) (dest).x=(src1).x-(src2).x; \
    (dest).y=(src1).y-(src2).y; (dest).z=(src1).z-(src2).z
/** */
#define vmul(dest,src) (dest).x*=(src).x; (dest).y*=(src).y; (dest).z*=(src).z
/** */
#define vmul2(dest,src1,src2) (dest).x=(src1).x*(src2).x; \
    (dest).y=(src1).y*(src2).y; (dest).z=(src1).z*(src2).z
/** */
#define vmul2c(dest,src1,src2) (dest).x=(src1).x*(src2); \
    (dest).y=(src1).y*(src2); (dest).z=(src1).z*(src2)
/** */
#define vmulc(dest,src) (dest).x*=(src); (dest).y*=(src); (dest).z*=(src)

#define vdiv(dest,src) (dest).x/=(src).x; (dest).y/=(src).y; (dest).z/=(src).z
#define vdiv2(dest,src1,src2) (dest).x=(src1).x/(src2).x; \
    (dest).y=(src1).y/(src2).y; (dest).z=(src1).z/(src2).z
#define vdivc(dest,src) (dest).x/=(src); (dest).y/=(src); (dest).z/=(src)

/** */
#define vset(dest,src) (dest).x=(src).x; (dest).y=(src).y; (dest).z=(src).z
/** */
#define vsetc(dest,src) (dest).x=(src); (dest).y=(src); (dest).z=(src)
/** */
#define vsetn(dest,src) (dest).x= -(src).x; (dest).y= -(src).y; (dest).z= -(src).z

/** */
#define vmin(dest,src) (dest).x = min((dest).x,(src).x); \
    (dest).y = min((dest).y,(src).y); (dest).z = min((dest).z,(src).z);
/** */
#define vmax(dest,src) (dest).x = max((dest).x,(src).x); \
    (dest).y = max((dest).y,(src).y); (dest).z = max((dest).z,(src).z);

/** */
#define vdot(src1,src2) ((src1).x*(src2).x+(src1).y*(src2).y+(src1).z*(src2).z)
// cross product
#define v2x(dest,src1,src2)  (dest).x = (src1).y * (src2).z - (src1).z * (src2).y;\
	(dest).y = (src1).z * (src2).x - (src1).x * (src2).z;\
	(dest).z = (src1).x * (src2).y - (src1).y * (src2).x;

#endif