# Kirby Urner, 4D Studios, Portland, Oregon July 27, 2006 # read OFF file, build vertex dictionary, get faces as tuples, construct edges (low, high), # and brute-force determine what neighbors what and store in a big globalmatrix dictionary # See: http://www.4dsolutions.net/ocn/hexapent.html thepath = 'd:/python25/Lib/site-packages/' def getinfo(filename): thefile = file(thepath + filename, 'r') check = thefile.next() if check[:3] <> "OFF": raise Exception, "this is not an OFF file" vertno = 0 numverts = int(thefile.next().split(' ')[0]) verts = {} for i in range(numverts): verts[vertno] = tuple([float(i) for i in thefile.next().split(' ')]) vertno += 1 faceno = 0 faces = [] while True: try: # drop first digit as it only says how many verts in this face faces.append( tuple([int(i) for i in thefile.next().split(' ')[1:]]) ) except StopIteration: break faceno += 1 unique = set() for f in faces: pairs = zip(f, list(f[1:]) + [f[0]]) for p in pairs: unique.add(tuple(sorted(p))) edges = list(unique) thefile.close() return (verts, faces, edges) def getneighbors(thefile): globalmatrix = {} theverts, thefaces, theedges = getinfo(thefile) for f in thefaces: neighbors = [] for candidate in thefaces: votes = 0 for v in f: if v in candidate: votes += 1 if votes == 2: neighbors.append(candidate) globalmatrix[f] = neighbors return (globalmatrix, theverts, thefaces, theedges)