00001
00002
00003 #ifndef GLAST_MODULEID_H
00004 #define GLAST_MODULEID_H 1
00005
00006 #include <stdlib.h>
00007 #include <iostream>
00008 #include <cmath>
00009
00010 inline static int iabs(int i){return i>0?i:-i;}
00011
00012 static int yNum = 4;
00013
00014 class ModuleId
00015 {
00016 public:
00017 ModuleId (unsigned int id = 0);
00018 ModuleId (unsigned int ix, unsigned int iy);
00019
00020
00021 int ix () const;
00022 int iy () const;
00023
00024
00025 bool neighbor (const ModuleId& n);
00026
00027
00028 void write (std::ostream& out) const;
00029 void read (std::istream& in);
00030
00031
00032 operator unsigned int () const { return m_id; }
00033
00034 private:
00035 unsigned int m_id;
00036 unsigned int row, col;
00037 };
00038
00039
00040
00041 inline ModuleId::ModuleId ( unsigned int id ) : m_id (id)
00042 {
00043
00044 row = (int) floor(double(id) / yNum) + 1;
00045 col = (id % yNum) + 1;
00046 }
00047
00048 inline ModuleId::ModuleId ( unsigned int ix, unsigned int iy ) : row(ix), col(iy)
00049 {
00050
00051 m_id = ((ix-1) * yNum) + (iy - 1);
00052 }
00053
00054 inline int ModuleId::ix () const
00055 {
00056 return row;
00057 }
00058
00059 inline int ModuleId::iy () const
00060 {
00061 return col;
00062 }
00063
00064 inline bool ModuleId::neighbor (const ModuleId& n)
00065 {
00066 return iabs(ix()-n.ix())<2 && iabs(iy()-n.iy())<2;
00067 }
00068
00069 inline void ModuleId::write (std::ostream& out) const
00070 {
00071 out << m_id;
00072 }
00073
00074 inline void ModuleId::read (std::istream& in)
00075 {
00076 in >> m_id;
00077 row = (int) floor(double(m_id) / yNum) + 1;
00078 col = (m_id % yNum) + 1;
00079 }
00080
00081 inline std::ostream& operator<<(std::ostream& out,const ModuleId& m)
00082 {m.write(out); return out;}
00083
00084 inline std::istream& operator>>(std::istream& in, ModuleId& m)
00085 {m.read(in); return in;}
00086
00087 #endif