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 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

* 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.
* 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
12 changes: 11 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 @@ -172,7 +173,16 @@
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(

Check warning on line 179 in src/compas/colors/color.py

View check run for this annotation

Codecov / codecov/patch

src/compas/colors/color.py#L179

Added line #L179 was not covered by tests
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 @@ -108,6 +108,14 @@
self.yaxis,
)

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

Check warning on line 112 in src/compas/geometry/frame.py

View check run for this annotation

Codecov / codecov/patch

src/compas/geometry/frame.py#L112

Added line #L112 was not covered by tests
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 @@
self.normal,
)

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

Check warning on line 80 in src/compas/geometry/plane.py

View check run for this annotation

Codecov / codecov/patch

src/compas/geometry/plane.py#L80

Added line #L80 was not covered by tests
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 @@ -56,6 +56,9 @@
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))

Check warning on line 60 in src/compas/geometry/pointcloud.py

View check run for this annotation

Codecov / codecov/patch

src/compas/geometry/pointcloud.py#L60

Added line #L60 was not covered by tests

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

Expand Down Expand Up @@ -244,11 +247,9 @@
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 @@ -104,6 +104,9 @@
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__, ", ".join([str(point) for point in self.points]))

Check warning on line 108 in src/compas/geometry/polygon.py

View check run for this annotation

Codecov / codecov/patch

src/compas/geometry/polygon.py#L108

Added line #L108 was not covered by tests

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

Expand Down
8 changes: 8 additions & 0 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 transform_points
from compas.itertools import pairwise
from compas.tolerance import TOL

from .geometry import Geometry
gonzalocasas marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -216,6 +217,13 @@
self.faces,
)

def __str__(self):
return "{0}(vertices={1}, faces={2})".format(

Check warning on line 221 in src/compas/geometry/polyhedron.py

View check run for this annotation

Codecov / codecov/patch

src/compas/geometry/polyhedron.py#L221

Added line #L221 was not covered by tests
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
9 changes: 9 additions & 0 deletions src/compas/geometry/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,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
11 changes: 11 additions & 0 deletions tests/compas/geometry/test_pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import compas
from random import random, shuffle
from compas.geometry import Box
from compas.geometry import Point # noqa: F401
from compas.geometry import Pointcloud

Expand Down Expand Up @@ -54,3 +55,13 @@ def test_pointcloud__neq__():
assert a != b
b = Pointcloud.from_bounds(10, 10, 10, 10)
assert a != b


def test_pointcloud_from_box():
x_size = 10.0
y_size = 5.0
z_size = 3.0
box = Box.from_width_height_depth(x_size, z_size, y_size)
pointcloud = Pointcloud.from_box(box, 100)
assert len(pointcloud.points) == 100
assert all((-x_size / 2 < x < x_size / 2) and (-y_size / 2 < y < y_size / 2) and (-z_size / 2 < z < z_size / 2) for x, y, z in pointcloud.points)
22 changes: 22 additions & 0 deletions tests/compas/geometry/test_polyhedron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
import json
import compas
from random import random
from compas.geometry import Point
from compas.geometry import Polyhedron
from compas.itertools import pairwise


def test_polyhedron():
vertices = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]
faces = [[0, 1, 2, 3]]
name = "Test Polyhedron"
polyhedron = Polyhedron(vertices, faces, name)

assert polyhedron.vertices == vertices
assert polyhedron.faces == faces
assert polyhedron.name == name
assert polyhedron.points == vertices
assert polyhedron.lines == [(a, b) for a, b in pairwise(vertices + vertices[:1])]
assert polyhedron.points[0] == vertices[0]
assert polyhedron.points[-1] != polyhedron.points[0]
3 changes: 2 additions & 1 deletion tests/compas/geometry/test_quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,5 @@ def test_quaternion_other_methods():
quaternion = Quaternion.from_frame(Frame.worldZX())
canonized = quaternion.canonized()

assert str(canonized) == str("Quaternion(0.5, -0.5, -0.5, -0.5)")
value = TOL.format_number(0.5)
assert str(canonized) == str("Quaternion(" + value + ", -" + value + ", -" + value + ", -" + value + ")")
Loading