00001 #ifndef HexLatice_h
00002 #define HexLatice_h
00003 #include <math.h>
00004 class HexLatice {
00005 public:
00006
00007 TVector2 U,V;
00008 TVector2 bU,bV;
00009 double uv,w0,pitch;
00010
00011 HexLatice(double pitchX,double phi1deg) {
00012 const double pi=2*acos(0);
00013 pitch=pitchX;
00014 bU.SetMagPhi(pitch,phi1deg/180*pi);
00015 bV=bU.Rotate(pi/3);
00016 U=bU.Unit();
00017 V=bV.Unit();
00018 uv=U*V; w0=(1-uv*uv)*pitch;
00019 printf("Hex Grid base: U(%.3f,%3f), V(%.3f,%3f) U*V=%.3f\n",U.X(),U.Y(),V.X(),V.Y(),uv);
00020 }
00021
00022 TVector2 snap(TVector2 &P, int &ku,int &kv){
00023
00024 double a=P*U, b=P*V;
00025 double u=(a-b*uv)/w0, v=(b-a*uv)/w0;
00026 double iu=floor(u), iv=floor(v);
00027 printf("P(%.3f,%3f) --> hexLatice(%.3f,%.3f)*pitch -->floor(%.0f %.0f) \n", P.X(),P.Y(),u,v,iu,iv);
00028 int iN;
00029 double maxD2=999999;
00030
00031 TVector2 Psnap=TVector2(99999,99999);
00032 for(iN=0;iN<4;iN++) {
00033 double ju=iu+iN%2;
00034 double jv=iv+iN/2;
00035 TVector2 Q=ju*bU + jv*bV;
00036 double dist2=(Q-P).Mod2();
00037
00038 if(maxD2<dist2) continue;
00039 maxD2=dist2;
00040 Psnap=Q;
00041
00042 ku=(int)ju;
00043 kv=(int)jv;
00044 }
00045
00046 printf("closest node(%d,%d) Psnap(%.3f,%3f) -->dist/um=%.2f\n",ku,kv,Psnap.X(),Psnap.Y(),sqrt(maxD2)*1e4);
00047
00048 return Psnap;
00049 }
00050 };
00051
00052 #endif