/*-
 *  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 de.hft.stuttgart.citydoctor2.checks.util.GeometryTestUtils.addPolygon;
import static de.hft.stuttgart.citydoctor2.checks.util.GeometryTestUtils.createVertex;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import de.hft.stuttgart.citydoctor2.check.CheckResult;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
import de.hft.stuttgart.citydoctor2.checks.util.GeometryTestUtils;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.GeometryType;
import de.hft.stuttgart.citydoctor2.datastructure.Lod;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;

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

	@Test
	public void testNonManifoldVertex() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		
		Vertex v12 = createVertex(10, 10, 3, geom);
		Vertex v13 = createVertex(10, 10, 0, geom);
		Vertex v9 = createVertex(10, 15, 0, geom);
		Vertex v10 = createVertex(10, 15, 3, geom);
		Vertex v14 = createVertex(11.5, 10, 4.5, geom);
		Vertex v11 = createVertex(11.5, 15, 4.5, geom);
		Vertex v0 = createVertex(13, 15, 0, geom);
		Vertex v1 = createVertex(13, 15, 3, geom);
		Vertex v2 = createVertex(13, 10, 3, geom);
		Vertex v3 = createVertex(13, 10, 0, geom);
		Vertex v8 = createVertex(13, 16, 3, geom);
		Vertex v5 = createVertex(13.5, 15.5, 3.5, geom);
		Vertex v6 = createVertex(14, 15, 3, geom);
		Vertex v7 = createVertex(14, 16, 3, geom);
		
		List<Vertex> vertices = new ArrayList<>();
		Collections.addAll(vertices, v0, v1, v2, v3, v12, v13, v9, v10, v14, v11, v8, v5, v6, v7);
		geom.setVertices(vertices);
		
		addPolygon(geom, v0, v1, v2, v3);
		addPolygon(geom, v1, v6, v5);
		addPolygon(geom, v6, v7, v5);
		addPolygon(geom, v7, v8, v5);
		addPolygon(geom, v8, v1, v5);
		addPolygon(geom, v1, v8, v7, v6);
		
		addPolygon(geom, v9, v10, v11, v1, v0);
		addPolygon(geom, v12, v10, v9, v13);
		addPolygon(geom, v3, v2, v14, v12, v13);
		addPolygon(geom, v12, v14, v11, v10);
		addPolygon(geom, v14, v2, v1, v11);
		addPolygon(geom, v13, v9, v0, v3);
		geom.updateEdges();
		
		ManifoldVertexCheck check = new ManifoldVertexCheck();
		check.check(geom);
		CheckResult cr = geom.getCheckResult(check);
		Assert.assertNotNull(cr);
		Assert.assertEquals(ResultStatus.ERROR, cr.getResultStatus());
		

	}
	
	@Test
	public void testGoodSolid() {
		Geometry g = GeometryTestUtils.createGoodGeometry();
		
		g.updateEdges();
		ManifoldVertexCheck check = new ManifoldVertexCheck();
		check.check(g);
		CheckResult cr = g.getCheckResult(check);
		Assert.assertNotNull(cr);
		Assert.assertEquals(ResultStatus.OK, cr.getResultStatus());
	}

}
