using System;
using System.IO;
using System.Linq;
using Deltares.DeltaModel;
using Deltares.Geometry;
using Deltares.Geotechnics.GeotechnicalGeometry;
using Deltares.Geotechnics.IO.Importers;
using Deltares.Standard.Extensions;
using Deltares.Standard.Forms;
using Deltares.Standard.IO;
using NUnit.Framework;
namespace Deltares.Geotechnics.IO.Tests
{
[TestFixture]
public class CharacteristicPointsImporterTest
{
///
/// Check if Import() returns the correct characteristic points.
///
[Test]
public void ImportReturnsCorrectCharacteristicPointsWhenReadingSurfaceLinesAfterwards()
{
var deltaModel = new DeltaModel.DeltaModel();
var deltaModelFactory = new DeltaModelFactory(deltaModel);
var multiImporter = new MultiImporter();
multiImporter.DataFactory = deltaModelFactory;
var fileName = Path.Combine(mapTestData, surfaceLinesFilename);
Assert.IsTrue(File.Exists(fileName));
multiImporter.Importers.Add(new SurfaceLinesImporter
{
FileName = fileName, Factory = deltaModelFactory
});
fileName = Path.Combine(mapTestData, characteristicPointsFilename);
Assert.IsTrue(File.Exists(fileName));
multiImporter.Importers.Add(new CharacteristicPointsImporter
{
FileName = fileName,
Factory = deltaModelFactory
});
multiImporter.ImportAll(null);
Assert.AreEqual(32, deltaModel.SurfaceLines.Count, "Read incorrect number of surface lines in import of characteristic points");
SurfaceLine2 surfaceLine = deltaModel.SchematizedSurfaceLines.FirstOrDefault(x => x.Name.Equals("dijkring10_dwp36_9"));
Assert.IsNotNull(surfaceLine);
Assert.AreEqual(31, surfaceLine.Geometry.Count, "Incorrect number of points in surface line");
Assert.AreEqual(0.0, surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelOutside).X, tolerance);
Assert.AreEqual(1.06, surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelOutside).Z, tolerance);
Assert.AreEqual(100.0, surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelInside).X, tolerance);
Assert.AreEqual(-0.61, surfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelInside).Z, tolerance);
}
///
/// Check if Import() returns the correct characteristic points.
///
[Test]
public void ImportReturnsCorrectCharacteristicPointsWhenReadingSurfaceLinesFirst()
{
var deltaModel = new DeltaModel.DeltaModel();
var deltaModelFactory = new DeltaModelFactory(deltaModel);
var multiImporter = new MultiImporter();
multiImporter.DataFactory = deltaModelFactory;
var fileName = Path.Combine(mapTestData, surfaceLinesFilename);
Assert.IsTrue(File.Exists(fileName));
multiImporter.Importers.Add(new SurfaceLinesImporter
{
FileName = fileName, Factory = deltaModelFactory
});
fileName = Path.Combine(mapTestData, characteristicPointsFilename);
Assert.IsTrue(File.Exists(fileName));
multiImporter.Importers.Add(new CharacteristicPointsImporter
{
FileName = fileName, Factory = deltaModelFactory
});
multiImporter.ImportAll(null);
Assert.AreEqual(32, deltaModel.SurfaceLines.Count, "Read incorrect number of surface lines in import of characteristic points");
SurfaceLine2 schematizedSurfaceLine = deltaModel.SchematizedSurfaceLines.FirstOrDefault(x => x.Name.Equals("dijkring10_dwp36_9"));
Assert.IsNotNull(schematizedSurfaceLine);
Assert.AreEqual(31, schematizedSurfaceLine.Geometry.Count, "Incorrect number of points in surface line");
Assert.AreEqual(0.0, schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelOutside).X, tolerance);
Assert.AreEqual(1.06, schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelOutside).Z, tolerance);
Assert.AreEqual(100.0, schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelInside).X, tolerance);
Assert.AreEqual(-0.61, schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelInside).Z, tolerance);
}
///
/// Check if Import() returns the correct characteristic points.
///
[Test]
[TestCase(characteristicPointsFilename)] // Ringtoets format
[TestCase("characteristicpointsDAM.csv")] // Dam format
public void ImportReturnsCorrectCharacteristicPointsWithoutReadingSurfaceLines(string fileName)
{
var deltaModel = new DeltaModel.DeltaModel();
var testDeltaModelFactory = new TestDeltaModelFactory(deltaModel,false);
var fullName = Path.Combine(mapTestData, fileName);
var characteristicPointsImporter = new CharacteristicPointsImporter
{
FileName = fullName, Factory = testDeltaModelFactory
};
characteristicPointsImporter.Import();
Assert.AreEqual(32, deltaModel.SchematizedSurfaceLines.Count, "Read incorrect number of surface lines in import of characteristic points");
var schematizedSurfaceLine = deltaModel.SchematizedSurfaceLines.FirstOrDefault(x => x.Name.Equals("dijkring10_dwp36_9"));
Assert.IsNotNull(schematizedSurfaceLine);
Assert.IsEmpty(schematizedSurfaceLine.Geometry.Points);
Assert.AreEqual(0.0, schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelOutside).X, tolerance);
Assert.IsNaN(schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelOutside).Z,
"No geometry was imported, therefore no height can be set.");
Assert.AreEqual(100.0, schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelInside).X, tolerance);
Assert.IsNaN(schematizedSurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.SurfaceLevelInside).Z,
"No geometry was imported, therefore no height can be set.");
}
[Test]
public void Import_RD_coordinates_on_LocalizedGeometryPointString()
{
var name = "haha";
var localizedGeometryPointString = new LocalizedGeometryPointString
{
Name = name
};
var schematizedSurfaceLine = new SurfaceLine2
{
CharacteristicPoints =
{
GeometryMustContainPoint = false
},
Geometry = localizedGeometryPointString,
Name = name
};
localizedGeometryPointString.Points.AddRange(new[]
{
new GeometryPoint(104, 102, 4), new GeometryPoint(100, 100, 0)
});
localizedGeometryPointString.ConvertGlobalToLocalXZSurfaceLine();
//check localisation
Assert.AreEqual(100, localizedGeometryPointString.OriginalXy.X);
Assert.AreEqual(0, localizedGeometryPointString.Points.First().X);
Assert.AreEqual(Math.Sqrt(Math.Pow(4, 2) + Math.Pow(2, 2)), localizedGeometryPointString.Points.Last().X, 0.00001);
//Import characteristic point
var characteristicPointsImporterMock = new CharacteristicPointsImporterMock();
characteristicPointsImporterMock.AddCharacteristicPointToSurfaceLine(schematizedSurfaceLine, 102, 101, 2, CharacteristicPointType.DikeToeAtRiver, false);
Assert.AreEqual(1, schematizedSurfaceLine.CharacteristicPoints.Count);
Assert.AreEqual(Math.Sqrt(Math.Pow(2, 2) + Math.Pow(1, 2)), schematizedSurfaceLine.CharacteristicPoints.First().X, 0.00001);
Assert.AreEqual(0, schematizedSurfaceLine.CharacteristicPoints.First().Y);
Assert.AreEqual(2, schematizedSurfaceLine.CharacteristicPoints.First().Z);
}
private const string mapTestData = @"..\..\Geotechnics\Tests\TestData\CSVImportTest";
private const string characteristicPointsFilename = "characteristicpoints.csv";
private const string surfaceLinesFilename = "surfacelines.csv";
private const double tolerance = 0.0000001;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
Messenger.RerouteMessageBoxMessages = true;
}
public class CharacteristicPointsImporterMock : CharacteristicPointsImporter
{
public new bool AddCharacteristicPointToSurfaceLine(SurfaceLine2 surfaceLine, double xGlobal, double yGlobal, double zGlobal, CharacteristicPointType type, bool isAddPointToSurfaceLineIfMissing)
{
return base.AddCharacteristicPointToSurfaceLine(surfaceLine, xGlobal, yGlobal, zGlobal, type, isAddPointToSurfaceLineIfMissing);
}
}
}
}