def strang_mesh(filename):
    """Read Strang generated mesh.
    """

    from math import pi
    from anuga.utilities.numerical_tools import anglediff

    fid = open(filename)
    points = []  # List of x, y coordinates
    triangles = []  # List of vertex ids as listed in the file

    for line in fid.readlines():
        fields = line.split()
        if len(fields) == 2:
            # we are reading vertex coordinates
            points.append([float(fields[0]), float(fields[1])])
        elif len(fields) == 3:
            # we are reading triangle point id's (format ae+b)
            triangles.append([
                int(float(fields[0])) - 1,
                int(float(fields[1])) - 1,
                int(float(fields[2])) - 1
            ])
        else:
            raise Excetion('wrong format in %s' % filename)

    elements = []  #Final list of elements

    for t in triangles:
        #Get vertex coordinates
        v0 = t[0]
        v1 = t[1]
        v2 = t[2]

        x0 = points[v0][0]
        y0 = points[v0][1]
        x1 = points[v1][0]
        y1 = points[v1][1]
        x2 = points[v2][0]
        y2 = points[v2][1]

        #Check that points are arranged in counter clock-wise order
        vec0 = [x1 - x0, y1 - y0]
        vec1 = [x2 - x1, y2 - y1]
        vec2 = [x0 - x2, y0 - y2]

        a0 = anglediff(vec1, vec0)
        a1 = anglediff(vec2, vec1)
        a2 = anglediff(vec0, vec2)

        if a0 < pi and a1 < pi and a2 < pi:
            elements.append([v0, v1, v2])
        else:
            elements.append([v0, v2, v1])

    return points, elements
def strang_mesh(filename):
    """Read Strang generated mesh.
    """

    from math import pi
    from anuga.utilities.numerical_tools import anglediff


    fid = open(filename)
    points = []    # List of x, y coordinates
    triangles = [] # List of vertex ids as listed in the file

    for line in fid.readlines():
        fields = line.split()
        if len(fields) == 2:
            # we are reading vertex coordinates
            points.append([float(fields[0]), float(fields[1])])
        elif len(fields) == 3:
            # we are reading triangle point id's (format ae+b)
            triangles.append([int(float(fields[0]))-1,
                              int(float(fields[1]))-1,
                              int(float(fields[2]))-1])
        else:
            raise Excetion('wrong format in %s' % filename)

    elements = [] #Final list of elements

    for t in triangles:
        #Get vertex coordinates
        v0 = t[0]
        v1 = t[1]
        v2 = t[2]

        x0 = points[v0][0]
        y0 = points[v0][1]
        x1 = points[v1][0]
        y1 = points[v1][1]
        x2 = points[v2][0]
        y2 = points[v2][1]

        #Check that points are arranged in counter clock-wise order
        vec0 = [x1-x0, y1-y0]
        vec1 = [x2-x1, y2-y1]
        vec2 = [x0-x2, y0-y2]

        a0 = anglediff(vec1, vec0)
        a1 = anglediff(vec2, vec1)
        a2 = anglediff(vec0, vec2)

        if a0 < pi and a1 < pi and a2 < pi:
            elements.append([v0, v1, v2])
        else:
            elements.append([v0, v2, v1])

    return points, elements
