CoplanarPolygonGeometryLibrary.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import Cartesian2 from "./Cartesian2.js";
  2. import Cartesian3 from "./Cartesian3.js";
  3. import Check from "./Check.js";
  4. import Matrix3 from "./Matrix3.js";
  5. import OrientedBoundingBox from "./OrientedBoundingBox.js";
  6. /**
  7. * @private
  8. */
  9. const CoplanarPolygonGeometryLibrary = {};
  10. const scratchIntersectionPoint = new Cartesian3();
  11. const scratchXAxis = new Cartesian3();
  12. const scratchYAxis = new Cartesian3();
  13. const scratchZAxis = new Cartesian3();
  14. const obbScratch = new OrientedBoundingBox();
  15. CoplanarPolygonGeometryLibrary.validOutline = function (positions) {
  16. //>>includeStart('debug', pragmas.debug);
  17. Check.defined("positions", positions);
  18. //>>includeEnd('debug');
  19. const orientedBoundingBox = OrientedBoundingBox.fromPoints(
  20. positions,
  21. obbScratch
  22. );
  23. const halfAxes = orientedBoundingBox.halfAxes;
  24. const xAxis = Matrix3.getColumn(halfAxes, 0, scratchXAxis);
  25. const yAxis = Matrix3.getColumn(halfAxes, 1, scratchYAxis);
  26. const zAxis = Matrix3.getColumn(halfAxes, 2, scratchZAxis);
  27. const xMag = Cartesian3.magnitude(xAxis);
  28. const yMag = Cartesian3.magnitude(yAxis);
  29. const zMag = Cartesian3.magnitude(zAxis);
  30. // If all the points are on a line return undefined because we can't draw a polygon
  31. return !(
  32. (xMag === 0 && (yMag === 0 || zMag === 0)) ||
  33. (yMag === 0 && zMag === 0)
  34. );
  35. };
  36. // call after removeDuplicates
  37. CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments = function (
  38. positions,
  39. centerResult,
  40. planeAxis1Result,
  41. planeAxis2Result
  42. ) {
  43. //>>includeStart('debug', pragmas.debug);
  44. Check.defined("positions", positions);
  45. Check.defined("centerResult", centerResult);
  46. Check.defined("planeAxis1Result", planeAxis1Result);
  47. Check.defined("planeAxis2Result", planeAxis2Result);
  48. //>>includeEnd('debug');
  49. const orientedBoundingBox = OrientedBoundingBox.fromPoints(
  50. positions,
  51. obbScratch
  52. );
  53. const halfAxes = orientedBoundingBox.halfAxes;
  54. const xAxis = Matrix3.getColumn(halfAxes, 0, scratchXAxis);
  55. const yAxis = Matrix3.getColumn(halfAxes, 1, scratchYAxis);
  56. const zAxis = Matrix3.getColumn(halfAxes, 2, scratchZAxis);
  57. const xMag = Cartesian3.magnitude(xAxis);
  58. const yMag = Cartesian3.magnitude(yAxis);
  59. const zMag = Cartesian3.magnitude(zAxis);
  60. const min = Math.min(xMag, yMag, zMag);
  61. // If all the points are on a line return undefined because we can't draw a polygon
  62. if (
  63. (xMag === 0 && (yMag === 0 || zMag === 0)) ||
  64. (yMag === 0 && zMag === 0)
  65. ) {
  66. return false;
  67. }
  68. let planeAxis1;
  69. let planeAxis2;
  70. if (min === yMag || min === zMag) {
  71. planeAxis1 = xAxis;
  72. }
  73. if (min === xMag) {
  74. planeAxis1 = yAxis;
  75. } else if (min === zMag) {
  76. planeAxis2 = yAxis;
  77. }
  78. if (min === xMag || min === yMag) {
  79. planeAxis2 = zAxis;
  80. }
  81. Cartesian3.normalize(planeAxis1, planeAxis1Result);
  82. Cartesian3.normalize(planeAxis2, planeAxis2Result);
  83. Cartesian3.clone(orientedBoundingBox.center, centerResult);
  84. return true;
  85. };
  86. function projectTo2D(position, center, axis1, axis2, result) {
  87. const v = Cartesian3.subtract(position, center, scratchIntersectionPoint);
  88. const x = Cartesian3.dot(axis1, v);
  89. const y = Cartesian3.dot(axis2, v);
  90. return Cartesian2.fromElements(x, y, result);
  91. }
  92. CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction = function (
  93. center,
  94. axis1,
  95. axis2
  96. ) {
  97. return function (positions) {
  98. const positionResults = new Array(positions.length);
  99. for (let i = 0; i < positions.length; i++) {
  100. positionResults[i] = projectTo2D(positions[i], center, axis1, axis2);
  101. }
  102. return positionResults;
  103. };
  104. };
  105. CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction = function (
  106. center,
  107. axis1,
  108. axis2
  109. ) {
  110. return function (position, result) {
  111. return projectTo2D(position, center, axis1, axis2, result);
  112. };
  113. };
  114. export default CoplanarPolygonGeometryLibrary;