StRoot  1
HexLatice.h
1 #ifndef HexLatice_h
2 #define HexLatice_h
3 #include <math.h>
4 class HexLatice {
5  public:
6  // data members
7  TVector2 U,V; // _versors_ of the grid
8  TVector2 bU,bV; // base vectors of the grid, length=pitch
9  double uv,w0,pitch;
10
11  HexLatice(double pitchX,double phi1deg) {
12  const double pi=2*acos(0);
13  pitch=pitchX;
14  bU.SetMagPhi(pitch,phi1deg/180*pi);
15  bV=bU.Rotate(pi/3);
16  U=bU.Unit();
17  V=bV.Unit();
18  uv=U*V; w0=(1-uv*uv)*pitch;
19  printf("Hex Grid base: U(%.3f,%3f), V(%.3f,%3f) U*V=%.3f\n",U.X(),U.Y(),V.X(),V.Y(),uv);
20  }
21
22  TVector2 snap(TVector2 &P, int &ku,int &kv){ // returns hexLatice coordinates
23  // f ind hex latice coordinates
24  double a=P*U, b=P*V;
25  double u=(a-b*uv)/w0, v=(b-a*uv)/w0;
26  double iu=floor(u), iv=floor(v);
27  printf("P(%.3f,%3f) --> hexLatice(%.3f,%.3f)*pitch -->floor(%.0f %.0f) \n", P.X(),P.Y(),u,v,iu,iv);
28  int iN;
29  double maxD2=999999;
30
31  TVector2 Psnap=TVector2(99999,99999);
32  for(iN=0;iN<4;iN++) {
33  double ju=iu+iN%2;
34  double jv=iv+iN/2;
35  TVector2 Q=ju*bU + jv*bV;
36  double dist2=(Q-P).Mod2();
37  //printf("iNode=%d Q(%.3f,%3f) -->dist/um=%.2f\n",iN,Q.X(),Q.Y(),sqrt(dist2)*1e4);
38  if(maxD2<dist2) continue;
39  maxD2=dist2;
40  Psnap=Q;
41
42  ku=(int)ju;
43  kv=(int)jv;
44  }
45
46  printf("closest node(%d,%d) Psnap(%.3f,%3f) -->dist/um=%.2f\n",ku,kv,Psnap.X(),Psnap.Y(),sqrt(maxD2)*1e4);
47
48  return Psnap;
49  }
50 };
51
52 #endif