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 File Reference

Mathematic utilities. More...

#include <math.h>
#include "Gpu_Hal.h"
#include "Maths.h"

Go to the source code of this file.

Macros

#define doubleswap(x, y)   {double z = x;x=y;y=z;}
 Use PROGMEM.
 
#define xyzswap(x, y)   {Math_3d_Xyz_t z = x;x=y;y=z;}
 

Functions

int16_t Math_Qsin (uint16_t a)
 Sin function.
 
int16_t Math_Qcos (uint16_t a)
 Cos function.
 
void Math_Polarxy (int32_t r, float th, int32_t *x, int32_t *y, int32_t ox, int32_t oy)
 Polar function.
 
float Math_Da (float i, int16_t degree)
 Da function 1 uint = 0.5 degree. 1 circle = 720 degree.
 
float Math_Power (float x, unsigned int y)
 
uint32_t Math_Points_Distance (uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
 
uint32_t Math_Points_Nearby_NextX (uint32_t x1, uint32_t y1, uint32_t y2, uint32_t Distance)
 
void Normalise (Math_3d_Xyz_t *p)
 
Math_3d_Xyz_t Math_3D_ArbitraryRotate (Math_3d_Xyz_t p, double theta, Math_3d_Xyz_t r)
 
Math_3d_Xyz_t Math_3D_ArbitraryRotate2 (Math_3d_Xyz_t p, double theta, Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
 
static Math_3d_Xyz_t subVector (Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
 
static Math_3d_Xyz_t normalVector (Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
 
static double dotProduct (Math_3d_Xyz_t a, Math_3d_Xyz_t b)
 
int Math_3D_Backface_Find_Visible (Math_3d_Face_t face, Math_3d_Xyz_t view)
 

Variables

PROGMEM prog_uint16_t sintab []
 

Detailed Description

Mathematic utilities.

Author
Tuan Nguyen tuan..nosp@m.nguy.nosp@m.en@br.nosp@m.tchi.nosp@m.p.com
Date
2019

MIT License

Copyright (c) [2019] [Bridgetek Pte Ltd (BRTChip)]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Definition in file Maths.c.

Macro Definition Documentation

◆ doubleswap

#define doubleswap (   x,
 
)    {double z = x;x=y;y=z;}

Use PROGMEM.

Definition at line 37 of file Maths.c.

◆ xyzswap

#define xyzswap (   x,
 
)    {Math_3d_Xyz_t z = x;x=y;y=z;}

Definition at line 38 of file Maths.c.

Function Documentation

◆ dotProduct()

static double dotProduct ( Math_3d_Xyz_t  a,
Math_3d_Xyz_t  b 
)
static

Definition at line 247 of file Maths.c.

247 {
248 return a.x * b.x + a.y * b.y + a.z * b.z;
249}
static uint32_t a
Definition Common.c:36
static uint32_t b
Definition Common.c:37

◆ Math_3D_ArbitraryRotate()

Math_3d_Xyz_t Math_3D_ArbitraryRotate ( Math_3d_Xyz_t  p,
double  theta,
Math_3d_Xyz_t  r 
)

Definition at line 161 of file Maths.c.

161 {
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}
void Normalise(Math_3d_Xyz_t *p)
Definition Maths.c:137
double x
Definition Maths.h:40
double z
Definition Maths.h:40
double y
Definition Maths.h:40

◆ Math_3D_ArbitraryRotate2()

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 at line 192 of file Maths.c.

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}

◆ Math_3D_Backface_Find_Visible()

int Math_3D_Backface_Find_Visible ( Math_3d_Face_t  face,
Math_3d_Xyz_t  view 
)

Definition at line 251 of file Maths.c.

251 {
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}
static Math_3d_Xyz_t subVector(Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
Definition Maths.c:228
static double dotProduct(Math_3d_Xyz_t a, Math_3d_Xyz_t b)
Definition Maths.c:247
static Math_3d_Xyz_t normalVector(Math_3d_Xyz_t p1, Math_3d_Xyz_t p2)
Definition Maths.c:237
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

◆ Math_Da()

float Math_Da ( float  i,
int16_t  degree 
)

Da function 1 uint = 0.5 degree. 1 circle = 720 degree.

Parameters
i
degree
Returns
float

Definition at line 111 of file Maths.c.

112{
113 return (i - degree) * 32768 / 360;
114}

◆ Math_Points_Distance()

uint32_t Math_Points_Distance ( uint32_t  x1,
uint32_t  y1,
uint32_t  x2,
uint32_t  y2 
)

Definition at line 126 of file Maths.c.

126 {
127 return (uint32_t)sqrt((double)(Math_Power((float)(x2 - x1), 2) + Math_Power((float)(y2 - y1), 2)));
128}
unsigned int uint32_t
float Math_Power(float x, unsigned int y)
Definition Maths.c:116

◆ Math_Points_Nearby_NextX()

uint32_t Math_Points_Nearby_NextX ( uint32_t  x1,
uint32_t  y1,
uint32_t  y2,
uint32_t  Distance 
)

Definition at line 130 of file Maths.c.

130 {
131 return (uint32_t)sqrt((double)Math_Power((float)Distance, 2) - Math_Power((float)abs(y2 - y1), 2)) + x1;
132}

◆ Math_Polarxy()

void Math_Polarxy ( int32_t  r,
float  th,
int32_t x,
int32_t y,
int32_t  ox,
int32_t  oy 
)

Polar function.

Parameters
r
th
x
y
ox
oy

Definition at line 97 of file Maths.c.

97 {
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}
unsigned short uint16_t
int16_t Math_Qsin(uint16_t a)
Sin function.
Definition Maths.c:58
int16_t Math_Qcos(uint16_t a)
Cos function.
Definition Maths.c:82

◆ Math_Power()

float Math_Power ( float  x,
unsigned int  y 
)

Definition at line 116 of file Maths.c.

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}

◆ Math_Qcos()

int16_t Math_Qcos ( uint16_t  a)

Cos function.

Parameters
aAngle
Returns
int16_t

Definition at line 82 of file Maths.c.

83{
84 return (Math_Qsin(a + 16384));
85}

◆ Math_Qsin()

int16_t Math_Qsin ( uint16_t  a)

Sin function.

Parameters
aAngle
Returns
int16_t

Definition at line 58 of file Maths.c.

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}
static uint32_t f
Definition Common.c:41
int int32_t
short int16_t
unsigned char uint8_t
#define pgm_read_word(addr)
Definition Gpu_Hal.h:467
PROGMEM prog_uint16_t sintab[]
Definition Maths.c:41

