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