private static boolean getNumberOfLines(File file, int total, float mean) {

    try {
      double difSum = 0;
      long last, actual;
      int nLines = 0;

      Scanner scan = new Scanner(file);
      scan.useLocale(Locale.US);

      if (scan.hasNextLine()) {
        scan.nextLine();
        if (scan.hasNextLong() && scan.hasNextLine()) {
          last = scan.nextLong();
          nLines++;
          scan.nextLine();
        } else {
          scan.close();
          return false;
        }
      } else {
        scan.close();
        return false;
      }

      while (scan.hasNextLong()) {
        actual = scan.nextLong();
        nLines++;
        difSum += actual - last;
        last = actual;

        if (scan.hasNextLine()) {
          scan.nextLine();
        } else {
          break;
        }
      }

      scan.close();

      total = nLines;
      mean = (float) difSum / (total - 1);

      return true;
    } catch (FileNotFoundException e1) {
      return false;
    }
  }
Example #2
0
  /** Leicht antiquierte Funktion, um das eigentliche Mesh zu laden */
  private boolean doLoad() {
    try {
      ArrayList<Vec3> v = new ArrayList<Vec3>();
      ArrayList<Vec3> vn = new ArrayList<Vec3>();
      ArrayList<Vec3> vt = new ArrayList<Vec3>();
      ArrayList<Triangle3D> f = new ArrayList<Triangle3D>();

      Scanner in = new Scanner(infile);
      in.useLocale(java.util.Locale.US); // Punkt statt Komma...

      while (in.hasNextLine()) {
        String line = in.nextLine().trim();
        String[] tokens = line.split(" ");

        if (tokens[0].equals("v")) {
          // Neuen Vertex einlesen von der Form:
          // 		v 0.83912 -1.14377 -1.11928
          v.add(
              new Vec3(new Double(tokens[1]), new Double(tokens[2]), new Double(tokens[3]))
                  .plus(origin));
        }

        if (tokens[0].equals("vn")) {
          // Neue Normale einlesen von der Form:
          // 		vn 0.99454 0.0172 0.10289
          vn.add(
              new Vec3(new Double(tokens[1]), new Double(tokens[2]), new Double(tokens[3]))
                  .normalized());
        }

        if (tokens[0].equals("vt")) {
          // Neue Texturkoordinate einlesen von der Form:
          // 		vt 0.33333333 0.66666667
          vt.add(new Vec3(new Double(tokens[1]), new Double(tokens[2]), 0.0));
        }

        if (tokens[0].equals("f")) {
          // Neues Dreieck einlesen. Es wird angenommen, dass
          // jetzt schon die entsprechenden Vertices und
          // Normalen definiert sind. Sonst knallt's. Form:
          // 		f 611//21799 10911//21799 3904//21799
          // Also: Vertex//Normale, jeweils der Index

          if (tokens.length != 4) {
            System.err.println("FEHLER! Das ist kein TriMesh!");
            return false;
          }

          String p1[] = tokens[1].split("/");
          String p2[] = tokens[2].split("/");
          String p3[] = tokens[3].split("/");

          // Beachten, dass die Indizes in OBJ-Files bei 1
          // anfangen..........

          Vec3[] verts =
              new Vec3[] {
                v.get(new Integer(p1[0]) - 1),
                v.get(new Integer(p2[0]) - 1),
                v.get(new Integer(p3[0]) - 1)
              };
          Vec3[] norms =
              new Vec3[] {
                vn.get(new Integer(p1[2]) - 1),
                vn.get(new Integer(p2[2]) - 1),
                vn.get(new Integer(p3[2]) - 1)
              };

          Vec3[] tex = null;
          if (!p1[1].equals("")) {
            tex =
                new Vec3[] {
                  vt.get(new Integer(p1[1]) - 1),
                  vt.get(new Integer(p2[1]) - 1),
                  vt.get(new Integer(p3[1]) - 1)
                };
          }

          f.add(new Triangle3D(verts, norms, tex, mat, smooth));
        }
      }

      out = f.toArray(new RenderingPrimitive[0]);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return true;
  }
  public static boolean generateTrainingFile(
      String sensorDataFolder,
      String sensorDataFile,
      String sensorMarksFolder,
      String sensorMarksFile,
      String outFolder,
      String outFile,
      Context context) {
    IWindowData windowData = new WindowHalfOverlap(128);
    IFeatures myFeatures = new MyFeatures();

    Scanner scanSD = null, scanSM = null;
    File sensorData = MyUtil.getSDFile(sensorDataFolder, sensorDataFile);
    File sensorMarks = MyUtil.getSDFile(sensorMarksFolder, sensorMarksFile);

    try {
      scanSD = new Scanner(sensorData);
      scanSD.useLocale(Locale.US);
    } catch (FileNotFoundException e1) {
      return false;
    }

    try {
      scanSM = new Scanner(sensorMarks);
      scanSM.useLocale(Locale.US);
    } catch (FileNotFoundException e1) {
      scanSD.close();
      return false;
    }

    // progress bar
    int total = getNumberOfLines(sensorData);

    /*
       int total = 0;
       float mean = 0;
       boolean correct = getNumberOfLines(sensorData, total, mean);
       if(!correct){
       	scanSM.close();
       	scanSD.close();
       	return false;
       }

       Intent intent = new Intent("com.example.activityregistrator.UPDATE_HZ");
    intent.putExtra("hz", 1/mean);
    context.sendBroadcast(intent);
    */
    float done = 0;

    Long prevTimeSM, postTimeSM, timeSD = -1l, lastTimeSD = 0l, firstTimeSD = -1l;
    int prevLabel, postLabel;

    if (scanSM.hasNextLine()) {
      scanSM.nextLine();
      prevTimeSM = scanSM.nextLong();
      prevLabel = scanSM.nextInt();

      scanSD.nextLine();
      Intent intent = new Intent("com.example.activityregistrator.UPDATE_PROGRESS");
      intent.putExtra("progress", (float) ((++done) / total) * 100);
      context.sendBroadcast(intent);

      // find next time and label
      while (scanSM.hasNextLine()) {
        scanSM.nextLine();
        if (!scanSM.hasNextLong()) break;

        Log.d(TAG, "prevTimeSM: " + prevTimeSM + "; prevLabel: " + prevLabel);

        postTimeSM = scanSM.nextLong();
        postLabel = scanSM.nextInt();

        Log.d(TAG, "postTimeSM: " + postTimeSM + "; postLabel: " + postLabel);

        // each window has unique label
        windowData.clean();

        // search windowData beginning
        // assumption: register only label>0  -->  IGNORE<=0
        while (prevLabel > 0) {

          if (timeSD < 0) {
            if (!scanSD.hasNextLong()) {
              break;
            }
            timeSD = scanSD.nextLong();
          }

          // used to calculate hz
          lastTimeSD = timeSD;
          if (firstTimeSD < 0) firstTimeSD = timeSD;

          if (timeSD >= prevTimeSM) {
            if (timeSD <= postTimeSM) {
              Log.d(TAG, "timeSD: " + timeSD);

              // add x y z to windowData
              float[] linearAccel = {scanSD.nextFloat(), scanSD.nextFloat(), scanSD.nextFloat()};

              if (windowData.addData((float) vecLength(linearAccel))) {
                // we have a complete windowData

                // calculate features
                float[] features = myFeatures.getFeatures(windowData);

                // save to libsvm file with the correct label
                String[] textToFile = new String[1];
                textToFile[0] = prevLabel + " ";
                for (int i = 0; i < features.length; i++) {
                  textToFile[0] += (i + 1) + ":" + features[i] + " ";
                }
                MyUtil.writeToSDFile(textToFile, outFolder, outFile, true);
              }

              if (scanSD.hasNextLine()) {
                scanSD.nextLine();
                intent = new Intent("com.example.activityregistrator.UPDATE_PROGRESS");
                intent.putExtra("progress", (float) ((++done) / total) * 100);
                context.sendBroadcast(intent);
                timeSD = -1l; // get nextLong()
              } else {
                break;
              }
            } else {
              break;
            }
          } else {
            if (scanSD.hasNextLine()) {
              scanSD.nextLine();
              intent = new Intent("com.example.activityregistrator.UPDATE_PROGRESS");
              intent.putExtra("progress", (float) ((++done) / total) * 100);
              context.sendBroadcast(intent);
              timeSD = -1l; // get nextLong()
            } else {
              break;
            }
          }
        }

        prevTimeSM = postTimeSM;
        prevLabel = postLabel;
      }
    }

    scanSD.close();
    scanSM.close();

    Intent intent = new Intent("com.example.activityregistrator.UPDATE_HZ");
    intent.putExtra("hz", done / (((float) (lastTimeSD - firstTimeSD)) / 1000f));
    context.sendBroadcast(intent);

    Log.d(TAG, "[firstTimeSD,lastTimeSD,done]: " + firstTimeSD + " " + lastTimeSD + " " + done);

    return true;
  }