barycentricCoordinates.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import Cartesian2 from "./Cartesian2.js";
  2. import Cartesian3 from "./Cartesian3.js";
  3. import Check from "./Check.js";
  4. import defined from "./defined.js";
  5. import CesiumMath from "./Math.js";
  6. const scratchCartesian1 = new Cartesian3();
  7. const scratchCartesian2 = new Cartesian3();
  8. const scratchCartesian3 = new Cartesian3();
  9. /**
  10. * Computes the barycentric coordinates for a point with respect to a triangle.
  11. *
  12. * @function
  13. *
  14. * @param {Cartesian2|Cartesian3} point The point to test.
  15. * @param {Cartesian2|Cartesian3} p0 The first point of the triangle, corresponding to the barycentric x-axis.
  16. * @param {Cartesian2|Cartesian3} p1 The second point of the triangle, corresponding to the barycentric y-axis.
  17. * @param {Cartesian2|Cartesian3} p2 The third point of the triangle, corresponding to the barycentric z-axis.
  18. * @param {Cartesian3} [result] The object onto which to store the result.
  19. * @returns {Cartesian3|undefined} The modified result parameter or a new Cartesian3 instance if one was not provided. If the triangle is degenerate the function will return undefined.
  20. *
  21. * @example
  22. * // Returns Cartesian3.UNIT_X
  23. * const p = new Cesium.Cartesian3(-1.0, 0.0, 0.0);
  24. * const b = Cesium.barycentricCoordinates(p,
  25. * new Cesium.Cartesian3(-1.0, 0.0, 0.0),
  26. * new Cesium.Cartesian3( 1.0, 0.0, 0.0),
  27. * new Cesium.Cartesian3( 0.0, 1.0, 1.0));
  28. */
  29. function barycentricCoordinates(point, p0, p1, p2, result) {
  30. //>>includeStart('debug', pragmas.debug);
  31. Check.defined("point", point);
  32. Check.defined("p0", p0);
  33. Check.defined("p1", p1);
  34. Check.defined("p2", p2);
  35. //>>includeEnd('debug');
  36. if (!defined(result)) {
  37. result = new Cartesian3();
  38. }
  39. // Implementation based on http://www.blackpawn.com/texts/pointinpoly/default.html.
  40. let v0;
  41. let v1;
  42. let v2;
  43. let dot00;
  44. let dot01;
  45. let dot02;
  46. let dot11;
  47. let dot12;
  48. if (!defined(p0.z)) {
  49. if (Cartesian2.equalsEpsilon(point, p0, CesiumMath.EPSILON14)) {
  50. return Cartesian3.clone(Cartesian3.UNIT_X, result);
  51. }
  52. if (Cartesian2.equalsEpsilon(point, p1, CesiumMath.EPSILON14)) {
  53. return Cartesian3.clone(Cartesian3.UNIT_Y, result);
  54. }
  55. if (Cartesian2.equalsEpsilon(point, p2, CesiumMath.EPSILON14)) {
  56. return Cartesian3.clone(Cartesian3.UNIT_Z, result);
  57. }
  58. v0 = Cartesian2.subtract(p1, p0, scratchCartesian1);
  59. v1 = Cartesian2.subtract(p2, p0, scratchCartesian2);
  60. v2 = Cartesian2.subtract(point, p0, scratchCartesian3);
  61. dot00 = Cartesian2.dot(v0, v0);
  62. dot01 = Cartesian2.dot(v0, v1);
  63. dot02 = Cartesian2.dot(v0, v2);
  64. dot11 = Cartesian2.dot(v1, v1);
  65. dot12 = Cartesian2.dot(v1, v2);
  66. } else {
  67. if (Cartesian3.equalsEpsilon(point, p0, CesiumMath.EPSILON14)) {
  68. return Cartesian3.clone(Cartesian3.UNIT_X, result);
  69. }
  70. if (Cartesian3.equalsEpsilon(point, p1, CesiumMath.EPSILON14)) {
  71. return Cartesian3.clone(Cartesian3.UNIT_Y, result);
  72. }
  73. if (Cartesian3.equalsEpsilon(point, p2, CesiumMath.EPSILON14)) {
  74. return Cartesian3.clone(Cartesian3.UNIT_Z, result);
  75. }
  76. v0 = Cartesian3.subtract(p1, p0, scratchCartesian1);
  77. v1 = Cartesian3.subtract(p2, p0, scratchCartesian2);
  78. v2 = Cartesian3.subtract(point, p0, scratchCartesian3);
  79. dot00 = Cartesian3.dot(v0, v0);
  80. dot01 = Cartesian3.dot(v0, v1);
  81. dot02 = Cartesian3.dot(v0, v2);
  82. dot11 = Cartesian3.dot(v1, v1);
  83. dot12 = Cartesian3.dot(v1, v2);
  84. }
  85. result.y = dot11 * dot02 - dot01 * dot12;
  86. result.z = dot00 * dot12 - dot01 * dot02;
  87. const q = dot00 * dot11 - dot01 * dot01;
  88. // Triangle is degenerate
  89. if (q === 0) {
  90. return undefined;
  91. }
  92. result.y /= q;
  93. result.z /= q;
  94. result.x = 1.0 - result.y - result.z;
  95. return result;
  96. }
  97. export default barycentricCoordinates;