NVIDIA / modulus-sym

Framework providing pythonic APIs, algorithms and utilities to be used with Modulus core to physics inform model training as well as higher level abstraction for domain experts
https://developer.nvidia.com/modulus
Apache License 2.0
163 stars 68 forks source link

🐛[BUG]: Incorrect sdf calculation in Polygon geometry #140

Open 052279 opened 6 months ago

052279 commented 6 months ago

Version

22.09

On which installation method(s) does this occur?

Docker

Describe the issue

When calculating the sdf of the polygon geometry, the sign of the sdf outside the polygon is reversed if there is only one (or an odd number) of points on the polygon whose y-coordinates match those of the point for which the sdf is calculated.

Minimum reproducible example

import numpy as np
from modulus.geometry.primitives_2d import Polygon

geo = Polygon([(0, 0), (1, 1), (1, -1)]) # triangle
point1 = {'x': np.array([[-1]], 'y': np.array([[0]])} # The y coordinate is the same as one point of the triangle
point2 = {'x': np.array([[0]], 'y': np.array([[2]])} # The x coordinate is the same as one point of the triangle
point3 = {'x': np.array([[0.01]], 'y': np.array([[2]])} # The x or y coordinates do not coincide with any point in the triangle

print(sdf.geo(point1, params={}) # sdf should be negative, but it is positive
print(sdf.geo(point2, params={})
print(sdf.geo(point3, params={})

geo = Polygon([(0, 0), (1, 1), (1, 0), (1, -1)]) # add dummy point to make 2 points with y-coordinate equal to point1

print(sdf.geo(point1, params={}) # sign of sdf becomes negative
print(sdf.geo(point2, params={})
print(sdf.geo(point3, params={})

Relevant log output

No response

Environment details

No response

Other/Misc.

No response