Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tolerance #1333

Merged
merged 9 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fixed broken import in `copas.geometry.trimesh_smoothing_numpy`.
* Changed `RhinoBrep.trimmed` to return single result or raise `BrepTrimmingError` instead of returning a list.
* Changed order of imports according to `isort` and changed line length to `179`.
* Changed use of `compas.geometry.allclose` to `compas.tolerance.TOL.is_allclose`.
* Changed use of `compas.geometry.close` to `compas.tolerance.TOL.is_close`.

### Removed

Expand Down
6 changes: 3 additions & 3 deletions src/compas/geometry/_core/tangent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def tangent_points_to_circle_xy(circle, point):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL
>>> circle = ((0, 0, 0), (0, 0, 1)), 1.0
>>> point = (2, 4, 0)
>>> t1, t2 = tangent_points_to_circle_xy(circle, point)
>>> allclose(t1, [-0.772, 0.636, 0.000], 1e-3)
>>> TOL.is_allclose(t1, [-0.772, 0.636, 0.000], atol=1e-3)
True
>>> allclose(t2, [0.972, -0.236, 0.000], 1e-3)
>>> TOL.is_allclose(t2, [0.972, -0.236, 0.000], atol=1e-3)
True

"""
Expand Down
6 changes: 3 additions & 3 deletions src/compas/geometry/_core/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ def orthonormalize_axes(xaxis, yaxis):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL
>>> xaxis = [1, 4, 5]
>>> yaxis = [1, 0, -2]
>>> xaxis, yaxis = orthonormalize_axes(xaxis, yaxis)
>>> allclose(xaxis, [0.1543, 0.6172, 0.7715], tol=0.001)
>>> TOL.is_allclose(xaxis, [0.1543, 0.6172, 0.7715], atol=0.001)
True
>>> allclose(yaxis, [0.6929, 0.4891, -0.5298], tol=0.001)
>>> TOL.is_allclose(yaxis, [0.6929, 0.4891, -0.5298], atol=0.001)
True

"""
Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/curves/arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from compas.geometry import Frame
from compas.geometry import Point
from compas.geometry import Vector
from compas.geometry import close
from compas.tolerance import TOL

from .curve import Curve

Expand Down Expand Up @@ -244,7 +244,7 @@ def circumference(self):

@property
def is_circle(self):
return close(abs(abs(self.angle) - PI2), 0.0)
return TOL.is_close(abs(self.angle), PI2)

@property
def is_closed(self):
Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/curves/polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from compas.geometry import Frame
from compas.geometry import Line
from compas.geometry import Point
from compas.geometry import allclose
from compas.geometry import is_point_on_line
from compas.geometry import is_point_on_polyline
from compas.geometry import transform_points
from compas.itertools import pairwise
from compas.tolerance import TOL

from .curve import Curve

Expand Down Expand Up @@ -118,7 +118,7 @@ def __len__(self):
def __eq__(self, other):
if not hasattr(other, "__iter__") or not hasattr(other, "__len__") or len(self) != len(other):
return False
return allclose(self, other)
return TOL.is_allclose(self, other)

# ==========================================================================
# properties
Expand Down
6 changes: 3 additions & 3 deletions src/compas/geometry/intersections.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,15 @@ def intersection_sphere_line(sphere, line):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL

>>> sphere = (3.0, 7.0, 4.0), 10.0
>>> line = (1.0, 0, 0.5), (2.0, 1.0, 0.5)
>>> x1, x2 = intersection_sphere_line(sphere, line)

>>> allclose(x1, [11.634, 10.634, 0.500], 1e-3)
>>> TOL.is_allclose(x1, [11.634, 10.634, 0.500], atol=1e-3)
True
>>> allclose(x2, [-0.634, -1.634, 0.50], 1e-3)
>>> TOL.is_allclose(x2, [-0.634, -1.634, 0.50], atol=1e-3)
True

"""
Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from compas.geometry import Plane
from compas.geometry import Point
from compas.geometry import Transformation
from compas.geometry import allclose
from compas.geometry import area_polygon
from compas.geometry import bounding_box
from compas.geometry import centroid_polygon
Expand All @@ -19,6 +18,7 @@
from compas.geometry import is_polygon_convex
from compas.geometry import transform_points
from compas.itertools import pairwise
from compas.tolerance import TOL


class Polygon(Geometry):
Expand Down Expand Up @@ -119,7 +119,7 @@ def __iter__(self):
def __eq__(self, other):
if not hasattr(other, "__iter__") or not hasattr(other, "__len__") or len(self) != len(other):
return False
return allclose(self, other)
return TOL.is_allclose(self, other)

# ==========================================================================
# Properties
Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"""

