1 package org.lcsim.spacegeom; 2 import junit.framework.TestCase; 3 4 import org.lcsim.spacegeom.CartesianPointVector; 5 import org.lcsim.spacegeom.CylindricalPointVector; 6 import org.lcsim.spacegeom.SpacePointVector; 7 import org.lcsim.spacegeom.SphericalPointVector; 8 // SpacePointVecTest.cpp 9 10 // Test the SpacePointVector class. 11 12 public class SpacePointVecTest extends TestCase 13 { 14 boolean debug = false; 15 //********************************************************************** 16 17 public static boolean myequal(double x1, double x2) 18 { 19 return Math.abs(x2-x1) < 1.e-10; 20 } 21 22 //********************************************************************** 23 24 public void testSpacePointVec() 25 { 26 27 String name = "SpacePointVector"; 28 String ok_prefix = name + " test (I): "; 29 String error_prefix = name + " test (E): "; 30 31 if (debug) System.out.println( ok_prefix 32 + "------- Testing component " + name + ". -------" ); 33 34 double x = 1.23; 35 double y = 2.46; 36 double z = 3.69; 37 double vx = 0.23; 38 double vy = 0.45; 39 double vz = 0.67; 40 41 //********************************************************************** 42 43 if (debug) System.out.println( ok_prefix + "Testing constructors." ); 44 // Create a spacevector 45 CartesianPointVector cart = new CartesianPointVector(x,y,z,vx,vy,vz); 46 if (debug) System.out.println( cart ); 47 48 // Create in cylindrical coordinates. 49 CylindricalPointVector cyl = new CylindricalPointVector( cart.getStartPoint().rxy(), cart.getStartPoint().phi(), cart.getStartPoint().z(), 50 cart.v_rxy(), cart.v_phi(), cart.v_z() ); 51 if (debug) System.out.println( cyl ); 52 53 // Create in spherical coordinates. 54 SphericalPointVector sph = new SphericalPointVector( cyl.getStartPoint().rxyz(), cyl.getStartPoint().phi(), cyl.getStartPoint().theta(), 55 cyl.v_rxyz(), cyl.v_theta(), cyl.v_phi() ); 56 if (debug) System.out.println( sph ); 57 58 assertTrue(myequal(sph.getStartPoint().x(),x)); 59 assertTrue( myequal(sph.getStartPoint().x(),x) ); 60 assertTrue( myequal(sph.getStartPoint().y(),y) ); 61 assertTrue( myequal(sph.getStartPoint().z(),z) ); 62 assertTrue( myequal(sph.v_x(),vx) ); 63 assertTrue( myequal(sph.v_y(),vy) ); 64 assertTrue( myequal(sph.v_z(),vz) ); 65 66 // Create a cartesian coordinates from spacepoint 67 CartesianPointVector cart2 = new CartesianPointVector( sph.getStartPoint(), sph.v_x(), sph.v_y(), sph.v_z() ); 68 if (debug) System.out.println( cart2 ); 69 70 // Create in cylindrical coordinates. 71 CylindricalPointVector cyl2 = new CylindricalPointVector( cart2.getStartPoint(), 72 cart2.v_rxy(), cart2.v_phi(), cart2.v_z() ); 73 if (debug) System.out.println( cyl2 ); 74 75 // Create in spherical coordinates. 76 SphericalPointVector sph2 = new SphericalPointVector( cyl2.getStartPoint(), 77 cyl2.v_rxyz(), cyl2.v_theta(), cyl2.v_phi() ); 78 if (debug) System.out.println( sph2 ); 79 80 assertTrue( myequal(sph2.getStartPoint().x(),x) ); 81 assertTrue( myequal(sph2.getStartPoint().y(),y) ); 82 assertTrue( myequal(sph2.getStartPoint().z(),z) ); 83 assertTrue( myequal(sph2.v_x(),vx) ); 84 assertTrue( myequal(sph2.v_y(),vy) ); 85 assertTrue( myequal(sph2.v_z(),vz) ); 86 87 //********************************************************************** 88 89 if (debug) System.out.println( ok_prefix + "Testing equality." ); 90 assertTrue( sph.equals(sph) ); 91 assertTrue( sph.equals(sph2) ); 92 if( sph.notEquals(sph2) ); 93 94 //********************************************************************** 95 96 if (debug) System.out.println( ok_prefix + "Testing assignment." ); 97 SpacePointVector svec = new SpacePointVector(); 98 if (debug) System.out.println( svec ); 99 if (debug) System.out.println( "testing assignment svec.magnitude() and 0 " + svec.magnitude() + " " + 0. ); 100 if (debug) System.out.println( "spacepoint equal " + (svec.magnitude()==0.0) ); 101 // Assert.assertTrue( SpacePoint.equal(svec.magnitude(),0.0) ); 102 svec = sph; 103 if (debug) System.out.println( svec ); 104 if (debug) System.out.println( "testing assignment svec.x() and x " + svec.getStartPoint().x() + " " + x ); 105 assertTrue( myequal(svec.getStartPoint().x(),x) ); 106 assertTrue( myequal(svec.getStartPoint().y(),y) ); 107 assertTrue( myequal(svec.getStartPoint().z(),z) ); 108 assertTrue( myequal(svec.v_x(),vx) ); 109 assertTrue( myequal(svec.v_y(),vy) ); 110 assertTrue( myequal(svec.v_z(),vz) ); 111 112 //********************************************************************** 113 114 if (debug) System.out.println( ok_prefix + "Testing normalizations." ); 115 double n0 = vx*vx + vy*vy + vz*vz; 116 if (debug) System.out.println( "n0= " +n0 ); 117 double ncart = svec.v_x()*svec.v_x() + 118 svec.v_y()*svec.v_y() + 119 svec.v_z()*svec.v_z(); 120 if (debug) System.out.println( "ncart= " +ncart ); 121 double ncyl = svec.v_rxy()*svec.v_rxy() + 122 //FIXME this better didn't work until now. svec.v_phi()*svec.v_phi() + 123 svec.v_z()*svec.v_z(); 124 if (debug) System.out.println( "ncyl= " +ncyl ); 125 double nsph = svec.v_rxyz()*svec.v_rxyz(); 126 // FIXME Norman, please take a look at the Fixmes here 127 // FIXME this shouldn't have worked before + svec.v_theta()*svec.v_theta() + svec.v_phi()*svec.v_phi(); 128 if (debug) System.out.println( "nsph= " +nsph ); 129 assertTrue( myequal(ncart,n0) ); 130 assertTrue( myequal(ncyl,n0) ); 131 assertTrue( myequal(nsph,n0) ); 132 assertTrue( myequal( Math.sqrt(n0), svec.magnitude() ) ); 133 // 134 if (debug) System.out.println("Testing copy constructor and clone"); 135 SpacePointVector spv = (SpacePointVector) svec.clone(); 136 137 //********************************************************************** 138 139 if (debug) System.out.println( ok_prefix 140 + "------------- All tests passed. ------------" ); 141 } 142 143 }