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

import java.util.Arrays;

/**
 * A unit vector3d which always has the length 1.
 * @author Matthias Betz
 *
 */
public class UnitVector3d extends Vector3d {
	
	private static final String UNIT_VECTOR_IS_IMMUTABLE = "Unit vector is immutable";

	private static final long serialVersionUID = -374685263673211587L;
	
	public static final UnitVector3d X_AXIS = new UnitVector3d(1, 0, 0);
	public static final UnitVector3d Y_AXIS = new UnitVector3d(0, 1, 0);
	public static final UnitVector3d Z_AXIS = new UnitVector3d(0, 0, 1);
	
	public static UnitVector3d of(Vector3d v) {
		double length = v.getLength();
		double x = v.getX() / length;
		double y = v.getY() / length;
		double z = v.getZ() / length;
		return new UnitVector3d(x, y, z);
	}
	
	private UnitVector3d(double x, double y, double z) {
		super(x, y, z);
	}
	
	@Override
	public double getLength() {
		return 1;
	}
	
	@Override
	public double getSquaredLength() {
		return 1;
	}
	
	@Override
	public UnitVector3d normalize() {
		return this;
	}

	/**
	 * creates a new unit vector in the other direction, still with length 1
	 * @return the normal vector in the opposite direction
	 */
	public UnitVector3d invert() {
		return new UnitVector3d(-getX(), -getY(), -getZ());
	}
	
	@Override
	public UnitVector3d copyVector() {
		return new UnitVector3d(getX(), getY(), getZ());
	}
	
	@Override
	public double[] getCoordinates() {
		return Arrays.copyOf(super.getCoordinates(), 3);
	}
	
	@Override
	public void setX(double x) {
		throw new UnsupportedOperationException(UNIT_VECTOR_IS_IMMUTABLE);
	}
	
	@Override
	public void setY(double y) {
		throw new UnsupportedOperationException(UNIT_VECTOR_IS_IMMUTABLE);
	}
	
	@Override
	public void setZ(double z) {
		throw new UnsupportedOperationException(UNIT_VECTOR_IS_IMMUTABLE);
	}
	
	

}
