dev: Первая итерация

Пока только простое преобразование и отрисовка графика. Без передачи информации о преобразованиях
This commit is contained in:
Pavel Naboka 2023-09-05 15:54:19 +03:00
commit 23c094bb71
4 changed files with 68 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
poetry.lock

34
main.py Normal file
View File

@ -0,0 +1,34 @@
from pathlib import Path
import numpy
from matplotlib import pyplot
from transformations import centroid, kabsch_algorithm
expected_points_file_path = Path('expected_points.txt')
actual_points_file_path = Path('actual_points.txt')
plot_3d = pyplot.figure().add_subplot(projection='3d')
def read_points_from_file(points_file_path: Path) -> numpy.ndarray:
with points_file_path.open('r') as points_file:
return numpy.array([[float(coordinate) for coordinate in point_line.strip().split(' ')]
for point_line in points_file])
if __name__ == '__main__':
expected_points = read_points_from_file(expected_points_file_path)
actual_points = read_points_from_file(actual_points_file_path)
expected_points -= centroid(expected_points)
actual_points -= centroid(actual_points)
actual_points = numpy.dot(actual_points, kabsch_algorithm(actual_points, expected_points))
plot_3d.plot(actual_points[:, 0], actual_points[:, 1], actual_points[:, 2], 'o-', markersize=12, linewidth=3)
plot_3d.plot(expected_points[:, 0], expected_points[:, 1], expected_points[:, 2], 'o-', markersize=12, linewidth=3)
plot_3d.grid(True)
plot_3d.tick_params(labelsize=15)
pyplot.show()

16
pyproject.toml Normal file
View File

@ -0,0 +1,16 @@
[tool.poetry]
name = "reinmetal"
version = "0.1.0"
description = ""
authors = ["Pavel Naboka <naboka@diakont.com>"]
[tool.poetry.dependencies]
python = "^3.10"
numpy = "^1.25.2"
[tool.poetry.dev-dependencies]
matplotlib = "^3.7.2"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

17
transformations.py Normal file
View File

@ -0,0 +1,17 @@
import numpy
def centroid(points: numpy.ndarray) -> numpy.ndarray:
return points.mean(axis=0)
def kabsch_algorithm(actual_points: numpy.ndarray, expected_points: numpy.ndarray) -> numpy.ndarray:
# TODO Заменить безликие C, V, S, W на что-то более внятное
C = numpy.dot(numpy.transpose(actual_points), expected_points)
V, S, W = numpy.linalg.svd(C)
if (numpy.linalg.det(V) * numpy.linalg.det(W)) < 0.0:
S[-1] = -S[-1]
V[:, -1] = -V[:, -1]
return numpy.dot(V, W)