from compas.geometry import Transformation
from compas.geometry import allclose
from compas.geometry import decompose_matrix
from compas.geometry import matrix_from_orthogonal_projection
from compas.geometry import matrix_from_parallel_projection
from compas.geometry import matrix_from_perspective_entries
from compas.geometry import matrix_from_perspective_projection
from compas.tolerance import TOL
from compas.utilities import flatten


Expand Down Expand Up @@ -47,7 +47,7 @@ class Projection(Transformation):
def __init__(self, matrix=None, check=False, name=None):
if matrix and check:
_, _, _, _, perspective = decompose_matrix(matrix)
if not allclose(flatten(matrix), flatten(matrix_from_perspective_entries(perspective))):
if not TOL.is_allclose(flatten(matrix), flatten(matrix_from_perspective_entries(perspective))):
raise ValueError("This is not a proper projection matrix.")
super(Projection, self).__init__(matrix=matrix, name=name)

Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,12 @@ def from_frame(cls, frame):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL
>>> from compas.geometry import Frame
>>> q = [1., -2., 3., -4.]
>>> F = Frame.from_quaternion(q)
>>> Q = Quaternion.from_frame(F)
>>> allclose(Q.canonized(), quaternion_canonize(quaternion_unitize(q)))
>>> TOL.is_allclose(Q.canonized(), quaternion_canonize(quaternion_unitize(q)))
True

"""
Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"""

from compas.geometry import Transformation
from compas.geometry import allclose
from compas.geometry import cross_vectors
from compas.geometry import decompose_matrix
from compas.geometry import dot_vectors
from compas.geometry import identity_matrix
from compas.geometry import matrix_from_perspective_entries
from compas.geometry import normalize_vector
from compas.tolerance import TOL
from compas.utilities import flatten


Expand Down Expand Up @@ -50,7 +50,7 @@ class Reflection(Transformation):
def __init__(self, matrix=None, check=False, name=None):
if matrix and check:
_, _, _, _, perspective = decompose_matrix(matrix)
if not allclose(flatten(matrix), flatten(matrix_from_perspective_entries(perspective))):
if not TOL.is_allclose(flatten(matrix), flatten(matrix_from_perspective_entries(perspective))):
raise ValueError("This is not a proper reflection matrix.")
super(Reflection, self).__init__(matrix=matrix, name=name)

Expand Down
20 changes: 10 additions & 10 deletions src/compas/geometry/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"""

from compas.geometry import Transformation
from compas.geometry import allclose
from compas.geometry import axis_and_angle_from_matrix
from compas.geometry import basis_vectors_from_matrix
from compas.geometry import cross_vectors
Expand All @@ -24,6 +23,7 @@
from compas.geometry import matrix_from_frame
from compas.geometry import matrix_from_quaternion
from compas.geometry import normalize_vector
from compas.tolerance import TOL
from compas.utilities import flatten


Expand Down Expand Up @@ -78,7 +78,7 @@ class Rotation(Transformation):
def __init__(self, matrix=None, check=False, name=None):
if matrix and check:
_, _, angles, _, _ = decompose_matrix(matrix)
if not allclose(flatten(matrix), flatten(matrix_from_euler_angles(angles))):
if not TOL.is_allclose(flatten(matrix), flatten(matrix_from_euler_angles(angles))):
raise ValueError("This is not a proper rotation matrix.")
super(Rotation, self).__init__(matrix=matrix, name=name)

Expand Down Expand Up @@ -230,11 +230,11 @@ def from_quaternion(cls, quaternion):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL
>>> q1 = [0.945, -0.021, -0.125, 0.303]
>>> R = Rotation.from_quaternion(q1)
>>> q2 = R.quaternion
>>> allclose(q1, q2)
>>> TOL.is_allclose(q1, q2)
True

"""
Expand All @@ -258,11 +258,11 @@ def from_axis_angle_vector(cls, axis_angle_vector, point=[0, 0, 0]):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL
>>> aav1 = [-0.043, -0.254, 0.617]
>>> R = Rotation.from_axis_angle_vector(aav1)
>>> aav2 = R.axis_angle_vector
>>> allclose(aav1, aav2)
>>> TOL.is_allclose(aav1, aav2)
True

