-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglextra.c
276 lines (228 loc) · 7.36 KB
/
glextra.c
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
276
#include "glextra.h"
#include "gl.h"
#include "timer.h"
// #include "printf.h"
#include "malloc.h"
#include "math.h"
struct Circle* head;
struct Line* head_width;
struct Point* leftArmLength;
struct Point* rightArmLength;
// struct Line* liny;
#define ROOM_TEMP 22
#define HEAD_RADIUS 5
#define HEAD_ERROR_MARGIN 1
#define BODY_WIDTH_MARGIN 2
#define BODY_LENGTH_MARGIN 3
#define THERMAL_BACKGROUND_COLOR GL_WHITE
#define abs(x) ((x) > (0) ? (x) : (0))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
#define ARM_MARGIN 5
void gl_draw_circle(int xCenter, int yCenter, int radius, color_t color) {
int x = radius;
int y = 0;
//draw 4 outerpoints
gl_draw_pixel(xCenter+x, yCenter+y, color);
gl_draw_pixel(xCenter+y, yCenter+x, color);
gl_draw_pixel(xCenter-x, yCenter-y, color);
gl_draw_pixel(xCenter-y, yCenter-x, color);
int P = (1-radius);
while (x > y) {
y++;
if (P <= 0) {
P += (2*y + 1);
} else {
x--;
P += (2*y - 2*x + 1);
}
if (x < y) {
break;
}
//draw point in all four quadrants
gl_draw_pixel(xCenter+x, yCenter+y, color);
gl_draw_pixel(xCenter-x, yCenter+y, color);
gl_draw_pixel(xCenter+x, yCenter-y, color);
gl_draw_pixel(xCenter-x, yCenter-y, color);
if (x != y) {
//draw reflected point over y=x in all four quadrants
gl_draw_pixel(xCenter+y, yCenter+x, color);
gl_draw_pixel(xCenter-y, yCenter+x, color);
gl_draw_pixel(xCenter+y, yCenter-x, color);
gl_draw_pixel(xCenter-y, yCenter-x, color);
}
}
}
void drawCircle(struct Circle circle, color_t color) {
struct Point circleCenter = circle.center;
int circleRad = circle.radius;
gl_draw_circle(circleCenter.x, circleCenter.y, circleRad, color);
}
void drawLine(struct Line line, color_t color) {
struct Point start = line.one;
struct Point end = line.two;
gl_draw_line(start.x, start.y, end.x, end.y, color);
}
static void checkHead(struct Point newHead) {
// if ( abs(newHead.x - head->center.x) >= 1) {
head->center.x = newHead.x;
// }
// if ( abs(newHead.y - head->center.y) >= 1 ) {
head->center.y = newHead.y;
// }
}
// struct Circle* calculateHead(const short* data, int rowSize){
static struct Point* findArm(const short* data, int rowSize, struct Point start, int mode) {
struct Point* extreme = (struct Point *)malloc(sizeof(struct Point *));
int maxLen = 0;
int row;
// /*to test*/printf("startFindArm\n");
// for (int row = start.y; row < rowSize; row++) {
for (row = start.y; row < min(row + ARM_MARGIN, rowSize); row++) {
// printf("row %d\n", row);
int col = start.x;
int len = 0;
//find MIN
if (mode < 0) {
while ((col >= 0) && (data[row*rowSize + col]/4 > ROOM_TEMP)) {
col--;
len++;
}
}
//find MAX
else {
if (mode > 0) {
//move down to warm
while ((col >= 0) && (data[row*rowSize + col]/4 <= ROOM_TEMP)) {
col--;
}
if ((head->center.x >= 0) && (col >= head->center.x)) {
while ( (col < rowSize) && (data[row*rowSize + col]/4 > ROOM_TEMP)) {
col++;
len++;
}
}
}
}
if (len > maxLen) {
maxLen = len;
if (mode < 0) {
extreme->x = (col+1);
} else {
extreme->x = (col-1);
}
extreme->y = row;
}
}
return extreme;
}
struct Line* calculateArms(const short* data, int rowSize){
// printf("start\n");
struct Line* maxMinLine = (struct Line *)malloc(sizeof(struct Line *));
// struct Point* search = data[];
// head_width->one = (struct Point){start, row};
// head_width->two = (struct Point){end, row};
if ((head->center.x < 0) || (head->center.y < 0)) {
leftArmLength->x = -1;
leftArmLength->y = -1;
rightArmLength->x = -1;
rightArmLength->y = -1;
maxMinLine->one = (struct Point){leftArmLength->x, leftArmLength->y};
maxMinLine->two = (struct Point){rightArmLength->x, rightArmLength->y};
return maxMinLine;
}
else {
// find min
// printf("malloc\n");
struct Point min = head_width->one;//data[start+row*rowSize];
leftArmLength = findArm(data, rowSize, min, -1);
// printf("FOUND LEFT ARM!\n");
// static struct Point* findArm(const short* data, int rowSize, struct Point start, int mode) {
// // struct Point* findLongestPath(const short* data, Point pos, int rowSize, int mode) {
//find max
struct Point max = head_width->two;//data[end+row*rowSize];
rightArmLength = findArm(data, rowSize, max, 1);
// printf("FOUND RIGHT ARM!\n");
// // struct Line* minMaxLine;
// // minMaxLine->one = (struct Point){leftArmLength->x, leftArmLength->y};
// // minMaxLine->two = (struct Point){rightArmLength->x, rightArmLength->y};
}
// return &(struct Line){*leftArmLength, *rightArmLength};
maxMinLine->one = (struct Point){leftArmLength->x, leftArmLength->y};
maxMinLine->two = (struct Point){rightArmLength->x, rightArmLength->y};
return maxMinLine;
}
struct Circle* calculateHead(const short* data, int rowSize){
// /*to test*/printf("start head\n");
head->radius = HEAD_RADIUS;
int row;
for (row = 0; row < rowSize; row++) {
int start = -1;
int end = -1;
for (int col = 0; col < rowSize; col++) {
if(*data/4 > ROOM_TEMP){ //warm pixel
end = col;
if (start < 0) {
start = col;
}
} else {
if ((start > 0) && (end > 0)) { //red then blue
// /*to test*/printf("block. ");
if ((end - start) >= BODY_WIDTH_MARGIN) {
// /*to test*/printf("long. ");
//CHECK THE OTHER ROW FOR WARM BLOCK
int checkRow = (row + BODY_LENGTH_MARGIN);
int checkStart = -1;
int checkEnd = -1;
for (int checkCol = 0; checkCol < rowSize; checkCol++) {
if (data[(checkRow*rowSize )+ checkCol]/4 > ROOM_TEMP) {
// /*to test*/printf("c%d, ", checkRow*rowSize + checkCol);
checkEnd = checkCol;
if (checkStart < 0) {
checkStart = checkCol;
}
}
}
if ((checkEnd - checkStart) >= BODY_WIDTH_MARGIN) {
int headX = (rowSize - (start+end)/2);
int headY = (row + HEAD_RADIUS);
struct Point headCenter = { headX, headY};
//for finding arms
head_width->one = (struct Point){start, row};
head_width->two = (struct Point){end, row};
// /*to test*/printf("HEAD at (%d,%d)\n", headX, headY);
// head->center = headCenter;
checkHead(headCenter);
return head;
}
}
}
}
data++;
}
}
head->center = (struct Point){-1, -1};
return head;
}
void projectInfraredDataToMonitor(short* infraredData, int rowSize){
gl_clear(THERMAL_BACKGROUND_COLOR);
int row = 0;
//TO:DO GET MATH LIB TO WORK SO THIS BECOMES MORE DYNAMIC, not(col % constant)
for(int col = 0; col < rowSize*rowSize; col++){
if(col % rowSize == 0 && col > 0){
row++;
}
//Quik maths
unsigned char red = 0;
unsigned char blue = 0;
if(*infraredData/4 > ROOM_TEMP){
red = *infraredData;
}
else{
blue = *infraredData;
}
gl_draw_pixel(col%rowSize,row,gl_color(red,0,blue));
infraredData++;
}
gl_swap_buffer();
}