|
| 1 | +#include "pch.hpp" |
| 2 | +#include <limits> |
| 3 | +#include "xrCore/_cylinder.h" |
| 4 | +#include "xrCommon/math_funcs_inline.h" |
| 5 | +#ifdef DEBUG |
| 6 | +#include "xrCore/xrDebug_macros.h" |
| 7 | +#include "xrCore/log.h" |
| 8 | +#endif |
| 9 | + |
| 10 | +template <class T> |
| 11 | +int _cylinder<T>::intersect(const VEC_TYPE& start, const VEC_TYPE& dir, T afT[2], ecode code[2]) const |
| 12 | +{ |
| 13 | + T fEpsilon = 1e-12f; |
| 14 | + |
| 15 | + // set up quadratic Q(t) = a*t^2 + 2*b*t + c |
| 16 | + VEC_TYPE kU, kV, kW = m_direction; |
| 17 | + VEC_TYPE::generate_orthonormal_basis(kW, kU, kV); |
| 18 | + VEC_TYPE kD; |
| 19 | + kD.set(kU.dotproduct(dir), kV.dotproduct(dir), kW.dotproduct(dir)); |
| 20 | +#ifdef DEBUG |
| 21 | + if (kD.square_magnitude() <= std::numeric_limits<T>::min()) |
| 22 | + { |
| 23 | + Msg("dir :%f,%f,%f", dir.x, dir.y, dir.z); |
| 24 | + Msg("kU :%f,%f,%f", kU.x, kU.y, kU.z); |
| 25 | + Msg("kV :%f,%f,%f", kV.x, kV.y, kV.z); |
| 26 | + Msg("kW :%f,%f,%f", kW.x, kW.y, kW.z); |
| 27 | + VERIFY2(0, "KD is zero"); |
| 28 | + } |
| 29 | +#endif |
| 30 | + T fDLength = kD.normalize_magn(); |
| 31 | + T fInvDLength = 1.0f / fDLength; |
| 32 | + VEC_TYPE kDiff; |
| 33 | + kDiff.sub(start, m_center); |
| 34 | + VEC_TYPE kP; |
| 35 | + kP.set(kU.dotproduct(kDiff), kV.dotproduct(kDiff), kW.dotproduct(kDiff)); |
| 36 | + T fHalfHeight = 0.5f * m_height; |
| 37 | + T fRadiusSqr = m_radius * m_radius; |
| 38 | + |
| 39 | + T fInv, fA, fB, fC, fDiscr, fRoot, fT, fT0, fT1, fTmp0, fTmp1; |
| 40 | + |
| 41 | + if (_abs(kD.z) >= 1.0f - fEpsilon) |
| 42 | + { |
| 43 | + // line is parallel to cylinder axis |
| 44 | + if (kP.x * kP.x + kP.y * kP.y <= fRadiusSqr) |
| 45 | + { |
| 46 | + fTmp0 = fInvDLength / kD.z; |
| 47 | + afT[0] = (+fHalfHeight - kP.z) * fTmp0; |
| 48 | + afT[1] = (-fHalfHeight - kP.z) * fTmp0; |
| 49 | + code[0] = cyl_cap; |
| 50 | + code[1] = cyl_cap; |
| 51 | + return 2; |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (_abs(kD.z) <= fEpsilon) |
| 60 | + { |
| 61 | + // line is perpendicular to axis of cylinder |
| 62 | + if (_abs(kP.z) > fHalfHeight) |
| 63 | + { |
| 64 | + // line is outside the planar caps of cylinder |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + fA = kD.x * kD.x + kD.y * kD.y; |
| 69 | + fB = kP.x * kD.x + kP.y * kD.y; |
| 70 | + fC = kP.x * kP.x + kP.y * kP.y - fRadiusSqr; |
| 71 | + fDiscr = fB * fB - fA * fC; |
| 72 | + if (fDiscr < 0.0f) |
| 73 | + { |
| 74 | + // line does not intersect cylinder wall |
| 75 | + return 0; |
| 76 | + } |
| 77 | + else if (fDiscr > 0.0f) |
| 78 | + { |
| 79 | + fRoot = _sqrt(fDiscr); |
| 80 | + fTmp0 = fInvDLength / fA; |
| 81 | + afT[0] = (-fB - fRoot) * fTmp0; |
| 82 | + afT[1] = (-fB + fRoot) * fTmp0; |
| 83 | + code[0] = cyl_wall; |
| 84 | + code[1] = cyl_wall; |
| 85 | + return 2; // wall |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + afT[0] = -fB * fInvDLength / fA; |
| 90 | + code[0] = cyl_wall; |
| 91 | + return 1; // wall |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // test plane intersections first |
| 96 | + int iQuantity = 0; |
| 97 | + fInv = 1.0f / kD.z; |
| 98 | + fT0 = (+fHalfHeight - kP.z) * fInv; |
| 99 | + fTmp0 = kP.x + fT0 * kD.x; |
| 100 | + fTmp1 = kP.y + fT0 * kD.y; |
| 101 | + if (fTmp0 * fTmp0 + fTmp1 * fTmp1 <= fRadiusSqr) |
| 102 | + { |
| 103 | + code[iQuantity] = cyl_cap; |
| 104 | + afT[iQuantity++] = fT0 * fInvDLength; |
| 105 | + } |
| 106 | + |
| 107 | + fT1 = (-fHalfHeight - kP.z) * fInv; |
| 108 | + fTmp0 = kP.x + fT1 * kD.x; |
| 109 | + fTmp1 = kP.y + fT1 * kD.y; |
| 110 | + if (fTmp0 * fTmp0 + fTmp1 * fTmp1 <= fRadiusSqr) |
| 111 | + { |
| 112 | + code[iQuantity] = cyl_cap; |
| 113 | + afT[iQuantity++] = fT1 * fInvDLength; |
| 114 | + } |
| 115 | + |
| 116 | + if (iQuantity == 2) |
| 117 | + { |
| 118 | + // line intersects both top and bottom |
| 119 | + return 2; // both caps |
| 120 | + } |
| 121 | + |
| 122 | + // If iQuantity == 1, then line must intersect cylinder wall |
| 123 | + // somewhere between caps in a single point. This case is detected |
| 124 | + // in the following code that tests for intersection between line and |
| 125 | + // cylinder wall. |
| 126 | + |
| 127 | + fA = kD.x * kD.x + kD.y * kD.y; |
| 128 | + fB = kP.x * kD.x + kP.y * kD.y; |
| 129 | + fC = kP.x * kP.x + kP.y * kP.y - fRadiusSqr; |
| 130 | + fDiscr = fB * fB - fA * fC; |
| 131 | + if (fDiscr < 0.0f) |
| 132 | + { |
| 133 | + // line does not intersect cylinder wall |
| 134 | + // VERIFY( iQuantity == 0 ); |
| 135 | + return 0; |
| 136 | + } |
| 137 | + else if (fDiscr > 0.0f) |
| 138 | + { |
| 139 | + fRoot = _sqrt(fDiscr); |
| 140 | + fInv = 1.0f / fA; |
| 141 | + fT = (-fB - fRoot) * fInv; |
| 142 | + if (fT0 <= fT1) |
| 143 | + { |
| 144 | + if (fT0 <= fT && fT <= fT1) |
| 145 | + { |
| 146 | + code[iQuantity] = cyl_wall; |
| 147 | + afT[iQuantity++] = fT * fInvDLength; |
| 148 | + } |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + if (fT1 <= fT && fT <= fT0) |
| 153 | + { |
| 154 | + code[iQuantity] = cyl_wall; |
| 155 | + afT[iQuantity++] = fT * fInvDLength; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if (iQuantity == 2) |
| 160 | + { |
| 161 | + // Line intersects one of top/bottom of cylinder and once on |
| 162 | + // cylinder wall. |
| 163 | + return 2; |
| 164 | + } |
| 165 | + |
| 166 | + fT = (-fB + fRoot) * fInv; |
| 167 | + if (fT0 <= fT1) |
| 168 | + { |
| 169 | + if (fT0 <= fT && fT <= fT1) |
| 170 | + { |
| 171 | + code[iQuantity] = cyl_wall; |
| 172 | + afT[iQuantity++] = fT * fInvDLength; |
| 173 | + } |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + if (fT1 <= fT && fT <= fT0) |
| 178 | + { |
| 179 | + code[iQuantity] = cyl_wall; |
| 180 | + afT[iQuantity++] = fT * fInvDLength; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + fT = -fB / fA; |
| 187 | + if (fT0 <= fT1) |
| 188 | + { |
| 189 | + if (fT0 <= fT && fT <= fT1) |
| 190 | + { |
| 191 | + code[iQuantity] = cyl_wall; |
| 192 | + afT[iQuantity++] = fT * fInvDLength; |
| 193 | + } |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + if (fT1 <= fT && fT <= fT0) |
| 198 | + { |
| 199 | + code[iQuantity] = cyl_wall; |
| 200 | + afT[iQuantity++] = fT * fInvDLength; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return iQuantity; |
| 206 | +} |
| 207 | + |
| 208 | +template <class T> |
| 209 | +typename _cylinder<T>::ERP_Result _cylinder<T>::intersect(const VEC_TYPE& start, const VEC_TYPE& dir, T& dist) const |
| 210 | +{ |
| 211 | + T afT[2]; |
| 212 | + ecode code[2]; |
| 213 | + int cnt; |
| 214 | + if (0 != (cnt = intersect(start, dir, afT, code))) |
| 215 | + { |
| 216 | + bool o_inside = false; |
| 217 | + bool b_result = false; |
| 218 | + for (int k = 0; k < cnt; k++) |
| 219 | + { |
| 220 | + if (afT[k] < 0.f) |
| 221 | + { |
| 222 | + if (cnt == 2) |
| 223 | + o_inside = true; |
| 224 | + continue; |
| 225 | + } |
| 226 | + if (afT[k] < dist) |
| 227 | + { |
| 228 | + dist = afT[k]; |
| 229 | + b_result = true; |
| 230 | + } |
| 231 | + } |
| 232 | + return b_result ? (o_inside ? rpOriginInside : rpOriginOutside) : rpNone; |
| 233 | + } |
| 234 | + else |
| 235 | + { |
| 236 | + return rpNone; |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +// instantiate on float and double |
| 241 | +template int Fcylinder::intersect(const VEC_TYPE& start, const VEC_TYPE& dir, float afT[2], ecode code[2]) const; |
| 242 | +template int Dcylinder::intersect(const VEC_TYPE& start, const VEC_TYPE& dir, double afT[2], ecode code[2]) const; |
| 243 | + |
| 244 | +template Fcylinder::ERP_Result Fcylinder::intersect(const VEC_TYPE& start, const VEC_TYPE& dir, TYPE& dist) const; |
| 245 | +template Dcylinder::ERP_Result Dcylinder::intersect(const VEC_TYPE& start, const VEC_TYPE& dir, TYPE& dist) const; |
0 commit comments