// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero 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 System.Collections.Generic; using Deltares.DamEngine.Data.Geometry; using NUnit.Framework; namespace Deltares.DamEngine.Data.Tests.Geometry; [TestFixture] public class GeometryCurveTests { [Test] [TestCase(true, true)] [TestCase(false, true)] [TestCase(true, false)] public void ContainsPoint_AnyPointNotSet_ReturnFalse(bool headPointNotSet, bool endPointNotSet) { // setup var point = new Point2D(); var curve = new GeometryCurve(headPointNotSet ? null : point, endPointNotSet ? null : point); Assert.That((curve.HeadPoint == null || curve.EndPoint == null), Is.EqualTo(true), "Test boundary case."); // call bool containsPoint = curve.ContainsPoint(new Point2D(point.X, point.Z), GeometryPoint.Precision); // assert Assert.That(containsPoint, Is.EqualTo(false)); } [Test] public void ContainsPoint_BothPointsSetAndPointIsNotOnLineInXZplane_ReturnFalse() { // setup var curve = new GeometryCurve(new Point2D(0.0, 0.0), new Point2D(10.0, 10.0)); var point = new Point2D(1.1, 6.7); // call bool containsPoint = curve.ContainsPoint(point, GeometryPoint.Precision); // assert Assert.That(containsPoint, Is.EqualTo(false)); } [Test] public void ContainsPoint_BothPointsSetAndPointIsOnLineInXZplane_ReturnTrue() { // setup var curve = new GeometryCurve(new Point2D(0.0, 0.0), new Point2D(10.0, 10.0)); var point = new Point2D(3.0, 3.0); // call bool containsPoint = curve.ContainsPoint(point, GeometryPoint.Precision); // assert Assert.That(containsPoint, Is.EqualTo(true)); } [Test] [TestCase(0.0, 0.0, 10.0, 10.0, true)] [TestCase(0.01, 0.0, 10.0, 10.0, false)] [TestCase(0.0, 0.01, 10.0, 10.0, false)] [TestCase(0.0, 0.0, 10.01, 10.0, false)] [TestCase(0.0, 0.0, 10.0, 10.01, false)] public void LocationEquals_ReturnsProperValue(double X1, double Z1, double X2, double Z2, bool expected) { // setup var baseCurve = new GeometryCurve(new Point2D(0.0, 0.0), new Point2D(10.0, 10.0)); var curve = new GeometryCurve(new Point2D(X1, Z1), new Point2D(X2, Z2)); // call bool locationEquals = curve.LocationEquals(baseCurve); curve.Reverse(); bool reversedLocationEquals = baseCurve.LocationEquals(curve); Assert.Multiple(() => { // assert Assert.That(locationEquals, Is.EqualTo(expected)); Assert.That(reversedLocationEquals, Is.EqualTo(expected)); }); } [Test] public void AssignSurfacesFromCurve_WorksAsExpected() { // setup var curve = new GeometryCurve(new Point2D(0.0, 0.0), new Point2D(10.0, 10.0)); var curveEmptySurface = new GeometryCurve(new Point2D(0.0, 0.0), new Point2D(10.0, 10.0)); var surfaceRight = new GeometrySurface { Name = "SurfaceRight" }; var surfaceLeft = new GeometrySurface { Name = "SurfaceLeft" }; curve.SurfaceAtLeft = surfaceLeft; curve.SurfaceAtRight = surfaceRight; // call curveEmptySurface.AssignSurfacesFromCurve(curve); curve.AssignSurfacesFromCurve(curveEmptySurface); Assert.Multiple(() => { // assert Assert.That(curveEmptySurface.SurfaceAtLeft.Name.Equals("SurfaceLeft")); Assert.That(curveEmptySurface.SurfaceAtRight.Name.Equals("SurfaceRight")); Assert.That(curve.SurfaceAtLeft.Name.Equals("SurfaceLeft")); Assert.That(curve.SurfaceAtRight.Name.Equals("SurfaceRight")); }); } public class GivenGeometryCurve { private static readonly GeometryCurve geometryCurve = CreateGeometryCurve(); private static readonly List points = ClonedPoints(geometryCurve); [TestFixture] public class WhenCloned : GivenGeometryCurve { private static readonly GeometryCurve clonedGeometryCurve = geometryCurve.Clone(points); [Test] public void ThenCopyOfCurveIsReturned() { Assert.That(clonedGeometryCurve, Is.Not.EqualTo(geometryCurve)); Assert.Multiple(() => { Assert.That(clonedGeometryCurve.Name, Is.EqualTo(geometryCurve.Name)); // points are copied Assert.That(clonedGeometryCurve.LocationEquals(geometryCurve), Is.True); Assert.That(clonedGeometryCurve.HeadPoint, Is.Not.EqualTo(geometryCurve.HeadPoint)); Assert.That(clonedGeometryCurve.EndPoint, Is.Not.EqualTo(geometryCurve.EndPoint)); // surfaces are references which are NOT to be cloned Assert.That(clonedGeometryCurve.SurfaceAtLeft, Is.EqualTo(null)); Assert.That(clonedGeometryCurve.SurfaceAtRight, Is.EqualTo(null)); }); } } [TestFixture] public class WithoutSurfacesWhenCloned : GivenGeometryCurve { [SetUp] public void Setup() { geometryCurve.SurfaceAtLeft = null; geometryCurve.SurfaceAtRight = null; } private static readonly GeometryCurve clonedGeometryCurve = geometryCurve.Clone(points); [Test] public void ThenCopyOfCurveIsReturned_WithoutExceptions() { Assert.Multiple(() => { Assert.That(clonedGeometryCurve, Is.Not.EqualTo(geometryCurve)); Assert.That(clonedGeometryCurve.Name, Is.EqualTo(geometryCurve.Name)); Assert.That(clonedGeometryCurve.LocationEquals(geometryCurve), Is.True); Assert.That(clonedGeometryCurve.HeadPoint, Is.Not.EqualTo(geometryCurve.HeadPoint)); Assert.That(clonedGeometryCurve.EndPoint, Is.Not.EqualTo(geometryCurve.EndPoint)); Assert.That(clonedGeometryCurve.SurfaceAtLeft, Is.Null); Assert.That(clonedGeometryCurve.SurfaceAtRight, Is.Null); }); } } private static List ClonedPoints(GeometryCurve curve) { var points = new List { curve.HeadPoint.Clone(), curve.EndPoint.Clone() }; return points; } private static GeometryCurve CreateGeometryCurve() { GeometryLoop loopLeft = CreateLoop(0.0); GeometryLoop loopRight = CreateLoop(10.0); loopRight.CurveList.Add(new GeometryCurve(new Point2D(10.1, 10.2), new Point2D(10.3, 10.4))); var curve = new GeometryCurve(new Point2D(1.0, 2.0), new Point2D(10.0, 20.0)) { SurfaceAtLeft = new GeometrySurface(loopLeft), SurfaceAtRight = new GeometrySurface(loopRight), Name = "Geometry Curve 1" }; return curve; } private static GeometryLoop CreateLoop(double x) { var loop = new GeometryLoop(); var point1 = new Point2D(0.0 + x, 0.2); var point2 = new Point2D(1.0 + x, 0.2); var point3 = new Point2D(1.0 + x, 0.4); loop.CurveList.Add(new GeometryCurve(point1, point2)); loop.CurveList.Add(new GeometryCurve(point2, point3)); loop.CurveList.Add(new GeometryCurve(point3, point1)); return loop; } } }