AddPoint.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 VectorType from "../enum/VectorType";
  11. import Constant from "../Constant";
  12. export default class AddPoint {
  13. constructor() {
  14. this.basePointIds = []; //所有基准点
  15. this.testPointIds = []; //所有待测点
  16. }
  17. buildPoint(position) {
  18. const newPoint = pointService.create(position);
  19. if (newPoint.getCategory() == VectorCategory.Point.BasePoint) {
  20. this.basePointIds.push(newPoint.vectorId);
  21. } else {
  22. if (Settings.locationMode == Constant.angleLocationMode) {
  23. this.setLocationByAngle(newPoint.vectorId);
  24. } else if (Settings.locationMode == Constant.allLocationMode) {
  25. this.setLocationByAll(newPoint.vectorId);
  26. } else if (Settings.locationMode == Constant.normalLocationMode) {
  27. this.setLocationByNormal(newPoint.vectorId);
  28. }
  29. if (newPoint.getCategory() == VectorCategory.Point.TestPoint) {
  30. this.testPointIds.push(newPoint.vectorId);
  31. }
  32. }
  33. listenLayer.clear();
  34. return newPoint;
  35. }
  36. isFocusBasePoint() {
  37. let focusItem = stateService.getFocusItem();
  38. if (focusItem && focusItem.type == VectorType.Point) {
  39. let point = dataService.getPoint(focusItem.vectorId);
  40. if (point.getCategory() == VectorCategory.Point.BasePoint) {
  41. return point;
  42. }
  43. }
  44. return null;
  45. }
  46. //直角定位法
  47. setLocationByAngle(testPointId) {
  48. let basePoint = this.isFocusBasePoint();
  49. if (!basePoint) {
  50. return;
  51. }
  52. let testPoint = dataService.getPoint(testPointId);
  53. if (testPoint.getCategory() != VectorCategory.Point.TestPoint) {
  54. return;
  55. }
  56. testPoint.setLinkedBasePointId(basePoint.vectorId);
  57. let lineGeometry = dataService.getLine(Settings.baseLineId);
  58. let startPoint = dataService.getPoint(lineGeometry.startId);
  59. let endPoint = dataService.getPoint(lineGeometry.endId);
  60. let line = mathUtil.createLine1(startPoint, endPoint);
  61. let vLine = mathUtil.getVerticalLine(line, testPoint);
  62. let join = mathUtil.getJoinLinePoint(basePoint, vLine);
  63. join = pointService.create(join);
  64. join.setCategory(VectorCategory.Point.TestBasePoint);
  65. join.setLinkedBasePointId(basePoint.vectorId);
  66. join.setLinkedTestPointId(testPointId);
  67. lineService.createByPointId(
  68. testPointId,
  69. join.vectorId,
  70. VectorCategory.Line.PositionLine
  71. );
  72. lineService.createByPointId(
  73. basePoint.vectorId,
  74. join.vectorId,
  75. VectorCategory.Line.PositionLine
  76. );
  77. }
  78. //待测基准点,待测点与基准线相交的点
  79. getTestBasePoint(basePointId, testPointId) {
  80. let points = dataService.getPoints();
  81. for (let key in points) {
  82. const point = dataService.getPoint(key);
  83. if (point.getCategory() == VectorCategory.Point.TestBasePoint) {
  84. if (
  85. point.getLinkedBasePointId() == basePointId &&
  86. point.getLinkedTestPointId() == testPointId
  87. ) {
  88. return point;
  89. }
  90. }
  91. }
  92. return null;
  93. }
  94. //更新待测点(直角定位法)
  95. updateTestPointByAngle(testPointId, newPosition) {
  96. let testPoint = dataService.getPoint(testPointId);
  97. mathUtil.clonePoint(testPoint, newPosition);
  98. let basePoint = dataService.getPoint(testPoint.getLinkedBasePointId());
  99. let lineGeometry = dataService.getLine(Settings.baseLineId);
  100. let startPoint = dataService.getPoint(lineGeometry.startId);
  101. let endPoint = dataService.getPoint(lineGeometry.endId);
  102. let line = mathUtil.createLine1(startPoint, endPoint);
  103. let vLine = mathUtil.getVerticalLine(line, testPoint);
  104. let join = mathUtil.getJoinLinePoint(basePoint, vLine);
  105. let testBasePoint = this.getTestBasePoint(basePoint.vectorId, testPointId);
  106. mathUtil.clonePoint(testBasePoint, join);
  107. }
  108. //综合定位法
  109. setLocationByAll(testPointId) {
  110. let basePoint = this.isFocusBasePoint();
  111. if (!basePoint) {
  112. return;
  113. }
  114. let testPoint = dataService.getPoint(testPointId);
  115. testPoint.setLinkedBasePointId(basePoint.vectorId);
  116. let lineGeometry = dataService.getLine(Settings.baseLineId);
  117. let startPoint = dataService.getPoint(lineGeometry.startId);
  118. let endPoint = dataService.getPoint(lineGeometry.endId);
  119. let line = mathUtil.createLine1(startPoint, endPoint);
  120. let join = mathUtil.getJoinLinePoint(testPoint, line);
  121. join = pointService.create(join); //经过待测点且与基准线垂直的线段,与基准线的交点
  122. join.setCategory(VectorCategory.Point.TestBasePoint);
  123. join.setLinkedBasePointId(basePoint.vectorId);
  124. join.setLinkedTestPointId(testPointId);
  125. //待测点与基准线的垂直线
  126. lineService.createByPointId(
  127. testPointId,
  128. join.vectorId,
  129. VectorCategory.Line.PositionLine
  130. );
  131. //暂时没有其他待测点
  132. if (this.testPointIds.length == 0) {
  133. //待测点与基准线点的连线
  134. lineService.createByPointId(
  135. basePoint.vectorId,
  136. testPointId,
  137. VectorCategory.Line.PositionLine
  138. );
  139. } else {
  140. //取上一个待测点
  141. lineService.createByPointId(
  142. this.testPointIds[this.testPointIds.length - 1],
  143. testPointId,
  144. VectorCategory.Line.PositionLine
  145. );
  146. testPoint.setLinkedTestPointId(
  147. this.testPointIds[this.testPointIds.length - 1]
  148. );
  149. }
  150. }
  151. //更新待测点(综合定位法)
  152. updateTestPointByAll(testPointId, newPosition) {
  153. let testPoint = dataService.getPoint(testPointId);
  154. mathUtil.clonePoint(testPoint, newPosition);
  155. let basePoint = dataService.getPoint(testPoint.getLinkedBasePointId());
  156. let lineGeometry = dataService.getLine(Settings.baseLineId);
  157. let startPoint = dataService.getPoint(lineGeometry.startId);
  158. let endPoint = dataService.getPoint(lineGeometry.endId);
  159. let line = mathUtil.createLine1(startPoint, endPoint);
  160. let join = getJoinLinePoint(testPoint, line);
  161. let testBasePoint = this.getTestBasePoint(basePoint.vectorId, testPointId);
  162. mathUtil.clonePoint(testBasePoint, join);
  163. }
  164. setLocationByNormal(testPointId) {
  165. let testPoint = dataService.getPoint(testPointId);
  166. if (testPoint.getCategory() != VectorCategory.Point.TestPoint) {
  167. return;
  168. }
  169. let lineGeometry = dataService.getLine(Settings.baseLineId);
  170. let startPoint = dataService.getPoint(lineGeometry.startId);
  171. let endPoint = dataService.getPoint(lineGeometry.endId);
  172. let line = mathUtil.createLine1(startPoint, endPoint);
  173. let vLine = mathUtil.getVerticalLine(line, testPoint);
  174. let join = mathUtil.getIntersectionPoint(vLine, line);
  175. join = pointService.create(join);
  176. join.setCategory(VectorCategory.Point.TestBasePoint);
  177. join.setLinkedTestPointId(testPointId);
  178. lineService.createByPointId(
  179. testPointId,
  180. join.vectorId,
  181. VectorCategory.Line.PositionLine
  182. );
  183. }
  184. //更新待测点(自由定位法)
  185. updateTestPointByNormal(testPointId, newPosition) {
  186. let testPoint = dataService.getPoint(testPointId);
  187. mathUtil.clonePoint(testPoint, newPosition);
  188. let basePoint = dataService.getPoint(testPoint.getLinkedBasePointId());
  189. let lineGeometry = dataService.getLine(Settings.baseLineId);
  190. let startPoint = dataService.getPoint(lineGeometry.startId);
  191. let endPoint = dataService.getPoint(lineGeometry.endId);
  192. let line = mathUtil.createLine1(startPoint, endPoint);
  193. let join = getJoinLinePoint(testPoint, line);
  194. let testBasePoint = this.getTestBasePoint(basePoint.vectorId, testPointId);
  195. mathUtil.clonePoint(testBasePoint, join);
  196. }
  197. }
  198. const addPoint = new AddPoint();
  199. export { addPoint };