Pushpak21 commited on
Commit
6dd991a
·
verified ·
1 Parent(s): 6b173e0

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +19 -84
index.html CHANGED
@@ -12,96 +12,31 @@
12
  </head>
13
  <body>
14
  <h1>DCM Assembly Visualizer</h1>
15
- <input type="file" id="file1" accept=".txt"> File A<br>
16
- <input type="file" id="file2" accept=".txt"> File B<br><br>
17
- <button onclick="uploadFiles()">Visualize</button>
18
- <label><input type="checkbox" id="showNormals" onchange="toggleNormals()"> Show Normal Vectors (Planes)</label>
19
  <div id="plot"></div>
20
 
21
  <script>
22
  let allTraces = [];
23
  let normalTraces = [];
24
 
25
- async function uploadFiles() {
26
- const file1 = document.getElementById("file1").files[0];
27
- const file2 = document.getElementById("file2").files[0];
28
-
29
- if (!file1 && !file2) {
30
- alert("Please select at least one file.");
31
- return;
32
- }
33
-
34
  allTraces = [];
35
  normalTraces = [];
36
 
37
- if (file1) {
38
- const data1 = await sendFile(file1);
39
- extractTraces(data1, 'File A', 'red', 'blue');
40
  }
41
 
42
- if (file2) {
43
- const data2 = await sendFile(file2);
44
- extractTraces(data2, 'File B', 'purple', 'orange');
45
- }
46
-
47
- Plotly.newPlot('plot', [...allTraces], {
48
  margin: { t: 30 },
49
  scene: { aspectmode: 'data' },
50
  title: '3D Assembly Viewer'
51
  });
52
  }
53
 
54
- async function sendFile(file) {
55
- const formData = new FormData();
56
- formData.append("file", file);
57
- const res = await fetch("/parse", {
58
- method: "POST",
59
- body: formData
60
- });
61
- return await res.json();
62
- }
63
-
64
- function createPlaneTrace(x, y, z, nx, ny, nz, color) {
65
- const size = 0.5;
66
- let ux = 1, uy = 0, uz = 0;
67
- if (Math.abs(nx) < 0.9) {
68
- ux = 0; uy = 1; uz = 0;
69
- }
70
- let vx = ny * uz - nz * uy;
71
- let vy = nz * ux - nx * uz;
72
- let vz = nx * uy - ny * ux;
73
- const len = Math.sqrt(vx**2 + vy**2 + vz**2);
74
- vx /= len; vy /= len; vz /= len;
75
- const wx = ny * vz - nz * vy;
76
- const wy = nz * vx - nx * vz;
77
- const wz = nx * vy - ny * vx;
78
-
79
- const planePoints = [];
80
- for (let dx of [-1, 1]) {
81
- for (let dy of [-1, 1]) {
82
- planePoints.push([
83
- x + dx * vx * size + dy * wx * size,
84
- y + dx * vy * size + dy * wy * size,
85
- z + dx * vz * size + dy * wz * size
86
- ]);
87
- }
88
- }
89
-
90
- return {
91
- type: 'mesh3d',
92
- x: planePoints.map(p => p[0]),
93
- y: planePoints.map(p => p[1]),
94
- z: planePoints.map(p => p[2]),
95
- i: [0, 0],
96
- j: [1, 2],
97
- k: [2, 3],
98
- opacity: 0.5,
99
- color: color,
100
- visible: false
101
- };
102
- }
103
-
104
- function extractTraces(data, prefix, baseColor, normalColor) {
105
  for (const [tag, g] of Object.entries(data.geometry)) {
106
  const [x, y, z] = g.base_point;
107
  const [nx, ny, nz] = g.normal;
@@ -111,11 +46,19 @@
111
  mode: 'markers+text',
112
  x: [x], y: [y], z: [z],
113
  marker: { color: baseColor, size: 5 },
114
- text: [`${prefix}: ${g.label || tag}`],
115
  textposition: 'top center'
116
  });
117
 
118
- normalTraces.push(createPlaneTrace(x, y, z, nx, ny, nz, normalColor));
 
 
 
 
 
 
 
 
119
  }
120
 
121
  for (const c of data.constraints) {
@@ -134,15 +77,7 @@
134
  }
135
  }
136
 
137
- function toggleNormals() {
138
- const checked = document.getElementById("showNormals").checked;
139
- const updatedTraces = normalTraces.map(t => ({ ...t, visible: checked }));
140
- Plotly.react('plot', [...allTraces, ...updatedTraces], {
141
- margin: { t: 30 },
142
- scene: { aspectmode: 'data' },
143
- title: '3D Assembly Viewer'
144
- });
145
- }
146
  </script>
147
  </body>
148
  </html>
 
12
  </head>
13
  <body>
14
  <h1>DCM Assembly Visualizer</h1>
15
+ <p>This visualizer is now intended to be used with a Streamlit frontend for file upload.</p>
 
 
 
16
  <div id="plot"></div>
17
 
18
  <script>
19
  let allTraces = [];
20
  let normalTraces = [];
21
 
22
+ async function drawFromServer() {
23
+ const res = await fetch("/parsed_data.json");
24
+ const payload = await res.json();
 
 
 
 
 
 
25
  allTraces = [];
26
  normalTraces = [];
27
 
28
+ for (const [color, label, data] of payload.files) {
29
+ extractTraces(data, color, label);
 
30
  }
31
 
32
+ Plotly.newPlot('plot', [...allTraces, ...normalTraces.filter(t => t.visible)], {
 
 
 
 
 
33
  margin: { t: 30 },
34
  scene: { aspectmode: 'data' },
35
  title: '3D Assembly Viewer'
36
  });
37
  }
38
 
39
+ function extractTraces(data, baseColor, normalColor) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  for (const [tag, g] of Object.entries(data.geometry)) {
41
  const [x, y, z] = g.base_point;
42
  const [nx, ny, nz] = g.normal;
 
46
  mode: 'markers+text',
47
  x: [x], y: [y], z: [z],
48
  marker: { color: baseColor, size: 5 },
49
+ text: [g.label || tag],
50
  textposition: 'top center'
51
  });
52
 
53
+ normalTraces.push({
54
+ type: 'scatter3d',
55
+ mode: 'lines',
56
+ x: [x, x + nx],
57
+ y: [y, y + ny],
58
+ z: [z, z + nz],
59
+ line: { color: normalColor, width: 2 },
60
+ visible: true
61
+ });
62
  }
63
 
64
  for (const c of data.constraints) {
 
77
  }
78
  }
79
 
80
+ drawFromServer();
 
 
 
 
 
 
 
 
81
  </script>
82
  </body>
83
  </html>