1 package org.lcsim.spacegeom; 2 // SpacePointVecTest.cpp 3 import junit.framework.TestCase; 4 5 import org.lcsim.spacegeom.CartesianPointVector; 6 import org.lcsim.spacegeom.CylindricalPointVector; 7 import org.lcsim.spacegeom.SpacePointVector; 8 import org.lcsim.spacegeom.SphericalPointVector; 9 10 // Test the SpacePointVector class. 11 12 public class CartesianPointVectorTest 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 testCartesianPointVector() 25 { 26 27 String name = "CartesianPointVector"; 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().y(),y) ); 60 assertTrue( myequal(sph.getStartPoint().z(),z) ); 61 assertTrue( myequal(sph.v_x(),vx) ); 62 assertTrue( myequal(sph.v_y(),vy) ); 63 assertTrue( myequal(sph.v_z(),vz) ); 64 65 // Create a cartesian coordinates from spacepoint 66 CartesianPointVector cart2 = new CartesianPointVector( sph.getStartPoint(), sph.v_x(), sph.v_y(), sph.v_z() ); 67 if (debug) System.out.println( cart2 ); 68 69 // Create in cylindrical coordinates. 70 CylindricalPointVector cyl2 = new CylindricalPointVector( cart2.getStartPoint(), 71 cart2.v_rxy(), cart2.v_phi(), cart2.v_z() ); 72 if (debug) System.out.println( cyl2 ); 73 74 // Create in spherical coordinates. 75 SphericalPointVector sph2 = new SphericalPointVector( cyl2.getStartPoint(), 76 cyl2.v_rxyz(), cyl2.v_theta(), cyl2.v_phi() ); 77 if (debug) System.out.println( sph2 ); 78 79 assertTrue( myequal(sph2.getStartPoint().x(),x) ); 80 assertTrue( myequal(sph2.getStartPoint().y(),y) ); 81 assertTrue( myequal(sph2.getStartPoint().z(),z) ); 82 assertTrue( myequal(sph2.v_x(),vx) ); 83 assertTrue( myequal(sph2.v_y(),vy) ); 84 assertTrue( myequal(sph2.v_z(),vz) ); 85 86 //********************************************************************** 87 88 if (debug) System.out.println( ok_prefix + "Testing equality." ); 89 assertTrue( sph.equals(sph) ); 90 assertTrue( sph.equals(sph2) ); 91 if( sph.notEquals(sph2) ); 92 93 //********************************************************************** 94 95 if (debug) System.out.println( ok_prefix + "Testing assignment." ); 96 SpacePointVector svec = new SpacePointVector(); 97 if (debug) System.out.println( svec ); 98 if (debug) System.out.println( "testing assignment svec.magnitude() and 0 " + svec.magnitude() + " " + 0. ); 99 if (debug) System.out.println( "spacepoint equal " + (svec.magnitude()==0.0) ); 100 // Assert.assertTrue( SpacePoint.equal(svec.magnitude(),0.0) ); 101 svec = sph; 102 if (debug) System.out.println( svec ); 103 if (debug) System.out.println( "testing assignment svec.x() and x " + svec.getStartPoint().x() + " " + x ); 104 assertTrue( myequal(svec.getStartPoint().x(),x) ); 105 assertTrue( myequal(svec.getStartPoint().y(),y) ); 106 assertTrue( myequal(svec.getStartPoint().z(),z) ); 107 assertTrue( myequal(svec.v_x(),vx) ); 108 assertTrue( myequal(svec.v_y(),vy) ); 109 assertTrue( myequal(svec.v_z(),vz) ); 110 111 //********************************************************************** 112 113 if (debug) System.out.println( ok_prefix + "Testing normalizations." ); 114 double n0 = vx*vx + vy*vy + vz*vz; 115 if (debug) System.out.println( "n0= " +n0 ); 116 double ncart = svec.v_x()*svec.v_x() + 117 svec.v_y()*svec.v_y() + 118 svec.v_z()*svec.v_z(); 119 if (debug) System.out.println( "ncart= " +ncart ); 120 double ncyl = svec.v_rxy()*svec.v_rxy() + 121 // FIXME if this worked before, there is a serious problem svec.v_phi()*svec.v_phi() + 122 svec.v_z()*svec.v_z(); 123 if (debug) System.out.println( "ncyl= " +ncyl ); 124 double nsph = svec.v_rxyz()*svec.v_rxyz(); 125 // FIXME this might really indicate a problem + svec.v_theta()*svec.v_theta() + svec.v_phi()*svec.v_phi(); 126 if (debug) System.out.println( "nsph= " +nsph ); 127 assertTrue( myequal(ncart,n0) ); 128 assertTrue( myequal(ncyl,n0) ); 129 assertTrue( myequal(nsph,n0) ); 130 assertTrue( myequal( Math.sqrt(n0), svec.magnitude() ) ); 131 // 132 if (debug) System.out.println("Testing copy constructor and clone"); 133 SpacePointVector spv = (SpacePointVector) svec.clone(); 134 135 //********************************************************************** 136 137 if (debug) System.out.println( ok_prefix 138 + "------------- All tests passed. ------------" ); 139 } 140 141 }