-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmtree.js
253 lines (206 loc) · 5.12 KB
/
mmtree.js
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
/**
* Consts
*/
var Region = {};
Object.defineProperty(Region, 'I', {
enumerable: false,
configurable: false,
writable: false,
value: 0
});
Object.defineProperty(Region, 'II', {
enumerable: false,
configurable: false,
writable: false,
value: 1
});
Object.defineProperty(Region, 'III', {
enumerable: false,
configurable: false,
writable: false,
value: 2
});
Object.defineProperty(Region, 'IV', {
enumerable: false,
configurable: false,
writable: false,
value: 3
});
Object.defineProperty(Region, 'NONE', {
enumerable: false,
configurable: false,
writable: false,
value: -1
});
/**
* Singleton for configurations
*/
var Config = (function() {
var instance;
function Config() {
this.distanceFunction = Distance.euclidianDistance;
this.extractorFunction = Extractor.euclidianExtractor;
this.calculateBestPivots = Policy.simplePivotChoose;
return instance;
}
/**
* Singleton methods
* @return This class instance
*/
Config.getInstance = function() {
if (!instance) instance = createInstance();
return instance;
};
function createInstance() {
var obj = new Config();
return obj;
};
return Config;
})();
/**
* Namespace for computing distances
*/
var Distance = {
euclidianDistance: function(a, b) {
return Math.sqrt(Math.pow(a.x + b.x, 2) + Math.pow(a.y + b.y, 2));
}
};
/**
* Namespace for extract characteristics
*/
var Extractor = {
euclidianExtractor: function(a) {
return a;
}
}
/**
* Policies
*/
var Policy = {
simplePivotChoose: function(data) {
return [data[0], data[1]];
}
}
/**
* Node class
*/
var Node = (function() {
'use strict';
function Node() {
this.pivots = new Array(2+1).join('0').split('').map(parseFloat);
this.regions = new Array(4+1).join('0').split('').map(parseFloat);
this.distancesToParents = new Array(4+1).join('0').split('').map(parseFloat);
this.distanceBetweenPivots = 0;
this.pivots.forEach((el, i) => {
this.pivots[i] = null;
});
this.regions.forEach((el, i) => {
this.regions[i] = null;
});
}
Node.prototype.getPivots = function() {
return this.pivots;
}
Node.prototype.addVector = function(vector, dist0, dist1) {
var isFull = this.isNodeFullOfPivots();
if (!isFull.full) {
this.pivots[isFull.index] = vector;
this.distancesToParents[2*isFull.index] = dist0;
this.distancesToParents[2*isFull.index+1] = dist1;
}
else {
var dist0 = Config.getInstance().distanceFunction(this.pivots[0], vector),
dist1 = Config.getInstance().distanceFunction(this.pivots[1], vector);
var region = Region.NONE;
if (dist0 < this.distanceBetweenPivots && dist1 < this.distanceBetweenPivots)
// ADD ON INTERSECTION (Region I)
region = Region.I;
else if (dist0 < this.distanceBetweenPivots && dist1 >= this.distanceBetweenPivots)
// ADD ON Region II
region = Region.II;
else if (dist0 >= this.distanceBetweenPivots && dist1 < this.distanceBetweenPivots)
// ADD ON Region III
region = Region.III;
else if (dist0 >= this.distanceBetweenPivots && dist1 >= this.distanceBetweenPivots)
// ADD OUTSIDE (Region IV)
region = Region.IV;
if (this.regions[region] == null)
this.regions[region] = new Node();
this.regions[region].addVector(vector, dist0, dist1);
}
isFull = this.isNodeFullOfPivots();
if (isFull.full) {
this.calculateDistanceBetweenPivots();
}
}
/**
* Calculate the distance between pivots in this node
*/
Node.prototype.calculateDistanceBetweenPivots = function() {
this.distanceBetweenPivots = Config.getInstance().distanceFunction(this.pivots[0], this.pivots[1]);
}
/**
* Verify if the node is full
* @return {Object} An object containing if the node is full and the index
*/
Node.prototype.isNodeFullOfPivots = function() {
var isFull = true;
for (var i = 0; i < this.pivots.length; i++) {
if (this.pivots[i] == null) {
isFull = false;
break;
}
}
return {
full: isFull,
index: i
};
}
Node.prototype.walk = function(options) {
if (options != undefined && 'callback' in options) {
options.callback(this);
}
for (var i = 0; i < this.regions.length; i++) {
if (this.regions[i] == null) continue;
this.regions[i].walk(options);
}
}
return Node;
}());
/**
* MMTree
*/
var MMTree = (function() {
'use strict';
function MMTree() {
this.root = new Node();
}
MMTree.prototype.insert = function(vector) {
this.root.addVector(vector, 0, 0);
}
MMTree.prototype.walk = function(options) {
this.root.walk(options);
}
return MMTree;
}());
// document.addEventListener('DOMContentLoaded', () => {
// var mmt = new MMTree();
// var data = [
// { x: 1, y: 2 },
// { x: 1, y: 5 },
// { x: 3, y: 1 },
// { x: 4, y: 6 },
// { x: 2, y: 1 },
// { x: 6, y: 3 },
// { x: 2, y: 5 },
// ];
// var pivots = Config.getInstance().calculateBestPivots(data);
// console.log(pivots);
// mmt.insert(Config.getInstance().extractorFunction(pivots[0]));
// mmt.insert(Config.getInstance().extractorFunction(pivots[1]));
// for (var i = 2; i < data.length; i++) {
// var vector = Config.getInstance().extractorFunction(data[i]);
// mmt.insert(vector);
// }
// console.log(mmt);
// });