halfedge 0.2.0__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
halfedge/__init__.py CHANGED
@@ -1,6 +1,7 @@
1
1
  """Allow modules to be imported from top-level."""
2
2
 
3
- from halfedge.half_edge_elements import Edge, Face, Vert
3
+ from halfedge.half_edge_constructors import BlindHalfEdges
4
+ from halfedge.half_edge_elements import Edge, Face, MeshElementBase, Vert
4
5
  from halfedge.half_edge_object import HalfEdges
5
6
  from halfedge.type_attrib import (
6
7
  Attrib,
@@ -13,11 +14,13 @@ from halfedge.type_attrib import (
13
14
 
14
15
  __all__ = [
15
16
  "Attrib",
17
+ "BlindHalfEdges",
16
18
  "ContagionAttrib",
17
19
  "Edge",
18
20
  "Face",
19
21
  "HalfEdges",
20
22
  "IncompatibleAttrib",
23
+ "MeshElementBase",
21
24
  "NumericAttrib",
22
25
  "Vector2Attrib",
23
26
  "Vector3Attrib",
@@ -30,7 +30,10 @@ from paragraphs import par
30
30
  from halfedge.half_edge_elements import Edge, Face, ManifoldMeshError, Vert
31
31
 
32
32
  if TYPE_CHECKING:
33
- from halfedge.type_attrib import Attrib
33
+ from halfedge.type_attrib import Attrib, StaticAttrib
34
+
35
+
36
+ _T = TypeVar("_T")
34
37
 
35
38
  _TBlindHalfEdges = TypeVar("_TBlindHalfEdges", bound="BlindHalfEdges")
36
39
 
@@ -44,6 +47,27 @@ class BlindHalfEdges:
44
47
  self.edges: set[Edge] = set()
45
48
  else:
46
49
  self.edges = edges
50
+ self.attrib: dict[str, StaticAttrib[Any]] = {}
51
+
52
+ def set_attrib(self, attrib: StaticAttrib[Any]) -> None:
53
+ """Set an attribute.
54
+
55
+ :param attrib: StaticAttrib instance
56
+ """
57
+ self.attrib[type(attrib).__name__] = attrib.copy_to_element(self)
58
+
59
+ def get_attrib(self, attrib: type[StaticAttrib[_T]]) -> StaticAttrib[_T]:
60
+ """Get a StaticAttrib.
61
+
62
+ :param attrib: StaticAttrib class
63
+ :returns: StaticAttrib instance
64
+ :raise AttributeError: if StaticAttrib not found in self.attrib
65
+ """
66
+ try:
67
+ return self.attrib[attrib.__name__]
68
+ except KeyError as e:
69
+ msg = f"{attrib.__name__} not found in {self.__class__.__name__}"
70
+ raise AttributeError(msg) from e
47
71
 
48
72
  def new_vert(self, *attributes: Attrib[Any], edge: Edge | None = None) -> Vert:
49
73
  """Create a new Vert instance.
halfedge/type_attrib.py CHANGED
@@ -66,11 +66,106 @@ from typing import TYPE_CHECKING, Any, Generic, Literal, Tuple, TypeVar
66
66
  from paragraphs import par
67
67
 
68
68
  if TYPE_CHECKING:
69
+ from halfedge.half_edge_constructors import BlindHalfEdges
69
70
  from halfedge.half_edge_elements import MeshElementBase
70
71
 
71
72
  _T = TypeVar("_T")
72
73
 
73
74
 
75
+ class StaticAttrib(Generic[_T]):
76
+ """Base class for storing a, potentially inferred, attribute value.
77
+
78
+ This is the equivalent to the Attrib class, but for meshes, which will never be
79
+ merged or split.
80
+ """
81
+
82
+ __slots__ = ("_value", "mesh")
83
+
84
+ def __new__(
85
+ cls: type[_TStaticAttrib],
86
+ value: _T | None = None,
87
+ mesh: BlindHalfEdges | None = None,
88
+ ) -> _TStaticAttrib:
89
+ """Raise an exception if the attribute is not subclassed."""
90
+ del value
91
+ del mesh
92
+ if cls is StaticAttrib:
93
+ msg = "StaticAttrib is an abstract class and cannot be instantiated."
94
+ raise TypeError(msg)
95
+ return object.__new__(cls)
96
+
97
+ def __init__(
98
+ self, value: _T | None = None, mesh: BlindHalfEdges | None = None
99
+ ) -> None:
100
+ """Set value and mesh."""
101
+ self._value = value
102
+ self.mesh = mesh
103
+
104
+ def copy_to_element(
105
+ self: StaticAttrib[_T], mesh: BlindHalfEdges
106
+ ) -> StaticAttrib[_T]:
107
+ """Return a new instance with the same value, assigned to a new mesh.
108
+
109
+ :param mesh: BlindHalfEdges instance to which attrib will be assigned.
110
+ :return: Attrib instance
111
+ """
112
+ return type(self)(self._value, mesh)
113
+
114
+ @property
115
+ def value(self) -> _T:
116
+ """Return value if set, else try to infer a value.
117
+
118
+ :return: Value of the attribute
119
+ :raises AttributeError: If no value is set and _infer_value fails
120
+ """
121
+ if self._value is not None:
122
+ return self._value
123
+ with suppress(NotImplementedError, ValueError):
124
+ value = self._infer_value()
125
+ self._value = value
126
+ return self._value
127
+ msg = "no value set and failed to infer from 'self.mesh'"
128
+ raise AttributeError(msg)
129
+
130
+ def _infer_value(self) -> _T:
131
+ """Get value of self from self._mesh.
132
+
133
+ Use the containing mesh to determine a value for self. If no value can be
134
+ determined, return None.
135
+
136
+ The purpose is to allow lazy attributes like edge norm and face area. Use
137
+ caution, however. These need to be calculated before merging since the method
138
+ may not support the new shape. For instance, this method might calculate the
139
+ area of a triangle, but would fail if two triangles were merged into a
140
+ square. To keep this safe, the _value is calculated *before* any merging. In
141
+ the "area of a triangle" example,
142
+
143
+ * The area calculation is deferred until the first merge.
144
+ * At the first merge, the area of each merged triangle is calculated. The
145
+ implication here is that calculation *cannot* be deferred till after a
146
+ merge.
147
+ * The merged method sums areas of the merged triangles at the first and
148
+ subsequent mergers, so further triangle area calculations (which
149
+ wouldn't work on the merged shapes anyway) are not required.
150
+
151
+ If you infer a value, cache it by setting self._value.
152
+
153
+ If you do not intend to infer values, raise an exception. This exception
154
+ should occur *before* an AttributeError is raised for a potentially missing
155
+ mesh attribute. It should be clear that _infer_value failed because there
156
+ is no provision for inferring this Attrib.value, *not* because the
157
+ user failed to set the Attrib property attribute.
158
+ """
159
+ msg = par(
160
+ f"""'{type(self).__name__}' has no provision for inferring a value from
161
+ 'self.mesh'"""
162
+ )
163
+ raise AttributeError(msg)
164
+
165
+
166
+ _TStaticAttrib = TypeVar("_TStaticAttrib", bound=StaticAttrib[Any])
167
+
168
+
74
169
  class Attrib(Generic[_T]):
75
170
  """Base class for element attributes.
76
171
 
@@ -121,7 +216,7 @@ class Attrib(Generic[_T]):
121
216
  value = self._infer_value()
122
217
  self._value = value
123
218
  return self._value
124
- msg = f"no value set and failed to infer from {self.element}"
219
+ msg = "no value set and failed to infer from 'self.element'"
125
220
  raise AttributeError(msg)
126
221
 
127
222
  def copy_to_element(self: Attrib[_T], element: MeshElementBase) -> Attrib[_T]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: halfedge
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: A typical half-edge data structure with some padding
5
5
  Author-email: Shay Hill <shay_public@hotmail.com>
6
6
  License: MIT
@@ -0,0 +1,12 @@
1
+ halfedge/__init__.py,sha256=SIezwH5Ox5DQDw5Rr5lmg1W0Y8JW2Q90gQa_PHpKVoY,623
2
+ halfedge/half_edge_constructors.py,sha256=ToGcOYW2b848ynlY59xRNd7ibZuu3OqQpauPCl-N1YE,8654
3
+ halfedge/half_edge_elements.py,sha256=6PkPjGxe_0FYSWXi5MY8Yp4an7yYMWir8MPZn8QlUOc,18042
4
+ halfedge/half_edge_object.py,sha256=Cc1t_hy2tF52O9iD5tvZN6_w4H7Wt9hwDGFLdBqUxr4,28451
5
+ halfedge/half_edge_querries.py,sha256=q5xVOFdpw7t6fhs-Z8yF5j3RlG4DOrOgoLbBsAfkm4A,5179
6
+ halfedge/py.typed,sha256=MsSFjiLMLJZ7QhUPpVBWKiyDnCzryquRyr329NoCACI,2
7
+ halfedge/type_attrib.py,sha256=bo0Tw-9dhvi0XwehaYY9WowEZuRKlg07Vx0vRuJtDPw,19702
8
+ halfedge/validations.py,sha256=FhUv_J7XcaAduyudYGmi7ZL1tEqwJoCxKZwJU2zQolg,4512
9
+ halfedge-0.3.0.dist-info/METADATA,sha256=gfvX38VnIi-mGzjVwD5sJUphMigznpZF0uny0gnRRyE,7411
10
+ halfedge-0.3.0.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
11
+ halfedge-0.3.0.dist-info/top_level.txt,sha256=iqphKHiIR4DfFMs14-gkFOWTXVkRWfGmDZmCYHhuoFM,9
12
+ halfedge-0.3.0.dist-info/RECORD,,
@@ -1,12 +0,0 @@
1
- halfedge/__init__.py,sha256=XuyK1eJ6mn_0Y7GYZ9yZKGjUf2lBDr31u8l_pW9q49k,502
2
- halfedge/half_edge_constructors.py,sha256=AVxbZRuqEgjaWut3R7FfJBLid-9eDPscUQNoFh_gwrQ,7850
3
- halfedge/half_edge_elements.py,sha256=6PkPjGxe_0FYSWXi5MY8Yp4an7yYMWir8MPZn8QlUOc,18042
4
- halfedge/half_edge_object.py,sha256=Cc1t_hy2tF52O9iD5tvZN6_w4H7Wt9hwDGFLdBqUxr4,28451
5
- halfedge/half_edge_querries.py,sha256=q5xVOFdpw7t6fhs-Z8yF5j3RlG4DOrOgoLbBsAfkm4A,5179
6
- halfedge/py.typed,sha256=MsSFjiLMLJZ7QhUPpVBWKiyDnCzryquRyr329NoCACI,2
7
- halfedge/type_attrib.py,sha256=61HLkMlvPYbjT6ijaKms-3z9c-844sS8j3QBKYwA720,15980
8
- halfedge/validations.py,sha256=FhUv_J7XcaAduyudYGmi7ZL1tEqwJoCxKZwJU2zQolg,4512
9
- halfedge-0.2.0.dist-info/METADATA,sha256=bj7tyx2nCeNx2wNGbDjJwboGiAhSl5Unc6OpzZAq0uk,7411
10
- halfedge-0.2.0.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
11
- halfedge-0.2.0.dist-info/top_level.txt,sha256=iqphKHiIR4DfFMs14-gkFOWTXVkRWfGmDZmCYHhuoFM,9
12
- halfedge-0.2.0.dist-info/RECORD,,