@@ -1190,6 +1190,105 @@ def test_from_vtk():
11901190 _ = td .TriangularGridDataset .from_vtk ("tests/data/gmsh_2d.vtk" )
11911191
11921192
1193+ def test_triangular_from_vtu_near_planar_large_coordinates (tmp_path ):
1194+ """Regression for large-coordinate planar slices with small normal-axis jitter."""
1195+ pytest .importorskip ("vtk" )
1196+ import vtk
1197+
1198+ import tidy3d as td
1199+
1200+ tri_grid = td .TriangularGridDataset (
1201+ normal_axis = 1 ,
1202+ normal_pos = 0.0 ,
1203+ points = td .PointDataArray (
1204+ [
1205+ [100000.0 , 0.0 ],
1206+ [101000.0 , 0.0 ],
1207+ [100000.0 , 690.0 ],
1208+ [101000.0 , 690.0 ],
1209+ ],
1210+ dims = ("index" , "axis" ),
1211+ ),
1212+ cells = td .CellDataArray ([[0 , 1 , 2 ], [1 , 2 , 3 ]], dims = ("cell_index" , "vertex_index" )),
1213+ values = td .IndexedDataArray (
1214+ [1.0 , 2.0 , 3.0 , 4.0 ], coords = {"index" : np .arange (4 )}, name = "temp"
1215+ ),
1216+ )
1217+
1218+ vtu_path = tmp_path / "near_planar_large_coords.vtu"
1219+ tri_grid .to_vtu (vtu_path )
1220+
1221+ # Inject tiny jitter in the nominally planar normal direction.
1222+ reader = vtk .vtkXMLUnstructuredGridReader ()
1223+ reader .SetFileName (str (vtu_path ))
1224+ reader .Update ()
1225+ grid = reader .GetOutput ()
1226+ points = grid .GetPoints ()
1227+ for ind in range (points .GetNumberOfPoints ()):
1228+ x , _ , z = points .GetPoint (ind )
1229+ points .SetPoint (ind , x , 6e-6 if (ind % 2 ) == 0 else - 6e-6 , z )
1230+ points .Modified ()
1231+
1232+ writer = vtk .vtkXMLUnstructuredGridWriter ()
1233+ writer .SetFileName (str (vtu_path ))
1234+ writer .SetInputData (grid )
1235+ writer .Write ()
1236+
1237+ loaded = td .TriangularGridDataset .from_vtu (vtu_path , field = "temp" )
1238+ assert loaded .normal_axis == 1
1239+ assert abs (float (loaded .normal_pos )) < 1e-4
1240+ assert loaded .points .sizes ["index" ] == 4
1241+
1242+
1243+ def test_triangular_from_vtu_far_from_origin_small_extent (tmp_path ):
1244+ """Regression: tolerance must depend on extent, not absolute position."""
1245+ pytest .importorskip ("vtk" )
1246+ import vtk
1247+
1248+ import tidy3d as td
1249+
1250+ tri_grid = td .TriangularGridDataset (
1251+ normal_axis = 1 ,
1252+ normal_pos = 0.0 ,
1253+ points = td .PointDataArray (
1254+ [
1255+ [1e9 , - 2e9 ],
1256+ [1e9 + 2.0 , - 2e9 ],
1257+ [1e9 , - 2e9 + 1.0 ],
1258+ [1e9 + 2.0 , - 2e9 + 1.0 ],
1259+ ],
1260+ dims = ("index" , "axis" ),
1261+ ),
1262+ cells = td .CellDataArray ([[0 , 1 , 2 ], [1 , 2 , 3 ]], dims = ("cell_index" , "vertex_index" )),
1263+ values = td .IndexedDataArray (
1264+ [1.0 , 2.0 , 3.0 , 4.0 ], coords = {"index" : np .arange (4 )}, name = "temp"
1265+ ),
1266+ )
1267+
1268+ vtu_path = tmp_path / "far_origin_small_extent.vtu"
1269+ tri_grid .to_vtu (vtu_path )
1270+
1271+ reader = vtk .vtkXMLUnstructuredGridReader ()
1272+ reader .SetFileName (str (vtu_path ))
1273+ reader .Update ()
1274+ grid = reader .GetOutput ()
1275+ points = grid .GetPoints ()
1276+ for ind in range (points .GetNumberOfPoints ()):
1277+ x , _ , z = points .GetPoint (ind )
1278+ points .SetPoint (ind , x , 2e-7 if (ind % 2 ) == 0 else - 2e-7 , z )
1279+ points .Modified ()
1280+
1281+ writer = vtk .vtkXMLUnstructuredGridWriter ()
1282+ writer .SetFileName (str (vtu_path ))
1283+ writer .SetInputData (grid )
1284+ writer .Write ()
1285+
1286+ loaded = td .TriangularGridDataset .from_vtu (vtu_path , field = "temp" )
1287+ assert loaded .normal_axis == 1
1288+ assert abs (float (loaded .normal_pos )) < 1e-5
1289+ assert loaded .points .sizes ["index" ] == 4
1290+
1291+
11931292def test_tetrahedral_from_vtk_obj_without_cell_types_array ():
11941293 """Regression test for VTK objects with missing ``GetCellTypesArray`` output."""
11951294 pytest .importorskip ("vtk" )
0 commit comments