Example #1
0
def triangulate_outer_polygon_with_hole_polygons(
        outerPolygonWithInnerHolePolygons):

    # TODO: this needs to be all changed, and support for holes needs to be
    # integrated
    tr = Triangulator()

    for vertex in outerPolygonWithInnerHolePolygons.outer_polygon.getVertices(
    ):
        vi = tr.addVertex(vertex[0], vertex[1])
        tr.addPolygonVertex(vi)

    for inner_hole_polygon in outerPolygonWithInnerHolePolygons.inner_hole_polygons:
        tr.beginHole()  # finish the last hole, and add to the next
        for vertex in inner_hole_polygon.getVertices():
            vi = tr.addVertex(vertex[0], vertex[1])
            tr.addHoleVertex(vi)

    # tr.beginHole()  # finish the last hole?
    tr.triangulate()

    # --- BEGIN getting the triangles vertices and indices ----

    def get_vertices_and_indices_from_p3d_triangulated_object(tr):

        # first linear array of triangle points
        triangles_indices = np.array([])
        num_triangles = tr.getNumTriangles()
        for i in range(num_triangles):
            triangle = np.array([])
            # 2d vertices
            triangle = np.append(triangle, tr.getTriangleV0(i))
            triangle = np.append(triangle, tr.getTriangleV1(i))
            triangles_indices = np.append(triangles_indices, triangle)

        # reshape it
        triangles_indices = np.reshape(triangles_indices, (-1, 2))
        triangles_indices = np.array(triangles_indices, np.int32)

        # print(tr.isLeftWinding())
        # print(tr.getNumTriangles())
        # print(tr.getNumVertices())

        # return all vertices and indices needed to create a GeomPrimitive

        vertices = tr.getVertices()
        return vertices, triangles_indices

    return get_vertices_and_indices_from_p3d_triangulated_object(tr)
Example #2
0
def create_GeomNode_Simple_Polygon_with_Hole(symbol_geometries):

    color_vec4 = Vec4(1., 1., 1., 1.)

    outerpolygon_contour_points = 0.1 * symbol_geometries[0][0]
    inner_hole_contour_points = 0.1 * symbol_geometries[0][1]


    from itertools import groupby

    inner_hole_contour_points = [k for k,g in groupby(inner_hole_contour_points.tolist())]
    # inner_hole_contour_points.append(inner_hole_contour_points[0])
    inner_hole_contour_points = np.array(inner_hole_contour_points)[:-1]
    outerpolygon_contour_points = [k for k,g in groupby(outerpolygon_contour_points.tolist())]
    # outerpolygon_contour_points.append(outerpolygon_contour_points[0])
    outerpolygon_contour_points = np.array(outerpolygon_contour_points)[:-1]

    # remove consecutive doubles in contour 

    # outerpolygon_contour_points = inner_hole_contour_points

    # outerpolygon_contour_points = (
    #     np.array([[0, 1], [-1, 0], [0, -1], [1, 0]], dtype=np.float64))

    # inner_hole_contour_points = (
    #     0.5 * np.array([[0, 1], [-1, 0], [0, -1], [1, 0]], dtype=np.float64))

    
    tr = Triangulator()

    # very simple triangular hole
    # v1 = np.array([ 
    #     np.amax(outerpolygon_contour_points[:, 0]), 
    #     np.amax(outerpolygon_contour_points[:, 1]), 
    # ]) + np.array([-0.2, -0.15]) 
    # v2 = v1 + np.array([-0.05, -0.05])
    # v3 = v1 + np.array([0.0, -0.05])
    # inner_hole_contour_points = [v1, v2, v3]

    for vertex in outerpolygon_contour_points: 
        vi = tr.addVertex(vertex[0], vertex[1])
        tr.addPolygonVertex(vi)

    tr.beginHole()
    for vertex in inner_hole_contour_points:
        vi = tr.addVertex(vertex[0], vertex[1])
        tr.addHoleVertex(vi)


    tr.triangulate()
    
    vertices = tr.getVertices()

    indices = []
    num_triangles = tr.getNumTriangles()
    for i in range(num_triangles):
        indices.append([tr.getTriangleV0(i), tr.getTriangleV1(i), tr.getTriangleV2(i)])

    # Own Geometry

    # format = GeomVertexFormat.getV3c4t2()
    format = GeomVertexFormat.getV3c4()
    vdata = GeomVertexData("colored_polygon", format, Geom.UHStatic)
    vdata.setNumRows(4)
    
    # let's also add color to each vertex
    colorWriter = GeomVertexWriter(vdata, "color")
    vertexPosWriter = GeomVertexWriter(vdata, "vertex")

    for v in vertices: 
        vertexPosWriter.addData3f(v[0], 0, v[1])
        colorWriter.addData4f(color_vec4)

    # make primitives and assign vertices to them (primitives and primitive
    # groups can be made independently from vdata, and are later assigned
    # to vdata)
    tris = GeomTriangles(Geom.UHStatic) 

    for index_triple in indices: 
        tris.addVertices(index_triple[0], index_triple[1], index_triple[2])

    tris.closePrimitive()

    # make a Geom object to hold the primitives
    geom = Geom(vdata)  # vdata contains the vertex position/color/... buffers
    geom.addPrimitive(tris)  # tris contains the index buffer

    # now put geom in a GeomNode
    geom_node = GeomNode("colored_polygon_node")
    geom_node.addGeom(geom)

    return geom_node
