Changeset 13391
- Timestamp:
- 04/19/12 11:28:02 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/pyfact/coor.py
r13231 r13391 7 7 import math 8 8 from euclid import * 9 import sys 9 10 10 11 class Coordinator(object): 11 12 """ class to transform chid <-> hexagonal coordinates and vice versa """ 12 13 13 def __init__(self, map_file_path = "../map _dn.txt"):14 def __init__(self, map_file_path = "../map.txt"): 14 15 """ read map text file and generate from the three columns 15 16 chid, xe and ye … … 24 25 ex = Vector2( sqrt(3)/2. , 1./2. ) 25 26 """ 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) 27 _CheckPath(map_file_path) 32 28 33 chid, y,x,xe,ye,yh,xh,softid,hardid= np.loadtxt(map_file_path, unpack=True)29 chid, xe, ye = np.loadtxt(map_file_path, unpack=True) 34 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 35 37 vectors_and_chids = [] 36 38 for c in coors: 37 39 vectors_and_chids.append( (Vector2(c[0], c[1]) , int(c[2])) ) 38 40 39 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' :-) 40 51 center = Vector2( 0. , 1./2.) 52 # the y-axis goes up 41 53 ey = Vector2( 0. , 1. ) 54 # but the x-axis is turned 30degrees up, wrt the euclidian x-axis. 42 55 ex = Vector2( math.sqrt(3)/2. , 1./2. ) 43 56 self.center = ( center.x , center.y ) … … 45 58 self.ex = ( ex.x, ex.y ) 46 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. 47 63 coor2chid = {} 48 64 chid2coor = {} 49 65 chid2coor_np = {} 50 66 chid2vec = {} 67 # we will fill these translators now. 51 68 for vector_and_chid in vectors_and_chids: 52 69 vec = vector_and_chid[0] 53 70 chid = vector_and_chid[1] 54 71 72 # translating from euclidian into hexagonal 73 # coordinates here... 55 74 x = (vec-center).x / float(ex.x) 56 75 y = ((vec-center)-x*ex).y / float(ey.y) 57 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... 58 81 if abs(x) < 0.01: 59 82 x=0.0 … … 61 84 y=0.0 62 85 63 86 # okay, now coor, is the hexagonal coordinate pair of the current pixel 87 # as a tuple 64 88 coor = (int(round(x)),int(round(y))) 89 # as Vector2 65 90 coor_vec = Vector2(coor[0], coor[1]) 66 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 67 96 if coor in coor2chid: 68 97 print 'error while filling "coor2chid":' … … 70 99 print 'is equal to coor of chid:',coor2chid[coor] 71 100 72 coor2chid[ coor ] = chid101 # now we fill the translators 73 102 chid2coor[ chid ] = coor 74 103 chid2coor_np[ chid ] = np.array(coor) 75 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 76 109 77 110 # hard code the offsets to the next neighbors 78 offsets = [ Vector2(1,0) , Vector2(-1,0) , Vector2(1,-1) , 79 Vector2(0,1) , Vector2(0,-1) , Vector2(-1,1) ] 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. 80 121 chid2nn = {} 81 122 for chid in chid2coor.keys(): … … 90 131 chid2nn[chid] = nn_chids 91 132 self.nn = chid2nn 133 92 134 self.chid2coor = chid2coor 93 135 self.chid2coor_np = chid2coor_np … … 104 146 return a[1] 105 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 106 157 if __name__ == '__main__': 107 158 co = Coordinator()
Note:
See TracChangeset
for help on using the changeset viewer.