algorithm.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import type { Cartesian3 } from "cesium";
  2. import type { AllPlotI } from "../../interface";
  3. import type { PointArr } from "../../interface";
  4. import { P, lonLatToCartesian } from "../../tools";
  5. const curseParams = {
  6. t: 0.3,
  7. };
  8. const gatheringPlaceParams = {
  9. t: 0.4,
  10. };
  11. export const areaPlot: AllPlotI = {
  12. version: "1.0.0",
  13. createTime: "2023-2-26",
  14. updateTime: "2023-2-26",
  15. author: "c-lei-en",
  16. algorithm: {
  17. // * 获取弓形坐标
  18. getArcPositions: (pnts: PointArr[]): Cartesian3[] => {
  19. let pnt;
  20. if (pnts.length == 2 || pnts[1].toString() == pnts[2].toString()) {
  21. const mid = P.PlotUtils.mid(pnts[0], pnts[1]);
  22. const d = P.PlotUtils.distance(pnts[0], mid);
  23. pnt = P.PlotUtils.getThirdPoint(pnts[0], mid, P.Constants.HALF_PI, d);
  24. }
  25. const pnt1 = pnts[0];
  26. const pnt2 = pnts[1];
  27. const pnt3 = pnt ? pnt : pnts[2];
  28. const center = P.PlotUtils.getCircleCenterOfThreePoints(pnt1, pnt2, pnt3);
  29. const radius = P.PlotUtils.distance(pnt1, center);
  30. const angle1 = P.PlotUtils.getAzimuth(pnt1, center);
  31. const angle2 = P.PlotUtils.getAzimuth(pnt2, center);
  32. let startAngle, endAngle;
  33. if (P.PlotUtils.isClockWise(pnt1, pnt2, pnt3)) {
  34. startAngle = angle2;
  35. endAngle = angle1;
  36. } else {
  37. startAngle = angle1;
  38. endAngle = angle2;
  39. }
  40. const pntArr = P.PlotUtils.getArcPoints(
  41. center,
  42. radius,
  43. startAngle,
  44. endAngle
  45. );
  46. return pntArr;
  47. },
  48. // * 获取扇形坐标
  49. getSectorPositions: (pnts: PointArr[]): Cartesian3[] => {
  50. const center = pnts[0];
  51. const radius = P.PlotUtils.distance(pnts[1], center);
  52. const startAngle = P.PlotUtils.getAzimuth(pnts[1], center);
  53. const endAngle = P.PlotUtils.getAzimuth(pnts[2], center);
  54. const pList = P.PlotUtils.getArcPoints(
  55. center,
  56. radius,
  57. startAngle,
  58. endAngle
  59. );
  60. pList.push(lonLatToCartesian(center), pList[0]);
  61. return pList;
  62. },
  63. // * 获取矩形坐标
  64. getRectanglePositions: (pnt1: PointArr, pnt2: PointArr): PointArr => {
  65. const xmin = Math.min(pnt1[0], pnt2[0]);
  66. const xmax = Math.max(pnt1[0], pnt2[0]);
  67. const ymin = Math.min(pnt1[1], pnt2[1]);
  68. const ymax = Math.max(pnt1[1], pnt2[1]);
  69. return [xmin, xmax, ymin, ymax];
  70. },
  71. // * 获取曲线面坐标
  72. getClosedCurvePositions: (pnts: PointArr[]): Cartesian3[] => {
  73. if (pnts.length == 2 || pnts[1].toString() == pnts[2].toString()) {
  74. const mid = P.PlotUtils.mid(pnts[0], pnts[1]);
  75. const d = P.PlotUtils.distance(pnts[0], mid);
  76. const pnt = P.PlotUtils.getThirdPoint(
  77. pnts[0],
  78. mid,
  79. P.Constants.HALF_PI,
  80. d
  81. );
  82. pnts.push(pnt);
  83. }
  84. pnts.push(pnts[0], pnts[1]);
  85. let normals: any = [];
  86. for (let i = 0; i < pnts.length - 2; i++) {
  87. const normalPoints = P.PlotUtils.getBisectorNormals(
  88. curseParams.t,
  89. pnts[i],
  90. pnts[i + 1],
  91. pnts[i + 2]
  92. );
  93. normals = normals.concat(normalPoints);
  94. }
  95. const count = normals.length;
  96. normals = [normals[count - 1]].concat(normals.slice(0, count - 1));
  97. const pList = [];
  98. for (let i = 0; i < pnts.length - 2; i++) {
  99. const pnt1 = pnts[i];
  100. const pnt2 = pnts[i + 1];
  101. pList.push(pnt1);
  102. for (let t = 0; t <= P.Constants.FITTING_COUNT; t++) {
  103. const pnt = P.PlotUtils.getCubicValue(
  104. t / P.Constants.FITTING_COUNT,
  105. pnt1,
  106. normals[i * 2],
  107. normals[i * 2 + 1],
  108. pnt2
  109. );
  110. pList.push(pnt);
  111. }
  112. pList.push(pnt2);
  113. }
  114. const cartesianList = [];
  115. for (let i = 0, len = pList.length; i < len - 1; i++) {
  116. cartesianList.push(lonLatToCartesian(pList[i]));
  117. }
  118. return cartesianList;
  119. },
  120. // * 获取聚集地坐标
  121. getGatheringPlacePositions: (pnts: PointArr[]) => {
  122. if (pnts.length == 2) {
  123. const mid = P.PlotUtils.mid(pnts[0], pnts[1]);
  124. const d = P.PlotUtils.distance(pnts[0], mid) / 0.9;
  125. const pnt = P.PlotUtils.getThirdPoint(
  126. pnts[0],
  127. mid,
  128. P.Constants.HALF_PI,
  129. d,
  130. true
  131. );
  132. pnts = [pnts[0], pnt, pnts[1]];
  133. }
  134. const mid = P.PlotUtils.mid(pnts[0], pnts[2]);
  135. pnts.push(mid, pnts[0], pnts[1]);
  136. let normals: any = [];
  137. for (let i = 0; i < pnts.length - 2; i++) {
  138. const pnt1 = pnts[i];
  139. const pnt2 = pnts[i + 1];
  140. const pnt3 = pnts[i + 2];
  141. const normalPoints = P.PlotUtils.getBisectorNormals(
  142. gatheringPlaceParams.t,
  143. pnt1,
  144. pnt2,
  145. pnt3
  146. );
  147. normals = normals.concat(normalPoints);
  148. }
  149. const count = normals.length;
  150. normals = [normals[count - 1]].concat(normals.slice(0, count - 1));
  151. const pList = [];
  152. for (let i = 0; i < pnts.length - 2; i++) {
  153. const pnt1 = pnts[i];
  154. const pnt2 = pnts[i + 1];
  155. pList.push(pnt1);
  156. for (let t = 0; t <= P.Constants.FITTING_COUNT; t++) {
  157. const pnt = P.PlotUtils.getCubicValue(
  158. t / P.Constants.FITTING_COUNT,
  159. pnt1,
  160. normals[i * 2],
  161. normals[i * 2 + 1],
  162. pnt2
  163. );
  164. pList.push(pnt);
  165. }
  166. pList.push(pnt2);
  167. }
  168. const cartesianList = [];
  169. for (let i = 0, len = pList.length; i < len - 1; i++) {
  170. cartesianList.push(lonLatToCartesian(pList[i]));
  171. }
  172. return cartesianList;
  173. },
  174. },
  175. };