Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration and of QinetiQ Ltd, * 20 // * subject to DEFCON 705 IPR conditions. << 21 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 22 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 23 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 24 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 25 // ******************************************* 24 // ******************************************************************** 26 // 25 // 27 // G4TessellatedGeometryAlgorithms implementat << 26 // $Id: G4TessellatedGeometryAlgorithms.cc,v 1.5 2007/12/12 16:51:12 gcosmo Exp $ >> 27 // GEANT4 tag $Name: geant4-09-01 $ >> 28 // >> 29 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% >> 30 // >> 31 // MODULE: G4TessellatedGeometryAlgorithms.cc >> 32 // >> 33 // Date: 07/08/2005 >> 34 // Author: Rickard Holmberg & Pete Truscott >> 35 // Organisation: QinetiQ Ltd, UK (PT) >> 36 // Customer: ESA-ESTEC / TEC-EES >> 37 // Contract: >> 38 // >> 39 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% >> 40 // >> 41 // CHANGE HISTORY >> 42 // -------------- 28 // 43 // 29 // 07 August 2007, P R Truscott, QinetiQ Ltd, 44 // 07 August 2007, P R Truscott, QinetiQ Ltd, UK - Created, with member 30 // functions based on the work 45 // functions based on the work of Rickard Holmberg. 31 // 12 October 2012, M Gayer, CERN, - Reviewed << 46 // 32 // ------------------------------------------- << 47 // 26 September 2007 >> 48 // P R Truscott, qinetiQ Ltd, UK >> 49 // Updated to assign values of location array, not update >> 50 // just the pointer. >> 51 // >> 52 /////////////////////////////////////////////////////////////////////////////// 33 53 34 #include "G4TessellatedGeometryAlgorithms.hh" 54 #include "G4TessellatedGeometryAlgorithms.hh" >> 55 /////////////////////////////////////////////////////////////////////////////// >> 56 // >> 57 // Pointer to single instance of class. >> 58 // >> 59 G4TessellatedGeometryAlgorithms* G4TessellatedGeometryAlgorithms::fInstance = 0; 35 60 36 #include <cfloat> << 61 /////////////////////////////////////////////////////////////////////////////// >> 62 // >> 63 // G4TessellatedGeometryAlgorithms >> 64 // >> 65 // Constructor doesn't need to do anything since this class just allows access >> 66 // to the geometric algorithms contained in member functions. >> 67 // >> 68 G4TessellatedGeometryAlgorithms::G4TessellatedGeometryAlgorithms () >> 69 { >> 70 } >> 71 >> 72 /////////////////////////////////////////////////////////////////////////////// >> 73 // >> 74 // GetInstance >> 75 // >> 76 // This is the access point for this singleton. >> 77 // >> 78 G4TessellatedGeometryAlgorithms* G4TessellatedGeometryAlgorithms::GetInstance() >> 79 { >> 80 static G4TessellatedGeometryAlgorithms worldStdGeom; >> 81 if (!fInstance) >> 82 { >> 83 fInstance = &worldStdGeom; >> 84 } >> 85 return fInstance; >> 86 } 37 87 38 ////////////////////////////////////////////// 88 /////////////////////////////////////////////////////////////////////////////// 39 // 89 // 40 // IntersectLineAndTriangle2D 90 // IntersectLineAndTriangle2D 41 // 91 // 42 // Determines whether there is an intersection 92 // Determines whether there is an intersection between a line defined 43 // by r = p + s.v and a triangle defined by ve << 93 // by r = p + s.v and a triangle defined by verticies P0, P0+E0 and P0+E1. 44 // 94 // 45 // Here: 95 // Here: 46 // p = 2D vector 96 // p = 2D vector 47 // s = scaler on [0,infinity) 97 // s = scaler on [0,infinity) 48 // v = 2D vector 98 // v = 2D vector 49 // p0, e0 and e1 are 2D vectors << 99 // P0, E0 and E1 are 2D vectors 50 // Information about where the intersection oc 100 // Information about where the intersection occurs is returned in the 51 // variable location. 101 // variable location. 52 // 102 // 53 // This is based on the work of Rickard Holmbe 103 // This is based on the work of Rickard Holmberg. 54 // 104 // 55 G4bool G4TessellatedGeometryAlgorithms::Inters 105 G4bool G4TessellatedGeometryAlgorithms::IntersectLineAndTriangle2D ( 56 const G4TwoVector& p, const G4TwoVector& v, << 106 const G4TwoVector p, const G4TwoVector v, 57 const G4TwoVector& p0, const G4TwoVector& e0 << 107 const G4TwoVector P0, const G4TwoVector E0, const G4TwoVector E1, 58 G4TwoVector location[2]) 108 G4TwoVector location[2]) 59 { 109 { 60 G4TwoVector loc0[2]; 110 G4TwoVector loc0[2]; 61 G4int e0i = IntersectLineAndLineSegment2D (p << 111 G4int e0i = IntersectLineAndLineSegment2D (p,v,P0,E0,loc0); 62 if (e0i == 2) 112 if (e0i == 2) 63 { 113 { 64 location[0] = loc0[0]; 114 location[0] = loc0[0]; 65 location[1] = loc0[1]; 115 location[1] = loc0[1]; 66 return true; 116 return true; 67 } 117 } 68 << 118 69 G4TwoVector loc1[2]; 119 G4TwoVector loc1[2]; 70 G4int e1i = IntersectLineAndLineSegment2D (p << 120 G4int e1i = IntersectLineAndLineSegment2D (p,v,P0,E1,loc1); 71 if (e1i == 2) 121 if (e1i == 2) 72 { 122 { 73 location[0] = loc1[0]; 123 location[0] = loc1[0]; 74 location[1] = loc1[1]; 124 location[1] = loc1[1]; 75 return true; 125 return true; 76 } 126 } 77 << 127 78 if ((e0i == 1) && (e1i == 1)) 128 if ((e0i == 1) && (e1i == 1)) 79 { 129 { 80 if ((loc0[0]-p).mag2() < (loc1[0]-p).mag2( 130 if ((loc0[0]-p).mag2() < (loc1[0]-p).mag2()) 81 { 131 { 82 location[0] = loc0[0]; 132 location[0] = loc0[0]; 83 location[1] = loc1[0]; 133 location[1] = loc1[0]; 84 } 134 } 85 else 135 else 86 { 136 { 87 location[0] = loc1[0]; 137 location[0] = loc1[0]; 88 location[1] = loc0[0]; 138 location[1] = loc0[0]; 89 } 139 } 90 return true; 140 return true; 91 } 141 } 92 << 142 93 G4TwoVector p1 = p0 + e0; << 143 G4TwoVector P1 = P0 + E0; 94 G4TwoVector DE = e1 - e0; << 144 G4TwoVector DE = E1 - E0; 95 G4TwoVector loc2[2]; 145 G4TwoVector loc2[2]; 96 G4int e2i = IntersectLineAndLineSegment2D (p << 146 G4int e2i = IntersectLineAndLineSegment2D (p,v,P1,DE,loc2); 97 if (e2i == 2) 147 if (e2i == 2) 98 { 148 { 99 location[0] = loc2[0]; 149 location[0] = loc2[0]; 100 location[1] = loc2[1]; 150 location[1] = loc2[1]; 101 return true; 151 return true; 102 } 152 } 103 153 104 if ((e0i == 0) && (e1i == 0) && (e2i == 0)) << 154 if ((e0i == 0) && (e1i == 0) && (e2i == 0)) { return false; } 105 155 106 if ((e0i == 1) && (e2i == 1)) 156 if ((e0i == 1) && (e2i == 1)) 107 { 157 { 108 if ((loc0[0]-p).mag2() < (loc2[0]-p).mag2( 158 if ((loc0[0]-p).mag2() < (loc2[0]-p).mag2()) 109 { 159 { 110 location[0] = loc0[0]; 160 location[0] = loc0[0]; 111 location[1] = loc2[0]; 161 location[1] = loc2[0]; 112 } 162 } 113 else 163 else 114 { 164 { 115 location[0] = loc2[0]; 165 location[0] = loc2[0]; 116 location[1] = loc0[0]; 166 location[1] = loc0[0]; 117 } 167 } 118 return true; 168 return true; 119 } 169 } 120 170 121 if ((e1i == 1) && (e2i == 1)) 171 if ((e1i == 1) && (e2i == 1)) 122 { 172 { 123 if ((loc1[0]-p).mag2() < (loc2[0]-p).mag2( 173 if ((loc1[0]-p).mag2() < (loc2[0]-p).mag2()) 124 { 174 { 125 location[0] = loc1[0]; 175 location[0] = loc1[0]; 126 location[1] = loc2[0]; 176 location[1] = loc2[0]; 127 } 177 } 128 else 178 else 129 { 179 { 130 location[0] = loc2[0]; 180 location[0] = loc2[0]; 131 location[1] = loc1[0]; 181 location[1] = loc1[0]; 132 } 182 } 133 return true; 183 return true; 134 } 184 } 135 185 136 return false; 186 return false; 137 } 187 } 138 188 139 ////////////////////////////////////////////// 189 /////////////////////////////////////////////////////////////////////////////// 140 // 190 // 141 // IntersectLineAndLineSegment2D 191 // IntersectLineAndLineSegment2D 142 // 192 // 143 // Determines whether there is an intersection 193 // Determines whether there is an intersection between a line defined 144 // by r = p0 + s.d0 and a line-segment with en << 194 // by r = P0 + s.D0 and a line-segment with endpoints P1 and P1+D1. 145 // Here: 195 // Here: 146 // p0 = 2D vector << 196 // P0 = 2D vector 147 // s = scaler on [0,infinity) 197 // s = scaler on [0,infinity) 148 // d0 = 2D vector << 198 // D0 = 2D vector 149 // p1 and d1 are 2D vectors << 199 // P1 and D1 are 2D vectors 150 // 200 // 151 // This function returns: 201 // This function returns: 152 // 0 - if there is no intersection; 202 // 0 - if there is no intersection; 153 // 1 - if there is a unique intersection; 203 // 1 - if there is a unique intersection; 154 // 2 - if the line and line-segments overlap, 204 // 2 - if the line and line-segments overlap, and the intersection is a 155 // segment itself. 205 // segment itself. 156 // Information about where the intersection oc 206 // Information about where the intersection occurs is returned in the 157 // as ??. 207 // as ??. 158 // 208 // 159 // This is based on the work of Rickard Holmbe 209 // This is based on the work of Rickard Holmberg as well as material published 160 // by Philip J Schneider and David H Eberly, " 210 // by Philip J Schneider and David H Eberly, "Geometric Tools for Computer 161 // Graphics," ISBN 1-55860-694-0, pp 244-245, 211 // Graphics," ISBN 1-55860-694-0, pp 244-245, 2003. 162 // 212 // 163 G4int G4TessellatedGeometryAlgorithms::Interse 213 G4int G4TessellatedGeometryAlgorithms::IntersectLineAndLineSegment2D ( 164 const G4TwoVector& p0, const G4TwoVector& d0 << 214 const G4TwoVector P0, const G4TwoVector D0, 165 const G4TwoVector& p1, const G4TwoVector& d1 << 215 const G4TwoVector P1, const G4TwoVector D1, >> 216 G4TwoVector location[2]) 166 { 217 { 167 G4TwoVector e = p1 - p0; << 218 G4TwoVector E = P1 - P0; 168 G4double kross = cross(d0,d1); << 219 G4double kross = cross(D0,D1); 169 G4double sqrKross = kross * kross; 220 G4double sqrKross = kross * kross; 170 G4double sqrLen0 = d0.mag2(); << 221 G4double sqrLen0 = D0.mag2(); 171 G4double sqrLen1 = d1.mag2(); << 222 G4double sqrLen1 = D1.mag2(); 172 location[0] = G4TwoVector(0.0,0.0); 223 location[0] = G4TwoVector(0.0,0.0); 173 location[1] = G4TwoVector(0.0,0.0); 224 location[1] = G4TwoVector(0.0,0.0); 174 225 175 if (sqrKross > DBL_EPSILON * DBL_EPSILON * s 226 if (sqrKross > DBL_EPSILON * DBL_EPSILON * sqrLen0 * sqrLen1) 176 { 227 { 177 // << 228 // 178 // The line and line segment are not paral << 229 // 179 // is in positive s where r=p0 + s*d0, and << 230 // The line and line segment are not parallel. Determine if the intersection 180 // << 231 // is in positive s where r = P0 + s*D0, and for 0<=t<=1 where r = p1 + t*D1. 181 G4double ss = cross(e,d1)/kross; << 232 // 182 if (ss < 0) return 0; // Intersect << 233 G4double s = cross(E,D1)/kross; 183 G4double t = cross(e,d0)/kross; << 234 if (s < 0) return 0; // Intersection does not occur for positive s. 184 if (t < 0 || t > 1) return 0; // Intersect << 235 G4double t = cross(E,D0)/kross; 185 // << 236 if (t < 0 || t > 1) return 0; // Intersection does not occur on line-segment. 186 // Intersection of lines is a single point << 237 // 187 // defined by r=p0 + ss*d0, and the line s << 238 // 188 // << 239 // Intersection of lines is a single point on the forward-propagating line 189 location[0] = p0 + ss*d0; << 240 // defined by r = P0 + s*D0, and the line segment defined by r = p1 + t*D1. >> 241 // >> 242 location[0] = P0 + s*D0; 190 return 1; 243 return 1; 191 } 244 } 192 // << 245 // 193 // Line and line segment are parallel. Deter << 246 // 194 // << 247 // Line and line segment are parallel. Determine whether they overlap or not. 195 G4double sqrLenE = e.mag2(); << 248 // 196 kross = cross(e,d0); << 249 G4double sqrLenE = E.mag2(); >> 250 kross = cross(E,D0); 197 sqrKross = kross * kross; 251 sqrKross = kross * kross; 198 if (sqrKross > DBL_EPSILON * DBL_EPSILON * s 252 if (sqrKross > DBL_EPSILON * DBL_EPSILON * sqrLen0 * sqrLenE) 199 { 253 { 200 return 0; //Lines are different. 254 return 0; //Lines are different. 201 } 255 } 202 // << 256 // 203 // Lines are the same. Test for overlap. << 257 // 204 // << 258 // Lines are the same. Test for overlap. 205 G4double s0 = d0.dot(e)/sqrLen0; << 259 // 206 G4double s1 = s0 + d0.dot(d1)/sqrLen0; << 260 G4double s0 = D0.dot(E)/sqrLen0; >> 261 G4double s1 = s0 + D0.dot(D1)/sqrLen0; 207 G4double smin = 0.0; 262 G4double smin = 0.0; 208 G4double smax = 0.0; 263 G4double smax = 0.0; 209 264 210 if (s0 < s1) {smin = s0; smax = s1;} 265 if (s0 < s1) {smin = s0; smax = s1;} 211 else {smin = s1; smax = s0;} 266 else {smin = s1; smax = s0;} 212 267 213 if (smax < 0.0) return 0; 268 if (smax < 0.0) return 0; 214 else if (smin < 0.0) 269 else if (smin < 0.0) 215 { 270 { 216 location[0] = p0; << 271 location[0] = P0; 217 location[1] = p0 + smax*d0; << 272 location[1] = P0 + smax*D0; 218 return 2; 273 return 2; 219 } 274 } 220 else 275 else 221 { 276 { 222 location[0] = p0 + smin*d0; << 277 location[0] = P0 + smin*D0; 223 location[1] = p0 + smax*d0; << 278 location[1] = P0 + smax*D0; 224 return 2; 279 return 2; 225 } 280 } 226 } << 227 << 228 ////////////////////////////////////////////// << 229 // << 230 // CrossProduct << 231 // << 232 // This is just a ficticious "cross-product" f << 233 // "ficticious" because such an operation is n << 234 // with 3D space. << 235 // << 236 G4double G4TessellatedGeometryAlgorithms::cros << 237 << 238 { << 239 return v1.x()*v2.y() - v1.y()*v2.x(); << 240 } 281 } 241 282