Skip to content

Commit e13bc89

Browse files
authored
add save to VTK (#1004)
# Description Please include a summary of the changes. Relates to <issue> # Checklist - [ ] My code uses type hinting for function and method arguments and return values. - [ ] I have created tests which cover my code. - [ ] The test code either 1. demonstrates at least one valuable use case (e.g. integration tests) or 2. verifies that outputs are as expected for given inputs (e.g. unit tests). - [ ] New tests pass locally with my changes.
2 parents 901dbed + d2952d3 commit e13bc89

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

examples/examples/geometries/a01_horizontal_stratigraphic.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,88 @@
4747
# Plot the result of the model in the x and y direction with data and without boundaries
4848
gpv.plot_2d(geo_data, direction=['x'], show_data=True, show_boundaries=False)
4949
gpv.plot_2d(geo_data, direction=['y'], show_data=True, show_boundaries=False)
50+
51+
# %%
52+
# Export to VTK model
53+
p = gpv.plot_3d(geo_data, show_data=True, show_results=True, show_boundaries=True,image=True)
54+
p.surface_poly['rock1'].save('rock1.vtk') # Save the vtk file for formation 1
55+
p.surface_poly['rock2'].save('rock2.vtk') # Save the vtk file for formation 2
56+
p.orientations_mesh.save('orientations.vtk') # Save the vtk file for the orientations
57+
p.surface_points_mesh.save('surface_points.vtk') # Save the vtk file for the surface points
58+
box = p.regular_grid_actor.GetMapper().GetInput() # Get the vtk file for the regular grid
59+
box.save('box.vtk')
60+
61+
# %%
62+
import pyvista as pv
63+
pv.read('rock1.vtk').plot(show_edges=False)
64+
pv.read('rock2.vtk').plot(show_edges=False)
65+
pv.read('orientations.vtk').plot(show_edges=False)
66+
pv.read('surface_points.vtk').plot(show_edges=False)
67+
pv.read('box.vtk').plot(show_edges=False)
68+
69+
# %%
70+
# Export vertices of mesh
71+
from builtins import range
72+
import vtk
73+
import pandas as pd
74+
def generate_normals(polydata):
75+
normal_generator = vtk.vtkPolyDataNormals()
76+
normal_generator.SetInputData(polydata)
77+
normal_generator.ComputePointNormalsOn()
78+
normal_generator.ComputeCellNormalsOff()
79+
normal_generator.Update()
80+
return normal_generator.GetOutput()
81+
82+
def get_vertices_and_normals(mesh):
83+
84+
surface_mesh = mesh.extract_surface()
85+
polydata = surface_mesh
86+
87+
# Generate normals if not present
88+
polydata_with_normals = generate_normals(polydata)
89+
90+
# Get points (vertices)
91+
points = polydata_with_normals.GetPoints()
92+
vertices = []
93+
for i in range(points.GetNumberOfPoints()):
94+
vertices.append(points.GetPoint(i))
95+
96+
# Get normals
97+
normals_array = polydata_with_normals.GetPointData().GetNormals()
98+
normals = []
99+
for i in range(normals_array.GetNumberOfTuples()):
100+
normals.append(normals_array.GetTuple(i))
101+
102+
return vertices, normals
103+
104+
def save_to_excel(vertices, normals, vertices_file, normals_file):
105+
# Create DataFrames
106+
vertices_df = pd.DataFrame(vertices, columns=['X', 'Y', 'Z'])
107+
normals_df = pd.DataFrame(normals, columns=['x', 'y', 'z'])
108+
109+
# Save to Excel files
110+
vertices_df.to_excel(vertices_file, index=False)
111+
normals_df.to_excel
112+
mesh = p.surface_poly['rock1']
113+
vertices, normals = get_vertices_and_normals(mesh)
114+
vertices_df = pd.DataFrame(vertices, columns=['X', 'Y', 'Z'])
115+
normals_df = pd.DataFrame(normals, columns=['x', 'y', 'z'])
116+
# Save to Excel filesthe
117+
vertices_file = "rock1_vertices.xlsx"
118+
normals_file = "rock1_norms.xlsx"
119+
save_to_excel(vertices, normals, vertices_file, normals_file)
120+
pd.read_excel(vertices_file)
121+
pd.read_excel(normals_file)
122+
123+
# %%
124+
# Convert the DataFrame to an XYZ file
125+
def dataframe_to_xyz(df, filename):
126+
with open(filename, 'w') as f:
127+
for index, row in df.iterrows():
128+
f.write(f"{row['X']} {row['Y']} {row['Z']}\n")
129+
130+
# Specify the filename
131+
filename = "output.xyz"
132+
133+
# Call the function to write the DataFrame to an XYZ file
134+
dataframe_to_xyz(vertices_df, filename)

0 commit comments

Comments
 (0)