jonigata commited on
Commit
2cd429d
·
1 Parent(s): c4aa350

rewrite internal data structure

Browse files
Files changed (1) hide show
  1. static/poseEditor.js +125 -160
static/poseEditor.js CHANGED
@@ -2,50 +2,45 @@ console.log("hello from poseEditor.js")
2
  var canvas = null;
3
  var ctx = null;
4
 
5
- // candidateの形式:[[x1, y1, score1], [x2, y2, score2], ...]
6
- let candidateSource = [
7
- [235, 158, 0.93167633],
8
- [234, 220, 0.97106987],
9
- [193, 222, 0.93366587],
10
- [138, 263, 0.87655306],
11
- [89, 308, 0.8868227],
12
- [276, 220, 0.9038924],
13
- [325, 264, 0.92930061],
14
- [375, 309, 0.9217211],
15
- [207, 347, 0.86410147],
16
- [203, 433, 0.86538297],
17
- [199, 523, 0.95236528],
18
- [261, 347, 0.88489777],
19
- [262, 430, 0.90848708],
20
- [261, 522, 0.94749999],
21
- [227, 148, 0.94189668],
22
- [245, 148, 0.93967074],
23
- [208, 158, 0.92053992],
24
- [258, 154, 0.73533273]
25
- ];
26
-
27
  function deepCopy(arr) {
28
- return JSON.parse(JSON.stringify(arr));
 
 
 
 
29
  }
30
 
31
- // subsetの形式:[[index1, index2, ..., -1], [index1, index2, ..., -1], ...]
32
- let subsetSource = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]];
33
- let subset = deepCopy(subsetSource);
 
 
 
34
 
35
- // const candidateSource = [[618.00, 0.00], [618.00, 44.00], [304.00, 81.00], [482.00, 96.00], [66.00, 270.00], [171.00, 280.00], [618.00, 82.00], [307.00, 112.00], [460.00, 143.00], [0.00, 301.00], [65.00, 301.00], [172.00, 303.00], [584.00, 86.00], [275.00, 119.00], [420.00, 139.00], [0.00, 301.00], [41.00, 301.00], [144.00, 303.00], [544.00, 131.00], [348.00, 139.00], [262.00, 160.00], [0.00, 337.00], [52.00, 339.00], [130.00, 348.00], [570.00, 175.00], [283.00, 177.00], [78.00, 338.00], [172.00, 380.00], [651.00, 78.00], [338.00, 111.00], [505.00, 144.00], [92.00, 301.00], [198.00, 305.00], [661.00, 132.00], [349.00, 156.00], [541.00, 179.00], [106.00, 336.00], [203.00, 348.00], [305.00, 159.00], [665.00, 160.00], [563.00, 192.00], [80.00, 343.00], [181.00, 385.00], [614.00, 205.00], [291.00, 220.00], [432.00, 320.00], [152.00, 372.00], [43.00, 380.00], [0.00, 386.00], [623.00, 281.00], [306.00, 290.00], [92.00, 357.00], [509.00, 434.00], [304.00, 357.00], [622.00, 368.00], [47.00, 394.00], [0.00, 395.00], [142.00, 405.00], [535.00, 565.00], [655.00, 200.00], [337.00, 217.00], [467.00, 322.00], [191.00, 372.00], [83.00, 375.00], [344.00, 282.00], [655.00, 282.00], [103.00, 343.00], [237.00, 368.00], [22.00, 377.00], [0.00, 379.00], [460.00, 459.00], [305.00, 352.00], [638.00, 355.00], [0.00, 401.00], [110.00, 412.00], [411.00, 570.00], [608.00, 0.00], [608.00, 40.00], [297.00, 75.00], [469.00, 84.00], [0.00, 261.00], [58.00, 263.00], [165.00, 275.00], [625.00, 0.00], [625.00, 39.00], [309.00, 74.00], [486.00, 83.00], [71.00, 264.00], [180.00, 276.00], [599.00, 0.00], [599.00, 44.00], [284.00, 80.00], [440.00, 93.00], [48.00, 271.00], [0.00, 272.00], [157.00, 277.00], [634.00, 0.00], [633.00, 41.00], [319.00, 77.00], [79.00, 269.00], [190.00, 277.00]];
36
- // const subset = [[1.00,6.00,12.00,18.00,24.00,28.00,33.00,39.00,43.00,49.00,54.00,59.00,65.00,72.00,77.00,84.00,90.00,97.00,32.98,18.00],[5.00,11.00,17.00,23.00,27.00,32.00,37.00,42.00,46.00,-1.00,-1.00,62.00,67.00,-1.00,82.00,88.00,95.00,100.00,25.45,15.00],[4.00,10.00,16.00,22.00,26.00,31.00,36.00,41.00,47.00,51.00,57.00,63.00,66.00,74.00,81.00,87.00,93.00,99.00,26.97,18.00],[3.00,8.00,14.00,19.00,25.00,30.00,35.00,40.00,45.00,52.00,58.00,61.00,70.00,75.00,79.00,86.00,92.00,-1.00,30.45,17.00],[2.00,7.00,13.00,20.00,-1.00,29.00,34.00,38.00,44.00,50.00,53.00,60.00,64.00,71.00,78.00,85.00,91.00,98.00,27.89,17.00],[0.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,76.00,83.00,-1.00,96.00,3.33,4.00]];
 
 
 
 
 
 
 
 
37
 
