-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.js
564 lines (501 loc) · 16.1 KB
/
mapper.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
class Location {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '{' + this.x.toString() + ', ' + this.y.toString() + '}';
}
}
class TriMesh {
vertices = new Array();
midpoints = new Array();
midpointFrom(loc1, loc2) {
return new Location((loc1.x + loc2.x) / 2, (loc1.y + loc2.y) / 2);
}
genMidpoints() {
// create array of midpoints
let midpoints = new Array();
midpoints.push(this.midpointFrom(this.vertices[0], this.vertices[1]));
midpoints.push(this.midpointFrom(this.vertices[1], this.vertices[2]));
midpoints.push(this.midpointFrom(this.vertices[0], this.vertices[2]));
return midpoints;
}
genSegments() {
let segments = new Array();
segments.push([this.vertices[0], this.vertices[1]]);
segments.push([this.vertices[1], this.vertices[2]]);
segments.push([this.vertices[0], this.vertices[2]]);
return segments;
}
has(loc) {
for (const vtx of this.vertices) {
if (locsMatch(vtx, loc)) { return true; }
}
return false;
}
toString() {
let str = new String();
str += '{';
str += arrCString(this.vertices);
//str += ', ';
//str += arrCString(this.midpoints);
str += '}\n';
return str;
}
}
class Graph {
nodes = new LocationMap();
meshes = new Array();
boundaries = new Array();
inside_edges = new Array();
clone() {
var cpy = new Graph();
cpy.nodes = this.nodes.clone();
cpy.meshes = this.meshes.slice();
return cpy;
}
addLoc(loc, nbor) {
// if vertex is already in the map, access it and add neighbor to its neighbors
if (this.nodes.has(loc)) {
let nbors = this.nodes.get(loc).slice();
let nborInNbors = false;
nbors.forEach(function(itm) {
nborInNbors = nborInNbors || locsMatch(itm, nbor);
});
if (!nborInNbors) {
nbors.push(nbor);
this.nodes.set(loc, nbors);
}
} else {
// else add vertex to map and create new array with neighbor
this.nodes.set(loc, Array.from([nbor]));
}
}
addVertex(vtx, nbor) {
// kludge that I might have to resolve later:
if (locsMatch(vtx, nbor)) return;
this.addLoc(vtx, nbor);
this.addLoc(nbor, vtx);
}
removeNeighbor(loc, nbor) {
if (this.nodes.has(loc)) {
let idx = indexOfLoc(nbor, this.nodes.get(loc));
if (idx != -1) {
let nbors = this.nodes.get(loc).slice();
nbors.splice(idx, 1);
if (nbors.length == 0) {
this.nodes.delete(loc);
} else {
this.nodes.set(loc, nbors);
}
}
}
}
removeMeshNeighbors(mesh) {
this.removeNeighbor(mesh.vertices[0], mesh.vertices[1]);
this.removeNeighbor(mesh.vertices[0], mesh.vertices[2]);
this.removeNeighbor(mesh.vertices[1], mesh.vertices[0]);
this.removeNeighbor(mesh.vertices[1], mesh.vertices[2]);
this.removeNeighbor(mesh.vertices[2], mesh.vertices[0]);
this.removeNeighbor(mesh.vertices[2], mesh.vertices[1]);
}
genMidpoints() {
// generate all possible midpoints -> store in hash map
let midpointMap = new LocationMap();
this.meshes.forEach(function(mesh) {
let midpoints = mesh.genMidpoints();
midpoints.forEach(function(point) {
if (midpointMap.has(point)) {
midpointMap.set(point, midpointMap.get(point) + 1);
} else {
midpointMap.set(point, 1);
}
});
});
// go through meshes -> store only midpoints that show up more than once
for (const mesh of this.meshes) {
let midpoints = mesh.genMidpoints();
for (const point of midpoints) {
if (midpointMap.get(point) > 1) {
// connect midpoints to mesh nodes
for (const itm of mesh.midpoints) {this.addVertex(point, itm);}
mesh.midpoints.push(point);
this.addVertex(point, mesh.vertices[0]);
this.addVertex(point, mesh.vertices[1]);
this.addVertex(point, mesh.vertices[2]);
}
}
}
}
genEdges() {
let segmentMap = new SegmentMap();
for (const mesh of this.meshes) {
let segments = mesh.genSegments();
for (const segment of segments) {
if (segmentMap.has(segment)) {
segmentMap.set(segment, segmentMap.get(segment) + 1);
} else {
segmentMap.set(segment, 1);
}
}
}
for (const pair of segmentMap.segmentPairs) {
if (pair[1] == 1) this.boundaries.push(pair[0]);
else this.inside_edges.push(pair[0]);
}
}
deleteMeshesWith(loc) {
var remainingMeshes = new Array();
for (const mesh of this.meshes) {
if (!mesh.has(loc)) {
remainingMeshes.push(mesh);
} else {
this.removeMeshNeighbors(mesh);
}
}
this.meshes = remainingMeshes;
}
}
class LocationMap {
locPairs = new Array();
clone() {
var cpy = new LocationMap();
cpy.locPairs = this.locPairs.slice();
return cpy;
}
has(point) {
var cond = false;
this.locPairs.forEach(function(pair) {
if (locsMatch(pair[0], point)) cond = true;
});
return cond;
}
get(point) {
var val = undefined;
this.locPairs.forEach(function(pair) {
if (locsMatch(pair[0], point)) val = pair[1];
});
return val;
}
set(point, val) {
for (const pair of this.locPairs) {
if (locsMatch(pair[0], point)) {
let newPair = [point, val];
this.locPairs[this.locPairs.indexOf(pair)] = newPair;
return
}
}
this.locPairs.push([point, val]);
}
delete(point) {
for (var i = 0; i < this.locPairs.length; i++) {
if (locsMatch(this.locPairs[i][0], point)) {
this.locPairs.splice(i, 1);
return true;
}
}
return false;
}
}
// segment:
// pair of locations
// which is just a length two array of locations
function segmentsMatch(seg1, seg2) {
if ((locsMatch(seg1[0], seg2[0]) && locsMatch(seg1[1], seg2[1]))
|| (locsMatch(seg1[0], seg2[1]) && locsMatch(seg1[1], seg2[0]))) return true;
return false;
}
class SegmentMap {
segmentPairs = new Array();
has(segment) {
var cond = false;
this.segmentPairs.forEach(function(pair) {
if (segmentsMatch(pair[0], segment)) cond = true;
});
return cond;
}
get(segment) {
var val = undefined;
this.segmentPairs.forEach(function(pair) {
if (segmentsMatch(pair[0], segment)) val = pair[1];
});
return val;
}
set(segment, val) {
for (const pair of this.segmentPairs) {
if (segmentsMatch(pair[0], segment)) {
let newPair = [segment, val];
this.segmentPairs[this.segmentPairs.indexOf(pair)] = newPair;
return
}
}
this.segmentPairs.push([segment, val]);
}
delete(segment) {
for (var i = 0; i < this.segmentPairs.length; i++) {
if (segmentsMatch(this.segmentPairs[i][0], segment)) {
this.segmentPairs.splice(i, 1);
return true;
}
}
return false;
}
}
var graph = new Graph();
var tempMesh = new TriMesh();
var locations = new Array();
var reader = new FileReader();
var ctx = document.getElementById('bbmap').getContext('2d');
ctx.imageSmoothingEnabled = false;
var canvasFunc;
var img = new Image();
reader.onload = function(e) {
img.src = e.target.result;
img.onload = function() {
ctx.drawImage(img, 0, 0, 640, 400);
};
};
function getOffsetLoc(e) {
let x = e.offsetX - 1;
let y = e.offsetY - 3;
if (x < 0) x = 0;
if (y < 0) x = 0;
x = x - (x % 2);
y = y - (y % 2);
return new Location(x, y);
}
function locsMatch(loc1, loc2) { return loc1.y == loc2.y && loc1.x == loc2.x; }
function indexOfLoc(loc, list) {
for (var i = 0; i < list.length; i++) {
if (locsMatch(loc, list[i])) { return i; }
}
return -1;
}
function addLocation(e) {
let loc = getOffsetLoc(e);
locations.push(loc);
}
function selectLocation(loc) {
let sel = -1;
locations.forEach(function(itm, idx) {
if ((loc.x <= itm.x + 2 && loc.x >= itm.x - 2) && (loc.y >= itm.y - 2 && loc.y <= itm.y + 2)) {
sel = idx; return;
}
});
return sel;
}
function removeLocation(e) {
let idx = selectLocation(getOffsetLoc(e));
if (idx != -1) {
let loc = locations[idx];
graph.deleteMeshesWith(loc);
locations.splice(idx, 1);
}
}
function checkTempMesh(loc) {
let alreadyPresent = false;
if (tempMesh.vertices[0]) {
let loc1 = tempMesh.vertices[0];
alreadyPresent = alreadyPresent || locsMatch(loc, loc1);
}
if (tempMesh.vertices[1]) {
let loc2 = tempMesh.vertices[1];
alreadyPresent = alreadyPresent || locsMatch(loc, loc2);
}
return alreadyPresent;
}
function constructTriMesh(e) {
let idx = selectLocation(getOffsetLoc(e));
if (idx != -1) {
var loc = locations[idx];
if (checkTempMesh(loc)) return;
tempMesh.vertices.push(loc);
if (tempMesh.vertices.length == 2) {
// add new vertex to mesh, connect vertex in mesh to new vertex as neighbor
graph.addVertex(tempMesh.vertices[0], loc);
} else if (tempMesh.vertices.length == 3) {
// add new vertex to mesh, connect vertices in mesh to new vertex, save mesh in map, create new temp mesh
graph.addVertex(tempMesh.vertices[0], loc);
graph.addVertex(tempMesh.vertices[1], loc);
graph.meshes.push(tempMesh);
tempMesh = new TriMesh();
}
}
}
function calc_area(v1, v2, v3) {
let det = 0;
det = (
(v1.x - v3.x) *
(v2.y - v3.y)) - (
(v2.x - v3.x) *
(v1.y - v3.y));
return Math.abs(det / 2);
}
function contains_point(mesh, loc) {
var vertices = mesh.vertices;
var total = calc_area(vertices[0], vertices[1], vertices[2]);
var area1 = calc_area(loc, vertices[1], vertices[2]);
var area2 = calc_area(loc, vertices[0], vertices[2]);
var area3 = calc_area(loc, vertices[0], vertices[1]);
return (area1 + area2 + area3) <= total
}
function destroyTriangle(e) {
let loc = getOffsetLoc(e);
// see if click is inside triangle
// if it is, delete that mesh
graph.meshes.forEach(function(itm, idx) {
if (contains_point(itm, loc)) {
graph.removeMeshNeighbors(itm);
graph.meshes.splice(idx, 1);
return;
}
});
}
function readImage(input) {
if (input.files && input.files[0]) {
reader.readAsDataURL(input.files[0]);
}
}
function setImage() { readImage(document.querySelector('input[type="file"]')); }
function drawDots(arr) {
arr.forEach(function(itm) {
ctx.fillRect(itm.x - 3, itm.y, 4, 4);
});
}
function canvasClick(e) {
if (canvasFunc) canvasFunc(e);
updateCanvas();
}
function updateCanvas() {
// clear everything
ctx.clearRect(0, 0, 640, 400);
// draw image
if (img) ctx.drawImage(img, 0, 0, 640, 400);
// draw meshes
ctx.fillStyle = "rgba(0, 255, 0, 0.5)";
graph.meshes.forEach(function(itm) {
// first vertex
ctx.beginPath();
ctx.moveTo(itm.vertices[0].x, itm.vertices[0].y);
// second vertex
ctx.lineTo(itm.vertices[1].x, itm.vertices[1].y);
// third vertex
ctx.lineTo(itm.vertices[2].x, itm.vertices[2].y);
ctx.fill();
});
// draw locations array
ctx.fillStyle = 'red';
drawDots(locations);
// draw any locations in tempmesh as green
ctx.fillStyle = 'lime';
drawDots(tempMesh.vertices);
}
function selectButton(btn) {
if (document.getElementById('selected-btn'))
document.getElementById('selected-btn').id = null;
btn.id = 'selected-btn';
}
function setAddVertex(e) {
selectButton(e.target);
canvasFunc = addLocation;
}
function setRemoveVertex(e) {
selectButton(e.target);
canvasFunc = removeLocation;
}
function setCreateTriangle(e) {
selectButton(e.target);
canvasFunc = constructTriMesh;
}
function setDestroyTriangle(e) {
selectButton(e.target);
canvasFunc = destroyTriangle;
}
function arrArrCString(arr) {
let str = '{';
if (arr[0]) str += '{' + arr[0].toString() + '}';
arr.forEach(function(itm, idx) {
if (idx > 0) str += ', ' + '{' + itm.toString() + '}';
});
str += '}';
return str;
}
function arrCString(arr) {
let str = '{';
if (arr[0]) str += arr[0].toString();
arr.forEach(function(itm, idx) {
if (idx > 0) str += ', ' + itm.toString();
});
str += '}';
return str;
}
function exportJSON() {
var jsonString = JSON.stringify({graph, locations});
window.open(URL.createObjectURL(new Blob([jsonString], {type : 'application/json'})));
}
var jsonReader = new FileReader();
jsonReader.onload = function(e) {
locations = new Array();
for (const loc of JSON.parse(e.target.result).locations) {
locations.push(new Location(loc.x, loc.y));
}
graph = new Graph();
jsonGraph = JSON.parse(e.target.result).graph;
// create a new Location with the LocationMap first pair member, copy over second member
for (const pair of jsonGraph.nodes.locPairs) {
let nbors = new Array();
for (const loc of pair[1]) {
nbors.push(new Location(loc.x, loc.y));
}
graph.nodes.locPairs.push([new Location(pair[0].x, pair[0].y), nbors]);
}
for (const mesh of jsonGraph.meshes) {
// fill out a new mesh here
let newMesh = new TriMesh();
for (const vtx of mesh.vertices) {
newMesh.vertices.push(new Location(vtx.x, vtx.y));
}
graph.meshes.push(newMesh);
}
updateCanvas();
};
function readJSON(input) {
if (input.files && input.files[0]) {
jsonReader.readAsText(input.files[0]);
}
}
function importJSON() { readJSON(document.querySelector('#import-json')); }
// exporting graph data:
// straightforward approach for now - only export Graph struct, save it all as literals
// don't really need references to individual locations
// later: figure out logic for how midpoints will be generated
// idea: create new graph for export with all the midpoints
// will have to connect each midpoint to all the other midpoints and vertices in the mesh
// idea to generate less superfluous midpoints: only generate midpoints that match for more than one mesh
// can be implemented simply by adding all midpoints to a map and checking for any doubles
function exportData() {
var dataString = new String();
var exportGraph = graph.clone();
exportGraph.genEdges();
//exportGraph.genMidpoints();
dataString += '#include "graph.h"\n\n';
dataString += 'Graph exportedGraph {{\n';
// location unordered map
for (const pair of exportGraph.nodes.locPairs) {
dataString += '{' + pair[0].toString() + ', ';
dataString += arrCString(pair[1]);
dataString += '},\n';
}
dataString += '},\n';
// graph mesh array
dataString += arrCString(exportGraph.meshes);
dataString += ',\n';
// boundaries array
dataString += arrArrCString(exportGraph.boundaries);
dataString += ',\n';
// edges array
dataString += arrArrCString(exportGraph.inside_edges);
dataString += '\n};'
window.open(URL.createObjectURL(new Blob([dataString], {type : 'text/plain'})));
}