File size: 12,032 Bytes
ba246bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from __future__ import annotations

import numpy as np


class Dart:
    _mesh_type: type = None

    def __init__(self, m: _mesh_type, dart_id: int):
        """
        A dart is defined from the mesh_struct it belongs to, and an id, which is the index
        in the mesh_struct container where the dart is stored. In this container, the dart d may be
        defined by 5 fields:
        - the id of the dart
        - the id of the beta1(d),
        - the id of beta2(d)
        - the id of the node d is coming from
        - the id of the face d it belongs to
        When a dart is created, it doesn't know any node or face
        :param m: a mesh_struct
        :param id: a index, that corresponds to the location of the dart data in the mesh_struct dart container
        """
        self.mesh = m
        if not isinstance(dart_id, (int, np.integer)):
            raise ValueError(f"The id must be an integer, {dart_id} is type {type(dart_id)}.")
        self.id = dart_id

    def __eq__(self, a_dart: Dart) -> bool:
        """
        Equality operator between two darts. It is only based on the mesh_struct and dart info
        :param a_dart: another dart
        :return: true if the darts are equal, false otherwise
        """
        if a_dart is None:
            return False
        else:
            return self.mesh == a_dart.mesh and self.id == a_dart.id

    def get_beta(self, i: int) -> Dart:
        """
        Get the dart connected to by alpha_i
        :param i: alpha dimension
        :return: the id of the dart alpha_i(self)
        """
        if i < 1 or i > 2:
            raise ValueError("Wrong alpha dimension")
        if self.mesh.dart_info[self.id, i] == -1:
            return None
        d2_id = self.mesh.dart_info[self.id, i]
        if self.mesh.dart_info[d2_id, 0] <0 :
            raise ValueError("Dart deleted")

        return Dart(self.mesh, self.mesh.dart_info[self.id, i])

    def set_beta(self, i: int, dart_to: Dart) -> None:
        """
        Set the dart connected to by alpha_i
        :param i: dimension of the beta function
        :param dart_to: dart to connect to
        :raises ValueError: if the dimension is not valid
        """
        if i < 1 or i > 2:
            raise ValueError("Wrong alpha dimension")
        elif dart_to is None:
            self.mesh.dart_info[self.id, i] = -1
        else:
            self.mesh.dart_info[self.id, i] = dart_to.id

    def get_node(self) -> Node:
        """
        :return: the embedded node of the dart. It is the node
        the dart comes from
        :raises ValueError: if there is no embedded node
        """
        node_id = self.mesh.dart_info[self.id, 3]
        if node_id == -1:
            raise ValueError("No associated node found")
        elif self.mesh.nodes[node_id, 2] < -1:
            raise ValueError("Node deleted")
        return Node(self.mesh, node_id)

    def set_node(self, node: Node) -> None:
        """
        Set the embedded node to the given value
        :param node: the embedded node
        """
        self.mesh.dart_info[self.id, 3] = node.id

    def get_face(self) -> Face:
        """
        :return: the embedded face of the dart.
        :raises ValueError: if there is no embedded face
        """
        face_id = self.mesh.dart_info[self.id, 4]
        if face_id == -1:
            raise ValueError("No associated face found")
        return Face(self.mesh, face_id)

    def set_face(self, face: Face) -> None:
        """
        Set the embedded face of the dart to the given value
        :param face: the face we want embed the dart on
        """
        self.mesh.dart_info[self.id, 4] = face.id

    def get_quality(self) -> int:
        """
        Get the geometric quality around a given dart.

        :return: the geometric quality around the dart.
        :raises ValueError: if there is no quality dimension
        """
        dart_quality = self.mesh.dart_info[self.id, 5]
        if dart_quality == -99:
            raise ValueError("No quality dimension")
        return dart_quality

    def is_starred(self) -> int:
        """
        Get the geometric quality around a given dart.

        :return: the geometric quality around the dart.
        :raises ValueError: if there is no quality dimension
        """
        is_starred_dart = self.mesh.dart_info[self.id, 6]
        if is_starred_dart == -99:
            raise ValueError("No starred dimension")
        elif is_starred_dart == -1:
            return False
        elif is_starred_dart == 0:
            return False
        elif is_starred_dart == 1:
            return True
        else:
            raise ValueError("Not defined starred value")

    def set_quality(self, quality: int) -> None:
        """
        Set the geometric quality around a given dart. Automatically set the same quality for the twin dart
        The quality is a parameter used to determine whether applying an operation to the dart would flip a face.

            * For triangular meshes:
        The dart's surrounding quality is determined by analyzing the quadrilateral formed by the two adjacent triangles.
        The configuration is classified as convex, crossed, or concave.

            * For quadrilateral meshes:
        The dart's surrounding quality is determined based on whether the associated node forms a "star-shaped" (étoilé) configuration.
        :param quality: calculated quality
        """

        d2_id = self.mesh.dart_info[self.id, 2]
        self.mesh.dart_info[self.id, 5] = quality
        if d2_id >=0: # inner dart
            self.mesh.dart_info[d2_id, 5] = quality

    def set_starred(self, is_starred: int) -> None:
        """
        Set if the surrounding nodes form a "star-shaped" polygon. Automatically set the same quality for the twin dart
        The starred  is a parameter used to determine whether applying an operation to the dart would flip a face.
        :param is_starred: 1 if it forms a "star-shaped" polygon, 0 otherwise
        """
        d2_id = self.mesh.dart_info[self.id, 2]
        self.mesh.dart_info[self.id, 6] = is_starred
        if d2_id >=0: # inner dart
            self.mesh.dart_info[d2_id, 6] = is_starred

