00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00046
00047 #include "G4TessellatedGeometryAlgorithms.hh"
00048
00049 #include <cfloat>
00050
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 G4bool G4TessellatedGeometryAlgorithms::IntersectLineAndTriangle2D (
00069 const G4TwoVector &p, const G4TwoVector &v,
00070 const G4TwoVector &p0, const G4TwoVector &e0, const G4TwoVector &e1,
00071 G4TwoVector location[2])
00072 {
00073 G4TwoVector loc0[2];
00074 G4int e0i = IntersectLineAndLineSegment2D (p,v,p0,e0,loc0);
00075 if (e0i == 2)
00076 {
00077 location[0] = loc0[0];
00078 location[1] = loc0[1];
00079 return true;
00080 }
00081
00082 G4TwoVector loc1[2];
00083 G4int e1i = IntersectLineAndLineSegment2D (p,v,p0,e1,loc1);
00084 if (e1i == 2)
00085 {
00086 location[0] = loc1[0];
00087 location[1] = loc1[1];
00088 return true;
00089 }
00090
00091 if ((e0i == 1) && (e1i == 1))
00092 {
00093 if ((loc0[0]-p).mag2() < (loc1[0]-p).mag2())
00094 {
00095 location[0] = loc0[0];
00096 location[1] = loc1[0];
00097 }
00098 else
00099 {
00100 location[0] = loc1[0];
00101 location[1] = loc0[0];
00102 }
00103 return true;
00104 }
00105
00106 G4TwoVector p1 = p0 + e0;
00107 G4TwoVector DE = e1 - e0;
00108 G4TwoVector loc2[2];
00109 G4int e2i = IntersectLineAndLineSegment2D (p,v,p1,DE,loc2);
00110 if (e2i == 2)
00111 {
00112 location[0] = loc2[0];
00113 location[1] = loc2[1];
00114 return true;
00115 }
00116
00117 if ((e0i == 0) && (e1i == 0) && (e2i == 0)) return false;
00118
00119 if ((e0i == 1) && (e2i == 1))
00120 {
00121 if ((loc0[0]-p).mag2() < (loc2[0]-p).mag2())
00122 {
00123 location[0] = loc0[0];
00124 location[1] = loc2[0];
00125 }
00126 else
00127 {
00128 location[0] = loc2[0];
00129 location[1] = loc0[0];
00130 }
00131 return true;
00132 }
00133
00134 if ((e1i == 1) && (e2i == 1))
00135 {
00136 if ((loc1[0]-p).mag2() < (loc2[0]-p).mag2())
00137 {
00138 location[0] = loc1[0];
00139 location[1] = loc2[0];
00140 }
00141 else
00142 {
00143 location[0] = loc2[0];
00144 location[1] = loc1[0];
00145 }
00146 return true;
00147 }
00148
00149 return false;
00150 }
00151
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 G4int G4TessellatedGeometryAlgorithms::IntersectLineAndLineSegment2D (
00177 const G4TwoVector &p0, const G4TwoVector &d0,
00178 const G4TwoVector &p1, const G4TwoVector &d1,
00179 G4TwoVector location[2])
00180 {
00181 G4TwoVector e = p1 - p0;
00182 G4double kross = cross(d0,d1);
00183 G4double sqrKross = kross * kross;
00184 G4double sqrLen0 = d0.mag2();
00185 G4double sqrLen1 = d1.mag2();
00186 location[0] = G4TwoVector(0.0,0.0);
00187 location[1] = G4TwoVector(0.0,0.0);
00188
00189 if (sqrKross > DBL_EPSILON * DBL_EPSILON * sqrLen0 * sqrLen1)
00190 {
00191
00192
00193
00194
00195 G4double ss = cross(e,d1)/kross;
00196 if (ss < 0) return 0;
00197 G4double t = cross(e,d0)/kross;
00198 if (t < 0 || t > 1) return 0;
00199
00200
00201
00202
00203 location[0] = p0 + ss*d0;
00204 return 1;
00205 }
00206
00207
00208
00209 G4double sqrLenE = e.mag2();
00210 kross = cross(e,d0);
00211 sqrKross = kross * kross;
00212 if (sqrKross > DBL_EPSILON * DBL_EPSILON * sqrLen0 * sqrLenE)
00213 {
00214 return 0;
00215 }
00216
00217
00218
00219 G4double s0 = d0.dot(e)/sqrLen0;
00220 G4double s1 = s0 + d0.dot(d1)/sqrLen0;
00221 G4double smin = 0.0;
00222 G4double smax = 0.0;
00223
00224 if (s0 < s1) {smin = s0; smax = s1;}
00225 else {smin = s1; smax = s0;}
00226
00227 if (smax < 0.0) return 0;
00228 else if (smin < 0.0)
00229 {
00230 location[0] = p0;
00231 location[1] = p0 + smax*d0;
00232 return 2;
00233 }
00234 else
00235 {
00236 location[0] = p0 + smin*d0;
00237 location[1] = p0 + smax*d0;
00238 return 2;
00239 }
00240 }
00241
00243
00244
00245
00246
00247
00248
00249
00250 G4double G4TessellatedGeometryAlgorithms::cross(const G4TwoVector &v1,
00251 const G4TwoVector &v2)
00252 {
00253 return v1.x()*v2.y() - v1.y()*v2.x();
00254 }