@@ -279,10 +279,15 @@ def apply_crs_transform(
279279 return transformed
280280
281281
282- def get_crs_origin_offset (crs_obj : Any ) -> List [ float | int ] :
282+ def get_crs_origin_offset (crs_obj : Any ) -> np . ndarray :
283283 """
284- Return a list [X,Y,Z] corresponding to the crs Offset [XOffset/OriginProjectedCoordinate1, ... ] depending on the
285- crs energyml version.
284+ Return a ``(3,) float64`` numpy array ``[X, Y, Z]`` corresponding to the
285+ CRS origin offset (``XOffset``/``OriginProjectedCoordinate1``, …) depending
286+ on the energyml version.
287+
288+ Returning an ndarray instead of a plain list avoids the ``np.asarray()``
289+ call in callers such as :func:`mesh_numpy.crs_displacement_np`.
290+
286291 :param crs_obj:
287292 :return:
288293 """
@@ -298,17 +303,18 @@ def get_crs_origin_offset(crs_obj: Any) -> List[float | int]:
298303 if tmp_offset_z is None :
299304 tmp_offset_z = get_object_attribute_rgx (crs_obj , "OriginProjectedCoordinate3" )
300305
301- crs_point_offset = [0.0 , 0.0 , 0.0 ]
302306 try :
303- crs_point_offset = [
304- float (tmp_offset_x ) if tmp_offset_x is not None else 0.0 ,
305- float (tmp_offset_y ) if tmp_offset_y is not None else 0.0 ,
306- float (tmp_offset_z ) if tmp_offset_z is not None else 0.0 ,
307- ]
307+ return np .array (
308+ [
309+ float (tmp_offset_x ) if tmp_offset_x is not None else 0.0 ,
310+ float (tmp_offset_y ) if tmp_offset_y is not None else 0.0 ,
311+ float (tmp_offset_z ) if tmp_offset_z is not None else 0.0 ,
312+ ],
313+ dtype = np .float64 ,
314+ )
308315 except Exception as e :
309316 logging .info (f"ERR reading crs offset { e } " )
310-
311- return crs_point_offset
317+ return np .zeros (3 , dtype = np .float64 )
312318
313319
314320def get_datum_information (
@@ -1037,28 +1043,33 @@ def read_constant_array(
10371043 path_in_root : Optional [str ] = None ,
10381044 workspace : Optional [EnergymlStorageInterface ] = None ,
10391045 sub_indices : Optional [Union [List [int ], np .ndarray ]] = None ,
1040- ) -> List [Any ]:
1046+ ) -> Union [ np . ndarray , List [Any ] ]:
10411047 """
1042- Read a constant array ( BooleanConstantArray, DoubleConstantArray, FloatingPointConstantArray, IntegerConstantArray ...)
1048+ Read a constant array (BooleanConstantArray, DoubleConstantArray,
1049+ FloatingPointConstantArray, IntegerConstantArray …).
1050+
1051+ For numeric (int / float / bool) values a ``numpy.ndarray`` is returned
1052+ via :func:`numpy.full`, avoiding a Python-list allocation. String values
1053+ fall back to a plain list because numpy object arrays add no benefit.
1054+
10431055 :param energyml_array:
10441056 :param root_obj:
10451057 :param path_in_root:
10461058 :param workspace:
10471059 :param sub_indices:
10481060 :return:
10491061 """
1050- # logging.debug(f"Reading constant array\n\t{energyml_array}")
1051-
10521062 value = get_object_attribute_no_verif (energyml_array , "value" )
10531063 count = (
10541064 len (sub_indices )
10551065 if sub_indices is not None and len (sub_indices ) > 0
10561066 else get_object_attribute_no_verif (energyml_array , "count" )
10571067 )
10581068
1059- # logging.debug(f"\tValue : {[value for i in range(0, count)]}")
1060-
1061- return [value ] * count
1069+ if isinstance (value , (int , float , bool , np .integer , np .floating )):
1070+ return np .full (int (count ), value )
1071+ # Non-numeric (e.g. string) — keep as Python list.
1072+ return [value ] * int (count )
10621073
10631074
10641075def read_xml_array (
@@ -1402,44 +1413,45 @@ def read_point3d_lattice_array(
14021413 # Add slowest offsets where i > 0
14031414 result_arr [1 :, :, :] += slowest_cumsum [:- 1 , np .newaxis , :]
14041415
1405- # Flatten to list of points
1406- result = result_arr .reshape (- 1 , 3 ). tolist ()
1416+ # Return the (N, 3) float64 numpy array directly — no .tolist().
1417+ result = result_arr .reshape (- 1 , 3 )
14071418
14081419 except (ValueError , TypeError ) as e :
1409- # Fallback to original implementation if NumPy conversion fails
1420+ # Fallback to original implementation if NumPy conversion fails.
14101421 logging .warning (f"NumPy vectorization failed ({ e } ), falling back to iterative approach" )
1422+ fallback : List = []
14111423 for i in range (slowest_size ):
14121424 for j in range (fastest_size ):
14131425 previous_value = origin
14141426
14151427 if j > 0 :
14161428 if i > 0 :
14171429 line_idx = i * fastest_size
1418- previous_value = result [line_idx + j - 1 ]
1430+ previous_value = fallback [line_idx + j - 1 ]
14191431 else :
1420- previous_value = result [j - 1 ]
1432+ previous_value = fallback [j - 1 ]
14211433 if zincreasing_downward :
1422- result .append (sum_lists (previous_value , slowest_table [i - 1 ]))
1434+ fallback .append (sum_lists (previous_value , slowest_table [i - 1 ]))
14231435 else :
1424- result .append (sum_lists (previous_value , fastest_table [j - 1 ]))
1436+ fallback .append (sum_lists (previous_value , fastest_table [j - 1 ]))
14251437 else :
14261438 if i > 0 :
14271439 prev_line_idx = (i - 1 ) * fastest_size
1428- previous_value = result [prev_line_idx ]
1440+ previous_value = fallback [prev_line_idx ]
14291441 if zincreasing_downward :
1430- result .append (sum_lists (previous_value , fastest_table [j - 1 ]))
1442+ fallback .append (sum_lists (previous_value , fastest_table [j - 1 ]))
14311443 else :
1432- result .append (sum_lists (previous_value , slowest_table [i - 1 ]))
1444+ fallback .append (sum_lists (previous_value , slowest_table [i - 1 ]))
14331445 else :
1434- result .append (previous_value )
1446+ fallback .append (previous_value )
1447+ # Convert fallback list to ndarray to keep the return type consistent.
1448+ result = np .array (fallback , dtype = np .float64 ).reshape (- 1 , 3 )
14351449 else :
14361450 raise Exception (f"{ type (energyml_array )} read with an offset of length { len (offset )} is not supported" )
14371451
14381452 if sub_indices is not None and len (sub_indices ) > 0 :
1439- if isinstance (result , np .ndarray ):
1440- result = result [sub_indices ].tolist ()
1441- else :
1442- result = [result [idx ] for idx in sub_indices ]
1453+ # result is always an ndarray here; index directly without .tolist().
1454+ result = result [np .asarray (sub_indices , dtype = np .int64 )]
14431455
14441456 return result
14451457
0 commit comments