|
| 1 | +import type { GraphicsObject } from "graphics-debug" |
| 2 | +import type { SimpleRouteJson } from "../../types/srj-types" |
| 3 | + |
| 4 | +/** |
| 5 | + * Create basic visualization showing board bounds/outline and obstacles. |
| 6 | + * This can be used before solver initialization to show the problem space. |
| 7 | + */ |
| 8 | +export function createBaseVisualization( |
| 9 | + srj: SimpleRouteJson, |
| 10 | + title: string = "RectDiff", |
| 11 | +): GraphicsObject { |
| 12 | + const rects: NonNullable<GraphicsObject["rects"]> = [] |
| 13 | + const lines: NonNullable<GraphicsObject["lines"]> = [] |
| 14 | + |
| 15 | + const boardBounds = { |
| 16 | + minX: srj.bounds.minX, |
| 17 | + maxX: srj.bounds.maxX, |
| 18 | + minY: srj.bounds.minY, |
| 19 | + maxY: srj.bounds.maxY, |
| 20 | + } |
| 21 | + |
| 22 | + // Draw board outline or bounds rectangle |
| 23 | + if (srj.outline && srj.outline.length > 1) { |
| 24 | + lines.push({ |
| 25 | + points: [...srj.outline, srj.outline[0]!], |
| 26 | + strokeColor: "#111827", |
| 27 | + strokeWidth: 0.01, |
| 28 | + label: "outline", |
| 29 | + }) |
| 30 | + } else { |
| 31 | + rects.push({ |
| 32 | + center: { |
| 33 | + x: (boardBounds.minX + boardBounds.maxX) / 2, |
| 34 | + y: (boardBounds.minY + boardBounds.maxY) / 2, |
| 35 | + }, |
| 36 | + width: boardBounds.maxX - boardBounds.minX, |
| 37 | + height: boardBounds.maxY - boardBounds.minY, |
| 38 | + fill: "none", |
| 39 | + stroke: "#111827", |
| 40 | + label: "board", |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + // Draw obstacles |
| 45 | + for (const obstacle of srj.obstacles ?? []) { |
| 46 | + if (obstacle.type === "rect" || obstacle.type === "oval") { |
| 47 | + rects.push({ |
| 48 | + center: { x: obstacle.center.x, y: obstacle.center.y }, |
| 49 | + width: obstacle.width, |
| 50 | + height: obstacle.height, |
| 51 | + fill: "#fee2e2", |
| 52 | + stroke: "#ef4444", |
| 53 | + layer: "obstacle", |
| 54 | + label: "obstacle", |
| 55 | + }) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return { |
| 60 | + title, |
| 61 | + coordinateSystem: "cartesian", |
| 62 | + rects, |
| 63 | + points: [], |
| 64 | + lines, |
| 65 | + } |
| 66 | +} |
0 commit comments