File size: 9,406 Bytes
8a01471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { NN3DModel, Position3D } from '@/schema/types';

/**
 * Layout algorithm type
 */
export type LayoutType = 'layered' | 'force' | 'circular' | 'hierarchical' | 'custom';

/**
 * Layout configuration options
 */
export interface LayoutOptions {
  type: LayoutType;
  layerSpacing: number;
  nodeSpacing: number;
  direction: 'horizontal' | 'vertical' | 'depth';
  centerGraph: boolean;
}

/**
 * Default layout options
 */
const DEFAULT_OPTIONS: LayoutOptions = {
  type: 'layered',
  layerSpacing: 3.0,
  nodeSpacing: 2.0,
  direction: 'horizontal',
  centerGraph: true,
};

/**
 * Layout result
 */
export interface LayoutResult {
  positions: Map<string, Position3D>;
  bounds: {
    min: Position3D;
    max: Position3D;
    center: Position3D;
  };
}

/**
 * Compute layout for a neural network graph
 */
export function computeLayout(
  model: NN3DModel,
  options: Partial<LayoutOptions> = {}
): LayoutResult {
  const opts = { ...DEFAULT_OPTIONS, ...options };
  
  switch (opts.type) {
    case 'layered':
      return computeLayeredLayout(model, opts);
    case 'force':
      return computeForceLayout(model, opts);
    case 'circular':
      return computeCircularLayout(model, opts);
    case 'hierarchical':
      return computeHierarchicalLayout(model, opts);
    default:
      return computeLayeredLayout(model, opts);
  }
}

/**
 * Layered layout - nodes arranged in layers based on depth
 */
function computeLayeredLayout(model: NN3DModel, options: LayoutOptions): LayoutResult {
  const { nodes, edges } = model.graph;
  const positions = new Map<string, Position3D>();
  
  // Build adjacency and compute depths
  const inDegree = new Map<string, number>();
  const adjacency = new Map<string, string[]>();
  
  nodes.forEach(node => {
    inDegree.set(node.id, 0);
    adjacency.set(node.id, []);
  });
  
  edges.forEach(edge => {
    inDegree.set(edge.target, (inDegree.get(edge.target) || 0) + 1);
    adjacency.get(edge.source)?.push(edge.target);
  });
  
  // Topological sort to determine layers
  const layers: string[][] = [];
  const nodeDepths = new Map<string, number>();
  const queue: string[] = [];
  
  // Start with nodes having no incoming edges
  nodes.forEach(node => {
    if (inDegree.get(node.id) === 0) {
      queue.push(node.id);
      nodeDepths.set(node.id, 0);
    }
  });
  
  // BFS to assign depths
  while (queue.length > 0) {
    const nodeId = queue.shift()!;
    const depth = nodeDepths.get(nodeId) || 0;
    
    // Ensure layer array exists
    if (!layers[depth]) {
      layers[depth] = [];
    }
    layers[depth].push(nodeId);
    
    // Process neighbors
    const neighbors = adjacency.get(nodeId) || [];
    for (const neighbor of neighbors) {
      const newDegree = (inDegree.get(neighbor) || 1) - 1;
      inDegree.set(neighbor, newDegree);
      
      if (newDegree === 0) {
        nodeDepths.set(neighbor, depth + 1);
        queue.push(neighbor);
      }
    }
  }
  
  // Handle cycles (nodes not yet placed)
  nodes.forEach(node => {
    if (!nodeDepths.has(node.id)) {
      const lastLayerIndex = layers.length;
      if (!layers[lastLayerIndex]) {
        layers[lastLayerIndex] = [];
      }
      layers[lastLayerIndex].push(node.id);
      nodeDepths.set(node.id, lastLayerIndex);
    }
  });
  
  // Compute positions
  let maxWidth = 0;
  layers.forEach(layer => {
    maxWidth = Math.max(maxWidth, layer.length);
  });
  
  layers.forEach((layer, layerIndex) => {
    const layerWidth = (layer.length - 1) * options.nodeSpacing;
    const startOffset = -layerWidth / 2;
    
    layer.forEach((nodeId, nodeIndex) => {
      let x: number, y: number, z: number;
      
      if (options.direction === 'vertical') {
        // Vertical: layers stack along Y (top to bottom), nodes spread on X
        x = startOffset + nodeIndex * options.nodeSpacing;
        y = -layerIndex * options.layerSpacing;
        z = 0;
      } else if (options.direction === 'horizontal') {
        // Horizontal: layers stack along X (left to right), nodes spread on Y
        x = layerIndex * options.layerSpacing;
        y = startOffset + nodeIndex * options.nodeSpacing;
        z = 0;
      } else {
        // Depth: layers stack along Z (front to back), nodes spread on X
        x = startOffset + nodeIndex * options.nodeSpacing;
        y = 0;
        z = -layerIndex * options.layerSpacing;
      }
      
      positions.set(nodeId, { x, y, z });
    });
  });
  
  return {
    positions,
    bounds: computeBounds(positions),
  };
}

/**
 * Force-directed layout using simple spring simulation
 */
