room_scan_sim/mklines.py

75 lines
2.0 KiB
Python

# Constructs line equastions from a set of polar cords that represent a room
# Invoke python mklines.py <filename>
import csv
import sys
import math
from os.path import exists as file_exists
def main():
filename = get_filename()
if filename == -1: return -1
if not file_exists(filename):
perr('File does not exist')
return -1
samples_polar = read_csv(filename, remove_header=True)
if not valid_data(samples_polar): # Assumes header is removed
perr('Invalid or corrupt samples')
return -1
x = polarlist_to_cart(samples_polar)
return 0
def read_csv(filename: str, remove_header=False) -> list:
tmp_data = list()
with open(filename, 'r') as csv_file:
reader = csv.reader(csv_file)
for i, row in enumerate(reader):
new_row = list()
if remove_header and i == 0: continue
for i, val in enumerate(row):
if i == 0: new_row.append(val)
else: new_row.append(float(val))
tmp_data.append(new_row)
return tmp_data
def get_line_eq(point1, point2)-> tuple:
m = ((point2[1] - point1[1])/(point2[0] - point1[0]))
# y = mx + b
# b = y - mx
# Using p1 to find b
b = point1[1] - (m*point1[0])
def valid_data(samples:list) -> bool:
# Check if we have even number of samples
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 points_to_straight_line():
pass
def get_filename() -> str:
try:
filename = sys.argv[1]
return filename
except IndexError:
perr('Provide target file name')
return -1
def perr(msg):
print(msg, file=sys.stderr)
if __name__ == '__main__':
exit(main())