import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

import Jcg.geometry.Point_;
import Jcg.geometry.Point_2;
import Jcg.geometry.Point_3;


public class IO {

	public static Collection<Point_> readPointCloud(String filename) {
		Collection<Point_> res = new ArrayList<Point_>();
		File file;
    	FileReader readfic = null;
    	BufferedReader input;
    	String line;
    	System.out.print(" --- Reading point cloud file " + filename + "...");
    	file = new File(filename);
    	try {
			readfic = new FileReader(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	input = new BufferedReader(readfic);
            
    	try {
    		boolean begin = true;
    		int dim = 0;
    		while ((line=input.readLine()) != null) {
				StringTokenizer tok = new StringTokenizer(line);
				if (begin) {
					begin = false;
					dim = Integer.parseInt(tok.nextToken());
					if (dim<2 || dim>3)
						throw new Error("Unhandled dimension (" + dim + ")");
//					System.out.println("dim=" + dim);
					continue;
				}
				double px=Double.parseDouble(tok.nextToken());
				double py=Double.parseDouble(tok.nextToken());            
				if (dim==2)
					res.add (new Point_2(px, py));
				else {  // dim == 3
					double pz = Double.parseDouble(tok.nextToken());
					res.add(new Point_3(px, py, pz));
				}
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	try {
			input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	System.out.println(" done");
    	return res;
	}

}

