rayEllipsoidIntersectionInterval.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "/**\n\
  3. * DOC_TBA\n\
  4. *\n\
  5. * @name czm_rayEllipsoidIntersectionInterval\n\
  6. * @glslFunction\n\
  7. */\n\
  8. czm_raySegment czm_rayEllipsoidIntersectionInterval(czm_ray ray, vec3 ellipsoid_center, vec3 ellipsoid_inverseRadii)\n\
  9. {\n\
  10. // ray and ellipsoid center in eye coordinates. radii in model coordinates.\n\
  11. vec3 q = ellipsoid_inverseRadii * (czm_inverseModelView * vec4(ray.origin, 1.0)).xyz;\n\
  12. vec3 w = ellipsoid_inverseRadii * (czm_inverseModelView * vec4(ray.direction, 0.0)).xyz;\n\
  13. \n\
  14. q = q - ellipsoid_inverseRadii * (czm_inverseModelView * vec4(ellipsoid_center, 1.0)).xyz;\n\
  15. \n\
  16. float q2 = dot(q, q);\n\
  17. float qw = dot(q, w);\n\
  18. \n\
  19. if (q2 > 1.0) // Outside ellipsoid.\n\
  20. {\n\
  21. if (qw >= 0.0) // Looking outward or tangent (0 intersections).\n\
  22. {\n\
  23. return czm_emptyRaySegment;\n\
  24. }\n\
  25. else // qw < 0.0.\n\
  26. {\n\
  27. float qw2 = qw * qw;\n\
  28. float difference = q2 - 1.0; // Positively valued.\n\
  29. float w2 = dot(w, w);\n\
  30. float product = w2 * difference;\n\
  31. \n\
  32. if (qw2 < product) // Imaginary roots (0 intersections).\n\
  33. {\n\
  34. return czm_emptyRaySegment;\n\
  35. }\n\
  36. else if (qw2 > product) // Distinct roots (2 intersections).\n\
  37. {\n\
  38. float discriminant = qw * qw - product;\n\
  39. float temp = -qw + sqrt(discriminant); // Avoid cancellation.\n\
  40. float root0 = temp / w2;\n\
  41. float root1 = difference / temp;\n\
  42. if (root0 < root1)\n\
  43. {\n\
  44. czm_raySegment i = czm_raySegment(root0, root1);\n\
  45. return i;\n\
  46. }\n\
  47. else\n\
  48. {\n\
  49. czm_raySegment i = czm_raySegment(root1, root0);\n\
  50. return i;\n\
  51. }\n\
  52. }\n\
  53. else // qw2 == product. Repeated roots (2 intersections).\n\
  54. {\n\
  55. float root = sqrt(difference / w2);\n\
  56. czm_raySegment i = czm_raySegment(root, root);\n\
  57. return i;\n\
  58. }\n\
  59. }\n\
  60. }\n\
  61. else if (q2 < 1.0) // Inside ellipsoid (2 intersections).\n\
  62. {\n\
  63. float difference = q2 - 1.0; // Negatively valued.\n\
  64. float w2 = dot(w, w);\n\
  65. float product = w2 * difference; // Negatively valued.\n\
  66. float discriminant = qw * qw - product;\n\
  67. float temp = -qw + sqrt(discriminant); // Positively valued.\n\
  68. czm_raySegment i = czm_raySegment(0.0, temp / w2);\n\
  69. return i;\n\
  70. }\n\
  71. else // q2 == 1.0. On ellipsoid.\n\
  72. {\n\
  73. if (qw < 0.0) // Looking inward.\n\
  74. {\n\
  75. float w2 = dot(w, w);\n\
  76. czm_raySegment i = czm_raySegment(0.0, -qw / w2);\n\
  77. return i;\n\
  78. }\n\
  79. else // qw >= 0.0. Looking outward or tangent.\n\
  80. {\n\
  81. return czm_emptyRaySegment;\n\
  82. }\n\
  83. }\n\
  84. }\n\
  85. ";