/*-
 *  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 static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
 * 
 * @author Matthias Betz
 *
 */
public class Segment3dTest {
	
	@Test
	public void testGetNormalThroughPoint1() {
		Vector3d v0 = new Vector3d(0, -1, 0);
		Vector3d v1 = new Vector3d(1, -1, 0);
		Vector3d v2 = new Vector3d(2, 1, 0);
		
		Segment3d seg = new Segment3d(v0, v1);
		
		assertEquals(new Vector3d(0, 2, 0), seg.getNormalThroughPoint(v2));
	}
	
	@Test
	public void testGetNormalThroughPoint2() {
		Vector3d v0 = new Vector3d(0, -1, 0);
		Vector3d v1 = new Vector3d(1, -1, 0);
		Vector3d v2 = new Vector3d(2, -3, 0);
		
		Segment3d seg = new Segment3d(v0, v1);
		
		assertEquals(new Vector3d(0, -2, 0), seg.getNormalThroughPoint(v2));
	}

	@Test
	public void testGetDistance1() {
		Vector3d v0 = new Vector3d(0, 0, 0);
		Vector3d v1 = new Vector3d(1, 1, 0);
		Vector3d v2 = new Vector3d(1, 0, 0);
		Vector3d v3 = new Vector3d(2, 1, 0);

		Segment3d s1 = new Segment3d(v0, v1);
		Segment3d s2 = new Segment3d(v2, v3);

		assertEquals(1 / Math.sqrt(2), s1.getDistance(s2), 0.000001);
	}

	@Test
	public void testGetDistance2() {
		Vector3d v0 = new Vector3d(0, 0, 1);
		Vector3d v1 = new Vector3d(1, 0, 1);
		Vector3d v2 = new Vector3d(2, 0, 1);
		Vector3d v3 = new Vector3d(3, 0, 1);

		Segment3d s1 = new Segment3d(v0, v1);
		Segment3d s2 = new Segment3d(v2, v3);

		assertEquals(1, s1.getDistance(s2), 0.000001);
	}

	@Test
	public void testGetDistance3() {
		Vector3d v0 = new Vector3d(0, 0, 0);
		Vector3d v1 = new Vector3d(1, 0, 0);
		Vector3d v2 = new Vector3d(2, 0, -1);
		Vector3d v3 = new Vector3d(2, 0, 1);

		Segment3d s1 = new Segment3d(v0, v1);
		Segment3d s2 = new Segment3d(v2, v3);

		assertEquals(1, s1.getDistance(s2), 0.000001);
	}

	@Test
	public void testGetDistance4() {
		Vector3d v0 = new Vector3d(0, 0, 0);
		Vector3d v1 = new Vector3d(1, 1, 0);
		Vector3d v2 = new Vector3d(0, -1, 0);
		Vector3d v3 = new Vector3d(2.1, 1, 0);

		Segment3d s1 = new Segment3d(v0, v1);
		Segment3d s2 = new Segment3d(v2, v3);

		assertEquals(0.7241379, s1.getDistance(s2), 0.000001);
	}

	@Test
	public void testGetDistance5() {
		Vector3d v0 = new Vector3d(0, 0, 0);
		Vector3d v1 = new Vector3d(1, 1, 0);
		Vector3d v2 = new Vector3d(1, 0, 0);
		Vector3d v3 = new Vector3d(0, 1, 0);

		Segment3d s1 = new Segment3d(v0, v1);
		Segment3d s2 = new Segment3d(v2, v3);

		assertEquals(0.0, s1.getDistance(s2), 0.000001);
	}

	@Test
	public void testGetDistancePoint1() {
		Vector3d v0 = new Vector3d(0, 0, 0);
		Vector3d v1 = new Vector3d(1, 0, 0);
		
		Segment3d s1 = new Segment3d(v0, v1);
		
		Vector3d v2 = new Vector3d(2, 0, 0);
		assertEquals(1.0, s1.getDistance(v2), 0.000001);

		Vector3d v3 = new Vector3d(-1, 0, 0);
		assertEquals(1.0, s1.getDistance(v3), 0.000001);

		Vector3d v4 = new Vector3d(0, 1, 0);
		assertEquals(1.0, s1.getDistance(v4), 0.000001);

		Vector3d v5 = new Vector3d(0.5, 1, 1);
		assertEquals(Math.sqrt(2), s1.getDistance(v5), 0.000001);
		
	}

}
