Skip to content

fix: input and output files are never closed - #18

Draft
andrewwhitecdw wants to merge 1 commit into
NVlabs:mainfrom
andrewwhitecdw:bugfix/mesh-to-tet-93533771
Draft

fix: input and output files are never closed#18
andrewwhitecdw wants to merge 1 commit into
NVlabs:mainfrom
andrewwhitecdw:bugfix/mesh-to-tet-93533771

Conversation

@andrewwhitecdw

@andrewwhitecdw andrewwhitecdw commented Jul 27, 2026

Copy link
Copy Markdown

Problem

fix: input and output files are never closed

Fix

Replace:

def convert_mesh_to_tet(mesh_file_path, tet_output_path):
    """Convert a .mesh file to a .tet file."""
    mesh_file = open(mesh_file_path, "r")
    tet_output = open(tet_output_path, "w")

    mesh_lines = list(mesh_file)
    mesh_lines = [line.strip('\n') for line in mesh_lines]
    vertices_start = mesh_lines.index('Vertices')
    num_vertices = mesh_lines[vertices_start + 1]

    vertices = mesh_lines[vertices_start + 2:vertices_start + 2
                          + int(num_vertices)]

    tetrahedra_start = mesh_lines.index('Tetrahedra')
    num_tetrahedra = mesh_lines[tetrahedra_start + 1]
    tetrahedra = mesh_lines[tetrahedra_start + 2:tetrahedra_start + 2
                            + int(num_tetrahedra)]

    print("# Vertices, # Tetrahedra:", num_vertices, num_tetrahedra)

    # Write to tet output
    tet_output.write("# Tetrahedral mesh generated using\n\n")
    tet_output.write("# " + num_vertices + " vertices\n")
    for v in vertices:
        tet_output.write("v " + v + "\n")
    tet_output.write("\n")
    tet_output.write("# " + num_tetrahedra + " tetrahedra\n")
    for t in tetrahedra:
        line = t.split(' 0')[0]
        line = line.split(" ")
        line = [str(int(k) - 1) for k in line]
        l_text = ' '.join(line)
        tet_output.write("t " + l_text + "\n")

with:

def convert_mesh_to_tet(mesh_file_path, tet_output_path):
    """Convert a .mesh file to a .tet file."""
    with open(mesh_file_path, "r") as mesh_file, open(tet_output_path, "w") as tet_output:

        mesh_lines = list(mesh_file)
        mesh_lines = [line.strip('\n') for line in mesh_lines]
        vertices_start = mesh_lines.index('Vertices')
        num_vertices = mesh_lines[vertices_start + 1]

        vertices = mesh_lines[vertices_start + 2:vertices_start + 2
                              + int(num_vertices)]

        tetrahedra_start = mesh_lines.index('Tetrahedra')
        num_tetrahedra = mesh_lines[tetrahedra_start + 1]
        tetrahedra = mesh_lines[tetrahedra_start + 2:tetrahedra_start + 2
                                + int(num_tetrahedra)]

        print("# Vertices, # Tetrahedra:", num_vertices, num_tetrahedra)

        # Write to tet output
        tet_output.write("# Tetrahedral mesh generated using\n\n")
        tet_output.write("# " + num_vertices + " vertices\n")
        for v in vertices:
            tet_output.write("v " + v + "\n")
        tet_output.write("\n")
        tet_output.write("# " + num_tetrahedra + " tetrahedra\n")
        for t in tetrahedra:
            line = t.split(' 0')[0]
            line = line.split(" ")
            line = [str(int(k) - 1) for k in line]
            l_text = ' '.join(line)
            tet_output.write("t " + l_text + "\n")

Files changed

  • mesh_to_tet.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant