CoplanarPolygonGeometryLibrary.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. var CoplanarPolygonGeometryLibrary = {};
  10. var scratchIntersectionPoint = new Cartesian3();
  11. var scratchXAxis = new Cartesian3();
  12. var scratchYAxis = new Cartesian3();
  13. var scratchZAxis = new Cartesian3();
  14. var obbScratch = new OrientedBoundingBox();
  15. CoplanarPolygonGeometryLibrary.validOutline = function(positions) {
  16. //>>includeStart('debug', pragmas.debug);
  17. Check.defined('positions', positions);
  18. //>>includeEnd('debug');
  19. var orientedBoundingBox = OrientedBoundingBox.fromPoints(positions, obbScratch);
  20. var halfAxes = orientedBoundingBox.halfAxes;
  21. var xAxis = Matrix3.getColumn(halfAxes, 0, scratchXAxis);
  22. var yAxis = Matrix3.getColumn(halfAxes, 1, scratchYAxis);
  23. var zAxis = Matrix3.getColumn(halfAxes, 2, scratchZAxis);
  24. var xMag = Cartesian3.magnitude(xAxis);
  25. var yMag = Cartesian3.magnitude(yAxis);
  26. var zMag = Cartesian3.magnitude(zAxis);
  27. // If all the points are on a line return undefined because we can't draw a polygon
  28. return !((xMag === 0 && (yMag === 0 || zMag === 0)) || (yMag === 0 && zMag === 0));
  29. };
  30. // call after removeDuplicates
  31. CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments = function(positions, centerResult, planeAxis1Result, planeAxis2Result) {
  32. //>>includeStart('debug', pragmas.debug);
  33. Check.defined('positions', positions);
  34. Check.defined('centerResult', centerResult);
  35. Check.defined('planeAxis1Result', planeAxis1Result);
  36. Check.defined('planeAxis2Result', planeAxis2Result);
  37. //>>includeEnd('debug');
  38. var orientedBoundingBox = OrientedBoundingBox.fromPoints(positions, obbScratch);
  39. var halfAxes = orientedBoundingBox.halfAxes;
  40. var xAxis = Matrix3.getColumn(halfAxes, 0, scratchXAxis);
  41. var yAxis = Matrix3.getColumn(halfAxes, 1, scratchYAxis);
  42. var zAxis = Matrix3.getColumn(halfAxes, 2, scratchZAxis);
  43. var xMag = Cartesian3.magnitude(xAxis);
  44. var yMag = Cartesian3.magnitude(yAxis);
  45. var zMag = Cartesian3.magnitude(zAxis);
  46. var min = Math.min(xMag, yMag, zMag);
  47. // If all the points are on a line return undefined because we can't draw a polygon
  48. if ((xMag === 0 && (yMag === 0 || zMag === 0)) || (yMag === 0 && zMag === 0)) {
  49. return false;
  50. }
  51. var planeAxis1;
  52. var planeAxis2;
  53. if (min === yMag || min === zMag) {
  54. planeAxis1 = xAxis;
  55. }
  56. if (min === xMag) {
  57. planeAxis1 = yAxis;
  58. } else if (min === zMag) {
  59. planeAxis2 = yAxis;
  60. }
  61. if (min === xMag || min === yMag) {
  62. planeAxis2 = zAxis;
  63. }
  64. Cartesian3.normalize(planeAxis1, planeAxis1Result);
  65. Cartesian3.normalize(planeAxis2, planeAxis2Result);
  66. Cartesian3.clone(orientedBoundingBox.center, centerResult);
  67. return true;
  68. };
  69. function projectTo2D(position, center, axis1, axis2, result) {
  70. var v = Cartesian3.subtract(position, center, scratchIntersectionPoint);
  71. var x = Cartesian3.dot(axis1, v);
  72. var y = Cartesian3.dot(axis2, v);
  73. return Cartesian2.fromElements(x, y, result);
  74. }
  75. CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction = function(center, axis1, axis2) {
  76. return function(positions) {
  77. var positionResults = new Array(positions.length);
  78. for (var i = 0; i < positions.length; i++) {
  79. positionResults[i] = projectTo2D(positions[i], center, axis1, axis2);
  80. }
  81. return positionResults;
  82. };
  83. };
  84. CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction = function(center, axis1, axis2) {
  85. return function(position, result) {
  86. return projectTo2D(position, center, axis1, axis2, result);
  87. };
  88. };
  89. export default CoplanarPolygonGeometryLibrary;