Skip to content

Commit

Permalink
Release v.0.1.
Browse files Browse the repository at this point in the history
Test Coverage 99%
  • Loading branch information
vangogih committed Apr 14, 2019
1 parent 3eb9cab commit cfddd09
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 168 deletions.
4 changes: 2 additions & 2 deletions SCPT/CalculateParameters/CalculateParameters.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
<ItemGroup>
<Compile Include="NewtonIterationProcess.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SystemCoordinate.cs" />
<Compile Include="Coordinates.cs" />
<Compile Include="Tests\NewtonIterationProcess\NewtonIterarionProcessTests.cs" />
<Compile Include="Tests\NewtonIterationProcess\SystemCoordinateTests.cs" />
<Compile Include="Tests\NewtonIterationProcess\CoordinatesTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
34 changes: 34 additions & 0 deletions SCPT/CalculateParameters/Coordinates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace CalculateParameters
{
public class Coordinates
{
public double X { get; }
public double Y { get; }
public double Z { get; }

public Coordinates(double x, double y, double z)
{
if (double.IsNaN(x))
throw new ArgumentException("x coordinate cannot be NaN");
if (double.IsNaN(y))
throw new ArgumentException("y coordinate cannot be NaN");
if (double.IsNaN(z))
throw new ArgumentException("z coordinate cannot be NaN");
if (double.IsInfinity(x))
throw new ArithmeticException("x coordinate attained infinity",
new ArgumentOutOfRangeException());
if (double.IsInfinity(y))
throw new ArithmeticException("y coordinate attained infinity",
new ArgumentOutOfRangeException());
if (double.IsInfinity(z))
throw new ArithmeticException("z coordinate attained infinity",
new ArgumentOutOfRangeException());

X = x;
Y = y;
Z = z;
}
}
}
81 changes: 34 additions & 47 deletions SCPT/CalculateParameters/NewtonIterationProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@

