-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotator.js
209 lines (174 loc) · 6.72 KB
/
annotator.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
let globalYamlData = null;
let firstClick = null;
let secondClick = null;
let firstPos = null;
let secondPos = null;
function dragOverHandler(ev) {
ev.preventDefault();
}
function dropHandler(ev) {
ev.preventDefault();
let files = ev.dataTransfer.files;
if (files.length !== 2) {
alert("Please drop exactly two files: one image and one YAML file.");
return;
}
let yamlFile = null;
let imageFile = null;
for (const file of files) {
if (file.type === "text/yaml" || file.name.endsWith('.yaml') || file.name.endsWith('.yml')) {
yamlFile = file;
} else if (file.type.startsWith('image/')) {
imageFile = file;
}
}
if (!yamlFile || !imageFile) {
alert("Please make sure to drop one YAML file and one image file.");
return;
}
let reader = new FileReader();
reader.onload = function(event) {
try {
let doc = jsyaml.load(event.target.result);
if (doc && doc.image) {
globalYamlData = jsyaml.load(event.target.result);
displayImage(imageFile);
} else {
alert("The YAML file does not reference an image.");
}
} catch (e) {
alert("Failed to parse the YAML file.");
}
};
reader.readAsText(yamlFile);
}
function displayImage(file) {
let reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.src = event.target.result;
img.onload = function() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
document.getElementById('drop_area').innerHTML = '';
document.getElementById('drop_area').appendChild(canvas);
mouseEvents(canvas, ctx, img);
};
};
reader.readAsDataURL(file);
}
function mouseEvents(canvas, ctx, img) {
drawAxis(ctx, globalYamlData.resolution, globalYamlData.origin, img.width, img.height);
canvas.addEventListener('click', function(event) {
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
const clickPos = {
x: ((mouseX / canvas.width) * img.width),
y: ((mouseY / canvas.height) * img.height)
};
const posX = (clickPos.x * globalYamlData.resolution) + globalYamlData.origin[0];
const posY = ((img.height - clickPos.y) * globalYamlData.resolution) + globalYamlData.origin[1];
if (firstClick === null) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
drawAxis(ctx, globalYamlData.resolution, globalYamlData.origin, img.width, img.height);
firstClick = clickPos;
firstPos = {
x: posX,
y: posY
};
drawArrow(ctx, firstClick, firstClick);
} else if (secondClick === null) {
secondClick = clickPos;
secondPos = {
x: posX,
y: posY
};
const x = firstPos.x.toFixed(3);
const y = firstPos.y.toFixed(3);
const yaw = calculateOrientation(firstPos, secondPos);
yaw_px = calculateOrientation(firstClick, secondClick);
end_x = firstClick.x + ((1.5 / globalYamlData.resolution) * Math.cos(yaw_px));
end_y = firstClick.y + ((1.5 / globalYamlData.resolution) * Math.sin(yaw_px));
drawArrow(ctx, firstClick, {x: end_x, y: end_y}, 'blue');
firstClick = null;
secondClick = null;
const poseString = `${x}, ${y}, ${yaw.toFixed(4)}`;
navigator.clipboard.writeText(`[${poseString}]`);
showToast(`${poseString} added to clipboard!`);
console.log(`{${x}, ${y}, ${yaw}}`);
}
});
canvas.addEventListener('mousemove', function(event) {
if (firstClick !== null && secondClick === null) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
drawAxis(ctx, globalYamlData.resolution, globalYamlData.origin, img.width, img.height);
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
const mousePos = {
x: (mouseX / canvas.width) * img.width,
y: (mouseY / canvas.height) * img.height
};
yaw_px = calculateOrientation(firstClick, mousePos)
end_x = firstClick.x + ((1.5 / globalYamlData.resolution) * Math.cos(yaw_px))
end_y = firstClick.y + ((1.5 / globalYamlData.resolution) * Math.sin(yaw_px))
drawArrow(ctx, firstClick, {x: end_x, y: end_y}, 'blue');
}
});
}
function drawArrow(ctx, from, to, color = 'rgba(0, 0, 255, 0.8)') {
ctx.beginPath();
ctx.moveTo(from.x, from.y);
ctx.lineTo(to.x, to.y);
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.stroke();
const angle = Math.atan2(to.y - from.y, to.x - from.x);
ctx.beginPath();
ctx.moveTo(to.x, to.y);
ctx.lineTo(to.x - 10 * Math.cos(angle - Math.PI / 6), to.y - 10 * Math.sin(angle - Math.PI / 6));
ctx.lineTo(to.x - 10 * Math.cos(angle + Math.PI / 6), to.y - 10 * Math.sin(angle + Math.PI / 6));
ctx.lineTo(to.x, to.y);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
function calculateOrientation(firstPoint, secondPoint) {
const deltaY = secondPoint.y - firstPoint.y;
const deltaX = secondPoint.x - firstPoint.x;
return Math.atan2(deltaY, deltaX);
}
function drawAxis(ctx, resolution, origin, imgWidth, imgHeight) {
const x_offset = origin[0] / resolution;
const y_offset = origin[1] / resolution;
const originX = -x_offset
const originY = imgHeight + y_offset
const xAxisEnd = originX + (2.5 / resolution);
const yAxisEnd = originY - (2.5 / resolution);
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(originX, originY);
ctx.lineTo(xAxisEnd, originY);
ctx.stroke();
ctx.strokeStyle = 'green';
ctx.beginPath();
ctx.moveTo(originX, originY);
ctx.lineTo(originX, yAxisEnd);
ctx.stroke();
}
function showToast(text) {
var toast = document.getElementById("toast");
toast.innerHTML = text;
toast.classList.add("show");
setTimeout(function(){
toast.classList.remove("show");
}, 10000);
toastShown = true;
}