Changeset 13391


Ignore:
Timestamp:
04/19/12 11:28:02 (13 years ago)
Author:
neise
Message:
using map.txt instead of map_dn.txt ... added a lot of comments
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/pyscripts/pyfact/coor.py

    r13231 r13391  
    77import math
    88from euclid import *
     9import sys
    910
    1011class Coordinator(object):
    1112    """ class to transform chid <-> hexagonal coordinates and vice versa """
    1213
    13     def __init__(self, map_file_path = "../map_dn.txt"):
     14    def __init__(self, map_file_path = "../map.txt"):
    1415        """ read map text file and generate from the three columns
    1516            chid, xe and ye
     
    2425            ex = Vector2( sqrt(3)/2. , 1./2. )
    2526        """
    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)
    3228
    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)
    3430        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
    3537        vectors_and_chids = []
    3638        for c in coors:
    3739            vectors_and_chids.append( (Vector2(c[0], c[1]) , int(c[2])) )
    3840
    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' :-)
    4051        center = Vector2( 0. , 1./2.)
     52        # the y-axis goes up
    4153        ey = Vector2( 0. , 1. )
     54        # but the x-axis is turned 30degrees up, wrt the euclidian x-axis.
    4255        ex = Vector2( math.sqrt(3)/2. , 1./2. )
    4356        self.center = ( center.x , center.y )
     
    4558        self.ex = ( ex.x, ex.y )
    4659
     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.
    4763        coor2chid = {}
    4864        chid2coor = {}
    4965        chid2coor_np = {}
    5066        chid2vec  = {}
     67        # we will fill these translators now.
    5168        for vector_and_chid in vectors_and_chids:
    5269            vec = vector_and_chid[0]
    5370            chid = vector_and_chid[1]
    54 
     71           
     72            # translating from euclidian into hexagonal
     73            # coordinates here...
    5574            x = (vec-center).x / float(ex.x)
    5675            y = ((vec-center)-x*ex).y / float(ey.y)
    5776       
     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...
    5881            if abs(x) < 0.01:
    5982                x=0.0
     
    6184                y=0.0
    6285
    63            
     86            # okay, now coor, is the hexagonal coordinate pair of the current pixel
     87            # as a tuple
    6488            coor = (int(round(x)),int(round(y)))
     89            # as Vector2
    6590            coor_vec = Vector2(coor[0], coor[1])
    6691           
     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
    6796            if coor in coor2chid:
    6897                print 'error while filling "coor2chid":'
     
    7099                print 'is equal to coor of chid:',coor2chid[coor]
    71100
    72             coor2chid[ coor ] = chid
     101            # now we fill the translators
    73102            chid2coor[ chid ] = coor
    74103            chid2coor_np[ chid ] = np.array(coor)
    75104            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
    76109
    77110        # 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.
    80121        chid2nn = {}
    81122        for chid in chid2coor.keys():
     
    90131            chid2nn[chid] = nn_chids
    91132        self.nn = chid2nn
     133       
    92134        self.chid2coor = chid2coor
    93135        self.chid2coor_np = chid2coor_np
     
    104146    return a[1]
    105147
     148
     149def _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
    106157if __name__ == '__main__':
    107158    co = Coordinator()
Note: See TracChangeset for help on using the changeset viewer.