@@ -265,7 +265,12 @@ export class GapFillSolver extends BaseSolver {
265265 this . state . currentExpansionPoints [ this . state . currentExpansionIndex ] !
266266
267267 const filledRect = this . expandPointToRect ( point )
268- if ( filledRect && ! this . overlapsExistingFill ( filledRect ) ) {
268+ if (
269+ filledRect &&
270+ ! this . overlapsExistingFill ( filledRect ) &&
271+ ! this . overlapsInputRects ( filledRect ) &&
272+ this . hasMinimumSize ( filledRect )
273+ ) {
269274 this . state . filledRects . push ( filledRect )
270275 }
271276
@@ -303,6 +308,41 @@ export class GapFillSolver extends BaseSolver {
303308 return false
304309 }
305310
311+ private overlapsInputRects ( candidate : Placed3D ) : boolean {
312+ for ( const input of this . state . inputRects ) {
313+ const sharedLayers = candidate . zLayers . filter ( ( z ) =>
314+ input . zLayers . includes ( z ) ,
315+ )
316+ if ( sharedLayers . length === 0 ) continue
317+
318+ const overlapX =
319+ Math . max ( candidate . rect . x , input . rect . x ) <
320+ Math . min (
321+ candidate . rect . x + candidate . rect . width ,
322+ input . rect . x + input . rect . width ,
323+ )
324+ const overlapY =
325+ Math . max ( candidate . rect . y , input . rect . y ) <
326+ Math . min (
327+ candidate . rect . y + candidate . rect . height ,
328+ input . rect . y + input . rect . height ,
329+ )
330+
331+ if ( overlapX && overlapY ) {
332+ return true
333+ }
334+ }
335+
336+ return false
337+ }
338+
339+ private hasMinimumSize ( candidate : Placed3D ) : boolean {
340+ const minSize = 0.01
341+ return (
342+ candidate . rect . width >= minSize && candidate . rect . height >= minSize
343+ )
344+ }
345+
306346 private expandPointToRect ( point : ExpansionPoint ) : Placed3D | null {
307347 const section = point . section
308348 const edge = section . edge
@@ -313,22 +353,24 @@ export class GapFillSolver extends BaseSolver {
313353 let rect : { x : number ; y : number ; width : number ; height : number }
314354
315355 if ( Math . abs ( edge . normal . x ) > 0.5 ) {
316- const x1 = Math . min ( edge . x1 , nearbyEdge . x1 )
317- const x2 = Math . max ( edge . x1 , nearbyEdge . x1 )
356+ const leftX = edge . normal . x > 0 ? edge . x1 : nearbyEdge . x1
357+ const rightX = edge . normal . x > 0 ? nearbyEdge . x1 : edge . x1
358+
318359 rect = {
319- x : x1 ,
360+ x : leftX ,
320361 y : section . y1 ,
321- width : x2 - x1 ,
362+ width : rightX - leftX ,
322363 height : section . y2 - section . y1 ,
323364 }
324365 } else {
325- const y1 = Math . min ( edge . y1 , nearbyEdge . y1 )
326- const y2 = Math . max ( edge . y1 , nearbyEdge . y1 )
366+ const bottomY = edge . normal . y > 0 ? edge . y1 : nearbyEdge . y1
367+ const topY = edge . normal . y > 0 ? nearbyEdge . y1 : edge . y1
368+
327369 rect = {
328370 x : section . x1 ,
329- y : y1 ,
371+ y : bottomY ,
330372 width : section . x2 - section . x1 ,
331- height : y2 - y1 ,
373+ height : topY - bottomY ,
332374 }
333375 }
334376
0 commit comments