does not work

This commit is contained in:
Dmitriy Shishkov 2025-04-04 12:38:48 +03:00
commit 03db246d88
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
10 changed files with 436 additions and 0 deletions

80
Calculator.cs Normal file
View File

@ -0,0 +1,80 @@
namespace omp
{
public class OverlappingPoints : Exception { }
public static class Calculator
{
public const double StationPDOP = -1000;
public static double[,] Jacobian1T(List<Point> stations, Point position)
{
int n = stations.Count;
double[,] J = new double[4, n];
for (int i = 0; i < n; ++i)
{
double distance = position.Distance(stations[i]);
if (distance == 0)
{
throw new OverlappingPoints();
}
J[0, i] = -(stations[i].x - position.x) / distance;
J[1, i] = -(stations[i].y - position.y) / distance;
J[2, i] = -(stations[i].z - position.z) / distance;
J[3, i] = 1; // Clock bias
}
return J;
}
public static double PDOP(List<Point> stations, Point position)
{
double[,] JR = Jacobian1T(stations, position);
double[,] mult = MatrixOps.MultTrans(JR);
double det = MatrixOps.Det(mult);
double sx2 = MatrixOps.Adj(mult, 0, 0) / det;
double sy2 = MatrixOps.Adj(mult, 1, 1) / det;
double sz2 = MatrixOps.Adj(mult, 2, 2) / det;
return Math.Sqrt(sx2 + sy2 + sz2);
}
public static void PDOPs(double[,] pdops, Configurator conf, out double min, out double max)
{
min = double.MaxValue;
max = double.MinValue;
for (int i = 0; i < pdops.GetLength(0); ++i)
{
for (int j = 0; j < pdops.GetLength(1); ++j)
{
try
{
pdops[i, j] = PDOP(conf.stations, new Point(i, j, conf.zCoordinate));
if (pdops[i, j] > max)
{
max = pdops[i, j];
}
if (pdops[i, j] < min)
{
min = pdops[i, j];
}
}
catch (OverlappingPoints)
{
pdops[i, j] = StationPDOP;
}
}
}
Console.WriteLine("{0}...{1}", min, max);
}
}
}

96
Configurator.cs Normal file
View File

@ -0,0 +1,96 @@
namespace omp
{
public class Configurator
{
public int xsize;
public int ysize;
public int zCoordinate;
public List<Point> stations = [];
public Configurator(string[] args)
{
List<string> lines;
if (args.Length == 0)
{
Console.WriteLine("Input parameters:");
lines = [];
string? line;
while ((line = Console.ReadLine()) != null && line != "")
{
lines.Add(line);
}
}
else if (args.Length == 1)
{
lines = new(File.ReadAllLines(args[0]));
}
else
{
throw new ArgumentException("You must either provide path to parameters file as an only cli argument, or insert it into STDIN");
}
ParseParameters(lines);
}
private void ParseParameters(IReadOnlyList<string> lines)
{
bool hasZ = CheckHasZ(lines[1]);
ParseSize(lines[0]);
ParseZCoord(lines[1], hasZ);
ParseStations(lines, hasZ);
}
private static bool CheckHasZ(string line)
{
return line.Split(' ').Length == 1; // line contains a single number, z coordinate
}
private void ParseSize(string line)
{
string[] size_parts = line.Split(' ', 2);
xsize = int.Parse(size_parts[0]);
ysize = int.Parse(size_parts[1]);
}
private void ParseZCoord(string line, bool hasZ)
{
if (hasZ)
{
zCoordinate = int.Parse(line);
}
else
{
zCoordinate = 0;
}
}
private void ParseStations(IReadOnlyList<string> lines, bool hasZ)
{
int firstIndex = 1 + (hasZ ? 1 : 0);
for (int i = firstIndex; i < lines.Count; ++i)
{
string[] point_parts = lines[i].Split(' ', 2 + (hasZ ? 1 : 0));
int x, y, z;
x = int.Parse(point_parts[0]) - 1;
y = int.Parse(point_parts[1]) - 1;
if (hasZ)
{
z = int.Parse(point_parts[2]);
}
else
{
z = 0;
}
stations.Add(new Point(x, y, z));
}
}
}
}

67
Drawer.cs Normal file
View File