"""
Expand Down Expand Up @@ -297,12 +297,12 @@ def from_euler_angles(cls, euler_angles, static=True, axes="xyz", **kwargs):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL
>>> ea1 = 1.4, 0.5, 2.3
>>> args = False, 'xyz'
>>> R1 = Rotation.from_euler_angles(ea1, *args)
>>> ea2 = R1.euler_angles(*args)
>>> allclose(ea1, ea2)
>>> TOL.is_allclose(ea1, ea2)
True

>>> alpha, beta, gamma = ea1
Expand Down Expand Up @@ -342,12 +342,12 @@ def euler_angles(self, static=True, axes="xyz"):

Examples
--------
>>> from compas.geometry import allclose
>>> from compas.tolerance import TOL
>>> ea1 = 1.4, 0.5, 2.3
>>> args = False, 'xyz'
>>> R1 = Rotation.from_euler_angles(ea1, *args)
>>> ea2 = R1.euler_angles(*args)
>>> allclose(ea1, ea2)
>>> TOL.is_allclose(ea1, ea2)
True

"""
Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"""

from compas.geometry import Transformation
from compas.geometry import allclose
from compas.geometry import decompose_matrix
from compas.geometry import matrix_from_frame
from compas.geometry import matrix_from_scale_factors
from compas.geometry import matrix_inverse
from compas.geometry import multiply_matrices
from compas.tolerance import TOL
from compas.utilities import flatten


Expand Down Expand Up @@ -61,7 +61,7 @@ class Scale(Transformation):
def __init__(self, matrix=None, check=False, name=None):
if matrix and check:
scale, _, _, _, _ = decompose_matrix(matrix)
if not allclose(flatten(matrix), flatten(matrix_from_scale_factors(scale))):
if not TOL.is_allclose(flatten(matrix), flatten(matrix_from_scale_factors(scale))):
raise ValueError("This is not a proper scale matrix.")
super(Scale, self).__init__(matrix=matrix, name=name)

Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/shear.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"""

from compas.geometry import Transformation
from compas.geometry import allclose
from compas.geometry import decompose_matrix
from compas.geometry import matrix_from_shear
from compas.geometry import matrix_from_shear_entries
from compas.tolerance import TOL
from compas.utilities import flatten


Expand Down Expand Up @@ -50,7 +50,7 @@ class Shear(Transformation):
def __init__(self, matrix=None, check=False, name=None):
if matrix and check:
_, shear, _, _, _ = decompose_matrix(matrix)
if not allclose(flatten(matrix), flatten(matrix_from_shear_entries(shear))):
if not TOL.is_allclose(flatten(matrix), flatten(matrix_from_shear_entries(shear))):
raise ValueError("This is not a proper shear matrix.")
super(Shear, self).__init__(matrix=matrix, name=name)

Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"""

from compas.geometry import Transformation
from compas.geometry import allclose
from compas.geometry import matrix_from_translation
from compas.geometry import translation_from_matrix
from compas.tolerance import TOL
from compas.utilities import flatten


Expand Down Expand Up @@ -72,7 +72,7 @@ class Translation(Transformation):
def __init__(self, matrix=None, check=False, name=None):
if matrix and check:
translation = translation_from_matrix(matrix)
if not allclose(flatten(matrix), flatten(matrix_from_translation(translation))):
if not TOL.is_allclose(flatten(matrix), flatten(matrix_from_translation(translation))):
raise ValueError("This is not a proper translation matrix.")
super(Translation, self).__init__(matrix=matrix, name=name)

Expand Down
4 changes: 2 additions & 2 deletions tests/compas/colors/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from random import random

from compas.colors import Color
from compas.geometry import allclose
from compas.tolerance import TOL


@pytest.mark.parametrize(
Expand All @@ -26,7 +26,7 @@ def test_color(color):
assert c.b == color[2]
assert c.a == color[3] if len(color) == 4 else 1.0

assert allclose(eval(repr(c)), c, tol=1e-12)
assert TOL.is_allclose(eval(repr(c)), c)


def test_color_data():
Expand Down
4 changes: 2 additions & 2 deletions tests/compas/geometry/test_bbox.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

import compas
from compas.geometry import allclose
from compas.tolerance import TOL
from compas.geometry import bounding_box
from compas.geometry import bounding_box_xy

Expand Down Expand Up @@ -120,4 +120,4 @@ def test_oriented_bounding_box_numpy(coords, expected):

results = oriented_bounding_box_numpy(coords)
for result, expected_values in zip(results, expected):
assert allclose(result, expected_values, tol=1e-3)
assert TOL.is_allclose(result, expected_values)
Loading
Loading