Skip to content

Commit

Permalink
fix Rectangle for small numbers (#5735)
Browse files Browse the repository at this point in the history
Co-authored-by: Tetsuo Koyama <tkoyama010@gmail.com>
  • Loading branch information
MatthewFlamm and tkoyama010 committed Mar 12, 2024
1 parent 67d8c4b commit 4d8e24e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
15 changes: 10 additions & 5 deletions pyvista/core/utilities/geometric_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1858,9 +1858,16 @@ def Rectangle(points=None):
vec_02 = point_2 - point_0
vec_12 = point_2 - point_1

scalar_pdct_01_02 = np.dot(vec_01, vec_02)
scalar_pdct_01_12 = np.dot(vec_01, vec_12)
scalar_pdct_02_12 = np.dot(vec_02, vec_12)
mag_01 = np.linalg.norm(vec_01)
mag_02 = np.linalg.norm(vec_02)
mag_12 = np.linalg.norm(vec_12)

if np.isclose(mag_01, 0) or np.isclose(mag_02, 0) or np.isclose(mag_12, 0):
raise ValueError("Unable to build a rectangle with less than three different points")

scalar_pdct_01_02 = np.dot(vec_01, vec_02) / min(mag_01, mag_02) ** 2
scalar_pdct_01_12 = np.dot(vec_01, vec_12) / min(mag_01, mag_12) ** 2
scalar_pdct_02_12 = np.dot(vec_02, vec_12) / min(mag_02, mag_12) ** 2

null_scalar_products = [
val
Expand All @@ -1869,8 +1876,6 @@ def Rectangle(points=None):
]
if len(null_scalar_products) == 0:
raise ValueError("The three points should defined orthogonal vectors")
if len(null_scalar_products) > 1:
raise ValueError("Unable to build a rectangle with less than three different points")

points = np.array([point_0, point_1, point_2, point_0])
if np.isclose(scalar_pdct_01_02, 0):
Expand Down
19 changes: 14 additions & 5 deletions tests/core/test_geometric_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,20 @@ def test_quadrilateral():
assert np.allclose(mesh.points, points)


def test_rectangle():
pointa = [3.0, 1.0, 1.0]
pointb = [3.0, 2.0, 1.0]
pointc = [1.0, 2.0, 1.0]
pointd = [1.0, 1.0, 1.0]
@pytest.mark.parametrize(
"points",
[
([3.0, 1.0, 1.0], [3.0, 2.0, 1.0], [1.0, 2.0, 1.0], [1.0, 1.0, 1.0]),
(
[0.043, 0.0359, 0.0001],
[0.044, 0.0359, 0.0001],
[0.043, 0.036, 0.0001],
[0.044, 0.036, 0.0001],
),
],
)
def test_rectangle(points):
pointa, pointb, pointc, pointd = points

# Do a rotation to be in full 3D space with floating point coordinates
trans = pv.core.utilities.transformations.axis_angle_rotation([1, 1, 1], 30)
Expand Down

0 comments on commit 4d8e24e

Please sign in to comment.