Skip to content

Commit 9f58472

Browse files
chriswmackeyChris Mackey
authored and
Chris Mackey
committed
style(dict): Changing the dict schema of all geometry objects
1 parent cd6d264 commit 9f58472

File tree

16 files changed

+137
-69
lines changed

16 files changed

+137
-69
lines changed

ladybug_geometry/geometry2d/_1d.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ def from_dict(cls, data):
3333
3434
Args:
3535
data: {
36-
"p": {"x": 10, "y": 0},
37-
"v": {"x": 10, "y": 10}}
36+
"p": [10, 0],
37+
"v": [10, 10]}
3838
"""
39-
return cls(Point2D.from_dict(data['p']), Vector2D.from_dict(data['v']))
39+
return cls(Point2D(data['p'][0], data['p'][1]),
40+
Vector2D(data['v'][0], data['v'][1]))
4041

4142
@property
4243
def p(self):
@@ -89,8 +90,8 @@ def duplicate(self):
8990

9091
def to_dict(self):
9192
"""Get LineSegment2D/Ray2D as a dictionary."""
92-
return {'p': self.p.to_dict(),
93-
'v': self.v.to_dict()}
93+
return {'p': (self.p.x, self.p.y),
94+
'v': (self.v.x, self.v.y)}
9495

9596
def __copy__(self):
9697
return self.__class__(self.p, self.v)

ladybug_geometry/geometry2d/arc.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ def from_dict(cls, data):
5656
5757
Args:
5858
data: {
59-
"c": {"x": 10, "y": 0},
59+
"type": "Arc2D"
60+
"c": [10, 0],
6061
"r": 5,
6162
"a1": 0,
6263
"a2": 3.14159}
6364
"""
64-
return cls(Point2D.from_dict(data['c']), data['r'], data['a1'], data['a2'])
65+
return cls(Point2D(data['c'][0], data['c'][1]),
66+
data['r'], data['a1'], data['a2'])
6567

6668
@classmethod
6769
def from_start_mid_end(cls, p1, m, p2, circle=False):
@@ -367,7 +369,7 @@ def duplicate(self):
367369