38
- let candidate = candidateSource.map(point => [point[0], point[1] - 70]);
 
 
39
 
40
  function addPerson() {
41
  var dx = Math.random() * 100;
42
  var dy = Math.random() * 100;
43
 
44
- newFirst = candidate.length;
45
- candidate = candidate.concat(candidateSource.map(point => [point[0] + dx, point[1] + dy]));
46
- subset.push(subsetSource[0].map((x, i) => x + newFirst));
47
- console.log(candidate);
48
- console.log(subset);
49
  Redraw();
50
  }
51
 
@@ -69,59 +64,50 @@ function resizeCanvas(width, height) {
69
  canvas.width = width ? width : canvas.width;
70
  canvas.height = height ? height : canvas.height;
71
  clearCanvas();
72
- drawBodyPose(candidate, subset);
73
  }
74
 
75
- function drawBodyPose(candidate, subset) {
76
- const stickWidth = 6;
77
- const limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10],
78
- [10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17],
79
- [1, 16], [16, 18], [3, 17], [6, 18]];
80
-
81
- const colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0],
82
- [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255],
83
- [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]];
 
 
84
 
85
- ctx.globalAlpha = 0.7;
 
86
 
87
  // edge
88
- for (let i = 0; i < 17; i++) {
89
- for (let n = 0; n < subset.length; n++) {
90
- const index0 = subset[n][limbSeq[i][0]-1];
91
- const index1 = subset[n][limbSeq[i][1]-1];
92
- if (index0 === -1 || index1 === -1) {
93
- continue;
94
- }
95
- const [X0, Y0] = candidate[index0].slice(0, 2);
96
- const [X1, Y1] = candidate[index1].slice(0, 2);
97
- ctx.beginPath();
98
- ctx.lineWidth=stickWidth;
99
- ctx.strokeStyle = `rgb(${colors[i].join(',')})`;
100
- ctx.moveTo(X0, Y0);
101
- ctx.lineTo(X1, Y1);
102
- ctx.stroke();
103
- }
104
  }
105
 
106
  // node
107
  ctx.font = '12px serif';
108
- for (let i = 0; i < 18; i++) {
109
- for (let n = 0; n < subset.length; n++) {
110
- const index = subset[n][i];
111
- if (index === -1) {
112
- continue;
113
- }
114
- const [x, y] = candidate[index].slice(0, 2);
115
- ctx.beginPath();
116
- ctx.arc(x, y, stickWidth*1.2, 0, 2 * Math.PI);
117
- ctx.fillStyle = `rgb(${colors[i].join(',')})`;
118
- ctx.fill();
119
- ctx.fillStyle = 'rgb(255,255,255)'
120
- // ctx.fillText(index, x-3, y+4);
121
- }
122
  }
 
123
 
124
- ctx.globalAlpha = 1.0;
125
  }
126
 
