| 249 | /* ── Helper: compute camera target from node IDs ────────── */ |
| 250 | |
| 251 | export function computeCameraTarget( |
| 252 | nodes: GraphNode[], |
| 253 | ids: Set<number>, |
| 254 | ): CameraTarget | null { |
| 255 | if (ids.size === 0) return null; |
| 256 | |
| 257 | let cx = 0, |
| 258 | cy = 0, |
| 259 | cz = 0, |
| 260 | count = 0; |
| 261 | for (const node of nodes) { |
| 262 | if (ids.has(node.id)) { |
| 263 | cx += node.x; |
| 264 | cy += node.y; |
| 265 | cz += node.z; |
| 266 | count++; |
| 267 | } |
| 268 | } |
| 269 | if (count === 0) return null; |
| 270 | |
| 271 | cx /= count; |
| 272 | cy /= count; |
| 273 | cz /= count; |
| 274 | |
| 275 | /* Distance based on cluster spread — ensure we never zoom too close */ |
| 276 | let maxDist = 0; |
| 277 | for (const node of nodes) { |
| 278 | if (ids.has(node.id)) { |
| 279 | const d = Math.sqrt( |
| 280 | (node.x - cx) ** 2 + (node.y - cy) ** 2 + (node.z - cz) ** 2, |
| 281 | ); |
| 282 | if (d > maxDist) maxDist = d; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* Minimum distance scales with count: single node = 300, cluster = spread-based */ |
| 287 | const spreadDist = maxDist * 3; |
| 288 | const minDist = count <= 5 ? 300 : 200; |
| 289 | const distance = Math.max(minDist, spreadDist); |
| 290 | const lookAt = new THREE.Vector3(cx, cy, cz); |
| 291 | const position = new THREE.Vector3( |
| 292 | cx + distance * 0.2, |
| 293 | cy + distance * 0.15, |
| 294 | cz + distance, |
| 295 | ); |
| 296 | |
| 297 | return { position, lookAt }; |
| 298 | } |