CoplanarPolygonGeometryLibrary-81d1880f.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './Check-70bec281', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-8f8a682c', './OrientedBoundingBox-0ede1598'], function (exports, Check, Cartographic, Cartesian2, BoundingSphere, OrientedBoundingBox) { 'use strict';
  24. /**
  25. * @private
  26. */
  27. var CoplanarPolygonGeometryLibrary = {};
  28. var scratchIntersectionPoint = new Cartographic.Cartesian3();
  29. var scratchXAxis = new Cartographic.Cartesian3();
  30. var scratchYAxis = new Cartographic.Cartesian3();
  31. var scratchZAxis = new Cartographic.Cartesian3();
  32. var obbScratch = new OrientedBoundingBox.OrientedBoundingBox();
  33. CoplanarPolygonGeometryLibrary.validOutline = function(positions) {
  34. //>>includeStart('debug', pragmas.debug);
  35. Check.Check.defined('positions', positions);
  36. //>>includeEnd('debug');
  37. var orientedBoundingBox = OrientedBoundingBox.OrientedBoundingBox.fromPoints(positions, obbScratch);
  38. var halfAxes = orientedBoundingBox.halfAxes;
  39. var xAxis = BoundingSphere.Matrix3.getColumn(halfAxes, 0, scratchXAxis);
  40. var yAxis = BoundingSphere.Matrix3.getColumn(halfAxes, 1, scratchYAxis);
  41. var zAxis = BoundingSphere.Matrix3.getColumn(halfAxes, 2, scratchZAxis);
  42. var xMag = Cartographic.Cartesian3.magnitude(xAxis);
  43. var yMag = Cartographic.Cartesian3.magnitude(yAxis);
  44. var zMag = Cartographic.Cartesian3.magnitude(zAxis);
  45. // If all the points are on a line return undefined because we can't draw a polygon
  46. return !((xMag === 0 && (yMag === 0 || zMag === 0)) || (yMag === 0 && zMag === 0));
  47. };
  48. // call after removeDuplicates
  49. CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments = function(positions, centerResult, planeAxis1Result, planeAxis2Result) {
  50. //>>includeStart('debug', pragmas.debug);
  51. Check.Check.defined('positions', positions);
  52. Check.Check.defined('centerResult', centerResult);
  53. Check.Check.defined('planeAxis1Result', planeAxis1Result);
  54. Check.Check.defined('planeAxis2Result', planeAxis2Result);
  55. //>>includeEnd('debug');
  56. var orientedBoundingBox = OrientedBoundingBox.OrientedBoundingBox.fromPoints(positions, obbScratch);
  57. var halfAxes = orientedBoundingBox.halfAxes;
  58. var xAxis = BoundingSphere.Matrix3.getColumn(halfAxes, 0, scratchXAxis);
  59. var yAxis = BoundingSphere.Matrix3.getColumn(halfAxes, 1, scratchYAxis);
  60. var zAxis = BoundingSphere.Matrix3.getColumn(halfAxes, 2, scratchZAxis);
  61. var xMag = Cartographic.Cartesian3.magnitude(xAxis);
  62. var yMag = Cartographic.Cartesian3.magnitude(yAxis);
  63. var zMag = Cartographic.Cartesian3.magnitude(zAxis);
  64. var min = Math.min(xMag, yMag, zMag);
  65. // If all the points are on a line return undefined because we can't draw a polygon
  66. if ((xMag === 0 && (yMag === 0 || zMag === 0)) || (yMag === 0 && zMag === 0)) {
  67. return false;
  68. }
  69. var planeAxis1;
  70. var planeAxis2;
  71. if (min === yMag || min === zMag) {
  72. planeAxis1 = xAxis;
  73. }
  74. if (min === xMag) {
  75. planeAxis1 = yAxis;
  76. } else if (min === zMag) {
  77. planeAxis2 = yAxis;
  78. }
  79. if (min === xMag || min === yMag) {
  80. planeAxis2 = zAxis;
  81. }
  82. Cartographic.Cartesian3.normalize(planeAxis1, planeAxis1Result);
  83. Cartographic.Cartesian3.normalize(planeAxis2, planeAxis2Result);
  84. Cartographic.Cartesian3.clone(orientedBoundingBox.center, centerResult);
  85. return true;
  86. };
  87. function projectTo2D(position, center, axis1, axis2, result) {
  88. var v = Cartographic.Cartesian3.subtract(position, center, scratchIntersectionPoint);
  89. var x = Cartographic.Cartesian3.dot(axis1, v);
  90. var y = Cartographic.Cartesian3.dot(axis2, v);
  91. return Cartesian2.Cartesian2.fromElements(x, y, result);
  92. }
  93. CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction = function(center, axis1, axis2) {
  94. return function(positions) {
  95. var positionResults = new Array(positions.length);
  96. for (var i = 0; i < positions.length; i++) {
  97. positionResults[i] = projectTo2D(positions[i], center, axis1, axis2);
  98. }
  99. return positionResults;
  100. };
  101. };
  102. CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction = function(center, axis1, axis2) {
  103. return function(position, result) {
  104. return projectTo2D(position, center, axis1, axis2, result);
  105. };
  106. };
  107. exports.CoplanarPolygonGeometryLibrary = CoplanarPolygonGeometryLibrary;
  108. });