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

Last change on this file since 13151 was 13143, checked in by neise, 13 years ago
included python -tt option. thus checking for tabs/spaces
  • Property svn:executable set to *
File size: 3.2 KB
Line 
1#!/usr/bin/python -tt
2#
3# Dominik Neise
4#
5import os.path
6import numpy as np
7from pylab import *
8from euclid import *
9
10class 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 chid2vec = {}
47 for vector_and_chid in vectors_and_chids:
48 vec = vector_and_chid[0]
49 chid = vector_and_chid[1]
50
51 x = (vec-center).x / float(ex.x)
52 y = ((vec-center)-x*ex).y / float(ey.y)
53
54 if abs(x) < 0.01:
55 x=0.0
56 if abs(y) < 0.01:
57 y=0.0
58
59
60 coor = (int(round(x)),int(round(y)))
61 coor_vec = Vector2(coor[0], coor[1])
62
63 if coor in coor2chid:
64 print 'error while filling "coor2chid":'
65 print 'coor:',coor,'of chid:',chid,
66 print 'is equal to coor of chid:',coor2chid[coor]
67
68 coor2chid[ coor ] = chid
69 chid2coor[ chid ] = coor
70 chid2vec[ chid ] = coor_vec
71
72 # hard code the offsets to the next neighbors
73 offsets = [ Vector2(1,0) , Vector2(-1,0) , Vector2(1,-1) ,
74 Vector2(0,1) , Vector2(0,-1) , Vector2(-1,1) ]
75 chid2nn = {}
76 for chid in chid2coor.keys():
77 coor = Vector2( chid2coor[chid][0] , chid2coor[chid][1] )
78 nn_coors = []
79 nn_chids = []
80 for offset in offsets:
81 nn_coors.append( ((coor+offset).x , (coor+offset).y) )
82 for coor in nn_coors:
83 if coor in coor2chid:
84 nn_chids.append( coor2chid[coor] )
85 chid2nn[chid] = nn_chids
86 self.nn = chid2nn
87 self.chid2coor = chid2coor
88 self.coor2chid = coor2chid
89 self.chid2vec = chid2vec
90
91# for chid in chid2nn.keys():
92# print chid, '->',chid2nn[chid]
93
94def first(a):
95 return a[0]
96
97def second(a):
98 return a[1]
99
100if __name__ == '__main__':
101 co = Coordinator()
102
Note: See TracBrowser for help on using the repository browser.