Index: DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryObjectTests.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryObjectTests.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryObjectTests.cs (revision 5064) @@ -0,0 +1,97 @@ +// 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 Deltares.DamEngine.Data.Geometry; +using NUnit.Framework; + +namespace Deltares.DamEngine.Data.Tests.Geometry; + +public class GeometryObjectTests +{ + [Test] + public void GetGeometryBounds_Always_ReturnNull() + { + // setup + var geometry = new GeometryObjectTestExerciser(); + + // call + var returnedBounds = geometry.GetGeometryBounds(); + + // assert + Assert.That(returnedBounds, Is.Null); + } + + [Test] + [TestCase(null)] + [TestCase("hihi")] + public void Name_SetToEmptyString_NameNotChanged(string newName) + { + // setup + var geometry = new GeometryObjectTestExerciser(); + geometry.SetName("haha"); + + // call + geometry.Name = newName; + + // assert + if (newName == null) + Assert.That("haha".Equals(geometry.Name)); + else + Assert.That(newName.Equals(geometry.Name)); + } + + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase("haha")] + public void SetName_AnyValue_NameIsSet(string expectedName) + { + // setup + var geometry = new GeometryObjectTestExerciser(); + + // call + geometry.SetName(expectedName); + + // assert + if (expectedName == null) + Assert.That(geometry.Name == null); + else + Assert.That(expectedName.Equals(geometry.Name)); + } + + [Test] + [TestCase(0.0000455454, 0.000)] + [TestCase(0.004455454, 0.004)] + [TestCase(0.004555454, 0.005)] + public void TestRoundValue(double value, double expectedValue) + { + // setup + var geometry = new GeometryObjectTestExerciser(); + + // call + double resultValue = geometry.RoundValue(value); + + // assert + Assert.That(resultValue, Is.EqualTo(expectedValue)); + } + + private class GeometryObjectTestExerciser : GeometryObject {} +} \ No newline at end of file Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryObject.cs =================================================================== diff -u -r4540 -r5064 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryObject.cs (.../GeometryObject.cs) (revision 4540) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryObject.cs (.../GeometryObject.cs) (revision 5064) @@ -48,7 +48,8 @@ } set { - name = value; + if (value != null) + name = value; } } @@ -82,4 +83,11 @@ { return Name; } + + /// + /// Rounds given value to geometry accuracy + /// + /// + /// + public double RoundValue(double originalValue) => Math.Round(originalValue / GeometryConstants.Accuracy) * GeometryConstants.Accuracy; } \ No newline at end of file Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryData.cs =================================================================== diff -u -r5057 -r5064 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryData.cs (.../GeometryData.cs) (revision 5057) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/GeometryData.cs (.../GeometryData.cs) (revision 5064) @@ -1000,137 +1000,8 @@ return curvePointDependency; } - + /// - /// CheckIfIntersectStricktly - /// Determines if two lines intersect each other stricktly (so no extrapolated points). - /// - /// Line 1 Point2D 1 - /// Line 1 Point2D 2 - /// Line 2 Point2D 1 - /// Line 2 Point2D 2 - /// Intersection coordinates - /// True if lines intersect each other - private bool CheckIfIntersectStricktly(Point2D beginPoint1, Point2D endPoint1, - Point2D beginPoint2, Point2D endPoint2, - ref Point2D intersect) - { - Point2D ip; - LineIntersection res = Routines2D.DetermineIf2DLinesIntersectStrickly(beginPoint1, endPoint1, - beginPoint2, endPoint2, out ip); - if (ip != null) - { - intersect = ip; - } - - return res == LineIntersection.Intersects; - } - - /// - /// CheckIfIntersect - /// Determines if two lines intersect each other stricktly (so no extrapolated points). - /// - /// Line 1 Point2D 1 - /// Line 1 Point2D 2 - /// Line 2 Point2D 1 - /// Line 2 Point2D 2 - /// Intersection coordinates - /// True if lines intersect each other - public bool CheckIfIntersect(double[] aL1P1, double[] aL1P2, double[] aL2P1, double[] aL2P2, - ref double[] aIntersect) - { - var p1 = new Point2D(aL1P1[0], aL1P1[1]); - var p2 = new Point2D(aL1P2[0], aL1P2[1]); - var p3 = new Point2D(aL2P1[0], aL2P1[1]); - var p4 = new Point2D(aL2P2[0], aL2P2[1]); - var ip = new Point2D(); - bool res = CheckIfIntersectStricktly(p1, p2, p3, p4, ref ip); - if (res) - { - aIntersect[0] = ip.X; - aIntersect[1] = ip.Z; - } - - return res; - } - - /// - /// Gets the height of the surface(s) intersected at the given x. - /// - /// The x. - /// - public double GetSurfaceHeight(double x) - { - double surfaceHeight = -Double.MaxValue; - double[] intersectionPoints = IntersectLayers(x, -9999); - for (var i = 0; i < intersectionPoints.Length; i++) - { - if (intersectionPoints[i] > surfaceHeight) - { - surfaceHeight = intersectionPoints[i]; - } - } - - return surfaceHeight; - } - - /// - /// All the Intersection of layers in respect with a given vertical are detemined here. - /// - /// Startingpoint of the Vertical (X) - /// Startingpoint of the Vertical (Y) - /// List of Z intersection coordinates - private double[] IntersectLayers(double aXCoord, double aZCoord) - { - if (Surfaces == null) - { - throw new Exception("Empty Surfaces in IntersectLayers"); - } - - var beginPoint2 = new Point2D - { - X = aXCoord, - Z = aZCoord - }; - - var endPoint2 = new Point2D - { - X = aXCoord, - Z = 99999 - }; - - var referencePoint = new Point2D(); - - var intersections = new List(); - - for (var surfaceIndexLocal = 0; surfaceIndexLocal < Surfaces.Count; surfaceIndexLocal++) - { - List outerLoopCurveList = Surfaces[surfaceIndexLocal].OuterLoop.CurveList; - for (var curveIndexLocal = 0; curveIndexLocal < outerLoopCurveList.Count; curveIndexLocal++) - { - //Check for each curve if it intersects with x coordinate - Point2D beginPoint1 = outerLoopCurveList[curveIndexLocal].GetHeadPoint(CurveDirection.Forward); - Point2D endPoint1 = outerLoopCurveList[curveIndexLocal].GetEndPoint(CurveDirection.Forward); - - if (Math.Max(beginPoint1.X, endPoint1.X) >= aXCoord && - Math.Min(beginPoint1.X, endPoint1.X) <= aXCoord) - { - if (CheckIfIntersectStricktly(beginPoint1, endPoint1, beginPoint2, endPoint2, - ref referencePoint)) - { - if (referencePoint.Z > aZCoord && !intersections.Contains(referencePoint.Z)) - { - intersections.Add(referencePoint.Z); - } - } - } - } - } - - return intersections.ToArray(); - } - - /// /// Returns a list of boundary curves. These are curves which are used in only one surface so they have to be on a boundary (inner or outer) /// /// Index: DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryCurveTests.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryCurveTests.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Geometry/GeometryCurveTests.cs (revision 5064) @@ -0,0 +1,127 @@ +// 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; +using Deltares.DamEngine.Data.Geometry; +using NUnit.Framework; + +namespace Deltares.DamEngine.Data.Tests.Geometry; + +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).Equals(true), + "Test boundary case."); + + // call + var containsPoint = curve.ContainsPoint(new Point2D(point.X, point.Z), GeometryPoint.Precision); + + // assert + Assert.That(containsPoint.Equals(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 + var containsPoint = curve.ContainsPoint(point, GeometryPoint.Precision); + + // assert + Assert.That(containsPoint.Equals(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 + var containsPoint = curve.ContainsPoint(point, GeometryPoint.Precision); + + // assert + Assert.That(containsPoint.Equals(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 + var locationEquals = curve.LocationEquals(baseCurve); + curve.Reverse(); + var reversedLocationEquals = baseCurve.LocationEquals(curve); + + // assert + Assert.That(locationEquals.Equals(expected)); + Assert.That(reversedLocationEquals.Equals(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(); + surfaceRight.Name = "SurfaceRight"; + var surfaceLeft = new GeometrySurface(); + surfaceLeft.Name = "SurfaceLeft"; + curve.SurfaceAtLeft = surfaceLeft; + curve.SurfaceAtRight = surfaceRight; + + // call + curveEmptySurface.AssignSurfacesFromCurve(curve); + curve.AssignSurfacesFromCurve(curveEmptySurface); + + // 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")); + } +} \ No newline at end of file