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

Add some more __str__ function to geometry classes for pretty print #1289

Merged
merged 21 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Fixed missing implementation of `Sphere.base`.
* Fixed bug in `intersection_sphere_sphere`.
* Changed the `__str__` of `compas.geometry.Frame`, `compas.geometry.Plane`, `compas.geometry.Polygon`, `compas.geometry.Polyhedron`, `compas.geometry.Quaternion` to use a limited number of decimals (determined by `Tolerance.PRECISION`). Note: `__repr__` will instead maintain full precision.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update this? Because after the merge it's way down in the file, changing an already released version

* Changed the `__str__` of `compas.geometry.Pointcloud` to print total number of points instead of the long list of points. Note: `__repr__` will still print all the points with full precision.
* Fixed bug in `Pointcloud.from_box()`.


### Removed

Expand Down
14 changes: 13 additions & 1 deletion src/compas/colors/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from compas.colors.html_colors import HTML_TO_RGB255
from compas.data import Data
from compas.tolerance import TOL

BASE16 = "0123456789abcdef"

Expand Down Expand Up @@ -170,7 +171,18 @@ def __init__(self, red, green, blue, alpha=1.0, name=None):
self.a = alpha

def __repr__(self):
return "{0}({1}, {2}, {3}, alpha={4})".format(type(self).__name__, self.r, self.g, self.b, self.a)
return "{0}(red={1}, green={2}, blue={3}, alpha={4})".format(
type(self).__name__, self.r, self.g, self.b, self.a
)

def __str__(self):
return "{0}(red={1}, green={2}, blue={3}, alpha={4})".format(
type(self).__name__,
TOL.format_number(self.r),
TOL.format_number(self.g),
TOL.format_number(self.b),
TOL.format_number(self.a),
)

def __getitem__(self, key):
if key == 0:
Expand Down
8 changes: 8 additions & 0 deletions src/compas/geometry/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ def __repr__(self):
self.yaxis,
)

def __str__(self):
return "{0}(point={1}, xaxis={2}, yaxis={3})".format(
type(self).__name__,
str(self.point),
str(self.xaxis),
str(self.yaxis),
)

def __len__(self):
return 3

Expand Down
7 changes: 7 additions & 0 deletions src/compas/geometry/plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def __repr__(self):
self.normal,
)

def __str__(self):
return "{0}(point={1}, normal={2})".format(
type(self).__name__,
str(self.point),
str(self.normal),
)

def __len__(self):
return 2

Expand Down
11 changes: 6 additions & 5 deletions src/compas/geometry/pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def __init__(self, points, name=None):
def __repr__(self):
return "{0}(points={1!r})".format(type(self).__name__, self.points)

def __str__(self):
return "{0}(len(points)={1})".format(type(self).__name__, len(self.points))

def __len__(self):
return len(self.points)

Expand Down Expand Up @@ -245,11 +248,9 @@ def from_box(cls, box, n):
True

"""
points = box.points
x, y, z = zip(*points)
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
zmin, zmax = min(z), max(z)
xmin, xmax = box.xmin, box.xmax
ymin, ymax = box.ymin, box.ymax
zmin, zmax = box.zmin, box.zmax
Comment on lines -247 to +252
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not related to the str stuff, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is there because I couldn't actually run tests without fixing the broken functions.

x = [uniform(xmin, xmax) for i in range(n)]
y = [uniform(ymin, ymax) for i in range(n)]
z = [uniform(zmin, zmax) for i in range(n)]
Expand Down
3 changes: 3 additions & 0 deletions src/compas/geometry/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def __init__(self, points, name=None):
def __repr__(self):
return "{0}(points={1!r})".format(type(self).__name__, self.points)

def __str__(self):
return "{0}(points={1})".format(type(self).__name__, [str(point) for point in self.points])

def __len__(self):
return len(self.points)

Expand Down
12 changes: 10 additions & 2 deletions src/compas/geometry/polyhedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from compas.geometry import Polygon
from compas.geometry import Point
from compas.geometry import Line
from compas.tolerance import TOL
from .geometry import Geometry
gonzalocasas marked this conversation as resolved.
Show resolved Hide resolved


Expand Down Expand Up @@ -215,6 +216,13 @@ def __repr__(self):
self.faces,
)

def __str__(self):
return "{0}(vertices={1}, faces={2})".format(
type(self).__name__,
[[TOL.format_number(num) for num in vertice] for vertice in self.vertices],
self.faces,
)

def __len__(self):
return 2

Expand Down Expand Up @@ -270,7 +278,7 @@ def __or__(self, other):

@property
def vertices(self):
if not self._vertices:
if self._vertices is None:
gonzalocasas marked this conversation as resolved.
Show resolved Hide resolved
self._vertices = []
return self._vertices

Expand All @@ -280,7 +288,7 @@ def vertices(self, vertices):

@property
def faces(self):
if not self._faces:
if self._faces is None:
gonzalocasas marked this conversation as resolved.
Show resolved Hide resolved
self._faces = []
return self._faces

Expand Down
9 changes: 9 additions & 0 deletions src/compas/geometry/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ def __init__(self, w, x, y, z, name=None):
def __repr__(self):
return "{0}({1}, {2}, {3}, {4})".format(type(self).__name__, self.w, self.x, self.y, self.z)

def __str__(self):
return "{0}({1}, {2}, {3}, {4})".format(
type(self).__name__,
TOL.format_number(self.w),
TOL.format_number(self.x),
TOL.format_number(self.y),
TOL.format_number(self.z),
)

def __eq__(self, other, tol=None):
if not hasattr(other, "__iter__") or not hasattr(other, "__len__") or len(self) != len(other):
return False
Expand Down
Loading