This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
h5_mesh.py
843 lines (665 loc) · 24.2 KB
/
h5_mesh.py
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
""" Mesh File
This module contains the implentation to store, acces,
and modify a file storing mesh data
"""
import tables
import uuid
from ..cuds.mesh import ABCMesh
from ..cuds.mesh_items import Edge, Face, Cell, Point
from ..core.data_container import DataContainer
from ..core import CUBA
from .data_container_table import DataContainerTable
from .indexed_data_container_table import IndexedDataContainerTable
MAX_POINTS_IN_EDGE = 2
MAX_POINTS_IN_FACE = 4
MAX_POINTS_IN_CELL = 8
MESH_CUDS_VERSION = 1
err_add = "Trying to add an already existing {} with uid: {}"
err_upd = "Trying to update an non existing {} with uid: {}"
class _PointDescriptor(tables.IsDescription):
""" Descriptor for storing Point information
Provides the column definition to store Point
information (x, y, z).
"""
uid = tables.StringCol(32, pos=0)
data = tables.StringCol(32, pos=1)
coordinates = tables.Float64Col(
pos=2, shape=(3,)
)
class _EdgeDescriptor(tables.IsDescription):
""" Descriptor for storing Edge information
Provides the column definition to store Edges
forming an edge and its data.
"""
uid = tables.StringCol(32, pos=0)
data = tables.StringCol(32, pos=1)
points_uids = tables.StringCol(
32, pos=2, shape=(MAX_POINTS_IN_EDGE,)
)
n_points = tables.UInt32Col(pos=3)
class _FaceDescriptor(tables.IsDescription):
""" Descriptor for storing Face information
Provides the column definition to store Faces
forming a face and its data.
"""
uid = tables.StringCol(32, pos=0)
data = tables.StringCol(32, pos=1)
points_uids = tables.StringCol(
32, pos=2, shape=(MAX_POINTS_IN_FACE,)
)
n_points = tables.UInt32Col(pos=3)
class _CellDescriptor(tables.IsDescription):
""" Descriptor for storing Cell information
Provides the column definition to store Cells
forming a cell and its data.
"""
uid = tables.StringCol(32, pos=0)
data = tables.StringCol(32, pos=1)
points_uids = tables.StringCol(
32, pos=2, shape=(MAX_POINTS_IN_CELL,)
)
n_points = tables.UInt32Col(pos=3)
class H5Mesh(ABCMesh):
""" H5Mesh.
Interface of the mesh file driver.
Stores general mesh information Points and Elements
such as Edges, Faces and Cells and provide the
methods to interact with them. The methods are
divided in four diferent blocks:
(1) methods to get the related item with the provided uid;
(2) methods to add a new item or replace;
(3) generator methods that return iterators
over all or some of the mesh items and;
(4) inspection methods to identify if there are any edges,
faces or cells described in the mesh.
Attributes
----------
data : Data
Data relative to the mesh
name : String
Name of the mesh
See Also
--------
get, add, update, iter, has,
_create_points_table, _create_edges_table
_create_faces_table, _create_cells_table
"""
def __init__(self, group, meshFile):
if not ("cuds_version" in group._v_attrs):
group._v_attrs.cuds_version = MESH_CUDS_VERSION
else:
if group._v_attrs.cuds_version != MESH_CUDS_VERSION:
raise ValueError(
"Mesh file layout has an incompatible version")
self._file = meshFile
self._group = group
self._data = IndexedDataContainerTable(group, 'data')
self._uidData = DataContainerTable(self._group, 'item_data')
if "points" not in self._group:
self._create_points_table()
if "edges" not in self._group:
self._create_edges_table()
if "faces" not in self._group:
self._create_faces_table()
if "cells" not in self._group:
self._create_cells_table()
self._items_count = {
CUBA.POINT: lambda: self._group.points,
CUBA.EDGE: lambda: self._group.edges,
CUBA.FACE: lambda: self._group.faces,
CUBA.CELL: lambda: self._group.cells
}
@property
def name(self):
return self._group._v_name
@name.setter
def name(self, value):
self._group._f_rename(value)
@property
def data(self):
if len(self._data) == 1:
return self._data[0]
else:
return DataContainer()
@data.setter
def data(self, value):
if len(self._data) == 0:
self._data.append(value)
else:
self._data[0] = value
def count_of(self, item_type):
""" Return the count of item_type in the container.
Parameters
----------
item_type : CUBA
The CUBA enum of the type of the items to return the count of.
Returns
-------
count : int
The number of items of item_type in the container.
Raises
------
ValueError :
If the type of the item is not supported in the current
container.
"""
try:
return len(self._items_count[item_type]())
except KeyError:
error_str = "Trying to obtain count a of non-supported item: {}"
raise ValueError(error_str.format(item_type))
# Private
def _get_point(self, uid):
""" Returns a point with a given uid.
Returns the point stored in the mesh
identified by uid. If such point do not
exists an exception is raised.
Parameters
----------
uid : UUID
uid of the desired point.
Returns
-------
Point
Mesh point identified by uid
Raises
------
Exception
If the point identified by uid was not found
"""
if not hasattr(uid, 'hex'):
message = 'Expected type for `uid` is uuid.UUID but received {!r}'
raise TypeError(message.format(type(uid)))
for row in self._group.points.where(
'uid == value', condvars={'value': uid.hex}):
return Point(
coordinates=tuple(row['coordinates']),
uid=uuid.UUID(hex=row['uid'], version=4),
data=self._uidData[uuid.UUID(hex=row['data'], version=4)])
else:
error_str = "Trying to get an non existing point with uid: {}"
raise KeyError(error_str.format(uid))
def _get_edge(self, uid):
""" Returns an edge with a given uid.
Returns the edge stored in the mesh
identified by uid. If such edge do not
exists a exception is raised.
Parameters
----------
uid : UUID
uid of the desired edge.
Returns
-------
Edge
Edge identified by uid
Raises
------
Exception
If the edge identified by uid was not found
"""
if not hasattr(uid, 'hex'):
message = 'Expected type for `uid` is uuid.UUID but received {!r}'
raise TypeError(message.format(type(uid)))
for row in self._group.edges.where(
'uid == value', condvars={'value': uid.hex}):
return Edge(
points=tuple(
uuid.UUID(hex=pb, version=4)
for pb in row['points_uids'][0:row['n_points']]),
uid=uuid.UUID(hex=row['uid'], version=4),
data=self._uidData[uuid.UUID(hex=row['data'], version=4)])
else:
error_str = "Trying to get an non existing edge with uid: {}"
raise KeyError(error_str.format(uid))
def _get_face(self, uid):
""" Returns an face with a given uid.
Returns the face stored in the mesh
identified by uid. If such face do not
exists a exception is raised.
Parameters
----------
uid : UUID
uid of the desired face.
Returns
-------
Face
Face identified by uid
Raises
------
Exception
If the face identified by uid was not found
"""
if not hasattr(uid, 'hex'):
message = 'Expected type for `uid` is uuid.UUID but received {!r}'
raise TypeError(message.format(type(uid)))
for row in self._group.faces.where(
'uid == value', condvars={'value': uid.hex}):
return Face(
uid=uuid.UUID(hex=row['uid'], version=4),
points=tuple(
uuid.UUID(hex=pb, version=4)
for pb in row['points_uids'][:row['n_points']]),
data=self._uidData[uuid.UUID(hex=row['data'], version=4)])
else:
error_str = "Trying to get an non existing face with uid: {}"
raise KeyError(error_str.format(uid))
def _get_cell(self, uid):
""" Returns an cell with a given uid.
Returns the cell stored in the mesh
identified by uid . If such cell do not
exists a exception is raised.
Parameters
----------
uid : UUID
uid of the desired cell.
Returns
-------
Cell
Cell identified by uid
Raises
------
Exception
If the cell identified by uid was not found
"""
if not hasattr(uid, 'hex'):
message = 'Expected type for `uid` is uuid.UUID but received {!r}'
raise TypeError(message.format(type(uid)))
for row in self._group.cells.where(
'uid == value', condvars={'value': uid.hex}):
return Cell(
points=tuple(
uuid.UUID(hex=pb, version=4)
for pb in row['points_uids'][0:row['n_points']]),
uid=uuid.UUID(hex=row['uid'], version=4),
data=self._uidData[uuid.UUID(hex=row['data'], version=4)])
else:
error_str = "Trying to get an non existing cell with id: {}"
raise KeyError(error_str.format(uid))
def _add_points(self, points):
""" Adds a new set of points to the mesh container.
Parameters
----------
points : iterable of Point
Points to be added to the mesh container
Raises
------
KeyError
If other point with the same uid was already
in the mesh
"""
rpoints = []
for point in points:
if point.uid is None:
point.uid = self._generate_uid()
for row in self._group.points.where(
'uid == value', condvars={'value': point.uid.hex}):
raise ValueError(err_add.format('point', point.uid))
row = self._group.points.row
row['uid'] = point.uid.hex
row['data'] = self._uidData.append(point.data).hex
row['coordinates'] = point.coordinates
row.append()
rpoints.append(point.uid)
self._group.points.flush()
return rpoints
def _add_edges(self, edges):
""" Adds a new set of edges to the mesh container.
Parameters
----------
edges : iterable of Edge
Edges to be added to the mesh container
Raises
------
KeyError
If other edge with the same uid was already
in the mesh
"""
redges = []
for edge in edges:
if edge.uid is None:
edge.uid = self._generate_uid()
else:
for row in self._group.edges.where(
'uid == value', condvars={'value': edge.uid.hex}):
raise ValueError(err_add.format('edge', edge.uid))
n = len(edge.points)
row = self._group.edges.row
row['uid'] = edge.uid.hex
row['data'] = self._uidData.append(edge.data).hex
row['n_points'] = n
row['points_uids'] = [puid.hex for puid in
edge.points] + [''] * (MAX_POINTS_IN_EDGE-n)
row.append()
redges.append(edge.uid)
self._group.edges.flush()
return redges
def _add_faces(self, faces):
""" Adds a new set of faces to the mesh container.
Parameters
----------
faces : iterable of Face
Faces to be added to the mesh container
Raises
------
KeyError
If other face with the same uid was already
in the mesh
"""
rfaces = []
for face in faces:
if face.uid is None:
face.uid = self._generate_uid()
else:
for row in self._group.faces.where(
'uid == value', condvars={'value': face.uid.hex}):
raise ValueError(err_add.format('face', face.uid))
n = len(face.points)
row = self._group.faces.row
row['uid'] = face.uid.hex
row['data'] = self._uidData.append(face.data).hex
row['n_points'] = n
row['points_uids'] = [puid.hex for puid in
face.points] + [''] * (MAX_POINTS_IN_FACE-n)
row.append()
rfaces.append(face.uid)
self._group.faces.flush()
return rfaces
def _add_cells(self, cells):
""" Adds a new set of cells to the mesh container.
Parameters
----------
cells : iterable of Cell
Cells to be added to the mesh container
Raises
------
KeyError
If other cell with the same uid was already
in the mesh
"""
rcells = []
for cell in cells:
if cell.uid is None:
cell.uid = self._generate_uid()
else:
for row in self._group.cells.where(
'uid == value', condvars={'value': cell.uid.hex}):
raise ValueError(err_add.format('cell', cell.uid))
n = len(cell.points)
row = self._group.cells.row
row['uid'] = cell.uid.hex
row['data'] = self._uidData.append(cell.data).hex
row['n_points'] = n
row['points_uids'] = [puid.hex for puid in
cell.points] + [''] * (MAX_POINTS_IN_CELL-n)
row.append()
rcells.append(cell.uid)
self._group.cells.flush()
return rcells
def _update_points(self, points):
""" Updates the information of a point.
Gets the mesh points identified by the same
uids as the ones provided points and updates their information.
Parameters
----------
points : iterable of Point
Points to be updated
Raises
------
KeyError
If any point was not found in the mesh container.
"""
for point in points:
for row in self._group.points.where(
'uid == value', condvars={'value': point.uid.hex}):
row['coordinates'] = list(point.coordinates)
self._uidData[
uuid.UUID(hex=row['data'], version=4)
] = point.data
row.update()
row._flush_mod_rows()
break
else:
raise ValueError(err_upd.format('point', point.uid))
def _update_edges(self, edges):
""" Updates the information of an edge.
Gets the mesh edges identified by the same
uids as the ones provided edges and updates their information.
Parameters
----------
edges : iterable of Edge
Edges to be updated.
Raises
------
KeyError
If any edge was not found in the mesh container.
"""
for edge in edges:
for row in self._group.edges.where(
'uid == value', condvars={'value': edge.uid.hex}):
n = len(edge.points)
row['n_points'] = n
row['points_uids'] = [
puid.hex for puid in edge.points
] + [0] * (MAX_POINTS_IN_EDGE-n)
self._uidData[
uuid.UUID(hex=row['data'], version=4)
] = edge.data
row.update()
row._flush_mod_rows()
break
else:
raise ValueError(err_upd.format('edge', edge.uid))
def _update_faces(self, faces):
""" Updates the information of a face.
Gets the mesh faces identified by the same
uids as the ones provided in faces and updates their information.
Parameters
----------
faces : iterable of Face
Faces to be updated.
Raises
------
KeyError
If any face was not found in the mesh container.
"""
for face in faces:
for row in self._group.faces.where(
'uid == value', condvars={'value': face.uid.hex}):
n = len(face.points)
row['n_points'] = n
row['points_uids'] = [
puid.hex for puid in face.points
] + [0] * (MAX_POINTS_IN_FACE-n)
self._uidData[
uuid.UUID(hex=row['data'], version=4)
] = face.data
row.update()
row._flush_mod_rows()
break
else:
raise ValueError(err_upd.format('face', face.uid))
def _update_cells(self, cells):
""" Updates the information of every cell in cells.
Gets the mesh cells identified by the same
uids as the ones provided in cells and updates their information.
Parameters
----------
cellss : iterable of Cell
Cells to be updated.
Raises
------
KeyError
If any cell was not found in the mesh container.
"""
for cell in cells:
for row in self._group.cells.where(
'uid == value', condvars={'value': cell.uid.hex}):
n = len(cell.points)
row['n_points'] = n
row['points_uids'] = [
puid.hex for puid in cell.points
] + [0] * (MAX_POINTS_IN_CELL-n)
self._uidData[
uuid.UUID(hex=row['data'], version=4)
] = cell.data
row.update()
row._flush_mod_rows()
break
else:
raise ValueError(err_upd.format('cell', cell.uid))
def _iter_points(self, uids=None):
""" Returns an iterator over points.
Parameters
----------
uids : iterable of uuid.UUID or None
When the uids are provided, then the points are returned in
the same order the uids are returned by the iterable. If uids is
None, then all points are returned by the iterable and there
is no restriction on the order that they are returned.
Returns
-------
iter
Iterator over the points
"""
if uids is None:
for row in self._group.points:
yield Point(
tuple(row['coordinates']),
uuid.UUID(hex=row['uid'], version=4),
self._uidData[uuid.UUID(hex=row['data'], version=4)]
)
else:
for uid in uids:
yield self._get_point(uid)
def _iter_edges(self, uids=None):
""" Returns an iterator over edges.
Parameters
----------
uids : iterable of uuid.UUID or None
When the uids are provided, then the edges are returned in the
same order the uids are returned by the iterable. If uids is None,
then all edges are returned by the iterable and there is no
restriction on the order that they are returned.
Returns
-------
iter
Iterator over the selected edges
"""
if uids is None:
for row in self._group.edges:
yield Edge(
list(uuid.UUID(hex=pb, version=4) for pb in
row['points_uids'][0:row['n_points']]),
uuid.UUID(hex=row['uid'], version=4),
self._uidData[uuid.UUID(hex=row['data'], version=4)]
)
else:
for uid in uids:
yield self._get_edge(uid)
def _iter_faces(self, uids=None):
""" Returns an iterator over faces.
Parameters
----------
uids : iterable of uuid.UUID or None
When the uids are provided, then the faces are returned in the
same order the uids are returned by the iterable. If uids is None,
then all faces are returned by the iterable and there is no
restriction on the order that they are returned.
Returns
-------
iter
Iterator over the faces
"""
if uids is None:
for row in self._group.faces:
yield Face(
list(uuid.UUID(hex=pb, version=4) for pb in
row['points_uids'][0:row['n_points']]),
uuid.UUID(hex=row['uid'], version=4),
self._uidData[uuid.UUID(hex=row['data'], version=4)]
)
else:
for uid in uids:
yield self._get_face(uid)
def _iter_cells(self, uids=None):
""" Returns an iterator over cells.
Parameters
----------
uids : iterable of uuid.UUID or None
When the uids are provided, then the cells are returned in the same
order the uids are returned by the iterable. If uids is None, then
all cells are returned by the iterable and there is no restriction
on the order that they are returned.
Returns
-------
iter
Iterator over the selected cells
"""
if uids is None:
for row in self._group.cells:
yield Cell(
list(uuid.UUID(hex=pb, version=4) for pb in
row['points_uids'][0:row['n_points']]),
uuid.UUID(hex=row['uid'], version=4),
self._uidData[uuid.UUID(hex=row['data'], version=4)]
)
else:
for uid in uids:
yield self._get_cell(uid)
def _has_points(self):
""" Check if the mesh container has edges
Returns
-------
bool
True of there are edges inside the mesh,
False otherwise
"""
return self._group.points.nrows != 0
def _has_edges(self):
""" Check if the mesh container has edges
Returns
-------
bool
True of there are edges inside the mesh,
False otherwise
"""
return self._group.edges.nrows != 0
def _has_faces(self):
""" Check if the mesh container has faces
Returns
-------
bool
True of there are faces inside the mesh,
False otherwise
"""
return self._group.faces.nrows != 0
def _has_cells(self):
""" Check if the mesh container has cells
Returns
-------
bool
True of there are cells inside the mesh,
False otherwise
"""
return self._group.cells.nrows != 0
def _generate_uid(self):
""" Provides and uid for the object
Provides an uid as defined in the standard RFC 4122
"""
return uuid.uuid4()
def _create_points_table(self):
""" Generates the table to store points """
self._file.create_table(
self._group, "points", _PointDescriptor)
def _create_edges_table(self):
""" Generates the table to store edges """
self._file.create_table(
self._group, "edges", _EdgeDescriptor)
def _create_faces_table(self):
""" Generates the table to store faces """
self._file.create_table(
self._group, "faces", _FaceDescriptor)
def _create_cells_table(self):
""" Generates the table to store cells """
self._file.create_table(
self._group, "cells", _CellDescriptor)