mklines: rewrite main to use new classes

Test with
	python mklines.py polar_cords.csv
	Shows correct conversion from polar to cart
	with new classes

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-01-25 23:01:24 +03:00
parent d0a2cbb0db
commit 1474e7e03a
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -6,7 +6,7 @@ import csv
import sys import sys
import math import math
from os.path import exists as file_exists from os.path import exists as file_exists
from basictypes import PointWithID, Line from basictypes import PointWithID, Line, point_from_polar
def main(): def main():
@ -15,41 +15,29 @@ def main():
if not file_exists(filename): if not file_exists(filename):
perr('File does not exist') perr('File does not exist')
return -1 return -1
samples_polar = read_csv(filename, remove_header=True) points = read_csv(filename, remove_header=True)
if not valid_data(samples_polar): # Assumes header is removed if not valid_data(points): # Assumes header is removed
perr('Invalid or corrupt samples') perr('Invalid or corrupt samples')
return -1 return -1
x = polarlist_to_cart(samples_polar) for val in points: print(val.get_cartesian())
return 0 return 0
def read_csv(filename: str, remove_header=False) -> list: def read_csv(filename: str, remove_header=False) -> list[PointWithID]:
tmp_data = list() tmp_data = list() # New list of points (PointWithID)
with open(filename, 'r') as csv_file: with open(filename, 'r') as csv_file:
reader = csv.reader(csv_file) reader = csv.reader(csv_file)
for i, row in enumerate(reader): for i, row in enumerate(reader):
new_row = list() if remove_header and i == 0: continue # Skip header
if remove_header and i == 0: continue # Assumes wall_id,r,theta
for i, val in enumerate(row): point_tmp = point_from_polar(float(row[1]), float(row[2]), row[0])
if i == 0: new_row.append(val) # Append wall_id as is tmp_data.append(point_tmp)
else: new_row.append(float(val))
tmp_data.append(new_row)
return tmp_data return tmp_data
def valid_data(samples:list) -> bool: def valid_data(samples:list) -> bool:
# Check if we have even number of samples # Check if we have even number of samples
return len(samples) % 2 == 0 return len(samples) % 2 == 0
def polarlist_to_cart(polar_points: list):
cart_tmp = list()
for i, polar_point in enumerate(polar_points, 1):
if i % 2 == 0: continue
# We can safely use i - 1 here
p1 = polar_points[i-1]
p2 = polar_points[i]
print(p1, p2)
p1 = polar_to_cart(p1[0],p1[1])
p2 = polar_to_cart(p2[0],p2[1])
def get_filename() -> str: def get_filename() -> str:
try: try:
@ -60,7 +48,7 @@ def get_filename() -> str:
return -1 return -1
def perr(*args): def perr(*args):
print(args, file=sys.stderr) print(*args, file=sys.stderr)
if __name__ == '__main__': if __name__ == '__main__':
exit(main()) exit(main())