Skip to content

Commit f4cf9cb

Browse files
authored
Merge pull request #1289 from compas-dev/yck011522/issue1287
Add some more __str__ function to geometry classes for pretty print
2 parents b23bf7b + 7d65bbb commit f4cf9cb

File tree

11 files changed

+91
-7
lines changed

11 files changed

+91
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
* 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.
15+
* 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.
16+
* Fixed bug in `Pointcloud.from_box()`.
17+
1418
### Removed
1519

1620

src/compas/colors/color.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from compas.colors.html_colors import HTML_TO_RGB255
1414
from compas.data import Data
15+
from compas.tolerance import TOL
1516

1617
BASE16 = "0123456789abcdef"
1718

@@ -172,7 +173,16 @@ def __init__(self, red, green, blue, alpha=1.0, name=None):
172173
self.a = alpha
173174

174175
def __repr__(self):
175-
return "{0}({1}, {2}, {3}, alpha={4})".format(type(self).__name__, self.r, self.g, self.b, self.a)
176+
return "{0}(red={1}, green={2}, blue={3}, alpha={4})".format(type(self).__name__, self.r, self.g, self.b, self.a)
177+
178+
def __str__(self):
179+
return "{0}(red={1}, green={2}, blue={3}, alpha={4})".format(
180+
type(self).__name__,
181+
TOL.format_number(self.r),
182+
TOL.format_number(self.g),
183+
TOL.format_number(self.b),
184+
TOL.format_number(self.a),
185+
)
176186

177187
def __getitem__(self, key):
178188
if key == 0:

src/compas/geometry/frame.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ def __repr__(self):
108108
self.yaxis,
109109
)
110110

111+
def __str__(self):
112+
return "{0}(point={1}, xaxis={2}, yaxis={3})".format(
113+
type(self).__name__,
114+
str(self.point),
115+
str(self.xaxis),
116+
str(self.yaxis),
117+
)
118+
111119
def __len__(self):
112120
return 3
113121

src/compas/geometry/plane.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ def __repr__(self):
7676
self.normal,
7777
)
7878

79+
def __str__(self):
80+
return "{0}(point={1}, normal={2})".format(
81+
type(self).__name__,
82+
str(self.point),
83+
str(self.normal),
84+
)
85+
7986
def __len__(self):
8087
return 2
8188

src/compas/geometry/pointcloud.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def __init__(self, points, name=None):
5656
def __repr__(self):
5757
return "{0}(points={1!r})".format(type(self).__name__, self.points)
5858

59+
def __str__(self):
60+
return "{0}(len(points)={1})".format(type(self).__name__, len(self.points))
61+
5962
def __len__(self):
6063
return len(self.points)
6164

@@ -244,11 +247,9 @@ def from_box(cls, box, n):
244247
True
245248
246249
"""
247-
points = box.points
248-
x, y, z = zip(*points)
249-
xmin, xmax = min(x), max(x)
250-
ymin, ymax = min(y), max(y)
251-
zmin, zmax = min(z), max(z)
250+
xmin, xmax = box.xmin, box.xmax
251+
ymin, ymax = box.ymin, box.ymax
252+
zmin, zmax = box.zmin, box.zmax
252253
x = [uniform(xmin, xmax) for i in range(n)]
253254
y = [uniform(ymin, ymax) for i in range(n)]
254255
z = [uniform(zmin, zmax) for i in range(n)]

src/compas/geometry/polygon.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def __init__(self, points, name=None):
104104
def __repr__(self):
105105
return "{0}(points={1!r})".format(type(self).__name__, self.points)
106106

107+
def __str__(self):
108+
return "{0}(points=[{1}])".format(type(self).__name__, ", ".join([str(point) for point in self.points]))
109+
107110
def __len__(self):
108111
return len(self.points)
109112

src/compas/geometry/polyhedron.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from compas.geometry import Polygon
1010
from compas.geometry import transform_points
1111
from compas.itertools import pairwise
12+
from compas.tolerance import TOL
1213

1314
from .geometry import Geometry
1415

@@ -216,6 +217,13 @@ def __repr__(self):
216217
self.faces,
217218
)
218219

220+
def __str__(self):
221+
return "{0}(vertices={1}, faces={2})".format(
222+
type(self).__name__,
223+
[[TOL.format_number(num) for num in vertice] for vertice in self.vertices],
224+
self.faces,
225+
)
226+
219227
def __len__(self):
220228
return 2
221229

src/compas/geometry/quaternion.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ def __init__(self, w, x, y, z, name=None):
148148
def __repr__(self):
149149
return "{0}({1}, {2}, {3}, {4})".format(type(self).__name__, self.w, self.x, self.y, self.z)
150150

151+
def __str__(self):
152+
return "{0}({1}, {2}, {3}, {4})".format(
153+
type(self).__name__,
154+
TOL.format_number(self.w),
155+
TOL.format_number(self.x),
156+
TOL.format_number(self.y),
157+
TOL.format_number(self.z),
158+
)
159+
151160
def __eq__(self, other, tol=None):
152161
if not hasattr(other, "__iter__") or not hasattr(other, "__len__") or len(self) != len(other):
153162
return False

tests/compas/geometry/test_pointcloud.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import compas
44
from random import random, shuffle
5+
from compas.geometry import Box
56
from compas.geometry import Point # noqa: F401
67
from compas.geometry import Pointcloud
78

@@ -54,3 +55,13 @@ def test_pointcloud__neq__():
5455
assert a != b
5556
b = Pointcloud.from_bounds(10, 10, 10, 10)
5657
assert a != b
58+
59+
60+
def test_pointcloud_from_box():
61+
x_size = 10.0
62+
y_size = 5.0
63+
z_size = 3.0
64+
box = Box.from_width_height_depth(x_size, z_size, y_size)
65+
pointcloud = Pointcloud.from_box(box, 100)
66+
assert len(pointcloud.points) == 100
67+
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)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
import json
3+
import compas
4+
from random import random
5+
from compas.geometry import Point
6+
from compas.geometry import Polyhedron
7+
from compas.itertools import pairwise
8+
9+
10+
def test_polyhedron():
11+
vertices = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]
12+
faces = [[0, 1, 2, 3]]
13+
name = "Test Polyhedron"
14+
polyhedron = Polyhedron(vertices, faces, name)
15+
16+
assert polyhedron.vertices == vertices
17+
assert polyhedron.faces == faces
18+
assert polyhedron.name == name
19+
assert polyhedron.points == vertices
20+
assert polyhedron.lines == [(a, b) for a, b in pairwise(vertices + vertices[:1])]
21+
assert polyhedron.points[0] == vertices[0]
22+
assert polyhedron.points[-1] != polyhedron.points[0]

tests/compas/geometry/test_quaternion.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,5 @@ def test_quaternion_other_methods():
149149
quaternion = Quaternion.from_frame(Frame.worldZX())
150150
canonized = quaternion.canonized()
151151

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

0 commit comments

Comments
 (0)