class Node:
    _mesh_type: type = None

    def __init__(self, m: _mesh_type, node_id: int):
        """
        A node is defined by the mesh_struct it belongs to and its id in this
        mesh_struct. Node data are stored in an array owned by its mesh_struct. A node
        can be understood as a handler to access data in a more abstract
        way
        :param m: mesh_struct the node belongs to
        :param id: node id
        """
        self.mesh = m
        self.id = node_id

    def __eq__(self, a_node: Node) -> bool:
        """
        Equality operator between two nodes. It is only based on the mesh_struct and node info and
        not on the node coordinate
        :param a_node: another node
        :return: true if the nodes are equal, false otherwise
        """
        return self.mesh == a_node.mesh and self.id == a_node.id

    def set_dart(self, dart: Dart) -> None:
        """
        Update the dart value associated with this node
        :param dart: the dart to be associated to the node
        """
        if dart is None:
            raise ValueError("Try to connect a node to a non-existing dart")

        self.mesh.nodes[self.id, 2] = dart.id

    def get_dart(self) -> Dart:
        """
        Get the dart value associated with this node
        :return: a dart
        """
        return Dart(self.mesh, int(self.mesh.nodes[self.id, 2]))

    def x(self) -> float:
        """
        Return the x coordinate of this node
        :return: the x coordinate of this node
        """
        return self.mesh.nodes[self.id, 0]

    def y(self) -> float:
        """
        Return the y coordinate of this node
        :return: the y coordinate of this node
        """
        return self.mesh.nodes[self.id, 1]

    def set_x(self, x: float) -> None:
        """
        Set the x coordinate of this node
        :param x: a float value
        """
        self.mesh.nodes[self.id, 0] = x

    def set_y(self, y: float) -> None:
        """
        Set the y coordinate of this node
        :param y: a float value
        """
        self.mesh.nodes[self.id, 1] = y

    def set_xy(self, x: float, y: float) -> None:
        """
        Set the node coordinates
        :param x: new x coordinate value
        :param y: new y coordinate value
        """
        self.set_x(x)
        self.set_y(y)

    def set_ideal_adjacency(self, i: int) -> None:
        """
        Set the ideal adjacency of this node.
        :param i: calculated ideal adjacency
        """
        self.mesh.nodes[self.id,3] = i

    def get_ideal_adjacency(self) -> int:
        """
        Get the ideal adjacency of this node.
        :return: ideal adjacency
        :raises ValueError: if there is no ideal adjacency
        """
        ideal_adjacency = self.mesh.nodes[self.id,3]
        if ideal_adjacency == -1:
            raise ValueError("No ideal adjacency")
        return ideal_adjacency

    def set_score(self, s: int) -> None:
        """
        Set the score of a node.
        :param s: calculated score
        """
        self.mesh.nodes[self.id,4] = s

    def get_score(self) -> int:
        """
        Get the score of this node.
        :return: score
        :raises ValueError: if there is no score defined
        """
        score = self.mesh.nodes[self.id,4]
        if score == -99:
            raise ValueError("No score")
        return score


class Face:
    _mesh_type: type = None

    def __init__(self, m: _mesh_type, face_id: int):
        """
        A face is defined by the mesh_struct it belongs to and its id in this
        mesh_struct. Face data are stored in an array owned by its mesh_struct. A face
        can be understood as a handler to access data in a more abstract
        way
        :param m: mesh_struct the node belongs to
        :param id: face id
        """
        self.mesh = m
        self.id = face_id

    def __eq__(self, a_face: Face) -> bool:
        """
        Equality operator between two faces. It is only based on the mesh_struct and node info and
        not on the node coordinate
        :param a_face: another face
        :return: true if the faces are equal, false otherwise
        """
        if a_face is None:
            return False
        else:
            return self.mesh == a_face.mesh and self.id == a_face.id

    def get_nodes(self) -> list[Node]:
        """
        Gives access to the list of nodes that bounds the faces. To get
        this list of nodes, the algorithm starts from the dart owned by the face,
        and traverse the face using beta_1. For each dart, we store the corresponding
        node.
        :return: a list of nodes
        """
        start = self.get_dart()
        nodes_list = []
        do_loop = False
        while not do_loop:
            nodes_list.append(start.get_node())
            if start.get_beta(1) == self.get_dart():
                do_loop = True
            start = start.get_beta(1)

        return nodes_list

    def get_dart(self) -> Dart:
        """
        Returns the unique dart of the mesh_struct, the face point to
        :return: a dart
        """
        return Dart(self.mesh, self.mesh.faces[self.id])

    def set_dart(self, d: Dart) -> None:
        """
        Update the dart the face is defined by in the mesh_struct
        :param d: a new dart to connect to
        """
        if d is None:
            raise ValueError("Try to connect a face to a non-existing dart")
        self.mesh.faces[self.id] = d.id

    def get_surrounding_triangle(self) -> [Dart, Dart, Dart, Node, Node, Node]:
        d = self.get_dart()
        d1 = d.get_beta(1)
        d11 = d1.get_beta(1)
        A = d.get_node()
        B = d1.get_node()
        C = d11.get_node()
        return d, d1, d11, A, B, C