Exemplo n.º 3
0
def from_polyfile(name):
    """Read mesh from .poly file, an obj like file format
    listing first vertex coordinates and then connectivity
    """

    from anuga.utilities.numerical_tools import anglediff
    from math import pi
    import os.path
    root, ext = os.path.splitext(name)

    if ext == 'poly':
        filename = name
    else:
        filename = name + '.poly'


    fid = open(filename)

    points = []    #x, y
    values = []    #z
    ##vertex_values = []    #Repeated z
    triangles = [] #v0, v1, v2

    lines = fid.readlines()

    keyword = lines[0].strip()
    msg = 'First line in .poly file must contain the keyword: POINTS'
    assert keyword == 'POINTS', msg

    offending = 0
    i = 1
    while keyword == 'POINTS':
        line = lines[i].strip()
        i += 1

        if line == 'POLYS':
            keyword = line
            break

        fields = line.split(':')
        assert int(fields[0]) == i-1, 'Point indices not consecutive'

        #Split the three floats
        xyz = fields[1].split()

        x = float(xyz[0])
        y = float(xyz[1])
        z = float(xyz[2])

        points.append([x, y])
        values.append(z)


    k = i
    while keyword == 'POLYS':
        line = lines[i].strip()
        i += 1

        if line == 'END':
            keyword = line
            break


        fields = line.split(':')
        assert int(fields[0]) == i-k, 'Poly indices not consecutive'

        #Split the three indices
        vvv = fields[1].split()

        i0 = int(vvv[0])-1
        i1 = int(vvv[1])-1
        i2 = int(vvv[2])-1

        #Check for and exclude degenerate areas
        x0 = points[i0][0]
        y0 = points[i0][1]
        x1 = points[i1][0]
        y1 = points[i1][1]
        x2 = points[i2][0]
        y2 = points[i2][1]

        area = old_div(abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0)),2)
        if area > 0:

            #Ensure that points are arranged in counter clock-wise order
            v0 = [x1-x0, y1-y0]
            v1 = [x2-x1, y2-y1]
            v2 = [x0-x2, y0-y2]

            a0 = anglediff(v1, v0)
            a1 = anglediff(v2, v1)
            a2 = anglediff(v0, v2)


            if a0 < pi and a1 < pi and a2 < pi:
                #all is well
                j0 = i0
                j1 = i1
                j2 = i2
            else:
                #Swap two vertices
                j0 = i1
                j1 = i0
                j2 = i2

            triangles.append([j0, j1, j2])
            ##vertex_values.append([values[j0], values[j1], values[j2]])
        else:
            offending +=1

    log.critical('Removed %d offending triangles out of %d'
                 % (offending, len(lines)))
    return points, triangles, values
def from_polyfile(name):
    """Read mesh from .poly file, an obj like file format
    listing first vertex coordinates and then connectivity
    """

    from anuga.utilities.numerical_tools import anglediff
    from math import pi
    import os.path
    root, ext = os.path.splitext(name)

    if ext == 'poly':
        filename = name
    else:
        filename = name + '.poly'


    fid = open(filename)

    points = []    #x, y
    values = []    #z
    ##vertex_values = []    #Repeated z
    triangles = [] #v0, v1, v2

    lines = fid.readlines()

    keyword = lines[0].strip()
    msg = 'First line in .poly file must contain the keyword: POINTS'
    assert keyword == 'POINTS', msg

    offending = 0
    i = 1
    while keyword == 'POINTS':
        line = lines[i].strip()
        i += 1

        if line == 'POLYS':
            keyword = line
            break

        fields = line.split(':')
        assert int(fields[0]) == i-1, 'Point indices not consecutive'

        #Split the three floats
        xyz = fields[1].split()

        x = float(xyz[0])
        y = float(xyz[1])
        z = float(xyz[2])

        points.append([x, y])
        values.append(z)


    k = i
    while keyword == 'POLYS':
        line = lines[i].strip()
        i += 1

        if line == 'END':
            keyword = line
            break


        fields = line.split(':')
        assert int(fields[0]) == i-k, 'Poly indices not consecutive'

        #Split the three indices
        vvv = fields[1].split()

        i0 = int(vvv[0])-1
        i1 = int(vvv[1])-1
        i2 = int(vvv[2])-1

        #Check for and exclude degenerate areas
        x0 = points[i0][0]
        y0 = points[i0][1]
        x1 = points[i1][0]
        y1 = points[i1][1]
        x2 = points[i2][0]
        y2 = points[i2][1]

        area = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2
        if area > 0:

            #Ensure that points are arranged in counter clock-wise order
            v0 = [x1-x0, y1-y0]
            v1 = [x2-x1, y2-y1]
            v2 = [x0-x2, y0-y2]

            a0 = anglediff(v1, v0)
            a1 = anglediff(v2, v1)
            a2 = anglediff(v0, v2)


            if a0 < pi and a1 < pi and a2 < pi:
                #all is well
                j0 = i0
                j1 = i1
                j2 = i2
            else:
                #Swap two vertices
                j0 = i1
                j1 = i0
                j2 = i2

            triangles.append([j0, j1, j2])
            ##vertex_values.append([values[j0], values[j1], values[j2]])
        else:
            offending +=1

    log.critical('Removed %d offending triangles out of %d'
                 % (offending, len(lines)))
    return points, triangles, values