popindoorAPI2.7.1.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. //V2.7.1 更改了CAD高亮HighlightSiteModelLayer,修改了带孔洞CAD的高亮效果
  2. /*3、区域模型高亮显示(改成了矢量地图,图层为room,share,balcony,重点是选中矢量面的高度设置,一定要吻合地面*/
  3. var HighlightSiteModelLayer = function(main_view, main_scene, map_view, map_scene) {
  4. var geo_area = new IV.THREE.PlaneGeometry(0.01, 0.01, 32); //随便创建个高亮用的网格,不创建会警告
  5. var mat_area = new IV.THREE.MeshBasicMaterial({ color: 0xffff00, transparent: true, opacity: 0.3 });
  6. //mat_area.depthTest=false;
  7. var mat_room = new IV.THREE.MeshLambertMaterial({ color: 0xffff00, transparent: true, opacity: 1 });
  8. var camera = main_view.getCamera();
  9. mat_area.side = IV.THREE.DoubleSide;
  10. //mat_area.depthTest=false;
  11. mat_room.side = IV.THREE.DoubleSide;
  12. mat_room.renderOrder = 2;
  13. mat_area.renderOrder = 3;
  14. //mat_room.depthTest=false;
  15. mat_room.transparent = false;
  16. mat_room.colorWrite = false;
  17. var high_light_geometry;
  18. var locked = false;
  19. var mesh_area = new IV.THREE.Mesh(geo_area, mat_area);
  20. map_scene.add(mesh_area);
  21. var geo_area_main = new IV.THREE.PlaneGeometry(0.01, 0.01, 32); //随便创建个高亮用的网格,不创建会警告
  22. var mesh_area_main = new IV.THREE.Mesh(geo_area_main, mat_area);
  23. var attributes = [{ name: "room", color: 0xffff00 }, { name: "share", color: 0x0000ff }, { name: "balcony", color: 0xff00ff }, { name: "park", color: 0x808000 }];
  24. mesh_area_main.position.set(0, 0, -4);
  25. main_view.overlayScene.add(mesh_area_main);
  26. // console.log(mesh_area_main);
  27. var rooms, floor, squareShape, preSelectedId = -1;
  28. var wall_group = new IV.THREE.Group();
  29. main_view.overlayScene.add(wall_group);
  30. var vectorMapRepository = window.IV.injector.get("VectorMapRepository");
  31. var vectorMapService = window.IV.injector.get("VectorMapService");
  32. var geo_service = IV.injector.get('GeoTransformationService');
  33. //console.log(vectorMapService);
  34. /* 以后通过射线获取本层的实际高度,这样高亮的网格能够画再地面上
  35. setTimeout(function(){
  36. var raycastService =window.IV.injector.get("RaycastService");
  37. console.log("settimeour");
  38. var camera = IV.getMainView().getCamera();
  39. var source_position = new IV.THREE.Vector3(camera.position.x, camera.position.y, camera.position.z);
  40. var ray_position = new IV.THREE.Vector3(camera.position.x, camera.position.y, camera.position.z-0.1);
  41. var dataset=IV.getMainView().currentImage.dataset;
  42. console.log(camera.position);
  43. console.log("iv",IV.getMainView().currentImage.location); //dataset
  44. var reponse = raycastService.serverSidePickPromise(source_position, ray_position, dataset);
  45. reponse.then(function (response) { console.log(response);}).catch(function(error){
  46. console.log(error);
  47. });
  48. }, 5000);
  49. * */
  50. // console.log(floor=map_view.FloorChangerService.currentFloors[0]);
  51. //目前每层只支持单个dxf文件来高亮显示
  52. var that = this;
  53. vectorMapRepository.initialized.then(function() {
  54. floor = map_view.FloorChangerService.currentFloors[0];
  55. var vectorFiles = vectorMapRepository.data;
  56. //console.log( that);
  57. var vectorFiles_currentFloor = [];
  58. for (var i = 0; i < vectorFiles.length; i++) {
  59. var vector_zMin = vectorFiles[i].zMin,
  60. vector_zMax = vectorFiles[i].zMax;
  61. var vectorLayers = vectorFiles[i].vectorLayers;
  62. if (vector_zMin < floor.zMin && vector_zMax > floor.zMin && isHightLightVectorLayer(vectorLayers)) { //先判断矢量文件是不是属于这个楼层,再判断是不是有控制高亮的矢量文件
  63. //rooms=vectorLayers;
  64. vectorFiles_currentFloor.push(vectorFiles[i]);
  65. //console.log(rooms);
  66. //draw_walls(rooms,floor);
  67. //break;
  68. }
  69. }
  70. //create_menu(vectorFiles_currentFloor,that);
  71. var current_vectorFileID = 0; //默认为读取的第一个文件矢量文件
  72. switchMap(vectorFiles_currentFloor, current_vectorFileID)
  73. create_menu(vectorFiles_currentFloor, that);
  74. });
  75. function switchMap(vectorFiles_currentFloor, current_vectorFileID) {
  76. var vectorFile = null;
  77. if (vectorFiles_currentFloor.length > 0) {
  78. vectorFile = vectorFiles_currentFloor[current_vectorFileID];
  79. setChooseCADVisbileOnly(vectorFiles_currentFloor, current_vectorFileID); //仅对属于当前楼层的CAD文件操作,设置选中CAD可见,其他不可见
  80. // 画墙壁,起遮挡效果;
  81. rooms = vectorFile.vectorLayers;
  82. draw_walls(rooms, floor);
  83. }
  84. }
  85. function setChooseCADVisbileOnly(vectorFiles_currentFloor, current_vectorFileID) {
  86. for (var i = 0; i < vectorFiles_currentFloor.length; i++) {
  87. var vectorLayers = vectorFiles_currentFloor[i].vectorLayers;
  88. if (i === current_vectorFileID) {
  89. for (var j = 0; j < vectorLayers.length; j++) {
  90. vectorLayers[j].visible = true;
  91. }
  92. } else {
  93. for (var j = 0; j < vectorLayers.length; j++) {
  94. vectorLayers[j].visible = false;
  95. }
  96. }
  97. }
  98. vectorMapService.visibilityUpdateEvent.emit()
  99. }
  100. function create_menu(vectorFiles_currentFloor, that) {
  101. var sub_menu_items = [];
  102. for (var i = 0; i < vectorFiles_currentFloor.length; i++) {
  103. var file_name = vectorFiles_currentFloor[i].name; //用文件名称来作为地图名称
  104. var map_name = file_name.substring(0, file_name.indexOf('.')); //去掉文件名称后缀
  105. var menu_item = {
  106. name: map_name,
  107. icon: "add",
  108. vid: i,
  109. callback: function(e) {
  110. IV.custom.select_map_id = e;
  111. switchMap(vectorFiles_currentFloor, sub_menu_items[e].vid)
  112. //console.log(sub_menu_items[e].vid)
  113. }
  114. };
  115. sub_menu_items.push(menu_item)
  116. }
  117. // add lock highlight
  118. that.onContextMenu = function(e) //点击测量图标执行删除功能
  119. {
  120. var menu_items = [{
  121. name: "地图模式",
  122. icon: "fa fa-list-ul",
  123. items: sub_menu_items
  124. }];
  125. if (rooms && rooms.length > 0) {
  126. // console.log(rooms.length)
  127. if (!locked) {
  128. var menu_item = {
  129. name: "区域锁定",
  130. icon: "fa-lock",
  131. vid: null,
  132. callback: function() {
  133. locked = true;
  134. }
  135. };
  136. menu_items.push(menu_item)
  137. } else {
  138. var menu_item = {
  139. name: "区域解锁",
  140. icon: "fa-unlock",
  141. vid: null,
  142. callback: function() {
  143. locked = false;
  144. }
  145. };
  146. menu_items.push(menu_item)
  147. }
  148. }
  149. return menu_items;
  150. }
  151. }
  152. IV.api.view.floorChangerService.onFloorChange.connect(floorChanged);
  153. function floorChanged(e) {
  154. ///目前每层最多只支持单个dxf文件
  155. var hasHightLightVector = false;
  156. locked = false;
  157. mesh_area.visible = false;
  158. mesh_area_main.visible = false;
  159. preSelectedId = -1;
  160. rooms = undefined;
  161. floor = map_view.FloorChangerService.currentFloors[0];
  162. var vectorFiles = vectorMapRepository.data;
  163. var vectorFiles_currentFloor = []
  164. for (var i = 0; i < vectorFiles.length; i++) {
  165. var vector_zMin = vectorFiles[i].zMin,
  166. vector_zMax = vectorFiles[i].zMax;
  167. var vectorLayers = vectorFiles[i].vectorLayers;
  168. if (vector_zMin < floor.zMin && vector_zMax > floor.zMin && isHightLightVectorLayer(vectorLayers)) { //先判断矢量文件是不是属于这个楼层,再判断是不是有控制高亮的矢量文件
  169. vectorFiles_currentFloor.push(vectorFiles[i]);
  170. hasHightLightVector = true;
  171. }
  172. }
  173. var current_vectorFileID = 0; //默认为读取的第一个文件矢量文件
  174. switchMap(vectorFiles_currentFloor, current_vectorFileID)
  175. create_menu(vectorFiles_currentFloor, that);
  176. // for (var i = 0; i < vectorFiles.length; i++) {
  177. // var vector_zMin=vectorFiles[i].zMin,vector_zMax=vectorFiles[i].zMax;
  178. // var vectorLayers=vectorFiles[i].vectorLayers;
  179. // console.log(vectorLayers);
  180. // if(vector_zMin<floor.zMin&&vector_zMax>floor.zMin&&isHightLightVectorLayer(vectorLayers)) { //先判断矢量文件是不是属于这个楼层,再判断是不是有控制高亮的矢量文件
  181. // rooms=vectorLayers;
  182. // floor=map_view.FloorChangerService.currentFloors[0];
  183. // draw_walls(rooms,floor);
  184. // hasHightLightVector=true;
  185. // break;
  186. // }
  187. // }
  188. if (!hasHightLightVector) {
  189. while (wall_group.children.length) {
  190. wall_group.remove(wall_group.children[0]);
  191. }
  192. }
  193. main_view.invalidateScene();
  194. map_view.invalidateScene();
  195. }
  196. //只要层级名称出现room,share,balcony等就说明是高亮层了
  197. function isHightLightVectorLayer(vectorLayers) {
  198. //console.log(vectorLayers);
  199. for (var j = 0; j < vectorLayers.length; j++) {
  200. for (var a = 0; a < attributes.length; a++) {
  201. if (attributes[a].name === vectorLayers[j].name) {
  202. return true;
  203. }
  204. }
  205. }
  206. return false;
  207. }
  208. function draw_walls(rooms, floor) {
  209. if (floor && rooms && rooms.length > 0) { //画sitmodel 多边形
  210. var zMin = floor.zMin,
  211. zMax = floor.zMax;
  212. var height = (zMax - zMin).toFixed(2);
  213. var extrudeSettings = {
  214. depth: height,
  215. steps: 1,
  216. bevelEnabled: false,
  217. };
  218. for (var i = 0; i < rooms.length; i++) { //这里rooms其实就是层级
  219. var layer = rooms[i];
  220. if (layer.name === "room") { //车位等不是房间,不用有遮挡效果
  221. var entities = layer.vectorEntities;
  222. for (var j = 0; j < entities.length; j++) {
  223. var entity_coord = new Array();
  224. var coordinates = entities[j].vectorEntity.coordinates;
  225. for (var z = 0; z < coordinates.length; z++) {
  226. var local_coordinate = geo_service.vectorToLocal(new IV.THREE.Vector3(coordinates[z].x, coordinates[z].y, 0));
  227. //geo_service.vectorToLocal
  228. entity_coord.push(local_coordinate);
  229. }
  230. var entiryShape = new IV.THREE.Shape();
  231. entiryShape.moveTo(entity_coord[0].x, entity_coord[0].y);
  232. for (var a = 1; a < entity_coord.length; a++) {
  233. entiryShape.lineTo(entity_coord[a].x, entity_coord[a].y);
  234. }
  235. entiryShape.moveTo(entity_coord[0].x, entity_coord[0].y);
  236. var geo_room = new IV.THREE.ExtrudeBufferGeometry(entiryShape, extrudeSettings);
  237. var mesh_room = new IV.THREE.Mesh(geo_room, mat_room);
  238. mesh_room.material.needsUpdate = true;
  239. mesh_room.visible = true;
  240. mesh_room.position.set(0, 0, zMin);
  241. wall_group.add(mesh_room);
  242. }
  243. // console.log("rooms",rooms[i]);
  244. /*
  245. var varray = rooms[i].polygon.boundary;
  246. var roomShape = new IV.THREE.Shape();
  247. roomShape.moveTo(varray[0].x, varray[0].y);
  248. for (var j = 1; j < varray.length; j++) {
  249. roomShape.lineTo(varray[j].x, varray[j].y);
  250. }
  251. roomShape.lineTo(varray[0].x, varray[0].y);
  252. var geo_room = new IV.THREE.ExtrudeBufferGeometry(roomShape, extrudeSettings);
  253. var mesh_room = new IV.THREE.Mesh(geo_room, mat_room);
  254. mesh_room.material.needsUpdate = true;
  255. mesh_room.visible = true;
  256. mesh_room.position.set(0, 0, zMin);
  257. main_scene.add(mesh_room);
  258. */
  259. }
  260. }
  261. //console.log(rooms);
  262. }
  263. }
  264. this.onContextMenu = function() {
  265. if (rooms && rooms.length > 0) {
  266. if (!locked) {
  267. return {
  268. name: "区域锁定",
  269. icon: "fa-lock",
  270. callback: function() {
  271. locked = true;
  272. }
  273. };
  274. } else {
  275. return {
  276. name: "区域解锁",
  277. icon: "fa-unlock",
  278. callback: function() {
  279. locked = false;
  280. }
  281. };
  282. }
  283. }
  284. };
  285. /*
  286. this.onClick=function(){
  287. for (var i = 0; i < rooms.length; i++) {
  288. if(preSelectedId==Number(rooms[i].id)){
  289. SiteModelService.selectModel(rooms[i],false); //显示选中房间
  290. }
  291. }
  292. }
  293. */
  294. var geo_map = new IV.THREE.BufferGeometry();
  295. //var MAX_POINTS = 500;
  296. //var positions_map = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
  297. //geo_map.addAttribute( 'position', new IV.THREE.BufferAttribute( positions_map, 3 ) );
  298. var highligh_mesh_map = new IV.THREE.Mesh(geo_map, mat_area);
  299. //highligh_mesh_map.frustumCulled = false;
  300. map_scene.add(highligh_mesh_map);
  301. var vertex2DArray = [];
  302. var face2DArray = [];
  303. var geo_main = new IV.THREE.BufferGeometry();
  304. // var positions_main = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
  305. //geo_main.addAttribute( 'position', new IV.THREE.BufferAttribute( positions_main, 3 ) );
  306. var highligh_mesh_main = new IV.THREE.Mesh(geo_main, mat_area);
  307. highligh_mesh_main.frustumCulled = false;
  308. main_view.overlayScene.add(highligh_mesh_main);
  309. function inside_least_area(point, rooms) { //判断当前鼠标点point,是不是在多边形顶点构成的数组中
  310. var included_entirs = [];
  311. for (var i = 0; i < rooms.length; i++) { //这里rooms其实就是层级
  312. var layer = rooms[i];
  313. var entities = layer.vectorEntities;
  314. for (var j = 0; j < entities.length; j++) {
  315. var entity_coord = new Array();
  316. var coordinates = entities[j].vectorEntity.coordinates;
  317. for (var z = 0; z < coordinates.length; z++) {
  318. var local_coordinate = geo_service.vectorToLocal(new IV.THREE.Vector3(coordinates[z].x, coordinates[z].y, 0));
  319. entity_coord.push(local_coordinate);
  320. }
  321. if (inside(point, entity_coord) && entity_coord.length >= 3) {
  322. var entity_info = {};
  323. entity_info.id = entities[j].id;
  324. entity_info.name = layer.name;
  325. entity_info.array = entity_coord;
  326. included_entirs.push(entity_info)
  327. }
  328. }
  329. }
  330. return find_holes(included_entirs, rooms)
  331. }
  332. function find_center_point(array) {
  333. var x = 0,
  334. y = 0,
  335. z = 0;
  336. var l = array.length;
  337. for (var i = 0; i < l; i++) {
  338. x = x + array[i].x;
  339. y = y + array[i].y;
  340. z = z + array[i].z;
  341. }
  342. return new IV.THREE.Vector3(x / l, y / l, z / l)
  343. }
  344. function find_holes(included_entirs, rooms) {
  345. var holes = []
  346. if (included_entirs.length === 1) {
  347. //var center_point = find_center_point(included_entirs[0].array)
  348. var poly = included_entirs[0].array;
  349. var poly_area = calcPolygonArea(poly);
  350. for (var i = 0; i < rooms.length; i++) { //这里rooms其实就是层级
  351. var layer = rooms[i];
  352. var entities = layer.vectorEntities;
  353. for (var j = 0; j < entities.length; j++) {
  354. var entity_coord = new Array();
  355. var coordinates = entities[j].vectorEntity.coordinates;
  356. for (var z = 0; z < coordinates.length; z++) {
  357. var local_coordinate = geo_service.vectorToLocal(new IV.THREE.Vector3(coordinates[z].x, coordinates[z].y, 0));
  358. //geo_service.vectorToLocal
  359. entity_coord.push(local_coordinate);
  360. }
  361. var center_point = find_center_point(entity_coord);
  362. var entity_area = calcPolygonArea(entity_coord);
  363. //一个多边形内又孔洞的认定条件:1/内部有重叠多边形,中心点来判断,2/重叠多边形的面积要小于当前多边形
  364. if (inside(center_point, poly) && poly_area > entity_area) {
  365. var entity_info = {};
  366. entity_info.id = entities[j].id;
  367. entity_info.name = layer.name;
  368. entity_info.array = entity_coord;
  369. holes.push(entity_info)
  370. }
  371. }
  372. }
  373. included_entirs[0].holes = holes;
  374. return included_entirs[0]
  375. } else {
  376. var min_area = 100000000;
  377. var min_index = -1;
  378. for (var i = 0; i < included_entirs.length; i++) {
  379. var current_area = calcPolygonArea(included_entirs[i].array);
  380. if (min_area > current_area) {
  381. min_area = current_area;
  382. min_index = i;
  383. }
  384. }
  385. if (min_index !== -1 && included_entirs[min_index].name !== "room") { //图层名称不是"room"的选择最小面积
  386. return included_entirs[min_index]
  387. } else {
  388. var entity_info = {};
  389. entity_info.id = -1;
  390. entity_info.name = "";
  391. entity_info.array = [];
  392. entity_info.holes = []
  393. return entity_info //当处于多重多边形时,就是不需要高亮
  394. }
  395. }
  396. }
  397. function calcPolygonArea(vertices) {
  398. var total = 0;
  399. for (var i = 0, l = vertices.length; i < l; i++) {
  400. var addX = vertices[i].x;
  401. var addY = vertices[i == vertices.length - 1 ? 0 : i + 1].y;
  402. var subX = vertices[i == vertices.length - 1 ? 0 : i + 1].x;
  403. var subY = vertices[i].y;
  404. total += (addX * addY * 0.5);
  405. total -= (subX * subY * 0.5);
  406. }
  407. return Math.abs(total);
  408. }
  409. function make_shape(entity_coord) {
  410. var shape = new IV.THREE.Shape();
  411. if (entity_coord.length > 0)
  412. shape.setFromPoints(entity_coord);
  413. return shape
  414. }
  415. function make_holes(holes) {
  416. var holes_format = []
  417. for (var i = 0; i < holes.length; i++) {
  418. var hs = holes[i].array;
  419. var hole_path = new IV.THREE.Path();
  420. if (hs.length > 0)
  421. hole_path.setFromPoints(hs);
  422. holes_format.push(hole_path);
  423. }
  424. return holes_format
  425. }
  426. this.onMouseMove = function(e) {
  427. if (rooms && !locked) {
  428. var p = map_view.getPositionOnGroundPlane(e.x, e.y, 0);
  429. var cursorInRoom = false;
  430. var entity = inside_least_area(p, rooms);
  431. var curr_id = entity.id;
  432. var room_name = entity.name;
  433. var entity_coord = entity.array;
  434. var entity_coord_shape = make_shape(entity_coord);
  435. if (entity_coord && entity_coord.length && entity_coord.length > 0) {
  436. cursorInRoom = true;
  437. if (preSelectedId !== Number(curr_id)) {
  438. preSelectedId = Number(curr_id);
  439. var color = 0xffa5000;
  440. for (var a = 0; a < attributes.length; a++) {
  441. if (attributes[a].name === room_name) {
  442. color = attributes[a].color;
  443. }
  444. }
  445. if (room_name === "room") { //图层名称是"room"的加洞
  446. entity_coord_shape.holes = make_holes(entity.holes);
  447. }
  448. high_light_geometry = new IV.THREE.ShapeBufferGeometry(entity_coord_shape);
  449. mat_area.color.setHex(color);
  450. highligh_mesh_map.geometry = high_light_geometry;
  451. highligh_mesh_map.visible = true;
  452. highligh_mesh_main.geometry = high_light_geometry;
  453. highligh_mesh_main.visible = true;
  454. highligh_mesh_main.position.z = floor.zMin + 0.2;
  455. map_view.invalidateScene();
  456. main_view.invalidateScene();
  457. }
  458. }
  459. if (!cursorInRoom && preSelectedId !== -1) {
  460. highligh_mesh_map.visible = false;
  461. highligh_mesh_main.visible = false;
  462. mesh_area.visible = false;
  463. mesh_area_main.visible = false;
  464. preSelectedId = -1;
  465. main_view.invalidateScene();
  466. map_view.invalidateScene();
  467. }
  468. }
  469. };
  470. function inside(point, vs) { //判断当前鼠标点point,是不是在多边形顶点构成的数组中
  471. var x = point.x,
  472. y = point.y;
  473. var inside = false;
  474. for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
  475. var xi = vs[i].x,
  476. yi = vs[i].y;
  477. var xj = vs[j].x,
  478. yj = vs[j].y;
  479. var intersect = ((yi > y) != (yj > y)) &&
  480. (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
  481. if (intersect) inside = !inside;
  482. }
  483. return inside;
  484. };
  485. };
  486. /* 查各种地理坐标的功能*/
  487. var LookupGeoLocation = function(main_view, main_scene, map_view) {
  488. if (!IV.custom) //自定义功能集合
  489. IV.custom = {};
  490. var geo_service = IV.injector.get('GeoTransformationService');
  491. var configService = window.IV.injector.get("ConfigService");
  492. var userService = IV.injector.get("UserService");
  493. // var geo_config={};
  494. var menuItems = IV.getMenuItems();
  495. var selected_proj4 = null;
  496. var newItem = {
  497. title: "坐标",
  498. icon: {
  499. className: "fa fa-thumb-tack"
  500. },
  501. isVisible: function() {
  502. return true;
  503. },
  504. isPreviewIconVisible: function() {
  505. return true;
  506. },
  507. order: 40,
  508. template: findUrl() + "template_menu/PickCoordSys.html"
  509. };
  510. newItem.onExit = function() {
  511. selected_proj4 = null;
  512. cursTxt.style.visibility = "hidden";
  513. };
  514. menuItems.push(newItem);
  515. var custom_config = {};
  516. var proj4_setting = null;
  517. var getCustomConfig = configService.configArray.find(function(t) { return t.name === 'core.map.measures_save' });
  518. //configService.saveConfig('core.map.measures_save', "");
  519. if (getCustomConfig.value != null && getCustomConfig.value !== "") {
  520. custom_config = JSON.parse(getCustomConfig.value);
  521. if (custom_config.proj4_setting != null && custom_config.proj4_setting.length > 0) {
  522. proj4_setting = custom_config.proj4_setting;
  523. } else {
  524. default_proj4();
  525. }
  526. } else {
  527. default_proj4();
  528. }
  529. function default_proj4() {
  530. proj4_setting = [{
  531. geo_name: '本地坐标',
  532. geo_proj4: 'local'
  533. }, {
  534. geo_name: 'Wgs84坐标',
  535. geo_proj4: 'world'
  536. }, {
  537. geo_name: 'EPSG 4549:CGCS2000 / 3-degree Gauss-Kruger CM 120E',
  538. geo_proj4: '+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
  539. }];
  540. }
  541. IV.custom.init_coord = function() {
  542. var select_coordinate = document.getElementById("select_coordinate");
  543. var str = '<option value=' + null + '>' + "无" + '</option>';
  544. for (var j = 0; j < proj4_setting.length; j++) {
  545. str += '<option value=' + JSON.stringify(proj4_setting[j].geo_proj4) + '>' + proj4_setting[j].geo_name + '</option>';
  546. if (proj4_setting.geo_proj4 !== "local" && proj4_setting.geo_proj4 !== "world") {
  547. proj4.defs(proj4_setting.geo_name, proj4_setting.geo_proj4);
  548. }
  549. }
  550. select_coordinate.innerHTML = str;
  551. if (!userService.isAdmin()) {
  552. var coord_div = document.getElementById("coord_editor");
  553. document.getElementById("editor_coord_froms").removeChild(coord_div);
  554. }
  555. };
  556. IV.custom.pick_coord = function(e) {
  557. selected_proj4 = e.options[e.selectedIndex];
  558. cursTxt.style.visibility = "hidden";
  559. // console.log(selected_proj4);
  560. };
  561. var MapCursor = function() {
  562. this.onMouseMove = function(e) {
  563. if (selected_proj4 && selected_proj4.value !== "null" && IV.getCurrentCursorPosition() && IV.getCurrentCursorPosition().location) {
  564. var p = map_view.getPositionOnGroundPlane(e.x, e.y, 0);
  565. var offset = $("#insetPanel").position();
  566. var e_offset = { x: e.x + offset.left, y: e.y + offset.top };
  567. handleCursorCoords(p, e_offset);
  568. }
  569. };
  570. this.onContextMenu = function(e) {
  571. if (selected_proj4 && selected_proj4.value !== "null") {
  572. var menu_items = [{
  573. name: "复制坐标",
  574. icon: "copy",
  575. callback: function() {
  576. setClipboardText(cursor_coordinates_forCopy);
  577. }
  578. }];
  579. return menu_items;
  580. }
  581. };
  582. }
  583. var getMapCursor = new MapCursor();
  584. map_view.addToScene(getMapCursor);
  585. var cursor_coordinates;
  586. var cursor_coordinates_forCopy;
  587. var cursTxt = document.getElementById("curscroll");
  588. this.onMouseMove = function(e) {
  589. // console.log(IV.getCurrentCursorPosition());
  590. if (selected_proj4 && selected_proj4.value !== "null" && IV.getCurrentCursorPosition() && IV.getCurrentCursorPosition().location) {
  591. var p = IV.getCurrentCursorPosition().location;
  592. handleCursorCoords(p, e);
  593. }
  594. };
  595. function handleCursorCoords(p, e) {
  596. var global = geo_service.vectorToGlobal(new IV.THREE.Vector3(p.x, p.y, p.z));
  597. if (selected_proj4.value === "local") {
  598. cursor_coordinates = " x: " + p.x.toFixed(2) + "<br/> y: " + p.y.toFixed(2) + "<br/> z: " + p.z.toFixed(2);
  599. cursor_coordinates_forCopy = " { x: " + p.x.toFixed(2) + " , y: " + p.y.toFixed(2) + " , z: " + p.z.toFixed(2) + " }";
  600. cursTxt.innerHTML = cursor_coordinates;
  601. cursTxt.style.left = (e.x + 10) + "px";
  602. } else if (selected_proj4.value === "world") {
  603. cursor_coordinates = " 经度(lon): " + global.x.toFixed(11) + "<br/> 纬度(lat): " + global.y.toFixed(11) + "<br/> 高度(h): " + global.z.toFixed(2);
  604. cursor_coordinates_forCopy = " { lon: " + global.x.toFixed(11) + " , lat: " + global.y.toFixed(11) + " , h: " + global.z.toFixed(2) + " }";
  605. cursTxt.innerHTML = cursor_coordinates;
  606. cursTxt.style.left = (e.x + 20) + "px";
  607. } else {
  608. var custom = proj4(selected_proj4.value).forward([global.x, global.y]);
  609. cursor_coordinates = " X: " + custom[0].toFixed(11) + "<br/>Y: " + custom[1].toFixed(11);
  610. cursor_coordinates_forCopy = " { x: " + custom[0].toFixed(11) + " , y: " + custom[1].toFixed(11) + " }";
  611. cursTxt.innerHTML = cursor_coordinates;
  612. cursTxt.style.left = (e.x + 10) + "px";
  613. }
  614. cursTxt.style.top = (e.y + 15) + "px";
  615. cursTxt.style.backgroundColor = "white";
  616. cursTxt.style.visibility = "visible";
  617. }
  618. this.onContextMenu = function(e) {
  619. if (selected_proj4 && selected_proj4.value !== "null") {
  620. var menu_items = [{
  621. name: "复制坐标",
  622. icon: "copy",
  623. callback: function() {
  624. setClipboardText(cursor_coordinates_forCopy);
  625. }
  626. }];
  627. return menu_items;
  628. }
  629. };
  630. function setClipboardText(text) {
  631. var id = "custom-clipboard-textarea-hidden-id";
  632. var existsTextarea = document.getElementById(id);
  633. if (!existsTextarea) {
  634. var textarea = document.createElement("textarea");
  635. textarea.id = id;
  636. textarea.style.position = 'fixed';
  637. textarea.style.top = 0;
  638. textarea.style.left = 0;
  639. textarea.style.width = '1px';
  640. textarea.style.height = '1px';
  641. textarea.style.padding = 0;
  642. textarea.style.border = 'none';
  643. textarea.style.outline = 'none';
  644. textarea.style.boxShadow = 'none';
  645. textarea.style.background = 'transparent';
  646. document.querySelector("body").appendChild(textarea);
  647. existsTextarea = document.getElementById(id);
  648. }
  649. existsTextarea.value = text;
  650. existsTextarea.select();
  651. try {
  652. document.execCommand('copy');
  653. } catch (err) {
  654. console.log('Unable to copy.');
  655. }
  656. }
  657. IV.custom.save_coord_action = function(e) {
  658. if (userService.isAdmin()) {
  659. var coord_name = document.getElementById("coord_name");
  660. var coord_proj4 = document.getElementById("coord_proj4");
  661. if (coord_name && coord_proj4) {
  662. if (!coord_name.value) {
  663. alert("坐标系名称不能为空!");
  664. return;
  665. }
  666. if (!coord_proj4.value) {
  667. alert("Proj4不能为空!");
  668. return;
  669. }
  670. var geo_coord = {
  671. geo_name: coord_name.value,
  672. geo_proj4: coord_proj4.value
  673. };
  674. try {
  675. proj4.defs(geo_coord.geo_name, geo_coord.geo_proj4);
  676. } catch (err) {
  677. alert("proj4输入错误,无法解析!") // 可执行
  678. return;
  679. }
  680. proj4_setting.push(geo_coord);
  681. coord_name.value = "";
  682. coord_proj4.value = "";
  683. custom_config.proj4_setting = proj4_setting;
  684. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  685. IV.custom.init_coord();
  686. }
  687. } else {
  688. alert("只有管理员权限才可以保存!"); //要换成自定义对话框
  689. }
  690. };
  691. IV.custom.del_coord_action = function(e) {
  692. if (userService.isAdmin()) {
  693. for (var i = 0; i < proj4_setting.length; i++) {
  694. var item = proj4_setting[i];
  695. if (item.geo_proj4 === selected_proj4.value && item.geo_name === selected_proj4.innerText) {
  696. proj4_setting.splice(i, 1);
  697. break;
  698. // console.log("111111");
  699. }
  700. }
  701. custom_config.proj4_setting = proj4_setting;
  702. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  703. IV.custom.init_coord();
  704. } else {
  705. alert("只有管理员权限才可以删除!"); //要换成自定义对话框
  706. }
  707. };
  708. };
  709. var Load3DModel = function(main_view, main_scene, map_view) {
  710. if (!IV.custom) //自定义功能集合
  711. IV.custom = {};
  712. var geo_service = IV.injector.get('GeoTransformationService');
  713. var uploadService = IV.injector.get("UploadService");
  714. var configService = window.IV.injector.get("ConfigService");
  715. var userService = IV.injector.get("UserService");
  716. var sitmodelRepo = IV.api.SiteModelApiService.repository;
  717. var menuItems = IV.getMenuItems();
  718. var newItem = {
  719. title: "加载3D模型",
  720. icon: {
  721. className: "fa fa-thumb-tack"
  722. },
  723. isVisible: function() {
  724. return true;
  725. },
  726. isPreviewIconVisible: function() {
  727. return true;
  728. },
  729. order: 40,
  730. template: findUrl() + "template_menu/LoadModels.html"
  731. };
  732. menuItems.push(newItem);
  733. var custom_config = {};
  734. var getCustomConfig = configService.configArray.find(function(t) { return t.name === 'core.map.measures_save' });
  735. //configService.saveConfig('core.map.measures_save', "");
  736. if (getCustomConfig.value != null && getCustomConfig.value !== "") {
  737. custom_config = JSON.parse(getCustomConfig.value);
  738. // do
  739. } else {
  740. //do
  741. }
  742. IV.custom.refModellDownload = function(e) {
  743. sitmodelRepo.findAll().then(function(buildings) {
  744. var exporter = new IV.THREE.OBJExporter();
  745. for (var i = 0; i < buildings.length; i++) {
  746. var building = buildings[i];
  747. var building_polygon = building.polygon.boundary;
  748. var floors = building.children;
  749. //console.log(building);
  750. for (var j = 0; j < floors.length; j++) {
  751. var floor = floors[j];
  752. var singleGeometry = new IV.THREE.Geometry();
  753. makeRoomsShape(singleGeometry, floor.children, floor.height);
  754. var floor_polygon;
  755. if (floor.polygon == null) {
  756. floor_polygon = building_polygon
  757. } else {
  758. floor_polygon = floor.polygon.boundary;
  759. }
  760. var mesh = makeShape(floor_polygon, floor.height);
  761. mesh.updateMatrix();
  762. singleGeometry.merge(mesh.geometry, mesh.matrix);
  763. var mat = new IV.THREE.MeshLambertMaterial({ color: 0xffff00, transparent: true, opacity: 1 });
  764. var fmesh = new IV.THREE.Mesh(singleGeometry, mat);
  765. var floor_obj_name = "floor_" + floor.name + ".obj";
  766. var result = exporter.parse(fmesh);
  767. download(floor_obj_name, result);
  768. }
  769. }
  770. });
  771. function makeRoomsShape(singleGeometry, children, height) {
  772. for (var i = 0; i < children.length; i++) {
  773. var childs = children[i];
  774. if (childs && childs.children) {
  775. //console.log(childs.children);
  776. for (var j = 0; j < childs.children.length; j++) {
  777. var boundary = childs.children[j].polygon.boundary;
  778. var mesh = makeShape(boundary, height);
  779. mesh.updateMatrix();
  780. singleGeometry.merge(mesh.geometry, mesh.matrix);
  781. }
  782. }
  783. }
  784. }
  785. function makeShape(boundary, height) {
  786. var varray = boundary;
  787. var shape = new IV.THREE.Shape();
  788. shape.moveTo(varray[0].x, varray[0].y);
  789. for (var j = 1; j < varray.length; j++) {
  790. shape.lineTo(varray[j].x, varray[j].y);
  791. }
  792. shape.lineTo(varray[0].x, varray[0].y);
  793. var extrudeSettings = {
  794. depth: height,
  795. steps: 1,
  796. bevelEnabled: false,
  797. };
  798. var geo = new IV.THREE.ShapeGeometry(shape);
  799. // var mat = new IV.THREE.MeshLambertMaterial({ color: 0xffff00,transparent: true,opacity:1 });
  800. // var fmesh = new IV.THREE.Mesh(geo,mat);
  801. //console.log(geo)
  802. var fmesh = new IV.THREE.Mesh(geo);
  803. return fmesh;
  804. }
  805. function download(filename, text) {
  806. var pom = document.createElement('a');
  807. pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  808. pom.setAttribute('download', filename);
  809. if (document.createEvent) {
  810. var event = document.createEvent('MouseEvents');
  811. event.initEvent('click', true, true);
  812. pom.dispatchEvent(event);
  813. // console.log("12312313")
  814. } else {
  815. pom.click();
  816. }
  817. }
  818. };
  819. };
  820. var addMinMap = function(main_view, main_scene, map_view) {
  821. if (!IV.custom) //自定义功能集合
  822. IV.custom = {};
  823. var geo_service = IV.injector.get('GeoTransformationService');
  824. var uploadService = IV.injector.get("UploadService");
  825. var configService = window.IV.injector.get("ConfigService");
  826. var userService = IV.injector.get("UserService");
  827. var sitmodelRepo = IV.api.SiteModelApiService.repository;
  828. var menuItems = IV.getMenuItems();
  829. var newItem = {
  830. title: "GPS设置",
  831. icon: {
  832. className: "fa fa-thumb-tack"
  833. },
  834. isVisible: function() {
  835. return true;
  836. },
  837. isPreviewIconVisible: function() {
  838. return true;
  839. },
  840. order: 40,
  841. template: findUrl() + "locat/addDataSet.html"
  842. };
  843. menuItems.push(newItem);
  844. };
  845. /*查找本站域名,一个通用的方法*/
  846. function findUrl() {
  847. var pathArray = window.location.pathname.split('/');
  848. var newPath;
  849. for (var i = 0; i < pathArray.length - 1; i++) {
  850. if (newPath)
  851. newPath += pathArray[i] + "/";
  852. else
  853. newPath = pathArray[i] + "/";
  854. }
  855. return window.location.protocol + "//" + window.location.host + newPath;
  856. }
  857. /*所有自定义功能设置*/
  858. var Setting = function() {
  859. var custom_config = {};
  860. var configService = IV.injector.get("ConfigService");
  861. var userService = IV.injector.get("UserService");
  862. var general_setting = null;
  863. var main_view = IV.getMainView();
  864. var main_scene = main_view.scene;
  865. var map_view = IV.getMapView();
  866. var map_scene = map_view.scene;
  867. var camera = main_view.getCamera();
  868. var getSettingConfig = configService.configArray.find(function(t) { return t.name === 'menu.settings.visible' });
  869. if (JSON.parse(getSettingConfig.value) && userService.isAdmin()) {
  870. if (!IV.custom) //自定义功能集合
  871. IV.custom = {};
  872. var menuItems = IV.getMenuItems();
  873. var newItem = {
  874. title: "特色功能",
  875. icon: {
  876. className: "fa fa-cogs"
  877. },
  878. isVisible: function() {
  879. return true;
  880. },
  881. isPreviewIconVisible: function() {
  882. return true;
  883. },
  884. order: 100,
  885. template: findUrl() + "template_menu/Setting.html"
  886. };
  887. menuItems.push(newItem);
  888. }
  889. setting_init();
  890. function setting_init() { //这个方法只执行一次
  891. var getCustomConfig = configService.configArray.find(function(t) { return t.name === 'core.map.measures_save' });
  892. //configService.saveConfig('core.map.measures_save', "");
  893. //console.log(getCustomConfig);
  894. if (getCustomConfig.value != null && getCustomConfig.value !== "") {
  895. custom_config = JSON.parse(getCustomConfig.value);
  896. if (custom_config.general_setting != null) {
  897. general_setting = custom_config.general_setting;
  898. } else {
  899. default_setting();
  900. }
  901. } else {
  902. default_setting();
  903. }
  904. load_menu();
  905. }
  906. IV.custom.settingOnload = function() { //这个方法每次打开侧边菜单栏执行
  907. document.getElementById("setting_sitemodel").checked = general_setting.subArea;
  908. document.getElementById("setting_coord").checked = general_setting.cursorPosition;
  909. document.getElementById("setting_map_switch").checked = general_setting.switchMap;
  910. document.getElementById("setting_3dmodel").checked = general_setting.loadl3DModels;
  911. };
  912. function default_setting() {
  913. general_setting = {
  914. subArea: false,
  915. cursorPosition: false,
  916. switchMap: false,
  917. loadl3DModels: false,
  918. map: true
  919. };
  920. }
  921. function load_menu() {
  922. if (general_setting.cursorPosition) {
  923. var lookupGeoLocation = new LookupGeoLocation(main_view, main_scene, map_view, map_scene);
  924. main_view.addToScene(lookupGeoLocation);
  925. }
  926. if (general_setting.loadl3DModels) {
  927. var load3DModel = new Load3DModel(main_view, main_scene, map_view, map_scene);
  928. main_view.addToScene(load3DModel);
  929. }
  930. // if(general_setting.switchMap){
  931. // var cadMapChnager = new CADMapChanger(map_view, map_scene);
  932. // map_view.addToScene(cadMapChnager);
  933. // }
  934. if (general_setting.subArea) {
  935. var highlightSiteModelLayer = new HighlightSiteModelLayer(main_view, main_scene, map_view, map_scene);
  936. map_view.addToScene(highlightSiteModelLayer);
  937. }
  938. //if(general_setting.map){
  939. var myMap = new addMinMap(main_view, main_scene, map_view, map_scene);
  940. main_view.addToScene(myMap);
  941. //}
  942. }
  943. IV.custom.settingSave = function(e) {
  944. general_setting.subArea = document.getElementById("setting_sitemodel").checked;
  945. general_setting.cursorPosition = document.getElementById("setting_coord").checked;
  946. general_setting.switchMap = document.getElementById("setting_map_switch").checked;
  947. general_setting.loadl3DModels = document.getElementById("setting_3dmodel").checked;
  948. custom_config.general_setting = general_setting;
  949. // console.log(custom_config);
  950. if (userService.isAdmin()) {
  951. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  952. alert("请注意刷新网页后才能生效!"); //要换成自定义对话框
  953. } else {
  954. alert("只有管理员权限才可以保存!"); //要换成自定义对话框
  955. }
  956. };
  957. };