/*-
 *  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.edge;


import java.util.Arrays;

public class Vector3d {

	private double[] val = new double[3];

	public Vector3d(double x, double y, double z) {
		val[0] = x;
		val[1] = y;
		val[2] = z;
	}

	public Vector3d(Point3d point) {
		this(point.getX(), point.getY(), point.getZ());
	}

	public double getLength() {
		double value = val[0] * val[0] + val[1] * val[1] + val[2] * val[2];
		return Math.sqrt(value);
	}

	public void normalize() {
		double length = getLength();
		if (length < Global.getHighAccuracyTolerance()) {
			val[0] = 0;
			val[1] = 0;
			val[2] = 0;
		} else {
			val[0] = val[0] /= length;
			val[1] = val[1] /= length;
			val[2] = val[2] /= length;
		}
	}

	public Vector3d cross(Vector3d crV2) {
		return new Vector3d(getY() * crV2.getZ() - crV2.getY() * getZ(), getZ() * crV2.getX() - crV2.getZ() * getX(),
				getX() * crV2.getY() - crV2.getX() * getY());
	}

	public UnitVector3d toUnitVector() {
		return UnitVector3d.of(this);
	}

	public Vector3d mult(double d) {
		return new Vector3d(val[0] * d, val[1] * d, val[2] * d);
	}

	public Vector3d plus(Vector3d other) {
		return new Vector3d(getX() + other.getX(), getY() + other.getY(), getZ() + other.getZ());
	}

	public double getX() {
		return val[0];
	}

	public double getY() {
		return val[1];
	}

	public double getZ() {
		return val[2];
	}

	public double getLength2() {
		return val[0] * val[0] + val[1] * val[1] + val[2] * val[2];
	}

	public double dot(Vector3d crVector) {
		return this.val[0] * crVector.getX() + this.val[1] * crVector.getY() + this.val[2] * crVector.getZ();
	}

	public static boolean areParallel(Vector3d crV1, Vector3d crV2, double angleEpsilon) {
		return Math.abs((crV1.dot(crV2)) - (crV1.getLength() * crV2.getLength())) < angleEpsilon;
	}

	public static boolean areAntiParallel(Vector3d crV1, Vector3d crV2, double angleEpsilon) {
		return Math.abs((crV1.dot(crV2)) + (crV1.getLength() * crV2.getLength())) < angleEpsilon;
	}

	public static boolean areAntiParallel(Vector3d rDir1, Vector3d rDir2) {
		return areAntiParallel(rDir1, rDir2, Global.getTolVectorsParallel());
	}

	public static boolean areParallel(Vector3d rDir1, Vector3d rDir2) {
		return areParallel(rDir1, rDir2, Global.getTolVectorsParallel());
	}

	public double[] toArray() {
		return Arrays.copyOf(val, 3);
	}

	@Override
	public String toString() {
		return "Vector3d [val=" + Arrays.toString(val) + "]";
	}
}
