| 1302 | /* Helper: compute camera target from node IDs */ |
| 1303 | |
| 1304 | export function computeCameraTarget( |
| 1305 | nodes: GraphNode[], |
| 1306 | ids: Set<number>, |
| 1307 | ): CameraTarget | null { |
| 1308 | if (ids.size === 0) return null; |
| 1309 | |
| 1310 | let cx = 0, |
| 1311 | cy = 0, |
| 1312 | cz = 0, |
| 1313 | count = 0; |
| 1314 | for (const node of nodes) { |
| 1315 | if (ids.has(node.id)) { |
| 1316 | cx += node.x; |
| 1317 | cy += node.y; |
| 1318 | cz += node.z; |
| 1319 | count++; |
| 1320 | } |
| 1321 | } |
| 1322 | if (count === 0) return null; |
| 1323 | |
| 1324 | cx /= count; |
| 1325 | cy /= count; |
| 1326 | cz /= count; |
| 1327 | |
| 1328 | /* Distance based on cluster spread: ensure we never zoom too close */ |
| 1329 | let maxDist = 0; |
| 1330 | for (const node of nodes) { |
| 1331 | if (ids.has(node.id)) { |
| 1332 | const d = Math.sqrt( |
| 1333 | (node.x - cx) ** 2 + (node.y - cy) ** 2 + (node.z - cz) ** 2, |
| 1334 | ); |
| 1335 | if (d > maxDist) maxDist = d; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | /* Minimum distance scales with count: single node = 300, cluster = spread-based */ |
| 1340 | const spreadDist = maxDist * 3; |
| 1341 | const minDist = count <= 5 ? 300 : 200; |
| 1342 | const distance = Math.max(minDist, spreadDist); |
| 1343 | const lookAt = new THREE.Vector3(cx, cy, cz); |
| 1344 | const position = new THREE.Vector3( |
| 1345 | cx + distance * 0.2, |
| 1346 | cy + distance * 0.15, |
| 1347 | cz + distance, |
| 1348 | ); |
| 1349 | |
| 1350 | return { position, lookAt }; |
| 1351 | } |