368370
def to_dict(self):
369371
"""Get Arc2D as a dictionary."""
370-
return {'c': self.c.to_dict(),
372+
return {'type': 'Arc2D', 'c': (self.c.x, self.c.y),
371373
'r': self.r, 'a1': self.a1, 'a2': self.a2}
372374

373375
def _pt_in(self, point):

ladybug_geometry/geometry2d/line.py

+6
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ def distance_to_line(self, line):
200200
dist, pts = closest_point2d_between_line2d(self, line)
201201
return dist
202202

203+
def to_dict(self):
204+
"""Get LineSegment2D as a dictionary."""
205+
base = Base1DIn2D.to_dict(self)
206+
base['type'] = 'LineSegment2D'
207+
return base
208+
203209
def _u_in(self, u):
204210
return u >= 0.0 and u <= 1.0
205211

ladybug_geometry/geometry2d/mesh.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def from_dict(cls, data):
6363
6464
Args:
6565
data: {
66-
"vertices": [{"x": 0, "y": 0}, {"x": 10, "y": 0}, {"x": 0, "y": 10}],
66+
"type": "Mesh2D",
67+
"vertices": [[0, 0], [10, 0], [0, 10]],
6768
"faces": [(0, 1, 2)],
6869
"colors": [{"r": 255, "g": 0, "b": 0}]
6970
}
@@ -76,7 +77,7 @@ def from_dict(cls, data):
7677
raise ImportError('Colors are specified in input Mesh2D dictionary '
7778
'but failed to import ladybug.color')
7879
colors = tuple(Color.from_dict(col) for col in data['colors'])
79-
return cls(tuple(Point2D.from_dict(pt) for pt in data['vertices']),
80+
return cls(tuple(Point2D(pt[0], pt[1]) for pt in data['vertices']),
8081
data['faces'], colors)
8182

8283
@classmethod
@@ -382,7 +383,8 @@ def to_dict(self):
382383
colors = None
383384
if self.colors is not None:
384385
colors = [col.to_dict() for col in self.colors]
385-
return {'vertices': [pt.to_dict() for pt in self.vertices],
386+
return {'type': 'Mesh2D',
387+
'vertices': [(pt.x, pt.y) for pt in self.vertices],
386388
'faces': self.faces, 'colors': colors}
387389

388390
def _calculate_min_max(self):

ladybug_geometry/geometry2d/pointvector.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ def duplicate(self):
126126
return self.__copy__()
127127

128128
def to_dict(self):
129-
"""Get Vector2D/Point2D as a dictionary."""
130-
return {'x': self.x,
129+
"""Get Vector2D as a dictionary."""
130+
return {'type': 'Vector2D',
131+
'x': self.x,
131132
'y': self.y}
132133

133134
def _cast_to_float(self, value):
@@ -340,6 +341,12 @@ def is_equivalent(self, point, tolerance):
340341
return abs(self.x - point.x) <= tolerance and \
341342
abs(self.y - point.y) <= tolerance
342343

344+
def to_dict(self):
345+
"""Get Point2D as a dictionary."""
346+
return {'type': 'Point2D',
347+
'x': self.x,
348+
'y': self.y}
349+
343350
def __add__(self, other):
344351
# Point + Vector -> Point
345352
# Point + Point -> Vector

ladybug_geometry/geometry2d/polygon.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ def from_dict(cls, data):
5656
5757
Args:
5858
data: {
59-
"vertices": [{"x": 0, "y": 0}, {"x": 10, "y": 0}, {"x": 0, "y": 10}]
59+
"type": "Polygon2D",
60+
"vertices": [[0, 0], [10, 0], [0, 10]]
6061
}
6162
"""
62-
return cls(tuple(Point2D.from_dict(pt) for pt in data['vertices']))
63+
return cls(tuple(Point2D(pt[0], pt[1]) for pt in data['vertices']))
6364

6465
@classmethod
6566
def from_rectangle(cls, base_point, height_vector, base, height):
@@ -627,7 +628,8 @@ def is_polygon_outside(self, polygon):
627628

628629
def to_dict(self):
629630
"""Get Polygon2D as a dictionary."""
630-
return {'vertices': [pt.to_dict() for pt in self.vertices]}
631+
return {'type': 'Polygon2D',
632+
'vertices': [(pt.x, pt.y) for pt in self.vertices]}
631633

632634
def _transfer_properties(self, new_polygon):
633635
"""Transfer properties from this polygon to a new polygon.

ladybug_geometry/geometry2d/ray.py

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def scale(self, factor, origin=None):
6868
"""
6969
return Ray2D(self.p.scale(factor, origin), self.v * factor)
7070

71+
def to_dict(self):
72+
"""Get Ray2D as a dictionary."""
73+
base = Base1DIn2D.to_dict(self)
74+
base['type'] = 'Ray2D'
75+
return base
76+
7177
def _u_in(self, u):
7278
return u >= 0.0
7379

ladybug_geometry/geometry3d/_1d.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ def from_dict(cls, data):
3434
3535
Args:
3636
data: {
37-
"p": {"x": 10, "y": 0, "z": 0},
38-
"v": {"x": 10, "y": 10, "z": 0}}
37+
"p": [10, 0, 0],
38+
"v": [10, 10, 0]}
3939
"""
40-
return cls(Point3D.from_dict(data['p']), Vector3D.from_dict(data['v']))
40+
return cls(Point3D(data['p'][0], data['p'][1], data['p'][2]),
41+
Vector3D(data['v'][0], data['v'][1], data['v'][2]))
4142

4243
@property
4344
def p(self):
@@ -114,8 +115,8 @@ def duplicate(self):
114115

115116
def to_dict(self):
116117
"""Get LineSegment3D/Ray3D as a dictionary."""
117-
return {'p': self.p.to_dict(),
118-
'v': self.v.to_dict()}
118+
return {'p': (self.p.x, self.p.y, self.p.z),
119+
'v': (self.v.x, self.v.y, self.v.z)}
119120

120121
def __copy__(self):
121122
return self.__class__(self.p, self.v)

ladybug_geometry/geometry3d/arc.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def from_dict(cls, data):
5151
5252
Args:
5353
data: {
54-
"plane": {"n": {"x": 0, "y": 0, "z": 1}, "o": {"x": 0, "y": 10, "z": 0},
55-
"x": {"x": 1, "y": 0, "z": 0}},
54+
"type": "Arc3D"
55+
"plane": {"n": [0, 0, 1], "o": [0, 10, 0], "x": [1, 0, 0]},
5656
"radius": 5,
5757
"a1": 0,
5858
"a2": 3.14159}
@@ -305,7 +305,7 @@ def duplicate(self):
305305

306306
def to_dict(self):
307307
"""Get Arc3D as a dictionary."""
308-
return {'plane': self.plane.to_dict(),
308+
return {'type': 'Arc3D', 'plane': self.plane.to_dict(),
309309
'radius': self.radius, 'a1': self.a1, 'a2': self.a2}
310310

311311
@staticmethod

ladybug_geometry/geometry3d/face.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,21 @@ def from_dict(cls, data):
141141
142142
Args:
143143
data: {
144-
"boundary": [{"x": 0, "y": 0, "z": 0}, {"x": 10, "y": 0, "z": 0},
145-
{"x": 0, "y": 10, "z": 0}],
146-
"plane": {"n": {"x": 0, "y": 0, "z": 1}, "o": {"x": 0, "y": 0, "z": 0},
147-
"x": {"x": 1, "y": 0, "z": 0}}
148-
"holes": [[{"x": 2, "y": 2, "z": 0}, {"x": 5, "y": 2, "z": 0},
149-
{"x": 2, "y": 5, "z": 0}]],
144+
"type": "Face3D",
145+
"boundary": [[0, 0, 0], [10, 0, 0], [0, 10, 0]],
146+
"plane": {"n": [0, 0, 1], "o": [0, 0, 0], "x": [1, 0, 0]},
147+
"holes": [[[2, 2, 0], [5, 2, 0], [2, 5, 0]]]
150148
}
151149
"""
152150
holes = None
153151
if 'holes' in data and data['holes'] is not None:
154-
holes = tuple(
155-
tuple(Point3D.from_dict(pt) for pt in hole) for hole in data['holes'])
156-
return cls(tuple(Point3D.from_dict(pt) for pt in data['boundary']),
157-
Plane.from_dict(data['plane']), holes)
152+
holes = tuple(tuple(
153+
Point3D(pt[0], pt[1], pt[2]) for pt in hole) for hole in data['holes'])
154+
plane = None
155+
if 'plane' in data and data['plane'] is not None:
156+
plane = Plane.from_dict(data['plane'])
157+
return cls(tuple(Point3D(pt[0], pt[1], pt[2]) for pt in data['boundary']),
158+
plane, holes)
158159

159160
@classmethod
160161
def from_extrusion(cls, line_segment, extrusion_vector):
@@ -1359,15 +1360,22 @@ def sub_rectangles_from_rectangle(base_plane, parent_base, parent_height, ratio,
13591360
base_plane)]
13601361
return final_faces
13611362

1362-
def to_dict(self):
1363-
"""Get Face3D as a dictionary."""
1364-
if not self.has_holes:
1365-
return {'boundary': [pt.to_dict() for pt in self.boundary],
1366-
'plane': self.plane.to_dict()}
1367-
else:
1368-
return {'boundary': [pt.to_dict() for pt in self.boundary],
1369-
'plane': self.plane.to_dict(),
1370-
'holes': [[pt.to_dict() for pt in hole] for hole in self.holes]}
1363+
def to_dict(self, include_plane=True):
1364+
"""Get Face3D as a dictionary.
1365+
1366+
Args:
1367+
include_plane: Set to True to include the Face3D plane in the
1368+
dictionary, which will preserve the underlying orientation
1369+
of the face plane. Default True.
1370+
"""
1371+
base = {'type': 'Face3D',
1372+
'boundary': [(pt.x, pt.y, pt.z) for pt in self.boundary]}
1373+
if include_plane:
1374+
base['plane'] = self.plane.to_dict()
1375+
if self.has_holes:
1376+
base['holes'] = [[(pt.x, pt.y, pt.z) for pt in hole]
1377+
for hole in self.holes]
1378+
return base
13711379

13721380
def _check_vertices_input(self, vertices, loop_name='boundary'):
13731381
if not isinstance(vertices, tuple):

ladybug_geometry/geometry3d/line.py

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ def point_at_length(self, length):
201201
"""
202202
return self.p + self.v * (length / self.length)
203203

204+
def to_dict(self):
205+
"""Get LineSegment3D as a dictionary."""
206+
base = Base1DIn3D.to_dict(self)
207+
base['type'] = 'LineSegment3D'
208+
return base
209+
204210
def _u_in(self, u):
205211
return u >= 0.0 and u <= 1.0
206212

ladybug_geometry/geometry3d/mesh.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ def from_dict(cls, data):
6565
6666
Args:
6767
data: {
68-
"vertices": [{"x": 0, "y": 0, "z": 0}, {"x": 10, "y": 0, "z": 0},
69-
{"x": 0, "y": 10, "z": 0}],
70-
"faces": [(0, 1, 2)],
68+
"type": "Mesh3D",
69+
"vertices": [[0, 0, 0], [10, 0, 0], [0, 10, 0]],
70+
"faces": [[0, 1, 2]],
7171
"colors": [{"r": 255, "g": 0, "b": 0}]
7272
}
7373
"""
@@ -79,7 +79,7 @@ def from_dict(cls, data):
7979
raise ImportError('Colors are specified in input Mesh2D dictionary '
8080
'but failed to import ladybug.color')
8181
colors = tuple(Color.from_dict(col) for col in data['colors'])
82-
return cls(tuple(Point3D.from_dict(pt) for pt in data['vertices']),
82+
return cls(tuple(Point3D(pt[0], pt[1], pt[2]) for pt in data['vertices']),
8383
data['faces'], colors)
8484

8585
@classmethod
@@ -332,11 +332,12 @@ def height_field_mesh(self, values, domain):
332332

333333
def to_dict(self):
334334
"""Get Mesh3D as a dictionary."""
335-
colors = None
335+
base = {'type': 'Mesh3D',
336+
'vertices': [(pt.x, pt.y, pt.z) for pt in self.vertices],
337+
'faces': self.faces}
336338
if self.colors is not None:
337-
colors = [col.to_dict() for col in self.colors]
338-
return {'vertices': [pt.to_dict() for pt in self.vertices],
339-
'faces': self.faces, 'colors': colors}
339+
base['colors'] = [col.to_dict() for col in self.colors]
340+
return base
340341

341342
def _calculate_min_max(self):
342343
"""Calculate maximum and minimum Point3D for this object."""

ladybug_geometry/geometry3d/plane.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ def from_dict(cls, data):
6565
6666
Args:
6767
data: {
68-
"n": {"x": 0, "y": 0, "z": 1},
69-
"o": {"x": 0, "y": 10, "z": 0},
70-
"x": {"x": 1, "y": 0, "z": 0}}
68+
"type": "Plane"
69+
"n": [0, 0, 1],
70+
"o": [0, 10, 0],
71+
"x": [1, 0, 0]}
7172
"""
7273
x = None
7374
if 'x' in data and data['x'] is not None:
74-
x = Vector3D.from_dict(data['x'])
75-
return cls(Vector3D.from_dict(data['n']), Point3D.from_dict(data['o']), x)
75+
x = Vector3D(data['x'][0], data['x'][1], data['x'][2])
76+
return cls(Vector3D(data['n'][0], data['n'][1], data['n'][2]),
77+
Point3D(data['o'][0], data['o'][1], data['o'][2]), x)
7678

7779
@classmethod
7880
def from_three_points(cls, o, p2, p3):
@@ -349,9 +351,10 @@ def duplicate(self):
349351

350352
def to_dict(self):
351353
"""Get Plane as a dictionary."""
352-
return {'n': self.n.to_dict(),
353-
'o': self.o.to_dict(),
354-
'x': self.x.to_dict()}
354+
return {'type': 'Plane',
355+
'n': (self.n.x, self.n.y, self.n.z),
356+
'o': (self.o.x, self.o.y, self.o.z),
357+
'x': (self.x.x, self.x.y, self.x.z)}
355358

356359
def __copy__(self):
357360
return self.__class__(self.n, self.o)

ladybug_geometry/geometry3d/pointvector.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ def duplicate(self):
130130
return self.__copy__()
131131

132132
def to_dict(self):
133-
"""Get Vector3D/Point3D as a dictionary."""
134-
return {'x': self.x,
133+
"""Get Vector3D as a dictionary."""
134+
return {'type': 'Vector3D',
135+
'x': self.x,
135136
'y': self.y,
136137
'z': self.z}
137138

@@ -401,6 +402,13 @@ def is_equivalent(self, point, tolerance):
401402
abs(self.y - point.y) <= tolerance and \
402403
abs(self.z - point.z) <= tolerance
403404

405+
def to_dict(self):
406+
"""Get Point3D as a dictionary."""
407+
return {'type': 'Point3D',
408+
'x': self.x,
409+
'y': self.y,
410+
'z': self.z}
411+
404412
def __add__(self, other):
405413
# Point + Vector -> Point
406414
# Point + Point -> Vector

0 commit comments

Comments
 (0)