123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- package com.fdkk.sxz.util;
- import cn.hutool.core.util.ObjectUtil;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import java.io.*;
- import java.util.Iterator;
- import java.util.Map;
- @Slf4j
- public class ReadFloorplan {
- private static String filePath = "F:\\2021\\����װ\\house.json";
- private static String version = "2.0";
- private static String outputPath = "F:\\2021\\����װ\\cad\\house2.json";
- public static JSONObject inputFile() {
- return null;
- }
- public static void main(String args[]) {
- try {
- //ReadFloorplan m_readFloorplan = new ReadFloorplan();
- //JSONObject cad = m_readFloorplan.readFile(m_readFloorplan.filePath);
- //JSONArray floors = cad.getJSONArray("floors");
- //floors = m_readFloorplan.analysisCad(floors);
- //JSONObject data = new JSONObject();
- //data.put("version", ReadFloorplan.version);
- //ReadFloorplan.writeFile(ReadFloorplan.outputPath, data.toString());
- //System.out.println();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void analysisCadJsonObj(JSONArray floors, JSONObject resultJson) {
- try {
- JSONArray jsonArray = analysisCad(floors);
- resultJson.put("floors", jsonArray);
- resultJson.put("version", ReadFloorplan.version);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public JSONArray analysisCad(JSONArray floors) {
- JSONArray floorplan = new JSONArray();
- for (int i = 0; i < floors.size(); ++i) {
- JSONObject item = new JSONObject();
- JSONObject floor = floors.getJSONObject(i);
- JSONArray doors = floor.getJSONArray("door");
- JSONArray slideDoors = floor.getJSONArray("slideDoor");
- JSONArray doubleDoors = floor.getJSONArray("doubleDoor");
- JSONArray windows = floor.getJSONArray("window");
- JSONArray bayCases = floor.getJSONArray("bayCase");
- JSONArray frenchWindows = floor.getJSONArray("groundCase");
- doors = createSymbols(doors, "door");
- slideDoors = createSymbols(slideDoors, "slideDoor");
- doubleDoors = createSymbols(doubleDoors, "doubleDoor");
- windows = createSymbols(windows, "window");
- bayCases = createSymbols(bayCases, "bayCase");
- frenchWindows = createSymbols(frenchWindows, "groundCase");
- JSONArray beams = floor.getJSONArray("furnColumn");
- beams = createBeams(beams);
- JSONArray flues = floor.getJSONArray("furnFlue");
- flues = createFlues(flues);
- JSONArray vertexs = floor.getJSONArray("vertex");
- vertexs = createPoints(vertexs);
- JSONArray walls = floor.getJSONArray("wall");
- walls = createWalls(walls);
- vertexs = updatePointsParent(vertexs, walls);
- JSONArray symbols = new JSONArray();
- for (int j = 0; j < doors.size(); ++j) {
- JSONObject door = doors.getJSONObject(j);
- symbols.add(door);
- }
- for (int j = 0; j < slideDoors.size(); ++j) {
- JSONObject slideDoor = slideDoors.getJSONObject(j);
- symbols.add(slideDoor);
- }
- for (int j = 0; j < doubleDoors.size(); ++j) {
- JSONObject doubleDoor = doubleDoors.getJSONObject(j);
- symbols.add(doubleDoor);
- }
- for (int j = 0; j < windows.size(); ++j) {
- JSONObject window = windows.getJSONObject(j);
- symbols.add(window);
- }
- for (int j = 0; j < bayCases.size(); ++j) {
- JSONObject bayCase = bayCases.getJSONObject(j);
- symbols.add(bayCase);
- }
- for (int j = 0; j < frenchWindows.size(); ++j) {
- JSONObject frenchWindow = frenchWindows.getJSONObject(j);
- symbols.add(frenchWindow);
- }
- item.put("points", vertexs);
- item.put("walls", walls);
- item.put("symbols", symbols);
- item.put("flues", flues);
- item.put("beams", beams);
- floorplan.add(item);
- }
- JSONObject V2data = new JSONObject();
- V2data.put("floors", floors);
- V2data.put("version", ReadFloorplan.version);
- return floorplan;
- }
- private JSONArray createPoints(JSONArray vertexs) {
- if (vertexs == null) {
- return null;
- }
- JSONArray result = new JSONArray();
- for (int i = 0; i < vertexs.size(); ++i) {
- JSONObject vertex = vertexs.getJSONObject(i);
- JSONObject item = new JSONObject();
- item.put("x", vertex.getDouble("x"));
- item.put("y", vertex.getDouble("y"));
- item.put("vectorId", "Point" + vertex.getString("id"));
- result.add(item);
- }
- return result;
- }
- private JSONArray createWalls(JSONArray walls) {
- if (walls == null) {
- return null;
- }
- JSONArray result = new JSONArray();
- for (int i = 0; i < walls.size(); ++i) {
- JSONObject wall = walls.getJSONObject(i);
- JSONObject item = new JSONObject();
- item.put("start", "Point" + wall.getInteger("p1"));
- item.put("end", "Point" + wall.getInteger("p2"));
- item.put("vectorId", "Wall" + wall.getString("id"));
- if (wall.getBoolean("border")) {
- item.put("border", 1);
- } else {
- item.put("border", 0);
- }
- result.add(item);
- }
- return result;
- }
- private JSONArray updatePointsParent(JSONArray points, JSONArray walls) {
- JSONObject jsonPoints = new JSONObject();
- for (int i = 0; i < points.size(); ++i) {
- JSONObject point = points.getJSONObject(i);
- jsonPoints.put(point.getString("vectorId"), point);
- }
- for (int i = 0; i < walls.size(); ++i) {
- JSONObject wall = walls.getJSONObject(i);
- String startPointId = wall.getString("start");
- String endPointId = wall.getString("end");
- String wallId = wall.getString("vectorId");
- JSONObject start = jsonPoints.getJSONObject(startPointId);
- JSONObject end = jsonPoints.getJSONObject(endPointId);
- //ReadFloorplan.log.info("watch-wallId-={},startPointId={},endPointId={}", wallId, startPointId, endPointId);
- JSONObject startParent = start.getJSONObject("parent");
- if (ObjectUtil.isNotNull(startParent) && !startParent.isEmpty()) {
- startParent.put(wallId, "start");
- } else {
- //ReadFloorplan.log.info("watch-startParent.isNull,json={}", start.toJSONString());
- startParent = new JSONObject();
- startParent.put(wallId, "start");
- }
- start.put("parent", startParent);
- JSONObject endParent = end.getJSONObject("parent");
- if (ObjectUtil.isNotNull(endParent) && !endParent.isEmpty()) {
- endParent.put(wallId, "end");
- } else {
- //ReadFloorplan.log.info("watch-endParent.isNull,json={}", end.toJSONString());
- endParent = new JSONObject();
- endParent.put(wallId, "end");
- }
- end.put("parent", endParent);
- }
- JSONArray result = new JSONArray();
- Iterator iter = jsonPoints.entrySet().iterator();
- while (iter.hasNext()) {
- Map.Entry entry = (Map.Entry) iter.next();
- result.add(entry.getValue());
- }
- return result;
- }
- private JSONArray createSymbols(JSONArray symbols, String geoType) {
- if (symbols == null) {
- return null;
- }
- JSONArray result = new JSONArray();
- for (int i = 0; i < symbols.size(); ++i) {
- JSONObject item = new JSONObject();
- JSONObject symbol = symbols.getJSONObject(i);
- int parent = symbol.getInteger("line");
- JSONArray pos = symbol.getJSONArray("pos");
- JSONObject start = new JSONObject();
- start.put("x", pos.getDouble(0));
- start.put("y", pos.getDouble(1));
- JSONObject end = new JSONObject();
- end.put("x", pos.getDouble(2));
- end.put("y", pos.getDouble(3));
- JSONArray points = new JSONArray();
- if (geoType.equals("door")) {
- JSONArray ctl = symbol.getJSONArray("ctl");
- JSONObject dirPoint = new JSONObject();
- dirPoint.put("x", ctl.getDouble(0));
- dirPoint.put("y", ctl.getDouble(1));
- double dis1 = distance(start, dirPoint);
- double dis2 = distance(end, dirPoint);
- if (dis1 > dis2) {
- JSONObject temp = start;
- start = end;
- end = temp;
- }
- points.add(start);
- points.add(end);
- points.add(dirPoint);
- boolean flag = this.isClockwise(points);
- if (flag) {
- //left
- item.put("openSide", "LEFT");
- } else {
- //right
- item.put("openSide", "RIGHT");
- }
- item.put("geoType", "SingleDoor");
- } else if (geoType.equals("doubleDoor")) {
- item.put("geoType", "DoubleDoor");
- } else if (geoType.equals("slideDoor")) {
- item.put("geoType", "SlideDoor");
- } else if (geoType.equals("window")) {
- item.put("geoType", "SingleWindow");
- } else if (geoType.equals("groundCase")) {
- item.put("geoType", "FrenchWindow");
- } else if (geoType.equals("bayCase")) {
- JSONArray ctl = symbol.getJSONArray("ctl");
- points.add(start);
- points.add(end);
- JSONObject point1 = new JSONObject();
- point1.put("x", ctl.getDouble(0));
- point1.put("y", ctl.getDouble(1));
- JSONObject point2 = new JSONObject();
- point2.put("x", ctl.getDouble(2));
- point2.put("y", ctl.getDouble(3));
- points.add(point1);
- points.add(point2);
- boolean flag = this.isClockwise(points);
- if (flag) {
- //left
- item.put("openSide", "LEFT");
- } else {
- //right
- item.put("openSide", "RIGHT");
- }
- item.put("geoType", "BayWindow");
- }
- item.put("start", start);
- item.put("end", end);
- item.put("parent", "Wall" + parent);
- result.add(item);
- }
- return result;
- }
- private JSONArray createBeams(JSONArray beams) {
- if (beams == null) {
- return null;
- }
- JSONArray result = new JSONArray();
- for (int i = 0; i < beams.size(); ++i) {
- JSONObject item = new JSONObject();
- JSONObject beam = beams.getJSONObject(i);
- JSONArray pos = beam.getJSONArray("pos");
- JSONObject point1 = new JSONObject();
- point1.put("x", pos.getDouble(0));
- point1.put("y", pos.getDouble(1));
- JSONObject point2 = new JSONObject();
- point2.put("x", pos.getDouble(2));
- point2.put("y", pos.getDouble(3));
- JSONObject point3 = new JSONObject();
- point3.put("x", pos.getDouble(4));
- point3.put("y", pos.getDouble(5));
- JSONObject point4 = new JSONObject();
- point4.put("x", pos.getDouble(6));
- point4.put("y", pos.getDouble(7));
- JSONArray points = new JSONArray();
- points.add(point1);
- points.add(point2);
- points.add(point3);
- points.add(point4);
- int angle = beam.getInteger("angle");
- angle += 90;
- if (angle > 360) {
- angle -= 360;
- }
- item.put("points", points);
- item.put("angle", angle);
- result.add(item);
- }
- return result;
- }
- private JSONArray createFlues(JSONArray flues) {
- if (flues == null) {
- return null;
- }
- JSONArray result = new JSONArray();
- for (int i = 0; i < flues.size(); ++i) {
- JSONObject item = new JSONObject();
- JSONObject flue = flues.getJSONObject(i);
- JSONArray pos = flue.getJSONArray("pos");
- JSONObject point1 = new JSONObject();
- point1.put("x", pos.getDouble(0));
- point1.put("y", pos.getDouble(1));
- JSONObject point2 = new JSONObject();
- point2.put("x", pos.getDouble(2));
- point2.put("y", pos.getDouble(3));
- JSONObject point3 = new JSONObject();
- point3.put("x", pos.getDouble(4));
- point3.put("y", pos.getDouble(5));
- JSONObject point4 = new JSONObject();
- point4.put("x", pos.getDouble(6));
- point4.put("y", pos.getDouble(7));
- JSONArray points = new JSONArray();
- points.add(point1);
- points.add(point2);
- points.add(point3);
- points.add(point4);
- int angle = flue.getInteger("angle");
- angle += 90;
- if (angle > 360) {
- angle -= 360;
- }
- item.put("points", points);
- item.put("angle", angle);
- result.add(item);
- }
- return result;
- }
- public JSONObject readFile(String floorPath) {
- String floorplan = readFileByLines(floorPath);
- if (floorplan == null) {
- return null;
- } else {
- JSONObject input = JSONObject.parseObject(floorplan);
- return input;
- }
- }
- public String readFileByLines(String fileName) {
- FileInputStream file = null;
- BufferedReader reader = null;
- InputStreamReader inputFileReader = null;
- String content = "";
- String tempString = null;
- File f = new File(fileName);
- if (!f.exists()) {
- return null;
- }
- try {
- file = new FileInputStream(fileName);
- inputFileReader = new InputStreamReader(file, "utf-8");
- reader = new BufferedReader(inputFileReader);
- // һ�ζ���һ�У�ֱ������nullΪ�ļ�����
- while ((tempString = reader.readLine()) != null) {
- content += tempString;
- }
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e1) {
- }
- }
- }
- return content;
- }
- public static void writeFile(String filePath, String str) throws IOException {
- File fout = new File(filePath);
- FileOutputStream fos = new FileOutputStream(fout);
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
- bw.write(str);
- bw.close();
- }
- private boolean isClockwise(JSONArray vertices) {
- double area = 0;
- for (int i = 0; i < vertices.size(); i++) {
- int j = (i + 1) % vertices.size();
- area += vertices.getJSONObject(i).getDouble("x") * vertices.getJSONObject(j).getDouble("y");
- area -= vertices.getJSONObject(j).getDouble("x") * vertices.getJSONObject(i).getDouble("y");
- }
- double sub = area / 2;
- if (sub > 0) {
- //��ʱ��
- return true;
- } else {
- //˳ʱ��
- return false;
- }
- }
- private double distance(JSONObject point1, JSONObject point2) {
- double dis = Math.pow(point1.getDouble("x") - point2.getDouble("x"), 2) + Math.pow(point1.getDouble("y") - point2.getDouble("y"), 2);
- return Math.sqrt(dis);
- }
- }
|