1
1
import json
2
2
import logging
3
+ from typing import List
3
4
4
5
import numpy as np
5
6
import pandas as pd
12
13
PLOT_SUBSURFACE_OBJECT = False
13
14
14
15
15
- def process_output (meshes : list [DualContouringMesh ], n_stack : int , solutions : Solutions , logger : logging .Logger ) \
16
- -> bytes :
17
- # * serialize meshes
16
+ def process_output (
17
+ meshes : List [DualContouringMesh ],
18
+ n_stack : int ,
19
+ solutions : Solutions ,
20
+ logger : logging .Logger
21
+ ) -> bytes :
22
+ """
23
+ Process model outputs into binary format for transmission.
24
+
25
+ Args:
26
+ meshes: List of dual contouring meshes
27
+ n_stack: Number of stacks in the model
28
+ solutions: Model solutions containing octree data
29
+ logger: Logger instance
30
+
31
+ Returns:
32
+ Binary data containing serialized meshes and octrees
33
+ """
34
+ # Serialize meshes
18
35
unstructured_data_meshes : UnstructuredData = _meshes_to_unstruct (meshes , logger )
19
36
if PLOT_SUBSURFACE_OBJECT :
20
37
_plot_subsurface_object (unstructured_data_meshes )
21
- body_meshes , header_meshes = unstructured_data_meshes .to_binary ()
22
- header_json = json .dumps (header_meshes )
23
- header_json_bytes = header_json .encode ('utf-8' )
24
- header_json_length = len (header_json_bytes )
25
- header_json_length_bytes = header_json_length .to_bytes (4 , byteorder = 'little' )
26
- body_meshes = header_json_length_bytes + header_json_bytes + body_meshes
27
-
28
- # * serialize octrees
38
+ body_meshes = unstructured_data_meshes .to_binary ()
39
+
40
+ # Serialize octrees
29
41
unstructured_data_volume = subsurface .UnstructuredData .from_array (
30
42
vertex = solutions .octrees_output [0 ].grid_centers .values ,
31
43
cells = "points" ,
32
44
cells_attr = pd .DataFrame (
33
45
data = solutions .octrees_output [0 ].last_output_center .ids_block ,
34
46
columns = ['id' ]
35
- ) # TODO: We have to create an array with the shape of simplex array with the id of each simplex
47
+ )
36
48
)
49
+ body_volume = unstructured_data_volume .to_binary ()
37
50
38
- body_volume , header_volume = unstructured_data_volume .to_binary ()
39
- header_json = json .dumps (header_volume )
40
- header_json_bytes = header_json .encode ('utf-8' )
41
- header_json_length = len (header_json_bytes )
42
- header_json_length_bytes = header_json_length .to_bytes (4 , byteorder = 'little' )
43
- body_volume = header_json_length_bytes + header_json_bytes + body_volume
44
-
45
- # * serialize global header
51
+ # Serialize global header and combine data
46
52
body = body_meshes + body_volume
47
53
global_header = json .dumps ({"mesh_size" : len (body_meshes ), "octree_size" : len (body_volume )})
48
54
global_header_bytes = global_header .encode ('utf-8' )
49
55
global_header_length = len (global_header_bytes )
50
56
global_header_length_bytes = global_header_length .to_bytes (4 , byteorder = 'little' )
51
- body = global_header_length_bytes + global_header_bytes + body
52
- return body
57
+
58
+ return global_header_length_bytes + global_header_bytes + body
53
59
54
60
55
- def _meshes_to_unstruct (meshes : list [DualContouringMesh ], logger : logging .Logger ) -> UnstructuredData :
56
- # ? I added this function to the Solutions class
61
+ def _meshes_to_unstruct (
62
+ meshes : List [DualContouringMesh ],
63
+ logger : logging .Logger
64
+ ) -> UnstructuredData :
65
+ """
66
+ Convert a list of dual contouring meshes to an unstructured data format.
67
+
68
+ Args:
69
+ meshes: List of dual contouring meshes
70
+ logger: Logger instance
71
+
72
+ Returns:
73
+ Unstructured data representation of the meshes
74
+ """
57
75
n_meshes = len (meshes )
58
76
logger .debug (f"Number of meshes: { n_meshes } " )
59
- logger .debug ("Mesh1TriShape" + str ( meshes [0 ].edges .shape ) )
77
+ logger .debug (f "Mesh1TriShape: { meshes [0 ].edges .shape } " )
60
78
79
+ # Prepare the vertex array
61
80
vertex_array = np .concatenate ([meshes [i ].vertices for i in range (n_meshes )])
81
+
82
+ # Check for edge uniqueness (debugging)
62
83
simplex_array = np .concatenate ([meshes [i ].edges for i in range (n_meshes )])
63
84
unc , count = np .unique (simplex_array , axis = 0 , return_counts = True )
64
85
logger .debug (f"edges shape { simplex_array .shape } " )
65
86
66
- # * Prepare the simplex array
87
+ # Prepare the simplex array with proper indexing
67
88
simplex_array = meshes [0 ].edges
68
- for i in range (1 ,n_meshes ):
89
+ for i in range (1 , n_meshes ):
69
90
adder = np .max (meshes [i - 1 ].edges ) + 1
70
- logger .debug ("triangle counts adder:" + str ( adder ) )
91
+ logger .debug (f "triangle counts adder: { adder } " )
71
92
add_mesh = meshes [i ].edges + adder
72
93
simplex_array = np .append (simplex_array , add_mesh , axis = 0 )
73
94
74
95
logger .debug (f"edges shape { simplex_array .shape } " )
75
96
76
- # * Prepare the cells_attr array
97
+ # Prepare the cells_attr array
77
98
ids_array = np .ones (simplex_array .shape [0 ])
78
99
l0 = 0
79
- id = 1
100
+ id_value = 1
80
101
for mesh in meshes :
81
102
l1 = l0 + mesh .edges .shape [0 ]
82
- logger .debug (f"l0 { l0 } l1 { l1 } id { id } " )
83
- ids_array [l0 :l1 ] = id
103
+ logger .debug (f"l0 { l0 } l1 { l1 } id { id_value } " )
104
+ ids_array [l0 :l1 ] = id_value
84
105
l0 = l1
85
- id += 1
86
- logger .debug ("ids_array count" + str ( np .unique (ids_array )) )
106
+ id_value += 1
107
+ logger .debug (f "ids_array count: { np .unique (ids_array )} " )
87
108
88
- # * Create the unstructured data
109
+ # Create the unstructured data
89
110
unstructured_data = subsurface .UnstructuredData .from_array (
90
111
vertex = vertex_array ,
91
112
cells = simplex_array ,
92
- cells_attr = pd .DataFrame (ids_array , columns = ['id' ]) # TODO: We have to create an array with the shape of simplex array with the id of each simplex
113
+ cells_attr = pd .DataFrame (ids_array , columns = ['id' ])
93
114
)
94
115
95
116
return unstructured_data
96
117
97
118
98
- def _plot_subsurface_object (unstructured_data ):
119
+ def _plot_subsurface_object (unstructured_data : UnstructuredData ) -> None :
120
+ """
121
+ Visualize the unstructured data using subsurface.
122
+
123
+ Args:
124
+ unstructured_data: The unstructured data to visualize
125
+ """
99
126
print (unstructured_data )
100
127
obj = subsurface .TriSurf (unstructured_data )
101
128
pv_unstruct = subsurface .visualization .to_pyvista_mesh (obj )
102
129
print (pv_unstruct )
103
- subsurface .visualization .pv_plot ([pv_unstruct ])
130
+ subsurface .visualization .pv_plot ([pv_unstruct ])
0 commit comments