-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruncateVTPtrace.py
69 lines (51 loc) · 2.2 KB
/
truncateVTPtrace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#truncates a vtk field line trace so that it is shorter.
#saves truncated version as another vtk
import vtk
# Function to read the VTK file
def read_vtk_file(filename):
reader = vtk.vtkPolyDataReader() # Use vtkPolyDataReader for .vtk files
reader.SetFileName(filename)
reader.Update()
data = reader.GetOutput()
# Check if data is loaded properly
if data is None or data.GetNumberOfPoints() == 0:
raise ValueError(f"Error reading VTK file: {filename}")
print(f"Number of points: {data.GetNumberOfPoints()}")
print(f"Number of cells: {data.GetNumberOfCells()}")
return data
# Function to write the truncated VTK file
def write_vtk_file(data, output_filename):
writer = vtk.vtkPolyDataWriter() # Use vtkPolyDataWriter for .vtk files
writer.SetFileName(output_filename)
writer.SetInputData(data)
writer.Write()
# Function to truncate the field line trace
def truncate_trace(data, max_index):
# Create a new vtkPoints and vtkCellArray for the truncated trace
points = vtk.vtkPoints()
cells = vtk.vtkCellArray()
# Truncate the points and lines
for i in range(max_index + 1):
points.InsertNextPoint(data.GetPoints().GetPoint(i))
for cell_id in range(data.GetNumberOfCells()):
cell = data.GetCell(cell_id)
cell_points = cell.GetPointIds()
# Only include cells that are within the truncated range
if cell_points.GetId(0) <= max_index and cell_points.GetId(1) <= max_index:
cells.InsertNextCell(cell)
# Create a new PolyData object with the truncated points and lines
truncated_data = vtk.vtkPolyData()
truncated_data.SetPoints(points)
truncated_data.SetLines(cells)
return truncated_data
# Main script
path = "/home/tlooby/Downloads/"
input_filename = path + "Field_trace_pt002.vtk" # Replace with your file path
output_filename = path + "output_trace_truncated.vtk" # Output file path
# Read the input file
data = read_vtk_file(input_filename)
# Truncate the trace at index 2939
truncated_data = truncate_trace(data, 2939)
# Write the truncated data to a new file
write_vtk_file(truncated_data, output_filename)
print(f"Truncated trace saved to {output_filename}")