127
  function drawUI() {
@@ -136,29 +122,34 @@ function drawUI() {
136
  ctx.beginPath();
137
  ctx.lineWidth=1;
138
  ctx.strokeStyle = 'rgb(255,255,255)';
139
- ctx.moveTo(dragStartX, dragStartY);
140
- ctx.lineTo(dragStartX+rotateBaseVector[0], dragStartY+rotateBaseVector[1]);
141
  ctx.stroke();
142
  }
143
  }
144
 
145
  function Redraw() {
146
  clearCanvas();
147
- drawBodyPose(candidate, subset);
148
  drawUI();
149
  }
150
 
151
- function getNearestCandidate(x, y) {
152
- let minDist = Infinity;
153
- let minIndex = -1;
154
- for (let i = 0; i < candidate.length; i++) {
155
- const dist = Math.sqrt((x - candidate[i][0]) ** 2 + (y - candidate[i][1]) ** 2);
156
- if (dist < minDist) {
157
- minDist = dist;
158
- minIndex = i;
 
 
 
 
 
159
  }
160
  }
161
- return [minIndex,minDist];
162
  }
163
 
164
  let dragRange = 64;
@@ -166,14 +157,12 @@ let dragRangeDelta = 16;
166
 
167
  // ドラッグ中に座標を保持するための変数
168
  let isDragging = false;
169
- let dragIndex = -1;
170
- let dragStartX = 0;
171
- let dragStartY = 0;
172
- let draggingCandidate = null;
173
  let dragPersonIndex = -1;
174
  let dragMarks = [];
175
  let dragMode = "";
176
  let rotateBaseVector = null;
 
177
 
