1 | #!/usr/bin/python -tt
|
---|
2 | #
|
---|
3 | # Dominik Neise
|
---|
4 | #
|
---|
5 | import os.path
|
---|
6 | import numpy as np
|
---|
7 | import math
|
---|
8 | from euclid import *
|
---|
9 | import sys
|
---|
10 |
|
---|
11 | class Coordinator(object):
|
---|
12 | """ class to transform chid <-> hexagonal coordinates and vice versa """
|
---|
13 |
|
---|
14 | def __init__(self, map_file_path = "../map.txt"):
|
---|
15 | """ read map text file and generate from the three columns
|
---|
16 | chid, xe and ye
|
---|
17 | 3 dictionaries: chid2coor, coor2chid, chid2nn
|
---|
18 | chid2nn means 'chid_to_next_neighbor_chids'
|
---|
19 |
|
---|
20 | this is done by calculating the hexagonal coordinates
|
---|
21 | from the euclidian coordinates given in xe & ye.
|
---|
22 | the center and the two base vectors are hard coded to be:
|
---|
23 | center = Vector2( 0. , 1./2.)
|
---|
24 | ey = Vector2( 0. , 1. )
|
---|
25 | ex = Vector2( sqrt(3)/2. , 1./2. )
|
---|
26 | """
|
---|
27 | _CheckPath(map_file_path)
|
---|
28 |
|
---|
29 | chid, xe, ye = np.loadtxt(map_file_path, unpack=True)
|
---|
30 | coors = zip(xe,ye,chid)
|
---|
31 |
|
---|
32 | # this list will contain vectors pointing to the center of pixel
|
---|
33 | # in euclidian space. The coordinate system is in the focalplane of the
|
---|
34 | # camera, the unit is not mm, but something like 9mm.
|
---|
35 | # actually the list will not only contain these vectors, but also
|
---|
36 | # also the CHID of the according pixel, both bundled in a tuple
|
---|
37 | vectors_and_chids = []
|
---|
38 | for c in coors:
|
---|
39 | vectors_and_chids.append( (Vector2(c[0], c[1]) , int(c[2])) )
|
---|
40 |
|
---|
41 | # In the next few lines, I will calculate hexagonal coordinates from
|
---|
42 | # the euclidian coordinates. The reason is, that I like to work with
|
---|
43 | # integers.
|
---|
44 | # I could have read these numbers from a file instead of calculating,
|
---|
45 | # but this is error prone, because one has to make sure, the different
|
---|
46 | # coordinates in a file are always conincident.
|
---|
47 |
|
---|
48 | # The center of the coordinate system is not 0. / 0. since there
|
---|
49 | # is not pixel :-) We decided to define the upper one of the two
|
---|
50 | # central pixels, as 'The Center' :-)
|
---|
51 | center = Vector2( 0. , 1./2.)
|
---|
52 | # the y-axis goes up
|
---|
53 | ey = Vector2( 0. , 1. )
|
---|
54 | # but the x-axis is turned 30degrees up, wrt the euclidian x-axis.
|
---|
55 | ex = Vector2( math.sqrt(3)/2. , 1./2. )
|
---|
56 | self.center = ( center.x , center.y )
|
---|
57 | self.ey = ( ey.x, ey.y)
|
---|
58 | self.ex = ( ex.x, ex.y )
|
---|
59 |
|
---|
60 | # these dicts will serve as translators,
|
---|
61 | # e.g. put a chid into chid2coor and you get a Vector2 out, which points
|
---|
62 | # to the center of the according pixel.
|
---|
63 | coor2chid = {}
|
---|
64 | chid2coor = {}
|
---|
65 | chid2coor_np = {}
|
---|
66 | chid2vec = {}
|
---|
67 | # we will fill these translators now.
|
---|
68 | for vector_and_chid in vectors_and_chids:
|
---|
69 | vec = vector_and_chid[0]
|
---|
70 | chid = vector_and_chid[1]
|
---|
71 |
|
---|
72 | # translating from euclidian into hexagonal
|
---|
73 | # coordinates here...
|
---|
74 | x = (vec-center).x / float(ex.x)
|
---|
75 | y = ((vec-center)-x*ex).y / float(ey.y)
|
---|
76 |
|
---|
77 | # I want them to be integger, so I think I have to
|
---|
78 | # treat the values, which are almost zero special,
|
---|
79 | # but maybe rounding is just sufficient, as it is done
|
---|
80 | # in the line after these...
|
---|
81 | if abs(x) < 0.01:
|
---|
82 | x=0.0
|
---|
83 | if abs(y) < 0.01:
|
---|
84 | y=0.0
|
---|
85 |
|
---|
86 | # okay, now coor, is the hexagonal coordinate pair of the current pixel
|
---|
87 | # as a tuple
|
---|
88 | coor = (int(round(x)),int(round(y)))
|
---|
89 | # as Vector2
|
---|
90 | coor_vec = Vector2(coor[0], coor[1])
|
---|
91 |
|
---|
92 | # since we just calculated this coordinate, we should make
|
---|
93 | # sure, that we did not make an error such, that two pixels have the
|
---|
94 | # same coordinates
|
---|
95 | # other errors like holes in the camera plane cannot be detected so easily
|
---|
96 | if coor in coor2chid:
|
---|
97 | print 'error while filling "coor2chid":'
|
---|
98 | print 'coor:',coor,'of chid:',chid,
|
---|
99 | print 'is equal to coor of chid:',coor2chid[coor]
|
---|
100 |
|
---|
101 | # now we fill the translators
|
---|
102 | chid2coor[ chid ] = coor
|
---|
103 | chid2coor_np[ chid ] = np.array(coor)
|
---|
104 | chid2vec[ chid ] = coor_vec
|
---|
105 | # this translator is hardly used by people, but the next step
|
---|
106 | # the calculation of the neighbors needs it
|
---|
107 | coor2chid[ coor ] = chid
|
---|
108 |
|
---|
109 |
|
---|
110 | # hard code the offsets to the next neighbors
|
---|
111 | # in hexagonal coordinates, the coordinates of neighbors are easily calculated.
|
---|
112 | # just add one of the Vectors below.
|
---|
113 | offsets = [ Vector2(1,0) , # right and up
|
---|
114 | Vector2(-1,0) , # left and down
|
---|
115 | Vector2(1,-1) , # right and down
|
---|
116 | Vector2(0,1) , # up
|
---|
117 | Vector2(0,-1) , # down
|
---|
118 | Vector2(-1,1) ] # left and up
|
---|
119 | # this dict serves as a neighbor look up table
|
---|
120 | # put a CHID in and get a list of neighboring CHIDs out.
|
---|
121 | chid2nn = {}
|
---|
122 | for chid in chid2coor.keys():
|
---|
123 | coor = Vector2( chid2coor[chid][0] , chid2coor[chid][1] )
|
---|
124 | nn_coors = []
|
---|
125 | nn_chids = []
|
---|
126 | for offset in offsets:
|
---|
127 | nn_coors.append( ((coor+offset).x , (coor+offset).y) )
|
---|
128 | for coor in nn_coors:
|
---|
129 | if coor in coor2chid:
|
---|
130 | nn_chids.append( coor2chid[coor] )
|
---|
131 | chid2nn[chid] = nn_chids
|
---|
132 | self.nn = chid2nn
|
---|
133 |
|
---|
134 | self.chid2coor = chid2coor
|
---|
135 | self.chid2coor_np = chid2coor_np
|
---|
136 | self.coor2chid = coor2chid
|
---|
137 | self.chid2vec = chid2vec
|
---|
138 |
|
---|
139 | # for chid in chid2nn.keys():
|
---|
140 | # print chid, '->',chid2nn[chid]
|
---|
141 |
|
---|
142 | def first(a):
|
---|
143 | return a[0]
|
---|
144 |
|
---|
145 | def second(a):
|
---|
146 | return a[1]
|
---|
147 |
|
---|
148 |
|
---|
149 | def _CheckPath( inpath ):
|
---|
150 | path = os.path.abspath(__file__)
|
---|
151 | path = os.path.dirname(path)
|
---|
152 | inpath = os.path.join(path, inpath)
|
---|
153 | if not os.path.isfile(inpath):
|
---|
154 | raise IOError('not able to find file: '+inpath)
|
---|
155 |
|
---|
156 |
|
---|
157 | if __name__ == '__main__':
|
---|
158 | co = Coordinator()
|
---|
159 |
|
---|