class TriangulacionPuntos(): """Clase TriangulacionPuntos""" def __init__(self, nube): self.nube = nube self.triangulacion = Grafica() self.dcel_triangulacion = DCEL() def get_nube(self): return self.nube def get_dcel_triangulacion(self): return self.dcel_triangulacion def ordena_puntos(self): ordenados = sorted(self.nube.get_puntos(), key=lambda punto: punto.x) return ordenados def add_triangulo(self, v1, v2, v3): a1 = Arista(v1, v2) a2 = Arista(v2, v3) a3 = Arista(v3, v1) self.triangulacion.agregar_arista(a1) self.triangulacion.agregar_arista(a2) self.triangulacion.agregar_arista(a3) def triangular(self): ordenados = self.ordena_puntos() self.add_triangulo(ordenados[0], ordenados[1], ordenados[2]) visitados = [ordenados[0], ordenados[1], ordenados[2]] for i in range(len(ordenados) - 3): #print("--------------------------------------------") #print("punto: "+ordenados[i+3].toString()) aristas_triang = self.triangulacion.get_aristas() for v in visitados: #print(self.triangulacion.toString()) arista = Arista(ordenados[i + 3], v) interseccion = False for a in aristas_triang: if arista.intersecta(a): #print(arista.toString()+" y "+a.toString()+" se intersectan") interseccion = True else: #print(arista.toString()+" y "+a.toString()+" no se intersectan") continue if not (interseccion): self.triangulacion.agregar_arista(arista) visitados.append(ordenados[i + 3]) self.dcel_triangulacion.construir(self.triangulacion) def flip_arista(self, a): pass
def main( displayFunction, argv=None, ): """.""" global xl, yl, xyl, xa, ya, cens, edgs, tris, neighs, triPts, dcel if argv is None: argv = sys.argv generate_points(True) xa = numpy.array(xl) # transform array data to list data (for delaunay()) ya = numpy.array(yl) cens, edgs, triPts, neighs = triang.delaunay(xa, ya) # Generate the DCEL dcel = DCEL.from_delaunay_triangulation(xl, yl, triPts, cens) glutInit(argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(width, height) glutInitWindowPosition(100, 100) glutCreateWindow("Delaunay triangulation") glutDisplayFunc(displayFunction) glutReshapeFunc(reshape) glutKeyboardFunc(keyboard) glutMainLoop() return
def launch_terminal(environment={}): global interface readline.parse_and_bind("tab: complete") environment.update(globals()) # sys.stdout = outbuff # sys.stderr = outbuff code.interact(">>>", raw_input, local=environment) sys.exit() if __name__ == "__main__": # outbuff = StringIO() dcel = DCEL() if len(sys.argv) < 2: launch_terminal() if option(sys.argv[1]): if sys.argv[1] == "--help": print "Uso: %s fichero [proporcion [--help] [--3d] [--auto-exit] [--disco] [--load] [--shuffle]]" % sys.argv[ 0] else: print "El primer fichero tiene que ser un path" sys.exit(1) if len(sys.argv) > 3: for i in xrange(3, len(sys.argv)): if not option(sys.argv[i]):
def get_dcel(self): return DCEL(self.vertices, self.halfedges, self.faces)
e_32.twin = e_23 e_32.next = e_24 e_32.prev = e_43 e_32.incident_face = f_234 e_34.twin = e_43 e_34.next = e_41 e_34.prev = e_23 e_34.incident_face = f_1234 e_43.twin = e_34 e_43.next = e_32 e_43.prev = e_24 e_43.incident_face = f_234 v_1.incident_edge = e_12 v_2.incident_edge = e_24 v_3.incident_edge = e_34 v_4.incident_edge = e_41 #dcel = {'vertex': [v_1, v_2, v_3, v_4], 'edge': [e_12, e_21, e_23, e_32, e_34, e_43, e_14, e_41, e_24, e_42], 'face': [f_124, f_234, f_1234]} # for v in [v_1, v_2, v_3, v_4]: # dcel.vertices.insert(v) vertices = [v_1, v_2, v_3, v_4] halfedges = [e_12, e_21, e_23, e_32, e_34, e_43, e_14, e_41, e_24, e_42] faces = [f_124, f_234, f_1234] dcel = DCEL(vertices, halfedges, faces) if __name__ == '__main__': dcel.plot_dcel()
def datadict2dcel(datadict): #assume ccw vertex order hedges = {} # he_id: (v_origin, v_end), f, nextedge, prevedge vertices = {} # v_id: (e0,...,en) i.e. the edges originating from this v m = len(datadict['coords']) for i in xrange(m): vertices[i] = [] # find all halfedges, keep track of their vertices and faces j=0 for i, face in enumerate(datadict['faces']): # face.reverse() n_vertices = len(face) for v_i in xrange(n_vertices): # store reference to this hedge in vertex list vertices[face[v_i]].append(j) if v_i ==0: hedges[j] = (face[v_i], face[v_i+1]), i, j+1, j+(n_vertices-1) vertices[face[v_i+1]].append(j) elif v_i < n_vertices-1: hedges[j] = (face[v_i], face[v_i+1]), i, j+1, j-1 vertices[face[v_i+1]].append(j) else: hedges[j] = (face[v_i], face[0]), i, j-(n_vertices-1), j-1 vertices[face[0]].append(j) vertices[face[v_i]].append(j) j+=1 D = DCEL() # create vertices for all points for v in datadict['coords']: dcel_v = D.createVertex(v[0], v[1], v[2]) # create faces for f in xrange(len(datadict['faces'])): D.createFace() # the last face in the DCEL will be the infinite face: infinite_face = D.createInfFace() # create all edges except for the ones incident to the infinite face for e in xrange(len(hedges)): D.createHedge() inf_edge = None for this_edge, value in hedges.iteritems(): v, face, nextedge, prevedge = value v_origin, v_end = v v_origin_edges = Set(vertices[v_origin]) v_end_edges = Set(vertices[v_end]) # print v_origin_edges, v_end_edges twin_edge = v_origin_edges.intersection(v_end_edges) twin_edge.discard(this_edge) e = D.hedgeList[this_edge] if len(twin_edge) == 0: # oh that must be incident to infinite face... # face = infinite_face e_twin = D.createHedge() e_twin.setTopology( D.vertexList[v_end], e, infinite_face, None, None ) # oops, forgetting to set something here... inf_edge = e_twin else: e_twin = D.hedgeList[twin_edge.pop()] D.faceList[face].setTopology(e) e.setTopology( D.vertexList[v_origin], e_twin, D.faceList[face], D.hedgeList[nextedge], D.hedgeList[prevedge] ) e.origin.setTopology(e) # now fix prev/next refs for all edges incident to inf face infinite_face.innerComponent = inf_edge current_edge = last_correct_edge = inf_edge while inf_edge.previous == None: current_edge = last_correct_edge while current_edge.twin.incidentFace != infinite_face: current_edge = current_edge.twin.previous current_edge = current_edge.twin last_correct_edge.next = current_edge current_edge.previous = last_correct_edge last_correct_edge = current_edge return D
help='specify input file', required=True) parser.add_argument('-o', '--output', help='specify output file', required=True) return parser.parse_args() if __name__ == '__main__': args = register_launch_arguments() try: with open(args.input) as input_file: input_data = json.load(input_file) dcel = DCEL(**input_data['pslg']) points = [Point(**point_dict) for point_dict in input_data['points']] search_system = SearchSystem(dcel) output_data = { 'faces': [search_system.locate_point(point) for point in points] } except FileNotFoundError as e: output_data = {'error': f"No such file '{e.filename}'"} except (json.decoder.JSONDecodeError, KeyError): output_data = {'error': 'Incorrect file format'} except IndexError: output_data = {'error': 'Incorrect indexing'} except Exception as e: output_data = {'error': str(e)} with open(args.output, 'w') as output_file:
def __init__(self, nube): self.nube = nube self.triangulacion = Grafica() self.dcel_triangulacion = DCEL()
def datadict2dcel(datadict): # assume ccw vertex order hedges = {} # he_id: (v_origin, v_end), f, nextedge, prevedge vertices = {} # v_id: (e0,...,en) i.e. the edges originating from this v m = len(datadict['coords']) for i in range(m): vertices[i] = [] # find all halfedges, keep track of their vertices and faces j = 0 for i, face in enumerate(datadict['faces']): # face.reverse() n_vertices = len(face) for v_i in range(n_vertices): # store reference to this hedge in vertex list vertices[face[v_i]].append(j) if v_i == 0: hedges[j] = (face[v_i], face[v_i + 1]), i, j + 1, j + (n_vertices - 1) vertices[face[v_i + 1]].append(j) elif v_i < n_vertices - 1: hedges[j] = (face[v_i], face[v_i + 1]), i, j + 1, j - 1 vertices[face[v_i + 1]].append(j) else: hedges[j] = (face[v_i], face[0]), i, j - (n_vertices - 1), j - 1 vertices[face[0]].append(j) vertices[face[v_i]].append(j) j += 1 D = DCEL() # create vertices for all points for v in datadict['coords']: dcel_v = D.createVertex(v[0], v[1], v[2]) # create faces for f in range(len(datadict['faces'])): D.createFace() # the last face in the DCEL will be the infinite face: infinite_face = D.createInfFace() # create all edges except for the ones incident to the infinite face for e in range(len(hedges)): D.createHedge() inf_edge = None for this_edge, value in hedges.items(): v, face, nextedge, prevedge = value v_origin, v_end = v v_origin_edges = set(vertices[v_origin]) v_end_edges = set(vertices[v_end]) # print v_origin_edges, v_end_edges twin_edge = v_origin_edges.intersection(v_end_edges) twin_edge.discard(this_edge) e = D.hedgeList[this_edge] if len(twin_edge) == 0: # oh that must be incident to infinite face... # face = infinite_face e_twin = D.createHedge() e_twin.setTopology( D.vertexList[v_end], e, infinite_face, None, None) # oops, forgetting to set something here... inf_edge = e_twin else: e_twin = D.hedgeList[twin_edge.pop()] D.faceList[face].setTopology(e) e.setTopology(D.vertexList[v_origin], e_twin, D.faceList[face], D.hedgeList[nextedge], D.hedgeList[prevedge]) e.origin.setTopology(e) # now fix prev/next refs for all edges incident to inf face # infinite_face.innerComponent = inf_edge # current_edge = last_correct_edge = inf_edge # # while inf_edge is not None and inf_edge.previous == None: # # current_edge = last_correct_edge # while current_edge.twin.incidentFace != infinite_face: # current_edge = current_edge.twin.previous # current_edge = current_edge.twin # # last_correct_edge.next = current_edge # current_edge.previous = last_correct_edge # last_correct_edge = current_edge return D