1 package org.lcsim.spacegeom; 2 // SpacePointVecTest.cpp 3 4 import junit.framework.TestCase; 5 6 import org.lcsim.spacegeom.CartesianPointVector; 7 import org.lcsim.spacegeom.CylindricalPointVector; 8 import org.lcsim.spacegeom.SpacePointVector; 9 import org.lcsim.spacegeom.SphericalPointVector; 10 11 // Test the SpacePointVector class. 12 13 public class CylindricalPointVectorTest extends TestCase 14 { 15 boolean debug = false; 16 //********************************************************************** 17 18 public static boolean myequal(double x1, double x2) 19 { 20 return Math.abs(x2-x1) < 1.e-10; 21 } 22 23 //********************************************************************** 24 25 public void testCylindricalPoint() 26 { 27 28 String name = "CylindricalPointVector"; 29 String ok_prefix = name + " test (I): "; 30 String error_prefix = name + " test (E): "; 31 32 if (debug) System.out.println( ok_prefix 33 + "------- Testing component " + name + ". -------" ); 34 35 double x = 1.23; 36 double y = 2.46; 37 double z = 3.69; 38 double vx = 0.23; 39 double vy = 0.45; 40 double vz = 0.67; 41 42 //********************************************************************** 43 44 if (debug) System.out.println( ok_prefix + "Testing constructors." ); 45 // Create a spacevector 46 CartesianPointVector cart = new CartesianPointVector(x,y,z,vx,vy,vz); 47 if (debug) System.out.println( cart ); 48 49 // Create in cylindrical coordinates. 50 CylindricalPointVector cyl = new CylindricalPointVector( cart.getStartPoint().rxy(), cart.getStartPoint().phi(), cart.getStartPoint().z(), 51 cart.v_rxy(), cart.v_phi(), cart.v_z() ); 52 if (debug) System.out.println( cyl ); 53 54 // Create in spherical coordinates. 55 SphericalPointVector sph = new SphericalPointVector( cyl.getStartPoint().rxyz(), cyl.getStartPoint().phi(), cyl.getStartPoint().theta(), 56 cyl.v_rxyz(), cyl.v_theta(), cyl.v_phi() ); 57 if (debug) System.out.println( sph ); 58 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 isn't supposed to be here 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 this shouldn't have worked before + svec.v_theta()*svec.v_theta() + svec.v_phi()*svec.v_phi(); 127 if (debug) System.out.println( "nsph= " +nsph ); 128 assertTrue( myequal(ncart,n0) ); 129 assertTrue( myequal(ncyl,n0) ); 130 assertTrue( myequal(nsph,n0) ); 131 assertTrue( myequal( Math.sqrt(n0), svec.magnitude() ) ); 132 // 133 if (debug) System.out.println("Testing copy constructor and clone"); 134 SpacePointVector spv = (SpacePointVector) svec.clone(); 135 136 //********************************************************************** 137 138 if (debug) System.out.println( ok_prefix 139 + "------------- All tests passed. ------------" ); 140 } 141 142 }