b630f1d315a110ba75e45a25218b8a2cfb1be562
[fw/altos] / src / test / ao_quaternion_test.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <getopt.h>
26 #include <math.h>
27
28 #include "ao_quaternion.h"
29
30 #if 0
31 static void
32 print_q(char *name, struct ao_quaternion *q)
33 {
34         printf ("%8.8s: r%8.5f x%8.5f y%8.5f z%8.5f ", name,
35                 q->r, q->x, q->y, q->z);
36 }
37 #endif
38
39 #define STEPS   16
40
41 #define DEG     (1.0f * 3.1415926535f / 180.0f)
42
43 struct ao_rotation {
44         int     steps;
45         float   x, y, z;
46 };
47
48 static struct ao_rotation ao_path[] = {
49         { .steps = 45, .x =  2*DEG, .y = 0, .z = 0 },
50
51         { .steps = 45, .x = 0, .y = 2*DEG, .z = 0 },
52         
53         { .steps = 45, .x = -2*DEG, .y = 0, .z = 0 },
54
55         { .steps = 45, .x = 0, .y = -2*DEG, .z = 0 },
56 };
57
58 #define NUM_PATH        (sizeof ao_path / sizeof ao_path[0])
59
60 static int close(float a, float b) {
61         return fabsf (a - b) < 1e-5;
62 }
63
64 static int check_quaternion(char *where, struct ao_quaternion *got, struct ao_quaternion *expect) {
65         if (!close (got->r, expect->r) ||
66             !close (got->x, expect->x) ||
67             !close (got->y, expect->y) ||
68             !close (got->z, expect->z))
69         {
70                 printf ("%s: got r%8.5f x%8.5f y%8.5f z%8.5f expect r%8.5f x%8.5f y%8.5f z%8.5f\n",
71                         where,
72                         got->r, got->x, got->y, got->z,
73                         expect->r, expect->x, expect->y, expect->z);
74                 return 1;
75         }
76         return 0;
77 }
78
79 int main(int argc, char **argv)
80 {
81         struct ao_quaternion    position;
82         struct ao_quaternion    position_expect;
83         struct ao_quaternion    rotation;
84         struct ao_quaternion    rotated;
85         struct ao_quaternion    little_rotation;
86         int                     i;
87         int                     p;
88         int                     ret = 0;
89
90         /* vector */
91         ao_quaternion_init_vector(&position, 1, 1, 1);
92         ao_quaternion_init_vector(&position_expect, -1, -1, 1);
93
94         /* zero rotation */
95         ao_quaternion_init_zero_rotation(&rotation);
96
97 #define dump() do {                                                     \
98                                                                         \
99                 ao_quaternion_rotate(&rotated, &position, &rotation);   \
100                 print_q("rotated", &rotated);                           \
101                 print_q("rotation", &rotation);                         \
102                 printf ("\n");                                          \
103         } while (0)
104
105 //      dump();
106
107         for (p = 0; p < NUM_PATH; p++) {
108                 ao_quaternion_init_half_euler(&little_rotation,
109                                               ao_path[p].x / 2.0f,
110                                               ao_path[p].y / 2.0f,
111                                               ao_path[p].z / 2.0f);
112 //              printf ("\t\tx: %8.4f, y: %8.4f, z: %8.4f ", ao_path[p].x, ao_path[p].y, ao_path[p].z);
113 //              print_q("step", &little_rotation);
114 //              printf("\n");
115                 for (i = 0; i < ao_path[p].steps; i++) {
116                         ao_quaternion_multiply(&rotation, &little_rotation, &rotation);
117
118                         ao_quaternion_normalize(&rotation, &rotation);
119
120 //                      dump();
121                 }
122         }
123
124         ao_quaternion_rotate(&rotated, &position, &rotation);
125
126         ret += check_quaternion("rotation", &rotated, &position_expect);
127
128         struct ao_quaternion    vertical;
129         struct ao_quaternion    angle;
130         struct ao_quaternion    rot;
131
132         ao_quaternion_init_vector(&vertical, 0, 0, 1);
133         ao_quaternion_init_vector(&angle, 0, 0, 1);
134
135         ao_quaternion_init_half_euler(&rot,
136                                       M_PI * 3.0 / 8.0 , 0, 0);
137
138         ao_quaternion_rotate(&angle, &angle, &rot);
139
140         struct ao_quaternion    rot_compute;
141
142         ao_quaternion_vectors_to_rotation(&rot_compute, &vertical, &angle);
143
144         ret += check_quaternion("vector rotation", &rot_compute, &rot);
145
146         struct ao_quaternion    rotd;
147
148         ao_quaternion_rotate(&rotd, &vertical, &rot_compute);
149
150         ret += check_quaternion("vector rotated", &rotd, &angle);
151
152         return ret;
153 }
154