source: fact/tools/pyscripts/pyfact/coor.py@ 13408

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