| 283 | /* ── Helper: compute camera target from node IDs ────────── */ |
| 284 | |
| 285 | export function computeCameraTarget( |
| 286 | nodes: GraphNode[], |
| 287 | ids: Set<number>, |
| 288 | ): CameraTarget | null { |
| 289 | if (ids.size === 0) return null; |
| 290 | |
| 291 | let cx = 0, |
| 292 | cy = 0, |
| 293 | cz = 0, |
| 294 | count = 0; |
| 295 | for (const node of nodes) { |
| 296 | if (ids.has(node.id)) { |
| 297 | cx += node.x; |
| 298 | cy += node.y; |
| 299 | cz += node.z; |
| 300 | count++; |
| 301 | } |
| 302 | } |
| 303 | if (count === 0) return null; |
| 304 | |
| 305 | cx /= count; |
| 306 | cy /= count; |
| 307 | cz /= count; |
| 308 | |
| 309 | /* Distance based on cluster spread — ensure we never zoom too close */ |
| 310 | let maxDist = 0; |
| 311 | for (const node of nodes) { |
| 312 | if (ids.has(node.id)) { |
| 313 | const d = Math.sqrt( |
| 314 | (node.x - cx) ** 2 + (node.y - cy) ** 2 + (node.z - cz) ** 2, |
| 315 | ); |
| 316 | if (d > maxDist) maxDist = d; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* Minimum distance scales with count: single node = 300, cluster = spread-based */ |
| 321 | const spreadDist = maxDist * 3; |
| 322 | const minDist = count <= 5 ? 300 : 200; |
| 323 | const distance = Math.max(minDist, spreadDist); |
| 324 | const lookAt = new THREE.Vector3(cx, cy, cz); |
| 325 | const position = new THREE.Vector3( |
| 326 | cx + distance * 0.2, |
| 327 | cy + distance * 0.15, |
| 328 | cz + distance, |
| 329 | ); |
| 330 | |
| 331 | return { position, lookAt }; |
| 332 | } |