-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuwb_dashboard.html
More file actions
1084 lines (929 loc) · 37.6 KB
/
Copy pathuwb_dashboard.html
File metadata and controls
1084 lines (929 loc) · 37.6 KB
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>UWB 3D Positioning System</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
color: #333;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h1 {
text-align: center;
color: white;
margin-bottom: 10px;
font-size: 2.5em;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.subtitle {
text-align: center;
color: rgba(255,255,255,0.9);
margin-bottom: 30px;
font-size: 1.1em;
}
.status {
background: rgba(255, 255, 255, 0.95);
padding: 15px;
border-radius: 10px;
margin-bottom: 20px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
gap: 15px;
}
.status-item {
text-align: center;
}
.status-label {
font-size: 0.9em;
color: #666;
margin-bottom: 5px;
}
.status-value {
font-size: 1.5em;
font-weight: bold;
color: #667eea;
}
.content-grid {
display: grid;
grid-template-columns: 250px 1fr;
gap: 20px;
margin-bottom: 20px;
}
.anchor-section {
background: rgba(255, 255, 255, 0.95);
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.section-title {
font-size: 1.3em;
font-weight: bold;
color: #333;
margin-bottom: 15px;
text-align: center;
}
.anchor-container {
display: grid;
grid-template-columns: 1fr;
gap: 10px;
}
.anchor {
background: rgba(255, 255, 255, 0.95);
padding: 12px;
border-radius: 8px;
border: 2px solid #e0e0e0;
transition: opacity 0.3s, border-color 0.3s;
}
.anchor.disconnected {
opacity: 0.4;
border-color: #ccc;
}
.anchor-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.anchor-name {
font-size: 0.9em;
font-weight: bold;
color: #333;
display: flex;
align-items: center;
gap: 6px;
}
.status-indicator {
width: 8px;
height: 8px;
border-radius: 50%;
display: inline-block;
}
.status-indicator.connected {
background: #4CAF50;
box-shadow: 0 0 6px #4CAF50;
}
.status-indicator.disconnected {
background: #ccc;
}
.anchor-distance {
font-size: 1.1em;
font-weight: bold;
color: #667eea;
}
.bar-container {
width: 100%;
height: 20px;
background: #e0e0e0;
border-radius: 10px;
overflow: hidden;
position: relative;
}
.bar {
height: 100%;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
border-radius: 10px;
transition: width 0.2s ease-out;
min-width: 0px;
}
#viewer3d {
width: 100%;
height: 500px;
background: #1a1a2e;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.position-info {
background: rgba(255, 255, 255, 0.95);
padding: 15px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
margin-top: 10px;
}
.position-coords {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
text-align: center;
}
.coord-item {
background: #f0f0f0;
padding: 10px;
border-radius: 5px;
}
.coord-label {
font-size: 0.8em;
color: #666;
margin-bottom: 3px;
}
.coord-value {
font-size: 1.3em;
font-weight: bold;
color: #667eea;
}
.controls-panel {
background: rgba(255, 255, 255, 0.95);
padding: 15px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
margin-top: 10px;
}
.controls-title {
font-size: 1.1em;
font-weight: bold;
color: #333;
margin-bottom: 12px;
text-align: center;
}
.control-item {
margin-bottom: 15px;
}
.control-label {
display: flex;
justify-content: space-between;
font-size: 0.9em;
color: #666;
margin-bottom: 5px;
}
.control-value {
color: #667eea;
font-weight: bold;
}
.control-hint {
font-size: 0.75em;
color: #999;
margin-top: 3px;
font-style: italic;
}
input[type="range"] {
width: 100%;
height: 6px;
border-radius: 3px;
background: #e0e0e0;
outline: none;
-webkit-appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
background: #667eea;
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 18px;
height: 18px;
border-radius: 50%;
background: #667eea;
cursor: pointer;
border: none;
}
input[type="number"] {
width: 80px;
padding: 5px 8px;
border: 2px solid #e0e0e0;
border-radius: 5px;
font-size: 0.9em;
text-align: center;
background: white;
color: #333;
}
input[type="number"]:focus {
outline: none;
border-color: #667eea;
}
.control-row {
display: flex;
gap: 10px;
align-items: center;
}
.control-row input[type="range"] {
flex: 1;
}
.connection-status {
position: fixed;
top: 20px;
right: 20px;
padding: 10px 20px;
border-radius: 5px;
font-weight: bold;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.connected {
background: #4CAF50;
color: white;
}
.disconnected {
background: #f44336;
color: white;
}
.calibration-button {
position: fixed;
top: 70px;
right: 20px;
padding: 10px 20px;
background: rgba(255, 255, 255, 0.95);
border: none;
border-radius: 5px;
color: #667eea;
font-weight: bold;
font-size: 0.95em;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
transition: all 0.3s;
z-index: 1000;
}
.calibration-button:hover {
background: white;
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
transform: translateY(-2px);
}
.github-link {
position: fixed;
top: 120px;
right: 20px;
padding: 10px 20px;
background: rgba(255, 255, 255, 0.95);
border: none;
border-radius: 5px;
color: #667eea;
font-weight: bold;
font-size: 0.95em;
text-decoration: none;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
transition: all 0.3s;
z-index: 1000;
display: inline-block;
}
.github-link:hover {
background: white;
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
transform: translateY(-2px);
}
@media (max-width: 1200px) {
.content-grid {
grid-template-columns: 1fr;
}
.anchor-container {
grid-template-columns: 1fr;
}
}
@media (max-width: 768px) {
h1 {
font-size: 1.8em;
}
.anchor-container {
grid-template-columns: 1fr;
}
.position-coords {
grid-template-columns: 1fr;
}
.calibration-button {
top: 120px;
font-size: 0.85em;
padding: 8px 16px;
}
.github-link {
top: 170px;
font-size: 0.85em;
padding: 8px 16px;
}
}
</style>
</head>
<body>
<div id="connectionStatus" class="connection-status disconnected">Connecting...</div>
<button class="calibration-button" onclick="window.location.href='calibration.html'">
📏 Calibration
</button>
<a href="https://github.com/DipFlip/ultra-wideband-positioning" target="_blank" class="github-link">
🔗 GitHub
</a>
<div class="container">
<h1>🎯 UWB 3D Positioning System</h1>
<div class="status">
<div class="status-item">
<div class="status-label">Update Rate</div>
<div class="status-value" id="updateRate">0 Hz</div>
</div>
<div class="status-item">
<div class="status-label">Packets</div>
<div class="status-value" id="packetCount">0</div>
</div>
<div class="status-item">
<div class="status-label">Last Update</div>
<div class="status-value" id="lastUpdate">--</div>
</div>
</div>
<div class="content-grid">
<div class="anchor-section">
<div class="section-title">Anchor Distances</div>
<div class="anchor-container" id="anchorContainer">
<!-- Anchors will be dynamically created here -->
</div>
</div>
<div>
<div id="viewer3d"></div>
<div class="position-info">
<div class="position-coords">
<div class="coord-item">
<div class="coord-label">X Position (m)</div>
<div class="coord-value" id="posX">--</div>
</div>
<div class="coord-item">
<div class="coord-label">Y Position (m)</div>
<div class="coord-value" id="posY">--</div>
</div>
<div class="coord-item">
<div class="coord-label">Z Position (m)</div>
<div class="coord-value" id="posZ">--</div>
</div>
</div>
<div id="positionFailureReason" style="display: none; margin-top: 10px; padding: 10px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 5px; color: #856404; font-size: 0.9em; text-align: center;"></div>
</div>
<div class="controls-panel">
<div class="controls-title">Kalman Filter Settings</div>
<div class="control-item">
<div class="control-label">
<span>Process Noise</span>
<span class="control-value" id="processNoiseValue">0.0020</span>
</div>
<div class="control-row">
<input type="range" id="processNoise" min="0.0001" max="0.20" step="0.0001" value="0.002">
<input type="number" id="processNoiseInput" min="0.0001" max="0.20" step="0.0001" value="0.002">
</div>
<div class="control-hint">How much to trust motion model (lower = smoother)</div>
</div>
<div class="control-item">
<div class="control-label">
<span>Measurement Noise</span>
<span class="control-value" id="measurementNoiseValue">0.09</span>
</div>
<div class="control-row">
<input type="range" id="measurementNoise" min="0.01" max="2.0" step="0.01" value="0.09">
<input type="number" id="measurementNoiseInput" min="0.01" max="2.0" step="0.01" value="0.09">
</div>
<div class="control-hint">How much to trust new positions (higher = smoother but more lag)</div>
</div>
<div class="control-item">
<div class="controls-title" style="margin-top: 15px; margin-bottom: 10px;">Trail Visualization</div>
<div style="display: flex; gap: 10px;">
<button id="trailToggle" style="flex: 1; padding: 10px; border: none; border-radius: 5px; background: #667eea; color: white; font-weight: bold; cursor: pointer; font-size: 0.9em;">
Show Trail
</button>
<button id="trailClear" style="flex: 1; padding: 10px; border: none; border-radius: 5px; background: #f44336; color: white; font-weight: bold; cursor: pointer; font-size: 0.9em;">
Clear Trail
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const MAX_DISTANCE = 3; // meters
let lastPacketCount = 0;
let lastUpdateTime = Date.now();
let currentAnchors = new Set();
let failureMessageTimer = null; // Timer for hiding failure messages
function createAnchorElement(anchorId) {
const div = document.createElement('div');
div.className = 'anchor disconnected';
div.id = `anchor${anchorId}`;
div.innerHTML = `
<div class="anchor-header">
<div class="anchor-name">
<span class="status-indicator disconnected" id="status${anchorId}"></span>
<span>A${anchorId}</span>
</div>
<div class="anchor-distance" id="distance${anchorId}">--</div>
</div>
<div class="bar-container">
<div class="bar" id="bar${anchorId}" style="width: 0%"></div>
</div>
`;
return div;
}
// Create all 8 anchors on page load
function initializeAnchors() {
const container = document.getElementById('anchorContainer');
for (let i = 0; i < 8; i++) {
container.appendChild(createAnchorElement(i));
}
}
function updateAnchors(anchors) {
let connectedCount = 0;
// Update all 8 anchors
for (let i = 0; i < 8; i++) {
const anchorId = String(i);
const anchorData = anchors[anchorId];
const anchorElement = document.getElementById(`anchor${anchorId}`);
const statusElement = document.getElementById(`status${anchorId}`);
const distanceElement = document.getElementById(`distance${anchorId}`);
const barElement = document.getElementById(`bar${anchorId}`);
if (anchorData && anchorData.connected) {
// Anchor is connected
connectedCount++;
anchorElement.classList.remove('disconnected');
statusElement.className = 'status-indicator connected';
if (anchorData.distance !== null && anchorData.distance > 0) {
distanceElement.textContent = anchorData.distance.toFixed(3) + ' m';
const percentage = Math.min((anchorData.distance / MAX_DISTANCE) * 100, 100);
barElement.style.width = percentage + '%';
}
} else {
// Anchor is disconnected or never seen
anchorElement.classList.add('disconnected');
statusElement.className = 'status-indicator disconnected';
distanceElement.textContent = '--';
barElement.style.width = '0%';
}
}
}
function updateDashboard(data) {
const now = Date.now();
const elapsed = (now - lastUpdateTime) / 1000;
// Update anchors
if (data.anchors) {
updateAnchors(data.anchors);
}
// Update position
if (data.position) {
updatePosition(data.position);
}
// Update packet count
document.getElementById('packetCount').textContent = data.packet_count || 0;
// Calculate update rate
if (elapsed > 0.5) {
const packetDiff = (data.packet_count || 0) - lastPacketCount;
const updateRate = (packetDiff / elapsed).toFixed(1);
document.getElementById('updateRate').textContent = updateRate + ' Hz';
lastPacketCount = data.packet_count || 0;
lastUpdateTime = now;
}
// Update last update time
const date = new Date();
document.getElementById('lastUpdate').textContent = date.toLocaleTimeString();
// Update connection status
const statusElement = document.getElementById('connectionStatus');
statusElement.textContent = 'Connected';
statusElement.className = 'connection-status connected';
}
function updatePosition(position) {
const failureReasonElement = document.getElementById('positionFailureReason');
if (position.success && position.x !== null) {
document.getElementById('posX').textContent = position.x.toFixed(2);
document.getElementById('posY').textContent = position.y.toFixed(2);
document.getElementById('posZ').textContent = position.z.toFixed(2);
// DO NOT hide failure message on success - let the 5s timer run out
// This ensures failures are visible even if successes occur right after
// Update 3D visualization
// Map real coords to Three.js: X=X, Y=Z(up), Z=depth-Y(north inverted)
if (tagMesh && anchorConfig) {
const roomDepth = anchorConfig.room.depth || 3;
const x = position.x;
const y = position.z;
const z = roomDepth - position.y;
tagMesh.position.set(x, y, z);
// Add point to trail
addTrailPoint(x, y, z);
}
} else {
document.getElementById('posX').textContent = '--';
document.getElementById('posY').textContent = '--';
document.getElementById('posZ').textContent = '--';
// Clear any existing timer (new message replaces old one and resets 5s timer)
if (failureMessageTimer) {
clearTimeout(failureMessageTimer);
}
// Show failure reason if available
if (position.failure_reason) {
failureReasonElement.textContent = '⚠️ Position solver failed: ' + position.failure_reason;
failureReasonElement.style.display = 'block';
} else {
failureReasonElement.textContent = '⚠️ Position solver failed (no reason provided)';
failureReasonElement.style.display = 'block';
}
// Set timer to hide message after 5 seconds
failureMessageTimer = setTimeout(() => {
failureReasonElement.style.display = 'none';
failureMessageTimer = null;
}, 5000);
}
}
function fetchData() {
fetch('/data')
.then(response => response.json())
.then(data => {
updateDashboard(data);
})
.catch(error => {
console.error('Error:', error);
const statusElement = document.getElementById('connectionStatus');
statusElement.textContent = 'Disconnected';
statusElement.className = 'connection-status disconnected';
});
}
// ===== Three.js 3D Visualization =====
let scene, camera, renderer, controls;
let anchorMeshes = {};
let tagMesh;
let anchorConfig = null;
// Trail tracking
let trailPoints = []; // Array of {position: Vector3, timestamp: number}
let trailLines = []; // Array of line segments with different opacities
let trailEnabled = false;
const TRAIL_DURATION = 4000; // 4 seconds in milliseconds
const TRAIL_SEGMENTS = 20; // Number of segments for opacity gradient
function init3DScene() {
const container = document.getElementById('viewer3d');
const width = container.clientWidth;
const height = container.clientHeight;
// Scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x1a1a2e);
scene.fog = new THREE.Fog(0x1a1a2e, 10, 50);
// Camera (positioned to look down at the room from above/side)
camera = new THREE.PerspectiveCamera(60, width / height, 0.1, 100);
camera.position.set(2, 5, 5); // Above and south of center
camera.lookAt(2, 1.0, 1.5);
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
// Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.target.set(2, 1.0, 1.5); // Center of room
// Lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(5, 10, 5);
scene.add(dirLight);
// Grid and axes
const gridHelper = new THREE.GridHelper(10, 20, 0x444444, 0x222222);
gridHelper.position.y = 0;
scene.add(gridHelper);
const axesHelper = new THREE.AxesHelper(1);
scene.add(axesHelper);
// Load anchor config and create room
loadAnchorConfig();
// Animation loop
animate();
}
function loadAnchorConfig() {
fetch('/anchor_config.json')
.then(response => response.json())
.then(config => {
anchorConfig = config;
createRoom(config.room);
createAnchors(config.anchors);
createTag();
})
.catch(error => {
console.error('Could not load anchor config:', error);
// Use default config
createDefaultRoom();
createTag();
});
}
function createRoom(room) {
const width = room.width || 4;
const depth = room.depth || 3;
const height = room.height || 2.5;
// Floor
const floorGeometry = new THREE.PlaneGeometry(width, depth);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x2a2a3e,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.8
});
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2;
floor.position.set(width/2, 0, depth/2);
scene.add(floor);
// No ceiling - removed for better visibility
// Walls (wireframe)
const wallMaterial = new THREE.LineBasicMaterial({ color: 0x667eea, linewidth: 2 });
// Create room outline
const points = [
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(width, 0, 0),
new THREE.Vector3(width, 0, depth),
new THREE.Vector3(0, 0, depth),
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, height, 0),
new THREE.Vector3(width, height, 0),
new THREE.Vector3(width, 0, 0),
new THREE.Vector3(width, height, 0),
new THREE.Vector3(width, height, depth),
new THREE.Vector3(width, 0, depth),
new THREE.Vector3(width, height, depth),
new THREE.Vector3(0, height, depth),
new THREE.Vector3(0, 0, depth),
new THREE.Vector3(0, height, depth),
new THREE.Vector3(0, height, 0),
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, wallMaterial);
scene.add(line);
}
function createDefaultRoom() {
createRoom({ width: 4, depth: 3, height: 2.5 });
}
function createAnchors(anchorsConfig) {
const roomDepth = anchorConfig.room.depth || 3;
for (const [anchorId, anchorData] of Object.entries(anchorsConfig)) {
if (anchorData.enabled) {
const pos = anchorData.position;
// Anchor sphere
// Map real coords to Three.js: X=X, Y=Z(up), Z=depth-Y(north inverted)
const geometry = new THREE.SphereGeometry(0.08, 16, 16);
const material = new THREE.MeshStandardMaterial({
color: 0x4CAF50,
emissive: 0x4CAF50,
emissiveIntensity: 0.5
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(pos[0], pos[2], roomDepth - pos[1]);
scene.add(mesh);
// Label
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = 128;
canvas.height = 64;
context.fillStyle = '#ffffff';
context.font = 'Bold 32px Arial';
context.textAlign = 'center';
context.fillText(`A${anchorId}`, 64, 40);
const texture = new THREE.CanvasTexture(canvas);
const spriteMaterial = new THREE.SpriteMaterial({ map: texture });
const sprite = new THREE.Sprite(spriteMaterial);
sprite.position.set(pos[0], pos[2] + 0.2, roomDepth - pos[1]);
sprite.scale.set(0.6, 0.3, 1); // Doubled size for better visibility
scene.add(sprite);
anchorMeshes[anchorId] = mesh;
}
}
}
function createTag() {
// Create tag as a sphere
const geometry = new THREE.SphereGeometry(0.15, 32, 32);
const material = new THREE.MeshStandardMaterial({
color: 0xff6b35,
emissive: 0xff6b35,
emissiveIntensity: 0.3,
metalness: 0.5,
roughness: 0.5
});
tagMesh = new THREE.Mesh(geometry, material);
// Start at room center: X=2, Y=1.5, Z=1.0 -> Three.js (2, 1.0, 1.5)
const roomDepth = anchorConfig ? anchorConfig.room.depth : 3;
tagMesh.position.set(2, 1.0, roomDepth - 1.5);
scene.add(tagMesh);
}
function addTrailPoint(x, y, z) {
if (!trailEnabled) return;
const now = Date.now();
// Add new point to trail with timestamp
trailPoints.push({
position: new THREE.Vector3(x, y, z),
timestamp: now
});
// Remove points older than 10 seconds
const cutoffTime = now - TRAIL_DURATION;
trailPoints = trailPoints.filter(point => point.timestamp >= cutoffTime);
updateTrailLine();
}
function updateTrailLine() {
// Remove old trail lines if they exist
trailLines.forEach(line => {
scene.remove(line);
line.geometry.dispose();
line.material.dispose();
});
trailLines = [];
// Create new trail lines if we have at least 2 points
if (trailPoints.length >= 2) {
const now = Date.now();
// Divide points into segments for opacity gradient
const pointsPerSegment = Math.max(1, Math.floor(trailPoints.length / TRAIL_SEGMENTS));
for (let segmentIndex = 0; segmentIndex < trailPoints.length - 1; segmentIndex++) {
const point1 = trailPoints[segmentIndex];
const point2 = trailPoints[segmentIndex + 1];
// Calculate opacity based on the age of this segment
const age = now - point1.timestamp;
const normalizedAge = age / TRAIL_DURATION;
const opacity = Math.max(0.1, 1 - normalizedAge); // Min opacity 0.1, max 1.0
// Create a line segment
const positions = [
point1.position.x, point1.position.y, point1.position.z,
point2.position.x, point2.position.y, point2.position.z
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
const material = new THREE.LineBasicMaterial({
color: 0xff6b35,
linewidth: 2,
transparent: true,
opacity: opacity
});
const line = new THREE.Line(geometry, material);
scene.add(line);
trailLines.push(line);
}
}
}
function clearTrail() {
trailPoints = [];
trailLines.forEach(line => {
scene.remove(line);
line.geometry.dispose();
line.material.dispose();
});
trailLines = [];
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
const container = document.getElementById('viewer3d');
const width = container.clientWidth;
const height = container.clientHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
});
// ===== Parameter Controls =====
function setupParameterControls() {
const processNoiseSlider = document.getElementById('processNoise');
const measurementNoiseSlider = document.getElementById('measurementNoise');
const processNoiseInput = document.getElementById('processNoiseInput');
const measurementNoiseInput = document.getElementById('measurementNoiseInput');
const processNoiseValue = document.getElementById('processNoiseValue');
const measurementNoiseValue = document.getElementById('measurementNoiseValue');
// Update displayed values and sync slider/input
processNoiseSlider.addEventListener('input', (e) => {
const value = parseFloat(e.target.value);
processNoiseValue.textContent = value.toFixed(4);
processNoiseInput.value = value;
});
measurementNoiseSlider.addEventListener('input', (e) => {
const value = parseFloat(e.target.value);
measurementNoiseValue.textContent = value.toFixed(2);
measurementNoiseInput.value = value;
});
// Sync text input to slider
processNoiseInput.addEventListener('input', (e) => {
let value = parseFloat(e.target.value);
// Clamp value to valid range
value = Math.max(0.0001, Math.min(0.20, value));
processNoiseSlider.value = value;
processNoiseValue.textContent = value.toFixed(4);
});
measurementNoiseInput.addEventListener('input', (e) => {
let value = parseFloat(e.target.value);
// Clamp value to valid range
value = Math.max(0.01, Math.min(2.0, value));
measurementNoiseSlider.value = value;
measurementNoiseValue.textContent = value.toFixed(2);
});
// Send updates to server on change (not while dragging)
processNoiseSlider.addEventListener('change', (e) => {
updateParameter('process_noise', parseFloat(e.target.value));