forked from opendatacam/node-moving-things-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemTracked.js
More file actions
167 lines (160 loc) · 4.73 KB
/
Copy pathItemTracked.js
File metadata and controls
167 lines (160 loc) · 4.73 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
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
var uuidv4 = require('uuid/v4');
var computeBearingIn360 = require('./utils').computeBearingIn360
var computeVelocityVector = require('./utils').computeVelocityVector
// Properties example
// {
// "x": 1021,
// "y": 65,
// "w": 34,
// "h": 27,
// "prob": 26,
// "name": "car"
// }
// Use a simple incremental unique id for the display
var idDisplay = 0;
exports.ItemTracked = function(properties, frameNb, DEFAULT_UNMATCHEDFRAMES_TOLERANCE){
var DEFAULT_UNMATCHEDFRAMES_TOLERANCE = DEFAULT_UNMATCHEDFRAMES_TOLERANCE;
var itemTracked = {};
// ==== Private =====
// Am I available to be matched?
itemTracked.available = true;
// Should I be deleted?
itemTracked.delete = false;
// How many unmatched frame should I survive?
itemTracked.frameUnmatchedLeftBeforeDying = DEFAULT_UNMATCHEDFRAMES_TOLERANCE;
itemTracked.isZombie = false;
itemTracked.appearFrame = frameNb;
itemTracked.disappearFrame = null;
itemTracked.disappearArea = {};
// ==== Public =====
itemTracked.x = properties.x;
itemTracked.y = properties.y;
itemTracked.w = properties.w;
itemTracked.h = properties.h;
itemTracked.name = properties.name;
itemTracked.itemHistory = [];
itemTracked.itemHistory.push({
x: properties.x,
y: properties.y,
w: properties.w,
h: properties.h
});
itemTracked.velocity = {
dx: 0,
dy: 0
};
itemTracked.nbTimeMatched = 1;
// Assign an unique id to each Item tracked
itemTracked.id = uuidv4();
// Use an simple id for the display and debugging
itemTracked.idDisplay = idDisplay;
idDisplay++
// Give me a new location / size
itemTracked.update = function(properties, frameNb){
// if it was zombie and disappear frame was set, reset it to null
if(this.disappearFrame) {
this.disappearFrame = null;
this.disappearArea = {}
}
this.isZombie = false;
this.nbTimeMatched += 1;
this.x = properties.x;
this.y = properties.y;
this.w = properties.w;
this.h = properties.h;
this.itemHistory.push({
x: this.x,
y: this.y,
w: this.w,
h: this.h
});
this.name = properties.name;
// Reset dying counter
this.frameUnmatchedLeftBeforeDying = DEFAULT_UNMATCHEDFRAMES_TOLERANCE
// Compute new velocityVector based on last positions history
this.velocity = this.updateVelocityVector();
}
itemTracked.makeAvailable = function() {
this.available = true;
return this;
}
itemTracked.makeUnavailable = function() {
this.available = false;
return this;
}
itemTracked.countDown = function(frameNb) {
// Set frame disappear number
if(this.disappearFrame === null) {
this.disappearFrame = frameNb;
this.disappearArea = {
x: this.x,
y: this.y,
w: this.w,
h: this.h
}
}
this.frameUnmatchedLeftBeforeDying--;
this.isZombie = true;
// If it was matched less than 1 time, it should die quick
if(this.nbTimeMatched <= 1) {
this.frameUnmatchedLeftBeforeDying = -1;
}
}
itemTracked.updateTheoricalPositionAndSize = function() {
this.itemHistory.push({
x: this.x,
y: this.y,
w: this.w,
h: this.h
});
this.x = this.x + this.velocity.dx
this.y = this.y + this.velocity.dy
}
itemTracked.predictNextPosition = function() {
return {
x : this.x + this.velocity.dx,
y : this.y + this.velocity.dy,
w: this.w,
h: this.h
};
}
itemTracked.isDead = function() {
return this.frameUnmatchedLeftBeforeDying < 0;
}
// Velocity vector based on the last 15 frames
itemTracked.updateVelocityVector = function() {
var AVERAGE_NBFRAME = 15;
if(this.itemHistory.length <= AVERAGE_NBFRAME) {
return computeVelocityVector(this.itemHistory[0], this.itemHistory[this.itemHistory.length - 1], this.itemHistory.length);
} else {
return computeVelocityVector(this.itemHistory[this.itemHistory.length - AVERAGE_NBFRAME], this.itemHistory[this.itemHistory.length - 1], AVERAGE_NBFRAME);
}
}
itemTracked.toJSON = function() {
return {
id: this.id,
idDisplay: this.idDisplay,
x: this.x,
y: this.y,
w: this.w,
h: this.h,
// Here we negate dy to be in "normal" carthesian coordinates
bearing: computeBearingIn360(this.velocity.dx, - this.velocity.dy),
name: this.name,
isZombie: this.isZombie,
appearFrame: this.appearFrame,
disappearFrame: this.disappearFrame
}
}
itemTracked.toJSONGenericInfo = function() {
return {
id: this.id,
idDisplay: this.idDisplay,
appearFrame: this.appearFrame,
disappearFrame: this.disappearFrame,
disappearArea: this.disappearArea,
nbActiveFrame: this.disappearFrame - this.appearFrame
}
}
return itemTracked;
};