gitignore: Updated
[simavr] / examples / shared / libc3 / src / c3algebra.h
1 /*
2         c3algebra.h
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         Derivative and inspiration from original C++:
7         Paul Rademacher & Jean-Francois DOUEG,
8
9         This file is part of simavr.
10
11         simavr is free software: you can redistribute it and/or modify
12         it under the terms of the GNU General Public License as published by
13         the Free Software Foundation, either version 3 of the License, or
14         (at your option) any later version.
15
16         simavr is distributed in the hope that it will be useful,
17         but WITHOUT ANY WARRANTY; without even the implied warranty of
18         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19         GNU General Public License for more details.
20
21         You should have received a copy of the GNU General Public License
22         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #ifndef __C3ALGEBRA_H___
27 #define __C3ALGEBRA_H___
28
29
30 #ifndef M_PI
31 #define M_PI 3.141592654
32 #endif
33
34 enum {VX, VY, VZ, VW};           // axes
35 enum {PA, PB, PC, PD};           // planes
36 enum {RED, GREEN, BLUE, ALPHA};  // colors
37 enum {KA, KD, KS, ES};           // phong coefficients
38
39 typedef float c3f;
40 typedef c3f (*V_FCT_PTR)(c3f);
41
42 typedef union c3vec2 {
43         struct { c3f x,y; };
44         c3f n[2];
45 } c3vec2;
46
47 typedef union c3vec3 {
48         struct { c3f x,y,z; };
49         c3f n[3];
50 } c3vec3;
51
52 typedef union c3vec4 {
53         struct { c3f x,y,z,w; };
54         c3f n[4];
55 } c3vec4, * c3vec4p;
56
57 typedef union c3mat3 {
58         c3vec3 v[3];
59         c3f n[3*3];
60 } c3mat3, * c3mat3p;
61
62 typedef union c3mat4 {
63         c3vec4 v[4];
64         c3f n[4*4];
65 } c3mat4, * c3mat4p;
66
67 /*
68  * c3vec2 related
69  */
70
71 c3vec2  c3vec2_zero();
72 c3vec2  c3vec2f(c3f x, c3f y);
73
74 c3vec2  c3vec2_add(c3vec2 a, const c3vec2 v);
75 c3vec2  c3vec2_sub(c3vec2 a, const c3vec2 v);
76 c3vec2  c3vec2_mulf(c3vec2 a, c3f d);
77 c3vec2  c3vec2_divf(c3vec2 a, c3f d);
78
79 c3f             c3vec2_length2(const c3vec2 a);
80 c3f             c3vec2_length(const c3vec2 a);
81 c3vec2  c3vec2_normalize(const c3vec2 a); // it is up to caller to avoid divide-by-zero
82 c3vec2  c3vec2_apply(c3vec2 a, V_FCT_PTR fct);
83 c3vec2  c3vec2_minus(const c3vec2 a);
84 c3f             c3vec2_dot(const c3vec2 a, const c3vec2 b);
85 c3vec2  c3vec2_min(const c3vec2 a, const c3vec2 b);
86 c3vec2  c3vec2_max(const c3vec2 a, const c3vec2 b);
87 c3vec2  c3vec2_prod(const c3vec2 a, const c3vec2 b);
88
89 /*
90  * c3vec4 related
91  */
92
93 c3vec3  c3vec3_zero();
94 c3vec3  c3vec3f(c3f x, c3f y, c3f z);
95 c3vec3  c3vec3_vec2f(const c3vec2 v, c3f d);
96 c3vec3  c3vec3_vec2(const c3vec2 v);
97 c3vec3  c3vec3_vec4(const c3vec4 v); // it is up to caller to avoid divide-by-zero
98
99 c3vec3  c3vec3_add(const c3vec3 a, const c3vec3 v);
100 c3vec3  c3vec3_sub(const c3vec3 a, const c3vec3 v);
101 c3vec3  c3vec3_mulf(const c3vec3 a, c3f d);
102 c3vec3  c3vec3_divf(const c3vec3 a, c3f d);
103
104 c3f             c3vec3_length2(const c3vec3 a);
105 c3f             c3vec3_length(const c3vec3 a);
106 c3vec3  c3vec3_normalize(const c3vec3 a); // it is up to caller to avoid divide-by-zero
107 c3vec3  c3vec3_homogenize(c3vec3 a); // it is up to caller to avoid divide-by-zero
108 c3vec3  c3vec3_apply(c3vec3 a, V_FCT_PTR fct);
109 c3vec3  c3vec3_minus(const c3vec3 a);
110 c3f             c3vec3_dot(const c3vec3 a, const c3vec3 b);
111 int             c3vec3_equal(const c3vec3 a, const c3vec3 b);
112 c3vec3  c3vec3_min(const c3vec3 a, const c3vec3 b);
113 c3vec3  c3vec3_max(const c3vec3 a, const c3vec3 b);
114 c3vec3  c3vec3_prod(const c3vec3 a, const c3vec3 b);
115
116 c3vec3  c3vec3_cross(const c3vec3 a, const c3vec3 b);
117 c3vec3  c3vec2_cross(const c3vec2 a, const c3vec2 b);
118
119 /*
120  * c3vec4 related
121  */
122
123 c3vec4  c3vec4_zero();
124 c3vec4  c3vec4f(c3f x, c3f y, c3f z, c3f w);
125 c3vec4  c3vec4_vec3(const c3vec3 v);
126 c3vec4  c3vec4_vec3f(const c3vec3 v, c3f d);
127
128 c3vec4  c3vec4_add(c3vec4 a, const c3vec4 v);
129 c3vec4  c3vec4_sub(c3vec4 a, const c3vec4 v);
130 c3vec4  c3vec4_mulf(c3vec4 a, c3f d);
131 c3vec4  c3vec4_divf(c3vec4 a, c3f d);
132
133 c3f             c3vec4_length2(const c3vec4 a);
134 c3f             c3vec4_length(const c3vec4 a);
135 c3vec4  c3vec4_normalize(c3vec4 a); // it is up to caller to avoid divide-by-zero
136 c3vec4  c3vec4_homogenize(c3vec4 a); // it is up to caller to avoid divide-by-zero
137 c3vec4  c3vec4_apply(c3vec4 a, V_FCT_PTR fct);
138 c3vec4  c3vec4_minus(const c3vec4 a);
139 int             c3vec4_equal(const c3vec4 a, const c3vec4 b);
140 c3vec4  c3vec4_min(const c3vec4 a, const c3vec4 b);
141 c3vec4  c3vec4_max(const c3vec4 a, const c3vec4 b);
142 c3vec4  c3vec4_prod(const c3vec4 a, const c3vec4 b);
143
144 /*
145  * c3mat3 related
146  */
147
148 c3mat3  c3mat3_identity();
149 c3mat3  c3mat3_vec3(const c3vec3 v0, const c3vec3 v1, const c3vec3 v2);
150 c3mat3p c3mat3_add(const c3mat3p a, const c3mat3p m);
151 c3mat3p c3mat3_sub(const c3mat3p a, const c3mat3p m);
152 c3mat3p c3mat3_mulf(const c3mat3p a, c3f d);
153 c3mat3p c3mat3_divf(const c3mat3p a, c3f d);
154
155 c3mat3  c3mat3_transpose(const c3mat3p a);
156 c3mat3  c3mat3_inverse(const c3mat3p m);   // Gauss-Jordan elimination with partial pivoting
157 c3mat3p c3mat3_apply(c3mat3p a, V_FCT_PTR fct);
158 c3mat3  c3mat3_minus(const c3mat3p a);
159
160 c3mat3  c3mat3_mul(const c3mat3p a, const c3mat3p b);
161 int             c3mat3_equal(const c3mat3p a, const c3mat3p b);
162
163 c3vec2  c3mat3_mulv2(const c3mat3p a, const c3vec2 v);
164 c3vec3  c3mat3_mulv3(const c3mat3p a, const c3vec3 v);
165 c3vec2  c3vec2_mulm3(const c3vec2 v, const c3mat3p a);
166 c3vec3  c3vec3_mulm3(const c3vec3 v, const c3mat3p a);
167
168 c3mat3  identity2D();
169 c3mat3  translation2D(const c3vec2 v);
170 c3mat3  rotation2D(const c3vec2 Center, c3f angleDeg);
171 c3mat3  scaling2D(const c3vec2 scaleVector);
172
173 /*
174  * c3mat4 related
175  */
176
177 c3mat4  c3mat4_identity();
178 c3mat4  c3mat4_vec4(const c3vec4 v0, const c3vec4 v1, const c3vec4 v2, const c3vec4 v3);
179 c3mat4  c3mat4f(
180      c3f a00, c3f a01, c3f a02, c3f a03,
181      c3f a10, c3f a11, c3f a12, c3f a13,
182      c3f a20, c3f a21, c3f a22, c3f a23,
183      c3f a30, c3f a31, c3f a32, c3f a33 );
184
185 c3mat4  c3mat4_minus(const c3mat4p a);
186 c3mat4p c3mat4p_add(c3mat4p a, const c3mat4p m);
187 c3mat4  c3mat4_add(const c3mat4p a, const c3mat4p b);
188 c3mat4p c3mat4p_sub(c3mat4p a, const c3mat4p m);
189 c3mat4  c3mat4_sub(const c3mat4p a, const c3mat4p b);
190 c3mat4p c3mat4p_mulf(c3mat4p a, c3f d);
191 c3mat4  c3mat4_mulf(const c3mat4p a, c3f d);
192 c3mat4  c3mat4_mul(const c3mat4p a, const c3mat4p b);
193 c3mat4p c3mat4p_divf(c3mat4p a, c3f d);
194 c3mat4  c3mat4_divf(const c3mat4p a, c3f d);
195
196 c3mat4  c3mat4_transpose(const c3mat4p a);
197 c3mat4  c3mat4_inverse(const c3mat4p m);       // Gauss-Jordan elimination with partial pivoting
198 c3mat4p c3mat4p_apply(c3mat4p a, V_FCT_PTR fct);
199 c3mat4p c3mat4p_swap_rows(c3mat4p a, int i, int j);
200 c3mat4p c3mat4p_swap_cols(c3mat4p a, int i, int j);
201 int             c3mat4_equal(const c3mat4p a, const c3mat4p b);
202
203 c3vec4  c3vec4_mulm4(const c3vec4 v, const c3mat4p a);
204 c3vec4  c3mat4_mulv4(const c3mat4p a, const c3vec4 v);
205 c3vec3  c3mat4_mulv3(const c3mat4p a, const c3vec3 v);
206
207 c3mat4  identity3D();
208 c3mat4  translation3D(const c3vec3 v);
209 c3mat4  rotation3D(const c3vec3 Axis, c3f angleDeg);
210 c3mat4  rotation3Drad(const c3vec3 Axis, c3f angleRad);
211 c3mat4  scaling3D(const c3vec3 scaleVector);
212 c3mat4  perspective3D(c3f d);
213
214 #endif /* __C3ALGEBRA_H___ */