View Javadoc

1   package org.lcsim.detector;
2   
3   import junit.framework.TestCase;
4   import junit.framework.TestSuite;
5   
6   import org.apache.commons.math3.geometry.euclidean.threed.Rotation;
7   import org.apache.commons.math3.geometry.euclidean.threed.RotationOrder;
8   import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
9   
10  public class RotationApacheTest extends TestCase
11  {		
12      public RotationApacheTest(String name)
13      {
14          super(name);
15      }
16      
17      public static junit.framework.Test suite()
18      {
19          return new TestSuite(RotationApacheTest.class);
20      }
21      
22      protected void setUp() throws Exception
23      {}
24      
25      
26      private boolean checkRotationEquality(Rotation r1, Rotation r2 ) {
27          double mat1[][] = r1.getMatrix();
28          double mat2[][] = r2.getMatrix();
29          boolean isEqual = true;
30          double diff = 0.0;
31          for(int row=0;row<3;++row) {
32              for(int col=0;col<3;++col) {
33                  diff = mat1[row][col]-mat2[row][col];
34                  if(Math.abs(diff) > 0.00001) {
35                      System.out.printf("rotation matrix index row %d and col %d is not equal: %f vs %f diff %f\n",row,col,mat1[row][col],mat2[row][col],diff);
36                      isEqual = false;
37                  }
38              }
39          }
40          
41          return isEqual;
42      }
43      
44      private boolean checkCardanAnglesEquality(double[] r1, double[] r2 ) {
45          boolean isEqual = true;
46          double diff = 0.0;
47          for(int row=0;row<3;++row) {
48              diff = r1[row]-r2[row];
49              if(Math.abs(diff) > 0.00001) {
50                  System.out.printf("angle at index %d is not equal: %f vs %f diff %f\n",row,r1[row],r2[row],diff);
51                  isEqual = false;
52              }
53          }
54          return isEqual;
55      }
56      
57      private void printMatrix(double m[][]) {
58         
59          for(int row=0;row<3;++row) {
60              System.out.printf("[ ");
61              for(int col=0;col<3;++col) {
62                  System.out.printf(" %f ", m[row][col]);
63                  
64              }
65              System.out.printf("]\n");
66          }
67      }
68      
69      public void testSimpleTransform()
70      {        
71          
72          Vector3D base_u = new Vector3D(1,0,0);
73          Vector3D base_v = new Vector3D(0,1,0);
74          Vector3D base_w = new Vector3D(0,0,1);
75          
76          //Rotate around w 
77          
78          double a = 0.03;
79          Rotation ra = new Rotation(base_w, a);
80          
81          //Apply rotation
82          
83          Vector3D ua = ra.applyTo(base_u);
84          Vector3D va = ra.applyTo(base_v);
85          Vector3D wa = ra.applyTo(base_w);
86  
87          //Flip around the rotated axis u 
88  
89          double b = Math.PI;
90          Rotation rb = new Rotation(ua, b);
91  
92          //Apply rotation
93          
94          Vector3D ub = rb.applyTo(ua);
95          Vector3D vb = rb.applyTo(va);
96          Vector3D wb = rb.applyTo(wa);
97          
98          
99          
100         System.out.printf("u %s\n",base_u.toString());
101         System.out.printf("v %s\n",base_v.toString());
102         System.out.printf("w %s\n",base_w.toString());
103         
104         System.out.printf("ua %s\n",ua.toString());
105         System.out.printf("va %s\n",va.toString());
106         System.out.printf("wa %s\n",wa.toString());
107         
108         System.out.printf("ub %s\n",ub.toString());
109         System.out.printf("vb %s\n",vb.toString());
110         System.out.printf("wb %s\n",wb.toString());
111         
112         // Create rotation from unit vector combinations
113         
114         Rotation rc_uv = new Rotation(ub, vb, base_u, base_v);
115         Rotation rc_uw = new Rotation(ub, wb, base_u, base_w);
116         Rotation rc_vw = new Rotation(vb, wb, base_v, base_w);
117         
118         // Get rotation matrix
119         
120         double rc_uv_mat[][] = rc_uv.getMatrix();
121         double rc_uw_mat[][] = rc_uw.getMatrix();
122         double rc_vw_mat[][] = rc_vw.getMatrix();
123 
124         System.out.printf("rc_uv_mat\n");
125         printMatrix(rc_uv_mat);
126         System.out.printf("rc_uw_mat\n");
127         printMatrix(rc_uw_mat);
128         System.out.printf("rc_vw_mat\n");
129         printMatrix(rc_vw_mat);
130         
131         // check that the matrix is the same
132         
133         boolean eq1 = checkRotationEquality(rc_uv, rc_uw);
134         boolean eq2 = checkRotationEquality(rc_uv, rc_vw);
135         boolean eq3 = checkRotationEquality(rc_uw, rc_vw);
136         
137         assertTrue(eq1 && eq2 &&eq3);
138         
139         // Get Cardan-angles
140         
141         double angles_rc_uv[] = rc_uv.getAngles(RotationOrder.XYZ);
142         double angles_rc_uw[] = rc_uw.getAngles(RotationOrder.XYZ);
143         double angles_rc_vw[] = rc_vw.getAngles(RotationOrder.XYZ);
144         
145         System.out.printf("angles_rc_uv (%f,%f,%f)\n",angles_rc_uv[0],angles_rc_uv[1],angles_rc_uv[2]);
146         System.out.printf("angles_rc_uw (%f,%f,%f)\n",angles_rc_uw[0],angles_rc_uw[1],angles_rc_uw[2]);
147         System.out.printf("angles_rc_vw (%f,%f,%f)\n",angles_rc_vw[0],angles_rc_vw[1],angles_rc_vw[2]);
148         
149         eq1 = checkCardanAnglesEquality(angles_rc_uv, angles_rc_uw);
150         eq2 = checkCardanAnglesEquality(angles_rc_uv, angles_rc_vw);
151         eq3 = checkCardanAnglesEquality(angles_rc_uw, angles_rc_vw);
152         
153         assertTrue(eq1 && eq2 &&eq3);
154         
155         
156         
157     }
158        
159    
160    
161 }