19
19
import scipy .stats
20
20
import numpy
21
21
22
+
23
+ def add_area_dims (area , num_dims ):
24
+ while (len (area ) // 2 ) < num_dims :
25
+ area = [2147483648 ] + area [:len (area ) // 2 ] + [0 ] + area [len (area ) // 2 :]
26
+ return area
27
+
22
28
def get_area_and_mult (conds , x_in , timestep_in ):
23
29
dims = tuple (x_in .shape [2 :])
24
30
area = None
@@ -34,8 +40,9 @@ def get_area_and_mult(conds, x_in, timestep_in):
34
40
return None
35
41
if 'area' in conds :
36
42
area = list (conds ['area' ])
37
- while (len (area ) // 2 ) < len (dims ):
38
- area = [2147483648 ] + area [:len (area ) // 2 ] + [0 ] + area [len (area ) // 2 :]
43
+ area = add_area_dims (area , len (dims ))
44
+ if (len (area ) // 2 ) > len (dims ):
45
+ area = area [:len (dims )] + area [len (area ) // 2 :(len (area ) // 2 ) + len (dims )]
39
46
40
47
if 'strength' in conds :
41
48
strength = conds ['strength' ]
@@ -53,7 +60,7 @@ def get_area_and_mult(conds, x_in, timestep_in):
53
60
if "mask_strength" in conds :
54
61
mask_strength = conds ["mask_strength" ]
55
62
mask = conds ['mask' ]
56
- assert (mask .shape [1 :] == x_in .shape [2 :])
63
+ assert (mask .shape [1 :] == x_in .shape [2 :])
57
64
58
65
mask = mask [:input_x .shape [0 ]]
59
66
if area is not None :
@@ -67,16 +74,17 @@ def get_area_and_mult(conds, x_in, timestep_in):
67
74
mult = mask * strength
68
75
69
76
if 'mask' not in conds and area is not None :
70
- rr = 8
77
+ fuzz = 8
71
78
for i in range (len (dims )):
79
+ rr = min (fuzz , mult .shape [2 + i ] // 4 )
72
80
if area [len (dims ) + i ] != 0 :
73
81
for t in range (rr ):
74
82
m = mult .narrow (i + 2 , t , 1 )
75
- m *= ((1.0 / rr ) * (t + 1 ))
83
+ m *= ((1.0 / rr ) * (t + 1 ))
76
84
if (area [i ] + area [len (dims ) + i ]) < x_in .shape [i + 2 ]:
77
85
for t in range (rr ):
78
86
m = mult .narrow (i + 2 , area [i ] - 1 - t , 1 )
79
- m *= ((1.0 / rr ) * (t + 1 ))
87
+ m *= ((1.0 / rr ) * (t + 1 ))
80
88
81
89
conditioning = {}
82
90
model_conds = conds ["model_conds" ]
@@ -551,25 +559,37 @@ def resolve_areas_and_cond_masks(conditions, h, w, device):
551
559
logging .warning ("WARNING: The comfy.samplers.resolve_areas_and_cond_masks function is deprecated please use the resolve_areas_and_cond_masks_multidim one instead." )
552
560
return resolve_areas_and_cond_masks_multidim (conditions , [h , w ], device )
553
561
554
- def create_cond_with_same_area_if_none (conds , c ): #TODO: handle dim != 2
562
+ def create_cond_with_same_area_if_none (conds , c ):
555
563
if 'area' not in c :
556
564
return
557
565
566
+ def area_inside (a , area_cmp ):
567
+ a = add_area_dims (a , len (area_cmp ) // 2 )
568
+ area_cmp = add_area_dims (area_cmp , len (a ) // 2 )
569
+
570
+ a_l = len (a ) // 2
571
+ area_cmp_l = len (area_cmp ) // 2
572
+ for i in range (min (a_l , area_cmp_l )):
573
+ if a [a_l + i ] < area_cmp [area_cmp_l + i ]:
574
+ return False
575
+ for i in range (min (a_l , area_cmp_l )):
576
+ if (a [i ] + a [a_l + i ]) > (area_cmp [i ] + area_cmp [area_cmp_l + i ]):
577
+ return False
578
+ return True
579
+
558
580
c_area = c ['area' ]
559
581
smallest = None
560
582
for x in conds :
561
583
if 'area' in x :
562
584
a = x ['area' ]
563
- if c_area [2 ] >= a [2 ] and c_area [3 ] >= a [3 ]:
564
- if a [0 ] + a [2 ] >= c_area [0 ] + c_area [2 ]:
565
- if a [1 ] + a [3 ] >= c_area [1 ] + c_area [3 ]:
566
- if smallest is None :
567
- smallest = x
568
- elif 'area' not in smallest :
569
- smallest = x
570
- else :
571
- if smallest ['area' ][0 ] * smallest ['area' ][1 ] > a [0 ] * a [1 ]:
572
- smallest = x
585
+ if area_inside (c_area , a ):
586
+ if smallest is None :
587
+ smallest = x
588
+ elif 'area' not in smallest :
589
+ smallest = x
590
+ else :
591
+ if math .prod (smallest ['area' ][:len (smallest ['area' ]) // 2 ]) > math .prod (a [:len (a ) // 2 ]):
592
+ smallest = x
573
593
else :
574
594
if smallest is None :
575
595
smallest = x
0 commit comments