function computeForceLayout(model: NN3DModel, options: LayoutOptions): LayoutResult {
  const { nodes, edges } = model.graph;
  const positions = new Map<string, Position3D>();
  const velocities = new Map<string, Position3D>();
  
  // Initialize random positions
  nodes.forEach((node, i) => {
    const angle = (i / nodes.length) * Math.PI * 2;
    const radius = 5 + Math.random() * 5;
    positions.set(node.id, {
      x: Math.cos(angle) * radius,
      y: Math.sin(angle) * radius,
      z: (Math.random() - 0.5) * 5,
    });
    velocities.set(node.id, { x: 0, y: 0, z: 0 });
  });
  
  // Simulation parameters
  const iterations = 100;
  const repulsion = 50;
  const attraction = 0.1;
  const damping = 0.9;
  
  for (let iter = 0; iter < iterations; iter++) {
    // Repulsion between all nodes
    for (let i = 0; i < nodes.length; i++) {
      for (let j = i + 1; j < nodes.length; j++) {
        const posA = positions.get(nodes[i].id)!;
        const posB = positions.get(nodes[j].id)!;
        
        const dx = posB.x - posA.x;
        const dy = posB.y - posA.y;
        const dz = posB.z - posA.z;
        const dist = Math.sqrt(dx * dx + dy * dy + dz * dz) + 0.1;
        
        const force = repulsion / (dist * dist);
        const fx = (dx / dist) * force;
        const fy = (dy / dist) * force;
        const fz = (dz / dist) * force;
        
        const velA = velocities.get(nodes[i].id)!;
        const velB = velocities.get(nodes[j].id)!;
        
        velA.x -= fx;
        velA.y -= fy;
        velA.z -= fz;
        velB.x += fx;
        velB.y += fy;
        velB.z += fz;
      }
    }
    
    // Attraction along edges
    edges.forEach(edge => {
      const posA = positions.get(edge.source);
      const posB = positions.get(edge.target);
      if (!posA || !posB) return;
      
      const dx = posB.x - posA.x;
      const dy = posB.y - posA.y;
      const dz = posB.z - posA.z;
      const dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
      
      const force = attraction * (dist - options.nodeSpacing);
      const fx = (dx / dist) * force;
      const fy = (dy / dist) * force;
      const fz = (dz / dist) * force;
      
      const velA = velocities.get(edge.source)!;
      const velB = velocities.get(edge.target)!;
      
      velA.x += fx;
      velA.y += fy;
      velA.z += fz;
      velB.x -= fx;
      velB.y -= fy;
      velB.z -= fz;
    });
    
    // Apply velocities with damping
    nodes.forEach(node => {
      const pos = positions.get(node.id)!;
      const vel = velocities.get(node.id)!;
      
      pos.x += vel.x;
      pos.y += vel.y;
      pos.z += vel.z;
      
      vel.x *= damping;
      vel.y *= damping;
      vel.z *= damping;
    });
  }
  
  // Center the graph
  if (options.centerGraph) {
    const bounds = computeBounds(positions);
    positions.forEach((pos, id) => {
      positions.set(id, {
        x: pos.x - bounds.center.x,
        y: pos.y - bounds.center.y,
        z: pos.z - bounds.center.z,
      });
    });
  }
  
  return {
    positions,
    bounds: computeBounds(positions),
  };
}

/**
 * Circular layout - nodes arranged in a circle by layer
 */
function computeCircularLayout(model: NN3DModel, options: LayoutOptions): LayoutResult {
  const { nodes } = model.graph;
  const positions = new Map<string, Position3D>();
  
  const nodeCount = nodes.length;
  const radius = Math.max(nodeCount * options.nodeSpacing / (2 * Math.PI), 5);
  
  nodes.forEach((node, i) => {
    const angle = (i / nodeCount) * Math.PI * 2;
    positions.set(node.id, {
      x: Math.cos(angle) * radius,
      y: Math.sin(angle) * radius,
      z: 0,
    });
  });
  
  return {
    positions,
    bounds: computeBounds(positions),
  };
}

/**
 * Hierarchical layout - tree-like structure
 */
function computeHierarchicalLayout(model: NN3DModel, options: LayoutOptions): LayoutResult {
  // Use layered layout as base, but with different spacing
  return computeLayeredLayout(model, {
    ...options,
    layerSpacing: options.layerSpacing * 1.5,
  });
}

/**
 * Compute bounding box of positions
 */
function computeBounds(positions: Map<string, Position3D>): {
  min: Position3D;
  max: Position3D;
  center: Position3D;
} {
  let minX = Infinity, minY = Infinity, minZ = Infinity;
  let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
  
  positions.forEach(pos => {
    minX = Math.min(minX, pos.x);
    minY = Math.min(minY, pos.y);
    minZ = Math.min(minZ, pos.z);
    maxX = Math.max(maxX, pos.x);
    maxY = Math.max(maxY, pos.y);
    maxZ = Math.max(maxZ, pos.z);
  });
  
  return {
    min: { x: minX, y: minY, z: minZ },
    max: { x: maxX, y: maxY, z: maxZ },
    center: {
      x: (minX + maxX) / 2,
      y: (minY + maxY) / 2,
      z: (minZ + maxZ) / 2,
    },
  };
}

export { computeBounds };