You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

area.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-04-24 14:49:37
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { Transform } from '@dc-modules/transform'
  7. /**
  8. *
  9. * @param p0
  10. * @param p1
  11. * @param p2
  12. * @returns {number}
  13. * @private
  14. */
  15. function triangleArea(p0, p1, p2) {
  16. let v0 = Cesium.Cartesian3.subtract(p0, p1, new Cesium.Cartesian3())
  17. let v1 = Cesium.Cartesian3.subtract(p2, p1, new Cesium.Cartesian3())
  18. let cross = Cesium.Cartesian3.cross(v0, v1, v0)
  19. return Cesium.Cartesian3.magnitude(cross) * 0.5
  20. }
  21. export default function area(positions) {
  22. let result = 0
  23. if (!Array.isArray(positions)) {
  24. return result
  25. }
  26. if (!(positions[0] instanceof Cesium.Cartesian3)) {
  27. positions = Transform.transformWGS84ArrayToCartesianArray(positions)
  28. }
  29. let geometry = Cesium.CoplanarPolygonGeometry.createGeometry(
  30. Cesium.CoplanarPolygonGeometry.fromPositions({
  31. positions: positions,
  32. vertexFormat: Cesium.VertexFormat.POSITION_ONLY
  33. })
  34. )
  35. if (!geometry) {
  36. return result
  37. }
  38. let flatPositions = geometry.attributes.position.values
  39. let indices = geometry.indices
  40. for (let i = 0; i < indices.length; i += 3) {
  41. let p0 = Cesium.Cartesian3.unpack(
  42. flatPositions,
  43. indices[i] * 3,
  44. new Cesium.Cartesian3()
  45. )
  46. let p1 = Cesium.Cartesian3.unpack(
  47. flatPositions,
  48. indices[i + 1] * 3,
  49. new Cesium.Cartesian3()
  50. )
  51. let p2 = Cesium.Cartesian3.unpack(
  52. flatPositions,
  53. indices[i + 2] * 3,
  54. new Cesium.Cartesian3()
  55. )
  56. result += triangleArea(p0, p1, p2)
  57. }
  58. return result
  59. }