Skip to content

Commit a8ee4e2

Browse files
authored
Merge pull request #1286 from compas-dev/serilialisation
Serilialisation
2 parents 5c292ca + 1218397 commit a8ee4e2

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
* Added test for `compas.scene.Scene` serialisation.
13+
1214
### Changed
1315

1416
* Fixed missing implementation of `Sphere.base`.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import pytest # noqa: F401
2+
import compas
3+
4+
from compas.data import Data
5+
from compas.scene import Scene
6+
from compas.geometry import Box
7+
from compas.geometry import Capsule
8+
from compas.geometry import Circle
9+
from compas.geometry import Cone
10+
from compas.geometry import Cylinder
11+
from compas.geometry import Ellipse
12+
from compas.geometry import Frame
13+
from compas.geometry import Line
14+
from compas.geometry import Point
15+
from compas.geometry import Polygon
16+
from compas.geometry import Polyhedron
17+
from compas.geometry import Polyline
18+
from compas.geometry import Sphere
19+
from compas.geometry import Torus
20+
from compas.geometry import Vector
21+
from compas.geometry import Plane
22+
from compas.datastructures import Mesh
23+
from compas.datastructures import Graph
24+
from compas.datastructures import VolMesh
25+
26+
27+
@pytest.fixture
28+
def items():
29+
box = Box.from_width_height_depth(1, 1, 1)
30+
capsule = Capsule(0.5, 1, Frame.worldXY())
31+
circle = Circle(1, Frame.worldXY())
32+
cone = Cone(1, 1, Frame.worldXY())
33+
cylinder = Cylinder(1, 1, Frame.worldXY())
34+
line = Line(Point(0, 0, 0), Point(1, 1, 1))
35+
point = Point(0, 0, 0)
36+
polygon = Polygon.from_sides_and_radius_xy(5, 1)
37+
polyhedron = Polyhedron.from_platonicsolid(4)
38+
polyline = Polyline([[0, 0, 0], [1, 0, 0], [1, 0, 1]])
39+
sphere = Sphere(1)
40+
torus = Torus(1, 0.3, Frame.worldXY())
41+
vector = Vector(0, 0, 1)
42+
ellipse = Ellipse(1, 0.5, Frame.worldXY())
43+
frame = Frame.worldXY()
44+
plane = Plane(Point(0, 0, 0), Vector(0, 0, 1))
45+
mesh = Mesh.from_polyhedron(8)
46+
graph = Graph.from_nodes_and_edges([(0, 0, 0), (0, -1.5, 0), (-1, 1, 0), (1, 1, 0)], [(0, 1), (0, 2), (0, 3)])
47+
volmesh = VolMesh.from_meshgrid(1, 1, 1, 2, 2, 2)
48+
49+
return [
50+
box,
51+
capsule,
52+
circle,
53+
cone,
54+
cylinder,
55+
line,
56+
point,
57+
polygon,
58+
polyhedron,
59+
polyline,
60+
sphere,
61+
torus,
62+
vector,
63+
ellipse,
64+
frame,
65+
plane,
66+
mesh,
67+
graph,
68+
volmesh,
69+
]
70+
71+
72+
def assert_is_data_equal(obj1, obj2, path=""):
73+
74+
if type(obj1) is not type(obj2):
75+
print("Type mismatch: {} != {} for {}:{} and {}:{}".format(type(obj1), type(obj2), path, obj1, path, obj2))
76+
return False
77+
78+
if isinstance(obj1, (list, tuple)):
79+
if len(obj1) != len(obj2):
80+
print("Length mismatch: {} != {} for {} and {}".format(len(obj1), len(obj2), path, path))
81+
return False
82+
83+
for i, (item1, item2) in enumerate(zip(obj1, obj2)):
84+
if not assert_is_data_equal(item1, item2, path="{}[{}]".format(path, i)):
85+
return False
86+
87+
return True
88+
89+
elif isinstance(obj1, dict):
90+
if set(obj1.keys()) != set(obj2.keys()):
91+
print("Key mismatch: {} != {} for {} and {}".format(set(obj1.keys()), set(obj2.keys()), path, path))
92+
return False
93+
94+
for key in obj1:
95+
if not assert_is_data_equal(obj1[key], obj2[key], path='{}["{}"]'.format(path, key)):
96+
return False
97+
98+
return True
99+
100+
elif isinstance(obj1, Data):
101+
return assert_is_data_equal(obj1.__data__, obj2.__data__, path="{}.__data__".format(path))
102+
103+
else:
104+
if obj1 != obj2:
105+
print("Value mismatch: {} != {} for {}:{} and {}:{}".format(obj1, obj2, path, obj1, path, obj2))
106+
return False
107+
else:
108+
return True
109+
110+
111+
def test_scene_serialisation(items, mocker):
112+
113+
if compas.IPY:
114+
mocker.patch("compas.is_rhino", return_value=False)
115+
116+
scene1 = Scene()
117+
for item in items:
118+
scene1.add(item)
119+
120+
scene2 = Scene.from_jsonstring(scene1.to_jsonstring())
121+
assert assert_is_data_equal(scene1, scene2)

0 commit comments

Comments
 (0)