2022-01-25 19:48:48 +01:00
|
|
|
# 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
|
2022-01-30 17:33:46 +01:00
|
|
|
from basictypes import PointWithID, StraightLine, point_from_polar
|
2022-01-25 19:48:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
filename = get_filename()
|
|
|
|
if filename == -1: return -1
|
|
|
|
if not file_exists(filename):
|
|
|
|
perr('File does not exist')
|
|
|
|
return -1
|
2022-01-25 21:01:24 +01:00
|
|
|
points = read_csv(filename, remove_header=True)
|
|
|
|
if not valid_data(points): # Assumes header is removed
|
2022-01-25 19:48:48 +01:00
|
|
|
perr('Invalid or corrupt samples')
|
|
|
|
return -1
|
2022-01-30 17:33:46 +01:00
|
|
|
#for val in points: print(val.get_cartesian())
|
2022-02-14 12:46:47 +01:00
|
|
|
lines = list()
|
2022-01-30 17:33:46 +01:00
|
|
|
for i in range(0, len(points), 2):
|
|
|
|
p1 = points[i]
|
|
|
|
p2 = points[i+1]
|
|
|
|
print('p1 ', p1.get_x(), p1.get_y())
|
|
|
|
print('p2 ', p2.get_x(), p2.get_y())
|
|
|
|
line = StraightLine(p1, p2)
|
2022-02-14 12:46:47 +01:00
|
|
|
lines.append(line)
|
2022-01-30 17:33:46 +01:00
|
|
|
print('line ', line.get_m(), line.get_b())
|
2022-02-14 12:46:47 +01:00
|
|
|
intercepts = list()
|
|
|
|
for i, line in enumerate(lines):
|
2022-02-14 13:33:53 +01:00
|
|
|
p = None # This is risky
|
|
|
|
if i == len(lines) - 1:
|
|
|
|
print("last")
|
|
|
|
p = lines[i].intercept(lines[0])
|
|
|
|
# Last line is a special case
|
|
|
|
else:
|
|
|
|
p = lines[i].intercept(lines[i+1])
|
2022-02-14 12:46:47 +01:00
|
|
|
print(p.get_x(), p.get_y())
|
2022-01-25 19:48:48 +01:00
|
|
|
return 0
|
|
|
|
|
2022-01-25 21:01:24 +01:00
|
|
|
def read_csv(filename: str, remove_header=False) -> list[PointWithID]:
|
|
|
|
tmp_data = list() # New list of points (PointWithID)
|
2022-01-25 19:48:48 +01:00
|
|
|
with open(filename, 'r') as csv_file:
|
|
|
|
reader = csv.reader(csv_file)
|
|
|
|
for i, row in enumerate(reader):
|
2022-01-25 21:01:24 +01:00
|
|
|
if remove_header and i == 0: continue # Skip header
|
|
|
|
# Assumes wall_id,r,theta
|
2022-02-14 14:14:52 +01:00
|
|
|
point_tmp = PointWithID(float(row[1]), float(row[2]), row[0], PointWithID.FROM_POLAR)
|
2022-01-25 21:01:24 +01:00
|
|
|
tmp_data.append(point_tmp)
|
|
|
|
|
2022-01-25 19:48:48 +01:00
|
|
|
return tmp_data
|
|
|
|
|
|
|
|
def valid_data(samples:list) -> bool:
|
2022-01-30 17:33:46 +01:00
|
|
|
# Check if we have an even number of samples
|
2022-01-25 19:48:48 +01:00
|
|
|
return len(samples) % 2 == 0
|
|
|
|
|
|
|
|
|
|
|
|
def get_filename() -> str:
|
|
|
|
try:
|
|
|
|
filename = sys.argv[1]
|
|
|
|
return filename
|
|
|
|
except IndexError:
|
|
|
|
perr('Provide target file name')
|
|
|
|
return -1
|
|
|
|
|
2022-01-25 20:45:45 +01:00
|
|
|
def perr(*args):
|
2022-01-25 21:01:24 +01:00
|
|
|
print(*args, file=sys.stderr)
|
2022-01-25 19:48:48 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
exit(main())
|