// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using Deltares.Geometry;
namespace Deltares.Dam.Tests
{
using System;
using NUnit.Framework;
using Deltares.Dam.Data;
[TestFixture]
public class PolyLineTest
{
[TestFixtureSetUp]
public void FixtureSetup()
{
}
[Test]
public void LineWithoutPointsShouldNotExist()
{
var polyLine = new PolyLine();
Assert.IsFalse(polyLine.Exists());
}
[Test]
public void LineWithOnePointShouldNotExist()
{
var polyLine = new PolyLine();
polyLine.Points.Add(new GeometryPoint());
Assert.IsFalse(polyLine.Exists());
}
[Test]
public void LineWithTwoPointsShouldExist()
{
var polyLine = new PolyLine();
polyLine.Points.Add(new GeometryPoint());
polyLine.Points.Add(new GeometryPoint() { X = 1.0 });
Assert.IsTrue(polyLine.Exists());
}
[Test]
public void CheckIfLineWithAscendingXIsEvaluatedCorrectlyAsAscending()
{
var polyLine = CreateStrictAscendingPolyLine();
Assert.IsTrue(polyLine.IsXAscending());
}
[Test]
public void CheckIfLineWithNotStrictAscendingXIsEvaluatedCorrectlyAsAscending()
{
var polyLine = CreateNotStrictAscendingPolyLine();
Assert.IsTrue(polyLine.IsXAscending());
}
[Test]
public void CheckIfLineWithNotStrictAscendingXIsEvaluatedCorrectlyAsNotStrictAscending()
{
var polyLine = CreateNotStrictAscendingPolyLine();
Assert.IsFalse(polyLine.IsXStrictAscending());
}
[Test]
public void CheckIfLineWithNotAscendingXIsEvaluatedCorrectlyAsNotAscending()
{
var polyLine = CreateNotAscendingPolyLine();
Assert.IsFalse(polyLine.IsXAscending());
}
[Test]
[ExpectedException(typeof(PolyLineException))]
public void ThrowsExceptionOnNotStrictAscendingXPolylineYFromXCalculation()
{
var polyLine = CreateNotAscendingPolyLine();
double Y = polyLine.YFromX(1.0);
}
[Test]
public void CheckIfYFromXEvaluatesCorrectly()
{
const double cTolerance = 0.0001;
var polyLine = CreateStrictAscendingPolyLine();
Assert.AreEqual(5.0, polyLine.YFromX(1.0), cTolerance);
Assert.AreEqual(9.0, polyLine.YFromX(4.0), cTolerance);
}
[Test]
public void CheckIfYFromXEvaluatesCorrectlyWithOutOfBoundsXValues()
{
const double cTolerance = 0.0001;
var polyLine = CreateStrictAscendingPolyLine();
Assert.AreEqual(0.0, polyLine.YFromX(-3.0), cTolerance);
Assert.AreEqual(7.0, polyLine.YFromX(10.0), cTolerance);
}
///
/// If vertical line segment is present (i.e. two points have same X-coord.) then the Y value form the first point will be returned
///
[Test]
public void CheckIfYFromXEvaluatesCorrectlyWithNotStrictAscendingPolyLine()
{
const double cTolerance = 0.0001;
var polyLine = CreateNotStrictAscendingPolyLine();
Assert.AreEqual(5.0, polyLine.YFromX(1.0), cTolerance);
Assert.AreEqual(10.0, polyLine.YFromX(3.0), cTolerance);
}
///
/// Check if Equals works with unequal lines
///
[Test]
public void EqualsReturnsFalseWithUnequalLines()
{
var polyLine1 = CreateStrictAscendingPolyLine();
var polyLine2 = CreateNotStrictAscendingPolyLine();
Assert.IsFalse(polyLine1.Equals(polyLine2));
}
///
/// Check if considing points are deleted
///
[Test]
public void CheckIfDeleteCoinsidingPointsReturnsLessPoints()
{
var polyLine1 = CreateStrictAscendingPolyLine();
polyLine1.Points.Add(polyLine1.Points[0]);
int currentLength = polyLine1.Points.Count;
polyLine1.DeleteCoinsidingPoints();
Assert.AreEqual(currentLength - 1, polyLine1.Points.Count);
}
///
/// Check if Equals works with equal lines
///
[Test]
public void EqualsReturnsTrueWithEqualLines()
{
var polyLine1 = CreateStrictAscendingPolyLine();
var polyLine2 = CreateStrictAscendingPolyLine();
Assert.IsTrue(polyLine1.Equals(polyLine2));
}
///
/// Check if Equals works with equal lines
///
[Test]
public void TestOnePointIntersectionPointsXzWithLineXz()
{
var polyLine1 = CreateIntersectPolyLine();
var line = new Line { BeginPoint = new GeometryPoint(-5, 0, -1), EndPoint = new GeometryPoint(5, 0, -1) };
var intersectionPoints = polyLine1.IntersectionPointsXzWithLineXz(line);
Assert.IsTrue(intersectionPoints.Count == 1);
Assert.AreEqual(intersectionPoints[0].X, 1.0, GeometryPoint.Precision);
Assert.AreEqual(intersectionPoints[0].Z, -1.0, GeometryPoint.Precision);
}
[Test]
public void TestNoPointIntersectionPointsXzWithLineXz()
{
var polyLine1 = CreateIntersectPolyLine();
var line = new Line { BeginPoint = new GeometryPoint(-5, 0, -1), EndPoint = new GeometryPoint(-3, 0, -1) };
var intersectionPoints = polyLine1.IntersectionPointsXzWithLineXz(line);
Assert.IsTrue(intersectionPoints.Count == 0);
}
[Test]
public void TestTwoPointIntersectionPointsXzWithLineXz()
{
var polyLine1 = CreateIntersectPolyLine();
var line = new Line { BeginPoint = new GeometryPoint(-5, 0, -1), EndPoint = new GeometryPoint(15, 0, -1) };
var intersectionPoints = polyLine1.IntersectionPointsXzWithLineXz(line);
Assert.IsTrue(intersectionPoints.Count == 2);
Assert.AreEqual(intersectionPoints[0].X, 1.0, GeometryPoint.Precision);
Assert.AreEqual(intersectionPoints[0].Z, -1.0, GeometryPoint.Precision);
Assert.AreEqual(intersectionPoints[1].X, 11.0, GeometryPoint.Precision);
Assert.AreEqual(intersectionPoints[1].Z, -1.0, GeometryPoint.Precision);
}
private PolyLine CreateStrictAscendingPolyLine()
{
var polyLine = new PolyLine();
polyLine.Points.Add(new GeometryPoint() { X = -1.0});
polyLine.Points.Add(new GeometryPoint() { X = 3.0, Y = 10.0});
polyLine.Points.Add(new GeometryPoint() { X = 6.0, Y = 7.0});
return polyLine;
}
private PolyLine CreateNotAscendingPolyLine()
{
var polyLine = new PolyLine();
polyLine.Points.Add(GeometryPoint.CreateNewXYPoint(0.0, 0.0));
polyLine.Points.Add(GeometryPoint.CreateNewXYPoint(3.0, 10.0));
polyLine.Points.Add(GeometryPoint.CreateNewXYPoint(1.0, 20.0));
return polyLine;
}
private PolyLine CreateIntersectPolyLine()
{
var polyLine = new PolyLine();
polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(-10.0, 0.0));
polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(0.0, 0.0));
polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(2.0, -2.0));
polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(10.0, -2.0));
polyLine.Points.Add(GeometryPoint.CreateNewXZPoint(12.0, 0.0));
return polyLine;
}
private PolyLine CreateNotStrictAscendingPolyLine()
{
var polyLine = new PolyLine();
polyLine.Points.Add(new GeometryPoint() { X = -1.0 });
polyLine.Points.Add(new GeometryPoint() { X = 3.0, Y = 10.0 });
polyLine.Points.Add(new GeometryPoint() { X = 3.0, Y = 6.0 });
polyLine.Points.Add(new GeometryPoint() { X = 6.0, Y = 7.0 });
return polyLine;
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
}
}
}