Skip to content

Commit 61e8674

Browse files
committed
add xy version of geometric key
1 parent 724c40f commit 61e8674

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939
* Removed `compas.scene.SceneTree`, functionalities merged into `compas.scene.Scene`.
4040
* Removed default implementation of `compas.geometry.trimesh_geodistance` since nonexistent.
4141
* Removed `compas.utilities.geometric_key` and replaced it by `compas.tolerance.TOL.geometric_key`.
42-
* Removed `compas.utilities.geometric_key_xy`.
42+
* Removed `compas.utilities.geometric_key_xy` and replaced it by `compas.tolerance.TOL.geometric_key_xy`.
4343

4444
## [2.1.0] 2024-03-01
4545

src/compas/tolerance.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,81 @@ def geometric_key(self, xyz, precision=None, sanitize=True):
675675

676676
return "{0:.{3}f},{1:.{3}f},{2:.{3}f}".format(x, y, z, precision)
677677

678+
def geometric_key_xy(self, xy, precision=None, sanitize=True):
679+
"""Compute the geometric key of a point in the XY plane.
680+
681+
Parameters
682+
----------
683+
xy : list of float
684+
The XY coordinates of the point.
685+
precision : int, optional
686+
The precision used when converting numbers to strings.
687+
Default is ``None``, in which case ``self.precision`` is used.
688+
sanitize : bool, optional
689+
If ``True``, minus signs ("-") will be removed from values that are equal to zero up to the given precision.
690+
691+
Returns
692+
-------
693+
str
694+
The geometric key in XY.
695+
696+
Raises
697+
------
698+
ValueError
699+
If the precision is zero.
700+
701+
Examples
702+
--------
703+
>>> tol = Tolerance()
704+
>>> tol.geometric_key_xy([1.0, 2.0])
705+
'1.000,2.000'
706+
707+
>>> tol = Tolerance()
708+
>>> tol.geometric_key_xy([1.05725, 2.0195], precision=3)
709+
'1.057,2.019'
710+
711+
>>> tol = Tolerance()
712+
>>> tol.geometric_key_xy([1.0, 2.0], precision=-1)
713+
'1,2'
714+
715+
>>> tol = Tolerance()
716+
>>> tol.geometric_key_xy([1.0, 2.0], precision=-3)
717+
'0,0'
718+
719+
>>> tol = Tolerance()
720+
>>> tol.geometric_key_xy([1103, 205], precision=-3)
721+
'1100,200'
722+
723+
"""
724+
x = xy[0]
725+
y = xy[1]
726+
727+
if not precision:
728+
precision = self.precision
729+
730+
if precision == 0:
731+
raise ValueError("Precision cannot be zero.")
732+
733+
if precision == -1:
734+
return "{:d},{:d}".format(int(x), int(y))
735+
736+
if precision < -1:
737+
precision = -precision - 1
738+
factor = 10**precision
739+
return "{:d},{:d}".format(
740+
int(round(x / factor) * factor),
741+
int(round(y / factor) * factor),
742+
)
743+
744+
if sanitize:
745+
minzero = "-{0:.{1}f}".format(0.0, precision)
746+
if "{0:.{1}f}".format(x, precision) == minzero:
747+
x = 0.0
748+
if "{0:.{1}f}".format(y, precision) == minzero:
749+
y = 0.0
750+
751+
return "{0:.{2}f},{1:.{2}f}".format(x, y, precision)
752+
678753
def format_number(self, number, precision=None):
679754
"""Format a number as a string.
680755

0 commit comments

Comments
 (0)