86 lines
2.2 KiB
Python
86 lines
2.2 KiB
Python
from typing import Any, TypeVar, Type, cast
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
def from_float(x: Any) -> float:
|
|
assert isinstance(x, (float, int)) and not isinstance(x, bool)
|
|
return float(x)
|
|
|
|
|
|
def to_float(x: Any) -> float:
|
|
assert isinstance(x, float)
|
|
return x
|
|
|
|
|
|
def to_class(c: Type[T], x: Any) -> dict:
|
|
assert isinstance(x, c)
|
|
return cast(Any, x).to_dict()
|
|
|
|
|
|
class Axis:
|
|
x: float
|
|
y: float
|
|
z: float
|
|
|
|
def __init__(self, x: float, y: float, z: float) -> None:
|
|
self.x = x
|
|
self.y = y
|
|
self.z = z
|
|
|
|
@staticmethod
|
|
def from_dict(obj: Any) -> 'Axis':
|
|
assert isinstance(obj, dict)
|
|
x = from_float(obj.get("x"))
|
|
y = from_float(obj.get("y"))
|
|
z = from_float(obj.get("z"))
|
|
return Axis(x, y, z)
|
|
|
|
def to_dict(self) -> dict:
|
|
result: dict = {}
|
|
result["x"] = to_float(self.x)
|
|
result["y"] = to_float(self.y)
|
|
result["z"] = to_float(self.z)
|
|
return result
|
|
|
|
|
|
class GeometryPart:
|
|
euler: Axis
|
|
position: Axis
|
|
rotation: Axis
|
|
center: Axis
|
|
|
|
def __init__(self, euler: Axis, position: Axis, rotation: Axis, center: Axis) -> None:
|
|
self.euler = euler
|
|
self.position = position
|
|
self.rotation = rotation
|
|
self.center = center
|
|
|
|
@staticmethod
|
|
def from_dict(obj: Any) -> 'GeometryPart':
|
|
assert isinstance(obj, dict)
|
|
euler = Axis.from_dict(obj.get("euler"))
|
|
position = Axis.from_dict(obj.get("position"))
|
|
rotation = Axis.from_dict(obj.get("rotation"))
|
|
center = Axis.from_dict(obj.get("center"))
|
|
return GeometryPart(euler, position, rotation, center)
|
|
|
|
def to_dict(self) -> dict:
|
|
result: dict = {}
|
|
result["euler"] = to_class(Axis, self.euler)
|
|
result["position"] = to_class(Axis, self.position)
|
|
result["rotation"] = to_class(Axis, self.rotation)
|
|
result["center"] = to_class(Axis, self.center)
|
|
return result
|
|
|
|
def toJson(self) -> str:
|
|
return str(self.to_dict()).replace('\'', '"')
|
|
|
|
|
|
def geometry_part_from_dict(s: Any) -> GeometryPart:
|
|
return GeometryPart.from_dict(s)
|
|
|
|
|
|
def geometry_part_to_dict(x: GeometryPart) -> Any:
|
|
return to_class(GeometryPart, x)
|