11import json
22import logging
3+ from typing import List
34
45import numpy as np
56import pandas as pd
1213PLOT_SUBSURFACE_OBJECT = False
1314
1415
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
1835 unstructured_data_meshes : UnstructuredData = _meshes_to_unstruct (meshes , logger )
1936 if PLOT_SUBSURFACE_OBJECT :
2037 _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
2941 unstructured_data_volume = subsurface .UnstructuredData .from_array (
3042 vertex = solutions .octrees_output [0 ].grid_centers .values ,
3143 cells = "points" ,
3244 cells_attr = pd .DataFrame (
3345 data = solutions .octrees_output [0 ].last_output_center .ids_block ,
3446 columns = ['id' ]
35- ) # TODO: We have to create an array with the shape of simplex array with the id of each simplex
47+ )
3648 )
49+ body_volume = unstructured_data_volume .to_binary ()
3750
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
4652 body = body_meshes + body_volume
4753 global_header = json .dumps ({"mesh_size" : len (body_meshes ), "octree_size" : len (body_volume )})
4854 global_header_bytes = global_header .encode ('utf-8' )
4955 global_header_length = len (global_header_bytes )
5056 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
5359
5460
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+ """
5775 n_meshes = len (meshes )
5876 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 } " )
6078
79+ # Prepare the vertex array
6180 vertex_array = np .concatenate ([meshes [i ].vertices for i in range (n_meshes )])
81+
82+ # Check for edge uniqueness (debugging)
6283 simplex_array = np .concatenate ([meshes [i ].edges for i in range (n_meshes )])
6384 unc , count = np .unique (simplex_array , axis = 0 , return_counts = True )
6485 logger .debug (f"edges shape { simplex_array .shape } " )
6586
66- # * Prepare the simplex array
87+ # Prepare the simplex array with proper indexing
6788 simplex_array = meshes [0 ].edges
68- for i in range (1 ,n_meshes ):
89+ for i in range (1 , n_meshes ):
6990 adder = np .max (meshes [i - 1 ].edges ) + 1
70- logger .debug ("triangle counts adder:" + str ( adder ) )
91+ logger .debug (f "triangle counts adder: { adder } " )
7192 add_mesh = meshes [i ].edges + adder
7293 simplex_array = np .append (simplex_array , add_mesh , axis = 0 )
7394
7495 logger .debug (f"edges shape { simplex_array .shape } " )
7596
76- # * Prepare the cells_attr array
97+ # Prepare the cells_attr array
7798 ids_array = np .ones (simplex_array .shape [0 ])
7899 l0 = 0
79- id = 1
100+ id_value = 1
80101 for mesh in meshes :
81102 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
84105 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 )} " )
87108
88- # * Create the unstructured data
109+ # Create the unstructured data
89110 unstructured_data = subsurface .UnstructuredData .from_array (
90111 vertex = vertex_array ,
91112 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' ])
93114 )
94115
95116 return unstructured_data
96117
97118
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+ """
99126 print (unstructured_data )
100127 obj = subsurface .TriSurf (unstructured_data )
101128 pv_unstruct = subsurface .visualization .to_pyvista_mesh (obj )
102129 print (pv_unstruct )
103- subsurface .visualization .pv_plot ([pv_unstruct ])
130+ subsurface .visualization .pv_plot ([pv_unstruct ])
0 commit comments