| 1 | #!/usr/bin/python
|
|---|
| 2 | #
|
|---|
| 3 | # Dominik Neise
|
|---|
| 4 | #
|
|---|
| 5 | import os.path
|
|---|
| 6 | import numpy as np
|
|---|
| 7 | from pylab import *
|
|---|
| 8 | from euclid import *
|
|---|
| 9 |
|
|---|
| 10 | class Coordinator(object):
|
|---|
| 11 | """ class to transform chid <-> hexagonal coordinates and vice versa """
|
|---|
| 12 |
|
|---|
| 13 | def __init__(self, map_file_path = "../map_dn.txt"):
|
|---|
| 14 | """ read map text file and generate from the three columns
|
|---|
| 15 | chid, xe and ye
|
|---|
| 16 | 3 dictionaries: chid2coor, coor2chid, chid2nn
|
|---|
| 17 | chid2nn means 'chid_to_next_neighbor_chids'
|
|---|
| 18 |
|
|---|
| 19 | this is done by calculating the hexagonal coordinates
|
|---|
| 20 | from the euclidian coordinates given in xe & ye.
|
|---|
| 21 | the center and the two base vectors are hard coded to be:
|
|---|
| 22 | center = Vector2( 0. , 1./2.)
|
|---|
| 23 | ey = Vector2( 0. , 1. )
|
|---|
| 24 | ex = Vector2( sqrt(3)/2. , 1./2. )
|
|---|
| 25 | """
|
|---|
| 26 | path = os.path.abspath(__file__)
|
|---|
| 27 | path = os.path.dirname(path)
|
|---|
| 28 | map_file_path = os.path.join(path, map_file_path)
|
|---|
| 29 | if not os.path.isfile(map_file_path):
|
|---|
| 30 | print 'not able to find file:', map_file_path
|
|---|
| 31 | sys.exit(-2)
|
|---|
| 32 |
|
|---|
| 33 | chid, y,x,xe,ye,yh,xh,softid,hardid = np.loadtxt(map_file_path, unpack=True)
|
|---|
| 34 | coors = zip(xe,ye,chid)
|
|---|
| 35 | vectors_and_chids = []
|
|---|
| 36 | for c in coors:
|
|---|
| 37 | vectors_and_chids.append( (Vector2(c[0], c[1]) , int(c[2])) )
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | center = Vector2( 0. , 1./2.)
|
|---|
| 41 | ey = Vector2( 0. , 1. )
|
|---|
| 42 | ex = Vector2( sqrt(3)/2. , 1./2. )
|
|---|
| 43 |
|
|---|
| 44 | coor2chid = {}
|
|---|
| 45 | chid2coor = {}
|
|---|
| 46 | for vector_and_chid in vectors_and_chids:
|
|---|
| 47 | vec = vector_and_chid[0]
|
|---|
| 48 | chid = vector_and_chid[1]
|
|---|
| 49 |
|
|---|
| 50 | x = (vec-center).x / float(ex.x)
|
|---|
| 51 | y = ((vec-center)-x*ex).y / float(ey.y)
|
|---|
| 52 |
|
|---|
| 53 | if abs(x) < 0.01:
|
|---|
| 54 | x=0.0
|
|---|
| 55 | if abs(y) < 0.01:
|
|---|
| 56 | y=0.0
|
|---|
| 57 |
|
|---|
| 58 | coor = (int(round(x)),int(round(y)))
|
|---|
| 59 |
|
|---|
| 60 | if coor in coor2chid:
|
|---|
| 61 | print 'error while filling "coor2chid":'
|
|---|
| 62 | print 'coor:',coor,'of chid:',chid,
|
|---|
| 63 | print 'is equal to coor of chid:',coor2chid[coor]
|
|---|
| 64 |
|
|---|
| 65 | coor2chid[ coor ] = chid
|
|---|
| 66 | chid2coor[ chid ] = coor
|
|---|
| 67 |
|
|---|
| 68 | # hard code the offsets to the next neighbors
|
|---|
| 69 | offsets = [ Vector2(1,0) , Vector2(-1,0) , Vector2(1,-1) ,
|
|---|
| 70 | Vector2(0,1) , Vector2(0,-1) , Vector2(-1,1) ]
|
|---|
| 71 | chid2nn = {}
|
|---|
| 72 | for chid in chid2coor.keys():
|
|---|
| 73 | coor = Vector2( chid2coor[chid][0] , chid2coor[chid][1] )
|
|---|
| 74 | nn_coors = []
|
|---|
| 75 | nn_chids = []
|
|---|
| 76 | for offset in offsets:
|
|---|
| 77 | nn_coors.append( ((coor+offset).x , (coor+offset).y) )
|
|---|
| 78 | for coor in nn_coors:
|
|---|
| 79 | if coor in coor2chid:
|
|---|
| 80 | nn_chids.append( coor2chid[coor] )
|
|---|
| 81 | chid2nn[chid] = nn_chids
|
|---|
| 82 | self.nn = chid2nn
|
|---|
| 83 | self.chid2coor = chid2coor
|
|---|
| 84 | self.coor2chid = coor2chid
|
|---|
| 85 |
|
|---|
| 86 | # for chid in chid2nn.keys():
|
|---|
| 87 | # print chid, '->',chid2nn[chid]
|
|---|
| 88 |
|
|---|
| 89 | def first(a):
|
|---|
| 90 | return a[0]
|
|---|
| 91 |
|
|---|
| 92 | def second(a):
|
|---|
| 93 | return a[1]
|
|---|
| 94 |
|
|---|
| 95 | if __name__ == '__main__':
|
|---|
| 96 | co = Coordinator()
|
|---|
| 97 |
|
|---|