package uk.org.floop.epq3d; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.awt.*; import java.io.FileReader; import java.io.IOException; import java.util.Objects; // Program for print data in JSON format. public class JsonReader { JSONObject file; Texture[] textures; ObjectCollection mainCollection; public JsonReader(String filePath) { JSONParser parser = new JSONParser(); Object obj; try { obj = parser.parse(new FileReader(filePath)); } catch (IOException | ParseException e) { throw new RuntimeException(e); } file = (JSONObject)obj; } public void getObjects(){ textures = getTextures((JSONArray) file.get("textures")); mainCollection = separateCollection((JSONObject) ((JSONArray)file.get("collections")).get(0), textures); file.get("collection"); } private ObjectCollection separateCollection(JSONObject json, Texture[] textures){ ObjectCollection result = new ObjectCollection(); // first, get the sub collections of the object and separate them recursively. JSONArray subCollections = (JSONArray) json.get("subCollections"); if(subCollections != null) for (Object collection: subCollections) { result.addCollection(separateCollection((JSONObject) collection, textures)); } // next, get the name of the collection result.name = json.get("name").toString(); // next, get the objects contained in this collection JSONArray objects3d = (JSONArray) json.get("objects"); if(objects3d != null) for(Object objectJson: objects3d){ JSONObject object = (JSONObject)objectJson; JSONArray pointsJson = (JSONArray) object.get("points"); // pack points PointComp[] points = new PointComp[pointsJson.size()]; for (int i = 0; i < pointsJson.size(); i+=1){ JSONArray jsonPoint = (JSONArray) pointsJson.get(i); points[i] = new PointComp( (double) jsonPoint.get(0), (double) jsonPoint.get(1), (double) jsonPoint.get(2) ); } // make faceList, UV list and texture lists // (the indices should match up, if they don't something went wrong) JSONArray faceListJson = (JSONArray) object.get("faceList"); JSONArray uvPointsListJson = (JSONArray) object.get("uvPointsList"); JSONArray texturesNamesJson = (JSONArray) object.get("textureList"); int[][] faceList = new int[faceListJson.size()][]; Point2D[][] uvPointsList = new Point2D[faceListJson.size()][]; Texture[] texturesList = new Texture[faceListJson.size()]; for (int i = 0; i < faceListJson.size(); i+=1) { JSONArray jsonFace = (JSONArray) faceListJson.get(i); JSONArray jsonUV = (JSONArray) uvPointsListJson.get(i); String jsonTextureName = (String) texturesNamesJson.get(i); faceList[i] = new int[jsonFace.size()]; uvPointsList[i] = new Point2D[jsonFace.size()]; for (int j = 0; j < jsonFace.size(); j += 1){ faceList[i][j] = Integer.parseInt(String.valueOf(jsonFace.get(j))); uvPointsList[i][j] = new Point2D( 0,0 ); // Integer.parseInt(String.valueOf(((JSONArray)jsonUV.get(j)).get(0))), // Integer.parseInt(String.valueOf(((JSONArray)jsonUV.get(j)).get(1))) // ); } // terrible linear search but idc because this code only runs once for (Texture texture : textures) { if (Objects.equals(texture.name, jsonTextureName)) { texturesList[i] = texture; break; } } } result.addObject(new Object3d(points, faceList, uvPointsList, false, texturesList, true)); } return result; } public Texture[] getTextures(JSONArray textures){ Texture[] result = new Texture[textures.size()]; for(int i = 0; i < textures.size(); i += 1){ JSONObject JsonTexture = (JSONObject) textures.get(i); if(Objects.equals(JsonTexture.get("type"), "solid")){ result[i] = new Texture(JsonTexture.get("name").toString(), Color.decode('#' + (String) JsonTexture.get("color"))); } } return result; } }