ctrlPolygon.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. import * as THREE from "../../../../libs/three.js/build/three.module.js";
  2. import {LineDraw, MeshDraw} from "../../utils/DrawUtil.js";
  3. import math from "../../utils/math.js";
  4. const verticalLine = new THREE.Line3()
  5. //控制点和边的合集。具有可以拖拽修改的功能,拖拽时能防止线相交。
  6. export class ctrlPolygon extends THREE.Object3D {
  7. constructor (type, prop) {
  8. super()
  9. this.type = type
  10. this.maxMarkers = Number.MAX_SAFE_INTEGER;
  11. this.transformData(prop);
  12. for(let i in prop){
  13. this[i] = prop[i]
  14. }
  15. if((this.atPlane || this.showArea) && this.closed && this.dimension == '2d'){
  16. this.areaPlane = this.createAreaPlane();
  17. this.add(this.areaPlane)
  18. }
  19. //数据--刚开始一定是空的
  20. this.points = [];
  21. //mesh 不一定有
  22. this.markers = [];
  23. this.edges = [];
  24. this.center
  25. }
  26. initData(prop){
  27. //开始加数据
  28. prop.dataset_points && (this.dataset_points = prop.dataset_points)
  29. prop.datasetId && (this.datasetId = prop.datasetId)
  30. prop.points_datasets && (this.points_datasets = prop.points_datasets)
  31. if(Potree.settings.editType == 'merge' || this.measureType == 'MulDistance Ring'){ //融合页面没有地图,measure的不需要指定datasetId,每个点都有各自的datasetId,跟着各自的模型走
  32. if(this.dataset_points){
  33. this.dataset_points = this.dataset_points.map(e=>{
  34. return e && new THREE.Vector3().copy(e)
  35. })
  36. prop.points = this.dataset_points.map((p,i)=>{
  37. return Potree.Utils.datasetPosTransform({fromDataset:true, datasetId:this.points_datasets[i], position: p})
  38. })
  39. if(prop.points.some(e=>e == void 0)){
  40. return false
  41. }
  42. }else{
  43. this.dataset_points = []
  44. }
  45. }
  46. if(prop.points){
  47. for(const p of prop.points){
  48. const pos = new THREE.Vector3().copy(p)
  49. this.addMarker({point:pos});
  50. }
  51. if(Potree.settings.editType != 'merge' && this.measureType != 'MulDistance Ring'){
  52. if(this.datasetId != void 0){//初始化位置
  53. if(this.dataset_points){
  54. this.dataset_points = this.dataset_points.map(e=>{
  55. return e && new THREE.Vector3().copy(e)
  56. })
  57. this.transformByPointcloud() //根据dataset_points和this.datasetId生成points
  58. }
  59. }else{
  60. if(prop.dataset_points && prop.dataset_points.some(e=>e != void 0)){
  61. console.error('存在测量线的datasetId为空而dataset_points有值,请检查并删除:'+this.sid)//存在过的bug,原因未知,可能是后台处理dataset时替换的错误:http://192.168.0.21/index.php?m=bug&f=view&bugID=23601
  62. console.log(this)
  63. }
  64. }
  65. }
  66. this.facePlane = this.getFacePlane()
  67. this.getPoint2dInfo(this.points)
  68. this.update({ifUpdateMarkers:true})
  69. //this.dragChange(new THREE.Vector3().copy(prop.points[prop.points.length-1]), prop.points.length-1);
  70. this.setSelected(false )
  71. this.markers.forEach(marker=>{marker.dispatchEvent('addHoverEvent') })
  72. return true
  73. }
  74. }
  75. addMarker(o={}){
  76. var index = o.index == void 0 ? this.points.length : o.index //要当第几个
  77. this.points = [...this.points.slice(0,index), o.point, ...this.points.slice(index,this.points.length)]
  78. //this.points.push(o.point);
  79. if(o.dataset_point){
  80. this.dataset_points = [...this.dataset_points.slice(0,index), o.dataset_point, ...this.dataset_points.slice(index,this.dataset_points.length)]
  81. }
  82. if(o.points_dataset){
  83. this.points_datasets = [...this.points_datasets.slice(0,index), o.points_dataset, ...this.points_datasets.slice(index,this.points_datasets.length)]
  84. }
  85. if(o.marker){
  86. this.add(o.marker)
  87. this.markers = [...this.markers.slice(0,index), o.marker, ...this.markers.slice(index,this.markers.length)]
  88. this.updateMarker(o.marker, o.point)
  89. o.marker.addEventListener('drag', this.dragMarker.bind(this),{importance:2});
  90. o.marker.addEventListener('drop', this.dropMarker.bind(this),{importance:2});
  91. o.marker.createTime = Date.now()
  92. let addHoverEvent = (e)=>{
  93. let mouseover = (e) => {
  94. this.setMarkerSelected(e.object, 'hover', 'single');
  95. viewer.dispatchEvent({
  96. type : "CursorChange", action : "add", name:"markerMove"
  97. })
  98. };
  99. let mouseleave = (e) => {
  100. this.setMarkerSelected(e.object, 'unhover', 'single');
  101. viewer.dispatchEvent({
  102. type : "CursorChange", action : "remove", name:"markerMove"
  103. })
  104. }
  105. o.marker.addEventListener('mouseover', mouseover);
  106. o.marker.addEventListener('mouseleave', mouseleave);
  107. o.marker.addEventListener('startDragging', (e)=>{//for mobile
  108. this.setMarkerSelected(o.marker, 'hover', 'single');
  109. this.dispatchEvent('startDragging')
  110. });
  111. o.marker.addEventListener('drop', (e)=>{//for mobile
  112. this.setMarkerSelected(o.marker, 'unhover', 'single');
  113. });
  114. o.marker.removeEventListener('addHoverEvent',addHoverEvent)
  115. }
  116. o.marker.addEventListener('addHoverEvent',addHoverEvent)//当非isNew时才添加事件
  117. if(!this.isNew){
  118. o.marker.dispatchEvent('addHoverEvent')
  119. }
  120. }
  121. if(o.edge){
  122. this.add(o.edge)
  123. this.edges = [...this.edges.slice(0,index), o.edge, ...this.edges.slice(index,this.edges.length)]
  124. }
  125. }
  126. dragMarker(e){
  127. var I, atMap
  128. if(e.hoverViewport != e.drag.dragViewport){//不能使用e.dragViewport,要使用drag中的,因为drag中存储的要一直继承下来,不因mouseup了而改变。
  129. viewer.dispatchEvent({
  130. type : "CursorChange", action : "add", name:"polygon_AtWrongPlace"
  131. })
  132. return
  133. }
  134. if(e.drag.pointerDelta.length() == 0 && !this.isNew){ //部分设备(华为matePad11)在touchstart后立即执行了touchmove,导致marker立即移动,需要屏蔽..(刚创建时也会是0)
  135. return
  136. }
  137. viewer.dispatchEvent({
  138. type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace"
  139. })
  140. atMap = e.drag.dragViewport.name == 'mapViewport'
  141. if(atMap && this.unableDragAtMap){
  142. e.drag.object = null //取消拖拽
  143. return
  144. }
  145. e.drag.object.isDragging = true
  146. I = e.intersect && (e.intersect.adsorption ? e.intersect.location : (e.intersect.orthoIntersect || e.intersect.location))
  147. if(viewer.inputHandler.pressedKeys[18] || Potree.settings.dragPolyBeyondPoint&&!I ){//alt dragPolyBeyondPoint可以平移拖拽到无点的地方---测试用
  148. let i = this.markers.indexOf(e.drag.object);
  149. I = this.points[i].clone()
  150. const projected = I.clone().project(e.drag.dragViewport.camera);
  151. projected.x = e.pointer.x
  152. projected.y = e.pointer.y
  153. const unprojected = projected.clone().unproject(e.drag.dragViewport.camera);
  154. I.copy(unprojected);
  155. }
  156. if (I) {
  157. let i = this.markers.indexOf(e.drag.object);
  158. if (i !== -1) {
  159. this.dragChange(I.clone(), i, atMap)
  160. if(this.points_datasets){
  161. if(e.intersect.pointcloud) this.points_datasets[i] = e.intersect.pointcloud.dataset_id
  162. else if(e.intersect.object) this.points_datasets[i] = e.intersect.object.dataset_id
  163. else{
  164. if(this.measureType == 'MulDistance Ring'){//因为它的每个point跟着各自的dataset走,而不是整体的dataset
  165. let pointcloud = viewer.findClosestDatasetOnMap(I) || viewer.scene.pointclouds[0]
  166. this.points_datasets[i] = pointcloud.dataset_id
  167. }else this.points_datasets[i] = null
  168. }
  169. }
  170. }
  171. this.editStateChange(true)
  172. return true
  173. }
  174. };
  175. dragChange(intersectPos, i, atMap){
  176. let len = this.markers.length
  177. let oldPoint = this.points[i];
  178. if(atMap){
  179. intersectPos.setZ(oldPoint.z) //在地图上拖拽,不改变其高度。
  180. }
  181. let location = intersectPos.clone()
  182. if(this.faceDirection && this.maxMarkers == 2 && len == 2){//add 固定方向的点不直接拖拽
  183. var p1 = this.markers[0].position
  184. if(this.faceDirection == 'horizontal'){
  185. var projectPos = location.clone().setZ(p1.z)
  186. }else{
  187. var projectPos = p1.clone().setZ(location.z)
  188. }
  189. //var p2 = p1.clone().add(this.direction)
  190. //var projectPos = math.getFootPoint(location, p1, p2)
  191. LineDraw.updateLine(this.guideLine, [location, projectPos])
  192. location = projectPos
  193. this.guideLine.visible = true
  194. }else if( len > 1 ){
  195. var points = this.points.map(e=>e.clone())
  196. points[i].copy(location) //算normal需要提前确认point
  197. //若为定义了面朝向的矩形
  198. if(this.faceDirection == 'horizontal'){
  199. if(len == 2){
  200. location.setZ(points[0].z)
  201. }
  202. if(!this.facePlane){//一个点就能确定面
  203. this.facePlane = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3(0,0,1), this.points[0] )
  204. }
  205. }else if(this.faceDirection == 'vertical'){//当有两个点时, 有两个方向的可能
  206. if(len == 2){
  207. if(this.isRect){
  208. let vec = points[0].clone().sub(location)
  209. if(Math.sqrt(vec.x*vec.x+vec.y*vec.y) > Math.abs(vec.z) ){//水平(高度差小于水平距离时)
  210. location.setZ(points[0].z)
  211. //this.cannotConfirmNormal = false;//能确定面为水平方向
  212. }else{//垂直 (当两点一样时也属于这种)
  213. location.setX(points[0].x);
  214. location.setY(points[0].y);
  215. //this.cannotConfirmNormal = true; //不能确定面,因第三点可绕着纵轴线自由移动
  216. }
  217. }
  218. }else{
  219. {//判断cannotConfirmNormal. 如果前几段都在竖直线上,就不能固定出面方向。
  220. this.cannotConfirmNormal = true
  221. let max = this.isRect ? 1 : len-2
  222. for(let i=0;i<max;i++){
  223. let p1 = points[i].clone()
  224. let p2 = points[i+1].clone()
  225. let vec = p1.sub(p2);
  226. if(vec.x != 0 || vec.y != 0){
  227. this.cannotConfirmNormal = false
  228. break;
  229. }
  230. }
  231. }
  232. if(!this.facePlane || this.cannotConfirmNormal){//三个点且为水平方向时,计算面
  233. var points_ = points.map(e=>new THREE.Vector2(e.x,e.y))
  234. var points2 = getDifferentPoint(points_, 2);
  235. if(points2){
  236. let normal = math.getNormal2d({p1:points2[0], p2:points2[1]})
  237. normal = new THREE.Vector3(normal.x, normal.y, 0)
  238. this.facePlane = new THREE.Plane().setFromNormalAndCoplanarPoint( normal, this.points[0] )
  239. }
  240. }
  241. }
  242. }
  243. if(len > 2){
  244. if(!this.faceDirection && this.showArea){
  245. if(len == 3 || this.isRect) this.cannotConfirmNormal = true //当第三个点固定后(有四个点时)才能固定面
  246. if(!this.facePlane || this.cannotConfirmNormal){
  247. var points3 = getDifferentPoint(points, 3);//只有找到三个不同的点算拥有面和area
  248. if(points3){
  249. this.facePlane = new THREE.Plane().setFromCoplanarPoints(...points3 )
  250. }
  251. }
  252. }
  253. if(this.atPlane && this.facePlane && !this.cannotConfirmNormal ){//之后加的点一定要在面上
  254. if(atMap){
  255. //地图上用垂直线,得到和面的交点。
  256. verticalLine.set(location.clone().setZ(100000), location.clone().setZ(-100000))//确保长度范围覆盖所有测量面
  257. location = this.facePlane.intersectLine(verticalLine, new THREE.Vector3() )
  258. if(!location) return;
  259. }else{
  260. location = this.facePlane.projectPoint(intersectPos, new THREE.Vector3() )
  261. }
  262. }
  263. points[i].copy(location)//再copy确认一次
  264. if(len == 3 && this.faceDirection == 'horizontal' && this.closed){ //normal方向还不确定 会影响label在里侧还是外侧
  265. let facePlane = new THREE.Plane().setFromCoplanarPoints(...points)
  266. if(facePlane.normal.z && facePlane.normal.z * this.facePlane.normal.z < 0){
  267. this.facePlane.normal.z *= -1, this.facePlane.constant *= -1
  268. //console.log(this.facePlane.normal, this.facePlane.constant)
  269. }
  270. }
  271. if(this.isRect){ //是矩形 (即使没有faceDirection也能执行)
  272. //根据前两个点计算当前和下一个点
  273. var p1 = points[(i-2+len)%len]
  274. var p2 = points[(i-1+len)%len]
  275. if(p1.equals(p2)){//意外情况:重复点两次 ( bug点,改了好多遍)
  276. if(this.faceDirection == 'vertical'){
  277. p2.add(new THREE.Vector3(0,0,0.0001))
  278. }else{
  279. p2.add(new THREE.Vector3(0,0.0001,0))
  280. }
  281. }
  282. //p3 : location
  283. var foot = math.getFootPoint(location, p1, p2)//p2 修改p2到垂足的位置
  284. var vec = foot.clone().sub(location)
  285. var p4 = p1.clone().sub(vec)
  286. points[(i-1+len)%len].copy(foot)
  287. points[(i+1)%len].copy(p4)
  288. this.setPosition((i-1+len)%len, foot);//p2
  289. this.setPosition((i+1)%len, p4);
  290. }
  291. this.getPoint2dInfo(points)
  292. var isIntersectSelf = this.atPlane && this.closed && !this.isRect && this.point2dInfo && this.intersectSelf(this.point2dInfo.points2d)//检测相交
  293. this.isIntersectSelf = isIntersectSelf
  294. if(isIntersectSelf){//not-allowed
  295. if(!this.isNew && isIntersectSelf == 'lastLine') this.isIntersectSelf = 'all' //已经画好了就不用特别对待最后一条线
  296. if(this.isIntersectSelf == 'lastLine'){
  297. Potree.Utils.updateVisible(this.areaPlane, 'intersectLastLine', false)
  298. Potree.Utils.updateVisible(this.areaLabel, 'intersectLastLine', false)
  299. }else{
  300. viewer.dispatchEvent({
  301. type : "CursorChange", action : "add", name:"polygon_isIntersectSelf"
  302. })
  303. return
  304. }
  305. }
  306. if(!this.isIntersectSelf){
  307. this.areaPlane && Potree.Utils.updateVisible(this.areaPlane, 'intersectLastLine', true)
  308. this.areaLabel && Potree.Utils.updateVisible(this.areaLabel, 'intersectLastLine', true)
  309. }
  310. if(!this.isIntersectSelf || this.isIntersectSelf == 'lastLine' ){
  311. viewer.dispatchEvent({
  312. type : "CursorChange", action : "remove", name:"polygon_isIntersectSelf"
  313. })
  314. }
  315. }
  316. var showGuideLine = len>1 && (this.faceDirection || len > 3)
  317. if(showGuideLine && this.guideLine){
  318. LineDraw.updateLine(this.guideLine, [intersectPos, location])
  319. this.guideLine.visible = true
  320. }
  321. //console.log(this.points.map(e=>e.toArray()))
  322. }
  323. if(this.restrictArea){
  324. let holes = this.restrictArea.holes.concat(this.restrictArea.parentHoles)
  325. let holesPoints = holes.filter(e=>e!=this && e.points.length>2).map(e=>e.points)
  326. if(!math.isPointInArea(this.restrictArea.points, holesPoints, location)){
  327. viewer.dispatchEvent({
  328. type : "CursorChange", action : "add", name:"polygon_AtWrongPlace"
  329. })
  330. this.isAtWrongPlace = true
  331. return
  332. }
  333. //就不处理相交线了。 有个缺点:floor上的hole可以限制room,但hole不受room限制,会导致room的marker被框在hole里而动不了。只能去调整hole了
  334. }
  335. viewer.dispatchEvent({
  336. type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace"
  337. })
  338. this.isAtWrongPlace = false
  339. this.setPosition(i, location);
  340. this.update({index: this.isRect ? null : i})
  341. this.dispatchEvent({type:'dragChange', index:i})
  342. }
  343. dropMarker(e){
  344. //console.log('dropMarker')
  345. if (this.isNew && e.pressDistance>Potree.config.clickMaxDragDis){//拖拽的话返回
  346. return this.continueDrag(null,e)
  347. }
  348. if(e.hoverViewport != e.drag.dragViewport){//copy from dragMarker, for sitemodel, only mapViewport can be dropped
  349. return this.continueDrag(null,e)
  350. }
  351. if(e.isTouch){
  352. if(e.hoverViewport != viewer.mainViewport && this.unableDragAtMap){
  353. viewer.dispatchEvent({type:'reticule_forbit', v:true})
  354. //console.log('reticule_forbit',true)
  355. return this.continueDrag(null,e)
  356. }else{
  357. viewer.dispatchEvent({type:'reticule_forbit', v:false})
  358. //console.log('reticule_forbit',false)
  359. }
  360. this.isNew && this.dragMarker(e) //isNew触屏点击时必须先更新下点,因为指尖不在屏幕上时没更新。但对已经创建的marker点击时不应该更新
  361. }
  362. if (e.button != THREE.MOUSE.RIGHT && (//右键click的话继续执行,因为会停止
  363. this.isIntersectSelf == 'all' && this.isNew //有线相交了
  364. || this.isAtWrongPlace && this.isNew
  365. || !e.isAtDomElement && this.isNew//如果是刚添加时在其他dom点击, 不要响应
  366. || e.hoverViewport != viewer.mainViewport && this.unableDragAtMap //垂直的测量线不允许在地图上放点
  367. || this.isNew && !getDifferentPoint(this.points, this.points.length ) //不允许和之前的点相同, 但这句在点云稀疏时会导致难结束
  368. )
  369. ){
  370. return this.continueDrag(null,e)
  371. }
  372. //console.log('drop marker' )
  373. let i = this.markers.indexOf(e.drag.object);
  374. if (i !== -1) {
  375. this.dispatchEvent({
  376. 'type': 'marker_dropped',
  377. 'index': i
  378. });
  379. if(this.markers.length>2 && this.facePlane)this.cannotConfirmNormal = false
  380. this.guideLine &&(this.guideLine.visible = false)
  381. }
  382. this.setMarkerSelected(e.drag.object, 'unhover', 'single')
  383. this.editStateChange(false)
  384. e.drag.endDragFun && e.drag.endDragFun(e)// addmarker
  385. //if(this.changeCallBack)this.changeCallBack()
  386. return true
  387. };
  388. getFacePlane(){//最普通一种get方法,根据顶点。且假设所有点已经共面,且不重合
  389. if(/* !this.atPlane || */this.points.length<3) return
  390. /* this.facePlane = new THREE.Plane().setFromCoplanarPoints(...this.points.slice(0,3) ) */
  391. let facePlane = this.facePlane
  392. if(!this.atPlane || !facePlane){//多折线 没有实时更新facePlane所以重新算
  393. let normal = new THREE.Vector3, len = this.points.length - 2
  394. for(let i=0;i<len;i++){ //获取normal的顺序方法必须和setFromCoplanarPoints一致
  395. let vec0 = new THREE.Vector3().subVectors(this.points[i+2], this.points[i+1], )
  396. let vec1 = new THREE.Vector3().subVectors(this.points[i], this.points[i+1])
  397. let nor = vec0.cross(vec1).normalize();
  398. normal.add(nor)
  399. }
  400. normal.normalize();
  401. facePlane = new THREE.Plane().setFromNormalAndCoplanarPoint(normal, this.points[0])
  402. }
  403. return facePlane
  404. }
  405. getPoint2dInfo(points){ //在更新areaplane之前必须更新过point2dInfo
  406. if(this.facePlane && this.atPlane){
  407. var originPoint0 = points[0].clone()
  408. var qua = math.getQuaBetween2Vector(this.facePlane.normal, new THREE.Vector3(0,0,1), new THREE.Vector3(0,0,1));
  409. let points2d = points.map(e=>e.clone().applyQuaternion(qua))
  410. this.point2dInfo = {
  411. originPoint0 ,
  412. points2d,
  413. quaInverse : qua.clone().invert()
  414. }
  415. }
  416. }
  417. setPosition (index, position) {//拖拽后设置位置
  418. let point = this.points[index];
  419. point.copy(position);
  420. /* if(this.datasetId){
  421. this.dataset_points[index] = Potree.Utils.datasetPosTransform({toDataset:true, datasetId:this.datasetId, position:point.clone()})
  422. } */
  423. /* if(Potree.settings.editType == 'merge'){
  424. this.dataset_points[index] = Potree.Utils.datasetPosTransform({toDataset:true,this.points_datasets[i], position:point.clone()})
  425. } */
  426. let marker = this.markers[index];
  427. this.updateMarker(marker, point)
  428. }
  429. updateMarker(marker, pos){
  430. marker.position.copy(pos);
  431. marker.waitUpdate();
  432. }
  433. intersectSelf(points2d){//add
  434. var len = points2d.length
  435. for(var i=0;i<len;i++){
  436. for(var j=i+2;j<len;j++){
  437. if(Math.abs(j-len-i)<2)continue;//不和邻边比
  438. var p1 = points2d[i]
  439. var p2 = points2d[i+1]
  440. var p3 = points2d[j]
  441. var p4 = points2d[(j+1)%len]
  442. if(p1.equals(p2) || p3.equals(p4) || p1.equals(p3) || p2.equals(p3) || p1.equals(p4) || p2.equals(p4))continue
  443. var line1 = [p1,p2]
  444. var line2 = [p3,p4]
  445. var intersect = math.isLineIntersect(line1, line2, false, 0.001)
  446. if(intersect){
  447. if(i==len-1 || j==len-1){//最后一条线。如果还没绘制完,最后的线还没定下,可被允许继续绘制,但无法显示面积。
  448. return 'lastLine'
  449. }else{
  450. return 'all'
  451. }
  452. break
  453. }
  454. }
  455. }
  456. }
  457. removeMarker (index) {
  458. this.points.splice(index, 1);
  459. const marker = this.markers[index]
  460. //this.remove(marker);
  461. this.markers.splice(index, 1);
  462. marker.dispatchEvent({type:'dispose'})
  463. let edgeIndex = index //(index === 0) ? 0 : (index - 1);
  464. const edge = this.edges[edgeIndex]
  465. if(edge){
  466. this.remove(edge);
  467. this.edges.splice(edgeIndex, 1);
  468. edge.dispatchEvent({type:'dispose'})
  469. }
  470. this.point2dInfo && this.point2dInfo.points2d.splice(index, 1); //add
  471. this.dispatchEvent({type:'removeMarker',index,marker})
  472. }
  473. createAreaPlane(mat){
  474. var geometry = new THREE.Geometry();
  475. var mesh = new THREE.Mesh(geometry, mat)
  476. return mesh
  477. }
  478. updateAreaPlane(){
  479. this.areaPlane.geometry.dispose()
  480. if(this.points.length>2){
  481. if(this.point2dInfo){
  482. this.areaPlane.geometry = MeshDraw.getShapeGeo(this.point2dInfo.points2d)
  483. let center = math.getCenterOfGravityPoint(this.point2dInfo.points2d) //重心
  484. let firstPos = this.point2dInfo.points2d[0].clone()
  485. firstPos.z = 0 //因为shape只读取了xy,所以位移下, 再算出最终位置,得到差距
  486. firstPos.applyQuaternion(this.point2dInfo.quaInverse)
  487. let vec = this.point2dInfo.originPoint0.clone().sub(firstPos)
  488. center = new THREE.Vector3(center.x, center.y, 0)
  489. center.applyQuaternion(this.point2dInfo.quaInverse)
  490. this.areaPlane.quaternion.copy(this.point2dInfo.quaInverse)
  491. this.areaPlane.position.copy(vec)
  492. center.add(vec)
  493. this.center = center
  494. }else{//prism
  495. let z = this.horizonZ || 0
  496. let points2d = this.points.map(e=>e.clone().setZ(z))
  497. this.areaPlane.geometry = MeshDraw.getShapeGeo(points2d) //z=0
  498. let center = math.getCenterOfGravityPoint(points2d) //重心
  499. this.areaPlaneCenter = new THREE.Vector3(center.x, center.y, z)
  500. this.areaPlane.position.z = z
  501. }
  502. }else{
  503. this.areaPlane.geometry = new THREE.Geometry()
  504. }
  505. }
  506. getIndex(index, add){
  507. let lastIndex = this.points.length - 1
  508. if(add == -1) return (index === 0) ? lastIndex : index - 1;
  509. else if(add == 1) return (index + 1 > lastIndex) ? 0 : index + 1;
  510. }
  511. update(options={}){
  512. if(this.points.length === 0){
  513. return;
  514. }
  515. //performance.mark('measureUpdate-start')
  516. let lastIndex = this.points.length - 1
  517. if(options.index != void 0){//更新第几个点
  518. this.updateMarker(this.markers[options.index], this.points[options.index])
  519. let previousIndex = this.getIndex(options.index, -1)
  520. let nextIndex = this.getIndex(options.index, +1)
  521. if( this.closed || nextIndex != 0 ) LineDraw.updateLine( this.edges[options.index], [this.points[options.index], this.points[nextIndex]])
  522. if( this.closed || previousIndex != lastIndex ) LineDraw.updateLine( this.edges[previousIndex], [this.points[options.index], this.points[previousIndex]])
  523. }else{
  524. for (let index = 0; index <= lastIndex; index++) {
  525. let nextIndex = this.getIndex(index, +1)
  526. let previousIndex = this.getIndex(index, -1)
  527. let point = this.points[index];
  528. let nextPoint = this.points[nextIndex];
  529. let previousPoint = this.points[previousIndex];
  530. if(options.ifUpdateMarkers){
  531. this.updateMarker(this.markers[index], point)
  532. }
  533. if(!this.closed && nextIndex == 0 )break; //add
  534. {
  535. let edge = this.edges[index];
  536. if(edge){
  537. LineDraw.updateLine(edge, [point, nextPoint])
  538. }
  539. }
  540. }
  541. }
  542. if(this.areaPlane){
  543. this.updateAreaPlane()
  544. }
  545. //this.dispatchEvent({type:'update'})
  546. viewer.dispatchEvent('content_changed')
  547. viewer.mapViewer && viewer.mapViewer.dispatchEvent('content_changed') //暂时先这么都通知
  548. //performance.mark('measureUpdate-end')
  549. //let measure = performance.measure('measure-','measureUpdate-start', 'measureUpdate-end' );
  550. //console.log('update-time', measure.duration)
  551. }
  552. createPrismLines(color){
  553. this.lineMesh = LineDraw.createLine([],{color})
  554. this.lineMesh.name = 'PrismLines'
  555. this.add(this.lineMesh)
  556. }
  557. updatePrismLines(){
  558. if(!this.lineMesh)return
  559. let positions = [];
  560. let length = this.points.length
  561. this.points.forEach((point, index)=>{
  562. //竖线:
  563. positions.push(point.clone().setZ(this.zMin), point.clone().setZ(this.zMax))
  564. //横线
  565. let nextPoint = this.points[(index+1)%length];
  566. if(!nextPoint)return;//when length==1
  567. positions.push(point.clone().setZ(this.zMax), nextPoint.clone().setZ(this.zMax))//上横线
  568. positions.push(point.clone().setZ(this.zMin), nextPoint.clone().setZ(this.zMin))//下横线
  569. })
  570. LineDraw.moveLine(this.lineMesh,positions)
  571. viewer.dispatchEvent('content_changed')
  572. }
  573. dispose(){//add
  574. this.parent.remove(this)
  575. this.markers.concat(this.edges).forEach(e=>e.dispatchEvent({type:'dispose'}))
  576. }
  577. reDraw(restMarkerCount=0){//重新开始画
  578. let pointCount = this.points.length - restMarkerCount // restMarkerCount为需要留下的marker数量
  579. while(pointCount > 0){
  580. this.removeMarker(--pointCount)
  581. }
  582. this.point2dInfo = null
  583. this.facePlane = null
  584. }
  585. setMarkerSelected(){}
  586. editStateChange(state){
  587. if(!state){
  588. viewer.dispatchEvent({
  589. type : "CursorChange", action : "remove", name:"polygon_isIntersectSelf"
  590. })
  591. viewer.dispatchEvent({
  592. type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace"
  593. })
  594. viewer.dispatchEvent({type:'reticule_forbit', v:false})
  595. this.markers.forEach(e=>e.isDragging = false )
  596. }
  597. }
  598. transformData(prop){
  599. const pick = (defaul, alternative) => {
  600. if(defaul != null){
  601. return defaul;
  602. }else{
  603. return alternative;
  604. }
  605. };
  606. prop.showDistances = (prop.showDistances === null) ? true : prop.showDistances;
  607. prop.showArea = pick(prop.showArea, false);
  608. prop.showAngles = pick(prop.showAngles, false);
  609. prop.showCoordinates = pick(prop.showCoordinates, false);
  610. prop.showHeight = pick(prop.showHeight, false);
  611. prop.showCircle = pick(prop.showCircle, false);
  612. prop.showAzimuth = pick(prop.showAzimuth, false);
  613. prop.showEdges = pick(prop.showEdges, true);
  614. prop.closed = pick(prop.closed, false);
  615. prop.maxMarkers = pick(prop.maxMarkers, Infinity);
  616. prop.direction = prop.direction//add
  617. prop.type = prop.type
  618. prop.showGuideLine = pick(prop.showGuideLine, false);
  619. prop.isRect = pick(prop.isRect, false);
  620. }
  621. setSelected(){}
  622. continueDrag(marker, e){
  623. let object = marker || e.drag.object
  624. object.isDragging = true
  625. this.editStateChange(true)
  626. var timer = setTimeout(()=>{//等 drag=null之后 //右键拖拽结束后需要重新得到drag
  627. if(this.parent && object.parent && object.isDragging){
  628. //console.log('continueDrag')
  629. viewer.inputHandler.startDragging( object ,
  630. {endDragFun: e.drag.endDragFun, notPressMouse:e.drag.notPressMouse, dragViewport:e.drag.dragViewport}
  631. )
  632. }
  633. },1)
  634. return timer
  635. }
  636. }
  637. function getDifferentPoint(points, count){//for facePlane
  638. var result = [];
  639. for(let i=0;i<points.length;i++){
  640. var p = points[i];
  641. if(result.find(e=>e.equals(p)))continue;
  642. else result.push(p)
  643. if(result.length == count)break
  644. }
  645. if(result.length == count)return result
  646. }