Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.os.Environment;

import android.util.Log;

import java.io.DataInputStream;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

public class Main {
    final private static String TAG = "IOHelper";

    public static float[] readBinAverageShapeArray(String dir, String fileName, int size) {
        float x;
        int i = 0;
        float[] tab = new float[size];

        // theses values was for 64140 points average shape
        //float minX = -90.3540f, minY = -22.1150f, minZ = -88.7720f, maxX = 101.3830f, maxY = 105.1860f, maxZ = 102.0530f;

        // values for average shape after simplification (8489 points)
        float maxX = 95.4549f, minX = -85.4616f, maxY = 115.0088f, minY = -18.0376f, maxZ = 106.7329f,
                minZ = -90.4051f;

        float deltaX = (maxX - minX) / 2.0f;
        float deltaY = (maxY - minY) / 2.0f;
        float deltaZ = (maxZ - minZ) / 2.0f;

        float midX = (maxX + minX) / 2.0f;
        float midY = (maxY + minY) / 2.0f;
        float midZ = (maxZ + minZ) / 2.0f;

        File sdLien = Environment.getExternalStorageDirectory();
        File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
        Log.d(TAG, "path of file : " + inFile);
        if (!inFile.exists()) {
            throw new RuntimeException("File doesn't exist");
        }
        DataInputStream in = null;
        try {
            in = new DataInputStream(new FileInputStream(inFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //read first x
            x = in.readFloat();
            //Log.d(TAG,"first Vertices = "+x);
            while (true) {

                tab[i] = (x - midX) / deltaX; //rescale x
                i++;

                //read y
                x = in.readFloat();
                tab[i] = (x - midY) / deltaY; //rescale y
                i++;

                //read z
                x = in.readFloat();
                tab[i] = (x - midZ) / deltaZ; //rescale z
                i++;

                //read x
                x = in.readFloat();
            }
        } catch (EOFException e) {
            try {
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try { //free ressources
                    Log.d(TAG, "close");
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return tab;
    }
}