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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.Collections;

import org.junit.Test;

import de.hft.stuttgart.citydoctor2.check.CheckResult;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
import de.hft.stuttgart.citydoctor2.check.error.RingEdgeIntersectionError;
import de.hft.stuttgart.citydoctor2.datastructure.ConcretePolygon;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.GeometryType;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing.LinearRingType;
import de.hft.stuttgart.citydoctor2.datastructure.Lod;
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;

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

	@Test
	public void testIntersect() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		
		Polygon poly = new ConcretePolygon();
		geom.addPolygon(poly);
		
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		poly.setExteriorRing(lr);
		
		Vertex v0 = new Vertex(0, 0, 6);
		lr.addVertex(v0);
		
		Vertex v1 = new Vertex(10, 10, 4);
		lr.addVertex(v1);

		Vertex v2 = new Vertex(10, 0, 5);
		lr.addVertex(v2);

		Vertex v3 = new Vertex(0, 10, 5);
		lr.addVertex(v3);
		
		lr.addVertex(v0);
		
		geom.updateEdgesAndVertices();
		
		RingSelfIntCheck check = new RingSelfIntCheck();
		
		check.check(lr);
		CheckResult cr = lr.getCheckResult(check);
		assertSame(ResultStatus.ERROR, cr.getResultStatus());
		assertTrue(cr.getError() instanceof RingEdgeIntersectionError);
		RingEdgeIntersectionError err = (RingEdgeIntersectionError) cr.getError();
		assertNotNull(err.getIntersection());
		assertEquals(5d, err.getIntersection().getX(), 0.00001);
		assertEquals(5d, err.getIntersection().getY(), 0.00001);
		assertEquals(5d, err.getIntersection().getZ(), 0.00001);
	}
	
	@Test
	public void testNonIntersect() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		
		Polygon poly = new ConcretePolygon();
		geom.addPolygon(poly);
		
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		poly.setExteriorRing(lr);
		
		Vertex v0 = new Vertex(0, 0, 0);
		lr.addVertex(v0);
		
		Vertex v1 = new Vertex(10, 0, 0);
		lr.addVertex(v1);

		Vertex v2 = new Vertex(10, 10, 0);
		lr.addVertex(v2);

		Vertex v3 = new Vertex(0, 10, 0);
		lr.addVertex(v3);
		
		lr.addVertex(v0);
		
		geom.updateEdgesAndVertices();
		
		RingSelfIntCheck check = new RingSelfIntCheck();
		
		check.check(lr);
		CheckResult cr = lr.getCheckResult(check);
		assertSame(ResultStatus.OK, cr.getResultStatus());
	}
	
	@Test
	public void testTouching() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		
		Polygon poly = new ConcretePolygon();
		geom.addPolygon(poly);
		
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		poly.setExteriorRing(lr);
		
		Vertex v0 = new Vertex(5, 0, 0);
		lr.addVertex(v0);
		
		Vertex v1 = new Vertex(5, 3, 0);
		lr.addVertex(v1);

		Vertex v2 = new Vertex(2.5, 0.00005, 0);
		lr.addVertex(v2);

		Vertex v3 = new Vertex(0, 3, 0);
		lr.addVertex(v3);
		
		Vertex v4 = new Vertex(0, 0, 0);
		lr.addVertex(v4);

		lr.addVertex(v0);
		
		geom.updateEdgesAndVertices();
		
		RingSelfIntCheck check = new RingSelfIntCheck();
		check.init(Collections.emptyMap(), new ParserConfiguration(2, false));
		
		check.check(lr);
		CheckResult cr = lr.getCheckResult(check);
		assertEquals(ResultStatus.ERROR, cr.getResultStatus());

	}	
	
	@Test
	public void testNotTouching() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		
		Polygon poly = new ConcretePolygon();
		geom.addPolygon(poly);
		
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		poly.setExteriorRing(lr);
		lr.setParent(poly);
		
		Vertex v0 = new Vertex(5, 0, 0);
		lr.addVertex(v0);
		
		Vertex v1 = new Vertex(5, 3, 0);
		lr.addVertex(v1);

		Vertex v2 = new Vertex(2.5, 0.05, 0);
		lr.addVertex(v2);

		Vertex v3 = new Vertex(0, 3, 0);
		lr.addVertex(v3);
		
		Vertex v4 = new Vertex(0, 0, 0);
		lr.addVertex(v4);

		
		lr.addVertex(v0);
		
		geom.updateEdgesAndVertices();
		
		RingSelfIntCheck check = new RingSelfIntCheck();
		check.init(Collections.emptyMap(), new ParserConfiguration(2, false));
		
		check.check(lr);
		CheckResult cr = lr.getCheckResult(check);
		assertSame(ResultStatus.OK, cr.getResultStatus());

	}

}