@ -0,0 +1,67 @@
using SkiaSharp;
namespace omp
{
class Drawer
{
public static void Draw(Configurator conf, double[,] pdops, double min, double max)
{
var bitmap = new SKBitmap(conf.xsize, conf.ysize);
for (int i = 0; i < conf.xsize; ++i)
{
for (int j = 0; j < conf.ysize; ++j)
{
SKColor color;
if (pdops[i, j] == Calculator.StationPDOP)
{
color = SKColors.White;
}
else
{
double t = (pdops[i, j] - min) / (max - min);
color = Map2Color(t);
}
bitmap.SetPixel(i, j, color);
}
}
using (var image = SKImage.FromBitmap(bitmap))
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
using (var stream = File.OpenWrite("out.png"))
{
data.SaveTo(stream);
}
}
public static SKColor Map2Color(double t)
{
if (t < 0.25)
{
// Blue to cyan
double localT = t / 0.25;
return new SKColor(0, (byte)(255 * localT), 255);
}
else if (t < 0.5)
{
// Cyan to green
double localT = (t - 0.25) / 0.25;
return new SKColor(0, 255, (byte)(255 * (1 - localT)));
}
else if (t < 0.75)
{
// Green to yellow
double localT = (t - 0.5) / 0.25;
return new SKColor((byte)(255 * localT), 255, 0);
}
else
{
// Yellow to red
double localT = (t - 0.75) / 0.25;
return new SKColor(255, (byte)(255 * (1 - localT)), 0);
}
}
}
}

129
MatrixOps.cs Normal file
View File

@ -0,0 +1,129 @@
namespace omp
{
public static class MatrixOps
{
public static void Print(double[,] a)
{
int rows = a.GetLength(0);
int columns = a.GetLength(1);
Console.WriteLine("Matrix of size {0}x{1}:", rows, columns);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write("\t{0}", a[i, j]);
}
Console.WriteLine("");
}
}
public static double Adj(double[,] a, int i, int j)
{
double sgn = ((i + j) % 2 == 0) ? 1 : -1;
return sgn * Det(Minor(a, j, i));
}
public static double[,] Minor(double[,] a, int i, int j)
{
int rows = a.GetLength(0);
int columns = a.GetLength(1);
double[,] minor = new double[rows - 1, columns - 1];
int k = 0;
int l;
for (int r = 0; r < rows; ++r)
{
if (r == i)
{
continue;
}
l = 0;
for (int c = 0; c < columns; ++c)
{
if (c == j)
{
continue;
}
minor[k, l] = a[r, c];
l++;
}
k++;
}
return minor;
}
public static double Det(double[,] a)
{
int n = a.GetLength(0);
switch (n)
{
case 2:
return Det2(a);
case 3:
return Det3(a);
default:
double det = 0;
double sgn;
for (int j = 0; j < n; ++j)
{
sgn = (j % 2 == 0) ? 1 : -1;
det += sgn * a[0, j] * Det(Minor(a, 0, j));
}
return det;
}
}
public static double Det2(double[,] a)
{
return a[0, 0] * a[1, 1] - a[0, 1] * a[1, 0];
}
public static double Det3(double[,] a)
{
if (a.GetLength(0) != 3 || a.GetLength(1) != 3)
throw new ArgumentException("Matrix must be 3x3");
return
a[0, 0] * a[1, 1] * a[2, 2]
+ a[0, 1] * a[1, 2] * a[2, 0]
+ a[0, 2] * a[1, 0] * a[2, 1]
- a[0, 2] * a[1, 1] * a[2, 0]
- a[0, 0] * a[1, 2] * a[2, 1]
- a[2, 2] * a[0, 1] * a[1, 0];
}
public static double[,] MultTrans(double[,] a)
{
int rows = a.GetLength(0);
int columns = a.GetLength(1);
double[,] r = new double[rows, rows];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
for (int k = 0; k < columns; k++)
{
r[i, j] += a[i, k] * a[j, k];
}
}
}
return r;
}
}
}

21
Point.cs Normal file
View File

@ -0,0 +1,21 @@
namespace omp
{
public class Point(int x, int y, int z)
{
public int x = x, y = y, z = z;
public double Distance(Point a)
{
double dx = x - a.x;
double dy = y - a.y;
double dz = z - a.z;
return Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
public bool Equals(Point a)
{
return (x == a.x) && (y == a.y) && (z == a.z);
}
}
}

17
Program.cs Normal file
View File

@ -0,0 +1,17 @@
namespace omp
{
public class DOPCalculator
{
public static void Main(string[] args)
{
Configurator conf = new(args);
double[,] pdops = new double[conf.xsize, conf.ysize];
double min, max;
Calculator.PDOPs(pdops, conf, out min, out max);
Drawer.Draw(conf, pdops, min, max);
}
}
}

17
omp.csproj Normal file
View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="3.116.1" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" />
<PackageReference Include="System.Drawing.Common" Version="9.0.3" />
</ItemGroup>
</Project>

4
test2.txt Normal file
View File

@ -0,0 +1,4 @@
500 500
1
100 499 0
400 499 0

5
test3.txt Normal file
View File

@ -0,0 +1,5 @@
1000 1000
0
236 134 0
147 863 0
915 591 0