| 1 | //
|
|---|
| 2 | // ********************************************************************
|
|---|
| 3 | // * License and Disclaimer *
|
|---|
| 4 | // * *
|
|---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of *
|
|---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and *
|
|---|
| 7 | // * conditions of the Geant4 Software License, included in the file *
|
|---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These *
|
|---|
| 9 | // * include a list of copyright holders. *
|
|---|
| 10 | // * *
|
|---|
| 11 | // * Neither the authors of this software system, nor their employing *
|
|---|
| 12 | // * institutes,nor the agencies providing financial support for this *
|
|---|
| 13 | // * work make any representation or warranty, express or implied, *
|
|---|
| 14 | // * regarding this software system or assume any liability for its *
|
|---|
| 15 | // * use. Please see the license in the file LICENSE and URL above *
|
|---|
| 16 | // * for the full disclaimer and the limitation of liability. *
|
|---|
| 17 | // * *
|
|---|
| 18 | // * This code implementation is the result of the scientific and *
|
|---|
| 19 | // * technical work of the GEANT4 collaboration. *
|
|---|
| 20 | // * By using, copying, modifying or distributing the software (or *
|
|---|
| 21 | // * any work based on the software) you agree to acknowledge its *
|
|---|
| 22 | // * use in resulting scientific publications, and indicate your *
|
|---|
| 23 | // * acceptance of all terms of the Geant4 Software license. *
|
|---|
| 24 | // ********************************************************************
|
|---|
| 25 | //
|
|---|
| 26 | //
|
|---|
| 27 | // $Id: testG4AffineTransform.cc,v 1.9 2006/06/29 18:58:27 gunter Exp $
|
|---|
| 28 | // GEANT4 tag $Name: geant4-09-04-beta-cand-01 $
|
|---|
| 29 | //
|
|---|
| 30 |
|
|---|
| 31 | #include <assert.h>
|
|---|
| 32 | #include "G4AffineTransform.hh"
|
|---|
| 33 | #include "G4ThreeVector.hh"
|
|---|
| 34 | #include "G4RotationMatrix.hh"
|
|---|
| 35 |
|
|---|
| 36 | #include "ApproxEqual.hh"
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | G4bool testG4AffineTransform()
|
|---|
| 40 | {
|
|---|
| 41 | G4ThreeVector zeroVec,xVec(1,0,0),xyzVec(1,1,1),xyzrotVec(-1,1,1);
|
|---|
| 42 | G4RotationMatrix identity,xRot;
|
|---|
| 43 | // NOTE: xRot = rotation such that x axis->y axis & y axis->-x axis
|
|---|
| 44 | xRot.rotateZ(-pi*0.5);
|
|---|
| 45 |
|
|---|
| 46 | G4AffineTransform origin;
|
|---|
| 47 | assert(origin.NetRotation()==identity);
|
|---|
| 48 | assert(origin.NetTranslation()==zeroVec);
|
|---|
| 49 | assert(!origin.IsRotated());
|
|---|
| 50 | assert(!origin.IsTranslated());
|
|---|
| 51 |
|
|---|
| 52 | G4AffineTransform rotTf(xRot);
|
|---|
| 53 | assert(rotTf.NetRotation()==xRot);
|
|---|
| 54 | assert(rotTf.NetTranslation()==zeroVec);
|
|---|
| 55 | assert(rotTf.IsRotated());
|
|---|
| 56 | assert(!rotTf.IsTranslated());
|
|---|
| 57 |
|
|---|
| 58 | G4AffineTransform txTf(xyzVec);
|
|---|
| 59 | assert(txTf.NetRotation()==identity);
|
|---|
| 60 | assert(txTf.NetTranslation()==xyzVec);
|
|---|
| 61 | assert(!txTf.IsRotated());
|
|---|
| 62 | assert(txTf.IsTranslated());
|
|---|
| 63 |
|
|---|
| 64 | G4AffineTransform rtTf(xRot,xyzVec);
|
|---|
| 65 | assert(rtTf.NetRotation()==xRot);
|
|---|
| 66 | assert(rtTf.NetTranslation()==xyzVec);
|
|---|
| 67 | assert(rtTf.IsRotated());
|
|---|
| 68 | assert(rtTf.IsTranslated());
|
|---|
| 69 |
|
|---|
| 70 | G4AffineTransform copyTf(rtTf);
|
|---|
| 71 | assert(copyTf==rtTf);
|
|---|
| 72 |
|
|---|
| 73 | G4AffineTransform compoundTf1=rotTf*txTf;
|
|---|
| 74 | assert(compoundTf1==rtTf);
|
|---|
| 75 |
|
|---|
| 76 | G4AffineTransform compoundTf2(rotTf);
|
|---|
| 77 | compoundTf2*=txTf;
|
|---|
| 78 | assert(compoundTf2==rtTf);
|
|---|
| 79 |
|
|---|
| 80 | G4AffineTransform compoundTf3;
|
|---|
| 81 | compoundTf3.Product(rotTf,txTf);
|
|---|
| 82 | assert(compoundTf3==rtTf);
|
|---|
| 83 |
|
|---|
| 84 | G4AffineTransform compoundTf4;
|
|---|
| 85 | compoundTf4.InverseProduct(rtTf,txTf);
|
|---|
| 86 | assert(ApproxEqual(compoundTf4,rotTf));
|
|---|
| 87 | compoundTf4.InverseProduct(rtTf,rtTf);
|
|---|
| 88 | assert(ApproxEqual(compoundTf4,identity));
|
|---|
| 89 |
|
|---|
| 90 | G4AffineTransform compoundTf5;
|
|---|
| 91 | compoundTf5.Product(rotTf,rtTf);
|
|---|
| 92 | G4AffineTransform compoundTf6;
|
|---|
| 93 | compoundTf6.InverseProduct(compoundTf5,rtTf);
|
|---|
| 94 | assert(ApproxEqual(compoundTf6,rotTf));
|
|---|
| 95 |
|
|---|
| 96 | assert(ApproxEqual(rotTf.TransformPoint(xyzVec),xyzrotVec));
|
|---|
| 97 | assert(ApproxEqual(rotTf.TransformAxis(xyzVec),xyzrotVec));
|
|---|
| 98 | assert(ApproxEqual(txTf.TransformPoint(xyzVec),G4ThreeVector(2,2,2)));
|
|---|
| 99 | assert(txTf.TransformAxis(xyzVec)==xyzVec);
|
|---|
| 100 | assert(ApproxEqual(rtTf.TransformPoint(xVec),G4ThreeVector(1,2,1)));
|
|---|
| 101 | assert(ApproxEqual(rtTf.TransformAxis(xVec),G4ThreeVector(0,1,0)));
|
|---|
| 102 |
|
|---|
| 103 | G4ThreeVector vec(0,0,1);
|
|---|
| 104 | rtTf.ApplyPointTransform(vec);
|
|---|
| 105 | assert(ApproxEqual(vec,G4ThreeVector(1,1,2)));
|
|---|
| 106 | vec=G4ThreeVector(-1,2,-3);
|
|---|
| 107 | rtTf.ApplyAxisTransform(vec);
|
|---|
| 108 | assert(ApproxEqual(vec,G4ThreeVector(-2,-1,-3)));
|
|---|
| 109 | rtTf.ApplyPointTransform(vec);
|
|---|
| 110 | assert(ApproxEqual(vec,G4ThreeVector(2,-1,-2)));
|
|---|
| 111 |
|
|---|
| 112 | G4AffineTransform invTf=rtTf.Inverse();
|
|---|
| 113 |
|
|---|
| 114 | #if 0
|
|---|
| 115 | G4ThreeVector forwV= rtTf.TransformPoint(xyzVec);
|
|---|
| 116 | G4ThreeVector backV= invTf.TransformPoint(forwV);
|
|---|
| 117 |
|
|---|
| 118 | G4ThreeVector diffV= xyzVec - backV;
|
|---|
| 119 | G4cout << " Diff of xyzVec and backV is " << diffV << G4endl;
|
|---|
| 120 | #endif
|
|---|
| 121 | assert(ApproxEqual(invTf.TransformPoint(rtTf.TransformPoint(xyzVec)),
|
|---|
| 122 | xyzVec));
|
|---|
| 123 |
|
|---|
| 124 | invTf*=rtTf;
|
|---|
| 125 | // Might need tolerant checking:
|
|---|
| 126 | assert(ApproxEqual(invTf,origin));
|
|---|
| 127 |
|
|---|
| 128 | invTf=rtTf;
|
|---|
| 129 | invTf.Invert();
|
|---|
| 130 |
|
|---|
| 131 | G4double MaxAbsDiff(const G4AffineTransform &tf1,
|
|---|
| 132 | const G4AffineTransform &tf2);
|
|---|
| 133 | G4double maxabsdiff= MaxAbsDiff( invTf, rtTf.Inverse() );
|
|---|
| 134 | G4cout << "Max difference is " << maxabsdiff << G4endl;
|
|---|
| 135 | assert( maxabsdiff <= 1.e-12 );
|
|---|
| 136 |
|
|---|
| 137 | G4AffineTransform rtTf_inv=rtTf.Inverse();
|
|---|
| 138 | assert(MaxAbsDiff( invTf, rtTf_inv) <= 1.e-12 );
|
|---|
| 139 | #if 0
|
|---|
| 140 | assert(invTf==rtTf_inv);
|
|---|
| 141 |
|
|---|
| 142 | assert(invTf==rtTf_inv);
|
|---|
| 143 | #endif
|
|---|
| 144 |
|
|---|
| 145 | G4AffineTransform txTf2(xyzVec);
|
|---|
| 146 | txTf2+=xyzVec;
|
|---|
| 147 | assert(txTf2.NetRotation()==identity);
|
|---|
| 148 | assert(ApproxEqual(txTf2.NetTranslation(),xyzVec*2));
|
|---|
| 149 | assert(txTf2!=txTf);
|
|---|
| 150 | txTf2-=xyzVec;
|
|---|
| 151 | assert(txTf2.NetRotation()==identity);
|
|---|
| 152 | assert(ApproxEqual(txTf2.NetTranslation(),xyzVec));
|
|---|
| 153 | assert(ApproxEqual(txTf2,txTf));
|
|---|
| 154 | txTf2.SetNetRotation(xRot);
|
|---|
| 155 | assert(txTf2.NetRotation()==xRot);
|
|---|
| 156 | txTf2.SetNetTranslation(xyzVec*3);
|
|---|
| 157 | assert(txTf2.NetTranslation()==xyzVec*3);
|
|---|
| 158 |
|
|---|
| 159 | return true;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | int main()
|
|---|
| 163 | {
|
|---|
| 164 |
|
|---|
| 165 | #ifdef NDEBUG
|
|---|
| 166 | G4Exception("FAIL: *** Assertions must be compiled in! ***");
|
|---|
| 167 | #endif
|
|---|
| 168 | assert(testG4AffineTransform());
|
|---|
| 169 | return 0;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | G4double MaxAbsDiff(const G4AffineTransform &tf1,
|
|---|
| 173 | const G4AffineTransform &tf2)
|
|---|
| 174 | {
|
|---|
| 175 | G4double maxabs= 0.0;
|
|---|
| 176 | for (G4int i=0;i<15;i++)
|
|---|
| 177 | {
|
|---|
| 178 | G4double absdiff;
|
|---|
| 179 |
|
|---|
| 180 | absdiff= std::fabs(tf1[i]-tf2[i]);
|
|---|
| 181 | maxabs= std::max(absdiff, maxabs);
|
|---|
| 182 | }
|
|---|
| 183 | return maxabs;
|
|---|
| 184 | }
|
|---|