/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package colorsegmentation;

import baseclasses.ObjExport;
import baseclasses.ObjImport;
import baseclasses.Point;
import baseclasses.Triangle;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Alex
 */
public class RedCenter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        String fileName = "Scan 4-9-15-Defect2Center.obj";
        
        ObjImport scanImport = new ObjImport(fileName);
        try {
            scanImport.read();
        } catch (IOException e) {
            System.out.println("wat.");
        }
        Point[] pts = scanImport.getPoints();
        int[] red = {256, 0, 0};
        float[] hsb = hsbColorConvert(red);
        double[] tol = {2.0,0.1,0.1};
        Triangle[] tri = scanImport.getColorPoints(hsb, tol);
        Point[] colorPts = condensePoints(pts, tri);
        System.out.println("Number of Red Points: " + colorPts.length);
        ObjExport ex = new ObjExport("Center.obj", 
                colorPts, null);
        try {
            ex.write();
        } catch (IOException e) {
            System.out.println("Error on Export");
        }
    }
    
    public static Point[] condensePoints(Point[] objPts, Triangle[] tri) {
        ArrayList<Integer> ptInds = new ArrayList<Integer>();
        for (Triangle tTri : tri) {
            int[] inds = tTri.getVertIndices();
            for (int i = 0; i < inds.length; i++) {
                if (!ptInds.contains(inds[i])) {
                    ptInds.add(inds[i]);
                }
            }
        }
        Point[] pts = new Point[ptInds.size()];
        for (int i = 0; i < ptInds.size(); i++) {
            pts[i] = objPts[ptInds.get(i)];
        }
        return pts;
    }
    
    /**
     * Convert RGB to HSB.
     * @param rgb int array of R, G, B. 
     * @return a float of HSB colors.
     */
    public static float[] hsbColorConvert(int[] rgb) {
        float[] tVertColor = new float[3];
        tVertColor = Color.RGBtoHSB(rgb[0], rgb[1], rgb[2], null);
        tVertColor[0] = tVertColor[0] * 360.0f;
        tVertColor[1] = tVertColor[1] * 100.0f;
        tVertColor[2] = tVertColor[2] * 100.0f;
        return tVertColor;
    }
}
