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

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