Skip to content

Commit 783539d

Browse files
committed
Changes to polygon in constraints and polygon tests
1 parent 9beaf3c commit 783539d

File tree

2 files changed

+89
-20
lines changed

2 files changed

+89
-20
lines changed

pysensors/utils/_constraints.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,33 +1330,45 @@ def constraint_function(self, coords):
13301330
"""
13311331
Function to compute whether a certain point on the grid lies inside/outside
13321332
the defined constrained region
1333+
13331334
Attributes
13341335
----------
1335-
x : float,
1336-
x coordinate of point on the grid being evaluated to check whether it lies
1337-
inside or outside the constrained region
1338-
y : float,
1339-
y coordinate of point on the grid being evaluated to check whether it lies
1336+
coords : list or tuple
1337+
[x, y] coordinates of point on the grid being evaluated to check whether
1338+
it lies
13401339
inside or outside the constrained region
1340+
1341+
Returns
1342+
-------
1343+
bool
1344+
True if point satisfies the constraint (inside for "in", outside for "out"),
1345+
False otherwise
13411346
"""
1347+
if len(coords) != 2:
1348+
raise ValueError("coords must contain exactly 2 elements [x, y]")
1349+
13421350
x, y = coords[:]
1343-
# define point in polygon
13441351
polygon = self.xy_coords
13451352
n = len(polygon)
1353+
1354+
if n < 3:
1355+
raise ValueError("Polygon must have at least 3 vertices")
13461356
inFlag = False
13471357

13481358
for i in range(n):
13491359
x1, y1 = polygon[i]
13501360
x2, y2 = polygon[(i + 1) % n]
1351-
1352-
if (y1 < y and y2 >= y) or (y2 < y and y1 >= y):
1353-
if x1 + (y - y1) / (y2 - y1) * (x2 - x1) < x:
1354-
inFlag = not inFlag
1355-
1361+
if (y1 > y) != (y2 > y):
1362+
if y1 != y2:
1363+
x_intersect = x1 + (y - y1) * (x2 - x1) / (y2 - y1)
1364+
if x < x_intersect:
1365+
inFlag = not inFlag
13561366
if self.loc.lower() == "in":
13571367
return not inFlag
13581368
elif self.loc.lower() == "out":
13591369
return inFlag
1370+
else:
1371+
raise ValueError(f"Invalid constraint type: {self.loc}.Must be'in' or'out'")
13601372

13611373

13621374
class UserDefinedConstraints(BaseConstraint):

tests/utils/test_constraints.py

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,19 +2455,22 @@ def test_draw(self, triangle_coords):
24552455

