Esempio n. 1
0
  // Loads polygons into the scene from the saveFile
  private static void openPolygons() {
    if (saveFile.exists()) {
      polygons.clear();

      try {
        Scanner fileIn = new Scanner(new FileReader(saveFile));

        do {
          if (!fileIn.hasNextLine()) {
            break;
          }

          String name = fileIn.nextLine();

          int size = Integer.valueOf(fileIn.nextLine());

          Polygon p = new Polygon(name);

          for (int i = 0; i < size; i++) {
            String line = fileIn.nextLine();
            String[] pointParts = line.split(",");
            float x = Float.valueOf(pointParts[0]);
            float y = Float.valueOf(pointParts[1]);
            float z = Float.valueOf(pointParts[2]);

            p.addPoint(new Point3Df(x, y, z));
          }

          size = Integer.valueOf(fileIn.nextLine());

          for (int i = 0; i < size; i++) {
            String line = fileIn.nextLine();
            String[] edgeParts = line.split(",");
            int p1 = Integer.valueOf(edgeParts[0]);
            int p2 = Integer.valueOf(edgeParts[1]);

            p.addEdge(new Edge(p1, p2));
          }

          polygons.add(p);
        } while (true);

        rebound();

        fileIn.close();
      } catch (FileNotFoundException ex) {
        // Can't find file
      }
    } else {
      // File doesn't exist
    }
  }
Esempio n. 2
0
  // Loads polygon from Ply file
  // @ref http://people.sc.fsu.edu/~jburkardt/data/ply/ply.html
  // @todo Check for Ply file type ascii/binary
  // @todo Check for datatypes as per spec
  private static void importPly(File ply) {
    if (ply.exists()) {
      try {
        Scanner fileIn = new Scanner(new FileReader(ply));

        int vertexSize = 0;
        int faceSize = 0;

        do {
          if (!fileIn.hasNextLine()) {
            break;
          } else {
            String line = fileIn.nextLine();

            String[] lineParts = line.split("\\s+");

            if (lineParts[0].equals("element")) {
              if (lineParts[1].equals("vertex")) {
                vertexSize = Integer.valueOf(lineParts[2]);
              } else if (lineParts[1].equals("face")) {
                faceSize = Integer.valueOf(lineParts[2]);
              }
            } else if (lineParts[0].equals("end_header")) {
              break;
            }
          }
        } while (true);

        Polygon p = new Polygon(ply.getName());

        for (int i = 0; i < vertexSize; i++) {
          String line = fileIn.nextLine();
          String[] pointParts = line.split("\\s+");
          float x = Float.valueOf(pointParts[0]);
          float y = Float.valueOf(pointParts[1]);
          float z = Float.valueOf(pointParts[2]);

          p.addPoint(new Point3Df(x, y, z));
        }

        for (int i = 0; i < faceSize; i++) {
          String line = fileIn.nextLine();
          String[] edgeParts = line.split("\\s+");

          int edgeComp = Integer.valueOf(edgeParts[0]);

          for (int j = 0; j < edgeComp - 1; j++) {
            int p1 = Integer.valueOf(edgeParts[j + 1]);
            int p2 = Integer.valueOf(edgeParts[j + 2]);

            p.addEdge(new Edge(p1, p2));
          }
        }

        polygons.add(p);

        rebound();

        fileIn.close();
      } catch (FileNotFoundException ex) {
        // Can't find file
      }
    } else {
      // File doesn't exist
    }
  }