def mirror(pivot: Vector, center: Vector, norm: Vector): base = pivot - center norm.normalize() a = copy.deepcopy(base) a.rotate(Triangle.get_rotate_matrix(norm, 2 / 3 * math.pi)) a = center + a b = copy.deepcopy(base) b.rotate(Triangle.get_rotate_matrix(norm, -2 / 3 * math.pi)) b = center + b return [pivot.get_point(), a.get_point(), b.get_point()]
def test_point_and_vector_addition(self): p = Point(3, 5) v = Vector(0, 1) p2 = p + v self.assertEqual(3, p2.x) self.assertEqual(6, p2.y)
def parse(model: str) -> {}: with open( str(get_project_root()) + '/configs/' + model + '_input.json', 'r') as input_file: data = input_file.read() configs = json.loads(data) return { 'start_laser_location': Point3d(configs['laser_loc'][0], configs['laser_loc'][1], configs['laser_loc'][2]), 'start_laser_direction': Vector( Point3d(configs['laser_dir'][0], configs['laser_dir'][1], configs['laser_dir'][2])), 'down_triangle': [ Point3d(configs['down_tr'][0][0], configs['down_tr'][0][1], configs['down_tr'][0][2]), Point3d(configs['down_tr'][1][0], configs['down_tr'][1][1], configs['down_tr'][1][2]), Point3d(configs['down_tr'][2][0], configs['down_tr'][2][1], configs['down_tr'][2][2]), ], 'up_triangle': [ Point3d(configs['up_triangle'][0][0], configs['up_triangle'][0][1], configs['up_triangle'][0][2]), Point3d(configs['up_triangle'][1][0], configs['up_triangle'][1][1], configs['up_triangle'][1][2]), Point3d(configs['up_triangle'][2][0], configs['up_triangle'][2][1], configs['up_triangle'][2][2]), ], "target_radius": configs["target_radius"] }
def apply_fp(self, obj): fp = obj.footprint success = True if fp.is_square: vector = Vector.from_point(obj.coords) for pt in fp.points: pt = pt + vector if not self.get(pt).occupy(obj): success = False if not success: self.release_fp(obj) return False else: cx, cy = obj.coords.get() radius = fp.size / 2 gr = radius / self.CORE.sub_per_cell for y in range(int(cy-gr/2)-1, int(cy+gr/2)+2): if not success: break for x in range(int(cx-gr/2)-1, int(cx+gr/2)+2): if not self.get((x,y)).occupy(obj): success = False break if not success: self.release_fp(obj) return False return True
def release_fp(self, obj): fp = obj.footprint if fp.is_square: vector = Vector.from_point(obj.coords) for pt in fp.points: pt = pt + vector self.get(pt).release(obj) else: cx, cy = obj.coords.get() radius = fp.size / 2 gr = radius / self.CORE.sub_per_cell for y in range(int(cy-gr/2)-1, int(cy+gr/2)+2): for x in range(int(cx-gr/2)-1, int(cx+gr/2)+2): self.get((x,y)).release(obj)
class WindRose: NORTH = Vector(0, 1) NORTH_INDEX = 0 EAST = Vector(1, 0) EAST_INDEX = 1 SOUTH = Vector(0, -1) SOUTH_INDEX = 2 WEST = Vector(-1, 0) WEST_INDEX = 3 ORIENTATIONS = [NORTH, EAST, SOUTH, WEST] @staticmethod def clockwise_rotate(index): next = (index + 1) % 4 return WindRose.ORIENTATIONS[next], next @staticmethod def anti_clockwise_rotate(index): next = (index - 1) % 4 return WindRose.ORIENTATIONS[next], next
def sub_occupy(self, obj): if not self.crossable: return False if self.occupied == 2: return False size = obj.footprint.size / 2 # Radius for x in range(self.size): for y in range(self.size): sx, sy = self.coords.get() coords = Point(sx+(x/self.size), sy+(y/self.size)) v = Vector.from_abs(obj.coords, coords) if v.magnitude > size: continue if self.subcells[y][x] is not None: return False self.subcells[y][x] = obj self.occupied = 1 return True
def test_angle_between_vectors(self): self.assertEqual(angle_between_vectors(Vector(1, 0), Vector(1, 0)), 0) self.assertEqual(angle_between_vectors(Vector(1, 0), Vector(0, 1)), np.pi / 2) self.assertEqual(angle_between_vectors(Vector(-1, 1), Vector(-1, -1)), np.pi / 2)
def test_scalar_product(self): self.assertEqual(scalar_product(Vector(1, 0), Vector(0, 1)), 0) self.assertEqual(scalar_product(Vector(1, 0), Vector(1, 0)), 1) self.assertEqual(scalar_product(Vector(1, 0), Vector(-1, 0)), -1)
def test_normalize(self): d = Vector(1, 2) d = d.normalize() self.assertAlmostEqual(d.length(), 1)
def test_action(self): self.assertEqual(Vector(1, 1).to_direction(), Action.TopRight) self.assertEqual(Vector(1, -1).to_direction(), Action.BottomRight)
def test_reverse(self): d1 = Vector(1, 1) d1 = d1.reverse() d2 = Vector(-1, -1) self.assertAlmostEqual(d1.x, d2.x) self.assertAlmostEqual(d1.y, d2.y)
def test_turn(self): d1 = Vector(1, 1) d1 = d1.turn(np.pi / 4) self.assertAlmostEqual(d1.x, 0) self.assertAlmostEqual(d1.y, np.sqrt(2))
def test_distance(self): self.assertEqual(Vector(1, 0).length(), 1) self.assertEqual(Vector(1, 1).length(), np.sqrt(2))
def test_angle(self): self.assertEqual(Vector(1, 0).angle(), 0) self.assertEqual(Vector(0, 1).angle(), np.pi / 2) self.assertEqual(Vector(-1, 0).angle(), np.pi) self.assertEqual(Vector(0, -1).angle(), -np.pi / 2) self.assertEqual(Vector(1, 1).angle(), np.pi / 4)