From 5461422c6b7c5b48bf0036ed477552b4a3860c1d Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Tue, 25 Jan 2022 21:48:48 +0300 Subject: [PATCH] init: early test, classes WIP --- .gitignore | 1 + basictypes.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ mklines.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ notes | 14 +++++++++ polar_cords.csv | 9 ++++++ simulate.py | 0 6 files changed, 174 insertions(+) create mode 100644 .gitignore create mode 100644 basictypes.py create mode 100644 mklines.py create mode 100644 notes create mode 100644 polar_cords.csv create mode 100644 simulate.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/basictypes.py b/basictypes.py new file mode 100644 index 0000000..c59dac3 --- /dev/null +++ b/basictypes.py @@ -0,0 +1,75 @@ +# Container classes for points and lines + +import math + +class Point: + + IDX_X = 0 + IDX_Y = 1 + + def __init__(self, x, y): + self._x = x + self._y = y + + def get_cartesian(self) -> tuple: + return self.get_x(), self.get_y() + + def get_polar(self) -> tuple: + pass + + def get_x(self) -> float: + pass + + def get_y(self) -> float: + pass + + def get_r(self) -> float: + pass + + def get_theta(self) -> float: + pass + + def apply_polar_shift(self, r, theta) -> None: + pass + + def apply_cartesian_shift(self, x, y) -> None: + pass + + +class PointWithID(Point): + + IDX_WALLID = 3 + def __init__(self, x, y, wall_id): + super().__init__(x, y) + self._wall_id = wall_id + + def get_wallid(self): + return self._wall_id + +class Line: + + def __init__(self, p1: PointWithID, p2: PointWithID): + self.wall_id = p1.get_wallid() + self.__find_slope(p1, p2) + v = ValueError() + if not self.__matching_wall_ids(p1, p2): + raise WallIDMismatch('Wall ID for points do not match') + + def __find_slope(self, p1:PointWithID, p2:PointWithID) -> float: + pass + + def __matching_wall_ids(self, p1, p2) -> bool: + if p1.get_wallid() == p2.get_wallid(): return True + return False + +class WallIDMismatch (RuntimeError): ... + +# Helper methods +def polar_to_cart(self, r, theta_deg) -> tuple: + #print('polar to cart ',r, theta_deg) + x = r * math.cos(deg_to_rad(theta_deg)) + y = r * math.sin(deg_to_rad(theta_deg)) + return x,y + +def deg_to_rad(theta_deg) -> float: + return theta_deg*(math.pi/180) \ No newline at end of file diff --git a/mklines.py b/mklines.py new file mode 100644 index 0000000..790a63a --- /dev/null +++ b/mklines.py @@ -0,0 +1,75 @@ +# Constructs line equastions from a set of polar cords that represent a room + +# Invoke python mklines.py + +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()) \ No newline at end of file diff --git a/notes b/notes new file mode 100644 index 0000000..a0893de --- /dev/null +++ b/notes @@ -0,0 +1,14 @@ +1 - Generate a set of fixed measurements + May also construct a room to overlay output on to visualize possible erros +2 - Translate measurements into verticies on a plane. + If we only snapshot a straight wall once, a line an infinite line is assumed + We need to snap a wall twice to make the line + Thus 2 points one with angle 0 other is *x*. + Thus we represent a wall with + (4, 0) + (4.5, 30o) + We represent this with + (find intercept of 2 lines. i forgot) + With another line we can find the intercept as the vertix. + If we construct the walls in order it was taken (Not nessesary) +3 - Minimize number of times we convert polar to cartesian, since it does lose precision. \ No newline at end of file diff --git a/polar_cords.csv b/polar_cords.csv new file mode 100644 index 0000000..6c3d215 --- /dev/null +++ b/polar_cords.csv @@ -0,0 +1,9 @@ +wall id,distance,angle +1,2.03855,9.6007 +1,2.30208,29.683 +2,2.84987,76.40 +2,2.85707,104.182 +3,2.33238,149.036 +3,2.00997,174.289 +4,1.7,298.072 +4,1.68154,243 diff --git a/simulate.py b/simulate.py new file mode 100644 index 0000000..e69de29