Example #3
0
def makeTriMesh( verts, holeVerts=[[]]):
    pointmap = (lambda x, y: (x, y, 0))
    if not hasattr(holeVerts[0], '__iter__'):
        holeVerts = [holeVerts]

    frmt = GeomVertexFormat.getV3n3cp()
    vdata = GeomVertexData('triangle', frmt, Geom.UHDynamic)

    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    color = GeomVertexWriter(vdata, 'color')

    bl = Vec4(50, 50, 50, 255)
    gr = Vec4(256/2 - 1, 256/2 - 1, 256/2 - 1, 255)

    trilator = Triangulator()
    zUp = Vec3(0, 0, 1)

    for i in verts:
        #print "verts", verts
        trilator.addPolygonVertex(trilator.addVertex(i.x, i.y))
        vertex.addData3f(pointmap(i.x, i.y))
        normal.addData3f(zUp)
        color.addData4f(bl)
    #if len(holeVerts) != 1 and holeVerts[0] != []:
    for w in holeVerts:
        trilator.beginHole()
        print "new hole"
        for j in w:
            # print(j)  # ###################### PRINT #######################
            trilator.addHoleVertex(trilator.addVertex(j.x, j.y))
            vertex.addData3f(pointmap(j.x, j.y))
            normal.addData3f(zUp)
            color.addData4f(gr)

    # try:
    trilator.triangulate()
    triVerts = trilator.getVertices()
    p = trilator.getTriangleV0(0)
    print "trilator return", p
    # except AssertionError:
    #     pass
    # TODO:re-triangulate here and change AdjacencyList to expect a list of triangles rather than call Triangulator funcs
    trilator = makeDelaunayTriangulation(trilator)


    prim = GeomTriangles(Geom.UHStatic)
    if isinstance(trilator, Triangulator):
        # HACK just to switch back and forth from the non-Delaunay to the Delaunay for school
        for n in xrange(trilator.getNumTriangles()):
            prim.addVertices(trilator.getTriangleV0(n),
                             trilator.getTriangleV1(n),
                             trilator.getTriangleV2(n))
    else:  # it's an adjacency list

        for n in xrange(len(trilator.adjLst)):
            for v in range(0, len(triVerts)):
                print "\ncurr", triVerts[v], "\nv0", Point2D(trilator.adjLst[n].tri[0].x, trilator.adjLst[n].tri[0].y),\
                    "\nv1", Point2D(trilator.adjLst[n].tri[1].x, trilator.adjLst[n].tri[1].y),\
                    "\nv2", Point2D(trilator.adjLst[n].tri[2].x, trilator.adjLst[n].tri[2].y)
                    # trilator.adjLst[n].tri[0].getXy(),\
                    # "\nv1", trilator.adjLst[n].tri[1].getXy(),\
                    # "\nv2", trilator.adjLst[n].tri[2].getXy()
                # v0 = v1 = v2 = -1
                if Point2D(trilator.adjLst[n].tri[0].x, trilator.adjLst[n].tri[0].y) == triVerts[v]:
                    v0 = v  # we need indices into the vertex pool
                    print "found v0", v0

                if Point2D(trilator.adjLst[n].tri[1].x, trilator.adjLst[n].tri[1].y) == triVerts[v]:
                    v1 = v
                    print "found v1", v1

                if Point2D(trilator.adjLst[n].tri[2].x, trilator.adjLst[n].tri[2].y) == triVerts[v]:
                    v2 = v
                    print "found v2", v2
            # if v0 == -1 or v1 == -1 or v2 == -1:
            #     print "pass", v0, v1, v2
            #     pass
            # else:
            #     print "add"

            # i = triVerts[v0]
            # vertex.addData3f(pointmap(i.x, i.y))
            # normal.addData3f(zUp)
            # color.addData4f(bl)
            # i = triVerts[v1]
            # vertex.addData3f(pointmap(i.x, i.y))
            # normal.addData3f(zUp)
            # color.addData4f(bl)
            # i = triVerts[v2]
            # vertex.addData3f(pointmap(i.x, i.y))
            # normal.addData3f(zUp)
            # color.addData4f(bl)
            print "v 1 2 3", v0, v1, v2
            prim.addVertices(v0, v1, v2)

    prim.closePrimitive()
    geom = Geom(vdata)
    geom.addPrimitive(prim)
    node = GeomNode('gnode')
    node.addGeom(geom)

    # print trilator.isLeftWinding()
    # HACK just for school
    if hasattr(trilator, "adLst"):
        return tuple((node, trilator.adjLst))
    else:
        return tuple((node, trilator))