◆ Normalise()

void Normalise ( Math_3d_Xyz_t p)

Definition at line 137 of file Maths.c.

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}
static ft_uint32_t ft_uint8_t ft_uint32_t length
Definition FT_Gpu_Hal.h:140

◆ normalVector()

static Math_3d_Xyz_t normalVector ( Math_3d_Xyz_t  p1,
Math_3d_Xyz_t  p2 
)
static

Definition at line 237 of file Maths.c.

237 {
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}

◆ subVector()

static Math_3d_Xyz_t subVector ( Math_3d_Xyz_t  p1,
Math_3d_Xyz_t  p2 
)
static

Definition at line 228 of file Maths.c.

228 {
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}

Variable Documentation

◆ sintab

Initial value:
= {
0, 402, 804, 1206, 1607, 2009, 2410, 2811, 3211, 3611, 4011, 4409, 4807, 5205, 5601, 5997, 6392,
6786, 7179, 7571, 7961, 8351, 8739, 9126, 9511, 9895, 10278, 10659, 11038, 11416, 11792, 12166, 12539,
12909, 13278, 13645, 14009, 14372, 14732, 15090, 15446, 15799, 16150, 16499, 16845, 17189, 17530, 17868,
18204, 18537, 18867, 19194, 19519, 19840, 20159, 20474, 20787, 21096, 21402, 21705, 22004, 22301, 22594,
22883, 23169, 23452, 23731, 24006, 24278, 24546, 24811, 25072, 25329, 25582, 25831, 26077, 26318, 26556, 26789,
27019, 27244, 27466, 27683, 27896, 28105, 28309, 28510, 28706, 28897, 29085, 29268, 29446, 29621, 29790, 29955,
30116, 30272, 30424, 30571, 30713, 30851, 30984, 31113, 31236, 31356, 31470, 31580, 31684, 31785, 31880, 31970,
32056, 32137, 32213, 32284, 32350, 32412, 32468, 32520, 32567, 32609, 32646, 32678, 32705, 32727, 32744, 32757,
32764, 32767, 32764 }

Definition at line 41 of file Maths.c.

41 {
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 };