File: matrixmath.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (172 lines) | stat: -rw-r--r-- 3,035 bytes parent folder | download | duplicates (6)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <string.h>

#include <math.h>

#include "matrixmath.h"

#ifdef MEMWATCH
# include "memwatch.h"
#endif


void MatrixMulti3Rotate33(double *a, double *b, double *r);

void MatrixRotateHeading3(double *a, double theta, double *r);
void MatrixRotatePitch3(double *a, double theta, double *r);
void MatrixRotateBank3(double *a, double theta, double *r);

void MatrixGetHeading33(double theta, double *r);
void MatrixGetPitch33(double theta, double *r);
void MatrixGetBank33(double theta, double *r);


/*
 *	Multiplies matrix a (3 by 1) with rotational matrix b (3 by 3)
 *	and stores results into matrix r (3 by 1).
 */
void MatrixMulti3Rotate33(double *a, double *b, double *r)
{
	int i, j;
	int m = 3, n = 3;

	for(j = 0; j < n; j++)
	{
	    r[j] = 0.0;
	    for(i = 0; i < m; i++)
		r[j] += a[i] * b[(i * m) + j];
	}
}

/*
 *	Rotates position matrix a (3 * 1) about z axis clockwise.
 *
 *	Theta is in radians.
 */
void MatrixRotateHeading3(double *a, double theta, double *r)
{
	double b[3 * 3];

	b[0] = cos(theta);
	b[1] = -sin(theta);
	b[2] = 0.0;

	b[3] = -b[1];	/* -sin(theta) */
	b[4] = b[0];	/* cos(theta) */
	b[5] = 0.0;

	b[6] = 0.0;
	b[7] = 0.0;
	b[8] = 1.0;

	MatrixMulti3Rotate33(a, b, r);	
}

/*
 *	Rotates position matrix a (3 * 1) about x axis clockwise.
 *
 *	Theta is in radians.
 */
void MatrixRotatePitch3(double *a, double theta, double *r)
{
	double b[3 * 3];

	b[0] = 1.0;
	b[1] = 0.0;
	b[2] = 0.0;  

	b[3] = 0.0;
	b[4] = cos(theta);
	b[5] = -sin(theta);

	b[6] = 0.0;
	b[7] = -b[5];	/* sin(theta) */
	b[8] = b[4];	/* cos(theta) */

	MatrixMulti3Rotate33(a, b, r);
}       

/*
 *	Rotates position matrix a (3 * 1) about y axis clockwise.
 *
 *	Theta is in radians.
 */
void MatrixRotateBank3(double *a, double theta, double *r)
{
	double b[3 * 3];

	b[0] = cos(theta);
	b[1] = 0.0;
	b[2] = sin(theta);
	
	b[3] = 0.0;
	b[4] = 1.0;
	b[5] = 0.0;

	b[6] = -b[2];	/* -sin(theta) */
	b[7] = 0.0;
	b[8] = b[0];	/* cos(theta) */

	MatrixMulti3Rotate33(a, b, r);
}


/*
 *	Generates a rotation matrix r (3 * 3) about the z axis clockwise.
 *
 *      Theta is in radians.
 */
void MatrixGetHeading33(double theta, double *r)
{
	r[0] = cos(theta);
	r[1] = -sin(theta);
	r[2] = 0.0;

	r[3] = -r[1];   /* -sin(theta) */
	r[4] = r[0];    /* cos(theta) */
	r[5] = 0.0;

	r[6] = 0.0;
	r[7] = 0.0;
	r[8] = 1.0;
}

/*      
 *      Generates a rotation matrix r (3 * 3) about the x axis clockwise.
 *
 *      Theta is in radians.
 */
void MatrixGetPitch33(double theta, double *r)
{
	r[0] = 1.0;
	r[1] = 0.0;
	r[2] = 0.0;

	r[3] = 0.0;
	r[4] = cos(theta);
	r[5] = -sin(theta);

	r[6] = 0.0;
	r[7] = -r[5];   /* sin(theta) */
	r[8] = r[4];    /* cos(theta) */
}

/*
 *      Generates a rotation matrix r (3 * 3) about the y axis clockwise.
 *
 *      Theta is in radians.
 */
void MatrixGetBank33(double theta, double *r)
{
	r[0] = cos(theta);
	r[1] = 0.0;
	r[2] = sin(theta);

	r[3] = 0.0;
	r[4] = 1.0;
	r[5] = 0.0;

	r[6] = -r[2];   /* -sin(theta) */
	r[7] = 0.0;
	r[8] = r[0];    /* cos(theta) */
}