package com.cringe_studios.christmastreescanning;

import java.awt.geom.Point2D;

import com.cringe_studios.christmastreescanning.scanning.PointInterpolator;

public class Homograph4 implements Homograph{
	// Homograph 4 simplifies the code by first merging back and front side, as well as right and left side together. This step isn't perfect though, because the scanned images are not perfect.
	private Homograph2 internalHomograph;
	
	public Homograph4(Point2D[] frontSidePoints, Point2D[] rightSidePoints, Point2D[] behindSidePoints, Point2D[] leftSidePoints) {
		Point2D[] mergedFrontBack = new Point2D[frontSidePoints.length];
		Point2D[] mergedRightLeft = new Point2D[frontSidePoints.length];
		
		Point2D[] flippedBehindSide = new Point2D[frontSidePoints.length];
		Point2D[] flippedLeftSide = new Point2D[frontSidePoints.length];
		
		for(int i = 0; i < frontSidePoints.length; i++) {
			// the x coordinate goes from -1 to 1 after normalization
			flippedBehindSide[i] = new Point2D.Double(behindSidePoints[i].getX() * -1.0, behindSidePoints[i].getY());
			flippedLeftSide[i] = new Point2D.Double(leftSidePoints[i].getX() * -1.0, leftSidePoints[i].getY());
		}

		
		for(int i = 0; i < frontSidePoints.length; i++) {
			Point2D interpolatedFrontBehind = new PointInterpolator(frontSidePoints[i], flippedBehindSide[i]).getCenter();
			Point2D interpolatedRightLeft = new PointInterpolator(rightSidePoints[i], leftSidePoints[i]).getCenter();
			
			mergedFrontBack[i] = interpolatedFrontBehind;
			mergedRightLeft[i] = interpolatedRightLeft;
		}
	}
	
	@Override
	public void merge() {
		internalHomograph.merge();
	}

	@Override
	public void renormalize() {
		internalHomograph.renormalize();
	}

	@Override
	public Point3D[] getPoints() {
		return internalHomograph.getPoints();
	}
}