namespace CalculateParameters
{
public class NewtonIterationProcess : ITransform
public class NewtonIterationProcess
{
private const int MinListCount = 3;

private readonly List<SystemCoordinate> _sourceSystemCoordinates;
private readonly List<SystemCoordinate> _destinationSystemCoordinates;
public List<Coordinates> SourceSystemCoordinates { get; }
public List<Coordinates> DestinationSystemCoordinates { get; }

public double Wx { get; set; }
public double Wy { get; set; }
public double Wz { get; set; }
public double M { get; set; }
public Matrix<double> TransformParametersMatrix { get; private set; }
public Matrix<double> MeanSquareErrorsMatrix { get; private set; }

public NewtonIterationProcess(List<SystemCoordinate> source, List<SystemCoordinate> destination)
public NewtonIterationProcess(List<Coordinates> source, List<Coordinates> destination)
{
if (source == null)
throw new NullReferenceException("source list cannot be null");
Expand All @@ -32,13 +30,8 @@ public NewtonIterationProcess(List<SystemCoordinate> source, List<SystemCoordina
if (source.Count != destination.Count)
throw new ArgumentException("source list and destination list must be of the same length");

_sourceSystemCoordinates = source;
_destinationSystemCoordinates = destination;

Wx = 0;
Wy = 0;
Wz = 0;
M = 0;
SourceSystemCoordinates = source;
DestinationSystemCoordinates = destination;
}

public void CalculateTransformationParameters()
Expand All @@ -51,9 +44,12 @@ public void CalculateTransformationParameters()
var mCoefficient = CalculateMCoefficient(fCoefficient);
var qMatrix = GetQMatrix(aMatrix);
var mceMatrix = GetMeanSquareErrorsMatrix(qMatrix, mCoefficient);

TransformParametersMatrix = paramsTransformMatrix;
MeanSquareErrorsMatrix = mceMatrix;
}

private Matrix<double> FormingCoordinateMatrix(List<SystemCoordinate> list)
private Matrix<double> FormingCoordinateMatrix(List<Coordinates> list)
{
var vectorMatrix = Matrix.Create<double>(list.Count, 3); // matrix [nx3]
for (int i = 0; i < list.Count; i++)
Expand All @@ -68,54 +64,54 @@ private Matrix<double> FormingCoordinateMatrix(List<SystemCoordinate> list)

private Matrix<double> FormingAMatrix()
{
var aMatrix = Matrix.Create<double>(_sourceSystemCoordinates.Count * 3, 7);
var aMatrix = Matrix.Create<double>(SourceSystemCoordinates.Count * 3, 7);

for (int matrixIndex = 0, listIndex = 0;
matrixIndex < _sourceSystemCoordinates.Count * 3 || listIndex < _sourceSystemCoordinates.Count;
matrixIndex < SourceSystemCoordinates.Count * 3 || listIndex < SourceSystemCoordinates.Count;
matrixIndex += 3, listIndex++)
{
aMatrix[matrixIndex, 0] = 1;
aMatrix[matrixIndex, 1] = 0;
aMatrix[matrixIndex, 2] = 0;
aMatrix[matrixIndex, 3] = 0;
aMatrix[matrixIndex, 4] = -_sourceSystemCoordinates[listIndex].Z;
aMatrix[matrixIndex, 5] = _sourceSystemCoordinates[listIndex].Y;
aMatrix[matrixIndex, 6] = _sourceSystemCoordinates[listIndex].X;
aMatrix[matrixIndex, 4] = -SourceSystemCoordinates[listIndex].Z;
aMatrix[matrixIndex, 5] = SourceSystemCoordinates[listIndex].Y;
aMatrix[matrixIndex, 6] = SourceSystemCoordinates[listIndex].X;

aMatrix[matrixIndex + 1, 0] = 0;
aMatrix[matrixIndex + 1, 1] = 1;
aMatrix[matrixIndex + 1, 2] = 0;
aMatrix[matrixIndex + 1, 3] = _sourceSystemCoordinates[listIndex].Z;
aMatrix[matrixIndex + 1, 3] = SourceSystemCoordinates[listIndex].Z;
aMatrix[matrixIndex + 1, 4] = 0;
aMatrix[matrixIndex + 1, 5] = -_sourceSystemCoordinates[listIndex].X;
aMatrix[matrixIndex + 1, 6] = _sourceSystemCoordinates[listIndex].Y;
aMatrix[matrixIndex + 1, 5] = -SourceSystemCoordinates[listIndex].X;
aMatrix[matrixIndex + 1, 6] = SourceSystemCoordinates[listIndex].Y;

aMatrix[matrixIndex + 2, 0] = 0;
aMatrix[matrixIndex + 2, 1] = 0;
aMatrix[matrixIndex + 2, 2] = 1;
aMatrix[matrixIndex + 2, 3] = -_sourceSystemCoordinates[listIndex].Y;
aMatrix[matrixIndex + 2, 4] = _sourceSystemCoordinates[listIndex].X;
aMatrix[matrixIndex + 2, 3] = -SourceSystemCoordinates[listIndex].Y;
aMatrix[matrixIndex + 2, 4] = SourceSystemCoordinates[listIndex].X;
aMatrix[matrixIndex + 2, 5] = 0;
aMatrix[matrixIndex + 2, 6] = _sourceSystemCoordinates[listIndex].Z;
aMatrix[matrixIndex + 2, 6] = SourceSystemCoordinates[listIndex].Z;
}

return aMatrix;
}

private Matrix<double> FormingYMatrix()
{
var yMatrix = Matrix.Create<double>(_sourceSystemCoordinates.Count * 3, 1);
var yMatrix = Matrix.Create<double>(SourceSystemCoordinates.Count * 3, 1);

for (int matrixIndex = 0, listIndex = 0;
matrixIndex < _sourceSystemCoordinates.Count * 3 && listIndex < _sourceSystemCoordinates.Count;
matrixIndex < SourceSystemCoordinates.Count * 3 && listIndex < SourceSystemCoordinates.Count;
matrixIndex += 3, listIndex++)
{
yMatrix[matrixIndex, 0] =
_destinationSystemCoordinates[listIndex].X - _sourceSystemCoordinates[listIndex].X;
DestinationSystemCoordinates[listIndex].X - SourceSystemCoordinates[listIndex].X;
yMatrix[matrixIndex + 1, 0] =
_destinationSystemCoordinates[listIndex].Y - _sourceSystemCoordinates[listIndex].Y;
DestinationSystemCoordinates[listIndex].Y - SourceSystemCoordinates[listIndex].Y;
yMatrix[matrixIndex + 2, 0] =
_destinationSystemCoordinates[listIndex].Z - _sourceSystemCoordinates[listIndex].Z;
DestinationSystemCoordinates[listIndex].Z - SourceSystemCoordinates[listIndex].Z;
}

return yMatrix;
Expand Down Expand Up @@ -155,7 +151,7 @@ private bool IsSubtractMatrixValuesLessWhenDelta(Matrix<double> first, Matrix<do
private Matrix<double> GetVMatrix(Matrix<double> aMatrix, Matrix<double> yMatrix,
Matrix<double> vecParamsMatrix)
{
var vMatrix = Matrix.Create<double>(_sourceSystemCoordinates.Count * 3, 1);
var vMatrix = Matrix.Create<double>(SourceSystemCoordinates.Count * 3, 1);
var Ap = Matrix.Multiply(aMatrix, vecParamsMatrix);
// V = A * P - Y
vMatrix = (DenseMatrix<double>) Matrix.Subtract(Ap, yMatrix);
Expand All @@ -170,14 +166,14 @@ private double CalculateFCoefficient(Matrix<double> vMatrix)

private double CalculateMCoefficient(double fCoefficient)
{
return Math.Sqrt(fCoefficient / (_sourceSystemCoordinates.Count * 3 - 7));
return Math.Sqrt(fCoefficient / (SourceSystemCoordinates.Count * 3 - 7));
}

private Matrix<double> GetQMatrix(Matrix<double> aMatrix)
{
return Matrix.Multiply(aMatrix.Transpose(), aMatrix).GetInverse();
}

private Matrix<double> GetMeanSquareErrorsMatrix(Matrix<double> qMatrix, double mCoefficient)
{
var qDiagonal = qMatrix.GetDiagonal().ToColumnMatrix().SqrtInPlace();
Expand All @@ -186,7 +182,7 @@ private Matrix<double> GetMeanSquareErrorsMatrix(Matrix<double> qMatrix, double

#region InternalMethodsForTesting

internal Matrix<double> FormingCoordinateMatrixTst(List<SystemCoordinate> list)
internal Matrix<double> FormingCoordinateMatrixTst(List<Coordinates> list)
{
return FormingCoordinateMatrix(list);
}
Expand Down Expand Up @@ -233,16 +229,7 @@ internal Matrix<double> GetMeanSquareErrorsMatrixTst(Matrix<double> qMatrix, dou
{
return GetMeanSquareErrorsMatrix(qMatrix, mCoefficient);
}

#endregion
}

public interface ITransform
{
double Wx { get; set; }
double Wy { get; set; }
double Wz { get; set; }
double M { get; set; }
void CalculateTransformationParameters();
#endregion
}
}
45 changes: 0 additions & 45 deletions SCPT/CalculateParameters/SystemCoordinate.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Runtime.InteropServices;
using CalculateParameters;
using Xunit;


public class CoordinatesTests
{
[Fact]
private void Coordinate_InitCtorInvalidArgumentNaN_ThrowsArgumentException()
{
double x = double.NaN, y = double.NaN, z = double.NaN;

Action test = () => new Coordinates(x, y, z);

Assert.Throws<ArgumentException>(test);
}

[Fact]
private void Coordinate_InitCtorInvalidArgumentInfinity_ThrowsArithmeticException()
{
double xPositiveInfinity = double.PositiveInfinity;
double yPositiveInfinity = double.PositiveInfinity;
double zPositiveInfinity = double.PositiveInfinity;

double xNegativeInfinity = Double.NegativeInfinity;
double yNegativeInfinity = Double.NegativeInfinity;
double zNegativeInfinity = Double.NegativeInfinity;

Action test1 = () => new Coordinates(xPositiveInfinity, yPositiveInfinity, zPositiveInfinity);
Action test2 = () => new Coordinates(xNegativeInfinity, yNegativeInfinity, zNegativeInfinity);

Assert.Throws<ArithmeticException>(test1);
Assert.Throws<ArithmeticException>(test2);
}

[Fact]
private void Coordinate_TryGetXYZProperty_ValidXYZReturnValue()
{
var xExpected = 235654.242;
var yExpected = 267654.567;
var zExpected = 223654.234;

var coords = new Coordinates(xExpected, yExpected, zExpected);

Assert.Equal(xExpected, coords.X);
Assert.Equal(yExpected, coords.Y);
Assert.Equal(zExpected, coords.Z);
}
}
Loading

0 comments on commit cfddd09

Please sign in to comment.