[831] | 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 and of QinetiQ Ltd, * |
---|
| 20 | // * subject to DEFCON 705 IPR conditions. * |
---|
| 21 | // * By using, copying, modifying or distributing the software (or * |
---|
| 22 | // * any work based on the software) you agree to acknowledge its * |
---|
| 23 | // * use in resulting scientific publications, and indicate your * |
---|
| 24 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 25 | // ******************************************************************** |
---|
| 26 | // |
---|
[921] | 27 | // $Id: G4TriangularFacet.cc,v 1.12 2008/11/13 08:25:07 gcosmo Exp $ |
---|
| 28 | // GEANT4 tag $Name: geant4-09-02-cand-01 $ |
---|
[831] | 29 | // |
---|
| 30 | // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 31 | // |
---|
| 32 | // MODULE: G4TriangularFacet.cc |
---|
| 33 | // |
---|
| 34 | // Date: 15/06/2005 |
---|
| 35 | // Author: P R Truscott |
---|
| 36 | // Organisation: QinetiQ Ltd, UK |
---|
| 37 | // Customer: UK Ministry of Defence : RAO CRP TD Electronic Systems |
---|
| 38 | // Contract: C/MAT/N03517 |
---|
| 39 | // |
---|
| 40 | // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 41 | // |
---|
| 42 | // CHANGE HISTORY |
---|
| 43 | // -------------- |
---|
| 44 | // |
---|
| 45 | // 31 October 2004, P R Truscott, QinetiQ Ltd, UK - Created. |
---|
| 46 | // |
---|
| 47 | // 01 August 2007 P R Truscott, QinetiQ Ltd, UK |
---|
| 48 | // Significant modification to correct for errors and enhance |
---|
| 49 | // based on patches/observations kindly provided by Rickard |
---|
| 50 | // Holmberg |
---|
| 51 | // |
---|
| 52 | // 26 September 2007 |
---|
| 53 | // P R Truscott, QinetiQ Ltd, UK |
---|
| 54 | // Further chamges implemented to the Intersect member |
---|
| 55 | // function to correctly treat rays nearly parallel to the |
---|
| 56 | // plane of the triangle. |
---|
| 57 | // |
---|
| 58 | // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 59 | |
---|
| 60 | #include "G4TriangularFacet.hh" |
---|
| 61 | #include "G4TwoVector.hh" |
---|
| 62 | #include "globals.hh" |
---|
| 63 | #include "Randomize.hh" |
---|
| 64 | |
---|
| 65 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 66 | // |
---|
| 67 | // Definition of triangular facet using absolute vectors to vertices. |
---|
| 68 | // From this for first vector is retained to define the facet location and |
---|
| 69 | // two relative vectors (E0 and E1) define the sides and orientation of |
---|
| 70 | // the outward surface normal. |
---|
| 71 | // |
---|
| 72 | G4TriangularFacet::G4TriangularFacet (const G4ThreeVector Pt0, |
---|
| 73 | const G4ThreeVector vt1, const G4ThreeVector vt2, |
---|
| 74 | G4FacetVertexType vertexType) |
---|
| 75 | : G4VFacet() |
---|
| 76 | { |
---|
[921] | 77 | tGeomAlg = G4TessellatedGeometryAlgorithms::GetInstance(); |
---|
[831] | 78 | P0 = Pt0; |
---|
| 79 | nVertices = 3; |
---|
| 80 | if (vertexType == ABSOLUTE) |
---|
| 81 | { |
---|
| 82 | P.push_back(vt1); |
---|
| 83 | P.push_back(vt2); |
---|
| 84 | |
---|
| 85 | E.push_back(vt1 - P0); |
---|
| 86 | E.push_back(vt2 - P0); |
---|
| 87 | } |
---|
| 88 | else |
---|
| 89 | { |
---|
| 90 | P.push_back(P0 + vt1); |
---|
| 91 | P.push_back(P0 + vt2); |
---|
| 92 | |
---|
| 93 | E.push_back(vt1); |
---|
| 94 | E.push_back(vt2); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | G4double Emag1 = E[0].mag(); |
---|
| 98 | G4double Emag2 = E[1].mag(); |
---|
| 99 | G4double Emag3 = (E[1]-E[0]).mag(); |
---|
| 100 | |
---|
| 101 | if (Emag1 <= kCarTolerance || Emag2 <= kCarTolerance || |
---|
| 102 | Emag3 <= kCarTolerance) |
---|
| 103 | { |
---|
| 104 | G4Exception("G4TriangularFacet::G4TriangularFacet()", "InvalidSetup", |
---|
| 105 | JustWarning, "Length of sides of facet are too small."); |
---|
| 106 | G4cerr << G4endl; |
---|
| 107 | G4cerr << "P0 = " << P0 << G4endl; |
---|
| 108 | G4cerr << "P1 = " << P[0] << G4endl; |
---|
| 109 | G4cerr << "P2 = " << P[1] << G4endl; |
---|
| 110 | G4cerr << "Side lengths = P0->P1" << Emag1 << G4endl; |
---|
| 111 | G4cerr << "Side lengths = P0->P2" << Emag2 << G4endl; |
---|
| 112 | G4cerr << "Side lengths = P1->P2" << Emag3 << G4endl; |
---|
| 113 | G4cerr << G4endl; |
---|
| 114 | |
---|
| 115 | isDefined = false; |
---|
| 116 | geometryType = "G4TriangularFacet"; |
---|
| 117 | surfaceNormal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 118 | a = 0.0; |
---|
| 119 | b = 0.0; |
---|
| 120 | c = 0.0; |
---|
| 121 | det = 0.0; |
---|
| 122 | } |
---|
| 123 | else |
---|
| 124 | { |
---|
| 125 | isDefined = true; |
---|
| 126 | geometryType = "G4TriangularFacet"; |
---|
| 127 | surfaceNormal = E[0].cross(E[1]).unit(); |
---|
| 128 | a = E[0].mag2(); |
---|
| 129 | b = E[0].dot(E[1]); |
---|
| 130 | c = E[1].mag2(); |
---|
| 131 | det = std::abs(a*c - b*b); |
---|
| 132 | |
---|
| 133 | sMin = -0.5*kCarTolerance/std::sqrt(a); |
---|
| 134 | sMax = 1.0 - sMin; |
---|
| 135 | tMin = -0.5*kCarTolerance/std::sqrt(c); |
---|
| 136 | |
---|
| 137 | area = 0.5 * (E[0].cross(E[1])).mag(); |
---|
| 138 | |
---|
| 139 | // G4ThreeVector vtmp = 0.25 * (E[0] + E[1]); |
---|
| 140 | G4double lambda0 = (a-b) * c / (8.0*area*area); |
---|
| 141 | G4double lambda1 = (c-b) * a / (8.0*area*area); |
---|
| 142 | circumcentre = P0 + lambda0*E[0] + lambda1*E[1]; |
---|
| 143 | radiusSqr = (circumcentre-P0).mag2(); |
---|
| 144 | radius = std::sqrt(radiusSqr); |
---|
| 145 | |
---|
| 146 | for (size_t i=0; i<3; i++) { I.push_back(0); } |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 151 | // |
---|
| 152 | // ~G4TriangularFacet |
---|
| 153 | // |
---|
| 154 | // A pretty boring destructor indeed! |
---|
| 155 | // |
---|
| 156 | G4TriangularFacet::~G4TriangularFacet () |
---|
| 157 | { |
---|
| 158 | P.clear(); |
---|
| 159 | E.clear(); |
---|
| 160 | I.clear(); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 164 | // |
---|
| 165 | // GetClone |
---|
| 166 | // |
---|
| 167 | // Simple member function to generate a diplicate of the triangular facet. |
---|
| 168 | // |
---|
| 169 | G4VFacet *G4TriangularFacet::GetClone () |
---|
| 170 | { |
---|
| 171 | G4TriangularFacet *fc = new G4TriangularFacet (P0, P[0], P[1], ABSOLUTE); |
---|
| 172 | G4VFacet *cc = 0; |
---|
| 173 | cc = fc; |
---|
| 174 | return cc; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 178 | // |
---|
| 179 | // GetFlippedFacet |
---|
| 180 | // |
---|
| 181 | // Member function to generate an identical facet, but with the normal vector |
---|
| 182 | // pointing at 180 degrees. |
---|
| 183 | // |
---|
| 184 | G4TriangularFacet *G4TriangularFacet::GetFlippedFacet () |
---|
| 185 | { |
---|
| 186 | G4TriangularFacet *flipped = new G4TriangularFacet (P0, P[1], P[0], ABSOLUTE); |
---|
| 187 | return flipped; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 191 | // |
---|
| 192 | // Distance (G4ThreeVector) |
---|
| 193 | // |
---|
| 194 | // Determines the vector between p and the closest point on the facet to p. |
---|
| 195 | // This is based on the algorithm published in "Geometric Tools for Computer |
---|
| 196 | // Graphics," Philip J Scheider and David H Eberly, Elsevier Science (USA), |
---|
| 197 | // 2003. at the time of writing, the algorithm is also available in a |
---|
| 198 | // technical note "Distance between point and triangle in 3D," by David Eberly |
---|
| 199 | // at http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf |
---|
| 200 | // |
---|
| 201 | // The by-product is the square-distance sqrDist, which is retained |
---|
| 202 | // in case needed by the other "Distance" member functions. |
---|
| 203 | // |
---|
| 204 | G4ThreeVector G4TriangularFacet::Distance (const G4ThreeVector &p) |
---|
| 205 | { |
---|
| 206 | G4ThreeVector D = P0 - p; |
---|
| 207 | G4double d = E[0].dot(D); |
---|
| 208 | G4double e = E[1].dot(D); |
---|
| 209 | G4double f = D.mag2(); |
---|
| 210 | G4double s = b*e - c*d; |
---|
| 211 | G4double t = b*d - a*e; |
---|
| 212 | |
---|
| 213 | sqrDist = 0.0; |
---|
| 214 | |
---|
| 215 | if (s+t <= det) |
---|
| 216 | { |
---|
| 217 | if (s < 0.0) |
---|
| 218 | { |
---|
| 219 | if (t < 0.0) |
---|
| 220 | { |
---|
| 221 | // |
---|
| 222 | // We are in region 4. |
---|
| 223 | // |
---|
| 224 | if (d < 0.0) |
---|
| 225 | { |
---|
| 226 | t = 0.0; |
---|
| 227 | if (-d >= a) {s = 1.0; sqrDist = a + 2.0*d + f;} |
---|
| 228 | else {s = -d/a; sqrDist = d*s + f;} |
---|
| 229 | } |
---|
| 230 | else |
---|
| 231 | { |
---|
| 232 | s = 0.0; |
---|
| 233 | if (e >= 0.0) {t = 0.0; sqrDist = f;} |
---|
| 234 | else if (-e >= c) {t = 1.0; sqrDist = c + 2.0*e + f;} |
---|
| 235 | else {t = -e/c; sqrDist = e*t + f;} |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | else |
---|
| 239 | { |
---|
| 240 | // |
---|
| 241 | // We are in region 3. |
---|
| 242 | // |
---|
| 243 | s = 0.0; |
---|
| 244 | if (e >= 0.0) {t = 0.0; sqrDist = f;} |
---|
| 245 | else if (-e >= c) {t = 1.0; sqrDist = c + 2.0*e + f;} |
---|
| 246 | else {t = -e/c; sqrDist = e*t + f;} |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | else if (t < 0.0) |
---|
| 250 | { |
---|
| 251 | // |
---|
| 252 | // We are in region 5. |
---|
| 253 | // |
---|
| 254 | t = 0.0; |
---|
| 255 | if (d >= 0.0) {s = 0.0; sqrDist = f;} |
---|
| 256 | else if (-d >= a) {s = 1.0; sqrDist = a + 2.0*d + f;} |
---|
| 257 | else {s = -d/a; sqrDist = d*s + f;} |
---|
| 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
| 261 | // |
---|
| 262 | // We are in region 0. |
---|
| 263 | // |
---|
| 264 | s = s / det; |
---|
| 265 | t = t / det; |
---|
| 266 | sqrDist = s*(a*s + b*t + 2.0*d) + t*(b*s + c*t + 2.0*e) + f; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | else |
---|
| 270 | { |
---|
| 271 | if (s < 0.0) |
---|
| 272 | { |
---|
| 273 | // |
---|
| 274 | // We are in region 2. |
---|
| 275 | // |
---|
| 276 | G4double tmp0 = b + d; |
---|
| 277 | G4double tmp1 = c + e; |
---|
| 278 | if (tmp1 > tmp0) |
---|
| 279 | { |
---|
| 280 | G4double numer = tmp1 - tmp0; |
---|
| 281 | G4double denom = a - 2.0*b + c; |
---|
| 282 | if (numer >= denom) {s = 1.0; t = 0.0; sqrDist = a + 2.0*d + f;} |
---|
| 283 | else |
---|
| 284 | { |
---|
| 285 | s = numer/denom; |
---|
| 286 | t = 1.0 - s; |
---|
| 287 | sqrDist = s*(a*s + b*t +2.0*d) + t*(b*s + c*t + 2.0*e) + f; |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | else |
---|
| 291 | { |
---|
| 292 | s = 0.0; |
---|
| 293 | if (tmp1 <= 0.0) {t = 1.0; sqrDist = c + 2.0*e + f;} |
---|
| 294 | else if (e >= 0.0) {t = 0.0; sqrDist = f;} |
---|
| 295 | else {t = -e/c; sqrDist = e*t + f;} |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | else if (t < 0.0) |
---|
| 299 | { |
---|
| 300 | // |
---|
| 301 | // We are in region 6. |
---|
| 302 | // |
---|
| 303 | G4double tmp0 = b + e; |
---|
| 304 | G4double tmp1 = a + d; |
---|
| 305 | if (tmp1 > tmp0) |
---|
| 306 | { |
---|
| 307 | G4double numer = tmp1 - tmp0; |
---|
| 308 | G4double denom = a - 2.0*b + c; |
---|
| 309 | if (numer >= denom) {t = 1.0; s = 0.0; sqrDist = c + 2.0*e + f;} |
---|
| 310 | else |
---|
| 311 | { |
---|
| 312 | t = numer/denom; |
---|
| 313 | s = 1.0 - t; |
---|
| 314 | sqrDist = s*(a*s + b*t +2.0*d) + t*(b*s + c*t + 2.0*e) + f; |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | else |
---|
| 318 | { |
---|
| 319 | t = 0.0; |
---|
| 320 | if (tmp1 <= 0.0) {s = 1.0; sqrDist = a + 2.0*d + f;} |
---|
| 321 | else if (d >= 0.0) {s = 0.0; sqrDist = f;} |
---|
| 322 | else {s = -d/a; sqrDist = d*s + f;} |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | else |
---|
| 326 | // |
---|
| 327 | // We are in region 1. |
---|
| 328 | // |
---|
| 329 | { |
---|
| 330 | G4double numer = c + e - b - d; |
---|
| 331 | if (numer <= 0.0) |
---|
| 332 | { |
---|
| 333 | s = 0.0; |
---|
| 334 | t = 1.0; |
---|
| 335 | sqrDist = c + 2.0*e + f; |
---|
| 336 | } |
---|
| 337 | else |
---|
| 338 | { |
---|
| 339 | G4double denom = a - 2.0*b + c; |
---|
| 340 | if (numer >= denom) {s = 1.0; t = 0.0; sqrDist = a + 2.0*d + f;} |
---|
| 341 | else |
---|
| 342 | { |
---|
| 343 | s = numer/denom; |
---|
| 344 | t = 1.0 - s; |
---|
| 345 | sqrDist = s*(a*s + b*t + 2.0*d) + t*(b*s + c*t + 2.0*e) + f; |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | // |
---|
| 351 | // |
---|
| 352 | // Do a heck for rounding errors in the distance-squared. |
---|
| 353 | // |
---|
| 354 | if (sqrDist < 0.0) { sqrDist = 0.0; } |
---|
| 355 | |
---|
| 356 | return D + s*E[0] + t*E[1]; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 360 | // |
---|
| 361 | // Distance (G4ThreeVector, G4double) |
---|
| 362 | // |
---|
| 363 | // Determines the closest distance between point p and the facet. This makes |
---|
| 364 | // use of G4ThreeVector G4TriangularFacet::Distance, which stores the |
---|
| 365 | // square of the distance in variable sqrDist. If approximate methods show |
---|
| 366 | // the distance is to be greater than minDist, then forget about further |
---|
| 367 | // computation and return a very large number. |
---|
| 368 | // |
---|
| 369 | G4double G4TriangularFacet::Distance (const G4ThreeVector &p, |
---|
| 370 | const G4double minDist) |
---|
| 371 | { |
---|
| 372 | // |
---|
| 373 | // |
---|
| 374 | // Start with quicky test to determine if the surface of the sphere enclosing |
---|
| 375 | // the triangle is any closer to p than minDist. If not, then don't bother |
---|
| 376 | // about more accurate test. |
---|
| 377 | // |
---|
| 378 | G4double dist = kInfinity; |
---|
| 379 | if ((p-circumcentre).mag()-radius < minDist) |
---|
| 380 | { |
---|
| 381 | // |
---|
| 382 | // |
---|
| 383 | // It's possible that the triangle is closer than minDist, so do more accurate |
---|
| 384 | // assessment. |
---|
| 385 | // |
---|
| 386 | dist = Distance(p).mag(); |
---|
| 387 | // dist = std::sqrt(sqrDist); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | return dist; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 394 | // |
---|
| 395 | // Distance (G4ThreeVector, G4double, G4double) |
---|
| 396 | // |
---|
| 397 | // Determine the distance to point p. kInfinity is returned if either: |
---|
| 398 | // (1) outgoing is TRUE and the dot product of the normal vector to the facet |
---|
| 399 | // and the displacement vector from p to the triangle is negative. |
---|
| 400 | // (2) outgoing is FALSE and the dot product of the normal vector to the facet |
---|
| 401 | // and the displacement vector from p to the triangle is positive. |
---|
| 402 | // If approximate methods show the distance is to be greater than minDist, then |
---|
| 403 | // forget about further computation and return a very large number. |
---|
| 404 | // |
---|
| 405 | // This method has been heavily modified thanks to the valuable comments and |
---|
| 406 | // corrections of Rickard Holmberg. |
---|
| 407 | // |
---|
| 408 | G4double G4TriangularFacet::Distance (const G4ThreeVector &p, |
---|
| 409 | const G4double minDist, const G4bool outgoing) |
---|
| 410 | { |
---|
| 411 | // |
---|
| 412 | // |
---|
| 413 | // Start with quicky test to determine if the surface of the sphere enclosing |
---|
| 414 | // the triangle is any closer to p than minDist. If not, then don't bother |
---|
| 415 | // about more accurate test. |
---|
| 416 | // |
---|
| 417 | G4double dist = kInfinity; |
---|
| 418 | if ((p-circumcentre).mag()-radius < minDist) |
---|
| 419 | { |
---|
| 420 | // |
---|
| 421 | // |
---|
| 422 | // It's possible that the triangle is closer than minDist, so do more accurate |
---|
| 423 | // assessment. |
---|
| 424 | // |
---|
| 425 | G4ThreeVector v = Distance(p); |
---|
| 426 | G4double dist1 = std::sqrt(sqrDist); |
---|
| 427 | G4double dir = v.dot(surfaceNormal); |
---|
| 428 | G4bool wrongSide = (dir > 0.0 && !outgoing) || (dir < 0.0 && outgoing); |
---|
| 429 | if (dist1 <= kCarTolerance*0.5) |
---|
| 430 | { |
---|
| 431 | // |
---|
| 432 | // |
---|
| 433 | // Point p is very close to triangle. Check if it's on the wrong side, in |
---|
| 434 | // which case return distance of 0.0 otherwise . |
---|
| 435 | // |
---|
| 436 | if (wrongSide) dist = 0.0; |
---|
| 437 | else dist = dist1; |
---|
| 438 | } |
---|
| 439 | else if (!wrongSide) dist = dist1; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | return dist; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 446 | // |
---|
| 447 | // Extent |
---|
| 448 | // |
---|
| 449 | // Calculates the furthest the triangle extends in a particular direction |
---|
| 450 | // defined by the vector axis. |
---|
| 451 | // |
---|
| 452 | G4double G4TriangularFacet::Extent (const G4ThreeVector axis) |
---|
| 453 | { |
---|
| 454 | G4double s = P0.dot(axis); |
---|
| 455 | G4double sp = P[0].dot(axis); |
---|
| 456 | if (sp > s) s = sp; |
---|
| 457 | sp = P[1].dot(axis); |
---|
| 458 | if (sp > s) s = sp; |
---|
| 459 | |
---|
| 460 | return s; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 464 | // |
---|
| 465 | // Intersect |
---|
| 466 | // |
---|
| 467 | // Member function to find the next intersection when going from p in the |
---|
| 468 | // direction of v. If: |
---|
| 469 | // (1) "outgoing" is TRUE, only consider the face if we are going out through |
---|
| 470 | // the face. |
---|
| 471 | // (2) "outgoing" is FALSE, only consider the face if we are going in through |
---|
| 472 | // the face. |
---|
| 473 | // Member functions returns TRUE if there is an intersection, FALSE otherwise. |
---|
| 474 | // Sets the distance (distance along w), distFromSurface (orthogonal distance) |
---|
| 475 | // and normal. |
---|
| 476 | // |
---|
| 477 | // Also considers intersections that happen with negative distance for small |
---|
| 478 | // distances of distFromSurface = 0.5*kCarTolerance in the wrong direction. |
---|
| 479 | // This is to detect kSurface without doing a full Inside(p) in |
---|
| 480 | // G4TessellatedSolid::Distance(p,v) calculation. |
---|
| 481 | // |
---|
| 482 | // This member function is thanks the valuable work of Rickard Holmberg. PT. |
---|
| 483 | // However, "gotos" are the Work of the Devil have been exorcised with |
---|
| 484 | // extreme prejudice!! |
---|
| 485 | // |
---|
| 486 | // IMPORTANT NOTE: These calculations are predicated on v being a unit |
---|
| 487 | // vector. If G4TessellatedSolid or other classes call this member function |
---|
| 488 | // with |v| != 1 then there will be errors. |
---|
| 489 | // |
---|
| 490 | G4bool G4TriangularFacet::Intersect (const G4ThreeVector &p, |
---|
| 491 | const G4ThreeVector &v, G4bool outgoing, G4double &distance, |
---|
| 492 | G4double &distFromSurface, G4ThreeVector &normal) |
---|
| 493 | { |
---|
| 494 | // |
---|
| 495 | // |
---|
| 496 | // Check whether the direction of the facet is consistent with the vector v |
---|
| 497 | // and the need to be outgoing or ingoing. If inconsistent, disregard and |
---|
| 498 | // return false. |
---|
| 499 | // |
---|
| 500 | G4double w = v.dot(surfaceNormal); |
---|
| 501 | if ((outgoing && (w <-dirTolerance)) || (!outgoing && (w > dirTolerance))) |
---|
| 502 | { |
---|
| 503 | distance = kInfinity; |
---|
| 504 | distFromSurface = kInfinity; |
---|
| 505 | normal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 506 | return false; |
---|
| 507 | } |
---|
| 508 | // |
---|
| 509 | // |
---|
| 510 | // Calculate the orthogonal distance from p to the surface containing the |
---|
| 511 | // triangle. Then determine if we're on the right or wrong side of the |
---|
| 512 | // surface (at a distance greater than kCarTolerance) to be consistent with |
---|
| 513 | // "outgoing". |
---|
| 514 | // |
---|
| 515 | G4ThreeVector D = P0 - p; |
---|
| 516 | distFromSurface = D.dot(surfaceNormal); |
---|
| 517 | G4bool wrongSide = (outgoing && (distFromSurface < -0.5*kCarTolerance)) || |
---|
| 518 | (!outgoing && (distFromSurface > 0.5*kCarTolerance)); |
---|
| 519 | if (wrongSide) |
---|
| 520 | { |
---|
| 521 | distance = kInfinity; |
---|
| 522 | distFromSurface = kInfinity; |
---|
| 523 | normal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 524 | return false; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | wrongSide = (outgoing && (distFromSurface < 0.0)) || |
---|
| 528 | (!outgoing && (distFromSurface > 0.0)); |
---|
| 529 | if (wrongSide) |
---|
| 530 | { |
---|
| 531 | // |
---|
| 532 | // |
---|
| 533 | // We're slightly on the wrong side of the surface. Check if we're close |
---|
| 534 | // enough using a precise distance calculation. |
---|
| 535 | // |
---|
| 536 | G4ThreeVector u = Distance(p); |
---|
| 537 | if (std::sqrt(sqrDist) <= 0.5*kCarTolerance) |
---|
| 538 | { |
---|
| 539 | // |
---|
| 540 | // |
---|
| 541 | // We're very close. Therefore return a small negative number to pretend |
---|
| 542 | // we intersect. |
---|
| 543 | // |
---|
| 544 | distance = -0.5*kCarTolerance; |
---|
| 545 | normal = surfaceNormal; |
---|
| 546 | return true; |
---|
| 547 | } |
---|
| 548 | else |
---|
| 549 | { |
---|
| 550 | // |
---|
| 551 | // |
---|
| 552 | // We're close to the surface containing the triangle, but sufficiently |
---|
| 553 | // far from the triangle, and on the wrong side compared to the directions |
---|
| 554 | // of the surface normal and v. There is no intersection. |
---|
| 555 | // |
---|
| 556 | distance = kInfinity; |
---|
| 557 | distFromSurface = kInfinity; |
---|
| 558 | normal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 559 | return false; |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | if (w < dirTolerance && w > -dirTolerance) |
---|
| 563 | { |
---|
| 564 | // |
---|
| 565 | // |
---|
| 566 | // The ray is within the plane of the triangle. Project the problem into 2D |
---|
| 567 | // in the plane of the triangle. First try to create orthogonal unit vectors |
---|
| 568 | // mu and nu, where mu is E[0]/|E[0]|. This is kinda like |
---|
| 569 | // the original algorithm due to Rickard Holmberg, but with better mathematical |
---|
| 570 | // justification than the original method ... however, beware Rickard's was less |
---|
| 571 | // time-consuming. |
---|
| 572 | // |
---|
| 573 | // Note that vprime is not a unit vector. We need to keep it unnormalised |
---|
| 574 | // since the values of distance along vprime (s0 and s1) for intersection with |
---|
| 575 | // the triangle will be used to determine if we cut the plane at the same |
---|
| 576 | // time. |
---|
| 577 | // |
---|
| 578 | G4ThreeVector mu = E[0].unit(); |
---|
| 579 | G4ThreeVector nu = surfaceNormal.cross(mu); |
---|
| 580 | G4TwoVector pprime(p.dot(mu),p.dot(nu)); |
---|
| 581 | G4TwoVector vprime(v.dot(mu),v.dot(nu)); |
---|
| 582 | G4TwoVector P0prime(P0.dot(mu),P0.dot(nu)); |
---|
| 583 | G4TwoVector E0prime(E[0].mag(),0.0); |
---|
| 584 | G4TwoVector E1prime(E[1].dot(mu),E[1].dot(nu)); |
---|
| 585 | |
---|
| 586 | G4TwoVector loc[2]; |
---|
| 587 | if ( tGeomAlg->IntersectLineAndTriangle2D(pprime,vprime,P0prime, |
---|
| 588 | E0prime,E1prime,loc) ) |
---|
| 589 | { |
---|
| 590 | // |
---|
| 591 | // |
---|
| 592 | // There is an intersection between the line and triangle in 2D. Now check |
---|
| 593 | // which part of the line intersects with the plane containing the triangle |
---|
| 594 | // in 3D. |
---|
| 595 | // |
---|
| 596 | G4double vprimemag = vprime.mag(); |
---|
| 597 | G4double s0 = (loc[0] - pprime).mag()/vprimemag; |
---|
| 598 | G4double s1 = (loc[1] - pprime).mag()/vprimemag; |
---|
| 599 | G4double normDist0 = surfaceNormal.dot(s0*v) - distFromSurface; |
---|
| 600 | G4double normDist1 = surfaceNormal.dot(s1*v) - distFromSurface; |
---|
| 601 | |
---|
| 602 | if ((normDist0 < 0.0 && normDist1 < 0.0) || |
---|
| 603 | (normDist0 > 0.0 && normDist1 > 0.0)) |
---|
| 604 | { |
---|
| 605 | distance = kInfinity; |
---|
| 606 | distFromSurface = kInfinity; |
---|
| 607 | normal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 608 | return false; |
---|
| 609 | } |
---|
| 610 | else |
---|
| 611 | { |
---|
| 612 | G4double dnormDist = normDist1-normDist0; |
---|
| 613 | if (std::abs(dnormDist) < DBL_EPSILON) |
---|
| 614 | { |
---|
| 615 | distance = s0; |
---|
| 616 | normal = surfaceNormal; |
---|
| 617 | if (!outgoing) distFromSurface = -distFromSurface; |
---|
| 618 | return true; |
---|
| 619 | } |
---|
| 620 | else |
---|
| 621 | { |
---|
| 622 | distance = s0 - normDist0*(s1-s0)/dnormDist; |
---|
| 623 | normal = surfaceNormal; |
---|
| 624 | if (!outgoing) distFromSurface = -distFromSurface; |
---|
| 625 | return true; |
---|
| 626 | } |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | // G4ThreeVector dloc = loc1 - loc0; |
---|
| 630 | // G4ThreeVector dlocXv = dloc.cross(v); |
---|
| 631 | // G4double dlocXvmag = dlocXv.mag(); |
---|
| 632 | // if (dloc.mag() <= 0.5*kCarTolerance || dlocXvmag <= DBL_EPSILON) |
---|
| 633 | // { |
---|
| 634 | // distance = loc0.mag(); |
---|
| 635 | // normal = surfaceNormal; |
---|
| 636 | // if (!outgoing) distFromSurface = -distFromSurface; |
---|
| 637 | // return true; |
---|
| 638 | // } |
---|
| 639 | |
---|
| 640 | // G4ThreeVector loc0Xv = loc0.cross(v); |
---|
| 641 | // G4ThreeVector loc1Xv = loc1.cross(v); |
---|
| 642 | // G4double sameDir = -loc0Xv.dot(loc1Xv); |
---|
| 643 | // if (sameDir < 0.0) |
---|
| 644 | // { |
---|
| 645 | // distance = kInfinity; |
---|
| 646 | // distFromSurface = kInfinity; |
---|
| 647 | // normal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 648 | // return false; |
---|
| 649 | // } |
---|
| 650 | // else |
---|
| 651 | // { |
---|
| 652 | // distance = loc0.mag() + loc0Xv.mag() * dloc.mag()/dlocXvmag; |
---|
| 653 | // normal = surfaceNormal; |
---|
| 654 | // if (!outgoing) distFromSurface = -distFromSurface; |
---|
| 655 | // return true; |
---|
| 656 | // } |
---|
| 657 | } |
---|
| 658 | else |
---|
| 659 | { |
---|
| 660 | distance = kInfinity; |
---|
| 661 | distFromSurface = kInfinity; |
---|
| 662 | normal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 663 | return false; |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | // |
---|
| 667 | // |
---|
| 668 | // Use conventional algorithm to determine the whether there is an |
---|
| 669 | // intersection. This involves determining the point of intersection of the |
---|
| 670 | // line with the plane containing the triangle, and then calculating if the |
---|
| 671 | // point is within the triangle. |
---|
| 672 | // |
---|
| 673 | distance = distFromSurface / w; |
---|
| 674 | G4ThreeVector pp = p + v*distance; |
---|
| 675 | G4ThreeVector DD = P0 - pp; |
---|
| 676 | G4double d = E[0].dot(DD); |
---|
| 677 | G4double e = E[1].dot(DD); |
---|
| 678 | G4double s = b*e - c*d; |
---|
| 679 | G4double t = b*d - a*e; |
---|
| 680 | |
---|
| 681 | if (s < 0.0 || t < 0.0 || s+t > det) |
---|
| 682 | { |
---|
| 683 | // |
---|
| 684 | // |
---|
| 685 | // The intersection is outside of the triangle. |
---|
| 686 | // |
---|
| 687 | distance = kInfinity; |
---|
| 688 | distFromSurface = kInfinity; |
---|
| 689 | normal = G4ThreeVector(0.0,0.0,0.0); |
---|
| 690 | return false; |
---|
| 691 | } |
---|
| 692 | else |
---|
| 693 | { |
---|
| 694 | // |
---|
| 695 | // |
---|
| 696 | // There is an intersection. Now we only need to set the surface normal. |
---|
| 697 | // |
---|
| 698 | normal = surfaceNormal; |
---|
| 699 | if (!outgoing) distFromSurface = -distFromSurface; |
---|
| 700 | return true; |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | //////////////////////////////////////////////////////////////////////// |
---|
| 705 | // |
---|
| 706 | // GetPointOnFace |
---|
| 707 | // |
---|
| 708 | // Auxiliary method for get a random point on surface |
---|
| 709 | |
---|
| 710 | G4ThreeVector G4TriangularFacet::GetPointOnFace() const |
---|
| 711 | { |
---|
[921] | 712 | G4double alpha = CLHEP::RandFlat::shoot(0.,1.); |
---|
| 713 | G4double beta = CLHEP::RandFlat::shoot(0.,1); |
---|
| 714 | G4double lambda1=alpha*beta; |
---|
| 715 | G4double lambda0=alpha-lambda1; |
---|
| 716 | |
---|
[831] | 717 | return (P0 + lambda0*E[0] + lambda1*E[1]); |
---|
| 718 | } |
---|
| 719 | |
---|
| 720 | //////////////////////////////////////////////////////////////////////// |
---|
| 721 | // |
---|
| 722 | // GetArea |
---|
| 723 | // |
---|
| 724 | // Auxiliary method for returning the surface area |
---|
| 725 | |
---|
| 726 | G4double G4TriangularFacet::GetArea() |
---|
| 727 | { |
---|
| 728 | return area; |
---|
| 729 | } |
---|