/*-
 *  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 org.junit.Assert;
import org.junit.Test;

import de.hft.stuttgart.citydoctor2.check.ResultStatus;
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.GmlId;
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;

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

	/**
	 * One interior ring disconnects interior
	 */
	@Test
	public void testBadPolygon1() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);

		Polygon p = new ConcretePolygon();
		geom.addPolygon(p);
		
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);

		Vertex v0 = new Vertex(2, 2, 0);
		lr.addVertex(v0);

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

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

		Vertex v3 = new Vertex(2, 10, 0);
		lr.addVertex(v3);
		lr.addVertex(v0);

		LinearRing intRing = new LinearRing(LinearRingType.INTERIOR);
		p.addInteriorRing(intRing);

		Vertex v4 = new Vertex(4, 2, 0);
		intRing.addVertex(v4);

		Vertex v5 = new Vertex(2, 6, 0);
		intRing.addVertex(v5);

		Vertex v6 = new Vertex(4, 6, 0);
		intRing.addVertex(v6);
		intRing.addVertex(v4);

		InteriorDisconnectedCheck c = new InteriorDisconnectedCheck();
		c.check(p);

		Assert.assertEquals(ResultStatus.ERROR, p.getCheckResult(c).getResultStatus());
	}
	
	/**
	 * two interior rings are disconnecting some interior
	 */
	@Test
	public void testBadPolygon2() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);

		Polygon p = new ConcretePolygon();
		geom.addPolygon(p);
		
		Vertex v0 = addVertex(0, 0, 0);
		Vertex v1 = addVertex(20, 0, 0);
		Vertex v2 = addVertex(20, 20, 0);
		Vertex v3 = addVertex(0, 20, 0);

		Vertex v4 = addVertex(10, 10, 0);
		Vertex v5 = addVertex(12, 12, 0);
		Vertex v6 = addVertex(10, 14, 0);
		Vertex v7 = addVertex(14, 14, 0);
		Vertex v8 = addVertex(14, 10, 0);
		
		Vertex v9 = addVertex(8, 12, 0);
		
		LinearRing ext = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(ext);
		
		ext.addVertex(v0);
		ext.addVertex(v1);
		ext.addVertex(v2);
		ext.addVertex(v3);
		ext.addVertex(v0);
		
		LinearRing intRing1 = new LinearRing(LinearRingType.INTERIOR);
		p.addInteriorRing(intRing1);
		
		intRing1.addVertex(v4);
		intRing1.addVertex(v5);
		intRing1.addVertex(v6);
		intRing1.addVertex(v7);
		intRing1.addVertex(v8);
		intRing1.addVertex(v4);
		
		LinearRing intRing2 = new LinearRing(LinearRingType.INTERIOR);
		p.addInteriorRing(intRing2);
		
		intRing2.addVertex(v4);
		intRing2.addVertex(v9);
		intRing2.addVertex(v6);
		intRing2.addVertex(v4);
		
		InteriorDisconnectedCheck c = new InteriorDisconnectedCheck();
		c.check(p);

		Assert.assertEquals(ResultStatus.ERROR, p.getCheckResult(c).getResultStatus());
	}
	
	/**
	 * three interior rings are disconnecting some interior
	 */
	@Test
	public void testBadPolygon3() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);

		Polygon p = new ConcretePolygon();
		geom.addPolygon(p);
		
		Vertex v0 = addVertex(0, 0, 0);
		Vertex v1 = addVertex(10, 0, 0);
		Vertex v2 = addVertex(10, 10, 0);
		Vertex v3 = addVertex(0, 10, 0);

		Vertex v4 = addVertex(3, 3, 0);
		Vertex v5 = addVertex(2, 4, 0);
		Vertex v6 = addVertex(3, 5, 0);
		Vertex v7 = addVertex(5, 5, 0);
		Vertex v8 = addVertex(5, 4, 0);
		
		Vertex v9 = addVertex(5, 3, 0);
		
		LinearRing ext = new LinearRing(LinearRingType.EXTERIOR);
		ext.setGmlId(new GmlId("ext"));
		p.setExteriorRing(ext);
		
		ext.addVertex(v0);
		ext.addVertex(v1);
		ext.addVertex(v2);
		ext.addVertex(v3);
		ext.addVertex(v0);
		
		LinearRing intRing2 = new LinearRing(LinearRingType.INTERIOR);
		intRing2.setGmlId(new GmlId("IntRing2"));
		p.addInteriorRing(intRing2);
		
		intRing2.addVertex(v6);
		intRing2.addVertex(v7);
		intRing2.addVertex(v8);
		intRing2.addVertex(v6);
		
		LinearRing intRing1 = new LinearRing(LinearRingType.INTERIOR);
		intRing1.setGmlId(new GmlId("IntRing1"));
		p.addInteriorRing(intRing1);
		
		intRing1.addVertex(v4);
		intRing1.addVertex(v5);
		intRing1.addVertex(v6);
		intRing1.addVertex(v4);

		
		LinearRing intRing3 = new LinearRing(LinearRingType.INTERIOR);
		intRing3.setGmlId(new GmlId("IntRing3"));
		p.addInteriorRing(intRing3);
		
		intRing3.addVertex(v8);
		intRing3.addVertex(v9);
		intRing3.addVertex(v4);
		intRing3.addVertex(v8);

		
		InteriorDisconnectedCheck c = new InteriorDisconnectedCheck();
		c.check(p);

		Assert.assertEquals(ResultStatus.ERROR, p.getCheckResult(c).getResultStatus());
	}
	
	/**
	 * three interior rings are NOT disconnecting some interior
	 */
	@Test
	public void testGoodPolygon3() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);

		Polygon p = new ConcretePolygon();
		geom.addPolygon(p);
		
		Vertex v0 = addVertex(0, 0, 0);
		Vertex v1 = addVertex(10, 0, 0);
		Vertex v2 = addVertex(10, 10, 0);
		Vertex v3 = addVertex(0, 10, 0);

		Vertex v4 = addVertex(3, 3, 0);
		Vertex v5 = addVertex(2, 4, 0);
		Vertex v6 = addVertex(3, 5, 0);
		Vertex v7 = addVertex(5, 5, 0);
		Vertex v8 = addVertex(5, 4, 0);
		Vertex v9 = addVertex(5, 3, 0);
		Vertex v10 = addVertex(2, 3, 0);
		
		LinearRing ext = new LinearRing(LinearRingType.EXTERIOR);
		ext.setGmlId(new GmlId("ext"));
		p.setExteriorRing(ext);
		
		ext.addVertex(v0);
		ext.addVertex(v1);
		ext.addVertex(v2);
		ext.addVertex(v3);
		
		LinearRing intRing1 = new LinearRing(LinearRingType.INTERIOR);
		intRing1.setGmlId(new GmlId("intRing1"));
		p.addInteriorRing(intRing1);
		
		intRing1.addVertex(v10);
		intRing1.addVertex(v5);
		intRing1.addVertex(v6);
		
		LinearRing intRing2 = new LinearRing(LinearRingType.INTERIOR);
		intRing2.setGmlId(new GmlId("intRing2"));
		p.addInteriorRing(intRing2);
		
		intRing2.addVertex(v6);
		intRing2.addVertex(v7);
		intRing2.addVertex(v8);
		
		LinearRing intRing3 = new LinearRing(LinearRingType.INTERIOR);
		intRing3.setGmlId(new GmlId("intRing3"));
		p.addInteriorRing(intRing3);
		
		intRing3.addVertex(v8);
		intRing3.addVertex(v9);
		intRing3.addVertex(v4);

		
		InteriorDisconnectedCheck c = new InteriorDisconnectedCheck();
		c.check(p);

		Assert.assertEquals(ResultStatus.OK, p.getCheckResult(c).getResultStatus());
	}

	
	@Test
	public void testGoodPolygon2() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);

		Polygon p = new ConcretePolygon();
		geom.addPolygon(p);
		
		Vertex v0 = addVertex(0, 0, 0);
		Vertex v1 = addVertex(20, 0, 0);
		Vertex v2 = addVertex(20, 20, 0);
		Vertex v3 = addVertex(0, 20, 0);

		Vertex v4 = addVertex(10, 10, 0);
		Vertex v5 = addVertex(12, 12, 0);
		Vertex v6 = addVertex(10, 14, 0);
		Vertex v7 = addVertex(14, 14, 0);
		Vertex v8 = addVertex(14, 10, 0);
		
		Vertex v9 = addVertex(8, 12, 0);
		Vertex v10 = addVertex(8, 10, 0);
		
		LinearRing ext = new LinearRing(LinearRingType.EXTERIOR);
		ext.setGmlId(new GmlId("ext"));
		p.setExteriorRing(ext);
		
		ext.addVertex(v0);
		ext.addVertex(v1);
		ext.addVertex(v2);
		ext.addVertex(v3);
		
		LinearRing intRing1 = new LinearRing(LinearRingType.INTERIOR);
		intRing1.setGmlId(new GmlId("intRing1"));
		p.addInteriorRing(intRing1);
		
		intRing1.addVertex(v4);
		intRing1.addVertex(v5);
		intRing1.addVertex(v6);
		intRing1.addVertex(v7);
		intRing1.addVertex(v8);
		
		LinearRing intRing2 = new LinearRing(LinearRingType.INTERIOR);
		intRing2.setGmlId(new GmlId("IntRing2"));
		p.addInteriorRing(intRing2);
		
		intRing2.addVertex(v10);
		intRing2.addVertex(v9);
		intRing2.addVertex(v6);
		
		InteriorDisconnectedCheck c = new InteriorDisconnectedCheck();
		c.check(p);

		Assert.assertEquals(ResultStatus.OK, p.getCheckResult(c).getResultStatus());
	}
	
	private Vertex addVertex(double x, double y, double z) {
		Vertex v0 = new Vertex(x, y, z);
		return v0;
	}

}
