66 lines
1.8 KiB
Python
66 lines
1.8 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
|
|
from basictypes import PointWithID, Line
|
|
|
|
|
|
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) # Append wall_id as is
|
|
else: new_row.append(float(val))
|
|
tmp_data.append(new_row)
|
|
return tmp_data
|
|
|
|
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 get_filename() -> str:
|
|
try:
|
|
filename = sys.argv[1]
|
|
return filename
|
|
except IndexError:
|
|
perr('Provide target file name')
|
|
return -1
|
|
|
|
def perr(*args):
|
|
print(args, file=sys.stderr)
|
|
|
|
if __name__ == '__main__':
|
|
exit(main()) |