Example #4
0
def makeTriMesh( verts, holeVerts=[[]]):
    pointmap = (lambda x, y: (x, y, 0))
    if not holeVerts:
        holeVerts = [[]]

    if not hasattr(holeVerts[0], '__iter__'):
        holeVerts = [holeVerts]

    frmt = GeomVertexFormat.getV3n3cp()
    vdata = GeomVertexData('triangle', frmt, Geom.UHDynamic)

    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    color = GeomVertexWriter(vdata, 'color')

    bl = Vec4(50, 50, 50, 255)
    gr = Vec4(256/2 - 1, 256/2 - 1, 256/2 - 1, 255)

    trilator = Triangulator()
    zUp = Vec3(0, 0, 1)

    for i in verts:
        #print "verts", verts
        trilator.addPolygonVertex(trilator.addVertex(i.x, i.y))
        vertex.addData3f(pointmap(i.x, i.y))
        normal.addData3f(zUp)
        color.addData4f(bl)
    #if len(holeVerts) != 1 and holeVerts[0] != []:
    for w in holeVerts:
        trilator.beginHole()
        print "new hole"
        for j in w:
            # print(j)  # ###################### PRINT #######################
            trilator.addHoleVertex(trilator.addVertex(j.x, j.y))
            vertex.addData3f(pointmap(j.x, j.y))
            normal.addData3f(zUp)
            color.addData4f(gr)

    # try:
    trilator.triangulate()
    triVerts = trilator.getVertices()
    p = trilator.getTriangleV0(0)
    print "trilator return", p
    # except AssertionError:
    #     pass
    # TODO:re-triangulate here and change AdjacencyList to expect a list of triangles rather than call Triangulator funcs
    trilator = makeDelaunayTriangulation(trilator)


    prim = GeomTriangles(Geom.UHStatic)
    if isinstance(trilator, Triangulator):
        # HACK just to switch back and forth from the non-Delaunay to the Delaunay for school
        for n in xrange(trilator.getNumTriangles()):
            prim.addVertices(trilator.getTriangleV0(n),
                             trilator.getTriangleV1(n),
                             trilator.getTriangleV2(n))
    else:  # it's an adjacency list

        for n in xrange(len(trilator.adjLst)):
            for v in range(0, len(triVerts)):
                print "\ncurr", triVerts[v], "\nv0", Point2D(trilator.adjLst[n].tri[0].x, trilator.adjLst[n].tri[0].y),\
                    "\nv1", Point2D(trilator.adjLst[n].tri[1].x, trilator.adjLst[n].tri[1].y),\
                    "\nv2", Point2D(trilator.adjLst[n].tri[2].x, trilator.adjLst[n].tri[2].y)
                    # trilator.adjLst[n].tri[0].getXy(),\
                    # "\nv1", trilator.adjLst[n].tri[1].getXy(),\
                    # "\nv2", trilator.adjLst[n].tri[2].getXy()
                # v0 = v1 = v2 = -1
                if Point2D(trilator.adjLst[n].tri[0].x, trilator.adjLst[n].tri[0].y) == triVerts[v]:
                    v0 = v  # we need indices into the vertex pool
                    print "found v0", v0

                if Point2D(trilator.adjLst[n].tri[1].x, trilator.adjLst[n].tri[1].y) == triVerts[v]:
                    v1 = v
                    print "found v1", v1

                if Point2D(trilator.adjLst[n].tri[2].x, trilator.adjLst[n].tri[2].y) == triVerts[v]:
                    v2 = v
                    print "found v2", v2
            # if v0 == -1 or v1 == -1 or v2 == -1:
            #     print "pass", v0, v1, v2
            #     pass
            # else:
            #     print "add"

            # i = triVerts[v0]
            # vertex.addData3f(pointmap(i.x, i.y))
            # normal.addData3f(zUp)
            # color.addData4f(bl)
            # i = triVerts[v1]
            # vertex.addData3f(pointmap(i.x, i.y))
            # normal.addData3f(zUp)
            # color.addData4f(bl)
            # i = triVerts[v2]
            # vertex.addData3f(pointmap(i.x, i.y))
            # normal.addData3f(zUp)
            # color.addData4f(bl)
            print "v 1 2 3", v0, v1, v2
            prim.addVertices(v0, v1, v2)

    prim.closePrimitive()
    geom = Geom(vdata)
    geom.addPrimitive(prim)
    node = GeomNode('gnode')
    node.addGeom(geom)

    # print trilator.isLeftWinding()
    # HACK just for school
    if hasattr(trilator, "adLst"):
        return tuple((node, trilator.adjLst))
    else:
        return tuple((node, trilator))