-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tesseract.h
276 lines (225 loc) · 6.63 KB
/
tesseract.h
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#pragma once
#include "lookup_tables.h"
typedef std::vector<std::vector<float>> PointsArray;
typedef std::array<float,4> Coords;
typedef std::array<uint8_t,4> ByteCoords;
typedef std::vector<std::array<int16_t,2>> LinesIndexesArray;
typedef std::vector<int16_t> PointsIndexesArray;
typedef std::array<int16_t,4> Coords4D;
typedef std::array<int16_t,3> Coords3D;
float fov = .75; // field of view for perspective
float baseHScale = 1.25;
float baseVScale = 1.25;
float depthScale = (screenWidth*baseHScale)/50;
float sphereMass = (screenWidth / 50*baseHScale);
static float fps = 0;
static int fpscount;
static unsigned long lastfps = millis();
static int captured = 0;
static unsigned long timetraveller = millis();
static int framesCount = 60; // for the images animation, dynamically replaced
static int16_t zSortedPoints4D[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
static int16_t zSortedPoints4Dbuff[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
static float referenceAngle = 0.0;
static float QuarterPI = PI / 4.0;
static float TwoPi = 2.0* PI;
static float FourPi = 4.0 * PI;
static float scaletobyte = 1.0 / 256.0; // cheap minifloat
static bool linesProcessed = false;
static bool drawing = false;
static bool needsdrawing = false;
static bool dostrobe = true;
static bool drawfps = true;
static Coords tmpCoords;
static PointsArray rot4D = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
static PointsArray rotXY;
static PointsArray rotXZ;
static PointsArray rotYZ;
static PointsArray rotXW;
static PointsArray rotZW;
static PointsArray paXY;
static PointsArray paZW;
static PointsArray paID;
// TODO: use less of those
static PointsArray projection1;
static PointsArray projection2;
static PointsArray projection3;
static PointsArray projection4;
static Coords4D cache4D[16];
static Coords4D cache4Dbuff[16];
static LinesIndexesArray lines4DBuff;
static LinesIndexesArray lines4D;
static PointsIndexesArray points4D;
static RGBColor colorstart;
static RGBColor colorend;
static PointsArray rot3d =
{
{ 1, 0, 0 },
{ 0, (float)romcos(QuarterPI), (float)-romsin(QuarterPI) },
{ 0, (float)romsin(QuarterPI), (float)romcos(QuarterPI) }
};
static bool pointIsIndexed( int16_t point )
{
for(byte i=0;i<points4D.size();i++) {
if( point == points4D[i] ) {
return true;
}
}
return false;
}
static void pointPush( int16_t point )
{
if( ! pointIsIndexed( point ) ) {
points4D.push_back( point );
}
}
static bool lineIsIndexed( std::array<int16_t,2> line )
{
for(byte i=0;i<lines4D.size();i++) {
if( (line[0] == lines4D[i][0] && line[1] == lines4D[i][1])
|| (line[1] == lines4D[i][0] && line[0] == lines4D[i][1])
) {
return true;
}
}
return false;
}
static void linePush( std::array<int16_t,2> line )
{
if( !lineIsIndexed( line ) ) {
lines4D.push_back( line );
}
}
/* sort xyz by z depth */
static void zSortPoints()
{
bool swapped;
int16_t temp;
float zdepth, nextzdepth;
do {
swapped = false;
for(int16_t i=0; i<15; i++ ) {
zdepth = cache4Dbuff[ zSortedPoints4Dbuff[i] ][2];
nextzdepth = cache4Dbuff[ zSortedPoints4Dbuff[i+1] ][2];
if ( zdepth > nextzdepth ) {
temp = zSortedPoints4Dbuff[i];
zSortedPoints4Dbuff[i] = zSortedPoints4Dbuff[i + 1];
zSortedPoints4Dbuff[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
}
static void setCoordsCache( ByteCoords cc, Coords &pout )
{
byte x = cc[0], y=cc[1], z=cc[2], w=cc[3];
int16_t cacheID = x + y*2 + z*4 + w*8;
cache4D[cacheID] = { (int16_t)pout[0], (int16_t)pout[1], (int16_t)pout[2], (int16_t)pout[3] };
}
static bool isInCoordsCache( ByteCoords cc )
{
byte x = cc[0], y=cc[1], z=cc[2], w=cc[3];
int16_t cacheID = x + y*2 + z*4 + w*8;
if( cache4D[cacheID][0] + cache4D[cacheID][1] + cache4D[cacheID][2] + cache4D[cacheID][3] == 0 ) {
return false;
}
return true;
}
static void clearCoordsCache()
{
for( byte i=0;i<16;i++ ) {
cache4D[i] = {0,0,0,0};
}
}
static void multiplyCoords( PointsArray &A, PointsArray &B, PointsArray &C)
{
C.clear();
for (byte i = 0; i < A.size(); i++) {
std::vector<float> mm;
for (byte j = 0; j < B[0].size(); j++) {
float sum = 0;
for (byte k = 0; k < A[0].size(); k++) {
sum += A[i][k] * B[k][j];
}
mm.push_back( sum );
}
C.push_back( mm );
}
}
static void multiplyCoords( PointsArray &A, Coords &B, PointsArray &C)
{
PointsArray CoordsToPointsArray(4);
for(byte i=0;i<B.size();i++) {
CoordsToPointsArray[i].push_back(B[i]);
}
multiplyCoords( A, CoordsToPointsArray, C );
}
static void transformPoint(ByteCoords p0)
{
if( isInCoordsCache( p0 ) ) {
return;
}
for (int i = 0; i < p0.size(); i++) {
tmpCoords[i] = (p0[i] - 0.5);
}
// use the sin/cos lookup table
float romcosrefangle = romcos(referenceAngle);
float romsinrefangle = romsin(referenceAngle);
rotXY = {
{ romcosrefangle, -romsinrefangle, 0, 0},
{ romsinrefangle, romcosrefangle, 0, 0},
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
rotXZ = {
{ romcosrefangle, 0, -romsinrefangle, 0 },
{ romsinrefangle, 0, romcosrefangle, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
rotYZ = {
{ 1, romcosrefangle, -romsinrefangle, 0 },
{ 0, romsinrefangle, romcosrefangle, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
rotXW = {
{ romcosrefangle, 0, 0, -romsinrefangle },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ romsinrefangle, 0, 0, romcosrefangle }
};
rotZW = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, romcosrefangle, -romsinrefangle },
{ 0, 0, romsinrefangle, romcosrefangle }
};
multiplyCoords( rotXY, tmpCoords, paXY );
multiplyCoords( rotZW, paXY, paZW );
multiplyCoords( rot4D, paZW, projection2 );
/*
//multiplyCoords( rotXY, tmpCoords, paXY );
multiplyCoords( rotZW, tmpCoords, paZW );
multiplyCoords( rot4D, paZW, projection2 );
*/
/*
multiplyCoords( rotXY, tmpCoords, paXY );
multiplyCoords( rotZW, paXY, paZW );
multiplyCoords( rot4D, paZW, projection2 );
*/
float distance = 3;
float w = 1 / (distance - projection2[3][0]);
projection1 = {
{ w, 0, 0, 0 },
{ 0, w, 0, 0 },
{ 0, 0, w, 0 }
};
multiplyCoords( projection1, projection2, projection3); // project 4d to 3d
multiplyCoords( rot3d, projection3, projection4); // project 3d to 2d
// add some depth of field
projection4[0][0] += fov*projection4[0][0]*projection4[2][0];
projection4[1][0] += fov*projection4[1][0]*projection4[2][0];
Coords pout = {((projection4[0][0] * screenWidth ) * baseHScale)+screenWidth/2, ((projection4[1][0] * screenHeight) * baseVScale)+screenHeight/2, (projection4[2][0]*256)+127 };
setCoordsCache( p0, pout );
}