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

import static org.junit.Assert.*;

import org.junit.Test;

import de.hft.stuttgart.citydoctor2.datastructure.LinearRing.LinearRingType;
import de.hft.stuttgart.citydoctor2.math.Vector3d;

/**
 * 
 * @author Matthias Betz
 *
 */
public class LinearRingTest {

	@Test
	public void testIsPointInside1() {
		Polygon p = new ConcretePolygon();
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);
		
		Vertex v0 = new Vertex(0, 0, 0);
		Vertex v1 = new Vertex(10, 0, 0);
		Vertex v2 = new Vertex(10, 10, 0);
		Vertex v3 = new Vertex(0, 10, 0);
		lr.addVertex(v0);
		lr.addVertex(v1);
		lr.addVertex(v2);
		lr.addVertex(v3);
		lr.addVertex(v0);
		
		assertTrue(lr.isPointInside(v0));
		assertTrue(lr.isPointInside(v1));
		assertTrue(lr.isPointInside(v2));
		assertTrue(lr.isPointInside(v3));
		
		Vector3d v4 = new Vector3d(0.00001, 0.00001, 0);
		assertTrue(lr.isPointInside(v4));
		Vector3d v5 = new Vector3d(9.9999, 9.9999, 0);
		assertTrue(lr.isPointInside(v5));
		Vector3d v6 = new Vector3d(-0.00001, 0.00001, 0);
		assertFalse(lr.isPointInside(v6));

	}
	
	@Test
	public void testCalculateNormal() {
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		Vertex v0 = new Vertex(0, 0, 0);
		Vertex v1 = new Vertex(10, 0, 0);
		Vertex v2 = new Vertex(10, 10, 0);
		lr.addVertex(v0);
		lr.addVertex(v1);
		lr.addVertex(v2);
		lr.addVertex(v0);
		Vector3d normal = lr.calculateNormalNormalized();
		assertEquals(0.0, normal.getX(), 0.0000001);
		assertEquals(0.0, normal.getY(), 0.0000001);
		assertEquals(1.0, normal.getZ(), 0.0000001);
	}
	
	@Test
	public void testCalculateNormal2() {
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		Vertex v0 = new Vertex(0, 0, 0);
		Vertex v1 = new Vertex(10, 0, 0);
		Vertex v2 = new Vertex(10, 0, 10);
		lr.addVertex(v0);
		lr.addVertex(v1);
		lr.addVertex(v2);
		lr.addVertex(v0);
		Vector3d normal = lr.calculateNormalNormalized();
		assertEquals(0.0, normal.getX(), 0.0000001);
		assertEquals(-1.0, normal.getY(), 0.0000001);
		assertEquals(0.0, normal.getZ(), 0.0000001);
	}
	
	@Test
	public void testCalculateNormal3() {
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		Vertex v0 = new Vertex(0, 0, 0);
		Vertex v1 = new Vertex(0, 10, 0);
		Vertex v2 = new Vertex(0, 10, 10);
		lr.addVertex(v0);
		lr.addVertex(v1);
		lr.addVertex(v2);
		lr.addVertex(v0);
		Vector3d normal = lr.calculateNormalNormalized();
		assertEquals(1.0, normal.getX(), 0.0000001);
		assertEquals(0.0, normal.getY(), 0.0000001);
		assertEquals(0.0, normal.getZ(), 0.0000001);
	}

}
