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

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

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;

/**
 * 
 * @author Matthias Betz
 *
 */
public class GeometryTestUtils {
	
	public static Geometry createGoodGeometry() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		
		Vertex v0 = createVertex(0, 0, 0, geom);
		Vertex v1 = createVertex(10, 0, 0, geom);
		Vertex v2 = createVertex(10, 10, 0, geom);
		Vertex v3 = createVertex(0, 10, 0, geom);
		Vertex v4 = createVertex(0, 0, 10, geom);
		Vertex v5 = createVertex(10, 0, 10, geom);
		Vertex v6 = createVertex(10, 10, 10, geom);
		Vertex v7 = createVertex(0, 10, 10, geom);
		
		List<Vertex> vertices = new ArrayList<>();
		Collections.addAll(vertices, v0, v1, v2, v3, v4, v5, v6, v7);
		geom.setVertices(vertices);
		
		addPolygon(geom, v0, v1, v2, v3);
		addPolygon(geom, v1, v5, v6, v2);
		addPolygon(geom, v4, v7, v6, v5);
		addPolygon(geom, v0, v3, v7, v4);
		addPolygon(geom, v3, v2, v6, v7);
		addPolygon(geom, v0, v4, v5, v1);
		
		return geom;
	}
	
	public static Geometry createBadGeometry() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		
		Vertex v0 = createVertex(0, 0, 0, geom);
		Vertex v1 = createVertex(10, 0, 0, geom);
		Vertex v2 = createVertex(10, 10, 0, geom);
		Vertex v3 = createVertex(0, 10, 0, geom);
		Vertex v4 = createVertex(0, 0, 10, geom);
		Vertex v5 = createVertex(10, 0, 10, geom);
		Vertex v6 = createVertex(10, 10, 10, geom);
		Vertex v7 = createVertex(0, 10, 10, geom);
		Vertex v8 = createVertex(5, -5, 5, geom);
		
		addPolygon(geom, v3, v2, v1, v0);
		addPolygon(geom, v2, v6, v5, v1);
		addPolygon(geom, v5, v6, v7, v4);
		addPolygon(geom, v4, v7, v3, v0);
		addPolygon(geom, v1, v5, v4, v0);
		
		// create bad polygon
		addPolygon(geom, v8, v7, v6);
		addPolygon(geom, v8, v6, v2);
		addPolygon(geom, v8, v2, v3);
		addPolygon(geom, v8, v3, v7);
		
		return geom;
	}
	
	public static Polygon addPolygon(Geometry geom, Vertex... vertices) {
		Polygon poly;
		LinearRing ex;
		poly = new ConcretePolygon();
		geom.addPolygon(poly);
		ex = new LinearRing(LinearRingType.EXTERIOR);
		poly.setExteriorRing(ex);
		for (Vertex v : vertices) {
			ex.addVertex(v);
		}
		ex.addVertex(vertices[0]);
		return poly;
	}
	
	public static Vertex createVertex(double x, double y, double z, Geometry geom) {
		return new Vertex(x, y, z);
	}

}
