Example #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
    }
  }
Example #2
0
  // Writes polygons to the saveFile
  private static void savePolygons() {
    if (!saveFile.exists()) {
      try {
        saveFile.createNewFile();
      } catch (IOException ex) {
        // Can't create file
      }
    }

    try {
      PrintStream fileOut = new PrintStream(saveFile);
      for (Polygon p : polygons) {
        fileOut.println(p.getName());
        fileOut.println(p.getPoints().size());
        for (Point3Df q : p.getPoints()) {
          fileOut.print(q.x);
          fileOut.print(",");
          fileOut.print(q.y);
          fileOut.print(",");
          fileOut.println(q.z);
        }

        fileOut.println(p.getEdges().size());
        for (Edge q : p.getEdges()) {
          fileOut.print(q.p1);
          fileOut.print(",");
          fileOut.println(q.p2);
        }
      }
      fileOut.close();
    } catch (FileNotFoundException ex) {
      // Can't find file
    }
  }
Example #3
0
  private static void rebound() {
    float f = 0.0f;
    float n = 0.0f;
    float b = 0.0f;
    float t = 0.0f;
    float l = 0.0f;
    float r = 0.0f;

    for (Polygon poly : polygons) {
      poly.recalc();

      if (poly.getMinZ() < f) {
        f = poly.getMinZ();
      }
      if (poly.getMaxZ() > n) {
        n = poly.getMaxZ();
      }
      if (poly.getMinY() < b) {
        b = poly.getMinY();
      }
      if (poly.getMaxY() > t) {
        t = poly.getMaxY();
      }
      if (poly.getMinX() < l) {
        l = poly.getMinX();
      }
      if (poly.getMaxX() > r) {
        r = poly.getMaxX();
      }
    }

    float maxSpan = Math.max(Math.max(f - n, t - b), r - l);

    for (Polygon poly : polygons) {
      for (Point3Df p : poly.getPoints()) {
        p.x = p.x * (2 / maxSpan) - (r + l) / (r - l);
        p.y = p.y * (2 / maxSpan) - (t + b) / (t - b);
        p.z = p.z * (2 / maxSpan) - (n + f) / (n - f);
      }

      poly.recalc();
    }
  }
Example #4
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
    }
  }