ConvertCadKjl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. package com.example.demo.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.github.pagehelper.util.StringUtil;
  6. import java.io.*;
  7. import java.util.*;
  8. import java.util.concurrent.atomic.AtomicBoolean;
  9. public class ConvertCadKjl {
  10. // public String path = "G:\\javaProject\\baidu-master\\floor.json";
  11. // public String outpath = "G:\\javaProject\\baidu-master\\kjl.json";
  12. public JSONObject input = null;
  13. public JSONObject output = null;
  14. //重复的墙要删除,这是他们id的映射关系,key表示删除墙的id,value表示重复的且最终留下的墙的id
  15. public JSONObject wallMapping = new JSONObject();
  16. public final double minThickness = 0.01;
  17. //数据预处理,目前暂时处理点位坐标的吸附
  18. //1、坐标吸附
  19. public JSONObject preHandle(JSONObject json) {
  20. //坐标点
  21. JSONArray vertexs = json.getJSONArray("vertex-xy");
  22. for(int i=0;i<vertexs.size();++i) {
  23. JSONObject point1 = vertexs.getJSONObject(i);
  24. for(int j=i+1;j<vertexs.size();++j) {
  25. JSONObject point2 = vertexs.getJSONObject(j);
  26. double disc = getDistance(point1,point2);
  27. //这时候要合并点了。
  28. if(disc <= minThickness) {
  29. point2.put("x", point1.getDouble("x"));
  30. point2.put("y", point1.getDouble("y"));
  31. vertexs.set(j, point2);
  32. }
  33. }
  34. }
  35. json.put("vertex-xy", vertexs);
  36. return json;
  37. }
  38. //计算两点之间的距离
  39. //
  40. public double getDistance(JSONObject p1,JSONObject p2) {
  41. double x1 = p1.getDouble("x");
  42. double y1 = p1.getDouble("y");
  43. double x2 = p2.getDouble("x");
  44. double y2 = p2.getDouble("y");
  45. double disc = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  46. return disc;
  47. }
  48. public void init(JSONObject inputJson) {
  49. output = new JSONObject();
  50. output.put("communityName", "随心装");
  51. output.put("name", "随心装户型");
  52. output.put("province", "");
  53. output.put("height", "2.7");
  54. // readFile(floorPath); //读我们自己的floor.json
  55. input = inputJson;
  56. output.put("holes", new JSONArray());
  57. output.put("flues", new JSONArray());
  58. output.put("beams", new JSONArray());
  59. output.put("rooms", new JSONArray());
  60. output.put("walls", new JSONArray());
  61. }
  62. public static void writeBinary(byte[] buf,String filePath)throws Exception
  63. {
  64. File fout = new File(filePath);
  65. FileOutputStream fos = new FileOutputStream(fout);
  66. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  67. BufferedOutputStream bos = new BufferedOutputStream(fos);//设置输出路径
  68. BufferedInputStream bis = new BufferedInputStream(stream);
  69. int b = -1;
  70. while ((b = bis.read()) != -1) {
  71. bos.write(b);
  72. }
  73. bis.close();
  74. bos.close();
  75. }
  76. //获取酷家乐里,key是wall的值,其实对应我们的是segment字段
  77. public JSONArray getWalls() {
  78. if(input == null) {
  79. return null;
  80. }
  81. else if(input.getJSONArray("segment") == null) {
  82. return null;
  83. }
  84. else if(input.getJSONArray("segment").size() == 0) {
  85. return null;
  86. }
  87. else {
  88. JSONArray walls = input.getJSONArray("segment");
  89. JSONArray points = input.getJSONArray("vertex-xy");
  90. JSONArray result = new JSONArray();
  91. for(int i=0;i<walls.size();++i) {
  92. JSONObject wall = walls.getJSONObject(i);
  93. // if(wall.getInteger("id") == 30){
  94. // System.out.println(30);
  95. // }
  96. JSONObject startJson = getPoint(points,wall.getIntValue("a"));
  97. JSONObject endJson = getPoint(points,wall.getIntValue("b"));
  98. double x = startJson.getDouble("x");
  99. double y = startJson.getDouble("y");
  100. JSONObject start = new JSONObject();
  101. start.put("x", x );
  102. start.put("y", y );
  103. wall.put("start", start);
  104. x = endJson.getDouble("x");
  105. y = endJson.getDouble("y");
  106. JSONObject end = new JSONObject();
  107. end.put("x", x );
  108. end.put("y", y );
  109. wall.put("end", end);
  110. wall.put("thickness", 0.15f);
  111. wall.put("type", "LINE");
  112. wall.put("bulge", 0); //跟曲线有关,我们不是
  113. JSONObject control = new JSONObject();
  114. control.put("x", 0);
  115. control.put("y", 0);
  116. wall.put("control", control); //跟曲线有关,我们不是
  117. wall.put("height", 2.8 ); //默认高度
  118. //wall.remove("a");
  119. //wall.remove("b");
  120. //wall.remove("id");
  121. wall.remove("border");
  122. result.add(wall);
  123. }
  124. return result;
  125. }
  126. }
  127. public JSONObject getPoint(JSONArray points,int id) {
  128. for(int i=0;i<points.size();++i) {
  129. JSONObject point = points.getJSONObject(i);
  130. if(point.getIntValue("id") == id) {
  131. double x = point.getDouble("x");
  132. double y = point.getDouble("y");
  133. JSONObject p = new JSONObject();
  134. p.put("x", x);
  135. p.put("y", y);
  136. return p;
  137. }
  138. }
  139. return null;
  140. }
  141. //获取酷家乐里,key是rooms的值,其实对应我们的是tagging字段
  142. public JSONArray getRooms() {
  143. if(input == null) {
  144. return null;
  145. }
  146. if(input.containsKey("tagging")){
  147. if(input.getJSONArray("tagging") == null) {
  148. return null;
  149. }
  150. else if(input.getJSONArray("tagging").size() == 0) {
  151. return null;
  152. }
  153. else {
  154. JSONArray rooms = input.getJSONArray("tagging");
  155. JSONArray result = new JSONArray();
  156. for(int i=0;i<rooms.size();++i) {
  157. JSONObject item = new JSONObject();
  158. JSONObject room = rooms.getJSONObject(i);
  159. item.put("name", room.getString("title"));
  160. JSONObject position = new JSONObject();
  161. position.put("x", room.getJSONArray("pos").get(0));
  162. position.put("y", room.getJSONArray("pos").get(1));
  163. item.put("position", position);
  164. result.add(item);
  165. }
  166. return result;
  167. }
  168. }
  169. return null;
  170. }
  171. //柱子
  172. //获取酷家乐里,key是beams的值,其实对应我们的是furnFlue字段和furnColumn字段
  173. public JSONArray getBeams() {
  174. if(input == null) {
  175. return null;
  176. }
  177. JSONArray result = new JSONArray();
  178. if(input.containsKey("furnColumn") && input.getJSONArray("furnColumn") != null && input.getJSONArray("furnColumn").size()>0) {
  179. JSONArray furnColumns = input.getJSONArray("furnColumn"); //独立的柱子
  180. result = getColumnsModule(furnColumns);
  181. }
  182. if(input.containsKey("furnFlue") && input.getJSONArray("furnFlue") != null && input.getJSONArray("furnFlue").size()>0) {
  183. JSONArray furnFlues = input.getJSONArray("furnFlue"); //独立的柱子
  184. JSONArray columns = getColumnsModule(furnFlues);
  185. result.addAll(columns);
  186. }
  187. return result;
  188. }
  189. //柱子
  190. //获取酷家乐里,key是flues的值,其实对应我们的是column字段
  191. public JSONArray getFlues() {
  192. if(input == null) {
  193. return null;
  194. }
  195. //int id = 1;
  196. JSONArray result = new JSONArray();
  197. if(input.containsKey("column") && input.getJSONArray("column") != null && input.getJSONArray("column").size()>0) {
  198. JSONArray columns = input.getJSONArray("column"); //非独立的柱子
  199. for(int i=0;i<columns.size();++i) {
  200. JSONObject item = new JSONObject();
  201. JSONArray polygon = new JSONArray();
  202. JSONObject column = columns.getJSONObject(i);
  203. JSONArray pos = column.getJSONArray("pos");
  204. //item.put("id", id);
  205. JSONObject point1 = new JSONObject();
  206. point1.put("x", pos.getDouble(0));
  207. point1.put("y", pos.getDouble(1));
  208. polygon.add(point1);
  209. JSONObject point2 = new JSONObject();
  210. point2.put("x", pos.getDouble(2));
  211. point2.put("y", pos.getDouble(3));
  212. //polygon.add(point2);
  213. JSONObject point3 = new JSONObject();
  214. point3.put("x", pos.getDouble(4));
  215. point3.put("y", pos.getDouble(5));
  216. polygon.add(point3);
  217. JSONObject point4 = new JSONObject();
  218. point4.put("x", pos.getDouble(6));
  219. point4.put("y", pos.getDouble(7));
  220. polygon.add(point4);
  221. //4dkk的这个数据不是按照顺序来的。
  222. polygon.add(point2);
  223. item.put("id", column.getIntValue("line"));
  224. item.put("polygon", polygon);
  225. // ++id;
  226. result.add(item);
  227. }
  228. return result;
  229. }
  230. return null;
  231. }
  232. //门窗等等
  233. //注意:酷家乐对应的holes字段和我们的不一样,对应我们的是门,窗等等
  234. public JSONArray getHoles() {
  235. if(input == null) {
  236. return null;
  237. }
  238. JSONArray result = new JSONArray();
  239. if(input.containsKey("window") && input.getJSONArray("window") != null && input.getJSONArray("window").size()>0) {
  240. result = getItemsModule(input.getJSONArray("window"),"window");
  241. }
  242. if(input.containsKey("door") && input.getJSONArray("door") != null && input.getJSONArray("door").size()>0) {
  243. JSONArray doors = getItemsModule(input.getJSONArray("door"),"door");
  244. result.addAll(doors);
  245. }
  246. if(input.containsKey("slideDoor") && input.getJSONArray("slideDoor") != null && input.getJSONArray("slideDoor").size()>0) {
  247. JSONArray slideDoors = getItemsModule(input.getJSONArray("slideDoor"),"slideDoor");
  248. result.addAll(slideDoors);
  249. }
  250. if(input.containsKey("groundCase") && input.getJSONArray("groundCase") != null && input.getJSONArray("groundCase").size()>0) {
  251. JSONArray groundCases = getItemsModule(input.getJSONArray("groundCase"),"groundCase");
  252. result.addAll(groundCases);
  253. }
  254. if(input.containsKey("bayCase") && input.getJSONArray("bayCase") != null && input.getJSONArray("bayCase").size()>0) {
  255. JSONArray bayCases = getItemsModule(input.getJSONArray("bayCase"),"bayCase");
  256. result.addAll(bayCases);
  257. }
  258. JSONArray points = input.getJSONArray("vertex-xy");
  259. for(int i=0;i<result.size();++i) {
  260. JSONObject item = result.getJSONObject(i);
  261. int wallId = item.getIntValue("groundClearance");
  262. double _bottom = item.getDouble("bottom");
  263. //double bottom = getbottomForWallId(wallId);
  264. //double groundClearance = _bottom - bottom;
  265. item.put("groundClearance", _bottom);
  266. item.put("wallId", wallId);
  267. item.remove("bottom");
  268. }
  269. return result;
  270. }
  271. public double getbottomForWallId(int wallId) {
  272. JSONArray blocks = input.getJSONArray("block");
  273. JSONArray vertex_z = input.getJSONArray("vertex-z");
  274. int bottomId = -1;
  275. for(int i=0;i<blocks.size();++i) {
  276. JSONObject block = blocks.getJSONObject(i);
  277. JSONArray walls = block.getJSONArray("wall");
  278. for(int j=0;j<walls.size();++j) {
  279. int wid = walls.getIntValue(j);
  280. if(wid == wallId) {
  281. bottomId = block.getIntValue("bottom");
  282. break;
  283. }
  284. }
  285. if(bottomId>0) {
  286. for(int k=0;k<vertex_z.size();++k) {
  287. JSONObject z = vertex_z.getJSONObject(k);
  288. if(z.getIntValue("id") == bottomId) {
  289. return z.getDouble("z");
  290. }
  291. }
  292. }
  293. }
  294. return -999999;
  295. }
  296. public JSONArray getColumnsModule(JSONArray columns) {
  297. JSONArray result = new JSONArray();
  298. for(int i=0;i<columns.size();++i) {
  299. JSONObject item = new JSONObject();
  300. JSONArray polygon = new JSONArray();
  301. JSONObject furnColumn = columns.getJSONObject(i);
  302. JSONArray points = furnColumn.getJSONArray("pos");
  303. double x1 = points.getDouble(0);
  304. double y1 = points.getDouble(1);
  305. double x2 = points.getDouble(2);
  306. double y2 = points.getDouble(3);
  307. //double startX = (x1+x2)/2;
  308. //double startY = (y1+y2)/2;
  309. double x3 = points.getDouble(4);
  310. double y3 = points.getDouble(5);
  311. double x4 = points.getDouble(6);
  312. double y4 = points.getDouble(7);
  313. //double endX = (x3+x4)/2;
  314. //double endY = (y3+y4)/2;
  315. /*
  316. JSONObject start = new JSONObject();
  317. start.put("x", startX );
  318. start.put("y", startY );
  319. item.put("start", start);
  320. JSONObject end = new JSONObject();
  321. end.put("x", endX );
  322. end.put("y", endY );
  323. item.put("end", end);
  324. item.put("height", furnColumn.getDouble("bottom") );
  325. double _thickness = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  326. item.put("thickness", _thickness);
  327. result.add(item);
  328. */
  329. JSONObject p1 = new JSONObject();
  330. p1.put("x", x1 );
  331. p1.put("y", y1 );
  332. polygon.add(p1);
  333. JSONObject p2 = new JSONObject();
  334. p2.put("x", x2 );
  335. p2.put("y", y2 );
  336. polygon.add(p2);
  337. JSONObject p3 = new JSONObject();
  338. p3.put("x", x3 );
  339. p3.put("y", y3 );
  340. polygon.add(p3);
  341. JSONObject p4 = new JSONObject();
  342. p4.put("x", x4 );
  343. p4.put("y", y4 );
  344. polygon.add(p4);
  345. item.put("polygon", polygon);
  346. item.put("bottom", furnColumn.getDouble("bottom") );
  347. item.put("top", furnColumn.getDouble("top") );
  348. result.add(item);
  349. }
  350. return result;
  351. }
  352. //获取朝向
  353. //ctl表示跟样式相关的坐标点
  354. //可以通过三角形顶点的顺时针和逆时针来判断朝向
  355. public String getOpenSide(JSONObject start,JSONObject end,JSONArray ctl ){
  356. double x = ctl.getDoubleValue(0);
  357. double y = ctl.getDoubleValue(1);
  358. double result=(x-start.getDouble("x"))*(end.getDouble("y")-y)-(y-start.getDouble("y"))*(end.getDouble("x")-x);
  359. double dis1 = Math.sqrt((x-start.getDouble("x"))*(x-start.getDouble("x"))+(y-start.getDouble("y"))*(y-start.getDouble("y")));
  360. double dis2 = Math.sqrt((x-end.getDouble("x"))*(x-end.getDouble("x"))+(y-end.getDouble("y"))*(y-end.getDouble("y")));
  361. if(result>0){
  362. if(ctl.size()>0){
  363. return "LEFT";
  364. }
  365. //单开门
  366. else{
  367. if(dis1>dis2){
  368. return "RIGHT_INVERSE";
  369. }
  370. else{
  371. return "LEFT";
  372. }
  373. }
  374. }
  375. else{
  376. if(ctl.size()>0){
  377. return "RIGHT";
  378. }
  379. //单开门
  380. else{
  381. if(dis1>dis2){
  382. return "LEFT_INVERSE";
  383. }
  384. else{
  385. return "RIGHT";
  386. }
  387. }
  388. }
  389. }
  390. public JSONArray getItemsModule(JSONArray holes,String type) {
  391. JSONArray result = new JSONArray();
  392. for(int i=0;i<holes.size();++i) {
  393. JSONObject item = new JSONObject();
  394. JSONObject hole = holes.getJSONObject(i);
  395. JSONArray points = hole.getJSONArray("pos");
  396. double x1 = points.getDouble(0);
  397. double y1 = points.getDouble(1);
  398. double x2 = points.getDouble(2);
  399. double y2 = points.getDouble(3);
  400. JSONObject start = new JSONObject();
  401. start.put("x", x1 );
  402. start.put("y", y1 );
  403. item.put("start", start);
  404. JSONObject end = new JSONObject();
  405. end.put("x", x2 );
  406. end.put("y", y2 );
  407. item.put("end", end);
  408. double bottom = hole.getDouble("bottom");
  409. double top = hole.getDouble("top");
  410. item.put("height", (top-bottom) );
  411. item.put("openSide", "LEFT");
  412. JSONArray ctl = hole.getJSONArray("ctl");
  413. if(ctl!=null){
  414. String side = getOpenSide( start, end, ctl);
  415. item.put("openSide", side); //我们自己的暂时不分朝向
  416. }
  417. int wallId = hole.getIntValue("line");
  418. if(wallMapping.containsKey(wallId)){
  419. wallId = wallMapping.getIntValue(String.valueOf(wallId));
  420. }
  421. //这个值实际上应该是离地面的高度,但是我们在另一个地方再换,这里只保存墙的id
  422. item.put("groundClearance", wallId);
  423. item.put("bottom", bottom);
  424. switch(type){
  425. case "window" :
  426. item.put("type", "WINDOW");
  427. break;
  428. case "door" :
  429. item.put("type", "DOOR");
  430. break;
  431. case "slideDoor" :
  432. item.put("type", "SLIDE_DOOR");
  433. break;
  434. case "groundCase" :
  435. item.put("type", "FRENCH_WINDOW");
  436. break;
  437. case "bayCase" :
  438. item.put("type", "BAY_WINDOW");
  439. break;
  440. default :
  441. break;
  442. }
  443. result.add(item);
  444. }
  445. return result;
  446. }
  447. public static Map<String, Double> getAverageValue(String visionPath) throws Exception{
  448. Map<String, Double> map = new HashMap<>();
  449. JSONObject visionJson = JSONObject.parseObject(FileUtils.readFile(visionPath));
  450. JSONArray sweepLocations = visionJson.getJSONArray("sweepLocations");
  451. double translationTotal = 0;
  452. double puckTotal = 0;
  453. for(int i = 0, len = sweepLocations.size(); i < len; i++){
  454. translationTotal += sweepLocations.getJSONObject(i).getJSONObject("pose").
  455. getJSONObject("translation").getDoubleValue("z");
  456. puckTotal += sweepLocations.getJSONObject(i).getJSONObject("puck").getDoubleValue("z");
  457. }
  458. map.put("translationAverage", translationTotal / sweepLocations.size());
  459. map.put("puckAverage", puckTotal / sweepLocations.size());
  460. return map;
  461. }
  462. public static Map<String, Double> getMaxOrMinValue(JSONObject visionJson) throws Exception{
  463. Map<String, Double> map = new HashMap<>();
  464. // JSONObject visionJson = JSONObject.parseObject(FileUtils.readFile(visionPath));
  465. JSONArray vertexZ = visionJson.getJSONArray("vertex-z");
  466. double[] vertexZArr = new double[vertexZ.size()];
  467. if(vertexZ != null){
  468. for(int i = 0, len = vertexZ.size(); i < len; i++){
  469. vertexZArr[i] = vertexZ.getJSONObject(i).getDoubleValue("z");
  470. }
  471. }
  472. map.put("translationAverage", getBubbleSortData(vertexZArr, 0));
  473. map.put("puckAverage", getBubbleSortData(vertexZArr, 1));
  474. // map.put("translationAverage", translationTotal / sweepLocations.size());
  475. // map.put("puckAverage", puckTotal / sweepLocations.size());
  476. return map;
  477. }
  478. /*
  479. public static JSONArray removeRepeat(JSONArray walls, AtomicBoolean removeAgain){
  480. JSONArray repeat = new JSONArray();
  481. JSONArray repeat2 = new JSONArray();
  482. JSONObject object1 = new JSONObject();
  483. JSONObject object2 = new JSONObject();
  484. removeAgain.compareAndSet(true, false);
  485. //完全重复不删除的id,只删除前面的,留着后面的
  486. Set<Long> ids = new HashSet<>();
  487. System.out.println("wallsSize:" + walls.size());
  488. for(int i = 0, len = walls.size(); i < len; i++){
  489. for (int j = 0, lem = walls.size(); j < lem; j++){
  490. if(i != j){
  491. object1 = walls.getJSONObject(i);
  492. object2 = walls.getJSONObject(j);
  493. if(ids.contains(object1.getLong("id"))){
  494. continue;
  495. }
  496. if(object1.getJSONObject("end").getDoubleValue("x") == object2.getJSONObject("end").getDoubleValue("x") &&
  497. object1.getJSONObject("end").getDoubleValue("y") == object2.getJSONObject("end").getDoubleValue("y") &&
  498. object1.getJSONObject("start").getDoubleValue("x") == object2.getJSONObject("start").getDoubleValue("x") &&
  499. object1.getJSONObject("start").getDoubleValue("y") == object2.getJSONObject("start").getDoubleValue("y") ){
  500. System.out.println("相同obj1:" + object1.toJSONString());
  501. System.out.println("相同obj2:" + object2.toJSONString());
  502. repeat.add(object1);
  503. ids.add(object2.getLong("id"));
  504. }
  505. if(object1.getJSONObject("end").getDoubleValue("x") == object2.getJSONObject("start").getDoubleValue("x") &&
  506. object1.getJSONObject("end").getDoubleValue("y") == object2.getJSONObject("start").getDoubleValue("y") &&
  507. object1.getJSONObject("start").getDoubleValue("x") == object2.getJSONObject("end").getDoubleValue("x") &&
  508. object1.getJSONObject("start").getDoubleValue("y") == object2.getJSONObject("end").getDoubleValue("y") ){
  509. System.out.println("相似obj1:" + object1.toJSONString());
  510. System.out.println("相似obj2:" + object2.toJSONString());
  511. if(!repeat2.contains(object1)){
  512. if(repeat.contains(object1)){
  513. removeAgain.set(true);
  514. }
  515. repeat.add(object1);
  516. repeat2.add(object2);
  517. }
  518. }
  519. // System.out.println("obj1:" + object1.toJSONString());
  520. // System.out.println("obj2:" + object2.toJSONString());
  521. }
  522. }
  523. }
  524. System.out.println("repeatSize: "+ repeat.size());
  525. System.out.println("repeat: "+ repeat.toJSONString());
  526. walls.removeAll(repeat);
  527. System.out.println("newWallsSize1: "+ walls.size());
  528. return walls;
  529. }
  530. */
  531. public JSONArray removeRepeat(JSONArray walls){
  532. JSONArray result = new JSONArray();
  533. for(int i=0;i<walls.size();++i){
  534. Boolean flag = true;
  535. JSONObject wall = walls.getJSONObject(i);
  536. for(int j=0;j<result.size();++j){
  537. JSONObject _wall = result.getJSONObject(j);
  538. if((_wall.getIntValue("a") == wall.getIntValue("a") && _wall.getIntValue("b") == wall.getIntValue("b")) ||
  539. (_wall.getIntValue("a") == wall.getIntValue("b")&&_wall.getIntValue("b") == wall.getIntValue("a"))){
  540. wallMapping.put(wall.getString("id"), _wall.getIntValue("id"));
  541. flag = false;
  542. break;
  543. }
  544. }
  545. if(flag){
  546. result.add(wall);
  547. }
  548. }
  549. // System.out.println(wallMapping.toJSONString());
  550. return result;
  551. }
  552. /**
  553. * 冒泡排序
  554. * @param arr
  555. * @param type 0是获取最小值,1是获取最大值
  556. * @return
  557. */
  558. public static double getBubbleSortData(double[] arr, int type){
  559. if(arr == null){
  560. return 0;
  561. }
  562. //冒泡排序
  563. for(int i = 0;i < arr.length-1; i++){
  564. for(int j = 0;j < arr.length-1-i; j++){
  565. if(arr[j] > arr[j+1]){
  566. double temp = arr[j];
  567. arr[j] = arr[j+1];
  568. arr[j+1] = temp;
  569. }
  570. }
  571. }
  572. if(type == 1){
  573. return arr[arr.length - 1];
  574. }
  575. return arr[0];
  576. }
  577. public static void main(String args[]) throws Exception {
  578. // Map<String, Double> map = getMaxOrMinValue("F:\\桌面\\c11m-T11-EA\\vision.txt");
  579. // System.out.println("bottom:" + map.get("translationAverage"));
  580. // System.out.println("top:" + map.get("puckAverage"));
  581. ConvertCadKjl cad = new ConvertCadKjl();
  582. String data = FileUtils.readFile("D:\\floor.json");
  583. JSONObject inputJson = null;
  584. JSONArray inputArray = null;
  585. Map<String, Double> map = null;
  586. if(StringUtil.isNotEmpty(data)){
  587. inputJson = JSON.parseObject(data);
  588. if(inputJson.containsKey("floors")){
  589. JSONObject resultJson = new JSONObject();
  590. JSONArray resultArray = new JSONArray();
  591. inputArray = inputJson.getJSONArray("floors");
  592. for(int i = 0, len = inputArray.size(); i < len; i++){
  593. cad = new ConvertCadKjl();
  594. cad.init(inputArray.getJSONObject(i));
  595. cad.input = cad.preHandle(cad.input);
  596. JSONArray beams = cad.getBeams();
  597. JSONArray walls = cad.getWalls();
  598. walls = cad.removeRepeat(walls);
  599. //判断是否需要重复出去墙
  600. // AtomicBoolean removeAgain = new AtomicBoolean(false);
  601. //去除重复的墙
  602. /*
  603. walls = removeRepeat(walls, removeAgain);
  604. while (removeAgain.get()){
  605. //再去除一遍重复的墙,防止有多次重复的墙剩下
  606. walls = removeRepeat(walls, removeAgain);
  607. }
  608. */
  609. // walls = removeRepeat(walls);
  610. JSONArray rooms = cad.getRooms();
  611. JSONArray flues = cad.getFlues();
  612. JSONArray holes = cad.getHoles();
  613. cad.output.put("holes", holes);
  614. cad.output.put("flues", flues);
  615. cad.output.put("beams", beams);
  616. cad.output.put("rooms", rooms);
  617. cad.output.put("walls", walls);
  618. // if(map != null){
  619. // JSONObject scene = new JSONObject();
  620. // scene.put("bottom", map.get("puckAverage"));
  621. // scene.put("top", map.get("puckAverage") == null? null : map.get("puckAverage") + 2.7);
  622. // cad.output.put("scene", scene);
  623. // }
  624. map = ConvertCadKjl.getMaxOrMinValue(inputArray.getJSONObject(i));
  625. if(map != null){
  626. JSONObject scene = new JSONObject();
  627. scene.put("bottom", map.get("translationAverage"));
  628. scene.put("top", map.get("puckAverage") == null? null : map.get("puckAverage"));
  629. cad.output.put("scene", scene);
  630. }
  631. // cad.output.put("version", floorPublishVer);
  632. resultArray.add(cad.output);
  633. }
  634. resultJson.put("floors", resultArray);
  635. writeBinary(resultJson.toString().getBytes(), "F:\\文档\\WeChat Files\\Iove-bing\\FileStorage\\File\\2021-01\\kjl.json");
  636. }else {
  637. cad.init(inputJson);
  638. cad.input = cad.preHandle(cad.input);
  639. JSONArray beams = cad.getBeams();
  640. JSONArray walls = cad.getWalls();
  641. walls = cad.removeRepeat(walls);
  642. //判断是否需要重复出去墙
  643. // AtomicBoolean removeAgain = new AtomicBoolean(false);
  644. //去除重复的墙
  645. /*
  646. walls = removeRepeat(walls, removeAgain);
  647. while (removeAgain.get()){
  648. //再去除一遍重复的墙,防止有多次重复的墙剩下
  649. walls = removeRepeat(walls, removeAgain);
  650. }
  651. */
  652. // walls = removeRepeat(walls);
  653. JSONArray rooms = cad.getRooms();
  654. JSONArray flues = cad.getFlues();
  655. JSONArray holes = cad.getHoles();
  656. cad.output.put("holes", holes);
  657. cad.output.put("flues", flues);
  658. cad.output.put("beams", beams);
  659. cad.output.put("rooms", rooms);
  660. cad.output.put("walls", walls);
  661. map = ConvertCadKjl.getMaxOrMinValue(inputJson);
  662. if(map != null){
  663. JSONObject scene = new JSONObject();
  664. scene.put("bottom", map.get("translationAverage"));
  665. scene.put("top", map.get("puckAverage") == null? null : map.get("puckAverage"));
  666. cad.output.put("scene", scene);
  667. }
  668. writeBinary(cad.output.toString().getBytes(), "F:\\文档\\WeChat Files\\Iove-bing\\FileStorage\\File\\2021-01\\kjl.json");
  669. }
  670. }
  671. // ConvertCadKjl cad = new ConvertCadKjl();
  672. // cad.init("G:\\javaProject\\baidu-master\\floor.json");
  673. // cad.input = cad.preHandle(cad.input);
  674. // JSONArray walls = cad.getWalls();
  675. // System.out.println(walls.toJSONString());
  676. // double[] arr = new double[]{-12,3,2,34,5,8,1};
  677. // System.out.println(getBubbleSortData(arr, 1));
  678. }
  679. }