package com.cringe_studios.christmastreescanning.scanning;

import java.awt.geom.Point2D;

public class PointNormalizer {
	Point2D[] points;
	
	public PointNormalizer(Point2D[] newPoints) {
		this.points = newPoints;
	}
	/**
	 * This method normalizes the Point coordinates,
	 * Adjusts the height (y-coordinate) to make the virtual points not squished (puts them into a rectangle)
	 * 
	 * To get the normalized Points call .getPoints();
	 * 
	 */
	public void normalizePoints() {
		if(points == null) return;
		
		double smallestXValue = Double.POSITIVE_INFINITY;
		double biggestXValue = Double.NEGATIVE_INFINITY;
		double smallestYValue = Double.POSITIVE_INFINITY;
		double biggestYValue = Double.NEGATIVE_INFINITY;
		
		for(int i = 0; i < points.length; i++) {
			if(points[i].getX() < smallestXValue) {
				smallestXValue = points[i].getX();
			}
			
			if(points[i].getX() > biggestXValue) {
				biggestXValue = points[i].getX();
			}
			
			if(points[i].getY() < smallestYValue) {
				smallestYValue = points[i].getY();
			}
			
			if(points[i].getY() > biggestYValue) {
				biggestYValue = points[i].getY();
			}
		}
		
		// x between -1 and 1
		// y between 0 and 1
		
		Point2D normalizedPoints[] = new Point2D.Double[points.length];
		
		for(int i = 0; i < normalizedPoints.length; i++) {
			double normalizedX = (points[i].getX() - smallestXValue) / (biggestXValue - smallestXValue);
			double normalizedY = (points[i].getY() - smallestYValue) / (biggestYValue - smallestYValue);;
			
			normalizedX = normalizedX * 2.0 - 1.0;
			
			normalizedPoints[i] = new Point2D.Double(normalizedX, normalizedY);
		}
		
		this.points = normalizedPoints;
	}
	
	public Point2D[] getPoints() {
		return points;
	}
}
