Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions lib/solvers/GapFillSolver/ExpandEdgesToEmptySpaceSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,63 @@ export class ExpandEdgesToEmptySpaceSolver extends BaseSolver {
const nodeWidth = nodeBounds.maxX - nodeBounds.minX
const nodeHeight = nodeBounds.maxY - nodeBounds.minY

const expandedSegment = {
segment,
newNode: {
capacityMeshNodeId: `new-${segment.parent.capacityMeshNodeId}-${this.expandedSegments.length}`,
center: nodeCenter,
width: nodeWidth,
height: nodeHeight,
availableZ: [segment.z],
layer: segment.parent.layer,
},
let expandedSegment: ExpandedSegment

if (segment.isSegmentSameLengthAsParentEdge) {
// Segment is broken, create a new node
expandedSegment = {
segment,
newNode: {
capacityMeshNodeId: `new-${segment.parent.capacityMeshNodeId}-${this.expandedSegments.length}`,
center: nodeCenter,
width: nodeWidth,
height: nodeHeight,
availableZ: [segment.z],
layer: segment.parent.layer,
},
}
} else {
// Segment is not broken (original size), expand the parent node
const parentBounds = {
minX: segment.parent.center.x - segment.parent.width / 2,
minY: segment.parent.center.y - segment.parent.height / 2,
maxX: segment.parent.center.x + segment.parent.width / 2,
maxY: segment.parent.center.y + segment.parent.height / 2,
}

// Merge the parent bounds with the new expansion bounds
const mergedBounds = {
minX: Math.min(parentBounds.minX, nodeBounds.minX),
minY: Math.min(parentBounds.minY, nodeBounds.minY),
maxX: Math.max(parentBounds.maxX, nodeBounds.maxX),
maxY: Math.max(parentBounds.maxY, nodeBounds.maxY),
}

const mergedCenter = {
x: (mergedBounds.minX + mergedBounds.maxX) / 2,
y: (mergedBounds.minY + mergedBounds.maxY) / 2,
}
const mergedWidth = mergedBounds.maxX - mergedBounds.minX
const mergedHeight = mergedBounds.maxY - mergedBounds.minY

// Merge availableZ arrays (remove duplicates)
const mergedAvailableZ = Array.from(
new Set([...segment.parent.availableZ, segment.z]),
)

expandedSegment = {
segment,
newNode: {
capacityMeshNodeId: `expanded-${mergedWidth}-${mergedHeight}-${segment.parent.capacityMeshNodeId}`,
center: mergedCenter,
width: mergedWidth,
height: mergedHeight,
availableZ: mergedAvailableZ,
layer: segment.parent.layer,
},
}
}

this.lastExpandedSegment = expandedSegment

if (nodeWidth < EPS || nodeHeight < EPS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface SegmentWithAdjacentEmptySpace {
end: { x: number; y: number }
z: number
facingDirection: "x+" | "x-" | "y+" | "y-"
isSegmentSameLengthAsParentEdge: boolean
}

const EPS = 1e-4
Expand Down Expand Up @@ -73,6 +74,7 @@ export class FindSegmentsWithAdjacentEmptySpaceSolver extends BaseSolver {
end,
facingDirection: edge.facingDirection,
z,
isSegmentSameLengthAsParentEdge: false,
})
}
}
Expand Down Expand Up @@ -111,14 +113,14 @@ export class FindSegmentsWithAdjacentEmptySpaceSolver extends BaseSolver {
candidateEdge.end.y + EPS,
)

const overlappingEdges = nearbyEdges
const mightBeOverlappingEdges = nearbyEdges
.map((i) => this.allEdges[i]!)
.filter((e) => e.z === candidateEdge.z)
this.lastOverlappingEdges = overlappingEdges
this.lastOverlappingEdges = mightBeOverlappingEdges

const uncoveredSegments = projectToUncoveredSegments(
candidateEdge,
overlappingEdges,
mightBeOverlappingEdges,
)
this.lastUncoveredSegments = uncoveredSegments
this.segmentsWithAdjacentEmptySpace.push(...uncoveredSegments)
Expand Down
16 changes: 15 additions & 1 deletion lib/solvers/GapFillSolver/GapFillSolverPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,24 @@ export class GapFillSolverPipeline extends BasePipelineSolver<GapFillSolverInput
override getOutput(): { outputNodes: CapacityMeshNode[] } {
const expandedSegments =
this.expandEdgesToEmptySpaceSolver?.getOutput().expandedSegments ?? []

// Collect parent node IDs that were expanded (not broken)
const expandedParentNodeIds = new Set(
expandedSegments
.filter((es) => !es.segment.isSegmentSameLengthAsParentEdge)
.map((es) => es.segment.parent.capacityMeshNodeId),
)

// Filter out parent nodes that were expanded
const nonExpandedOriginalNodes = this.inputProblem.meshNodes.filter(
(node) => !expandedParentNodeIds.has(node.capacityMeshNodeId),
)

// Add all expanded nodes (both broken and non-broken segments)
const expandedNodes = expandedSegments.map((es) => es.newNode)

return {
outputNodes: [...this.inputProblem.meshNodes, ...expandedNodes],
outputNodes: [...nonExpandedOriginalNodes, ...expandedNodes],
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/solvers/GapFillSolver/projectToUncoveredSegments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const EPS = 1e-4

export function projectToUncoveredSegments(
primaryEdge: SegmentWithAdjacentEmptySpace,
overlappingEdges: SegmentWithAdjacentEmptySpace[],
mightBeOverlappingEdges: SegmentWithAdjacentEmptySpace[],
): Array<SegmentWithAdjacentEmptySpace> {
const isHorizontal = Math.abs(primaryEdge.start.y - primaryEdge.end.y) < EPS
const isVertical = Math.abs(primaryEdge.start.x - primaryEdge.end.x) < EPS
Expand All @@ -23,7 +23,7 @@ export function projectToUncoveredSegments(

// 1) project each overlapping edge to an interval on the primary edge
const intervals: Array<{ s: number; e: number }> = []
for (const e of overlappingEdges) {
for (const e of mightBeOverlappingEdges) {
if (e === primaryEdge) continue

// only consider edges parallel + colinear (within EPS) with the primary edge
Expand All @@ -48,6 +48,7 @@ export function projectToUncoveredSegments(
...primaryEdge,
start: { ...primaryEdge.start },
end: { ...primaryEdge.end },
isSegmentSameLengthAsParentEdge: false,
},
]
}
Expand Down Expand Up @@ -87,6 +88,7 @@ export function projectToUncoveredSegments(
start,
end,
z: primaryEdge.z,
isSegmentSameLengthAsParentEdge: true,
}
})
}
2 changes: 1 addition & 1 deletion tests/__snapshots__/should-expand-node.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.