popindoorAPI2.7.1.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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: '大地2000坐标',
  535. geo_proj4: 'world'
  536. }
  537. // , {
  538. // geo_name: 'EPSG 4549:CGCS2000 / 3-degree Gauss-Kruger CM 120E',
  539. // geo_proj4: '+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
  540. // }
  541. ];
  542. }
  543. IV.custom.init_coord = function() {
  544. var select_coordinate = document.getElementById("select_coordinate");
  545. var str = '<option value=' + null + '>' + "无" + '</option>';
  546. for (var j = 0; j < proj4_setting.length; j++) {
  547. str += '<option value=' + JSON.stringify(proj4_setting[j].geo_proj4) + '>' + proj4_setting[j].geo_name + '</option>';
  548. if (proj4_setting.geo_proj4 !== "local" && proj4_setting.geo_proj4 !== "world") {
  549. proj4.defs(proj4_setting.geo_name, proj4_setting.geo_proj4);
  550. }
  551. }
  552. select_coordinate.innerHTML = str;
  553. if (!userService.isAdmin()) {
  554. var coord_div = document.getElementById("coord_editor");
  555. document.getElementById("editor_coord_froms").removeChild(coord_div);
  556. }
  557. };
  558. IV.custom.pick_coord = function(e) {
  559. selected_proj4 = e.options[e.selectedIndex];
  560. cursTxt.style.visibility = "hidden";
  561. // console.log(selected_proj4);
  562. };
  563. var MapCursor = function() {
  564. this.onMouseMove = function(e) {
  565. if (selected_proj4 && selected_proj4.value !== "null" && IV.getCurrentCursorPosition() && IV.getCurrentCursorPosition().location) {
  566. var p = map_view.getPositionOnGroundPlane(e.x, e.y, 0);
  567. var offset = $("#insetPanel").position();
  568. var e_offset = { x: e.x + offset.left, y: e.y + offset.top };
  569. handleCursorCoords(p, e_offset);
  570. }
  571. };
  572. this.onContextMenu = function(e) {
  573. if (selected_proj4 && selected_proj4.value !== "null") {
  574. var menu_items = [{
  575. name: "复制坐标",
  576. icon: "copy",
  577. callback: function() {
  578. setClipboardText(cursor_coordinates_forCopy);
  579. }
  580. }];
  581. return menu_items;
  582. }
  583. };
  584. }
  585. var getMapCursor = new MapCursor();
  586. map_view.addToScene(getMapCursor);
  587. var cursor_coordinates;
  588. var cursor_coordinates_forCopy;
  589. var cursTxt = document.getElementById("curscroll");
  590. this.onMouseMove = function(e) {
  591. // console.log(IV.getCurrentCursorPosition());
  592. if (selected_proj4 && selected_proj4.value !== "null" && IV.getCurrentCursorPosition() && IV.getCurrentCursorPosition().location) {
  593. var p = IV.getCurrentCursorPosition().location;
  594. handleCursorCoords(p, e);
  595. }
  596. };
  597. function handleCursorCoords(p, e) {
  598. var global = geo_service.vectorToGlobal(new IV.THREE.Vector3(p.x, p.y, p.z));
  599. if (selected_proj4.value === "local") {
  600. cursor_coordinates = " x: " + p.x.toFixed(2) + "<br/> y: " + p.y.toFixed(2) + "<br/> z: " + p.z.toFixed(2);
  601. cursor_coordinates_forCopy = " { x: " + p.x.toFixed(2) + " , y: " + p.y.toFixed(2) + " , z: " + p.z.toFixed(2) + " }";
  602. cursTxt.innerHTML = cursor_coordinates;
  603. cursTxt.style.left = (e.x + 10) + "px";
  604. } else if (selected_proj4.value === "world") {
  605. cursor_coordinates = " 经度(lon): " + global.x.toFixed(11) + "<br/> 纬度(lat): " + global.y.toFixed(11) + "<br/> 高度(h): " + global.z.toFixed(2);
  606. cursor_coordinates_forCopy = " { lon: " + global.x.toFixed(11) + " , lat: " + global.y.toFixed(11) + " , h: " + global.z.toFixed(2) + " }";
  607. cursTxt.innerHTML = cursor_coordinates;
  608. cursTxt.style.left = (e.x + 20) + "px";
  609. } else {
  610. var custom = proj4(selected_proj4.value).forward([global.x, global.y]);
  611. cursor_coordinates = " X: " + custom[0].toFixed(11) + "<br/>Y: " + custom[1].toFixed(11);
  612. cursor_coordinates_forCopy = " { x: " + custom[0].toFixed(11) + " , y: " + custom[1].toFixed(11) + " }";
  613. cursTxt.innerHTML = cursor_coordinates;
  614. cursTxt.style.left = (e.x + 10) + "px";
  615. }
  616. cursTxt.style.top = (e.y + 15) + "px";
  617. cursTxt.style.backgroundColor = "white";
  618. cursTxt.style.visibility = "visible";
  619. }
  620. this.onContextMenu = function(e) {
  621. if (selected_proj4 && selected_proj4.value !== "null") {
  622. var menu_items = [{
  623. name: "复制坐标",
  624. icon: "copy",
  625. callback: function() {
  626. setClipboardText(cursor_coordinates_forCopy);
  627. }
  628. }];
  629. return menu_items;
  630. }
  631. };
  632. function setClipboardText(text) {
  633. var id = "custom-clipboard-textarea-hidden-id";
  634. var existsTextarea = document.getElementById(id);
  635. if (!existsTextarea) {
  636. var textarea = document.createElement("textarea");
  637. textarea.id = id;
  638. textarea.style.position = 'fixed';
  639. textarea.style.top = 0;
  640. textarea.style.left = 0;
  641. textarea.style.width = '1px';
  642. textarea.style.height = '1px';
  643. textarea.style.padding = 0;
  644. textarea.style.border = 'none';
  645. textarea.style.outline = 'none';
  646. textarea.style.boxShadow = 'none';
  647. textarea.style.background = 'transparent';
  648. document.querySelector("body").appendChild(textarea);
  649. existsTextarea = document.getElementById(id);
  650. }
  651. existsTextarea.value = text;
  652. existsTextarea.select();
  653. try {
  654. document.execCommand('copy');
  655. } catch (err) {
  656. console.log('Unable to copy.');
  657. }
  658. }
  659. IV.custom.save_coord_action = function(e) {
  660. if (userService.isAdmin()) {
  661. var coord_name = document.getElementById("coord_name");
  662. var coord_proj4 = document.getElementById("coord_proj4");
  663. if (coord_name && coord_proj4) {
  664. if (!coord_name.value) {
  665. alert("坐标系名称不能为空!");
  666. return;
  667. }
  668. if (!coord_proj4.value) {
  669. alert("Proj4不能为空!");
  670. return;
  671. }
  672. var geo_coord = {
  673. geo_name: coord_name.value,
  674. geo_proj4: coord_proj4.value
  675. };
  676. try {
  677. proj4.defs(geo_coord.geo_name, geo_coord.geo_proj4);
  678. } catch (err) {
  679. alert("proj4输入错误,无法解析!") // 可执行
  680. return;
  681. }
  682. proj4_setting.push(geo_coord);
  683. coord_name.value = "";
  684. coord_proj4.value = "";
  685. custom_config.proj4_setting = proj4_setting;
  686. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  687. IV.custom.init_coord();
  688. }
  689. } else {
  690. alert("只有管理员权限才可以保存!"); //要换成自定义对话框
  691. }
  692. };
  693. IV.custom.del_coord_action = function(e) {
  694. if (userService.isAdmin()) {
  695. for (var i = 0; i < proj4_setting.length; i++) {
  696. var item = proj4_setting[i];
  697. if (item.geo_proj4 === selected_proj4.value && item.geo_name === selected_proj4.innerText) {
  698. proj4_setting.splice(i, 1);
  699. break;
  700. // console.log("111111");
  701. }
  702. }
  703. custom_config.proj4_setting = proj4_setting;
  704. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  705. IV.custom.init_coord();
  706. } else {
  707. alert("只有管理员权限才可以删除!"); //要换成自定义对话框
  708. }
  709. };
  710. };
  711. var Load3DModel = function(main_view, main_scene, map_view) {
  712. if (!IV.custom) //自定义功能集合
  713. IV.custom = {};
  714. var geo_service = IV.injector.get('GeoTransformationService');
  715. var uploadService = IV.injector.get("UploadService");
  716. var configService = window.IV.injector.get("ConfigService");
  717. var userService = IV.injector.get("UserService");
  718. var sitmodelRepo = IV.api.SiteModelApiService.repository;
  719. var menuItems = IV.getMenuItems();
  720. var newItem = {
  721. title: "加载3D模型",
  722. icon: {
  723. className: "fa fa-thumb-tack"
  724. },
  725. isVisible: function() {
  726. return true;
  727. },
  728. isPreviewIconVisible: function() {
  729. return true;
  730. },
  731. order: 40,
  732. template: findUrl() + "template_menu/LoadModels.html"
  733. };
  734. menuItems.push(newItem);
  735. var custom_config = {};
  736. var getCustomConfig = configService.configArray.find(function(t) { return t.name === 'core.map.measures_save' });
  737. //configService.saveConfig('core.map.measures_save', "");
  738. if (getCustomConfig.value != null && getCustomConfig.value !== "") {
  739. custom_config = JSON.parse(getCustomConfig.value);
  740. // do
  741. } else {
  742. //do
  743. }
  744. IV.custom.refModellDownload = function(e) {
  745. sitmodelRepo.findAll().then(function(buildings) {
  746. var exporter = new IV.THREE.OBJExporter();
  747. for (var i = 0; i < buildings.length; i++) {
  748. var building = buildings[i];
  749. var building_polygon = building.polygon.boundary;
  750. var floors = building.children;
  751. //console.log(building);
  752. for (var j = 0; j < floors.length; j++) {
  753. var floor = floors[j];
  754. var singleGeometry = new IV.THREE.Geometry();
  755. makeRoomsShape(singleGeometry, floor.children, floor.height);
  756. var floor_polygon;
  757. if (floor.polygon == null) {
  758. floor_polygon = building_polygon
  759. } else {
  760. floor_polygon = floor.polygon.boundary;
  761. }
  762. var mesh = makeShape(floor_polygon, floor.height);
  763. mesh.updateMatrix();
  764. singleGeometry.merge(mesh.geometry, mesh.matrix);
  765. var mat = new IV.THREE.MeshLambertMaterial({ color: 0xffff00, transparent: true, opacity: 1 });
  766. var fmesh = new IV.THREE.Mesh(singleGeometry, mat);
  767. var floor_obj_name = "floor_" + floor.name + ".obj";
  768. var result = exporter.parse(fmesh);
  769. download(floor_obj_name, result);
  770. }
  771. }
  772. });
  773. function makeRoomsShape(singleGeometry, children, height) {
  774. for (var i = 0; i < children.length; i++) {
  775. var childs = children[i];
  776. if (childs && childs.children) {
  777. //console.log(childs.children);
  778. for (var j = 0; j < childs.children.length; j++) {
  779. var boundary = childs.children[j].polygon.boundary;
  780. var mesh = makeShape(boundary, height);
  781. mesh.updateMatrix();
  782. singleGeometry.merge(mesh.geometry, mesh.matrix);
  783. }
  784. }
  785. }
  786. }
  787. function makeShape(boundary, height) {
  788. var varray = boundary;
  789. var shape = new IV.THREE.Shape();
  790. shape.moveTo(varray[0].x, varray[0].y);
  791. for (var j = 1; j < varray.length; j++) {
  792. shape.lineTo(varray[j].x, varray[j].y);
  793. }
  794. shape.lineTo(varray[0].x, varray[0].y);
  795. var extrudeSettings = {
  796. depth: height,
  797. steps: 1,
  798. bevelEnabled: false,
  799. };
  800. var geo = new IV.THREE.ShapeGeometry(shape);
  801. // var mat = new IV.THREE.MeshLambertMaterial({ color: 0xffff00,transparent: true,opacity:1 });
  802. // var fmesh = new IV.THREE.Mesh(geo,mat);
  803. //console.log(geo)
  804. var fmesh = new IV.THREE.Mesh(geo);
  805. return fmesh;
  806. }
  807. function download(filename, text) {
  808. var pom = document.createElement('a');
  809. pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  810. pom.setAttribute('download', filename);
  811. if (document.createEvent) {
  812. var event = document.createEvent('MouseEvents');
  813. event.initEvent('click', true, true);
  814. pom.dispatchEvent(event);
  815. // console.log("12312313")
  816. } else {
  817. pom.click();
  818. }
  819. }
  820. };
  821. };
  822. var addMinMap = function(main_view, main_scene, map_view) {
  823. if (!IV.custom) //自定义功能集合
  824. IV.custom = {};
  825. var geo_service = IV.injector.get('GeoTransformationService');
  826. var uploadService = IV.injector.get("UploadService");
  827. var configService = window.IV.injector.get("ConfigService");
  828. var userService = IV.injector.get("UserService");
  829. var sitmodelRepo = IV.api.SiteModelApiService.repository;
  830. var menuItems = IV.getMenuItems();
  831. var newItem = {
  832. title: "控制点",
  833. icon: {
  834. className: "fa fa-thumb-tack"
  835. },
  836. isVisible: function() {
  837. return true;
  838. },
  839. isPreviewIconVisible: function() {
  840. return true;
  841. },
  842. order: 40,
  843. template: findUrl() + "locat/addDataSet.html"
  844. };
  845. menuItems.push(newItem);
  846. };
  847. /*查找本站域名,一个通用的方法*/
  848. function findUrl() {
  849. var pathArray = window.location.pathname.split('/');
  850. var newPath;
  851. for (var i = 0; i < pathArray.length - 1; i++) {
  852. if (newPath)
  853. newPath += pathArray[i] + "/";
  854. else
  855. newPath = pathArray[i] + "/";
  856. }
  857. return window.location.protocol + "//" + window.location.host + newPath;
  858. }
  859. /*所有自定义功能设置*/
  860. var Setting = function() {
  861. var custom_config = {};
  862. var configService = IV.injector.get("ConfigService");
  863. var userService = IV.injector.get("UserService");
  864. var general_setting = null;
  865. var main_view = IV.getMainView();
  866. console.log('!!!!!!!!!!!!!!!!!!!!!!!')
  867. console.log(main_view)
  868. var main_scene = main_view.scene;
  869. var map_view = IV.getMapView();
  870. var map_scene = map_view.scene;
  871. var camera = main_view.getCamera();
  872. var getSettingConfig = configService.configArray.find(function(t) { return t.name === 'menu.settings.visible' });
  873. if (JSON.parse(getSettingConfig.value) && userService.isAdmin()) {
  874. if (!IV.custom) //自定义功能集合
  875. IV.custom = {};
  876. var menuItems = IV.getMenuItems();
  877. var newItem = {
  878. title: "特色功能",
  879. icon: {
  880. className: "fa fa-cogs"
  881. },
  882. isVisible: function() {
  883. return true;
  884. },
  885. isPreviewIconVisible: function() {
  886. return true;
  887. },
  888. order: 100,
  889. template: findUrl() + "template_menu/Setting.html"
  890. };
  891. menuItems.push(newItem);
  892. }
  893. setting_init();
  894. function setting_init() { //这个方法只执行一次
  895. var getCustomConfig = configService.configArray.find(function(t) { return t.name === 'core.map.measures_save' });
  896. //configService.saveConfig('core.map.measures_save', "");
  897. //console.log(getCustomConfig);
  898. if (getCustomConfig.value != null && getCustomConfig.value !== "") {
  899. custom_config = JSON.parse(getCustomConfig.value);
  900. if (custom_config.general_setting != null) {
  901. general_setting = custom_config.general_setting;
  902. } else {
  903. default_setting();
  904. }
  905. } else {
  906. default_setting();
  907. }
  908. load_menu();
  909. window.eventBus = new EventBus();
  910. // window.eventBus.on('my-event', function() {
  911. // alert(1)
  912. // load_menu();
  913. // });
  914. }
  915. IV.custom.settingOnload = function() { //这个方法每次打开侧边菜单栏执行
  916. document.getElementById("setting_sitemodel").checked = general_setting.subArea;
  917. document.getElementById("setting_coord").checked = general_setting.cursorPosition;
  918. document.getElementById("setting_map_switch").checked = general_setting.switchMap;
  919. document.getElementById("setting_3dmodel").checked = general_setting.loadl3DModels;
  920. };
  921. function default_setting() {
  922. general_setting = {
  923. subArea: false,
  924. cursorPosition: false,
  925. switchMap: false,
  926. loadl3DModels: false,
  927. map: true
  928. };
  929. }
  930. function load_menu() {
  931. if (general_setting.cursorPosition) {
  932. var lookupGeoLocation = new LookupGeoLocation(main_view, main_scene, map_view, map_scene);
  933. main_view.addToScene(lookupGeoLocation);
  934. }
  935. if (general_setting.loadl3DModels) {
  936. var load3DModel = new Load3DModel(main_view, main_scene, map_view, map_scene);
  937. main_view.addToScene(load3DModel);
  938. }
  939. // if(general_setting.switchMap){
  940. // var cadMapChnager = new CADMapChanger(map_view, map_scene);
  941. // map_view.addToScene(cadMapChnager);
  942. // }
  943. if (general_setting.subArea) {
  944. var highlightSiteModelLayer = new HighlightSiteModelLayer(main_view, main_scene, map_view, map_scene);
  945. map_view.addToScene(highlightSiteModelLayer);
  946. }
  947. if (userService.isAdmin()) {
  948. var myMap = new addMinMap(main_view, main_scene, map_view, map_scene);
  949. main_view.addToScene(myMap);
  950. }
  951. }
  952. IV.custom.settingSave = function(e) {
  953. general_setting.subArea = document.getElementById("setting_sitemodel").checked;
  954. general_setting.cursorPosition = document.getElementById("setting_coord").checked;
  955. general_setting.switchMap = document.getElementById("setting_map_switch").checked;
  956. general_setting.loadl3DModels = document.getElementById("setting_3dmodel").checked;
  957. custom_config.general_setting = general_setting;
  958. // console.log(custom_config);
  959. if (userService.isAdmin()) {
  960. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  961. alert("请注意刷新网页后才能生效!"); //要换成自定义对话框
  962. } else {
  963. alert("只有管理员权限才可以保存!"); //要换成自定义对话框
  964. }
  965. };
  966. };