178
  function getCanvasPosition(event) {
179
  const rect = canvas.getBoundingClientRect();
@@ -182,51 +171,28 @@ function getCanvasPosition(event) {
182
  return [x, y];
183
  }
184
 
185
- function forEachCandidateOfPerson(personIndex, callback) {
186
- if (personIndex === -1) { return; }
187
-
188
- for (let i = 0; i < 18; i++) {
189
- const index = subset[personIndex][i];
190
- if (index === -1) {
191
- continue;
192
- }
193
- callback(index);
194
- }
195
- }
196
-
197
- function forEachMarkedCandidate(callback) {
198
  for (let i = 0; i < dragMarks.length; i++) {
199
- if (dragMarks[i]) {
200
- callback(i);
 
 
201
  }
202
  }
203
  }
204
 
205
  // Canvas要素上でマウスが押された場合に呼び出される関数
206
  function handleMouseDown(event) {
207
- const [x, y] = getCanvasPosition(event);
208
- const [index, minDist] = getNearestCandidate(x, y);
209
 
210
  // ドラッグ処理の開始
211
- dragIndex = index;
212
- dragStartX = x;
213
- dragStartY = y;
214
- draggingCandidate = JSON.parse(JSON.stringify(candidate))
215
- dragMarks = new Array(candidate.length).fill(false);
216
-
217
- // indexが含まれる人間を探す
218
- for (let i = 0; i < subset.length; i++) {
219
- var found = subset[i].indexOf(index);
220
- if (found != -1 && found < 18) {
221
- dragPersonIndex = i;
222
- break;
223
- }
224
- }
225
 
226
  if (event.altKey || event.ctrlKey || event.shiftKey) {
227
- forEachCandidateOfPerson(dragPersonIndex, (i) => {
228
- dragMarks[i] = true;
229
- });
230
  isDragging = true;
231
  if (event.altKey) {
232
  dragMode = "move";
@@ -238,19 +204,13 @@ function handleMouseDown(event) {
238
  }
239
  console.log(dragMode);
240
  } else if (keyDownFlags["Space"]) {
241
- var markCount = 0;
242
- forEachCandidateOfPerson(dragPersonIndex, (i) => {
243
- // dragRangeの範囲内のものだけマーク
244
- let distSq = (candidate[i][0] - x) ** 2 + (candidate[i][1] - y) ** 2;
245
- if (distSq < dragRange ** 2) {
246
- dragMarks[i] = true;
247
- markCount++;
248
- }
249
- });
250
- isDragging = 0 < markCount;
251
  dragMode = "move";
252
  } else if (minDist < 16) {
253
- dragMarks[index] = true;
254
  isDragging = true;
255
  dragMode = "move";
256
  }
@@ -260,21 +220,21 @@ function handleMouseDown(event) {
260
  function handleMouseMove(event) {
261
  mouseCursor = getCanvasPosition(event);
262
  if (isDragging) {
263
- const [x, y] = getCanvasPosition(event);
264
-
265
- const dragOffsetX = x - dragStartX;
266
- const dragOffsetY = y - dragStartY;
267
 
268
  if (dragMode == "scale") {
269
  // 拡大縮小
270
- let xScale = 1 + dragOffsetX / canvas.width;
271
- let yScale = 1 + dragOffsetY / canvas.height;
272
- forEachMarkedCandidate((index) => {
273
- candidate[index][0] = (draggingCandidate[index][0] - dragStartX) * xScale + dragStartX;
274
- candidate[index][1] = (draggingCandidate[index][1] - dragStartY) * yScale + dragStartY;
 
275
  });
276
  } else if (dragMode == "rotate") {
277
- rotateBaseVector = [dragOffsetX, dragOffsetY];
278
  if (!event.shiftKey) {
279
  dragMode = "rotate2";
280
  console.log("rotate2");
@@ -282,20 +242,22 @@ function handleMouseMove(event) {
282
  console.log("!");
283
  } else if (dragMode == "rotate2") {
284
  // 回転
285
- let angle = directedAngleTo(rotateBaseVector, [dragOffsetX, dragOffsetY]);
286
- forEachMarkedCandidate((index) => {
287
- let x = draggingCandidate[index][0] - dragStartX;
288
- let y = draggingCandidate[index][1] - dragStartY;
289
- candidate[index][0] = x * Math.cos(angle) - y * Math.sin(angle) + dragStartX;
290
- candidate[index][1] = x * Math.sin(angle) + y * Math.cos(angle) + dragStartY;
 
 
 
291
  });
292
  } else if (dragMode == "move") {
293
  // 移動
294
- forEachMarkedCandidate((index) => {
295
- if (dragMarks[index]) {
296
- candidate[index][0] = draggingCandidate[index][0] + dragOffsetX;
297
- candidate[index][1] = draggingCandidate[index][1] + dragOffsetY;
298
- }
299
  });
300
  }
301
  }
@@ -305,6 +267,7 @@ function handleMouseMove(event) {
305
 
306
  function handleMouseUp(event) {
307
  isDragging = false;
 
308
  Redraw();
309
  }
310
 
@@ -335,9 +298,11 @@ document.addEventListener("keyup", (event) => {
335
  function initializePose(jsonData,w,h) {
336
  console.log("initializePose");
337
  if (jsonData != null) {
338
- candidate = jsonData.candidate;
339
- subset = jsonData.subset;
 
340
  }
 
341
 
342
  canvas = document.getElementById('canvas');
343
  ctx = canvas.getContext('2d');
 
2
  var canvas = null;
3
  var ctx = null;
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  function deepCopy(arr) {
6
+ return JSON.parse(JSON.stringify(arr));
7
+ }
8
+
9
+ function distSq(p0, p1) {
10
+ return (p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2;
11
  }
12
 
13
+ // poseDataの形式:[[x1, y1], [x2, y2], ...]
14
+ let poseData = [];
15
+
16
+ // サンプルデータ
17
+ const sampleCandidateSource = [[235, 158],[234, 220],[193, 222],[138, 263],[89, 308],[276, 220],[325, 264],[375, 309],[207, 347],[203, 433],[199, 523],[261, 347],[262, 430],[261, 522],[227, 148],[245, 148],[208, 158],[258, 154]];
18
+ const sampleSubsetElementSource = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
19
 
20
+ // const sampleCandidateSource = [[618.00, 0.00], [618.00, 44.00], [304.00, 81.00], [482.00, 96.00], [66.00, 270.00], [171.00, 280.00], [618.00, 82.00], [307.00, 112.00], [460.00, 143.00], [0.00, 301.00], [65.00, 301.00], [172.00, 303.00], [584.00, 86.00], [275.00, 119.00], [420.00, 139.00], [0.00, 301.00], [41.00, 301.00], [144.00, 303.00], [544.00, 131.00], [348.00, 139.00], [262.00, 160.00], [0.00, 337.00], [52.00, 339.00], [130.00, 348.00], [570.00, 175.00], [283.00, 177.00], [78.00, 338.00], [172.00, 380.00], [651.00, 78.00], [338.00, 111.00], [505.00, 144.00], [92.00, 301.00], [198.00, 305.00], [661.00, 132.00], [349.00, 156.00], [541.00, 179.00], [106.00, 336.00], [203.00, 348.00], [305.00, 159.00], [665.00, 160.00], [563.00, 192.00], [80.00, 343.00], [181.00, 385.00], [614.00, 205.00], [291.00, 220.00], [432.00, 320.00], [152.00, 372.00], [43.00, 380.00], [0.00, 386.00], [623.00, 281.00], [306.00, 290.00], [92.00, 357.00], [509.00, 434.00], [304.00, 357.00], [622.00, 368.00], [47.00, 394.00], [0.00, 395.00], [142.00, 405.00], [535.00, 565.00], [655.00, 200.00], [337.00, 217.00], [467.00, 322.00], [191.00, 372.00], [83.00, 375.00], [344.00, 282.00], [655.00, 282.00], [103.00, 343.00], [237.00, 368.00], [22.00, 377.00], [0.00, 379.00], [460.00, 459.00], [305.00, 352.00], [638.00, 355.00], [0.00, 401.00], [110.00, 412.00], [411.00, 570.00], [608.00, 0.00], [608.00, 40.00], [297.00, 75.00], [469.00, 84.00], [0.00, 261.00], [58.00, 263.00], [165.00, 275.00], [625.00, 0.00], [625.00, 39.00], [309.00, 74.00], [486.00, 83.00], [71.00, 264.00], [180.00, 276.00], [599.00, 0.00], [599.00, 44.00], [284.00, 80.00], [440.00, 93.00], [48.00, 271.00], [0.00, 272.00], [157.00, 277.00], [634.00, 0.00], [633.00, 41.00], [319.00, 77.00], [79.00, 269.00], [190.00, 277.00]];
21
+ // const sampleSubsetElementSource = [1.00,6.00,12.00,18.00,24.00,28.00,33.00,39.00,43.00,49.00,54.00,59.00,65.00,72.00,77.00,84.00,90.00,97.00,32.98,18.00],[5.00,11.00,17.00,23.00,27.00,32.00,37.00,42.00,46.00,-1.00,-1.00,62.00,67.00,-1.00,82.00,88.00,95.00,100.00,25.45,15.00],[4.00,10.00,16.00,22.00,26.00,31.00,36.00,41.00,47.00,51.00,57.00,63.00,66.00,74.00,81.00,87.00,93.00,99.00,26.97,18.00],[3.00,8.00,14.00,19.00,25.00,30.00,35.00,40.00,45.00,52.00,58.00,61.00,70.00,75.00,79.00,86.00,92.00,-1.00,30.45,17.00],[2.00,7.00,13.00,20.00,-1.00,29.00,34.00,38.00,44.00,50.00,53.00,60.00,64.00,71.00,78.00,85.00,91.00,98.00,27.89,17.00],[0.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,-1.00,76.00,83.00,-1.00,96.00,3.33,4.00];
22
+
23
+ function makePoseFromCandidateAndSubsetElement(candidate, subsetElement) {
24
+ var pose = [];
25
+ for (let j = 0 ; j < 18; j++) {
26
+ pose.push(candidate[subsetElement[j]]);
27
+ }
28
+ return pose;
29
+ }
30
 
31
+ function makePoseDataFromCandidateAndSubset(candidate, subset) {
32
+ return subset.map(subsetElement => makePoseFromCandidateAndSubsetElement(candidate, subsetElement));
33
+ }
34
 
35
  function addPerson() {
36
  var dx = Math.random() * 100;
37
  var dy = Math.random() * 100;
38
 
39
+ poseData.push(
40
+ makePoseFromCandidateAndSubsetElement(
41
+ sampleCandidateSource.map(point => [point[0] + dx, point[1] + dy]),
42
+ sampleSubsetElementSource));
43
+
44
  Redraw();
45
  }
46
 
 
64
  canvas.width = width ? width : canvas.width;
65
  canvas.height = height ? height : canvas.height;
66
  clearCanvas();
67
+ drawBodyPose();
68
  }
69
 
70
+ function drawBodyPose() {
71
+ const stickWidth = 6;
72
+ const limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10],
73
+ [10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17],
74
+ [1, 16], [16, 18], [3, 17], [6, 18]];
75
+
76
+ const colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0],
77
+ [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255],
78
+ [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]];
79
+
80
+ ctx.globalAlpha = 0.7;
81
 
82
+ for (let i = 0; i < poseData.length; i++) {
83
+ const pose = poseData[i];
84
 
85
  // edge
86
+ for (let j = 0; j < 17; j++) {
87
+ const [X0, Y0] = pose[limbSeq[j][0]-1];
88
+ const [X1, Y1] = pose[limbSeq[j][1]-1];
89
+ ctx.beginPath();
90
+ ctx.lineWidth=stickWidth;
91
+ ctx.strokeStyle = `rgb(${colors[j].join(',')})`;
92
+ ctx.moveTo(X0, Y0);
93
+ ctx.lineTo(X1, Y1);
94
+ ctx.stroke();
 
 
 
 
 
 
 
95
  }
96
 
97
  // node
98
  ctx.font = '12px serif';
99
+ for (let j = 0; j < 18; j++) {
100
+ const [x, y] = pose[j];
101
+ ctx.beginPath();
102
+ ctx.arc(x, y, stickWidth*1.2, 0, 2 * Math.PI);
103
+ ctx.fillStyle = `rgb(${colors[j].join(',')})`;
104
+ ctx.fill();
105
+ // ctx.fillStyle = 'rgb(255,255,255)'
106
+ // ctx.fillText(j, x-3, y+4);
 
 
 
 
 
 
107
  }
108
+ }
109
 
110
+ ctx.globalAlpha = 1.0;
111
  }
112
 
113
  function drawUI() {
 
122
  ctx.beginPath();
123
  ctx.lineWidth=1;
124
  ctx.strokeStyle = 'rgb(255,255,255)';
125
+ ctx.moveTo(dragStart[0], dragStart[1]);
126
+ ctx.lineTo(dragStart[0]+rotateBaseVector[0], dragStart[1]+rotateBaseVector[1]);
127
  ctx.stroke();
128
  }
129
  }
130
 
131
  function Redraw() {
132
  clearCanvas();
133
+ drawBodyPose();
134
  drawUI();
135
  }
136
 
137
+ function getNearestNode(p) {
138
+ let minDistSq = Infinity;
139
+ let personIndex = -1;
140
+ let nodeIndex = -1;
141
+ for (let i = 0; i < poseData.length; i++) {
142
+ const pose = poseData[i];
143
+ for (let j = 0; j < pose.length; j++) {
144
+ const d = distSq(p, pose[j]);
145
+ if (d < minDistSq) {
146
+ minDistSq = d;
147
+ personIndex = i;
148
+ nodeIndex = j;
149
+ }
150
  }
151
  }
152
+ return [personIndex, nodeIndex, Math.sqrt(minDistSq)];
153
  }
154
 
155
  let dragRange = 64;
 
157
 
158
  // ドラッグ中に座標を保持するための変数
159
  let isDragging = false;
160
+ let dragStart = [0, 0];
 
 
 
161
  let dragPersonIndex = -1;
162
  let dragMarks = [];
163
  let dragMode = "";
164
  let rotateBaseVector = null;
165
+ let history = [];
166
 
167
  function getCanvasPosition(event) {
168
  const rect = canvas.getBoundingClientRect();
 
171
  return [x, y];
172
  }
173
 
174
+ function forEachMarkedNodes(fn) {
 
 
 
 
 
 
 
 
 
 
 
 
175
  for (let i = 0; i < dragMarks.length; i++) {
176
+ for (let j = 0; j < dragMarks[i].length; j++) {
177
+ if (dragMarks[i][j]) {
178
+ fn(i, j, poseData[i][j]);
179
+ }
180
  }
181
  }
182
  }
183
 
184
  // Canvas要素上でマウスが押された場合に呼び出される関数
185
  function handleMouseDown(event) {
186
+ const p = getCanvasPosition(event);
187
+ const [personIndex, nodeIndex, minDist] = getNearestNode(p);
188
 
189
  // ドラッグ処理の開始
190
+ dragStart = p;
191
+ dragMarks = poseData.map(pose => pose.map(node => false));
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
  if (event.altKey || event.ctrlKey || event.shiftKey) {
194
+ // dragMarksを設定
195
+ dragMarks[personIndex] = dragMarks[personIndex].map((node) => true);
 
196
  isDragging = true;
197
  if (event.altKey) {
198
  dragMode = "move";
 
204
  }
205
  console.log(dragMode);
206
  } else if (keyDownFlags["Space"]) {
207
+ dragMarks[personIndex] =
208
+ dragMarks[personIndex].map(
209
+ (node) => distSq(p, node) < dragRange ** 2);
210
+ isDragging = dragMarks[personIndex].some((mark) => mark);
 
 
 
 
 
 
211
  dragMode = "move";
212
  } else if (minDist < 16) {
213
+ dragMarks[personIndex][nodeIndex] = true;
214
  isDragging = true;
215
  dragMode = "move";
216
  }
 
220
  function handleMouseMove(event) {
221
  mouseCursor = getCanvasPosition(event);
222
  if (isDragging) {
223
+ const p = getCanvasPosition(event);
224
+ const dragOffset = [p[0] - dragStart[0], p[1] - dragStart[1]];
225
+ const lastPoseData = history[history.length - 1];
 
226
 
227
  if (dragMode == "scale") {
228
  // 拡大縮小
229
+ let xScale = 1 + dragOffset[0] / canvas.width;
230
+ let yScale = 1 + dragOffset[0] / canvas.height;
231
+ forEachMarkedNodes((i, j, node) => {
232
+ const lp = lastPoseData[i][j];
233
+ node[0] = (lp[0] - dragStart[0]) * xScale + dragStart[0];
234
+ node[1] = (lp[1] - dragStart[1]) * yScale + dragStart[1];
235
  });
236
  } else if (dragMode == "rotate") {
237
+ rotateBaseVector = dragOffset;
238
  if (!event.shiftKey) {
239
  dragMode = "rotate2";
240
  console.log("rotate2");
 
242
  console.log("!");
243
  } else if (dragMode == "rotate2") {
244
  // 回転
245
+ let angle = directedAngleTo(rotateBaseVector, dragOffset);
246
+ forEachMarkedNodes((i, j, node) => {
247
+ const lp = lastPoseData[i][j];
248
+ let x = lp[0] - dragStart[0];
249
+ let y = lp[1] - dragStart[1];
250
+ let sin = Math.sin(angle);
251
+ let cos = Math.cos(angle);
252
+ node[0] = x * cos - y * sin + dragStart[0];
253
+ node[1] = x * sin + y * cos + dragStart[1];
254
  });
255
  } else if (dragMode == "move") {
256
  // 移動
257
+ forEachMarkedNodes((i, j, node) => {
258
+ const lp = lastPoseData[i][j];
259
+ node[0] = lp[0] + dragOffset[0];
260
+ node[1] = lp[1] + dragOffset[1];
 
261
  });
262
  }
263
  }
 
267
 
268
  function handleMouseUp(event) {
269
  isDragging = false;
270
+ history.push(JSON.parse(JSON.stringify(poseData)));
271
  Redraw();
272
  }
273
 
 
298
  function initializePose(jsonData,w,h) {
299
  console.log("initializePose");
300
  if (jsonData != null) {
301
+ poseData = makePoseDataFromCandidateAndSubset(jsonData.candidate, jsonData.subset);
302
+ } else {
303
+ poseData = makePoseDataFromCandidateAndSubset(sampleCandidateSource, [sampleSubsetElementSource]);
304
  }
305
+ history.push(JSON.parse(JSON.stringify(poseData)));
306
 
307
  canvas = document.getElementById('canvas');
308
  ctx = canvas.getContext('2d');