示例#1
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    edge_filename = argv[0]
    mesh_filename = argv[1]
    mesh_size = float(argv[2])

    cdt = CDT()
    with open(edge_filename, 'rt') as edge_file:
        for line in edge_file:
            ax, ay, bx, by = map(float, line.split())
            a = cdt.insert(Point(ax, ay))
            b = cdt.insert(Point(bx, by))
            cdt.insert_constraint(a, b)

    make_conforming_delaunay(cdt)
    make_conforming_gabriel(cdt)

    mesher = Mesher(cdt)
    mesher.criteria = Criteria(aspect_bound=0.125, size_bound=mesh_size)
    mesher.refine_mesh()

    lloyd_optimize(cdt, time_limit=0.5)

    point_to_index_map = {
        vertex.point: index
        for index, vertex in enumerate(cdt.finite_vertices())
    }

    triangles = (tuple(point_to_index_map[face.vertex_handle(i).point]
                       for i in range(3)) for face in cdt.finite_faces())

    with open(mesh_filename, 'wt') as mesh_file:
        print(cdt.number_of_vertices(), file=mesh_file)
        for point in point_to_index_map:
            print(point.x, point.y, file=mesh_file)

        print(cdt.number_of_faces(), file=mesh_file)
        for triangle in triangles:
            print(' '.join(map(str, triangle)), file=mesh_file)
示例#2
0
def main():
    cdt = CDT()

    va = cdt.insert(Point(100, 269))
    vb = cdt.insert(Point(246, 269))
    vc = cdt.insert(Point(246, 223))
    vd = cdt.insert(Point(303, 223))
    ve = cdt.insert(Point(303, 298))
    vf = cdt.insert(Point(246, 298))
    vg = cdt.insert(Point(246, 338))
    vh = cdt.insert(Point(355, 338))
    vi = cdt.insert(Point(355, 519))
    vj = cdt.insert(Point(551, 519))
    vk = cdt.insert(Point(551, 445))
    vl = cdt.insert(Point(463, 445))
    vm = cdt.insert(Point(463, 377))
    vn = cdt.insert(Point(708, 377))
    vo = cdt.insert(Point(708, 229))
    vp = cdt.insert(Point(435, 229))
    vq = cdt.insert(Point(435, 100))
    vr = cdt.insert(Point(100, 100))

    cdt.insert_constraint(va, vb)
    cdt.insert_constraint(vb, vc)
    cdt.insert_constraint(vc, vd)
    cdt.insert_constraint(vd, ve)
    cdt.insert_constraint(ve, vf)
    cdt.insert_constraint(vf, vg)
    cdt.insert_constraint(vg, vh)
    cdt.insert_constraint(vh, vi)
    cdt.insert_constraint(vi, vj)
    cdt.insert_constraint(vj, vk)
    cdt.insert_constraint(vk, vl)
    cdt.insert_constraint(vl, vm)
    cdt.insert_constraint(vm, vn)
    cdt.insert_constraint(vn, vo)
    cdt.insert_constraint(vo, vp)
    cdt.insert_constraint(vp, vq)
    cdt.insert_constraint(vq, vr)
    cdt.insert_constraint(vr, va)

    vs = cdt.insert(Point(349, 236))
    vt = cdt.insert(Point(370, 236))
    vu = cdt.insert(Point(370, 192))
    vv = cdt.insert(Point(403, 192))
    vw = cdt.insert(Point(403, 158))
    vx = cdt.insert(Point(349, 158))

    cdt.insert_constraint(vs, vt)
    cdt.insert_constraint(vt, vu)
    cdt.insert_constraint(vu, vv)
    cdt.insert_constraint(vv, vw)
    cdt.insert_constraint(vw, vx)
    cdt.insert_constraint(vx, vs)

    vy = cdt.insert(Point(501, 336))
    vz = cdt.insert(Point(533, 336))
    v1 = cdt.insert(Point(519, 307))
    v2 = cdt.insert(Point(484, 307))

    cdt.insert_constraint(vy, vz)
    cdt.insert_constraint(vz, v1)
    cdt.insert_constraint(v1, v2)
    cdt.insert_constraint(v2, vy)

    print("Number of vertices:", cdt.number_of_vertices())

    mesher = Mesher(cdt)
    seeds = [
        Point(505, 325),
        Point(379, 172),
    ]
    mesher.seeds_from(seeds)

    make_conforming_delaunay(cdt)
    print("Number of vertices:", cdt.number_of_vertices())

    make_conforming_gabriel(cdt)
    print("Number of vertices:", cdt.number_of_vertices())

    mesher.criteria = Criteria(aspect_bound=0.125, size_bound=30)
    mesher.refine_mesh()
    print("Number of vertices:", cdt.number_of_vertices())

    lloyd_optimize(cdt, max_iteration_number=10)
    print("Number of vertices:", cdt.number_of_vertices())

    vertex_iterator = cdt.finite_vertices()
    print(next(vertex_iterator))
    for vertex in cdt.finite_vertices():
        print(vertex.point)

    for face in cdt.finite_faces():
        for index in range(3):
            print(index, face.vertex_handle(index).point)