image/svg+xml
Brteve's API for EveApps
Version 0.0.1
The reference document for common folder of EveApps project.
 
Loading...
Searching...
No Matches
Maths.c
Go to the documentation of this file.
1
32#include <math.h>
33
34#include "Gpu_Hal.h"
35#include "Maths.h"
36
37#define doubleswap(x, y) {double z = x;x=y;y=z;}
38#define xyzswap(x, y) {Math_3d_Xyz_t z = x;x=y;y=z;}
39
40 /* Optimized implementation of sin and cos table - precision is 16 bit */
42 0, 402, 804, 1206, 1607, 2009, 2410, 2811, 3211, 3611, 4011, 4409, 4807, 5205, 5601, 5997, 6392,
43 6786, 7179, 7571, 7961, 8351, 8739, 9126, 9511, 9895, 10278, 10659, 11038, 11416, 11792, 12166, 12539,
44 12909, 13278, 13645, 14009, 14372, 14732, 15090, 15446, 15799, 16150, 16499, 16845, 17189, 17530, 17868,
45 18204, 18537, 18867, 19194, 19519, 19840, 20159, 20474, 20787, 21096, 21402, 21705, 22004, 22301, 22594,
46 22883, 23169, 23452, 23731, 24006, 24278, 24546, 24811, 25072, 25329, 25582, 25831, 26077, 26318, 26556, 26789,
47 27019, 27244, 27466, 27683, 27896, 28105, 28309, 28510, 28706, 28897, 29085, 29268, 29446, 29621, 29790, 29955,
48 30116, 30272, 30424, 30571, 30713, 30851, 30984, 31113, 31236, 31356, 31470, 31580, 31684, 31785, 31880, 31970,
49 32056, 32137, 32213, 32284, 32350, 32412, 32468, 32520, 32567, 32609, 32646, 32678, 32705, 32727, 32744, 32757,
50 32764, 32767, 32764 };
51
59{
60 uint8_t f;
61 int16_t s0;
62 int16_t s1;
63
64 if (a & 32768) {
65 return -Math_Qsin(a & 32767);
66 }
67 if (a & 16384) {
68 a = 32768 - a;
69 }
70 f = a & 127;
71 s0 = pgm_read_word(sintab + (a >> 7));
72 s1 = pgm_read_word(sintab + (a >> 7) + 1);
73 return (int16_t)(s0 + (int16_t)((int32_t)f * (s1 - s0) >> 7));
74}
75
83{
84 return (Math_Qsin(a + 16384));
85}
86
97void Math_Polarxy(int32_t r, float th, int32_t* x, int32_t* y, int32_t ox, int32_t oy) {
98 *x = (16 * ox) + (((long)r * Math_Qsin((uint16_t)th)) >> 11) + 16;
99 *y = (16 * oy) - (((long)r * Math_Qcos((uint16_t)th)) >> 11);
100}
101
111float Math_Da(float i, int16_t degree)
112{
113 return (i - degree) * 32768 / 360;
114}
115
116float Math_Power(float x, unsigned int y)
117{
118 if (y == 0)
119 return 1;
120 else if (y % 2 == 0)
121 return Math_Power(x, y / 2) * Math_Power(x, y / 2);
122 else
123 return x * Math_Power(x, y / 2) * Math_Power(x, y / 2);
124}
125
127 return (uint32_t)sqrt((double)(Math_Power((float)(x2 - x1), 2) + Math_Power((float)(y2 - y1), 2)));
128}
129
131 return (uint32_t)sqrt((double)Math_Power((float)Distance, 2) - Math_Power((float)abs(y2 - y1), 2)) + x1;
132}
133
134/*
135 Normalise a vector
136*/
138{
139 double length;
140
141 length = sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
142 if (length != 0) {
143 p->x /= length;
144 p->y /= length;
145 p->z /= length;
146 }
147 else {
148 p->x = 0;
149 p->y = 0;
150 p->z = 0;
151 }
152}
153
154/*
155 Rotate a point p by angle theta around an arbitrary axis r
156 Return the rotated point.
157 Positive angles are anticlockwise looking down the axis
158 towards the origin.
159 Assume right hand coordinate system.
160*/
162 Math_3d_Xyz_t q = { 0.0,0.0,0.0 };
163 double costheta;
164 double sintheta;
165
166 Normalise(&r);
167 costheta = cos(theta);
168 sintheta = sin(theta);
169
170 q.x += (costheta + (1 - costheta) * r.x * r.x) * p.x;
171 q.x += ((1 - costheta) * r.x * r.y - r.z * sintheta) * p.y;
172 q.x += ((1 - costheta) * r.x * r.z + r.y * sintheta) * p.z;
173
174 q.y += ((1 - costheta) * r.x * r.y + r.z * sintheta) * p.x;
175 q.y += (costheta + (1 - costheta) * r.y * r.y) * p.y;
176 q.y += ((1 - costheta) * r.y * r.z - r.x * sintheta) * p.z;
177
178 q.z += ((1 - costheta) * r.x * r.z - r.y * sintheta) * p.x;
179 q.z += ((1 - costheta) * r.y * r.z + r.x * sintheta) * p.y;
180 q.z += (costheta + (1 - costheta) * r.z * r.z) * p.z;
181
182 return(q);
183}
184
185/*
186 Rotate a point p by angle theta around an arbitrary line segment p1-p2
187 Return the rotated point.
188 Positive angles are anticlockwise looking down the axis
189 towards the origin.
190 Assume right hand coordinate system.
191*/
193{
194 Math_3d_Xyz_t q = { 0.0,0.0,0.0 };
195 double costheta;
196 double sintheta;
198
199 r.x = p2.x - p1.x;
200 r.y = p2.y - p1.y;
201 r.z = p2.z - p1.z;
202 p.x -= p1.x;
203 p.y -= p1.y;
204 p.z -= p1.z;
205 Normalise(&r);
206
207 sintheta = sin(theta);
208 costheta = cos(theta);
209
210 q.y += ((1 - costheta) * r.x * r.y + r.z * sintheta) * p.x;
211 q.y += (costheta + (1 - costheta) * r.y * r.y) * p.y;
212 q.y += ((1 - costheta) * r.y * r.z - r.x * sintheta) * p.z;
213
214 q.x += (costheta + (1 - costheta) * r.x * r.x) * p.x;
215 q.x += ((1 - costheta) * r.x * r.y - r.z * sintheta) * p.y;
216 q.x += ((1 - costheta) * r.x * r.z + r.y * sintheta) * p.z;
217
218 q.z += ((1 - costheta) * r.x * r.z - r.y * sintheta) * p.x;
219 q.z += ((1 - costheta) * r.y * r.z + r.x * sintheta) * p.y;
220 q.z += (costheta + (1 - costheta) * r.z * r.z) * p.z;
221
222 q.x += p1.x;
223 q.y += p1.y;
224 q.z += p1.z;
225 return(q);
226}
227
229 Math_3d_Xyz_t ret;
230 ret.x = p1.x - p2.x;
231 ret.y = p1.y - p2.y;
232 ret.z = p1.z - p2.z;
233
234 return ret;
235}
236
239
240 n.x = p1.y * p2.z - p1.z * p2.y;
241 n.y = p1.z * p2.x - p1.x * p2.z;
242 n.z = p1.x * p2.y - p1.y * p2.x;
243
244 return n;
245}
246
248 return a.x * b.x + a.y * b.y + a.z * b.z;
249}
250
252 Math_3d_Xyz_t v12 = subVector(face.p2, face.p1);
253 Math_3d_Xyz_t v13 = subVector(face.p3, face.p1);
254 Math_3d_Xyz_t N = normalVector(v12, v13);
255 Math_3d_Xyz_t S = subVector(view, face.p1);
256
257 double Dot = dotProduct(N, S);
258
259 return Dot >= 0;
260}
261
static uint32_t a
Definition Common.c:36
static uint32_t b
Definition Common.c:37
static uint32_t f
Definition Common.c:41
unsigned short uint16_t
int int32_t
unsigned int uint32_t
short int16_t
unsigned char uint8_t
static ft_uint32_t ft_uint8_t ft_uint32_t length
Definition FT_Gpu_Hal.h:140
This file defines the generic APIs of phost access layer for the FT800 or EVE compatible silicon....
#define PROGMEM
Definition Gpu_Hal.h:72
#define pgm_read_word(addr)
Definition Gpu_Hal.h:467
Math_3d_Xyz_t Math_3D_ArbitraryRotate2(Math_3d_Xyz_t p, double theta, Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
Definition Maths.c:192
Math_3d_Xyz_t Math_3D_ArbitraryRotate(Math_3d_Xyz_t p, double theta, Math_3d_Xyz_t r)
Definition Maths.c:161
void Normalise(Math_3d_Xyz_t *p)
Definition Maths.c:137
static Math_3d_Xyz_t subVector(Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
Definition Maths.c:228
void Math_Polarxy(int32_t r, float th, int32_t *x, int32_t *y, int32_t ox, int32_t oy)
Polar function.
Definition Maths.c:97
int16_t Math_Qsin(uint16_t a)
Sin function.
Definition Maths.c:58
static double dotProduct(Math_3d_Xyz_t a, Math_3d_Xyz_t b)
Definition Maths.c:247
uint32_t Math_Points_Nearby_NextX(uint32_t x1, uint32_t y1, uint32_t y2, uint32_t Distance)
Definition Maths.c:130
static Math_3d_Xyz_t normalVector(Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
Definition Maths.c:237
int16_t Math_Qcos(uint16_t a)
Cos function.
Definition Maths.c:82
int Math_3D_Backface_Find_Visible(Math_3d_Face_t face, Math_3d_Xyz_t view)
Definition Maths.c:251
PROGMEM prog_uint16_t sintab[]
Definition Maths.c:41
float Math_Power(float x, unsigned int y)
Definition Maths.c:116
float Math_Da(float i, int16_t degree)
Da function 1 uint = 0.5 degree. 1 circle = 720 degree.
Definition Maths.c:111
uint32_t Math_Points_Distance(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
Definition Maths.c:126
Mathematic utilities.
PROGMEM const uint16_t prog_uint16_t
Definition Platform.h:208
Math_3d_Xyz_t p2
Definition Maths.h:44
Math_3d_Xyz_t p1
Definition Maths.h:44
Math_3d_Xyz_t p3
Definition Maths.h:44
double x
Definition Maths.h:40
double z
Definition Maths.h:40
double y
Definition Maths.h:40