/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.math;

/**
 * A two dimensional edge between two points
 * 
 * @author Matthias Betz
 *
 */
public class Edge2d {

	private static final double EPSILON = 0.0000001;
	private static final double ONE_MINUS_EPSILON = 1 - EPSILON;

	private ProjectedVector2d a;
	private ProjectedVector2d b;

	public Edge2d(ProjectedVector2d a, ProjectedVector2d b) {
		this.a = a;
		this.b = b;
	}

	public boolean intersects(Edge2d other) {
		double x1 = a.getX();
		double x2 = b.getX();
		double x3 = other.a.getX();
		double x4 = other.b.getX();

		double y1 = a.getY();
		double y2 = b.getY();
		double y3 = other.a.getY();
		double y4 = other.b.getY();

		double a1 = x2 - x1;
		double b1 = x4 - x3;
		double c1 = x3 - x1;
		double a2 = y2 - y1;
		double b2 = y4 - y3;
		double c2 = y3 - y1;

		double detA = b1 * a2 - a1 * b2;
		double detA1 = b1 * c2 - c1 * b2;
		double detA2 = a1 * c2 - c1 * a2;
		if (detA == 0) {
			// lines parallel or coincident
			if (detA1 == 0 && detA2 == 0) {
				// check if they are colinear
				return checkForContainingPoint(other);
			} else {
				// parallel
				return false;
			}
		}
		double s = detA1 / detA;
		double t = detA2 / detA;

		return !(s < EPSILON || s > ONE_MINUS_EPSILON || t < EPSILON || t > ONE_MINUS_EPSILON);
	}

	private boolean checkForContainingPoint(Edge2d other) {
		if (containsPoint(other.a)) {
			return true;
		}
		if (containsPoint(other.b)) {
			return true;
		}
		if (other.containsPoint(a)) {
			return true;
		}
		return other.containsPoint(b);
	}

	public Vector3d projectPoint(double scalar) {
		Vector3d dir = b.getOriginal().minus(a.getOriginal());
		return a.getOriginal().plus(dir.mult(scalar));
	}

	public boolean containsPoint(Vector2d p) {
		double xDir = b.getX() - a.getX();
		if (xDir == 0) {
			double yDir = b.getY() - a.getY();
			if (yDir == 0) {
				// line is one point
				return p.equals(a);
			}
			double d = (p.getY() - a.getY()) / yDir;
			return d < ONE_MINUS_EPSILON && d > EPSILON;
		}
		double d = (p.getX() - a.getX()) / xDir;
		if (d < EPSILON || d > ONE_MINUS_EPSILON) {
			return false;
		}
		// direction in y
		double yDir = b.getY() - a.getY();
		// check for colinearity
		return a.getY() + d * yDir == p.getY();
	}

	@Override
	public String toString() {
		return "Edge2d [a=" + a + ", b=" + b + "]";
	}

	public Vector2d getA() {
		return a;
	}

	public Vector2d getB() {
		return b;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((a == null) ? 0 : a.hashCode());
		result = prime * result + ((b == null) ? 0 : b.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Edge2d other = (Edge2d) obj;
		if (a == null) {
			if (other.a != null)
				return false;
		} else if (!a.equals(other.a))
			return false;
		if (b == null) {
			if (other.b != null)
				return false;
		} else if (!b.equals(other.b))
			return false;
		return true;
	}

}
