AddPoint.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { dataService } from "../Service/DataService";
  2. import { lineService } from "../Service/LineService";
  3. import { pointService } from "../Service/PointService";
  4. import VectorCategory from "../enum/VectorCategory";
  5. import Point from "../Geometry/Point.js";
  6. import { mathUtil } from "../Util/MathUtil";
  7. import addLine from "./AddLine";
  8. import Settings from "../Settings";
  9. import { stateService } from "../Service/StateService";
  10. import LayerEvents from "../enum/LayerEvents";
  11. import VectorType from "../enum/VectorType";
  12. import Constant from "../Constant";
  13. import { listenLayer } from "../ListenLayer";
  14. export default class AddPoint {
  15. constructor() {
  16. this.testPointIds = []; //所有待测点
  17. }
  18. buildPoint(position) {
  19. let newPoint;
  20. if (Settings.selectPointCategory == VectorCategory.Point.BasePoint) {
  21. newPoint = pointService.create(position);
  22. Settings.selectBasePointId = newPoint.vectorId;
  23. } else {
  24. if (
  25. Settings.selectBasePointId != null &&
  26. Settings.selectLocationMode == Constant.angleLocationMode
  27. ) {
  28. newPoint = pointService.create(position);
  29. this.setLocationByAngle(newPoint.vectorId);
  30. newPoint.setLocationMode(Constant.angleLocationMode);
  31. stateService.setEventName(LayerEvents.AddPoint);
  32. } else if (
  33. Settings.selectBasePointId != null &&
  34. Settings.selectLocationMode == Constant.allLocationMode
  35. ) {
  36. newPoint = pointService.create(position);
  37. this.setLocationByAll(newPoint.vectorId);
  38. newPoint.setLocationMode(Constant.allLocationMode);
  39. stateService.setEventName(LayerEvents.AddPoint);
  40. } else if (Settings.selectLocationMode == Constant.normalLocationMode) {
  41. newPoint = pointService.create(position);
  42. this.setLocationByNormal(newPoint.vectorId);
  43. newPoint.setLocationMode(Constant.normalLocationMode);
  44. } else if (
  45. Settings.selectBasePointId == null &&
  46. (Settings.selectLocationMode == Constant.angleLocationMode ||
  47. Settings.selectLocationMode == Constant.allLocationMode)
  48. ) {
  49. return null;
  50. }
  51. if (
  52. newPoint &&
  53. (newPoint.getLocationMode() == Constant.allLocationMode ||
  54. newPoint.getLocationMode() == Constant.angleLocationMode) &&
  55. newPoint.getCategory() == VectorCategory.Point.TestPoint
  56. ) {
  57. this.testPointIds.push(newPoint.vectorId);
  58. }
  59. }
  60. listenLayer.clear();
  61. return newPoint;
  62. }
  63. isFocusBasePoint() {
  64. if (Settings.selectBasePointId) {
  65. let point = dataService.getPoint(Settings.selectBasePointId);
  66. if (point.getCategory() == VectorCategory.Point.BasePoint) {
  67. return point;
  68. }
  69. }
  70. return null;
  71. }
  72. //直角定位法
  73. setLocationByAngle(testPointId) {
  74. let basePoint = this.isFocusBasePoint();
  75. if (!basePoint) {
  76. return;
  77. }
  78. let testPoint = dataService.getPoint(testPointId);
  79. if (testPoint.getCategory() != VectorCategory.Point.TestPoint) {
  80. return;
  81. }
  82. testPoint.setLinkedBasePointId(basePoint.vectorId);
  83. let lineGeometry = dataService.getLine(Settings.baseLineId);
  84. let startPoint = dataService.getPoint(lineGeometry.startId);
  85. let endPoint = dataService.getPoint(lineGeometry.endId);
  86. let line = mathUtil.createLine1(startPoint, endPoint);
  87. let vLine = mathUtil.getVerticalLine(line, testPoint);
  88. let join = mathUtil.getJoinLinePoint(basePoint, vLine);
  89. join = pointService.create(join);
  90. join.setCategory(VectorCategory.Point.TestBasePoint);
  91. join.setLocationMode(Constant.angleLocationMode);
  92. join.setLinkedBasePointId(basePoint.vectorId);
  93. join.setLinkedTestPointId(testPointId);
  94. lineService.createByPointId(
  95. testPointId,
  96. join.vectorId,
  97. VectorCategory.Line.GuidePositionLine
  98. );
  99. let guidePositionLine = lineService.createByPointId(
  100. basePoint.vectorId,
  101. join.vectorId,
  102. VectorCategory.Line.PositionLine
  103. );
  104. guidePositionLine.setLocationMode(Constant.angleLocationMode);
  105. }
  106. //综合定位法
  107. setLocationByAll(testPointId) {
  108. let basePoint = this.isFocusBasePoint();
  109. if (!basePoint) {
  110. return;
  111. }
  112. let testPoint = dataService.getPoint(testPointId);
  113. testPoint.setLinkedBasePointId(basePoint.vectorId);
  114. let lineGeometry = dataService.getLine(Settings.baseLineId);
  115. let startPoint = dataService.getPoint(lineGeometry.startId);
  116. let endPoint = dataService.getPoint(lineGeometry.endId);
  117. let line = mathUtil.createLine1(startPoint, endPoint);
  118. let join = mathUtil.getJoinLinePoint(testPoint, line);
  119. join = pointService.create(join); //经过待测点且与基准线垂直的线段,与基准线的交点
  120. join.setCategory(VectorCategory.Point.TestBasePoint);
  121. join.setLocationMode(Constant.allLocationMode);
  122. join.setLinkedBasePointId(basePoint.vectorId);
  123. join.setLinkedTestPointId(testPointId);
  124. //待测点与基准线的垂直线
  125. lineService.createByPointId(
  126. testPointId,
  127. join.vectorId,
  128. VectorCategory.Line.PositionLine
  129. );
  130. //暂时没有其他待测点
  131. if (this.testPointIds.length == 0) {
  132. //待测点与基准线点的连线
  133. lineService.createByPointId(
  134. basePoint.vectorId,
  135. testPointId,
  136. VectorCategory.Line.PositionLine
  137. );
  138. } else {
  139. //取上一个待测点
  140. lineService.createByPointId(
  141. this.testPointIds[this.testPointIds.length - 1],
  142. testPointId,
  143. VectorCategory.Line.PositionLine
  144. );
  145. testPoint.setLinkedTestPointId(
  146. this.testPointIds[this.testPointIds.length - 1]
  147. );
  148. }
  149. }
  150. setLocationByNormal(testPointId) {
  151. let testPoint = dataService.getPoint(testPointId);
  152. if (testPoint.getCategory() != VectorCategory.Point.TestPoint) {
  153. return;
  154. }
  155. if (!Settings.baseLineId) {
  156. return;
  157. }
  158. let lineGeometry = dataService.getLine(Settings.baseLineId);
  159. let startPoint = dataService.getPoint(lineGeometry.startId);
  160. let endPoint = dataService.getPoint(lineGeometry.endId);
  161. let line = mathUtil.createLine1(startPoint, endPoint);
  162. let vLine = mathUtil.getVerticalLine(line, testPoint);
  163. let join = mathUtil.getIntersectionPoint(vLine, line);
  164. join = pointService.create(join);
  165. join.setCategory(VectorCategory.Point.TestBasePoint);
  166. join.setLocationMode(Constant.normalLocationMode);
  167. join.setLinkedTestPointId(testPointId);
  168. lineService.createByPointId(
  169. testPointId,
  170. join.vectorId,
  171. VectorCategory.Line.PositionLine
  172. );
  173. }
  174. deleteTestPoints() {
  175. for (let i = 0; i < this.testPointIds.length; ++i) {
  176. pointService.deletePoint(this.testPointIds[i]);
  177. }
  178. this.testPointIds = [];
  179. }
  180. }
  181. const addPoint = new AddPoint();
  182. export { addPoint };