init: early test, classes WIP
This commit is contained in:
		
						commit
						5461422c6b
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
__pycache__/
 | 
			
		||||
							
								
								
									
										75
									
								
								basictypes.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								basictypes.py
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
							
								
								
									
										75
									
								
								mklines.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								mklines.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,75 @@
 | 
			
		||||
# 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())
 | 
			
		||||
							
								
								
									
										14
									
								
								notes
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								notes
									
									
									
									
									
										Normal file
									
								
							@ -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.
 | 
			
		||||
							
								
								
									
										9
									
								
								polar_cords.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								polar_cords.csv
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
		
		
			
  | 
							
								
								
									
										0
									
								
								simulate.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								simulate.py
									
									
									
									
									
										Normal file
									
								
							
		Loading…
	
		Reference in New Issue
	
	Block a user