-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverseKinematicsV2.js
855 lines (640 loc) · 24.5 KB
/
inverseKinematicsV2.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
let canReachFarther = true;
// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
const keys = {37: 1, 38: 1, 39: 1, 40: 1};
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
function preventDefaultForScrollKeys(e) {
if (keys[e.keyCode]) {
preventDefault(e);
return false;
}
}
function disableScroll() {
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
}
function enableScroll() {
if (window.removeEventListener)
window.removeEventListener('DOMMouseScroll', preventDefault, false);
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
document.onkeydown = null;
}
function createPoint({ x, y, z , size }) {
const pointGeometry = new THREE.Geometry();
pointGeometry.vertices.push(new THREE.Vector3(x, y, z));
const pointMaterial = new THREE.PointsMaterial({ size, sizeAttenuation: false, color: 0xffffff }); // 0x000000
const point = new THREE.Points(pointGeometry, pointMaterial);
return point;
}
function createLine({ x0, y0, z0, x1, y1, z1 }) {
const lineGeometry = new THREE.Geometry();
lineGeometry.vertices.push(new THREE.Vector3(x0, y0, z0)); // TODO: switch to a BufferGeometry for performance
lineGeometry.vertices.push(new THREE.Vector3(x1, y1, z1));
const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffffff });
const line = new THREE.Line(lineGeometry, lineMaterial);
return line;
}
function createPlane({ width, height, widthSegments, heightSegments }) {
const planeGeometry = new THREE.PlaneGeometry(width, height, widthSegments, heightSegments);
planeMaterial = new THREE.MeshBasicMaterial({ color: (Math.random() * 0xffffff), side: THREE.DoubleSide });
// const imageTexture = new THREE.TextureLoader().load('photographs/eye.jpg');
// const planeMaterial = new THREE.MeshBasicMaterial({ map: imageTexture, side: THREE.DoubleSide });
const plane = new THREE.Mesh(planeGeometry, planeMaterial);
return plane;
}
function updateColumn(column, columnWidth, columnImageHeight, points) {
for (let i = 0; i < column.length; i += 1) {
// update points
column[i][0].geometry.vertices[0].x = points[i].x - columnWidth / 2;
column[i][0].geometry.vertices[0].y = points[i].y;
column[i][0].geometry.vertices[0].z = points[i].z;
column[i][0].geometry.verticesNeedUpdate = true;
column[i][1].geometry.vertices[0].x = points[i].x + columnWidth / 2;
column[i][1].geometry.vertices[0].y = points[i].y;
column[i][1].geometry.vertices[0].z = points[i].z;
column[i][1].geometry.verticesNeedUpdate = true;
column[i][2].geometry.vertices[0].x = points[i + 1].x - columnWidth / 2;
column[i][2].geometry.vertices[0].y = points[i + 1].y;
column[i][2].geometry.vertices[0].z = points[i + 1].z;
column[i][2].geometry.verticesNeedUpdate = true;
column[i][3].geometry.vertices[0].x = points[i + 1].x + columnWidth / 2;
column[i][3].geometry.vertices[0].y = points[i + 1].y;
column[i][3].geometry.vertices[0].z = points[i + 1].z;
column[i][3].geometry.verticesNeedUpdate = true;
// update plane
column[i][4].geometry.vertices[0].x = points[i].x - columnWidth / 2;
column[i][4].geometry.vertices[0].y = points[i].y;
column[i][4].geometry.vertices[0].z = points[i].z;
column[i][4].geometry.vertices[1].x = points[i].x + (columnWidth / 2);
column[i][4].geometry.vertices[1].y = points[i].y;
column[i][4].geometry.vertices[1].z = points[i].z;
column[i][4].geometry.vertices[2].x = points[i + 1].x - columnWidth / 2;
column[i][4].geometry.vertices[2].y = points[i + 1].y;
column[i][4].geometry.vertices[2].z = points[i + 1].z;
column[i][4].geometry.vertices[3].x = points[i + 1].x + columnWidth / 2;
column[i][4].geometry.vertices[3].y = points[i + 1].y;
column[i][4].geometry.vertices[3].z = points[i + 1].z;
column[i][4].geometry.verticesNeedUpdate = true;
if (points[i].y < -10) {
column[i][4].traverse((child) => {
if (child instanceof THREE.Mesh) {
child.visible = true;
}
});
}
// If I switch to using BufferGeometry, then I should use:
// geometry.attributes.position.needsUpdate = true;
}
}
function moveTowards(currentVal, goalVal, moveSpeed) {
let newVal = currentVal;
if (currentVal < goalVal) {
const distLeft = goalVal - currentVal
if (distLeft < moveSpeed) {
newVal = currentVal + distLeft;
} else {
newVal = currentVal + moveSpeed;
}
}
if (currentVal > goalVal) {
const distLeft = currentVal - goalVal
if (distLeft < moveSpeed) {
newVal = currentVal - distLeft;
} else {
newVal = currentVal - moveSpeed;
}
}
if (currentVal == goalVal) {
return { reached: true, newVal };
}
return { reached: false, newVal };
}
function launchViz(numImages) {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.001, 1000);
camera.position.z = 5.5;
camera.position.y = -1.5;
const renderer = new THREE.WebGLRenderer(antialias = true);
renderer.setSize(window.innerWidth, window.innerHeight);
// each image consists of 4 points and a plane
// Images are distributed into one of 3 columns
const leftColumn = [];
const middleColumn = [];
const rightColumn = [];
const leftColumnWidth = 3;
const middleColumnWidth = 3;
const rightColumnWidth = 3;
const leftColumnImageHeight = 3;
const middleColumnImageHeight = 3;
const rightColumnImageHeight = 3;
const leftColumnStart = 0;
const leftColumnEnd = Math.floor(numImages/3);
const middleColumnStart = leftColumnEnd;
const middleColumnEnd = Math.ceil(numImages/3) * 2;
const rightColumnStart = middleColumnEnd;
const rightColumnEnd = numImages;
const spacingGap = 0.2;
const middleColumnPoints = [];
for(let i = middleColumnStart; i < middleColumnEnd; i += 1) {
const n = i - middleColumnStart;
middleColumn[n] = [];
const xLeft = -middleColumnWidth / 2;
const xRight = middleColumnWidth / 2;
let heightShift = 0;
if (((middleColumnEnd - middleColumnStart) % 2) > 0) {
heightShift = (middleColumnImageHeight / 2)
}
const middleHeight = Math.floor((middleColumnEnd - middleColumnStart) / 2) * 0.1;
const yBottom = n * 0.1 - middleHeight - heightShift;
const yTop = (n + 1) * 0.1 - middleHeight - heightShift;
const point0 = createPoint({ x: xLeft, y: yBottom, z: 0, size: 3 });
middleColumn[n][0] = point0;
point0.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point0);
middleColumnPoints[n] = { x: xLeft + middleColumnWidth / 2, y: yBottom, z: 0 };
const point1 = createPoint({ x: xRight, y: yBottom, z: 0, size: 3 });
middleColumn[n][1] = point1;
point1.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point1);
const point2 = createPoint({ x: xLeft, y: yTop, z: 0, size: 3 });
middleColumn[n][2] = point2;
point2.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point2);
const lastImage = (i == middleColumnEnd - 1);
if (lastImage) {
middleColumnPoints[n + 1] = { x: xLeft + middleColumnWidth / 2, y: yTop, z: 0 };
}
const point3 = createPoint({ x: xRight, y: yTop, z: 0, size: 3 });
middleColumn[n][3] = point3;
point3.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point3);
}
let middleColumnCircumference = 0;
for (let i = 0; i < middleColumn.length; i += 1) {
const plane = createPlane({ width: 3, height: 3, widthSegments: 1, heightSegments: 1 });
plane.traverse((child) => {
child.frustumCulled = false;
if (child instanceof THREE.Mesh) {
child.visible = false;
}
});
scene.add(plane);
const planeX = 0;// middleColumn[i][0].geometry.vertices[0].x + (middleColumnWidth / 2);
const planeY = 0;// middleColumn[i][0].geometry.vertices[0].y + (middleColumnImageHeight / 2);
const planeZ = 0;// middleColumn[i][0].geometry.vertices[0].z;
plane.position.x = planeX;
plane.position.y = planeY;
plane.position.z = planeZ;
middleColumn[i][4] = plane;
middleColumnCircumference += middleColumnImageHeight;
}
// ----- Left
const leftColumnPoints = [];
for(let i = leftColumnStart; i < leftColumnEnd; i += 1) {
const n = i - leftColumnStart;
leftColumn[n] = [];
const xLeft = (-middleColumnWidth / 2) - leftColumnWidth - spacingGap;
const xRight = (-middleColumnWidth / 2) - spacingGap;
let heightShift = 0;
if (((leftColumnEnd - leftColumnStart) % 2) > 0) {
heightShift = (leftColumnImageHeight / 2)
}
const middleHeight = Math.floor((leftColumnEnd - leftColumnStart) / 2) * 0.1;
const yBottom = n * 0.1 - middleHeight - heightShift;
const yTop = (n + 1) * 0.1 - middleHeight - heightShift;
const point0 = createPoint({ x: xLeft, y: yBottom, z: 0, size: 3 });
leftColumn[n][0] = point0;
point0.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point0);
leftColumnPoints[n] = { x: xLeft + leftColumnWidth / 2, y: yBottom, z: 0 };
const point1 = createPoint({ x: xRight, y: yBottom, z: 0, size: 3 });
leftColumn[n][1] = point1;
point1.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point1);
const point2 = createPoint({ x: xLeft, y: yTop, z: 0, size: 3 });
leftColumn[n][2] = point2;
point2.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point2);
const lastImage = (i == leftColumnEnd - 1);
if (lastImage) {
leftColumnPoints[n + 1] = { x: xLeft + leftColumnWidth / 2, y: yTop, z: 0 };
}
const point3 = createPoint({ x: xRight, y: yTop, z: 0, size: 3 });
leftColumn[n][3] = point3;
point3.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point3);
}
let leftColumnCircumference = 0;
for (let i = 0; i < leftColumn.length; i += 1) {
const plane = createPlane({ width: 3, height: 3, widthSegments: 1, heightSegments: 1 });
plane.traverse((child) => {
child.frustumCulled = false;
if (child instanceof THREE.Mesh) {
child.visible = false;
}
});
scene.add(plane);
const planeX = 0;// middleColumn[i][0].geometry.vertices[0].x + (middleColumnWidth / 2);
const planeY = 0;// middleColumn[i][0].geometry.vertices[0].y + (middleColumnImageHeight / 2);
const planeZ = 0;// middleColumn[i][0].geometry.vertices[0].z;
plane.position.x = planeX;
plane.position.y = planeY;
plane.position.z = planeZ;
leftColumn[i][4] = plane;
leftColumnCircumference += leftColumnImageHeight;
}
// ----- Right
const rightColumnPoints = [];
for(let i = rightColumnStart; i < rightColumnEnd; i += 1) {
const n = i - rightColumnStart;
rightColumn[n] = [];
const xLeft = (middleColumnWidth / 2) + spacingGap;
const xRight = (middleColumnWidth / 2) + rightColumnWidth + spacingGap;
let heightShift = 0;
if (((rightColumnEnd - rightColumnStart) % 2) > 0) {
heightShift = (rightColumnImageHeight / 2)
}
const middleHeight = Math.floor((rightColumnEnd - rightColumnStart) / 2) * 0.1;
const yBottom = n * 0.1 - middleHeight - heightShift;
const yTop = (n + 1) * 0.1 - middleHeight - heightShift;
const point0 = createPoint({ x: xLeft, y: yBottom, z: 0, size: 3 });
rightColumn[n][0] = point0;
point0.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point0);
rightColumnPoints[n] = { x: xLeft + rightColumnWidth / 2, y: yBottom, z: 0 };
const point1 = createPoint({ x: xRight, y: yBottom, z: 0, size: 3 });
rightColumn[n][1] = point1;
point1.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point1);
const point2 = createPoint({ x: xLeft, y: yTop, z: 0, size: 3 });
rightColumn[n][2] = point2;
point2.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point2);
const lastImage = (i == rightColumnEnd - 1);
if (lastImage) {
rightColumnPoints[n + 1] = { x: xLeft + rightColumnWidth / 2, y: yTop, z: 0 };
}
const point3 = createPoint({ x: xRight, y: yTop, z: 0, size: 3 });
rightColumn[n][3] = point3;
point3.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point3);
}
let rightColumnCircumference = 0;
for (let i = 0; i < rightColumn.length; i += 1) {
const plane = createPlane({ width: 3, height: 3, widthSegments: 1, heightSegments: 1 });
plane.traverse((child) => {
child.frustumCulled = false;
if (child instanceof THREE.Mesh) {
child.visible = false;
}
});
scene.add(plane);
const planeX = 0;// middleColumn[i][0].geometry.vertices[0].x + (middleColumnWidth / 2);
const planeY = 0;// middleColumn[i][0].geometry.vertices[0].y + (middleColumnImageHeight / 2);
const planeZ = 0;// middleColumn[i][0].geometry.vertices[0].z;
plane.position.x = planeX;
plane.position.y = planeY;
plane.position.z = planeZ;
rightColumn[i][4] = plane;
rightColumnCircumference += rightColumnImageHeight;
}
// --------------------------------------------------------------------------
document.body.appendChild(renderer.domElement);
// --------------------------------------------------------------------------
const crunchedPosition = { x: 0, y: -1, z: 0 };
const extendedPosition = { x: 0, y: 2, z: 15 }; // { x: 0, y: 2, z: 3 };
const downPosition = { x: 0, y: -999999999999, z: 0 };
const homePosition = { x: 0, y: -1.5, z: 5.5 };
const backPosition = { x: 0, y: -1.5, z: 25 };
const viewingOriginPosition = { x: 0, y: -40, z: 60 };// { x: 0, y: -30, z: 7 };
const moveCycle = [crunchedPosition, extendedPosition, downPosition];
const cameraMoveCyle = [homePosition, backPosition, viewingOriginPosition];
let currentGoalMoveIndex = 1;
let xVal = crunchedPosition.x + 0.0001; // cant be 0?
let yVal = crunchedPosition.y;
let zVal = crunchedPosition.z;
disableScroll();
let nextIndex = 1;
let previousIndex = 0;
let camX = camera.position.x;
let camY = camera.position.y;
let camZ = camera.position.z;
const moveSpeed = 0.25;
let galleryMode = false;
window.addEventListener("wheel", function(e) {
if (galleryMode) {
// pass
} else {
if (e.deltaY > 0) {
currentGoalMoveIndex = nextIndex;
} else {
currentGoalMoveIndex = previousIndex;
}
//if (e.deltaY < 0) {
const goalXVal = moveCycle[currentGoalMoveIndex].x;
const goalYVal = moveCycle[currentGoalMoveIndex].y;
const goalZVal = moveCycle[currentGoalMoveIndex].z;
const oldYVal = xVal;
const xMove = moveTowards(xVal, goalXVal, moveSpeed);
xVal = xMove.newVal;
const yMove = moveTowards(yVal, goalYVal, moveSpeed);
yVal = yMove.newVal;
const zMove = moveTowards(zVal, goalZVal, moveSpeed);
zVal = zMove.newVal;
let camGoalXVal = cameraMoveCyle[currentGoalMoveIndex].x;
let camGoalYVal = cameraMoveCyle[currentGoalMoveIndex].y;
let camGoalZVal = cameraMoveCyle[currentGoalMoveIndex].z;
if ((yVal > -5) && (currentGoalMoveIndex == 2)) {
camGoalXVal = cameraMoveCyle[1].x;
camGoalYVal = cameraMoveCyle[1].y;
camGoalZVal = cameraMoveCyle[1].z;
}
if (yVal < -5) {
camGoalXVal = cameraMoveCyle[2].x;
camGoalYVal = cameraMoveCyle[2].y;
camGoalZVal = cameraMoveCyle[2].z;
galleryMode = true;
}
const camXMove = moveTowards(camX, camGoalXVal, moveSpeed);
camX = camXMove.newVal;
const camYMove = moveTowards(camY, camGoalYVal, moveSpeed);
camY = camYMove.newVal;
const camZMove = moveTowards(camZ, camGoalZVal, moveSpeed * 2);
camZ = camZMove.newVal;
if ((!canReachFarther) && (currentGoalMoveIndex == 2)) {
console.log("!!!!!");
}
if (xMove.reached == true && yMove.reached == true && zMove.reached == true) {
if ((nextIndex > 1) && (e.deltaY < 0)) {
nextIndex -= 1;
}
if ((previousIndex > 0) && (e.deltaY < 0)) {
previousIndex -= 1;
}
if ((nextIndex < (moveCycle.length - 1)) && (e.deltaY > 0)) {
nextIndex += 1;
}
if ((previousIndex < (moveCycle.length - 2)) && (e.deltaY > 0)) {
previousIndex += 1;
}
}
}
}, true);
window.setInterval(function() {
if (galleryMode) {
const xMove = moveTowards(xVal, downPosition.x, moveSpeed * 2);
xVal = xMove.newVal;
const yMove = moveTowards(yVal, downPosition.y, moveSpeed * 2);
yVal = yMove.newVal;
const zMove = moveTowards(zVal, downPosition.z, moveSpeed * 2);
zVal = zMove.newVal;
camGoalXVal = cameraMoveCyle[2].x;
camGoalYVal = cameraMoveCyle[2].y;
camGoalZVal = cameraMoveCyle[2].z;
const camXMove = moveTowards(camX, camGoalXVal, moveSpeed);
camX = camXMove.newVal;
const camYMove = moveTowards(camY, camGoalYVal, moveSpeed);
camY = camYMove.newVal;
const camZMove = moveTowards(camZ, camGoalZVal, moveSpeed);
camZ = camZMove.newVal;
}
const middleGoalPos = { x: xVal, y: yVal, z: zVal };
const middlePoints = fabrik(middleColumnPoints, middleGoalPos);
if (middlePoints) {
updateColumn(middleColumn, middleColumnWidth, middleColumnImageHeight, middlePoints);
} else {
canReachFarther = false;
}
const leftGoalPos = { x: xVal - middleColumnWidth - spacingGap, y: yVal, z: zVal };
const leftPoints = fabrik(leftColumnPoints, leftGoalPos);
if (leftPoints) {
updateColumn(leftColumn, leftColumnWidth, leftColumnImageHeight, leftPoints);
}
const rightGoalPos = { x: xVal + middleColumnWidth + spacingGap, y: yVal, z: zVal };
const rightPoints = fabrik(rightColumnPoints, rightGoalPos);
if (rightPoints) {
updateColumn(rightColumn, rightColumnWidth, rightColumnImageHeight, rightPoints);
}
/*
if (points) {
updateColumn(middleColumn, middleColumnWidth, middleColumnImageHeight, middlePoints);
} else {
canReachFarther = false;
}
*/
// update camera
camera.position.x = camX;
camera.position.y = camY;
camera.position.z = camZ;
// animate();
renderer.render(scene, camera);
}, 100);
}
// ---------------------------- Inverse Kinematics ----------------------------
function distance(firstPoint, secondPoint) {
const xDif = Math.abs(secondPoint.x - firstPoint.x);
const yDif = Math.abs(secondPoint.y - firstPoint.y);
const zDif = Math.abs(secondPoint.z - firstPoint.z);
const dist = Math.sqrt(xDif * xDif + yDif * yDif + zDif * zDif);
return dist;
}
function needToMove(endEffectorPos, goalPos, epsilon) {
const distFromGoal = distance(endEffectorPos, goalPos);
return (distFromGoal > epsilon);
}
function targetReachable(points, goalPos) {
// console.log('Checking if the target is reachable');
const basePoint = points[0];
let maxReach = 0;
let lastPoint = basePoint;
points.forEach((point) => {
maxReach += distance(lastPoint, point);
lastPoint = point;
});
// console.log('Max reach is ' + maxReach);
const distFromGoal = distance(basePoint, goalPos);
const isReachable = distFromGoal <= maxReach;
return { isReachable, maxReach };
}
function reachUntilMaxInDirection(direction) {
// TODO: Make points travel along unit vector towards input direction
console.log('Unimplemented: Reaching until max');
}
function findMagnitude(vector) {
const xSqrd = vector.x * vector.x;
const ySqrd = vector.y * vector.y;
const zSqrd = vector.z * vector.z;
const mag = Math.sqrt(xSqrd + ySqrd + zSqrd);
return mag;
}
function normalize(vector) {
const mag = findMagnitude(vector);
const normX = vector.x / mag;
const normY = vector.y / mag;
const normZ = vector.z / mag;
const normVec = { x: normX, y: normY, z: normZ };
return normVec;
}
// Part one
function fabrik_finalToRoot(points, goalPos) {
let currentGoal = goalPos;
for (let i = points.length - 1; i > 0; i -= 1) {
const length = distance(points[i - 1], points[i]);
points[i] = {
x: currentGoal.x,
y: currentGoal.y,
z: currentGoal.z,
};
const lineCurGoalToCurManip = {
x: points[i - 1].x - currentGoal.x,
y: points[i - 1].y - currentGoal.y,
z: points[i - 1].z - currentGoal.z,
}
const lineDirection = normalize(lineCurGoalToCurManip);
const updatedLength = {
x: lineDirection.x * length,
y: lineDirection.y * length,
z: lineDirection.z * length,
};
currentGoal = {
x: currentGoal.x + updatedLength.x,
y: currentGoal.y + updatedLength.y,
z: currentGoal.z + updatedLength.z,
}
}
return points;
}
// Part two
function fabrik_rootToFinal(points, goalPos, length) {
let base = points[0];
for(let i = 0; i < points.length - 1; i += 1) {
// const length = distance(points[i], points[i + 1]);
const lineCurGoalToCurPt = {
x: points[i + 1].x - points[i].x,
y: points[i + 1].y - points[i].y,
z: points[i + 1].z - points[i].z,
}
const lineDirection = normalize(lineCurGoalToCurPt);
const updatedLength = {
x: lineDirection.x * length,
y: lineDirection.y * length,
z: lineDirection.z * length,
};
// This is where constraint adjustment would happen.
// Adjust the point before assigning it
points[i + 1] = {
x: points[i].x + updatedLength.x,
y: points[i].y + updatedLength.y,
z: points[i].z + updatedLength.z,
}
}
return points;
}
function fabrik(points, goalPos, length = 3, epsilon = 0.05) {
const { isReachable, maxReach } = targetReachable(points, goalPos)
// console.log(isReachable, maxReach);
if (isReachable) {
// console.log('Target reachable, running FABRIK');
let endEffectorPos = points[points.length - 1];
while(needToMove(endEffectorPos, goalPos, epsilon)) {
points = fabrik_finalToRoot(points, goalPos); // Part one
points = fabrik_rootToFinal(points, goalPos, length); // Part two
endEffectorPos = points[points.length - 1];
}
} else {
// console.log('Target not reachable');
// console.log('Target not reachable');
const direction = normalize(goalPos);
// reach until max in direction of goal
const reachGoalX = direction.x * (maxReach * 0.99);
const reachGoalY = direction.y * (maxReach * 0.99);
const reachGoalZ = direction.z * (maxReach * 0.99);
reachGoalPos = { x: reachGoalX, y: reachGoalY, z: reachGoalZ };
// console.log(reachGoalPos);
return fabrik(points, reachGoalPos);
}
return points;
// moveArm(points);
}
function testFabrik() {
// console.log('testing FABRIK');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 3;
renderer = new THREE.WebGLRenderer(antialias = true);
renderer.setSize(window.innerWidth, window.innerHeight);
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
const point0 = createPoint({ x: 0, y: 0, z: 0, size: 3});
const point1 = createPoint({ x: 0, y: 1, z: 0, size: 3});
const point2 = createPoint({ x: 0, y: 2, z: 0, size: 3});
point0.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point0);
point1.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point1);
point2.traverse((object) => {
object.frustumCulled = false;
});
scene.add(point2);
let points = [
{ x: 0, y: 0, z: 0 },
{ x: 0, y: 1, z: 0 },
{ x: 0, y: 2, z: 0 }
];
const goalPos = { x: 0, y: 2, z: 0 };
points = fabrik(points, goalPos);
point0.geometry.vertices[0].x = points[0].x;
point0.geometry.vertices[0].y = points[0].y;
point0.geometry.vertices[0].z = points[0].z;
point1.geometry.vertices[0].x = points[1].x;
point1.geometry.vertices[0].y = points[1].y;
point1.geometry.vertices[0].z = points[1].z;
point2.geometry.vertices[0].x = points[2].x;
point2.geometry.vertices[0].y = points[2].y;
point2.geometry.vertices[0].z = points[2].z;
document.body.appendChild(renderer.domElement);
animate();
}
//testFabrik();
launchViz(99);