ReadFloorplan.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. package com.fdkk.sxz.util;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import lombok.extern.slf4j.Slf4j;
  6. import java.io.*;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. @Slf4j
  10. public class ReadFloorplan {
  11. private static String filePath = "F:\\2021\\����װ\\house.json";
  12. private static String version = "2.0";
  13. private static String outputPath = "F:\\2021\\����װ\\cad\\house2.json";
  14. public static JSONObject inputFile() {
  15. return null;
  16. }
  17. public static void main(String args[]) {
  18. try {
  19. //ReadFloorplan m_readFloorplan = new ReadFloorplan();
  20. //JSONObject cad = m_readFloorplan.readFile(m_readFloorplan.filePath);
  21. //JSONArray floors = cad.getJSONArray("floors");
  22. //floors = m_readFloorplan.analysisCad(floors);
  23. //JSONObject data = new JSONObject();
  24. //data.put("version", ReadFloorplan.version);
  25. //ReadFloorplan.writeFile(ReadFloorplan.outputPath, data.toString());
  26. //System.out.println();
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. public void analysisCadJsonObj(JSONArray floors, JSONObject resultJson) {
  32. try {
  33. JSONArray jsonArray = analysisCad(floors);
  34. resultJson.put("floors", jsonArray);
  35. resultJson.put("version", ReadFloorplan.version);
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. public JSONArray analysisCad(JSONArray floors) {
  41. JSONArray floorplan = new JSONArray();
  42. for (int i = 0; i < floors.size(); ++i) {
  43. JSONObject item = new JSONObject();
  44. JSONObject floor = floors.getJSONObject(i);
  45. JSONArray doors = floor.getJSONArray("door");
  46. JSONArray slideDoors = floor.getJSONArray("slideDoor");
  47. JSONArray doubleDoors = floor.getJSONArray("doubleDoor");
  48. JSONArray windows = floor.getJSONArray("window");
  49. JSONArray bayCases = floor.getJSONArray("bayCase");
  50. JSONArray frenchWindows = floor.getJSONArray("groundCase");
  51. doors = createSymbols(doors, "door");
  52. slideDoors = createSymbols(slideDoors, "slideDoor");
  53. doubleDoors = createSymbols(doubleDoors, "doubleDoor");
  54. windows = createSymbols(windows, "window");
  55. bayCases = createSymbols(bayCases, "bayCase");
  56. frenchWindows = createSymbols(frenchWindows, "groundCase");
  57. JSONArray beams = floor.getJSONArray("furnColumn");
  58. beams = createBeams(beams);
  59. JSONArray flues = floor.getJSONArray("furnFlue");
  60. flues = createFlues(flues);
  61. JSONArray vertexs = floor.getJSONArray("vertex");
  62. vertexs = createPoints(vertexs);
  63. JSONArray walls = floor.getJSONArray("wall");
  64. walls = createWalls(walls);
  65. vertexs = updatePointsParent(vertexs, walls);
  66. JSONArray symbols = new JSONArray();
  67. for (int j = 0; j < doors.size(); ++j) {
  68. JSONObject door = doors.getJSONObject(j);
  69. symbols.add(door);
  70. }
  71. for (int j = 0; j < slideDoors.size(); ++j) {
  72. JSONObject slideDoor = slideDoors.getJSONObject(j);
  73. symbols.add(slideDoor);
  74. }
  75. for (int j = 0; j < doubleDoors.size(); ++j) {
  76. JSONObject doubleDoor = doubleDoors.getJSONObject(j);
  77. symbols.add(doubleDoor);
  78. }
  79. for (int j = 0; j < windows.size(); ++j) {
  80. JSONObject window = windows.getJSONObject(j);
  81. symbols.add(window);
  82. }
  83. for (int j = 0; j < bayCases.size(); ++j) {
  84. JSONObject bayCase = bayCases.getJSONObject(j);
  85. symbols.add(bayCase);
  86. }
  87. for (int j = 0; j < frenchWindows.size(); ++j) {
  88. JSONObject frenchWindow = frenchWindows.getJSONObject(j);
  89. symbols.add(frenchWindow);
  90. }
  91. item.put("points", vertexs);
  92. item.put("walls", walls);
  93. item.put("symbols", symbols);
  94. item.put("flues", flues);
  95. item.put("beams", beams);
  96. floorplan.add(item);
  97. }
  98. JSONObject V2data = new JSONObject();
  99. V2data.put("floors", floors);
  100. V2data.put("version", ReadFloorplan.version);
  101. return floorplan;
  102. }
  103. private JSONArray createPoints(JSONArray vertexs) {
  104. if (vertexs == null) {
  105. return null;
  106. }
  107. JSONArray result = new JSONArray();
  108. for (int i = 0; i < vertexs.size(); ++i) {
  109. JSONObject vertex = vertexs.getJSONObject(i);
  110. JSONObject item = new JSONObject();
  111. item.put("x", vertex.getDouble("x"));
  112. item.put("y", vertex.getDouble("y"));
  113. item.put("vectorId", "Point" + vertex.getString("id"));
  114. result.add(item);
  115. }
  116. return result;
  117. }
  118. private JSONArray createWalls(JSONArray walls) {
  119. if (walls == null) {
  120. return null;
  121. }
  122. JSONArray result = new JSONArray();
  123. for (int i = 0; i < walls.size(); ++i) {
  124. JSONObject wall = walls.getJSONObject(i);
  125. JSONObject item = new JSONObject();
  126. item.put("start", "Point" + wall.getInteger("p1"));
  127. item.put("end", "Point" + wall.getInteger("p2"));
  128. item.put("vectorId", "Wall" + wall.getString("id"));
  129. if (wall.getBoolean("border")) {
  130. item.put("border", 1);
  131. } else {
  132. item.put("border", 0);
  133. }
  134. result.add(item);
  135. }
  136. return result;
  137. }
  138. private JSONArray updatePointsParent(JSONArray points, JSONArray walls) {
  139. JSONObject jsonPoints = new JSONObject();
  140. for (int i = 0; i < points.size(); ++i) {
  141. JSONObject point = points.getJSONObject(i);
  142. jsonPoints.put(point.getString("vectorId"), point);
  143. }
  144. for (int i = 0; i < walls.size(); ++i) {
  145. JSONObject wall = walls.getJSONObject(i);
  146. String startPointId = wall.getString("start");
  147. String endPointId = wall.getString("end");
  148. String wallId = wall.getString("vectorId");
  149. JSONObject start = jsonPoints.getJSONObject(startPointId);
  150. JSONObject end = jsonPoints.getJSONObject(endPointId);
  151. //ReadFloorplan.log.info("watch-wallId-={},startPointId={},endPointId={}", wallId, startPointId, endPointId);
  152. JSONObject startParent = start.getJSONObject("parent");
  153. if (ObjectUtil.isNotNull(startParent) && !startParent.isEmpty()) {
  154. startParent.put(wallId, "start");
  155. } else {
  156. //ReadFloorplan.log.info("watch-startParent.isNull,json={}", start.toJSONString());
  157. startParent = new JSONObject();
  158. startParent.put(wallId, "start");
  159. }
  160. start.put("parent", startParent);
  161. JSONObject endParent = end.getJSONObject("parent");
  162. if (ObjectUtil.isNotNull(endParent) && !endParent.isEmpty()) {
  163. endParent.put(wallId, "end");
  164. } else {
  165. //ReadFloorplan.log.info("watch-endParent.isNull,json={}", end.toJSONString());
  166. endParent = new JSONObject();
  167. endParent.put(wallId, "end");
  168. }
  169. end.put("parent", endParent);
  170. }
  171. JSONArray result = new JSONArray();
  172. Iterator iter = jsonPoints.entrySet().iterator();
  173. while (iter.hasNext()) {
  174. Map.Entry entry = (Map.Entry) iter.next();
  175. result.add(entry.getValue());
  176. }
  177. return result;
  178. }
  179. private JSONArray createSymbols(JSONArray symbols, String geoType) {
  180. if (symbols == null) {
  181. return null;
  182. }
  183. JSONArray result = new JSONArray();
  184. for (int i = 0; i < symbols.size(); ++i) {
  185. JSONObject item = new JSONObject();
  186. JSONObject symbol = symbols.getJSONObject(i);
  187. int parent = symbol.getInteger("line");
  188. JSONArray pos = symbol.getJSONArray("pos");
  189. JSONObject start = new JSONObject();
  190. start.put("x", pos.getDouble(0));
  191. start.put("y", pos.getDouble(1));
  192. JSONObject end = new JSONObject();
  193. end.put("x", pos.getDouble(2));
  194. end.put("y", pos.getDouble(3));
  195. JSONArray points = new JSONArray();
  196. if (geoType.equals("door")) {
  197. JSONArray ctl = symbol.getJSONArray("ctl");
  198. JSONObject dirPoint = new JSONObject();
  199. dirPoint.put("x", ctl.getDouble(0));
  200. dirPoint.put("y", ctl.getDouble(1));
  201. double dis1 = distance(start, dirPoint);
  202. double dis2 = distance(end, dirPoint);
  203. if (dis1 > dis2) {
  204. JSONObject temp = start;
  205. start = end;
  206. end = temp;
  207. }
  208. points.add(start);
  209. points.add(end);
  210. points.add(dirPoint);
  211. boolean flag = this.isClockwise(points);
  212. if (flag) {
  213. //left
  214. item.put("openSide", "LEFT");
  215. } else {
  216. //right
  217. item.put("openSide", "RIGHT");
  218. }
  219. item.put("geoType", "SingleDoor");
  220. } else if (geoType.equals("doubleDoor")) {
  221. item.put("geoType", "DoubleDoor");
  222. } else if (geoType.equals("slideDoor")) {
  223. item.put("geoType", "SlideDoor");
  224. } else if (geoType.equals("window")) {
  225. item.put("geoType", "SingleWindow");
  226. } else if (geoType.equals("groundCase")) {
  227. item.put("geoType", "FrenchWindow");
  228. } else if (geoType.equals("bayCase")) {
  229. JSONArray ctl = symbol.getJSONArray("ctl");
  230. points.add(start);
  231. points.add(end);
  232. JSONObject point1 = new JSONObject();
  233. point1.put("x", ctl.getDouble(0));
  234. point1.put("y", ctl.getDouble(1));
  235. JSONObject point2 = new JSONObject();
  236. point2.put("x", ctl.getDouble(2));
  237. point2.put("y", ctl.getDouble(3));
  238. points.add(point1);
  239. points.add(point2);
  240. boolean flag = this.isClockwise(points);
  241. if (flag) {
  242. //left
  243. item.put("openSide", "LEFT");
  244. } else {
  245. //right
  246. item.put("openSide", "RIGHT");
  247. }
  248. item.put("geoType", "BayWindow");
  249. }
  250. item.put("start", start);
  251. item.put("end", end);
  252. item.put("parent", "Wall" + parent);
  253. result.add(item);
  254. }
  255. return result;
  256. }
  257. private JSONArray createBeams(JSONArray beams) {
  258. if (beams == null) {
  259. return null;
  260. }
  261. JSONArray result = new JSONArray();
  262. for (int i = 0; i < beams.size(); ++i) {
  263. JSONObject item = new JSONObject();
  264. JSONObject beam = beams.getJSONObject(i);
  265. JSONArray pos = beam.getJSONArray("pos");
  266. JSONObject point1 = new JSONObject();
  267. point1.put("x", pos.getDouble(0));
  268. point1.put("y", pos.getDouble(1));
  269. JSONObject point2 = new JSONObject();
  270. point2.put("x", pos.getDouble(2));
  271. point2.put("y", pos.getDouble(3));
  272. JSONObject point3 = new JSONObject();
  273. point3.put("x", pos.getDouble(4));
  274. point3.put("y", pos.getDouble(5));
  275. JSONObject point4 = new JSONObject();
  276. point4.put("x", pos.getDouble(6));
  277. point4.put("y", pos.getDouble(7));
  278. JSONArray points = new JSONArray();
  279. points.add(point1);
  280. points.add(point2);
  281. points.add(point3);
  282. points.add(point4);
  283. int angle = beam.getInteger("angle");
  284. angle += 90;
  285. if (angle > 360) {
  286. angle -= 360;
  287. }
  288. item.put("points", points);
  289. item.put("angle", angle);
  290. result.add(item);
  291. }
  292. return result;
  293. }
  294. private JSONArray createFlues(JSONArray flues) {
  295. if (flues == null) {
  296. return null;
  297. }
  298. JSONArray result = new JSONArray();
  299. for (int i = 0; i < flues.size(); ++i) {
  300. JSONObject item = new JSONObject();
  301. JSONObject flue = flues.getJSONObject(i);
  302. JSONArray pos = flue.getJSONArray("pos");
  303. JSONObject point1 = new JSONObject();
  304. point1.put("x", pos.getDouble(0));
  305. point1.put("y", pos.getDouble(1));
  306. JSONObject point2 = new JSONObject();
  307. point2.put("x", pos.getDouble(2));
  308. point2.put("y", pos.getDouble(3));
  309. JSONObject point3 = new JSONObject();
  310. point3.put("x", pos.getDouble(4));
  311. point3.put("y", pos.getDouble(5));
  312. JSONObject point4 = new JSONObject();
  313. point4.put("x", pos.getDouble(6));
  314. point4.put("y", pos.getDouble(7));
  315. JSONArray points = new JSONArray();
  316. points.add(point1);
  317. points.add(point2);
  318. points.add(point3);
  319. points.add(point4);
  320. int angle = flue.getInteger("angle");
  321. angle += 90;
  322. if (angle > 360) {
  323. angle -= 360;
  324. }
  325. item.put("points", points);
  326. item.put("angle", angle);
  327. result.add(item);
  328. }
  329. return result;
  330. }
  331. public JSONObject readFile(String floorPath) {
  332. String floorplan = readFileByLines(floorPath);
  333. if (floorplan == null) {
  334. return null;
  335. } else {
  336. JSONObject input = JSONObject.parseObject(floorplan);
  337. return input;
  338. }
  339. }
  340. public String readFileByLines(String fileName) {
  341. FileInputStream file = null;
  342. BufferedReader reader = null;
  343. InputStreamReader inputFileReader = null;
  344. String content = "";
  345. String tempString = null;
  346. File f = new File(fileName);
  347. if (!f.exists()) {
  348. return null;
  349. }
  350. try {
  351. file = new FileInputStream(fileName);
  352. inputFileReader = new InputStreamReader(file, "utf-8");
  353. reader = new BufferedReader(inputFileReader);
  354. // һ�ζ���һ�У�ֱ������nullΪ�ļ�����
  355. while ((tempString = reader.readLine()) != null) {
  356. content += tempString;
  357. }
  358. reader.close();
  359. } catch (IOException e) {
  360. e.printStackTrace();
  361. return null;
  362. } finally {
  363. if (reader != null) {
  364. try {
  365. reader.close();
  366. } catch (IOException e1) {
  367. }
  368. }
  369. }
  370. return content;
  371. }
  372. public static void writeFile(String filePath, String str) throws IOException {
  373. File fout = new File(filePath);
  374. FileOutputStream fos = new FileOutputStream(fout);
  375. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
  376. bw.write(str);
  377. bw.close();
  378. }
  379. private boolean isClockwise(JSONArray vertices) {
  380. double area = 0;
  381. for (int i = 0; i < vertices.size(); i++) {
  382. int j = (i + 1) % vertices.size();
  383. area += vertices.getJSONObject(i).getDouble("x") * vertices.getJSONObject(j).getDouble("y");
  384. area -= vertices.getJSONObject(j).getDouble("x") * vertices.getJSONObject(i).getDouble("y");
  385. }
  386. double sub = area / 2;
  387. if (sub > 0) {
  388. //��ʱ��
  389. return true;
  390. } else {
  391. //˳ʱ��
  392. return false;
  393. }
  394. }
  395. private double distance(JSONObject point1, JSONObject point2) {
  396. double dis = Math.pow(point1.getDouble("x") - point2.getDouble("x"), 2) + Math.pow(point1.getDouble("y") - point2.getDouble("y"), 2);
  397. return Math.sqrt(dis);
  398. }
  399. }