simetri.graphics.common
Simetri library's constants and common data.
1"""Simetri library's constants and common data.""" 2 3from math import pi, cos, sin 4from typing import Sequence, Tuple, Any, Iterator 5from ..settings.settings import defaults 6from ..helpers.vector import Vector2D 7 8Point = Sequence[float] # used for type hinting 9Vec2 = Tuple[float, float] # used for type hinting size, scale, offset etc. 10Line = Sequence[Sequence] # used for type hinting 11VecType = Sequence[float] # used for type hinting 12Polyline = Sequence[Point] # used for type hinting 13Polygon = Sequence[Point] # used for type hinting 14GraphEdge = Tuple[int, int] # used for type hinting 15Matrix = Sequence[Sequence[float]] # used for type hinting 16 17INCH = 72 # (used for converting inches to points) 18CM = 28.3464 # (used for converting centimeters to points) 19MM = 2.83464 # (used for converting millimeters to points) 20# 2 * inch is equal to 144 points 21# 10 * cm is equal to 283.46456 points 22 23VOID = "VOID" # used for non-existent values 24UNDER: bool = True 25 26# Pre-computed values 27two_pi = 2 * pi # 360 degrees 28tau = 2 * pi # 360 degrees 29phi = (1 + 5**0.5) / 2 # golden ratio 30 31_d_id_obj = {} # dictionary of obj.id: obj, use get_item_by_id(id) 32 33def common_properties(obj, graphics_object=True, id_only=False): 34 """ 35 Set common properties for an object. All objects in Simetri have these properties. 36 37 Args: 38 obj (Any): The object to set properties for. 39 graphics_object (bool, optional): Whether the object is a graphics object. Defaults to True. 40 id_only (bool, optional): Whether to set only the id. Defaults to False. 41 """ 42 obj.id = get_unique_id(obj) 43 _d_id_obj[obj.id] = obj 44 if id_only: 45 return 46 obj.active = True 47 if graphics_object: 48 obj.visible = True 49 50def gen_unique_ids() -> Iterator[int]: 51 """ 52 Generate unique Ids. 53 Every object in Simetri has a unique id. 54 55 Yields: 56 Iterator[int]: A unique id. 57 """ 58 id_ = 0 59 while True: 60 yield id_ 61 id_ += 1 62 63def get_item_by_id(id_: int) -> Any: 64 """ 65 Return an object by its id. 66 67 Args: 68 id_ (int): The id of the object. 69 70 Returns: 71 Any: The object with the given id. 72 """ 73 return _d_id_obj[id_] 74 75unique_id = gen_unique_ids() 76 77def get_unique_id(item) -> int: 78 """ 79 Return a unique id. 80 Every object in Simetri has a unique id. 81 Register the object in _d_id_obj. 82 83 Args: 84 item (Any): The object to get a unique id for. 85 86 Returns: 87 int: The unique id. 88 """ 89 id_ = next(unique_id) 90 _d_id_obj[id_] = item 91 return id_ 92 93origin = (0.0, 0.0) # used for a point at the origin 94axis_x = (origin, (1.0, 0.0)) # used for a line along x axis 95axis_y = (origin, (0.0, 1.0)) # used for a line along y axis 96 97axis_hex = ( 98 (0.0, 0.0), 99 (cos(pi / 3), sin(pi / 3)), 100) # used for 3 and 6 rotation symmetries 101 102i_vec = Vector2D(1.0, 0.0) # x direction unit vector 103j_vec = Vector2D(0.0, 1.0) # y direction unit vector 104 105def _set_Nones(obj, args, values): 106 """ 107 Internally used in instance construction to set default values for None values. 108 109 Args: 110 obj (Any): The object to set values for. 111 args (list): The arguments to set. 112 values (list): The values to set. 113 """ 114 for i, arg in enumerate(args): 115 if values[i] is None: 116 setattr(obj, arg, defaults[arg]) 117 else: 118 setattr(obj, arg, values[i]) 119 120def get_defaults(args, values): 121 """ 122 Internally used in instance construction to set default values for None values. 123 124 Args: 125 args (list): The arguments to set. 126 values (list): The values to set. 127 128 Returns: 129 list: The default values. 130 """ 131 res = len(args) * [None] 132 for i, arg in enumerate(args): 133 if values[i] is None: 134 res[i] = defaults[arg] 135 else: 136 res[i] = values[i] 137 138 return res
Point =
typing.Sequence[float]
Vec2 =
typing.Tuple[float, float]
Line =
typing.Sequence[typing.Sequence]
VecType =
typing.Sequence[float]
Polyline =
typing.Sequence[typing.Sequence[float]]
Polygon =
typing.Sequence[typing.Sequence[float]]
GraphEdge =
typing.Tuple[int, int]
Matrix =
typing.Sequence[typing.Sequence[float]]
INCH =
72
CM =
28.3464
MM =
2.83464
VOID =
'VOID'
UNDER: bool =
True
two_pi =
6.283185307179586
tau =
6.283185307179586
phi =
1.618033988749895
def
common_properties(obj, graphics_object=True, id_only=False):
34def common_properties(obj, graphics_object=True, id_only=False): 35 """ 36 Set common properties for an object. All objects in Simetri have these properties. 37 38 Args: 39 obj (Any): The object to set properties for. 40 graphics_object (bool, optional): Whether the object is a graphics object. Defaults to True. 41 id_only (bool, optional): Whether to set only the id. Defaults to False. 42 """ 43 obj.id = get_unique_id(obj) 44 _d_id_obj[obj.id] = obj 45 if id_only: 46 return 47 obj.active = True 48 if graphics_object: 49 obj.visible = True
Set common properties for an object. All objects in Simetri have these properties.
Arguments:
- obj (Any): The object to set properties for.
- graphics_object (bool, optional): Whether the object is a graphics object. Defaults to True.
- id_only (bool, optional): Whether to set only the id. Defaults to False.
def
gen_unique_ids() -> Iterator[int]:
51def gen_unique_ids() -> Iterator[int]: 52 """ 53 Generate unique Ids. 54 Every object in Simetri has a unique id. 55 56 Yields: 57 Iterator[int]: A unique id. 58 """ 59 id_ = 0 60 while True: 61 yield id_ 62 id_ += 1
Generate unique Ids. Every object in Simetri has a unique id.
Yields:
Iterator[int]: A unique id.
def
get_item_by_id(id_: int) -> Any:
64def get_item_by_id(id_: int) -> Any: 65 """ 66 Return an object by its id. 67 68 Args: 69 id_ (int): The id of the object. 70 71 Returns: 72 Any: The object with the given id. 73 """ 74 return _d_id_obj[id_]
Return an object by its id.
Arguments:
- id_ (int): The id of the object.
Returns:
Any: The object with the given id.
unique_id =
<generator object gen_unique_ids>
def
get_unique_id(item) -> int:
78def get_unique_id(item) -> int: 79 """ 80 Return a unique id. 81 Every object in Simetri has a unique id. 82 Register the object in _d_id_obj. 83 84 Args: 85 item (Any): The object to get a unique id for. 86 87 Returns: 88 int: The unique id. 89 """ 90 id_ = next(unique_id) 91 _d_id_obj[id_] = item 92 return id_
Return a unique id. Every object in Simetri has a unique id. Register the object in _d_id_obj.
Arguments:
- item (Any): The object to get a unique id for.
Returns:
int: The unique id.
origin =
(0.0, 0.0)
axis_x =
((0.0, 0.0), (1.0, 0.0))
axis_y =
((0.0, 0.0), (0.0, 1.0))
axis_hex =
((0.0, 0.0), (0.5000000000000001, 0.8660254037844386))
i_vec =
(1.00, 0.00)
j_vec =
(0.00, 1.00)
def
get_defaults(args, values):
121def get_defaults(args, values): 122 """ 123 Internally used in instance construction to set default values for None values. 124 125 Args: 126 args (list): The arguments to set. 127 values (list): The values to set. 128 129 Returns: 130 list: The default values. 131 """ 132 res = len(args) * [None] 133 for i, arg in enumerate(args): 134 if values[i] is None: 135 res[i] = defaults[arg] 136 else: 137 res[i] = values[i] 138 139 return res
Internally used in instance construction to set default values for None values.
Arguments:
- args (list): The arguments to set.
- values (list): The values to set.
Returns:
list: The default values.