-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.c
More file actions
130 lines (105 loc) · 3.5 KB
/
algorithm.c
File metadata and controls
130 lines (105 loc) · 3.5 KB
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
#define OLD_EVENT_THRESHOLD 40 miliseconds SOMEHOW
#define LINE_LENGTH_MINIMUM 70 //pixels
#define MAX_EVENTS_IN_LINE 100
#define MAX_EVENTS_IN_CLUSTER 100
#define NULL_EVENT_TIMESTAMP 0 //decides the timestamp for the null event.
//EXPLANATION: cause we cant null an entry of an array in C, any events with the timestamp 0 are considered null & gone.
int32[][] SAE = new int32[640][480];
struct event
{
int32 timestamp,
int16 x,
int16 y
};
struct cluster
{
event[] containedEvents = new event[MAX_EVENTS_IN_CLUSTER] //arbitrary
int16 inferredLineLength, //needs to be at least 640
int16 inferredLineAngleHough, //needs to be capable of holding 360
int16 inferredLineDistanceHough //needs to be capable of holding 640/2
};
struct line
{
event[] containedEvents = new event[MAX_EVENTS_IN_LINE] //arbitrary
int16 lineLength, //needs to be at least 640.
int16 lineAngleHough, //needs to be capable of holding 360
int16 lineDistanceHough, //needs to be capable of holding 640/2
int8 state //0 = null, 1 = initializing, 2 = active, 3 = hibernating
};
cluster[] clusterArray = new cluster[20];
line[] lineArray = new cluster[20];
int runPeriodicTicker = 0;
int runPeriodicLimit = 5;
void OnReceiveEvent(event receivedEvent)
{
//first check if it's garbage day
if (runPeriodicTicker == runPeriodicLimit)
{
runPeriodicTicker = 0;
periodicCleanup(receivedEvent); //needed to know current timestamp
}
else
runPeriodicTicker++;
//insert in SAE
SAE[event.x, event.y] = event.timestamp;
int8 passFilter = 0;
//noise filter
for (int8 i = 0 ; i++; i<3)
for (int8 j = 0 ; j++ ; j<3)
passFilter++;
if (passFilter < 3)
return;
//attach to line
for (int i = 0; i++; i<lineArray.length) //run thru each line
{
var lineToAdd = null;
if (distanceToLine(event.x, event.y, lineArray[i]) < 1.8px && distanceToMidpoint(event.x, event.y, lineArray[i]) < lineArray[i].lineLength)
{
if (lineToAdd != null)
return; //discard line cuz we got more than 1 match. alg says so
lineToAdd = lineArray[i];
}
addToLine(event, lineToAdd);
}
//attach to cluster
for (int i = 0; i++; i<lineArray.length)
{
var clusterToAdd;
if (distanceToLine(event.x, event.y, clusterArray[i]) < 1.8px && distanceToMidpoint(event.x, event.y, clusterArray[i]) < lineArray[i].inferredLineLength)
{
if (clusterToAdd != null)
TryMergeClusters(clusterToAdd, clusterArray[i]);
//algorithm does not specify wether to dump the event if 2 clusters are detected but they can't merge so
//lets just let it merge to the latest line it can find
clusterToAdd = clusterArray[i];
}
//this also handles cluster->line promotion if event amount breaches 35. i just gotta write it lmao
addToCluster(event, clusterToAdd);
return;
}
//if NOTHING returns by now then we gotta try this
TrySpawnCluster(event.x, event.y);
return;
}
void periodicCleanup(event latestEvent)
{
//LINES
for (int i = 0 ; i < lineArray.length, i++)
{
//IF line is null, ignore
if (lineArray[i].state = 0)
continue;
//run through contained events in each line
for (int j = 0 ; j < lineArray[i].containedEvents.length, j++)
{
//REMOVE OLD EVENTS
if (latestEvent.timestamp - lineArray[i].containedEvents[j].timestamp > OLD_EVENT_THRESHOLD)
lineArray[i].containedEvents[j].timestamp = 0; //null the event
//UPDATE PLANE ESTIMATE
updatePlaneEstimate(lineArray[i]);
//DELETE LINES TOO SHORT
if (lineArray[i].lineLength < LINE_LENGTH_LIMIT)
lineArray[i].state = 0; //null the line
}
}
}