24562456
def test_constraint_function_triangle(self, triangle_coords):
24572457
"""Test the constraint_function with a triangle."""
2458-
triangle = Polygon(
2458+
triangle_in = Polygon(
24592459
xy_coords=triangle_coords,
24602460
loc="in",
24612461
data=pd.DataFrame(),
24622462
X_axis="x",
24632463
Y_axis="y",
24642464
Field="f",
24652465
)
2466-
assert triangle.constraint_function([2, 1]) is False
2467-
assert triangle.constraint_function([-1, 0]) is True
2468-
assert triangle.constraint_function([5, 0]) is True
2469-
assert triangle.constraint_function([2, 5]) is True
2470-
assert triangle.constraint_function([0, 0]) is True
2466+
assert triangle_in.constraint_function([2, 1]) is False
2467+
assert triangle_in.constraint_function([1, 1]) is False
2468+
assert triangle_in.constraint_function([3, 1]) is False
2469+
assert triangle_in.constraint_function([2, 2]) is False
2470+
assert triangle_in.constraint_function([-1, 0]) is True
2471+
assert triangle_in.constraint_function([5, 0]) is True
2472+
assert triangle_in.constraint_function([2, 5]) is True
2473+
assert triangle_in.constraint_function([0, -1]) is True
24712474
triangle_out = Polygon(
24722475
xy_coords=triangle_coords,
24732476
loc="out",
@@ -2477,7 +2480,11 @@ def test_constraint_function_triangle(self, triangle_coords):
24772480
Field="f",
24782481
)
24792482
assert triangle_out.constraint_function([2, 1]) is True
2483+
assert triangle_out.constraint_function([1, 1]) is True
2484+
assert triangle_out.constraint_function([3, 1]) is True
24802485
assert triangle_out.constraint_function([-1, 0]) is False
2486+
assert triangle_out.constraint_function([5, 0]) is False
2487+
assert triangle_out.constraint_function([2, 5]) is False
24812488

24822489
def test_constraint_function_square(self, square_coords):
24832490
"""Test the constraint_function with a square."""
@@ -2496,7 +2503,23 @@ def test_constraint_function_square(self, square_coords):
24962503
assert square.constraint_function([5, 2]) is True
24972504
assert square.constraint_function([2, -1]) is True
24982505
assert square.constraint_function([2, 5]) is True
2499-
assert square.constraint_function([0, 0]) is True
2506+
assert square.constraint_function([0, 0]) is False
2507+
square_out = Polygon(
2508+
xy_coords=square_coords,
2509+
loc="out",
2510+
data=pd.DataFrame(),
2511+
X_axis="x",
2512+
Y_axis="y",
2513+
Field="f",
2514+
)
2515+
assert square_out.constraint_function([2, 2]) is True
2516+
assert square_out.constraint_function([1, 1]) is True
2517+
assert square_out.constraint_function([3, 3]) is True
2518+
assert square_out.constraint_function([-1, 2]) is False
2519+
assert square_out.constraint_function([5, 2]) is False
2520+
assert square_out.constraint_function([2, -1]) is False
2521+
assert square_out.constraint_function([2, 5]) is False
2522+
assert square_out.constraint_function([0, 0]) is True
25002523

25012524
def test_constraint_function_concave_polygon(self, pentagon_coords):
25022525
"""Test the constraint_function with a concave polygon."""
@@ -2511,11 +2534,30 @@ def test_constraint_function_concave_polygon(self, pentagon_coords):
25112534
assert pentagon.constraint_function([2, 2]) is False
25122535
assert pentagon.constraint_function([1, 1]) is False
25132536
assert pentagon.constraint_function([3, 3]) is False
2537+
25142538
assert pentagon.constraint_function([-2, 2]) is True
25152539
assert pentagon.constraint_function([6, 2]) is True
25162540
assert pentagon.constraint_function([2, -1]) is True
25172541
assert pentagon.constraint_function([2, 6]) is True
2518-
assert pentagon.constraint_function([0, 0]) is True
2542+
result_00 = pentagon.constraint_function([0, 0])
2543+
print(f"Point [0, 0] returns: {result_00}")
2544+
assert pentagon.constraint_function([0, 0]) is False
2545+
pentagon_out = Polygon(
2546+
xy_coords=pentagon_coords,
2547+
loc="out",
2548+
data=pd.DataFrame(),
2549+
X_axis="x",
2550+
Y_axis="y",
2551+
Field="f",
2552+
)
2553+
assert pentagon_out.constraint_function([2, 2]) is True
2554+
assert pentagon_out.constraint_function([1, 1]) is True
2555+
assert pentagon_out.constraint_function([3, 3]) is True
2556+
assert pentagon_out.constraint_function([-2, 2]) is False
2557+
assert pentagon_out.constraint_function([6, 2]) is False
2558+
assert pentagon_out.constraint_function([2, -1]) is False
2559+
assert pentagon_out.constraint_function([2, 6]) is False
2560+
assert pentagon_out.constraint_function([0, 0]) is True
25192561

25202562
def test_complex_polygons(self):
25212563
"""Test the constraint function with more complex polygons."""
@@ -2543,11 +2585,26 @@ def test_complex_polygons(self):
25432585
)
25442586
assert star.constraint_function([3, 3]) is False
25452587
assert star.constraint_function([3, 1]) is False
2546-
assert star.constraint_function([5, 3]) is False
2588+
assert star.constraint_function([5, 3]) is True
25472589
assert star.constraint_function([-2, 3]) is True
25482590
assert star.constraint_function([8, 3]) is True
25492591
assert star.constraint_function([3, -2]) is True
25502592
assert star.constraint_function([3, 7]) is True
2593+
star_out = Polygon(
2594+
xy_coords=star_coords,
2595+
loc="out",
2596+
data=pd.DataFrame(),
2597+
X_axis="x",
2598+
Y_axis="y",
2599+
Field="f",
2600+
)
2601+
assert star_out.constraint_function([3, 3]) is True
2602+
assert star_out.constraint_function([3, 1]) is True
2603+
assert star_out.constraint_function([5, 3]) is False
2604+
assert star_out.constraint_function([-2, 3]) is False
2605+
assert star_out.constraint_function([8, 3]) is False
2606+
assert star_out.constraint_function([3, -2]) is False
2607+
assert star_out.constraint_function([3, 7]) is False
25512608

25522609
def test_integration_with_base_constraint(self, sample_dataframe, triangle_coords):
25532610
"""Test that Polygon inherits and works with BaseConstraint methods."""

0 commit comments

Comments
 (0)