/*-
* 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 .
*/
package de.hft.stuttgart.citydoctor2.checks.geometry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import de.hft.stuttgart.citydoctor2.check.Check;
import de.hft.stuttgart.citydoctor2.check.CheckError;
import de.hft.stuttgart.citydoctor2.check.CheckId;
import de.hft.stuttgart.citydoctor2.check.CheckResult;
import de.hft.stuttgart.citydoctor2.check.CheckType;
import de.hft.stuttgart.citydoctor2.check.Checkable;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
import de.hft.stuttgart.citydoctor2.check.error.RingTooFewPointsError;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
/**
* CP_NUMPOINTS checks the minimum number of point in a linear ring. At least
* three vertices are minimum for a linear ring.
*
* Dependency:
*
* none
*
* @author Matthias Betz - 12bema1bif@hft-stuttgart.de
*/
public class NumPointsCheck extends Check {
private static final List> applicableToClasses;
private static final List dependencies;
static {
ArrayList> classes = new ArrayList<>();
classes.add(LinearRing.class);
applicableToClasses = Collections.unmodifiableList(classes);
ArrayList temp = new ArrayList<>();
temp.add(CheckId.C_GE_R_NOT_CLOSED);
dependencies = Collections.unmodifiableList(temp);
}
public NumPointsCheck() {
super(CheckId.C_GE_R_TOO_FEW_POINTS);
}
/**
* checks if every linear ring has at least 3 distinct points
*/
@Override
public void check(LinearRing lr) {
// put all vertices into a set, ignoring duplicates
Set vertices = new HashSet<>(lr.getVertices().size());
// if the ring has two vertices which are neighbors (can be the same point)
// count them, as they don't count as a distinct point
int numberOfContainingNeighbors = 0;
for (Vertex v : lr.getVertices()) {
boolean added = vertices.add(v);
if (!added) {
for (Vertex neighbor : v.getNeighbors()) {
if (vertices.contains(neighbor)) {
numberOfContainingNeighbors++;
}
}
}
}
// 2 points or less = always error
if (vertices.size() - numberOfContainingNeighbors < 3) {
CheckError err = new RingTooFewPointsError(lr);
CheckResult cr = new CheckResult(this, ResultStatus.ERROR, err);
lr.addCheckResult(cr);
return;
}
// rest is okay
CheckResult cr = new CheckResult(this, ResultStatus.OK, null);
lr.addCheckResult(cr);
}
@Override
public List> getApplicableToClasses() {
return applicableToClasses;
}
@Override
public List getDependencies() {
return dependencies;
}
@Override
public CheckType getType() {
return CheckType.GEOMETRY;
}
@Override
public Check createNewInstance() {
return new NumPointsCheck();
}
}