summaryrefslogtreecommitdiff
path: root/src/test/ao_quaternion_test.c
blob: 0e4b5b071fd01e443e42166c3f4ac4ce9909ae37 (plain) (blame)
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
/*
 * Copyright © 2013 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#define _GNU_SOURCE

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <getopt.h>
#include <math.h>

#include "ao_quaternion.h"

static void
print_q(char *name, struct ao_quaternion *q)
{
	printf ("%8.8s: r%8.5f x%8.5f y%8.5f z%8.5f ", name,
		q->r, q->x, q->y, q->z);
}

int main(int argc, char **argv)
{
	struct ao_quaternion	position;
	struct ao_quaternion	rotation;
	struct ao_quaternion	little_rotation;
	int			i;

	/* unit x vector */
	ao_quaternion_init_vector(&position, 1, 0, 0);

	/* zero rotation */
	ao_quaternion_init_zero_rotation(&rotation);

	/* π/16 rotation around Z axis */
	ao_quaternion_init_rotation(&little_rotation, 0, 0, 1,
				    sin((M_PI/16)/2),
				    cos((M_PI/16)/2));
	for (i = 0; i <= 16; i++) {
		struct ao_quaternion	rotated;

		ao_quaternion_rotate(&rotated, &position, &rotation);
		print_q("position", &position);
		print_q("rotated", &rotated);
		print_q("rotation", &rotation);
		printf ("\n");
		ao_quaternion_multiply(&rotation, &rotation, &little_rotation);
		ao_quaternion_normalize(&rotation, &rotation);
	}
	return 0;
}