Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generate_line_elements_from_ids method #28

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions VTUinterface/vtuIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,53 @@ def add_integration_point_field(self, field, fieldname, ofilename, writefile=Tru
if writefile is True:
self.write(ofilename, datamode=datamode)

def generate_line_elements_from_ids(self, list_of_tuples, filename, datamode=None):
"""
Generates a file containing line elements
from point ids.

Parameters
----------
list_of_tuples : `list`
filename : `str`
datamode : `str`
"""
points = vtk.vtkPoints()
pointids = []
lines = []
cells = []
for entry in list_of_tuples:
pt0 = entry[0]
pt1 = entry[1]
for pt in (pt0, pt1):
if not pt in pointids:
points.InsertNextPoint(self.points[pt][0], self.points[pt][1], self.points[pt][2])
pointids.append(pt)
lines.append(vtk.vtkLine())
lines[-1].GetPointIds().SetId(0, pointids.index(pt0))
lines[-1].GetPointIds().SetId(1, pointids.index(pt1))
cells.append(vtk.vtkCellArray())
cells[-1].InsertNextCell(lines[-1])
unstructuredGrid = vtk.vtkUnstructuredGrid()
unstructuredGrid.SetPoints(points)
for line in lines:
unstructuredGrid.InsertNextCell(line.GetCellType(), line.GetPointIds())
field_vtk = numpy_to_vtk(pointids, array_type=vtk.VTK_UNSIGNED_LONG)
pdata = unstructuredGrid.GetPointData()
r = pdata.AddArray(field_vtk)
pdata.GetArray(r).SetName("bulk_node_ids")

writer = vtk.vtkXMLUnstructuredGridWriter()
writer.SetFileName(filename)
writer.SetInputData(unstructuredGrid)
if datamode == "binary":
writer.SetDataModeToBinary()
elif datamode == "ascii":
writer.SetDataModeToAscii()
writer.Write()



def write(self, filename, datamode=None):
"""
Write data as file "filename".
Expand Down
Loading