def random_position(self, *, rect: Optional[types.Rect] = None, center: Optional[types.Position] = None, radius: Optional[float] = None, min_speed: float = 0, max_speed: float = 0) -> types.Position: self._apply_seed() # Generate random velocity vector speed = random.uniform(min_speed, max_speed) velocity = types.Vector(x=random.random(), y=random.random()) velocity.normalize().set_length(speed) # Build random position if rect: assert center is None and radius is None return types.Position(x=random.uniform(rect.left, rect.right), y=random.uniform(rect.bottom, rect.top), velocity=velocity) elif center: assert rect is None assert radius is not None alfa = random.uniform(0, 2 * math.pi) r = math.sqrt(random.uniform(0, radius**2)) return types.Position(x=center.x + r * math.cos(alfa), y=center.y + r * math.sin(alfa), velocity=velocity)
def test_prepare_fligh_plan_2D(self): # Some special cases: position = types.Position(0, 0, types.Vector(0, 100)) target = types.Position(100, 0, types.Vector(0, 100)) # Build flight plan plan = prepare_flight_plan(position, target, amax=10) # Predict flight plan result arrive = plan.apply_to(position) self.assertTrue( positions_equal( one=target, other=arrive, ds=max(0.01, position.distance_to(target) * 0.001), dv=max(0.01, (target.velocity - position.velocity).abs() * 0.001))) # Random cases rnd.seed(34254) for i in range(2000): case_seed = rnd.randint(1, 1000000) rnd.seed(case_seed) # Create position and target velocity_case = rnd.randint(0, 6) timestamp = random.randint(0, 10 * 10**6) if velocity_case == 0: position = random_position(0, 0, 100000, 0, timestamp) target = random_position(0, 0, 100000, 0) elif velocity_case == 1: position = random_position(0, 0, 100000, 5000, timestamp) target = random_position(0, 0, 100000, 0) elif velocity_case == 2: position = random_position(0, 0, 100000, 0, timestamp) target = random_position(0, 0, 100000, 5000) elif velocity_case >= 3: position = random_position(0, 0, 100000, 5000, timestamp) target = random_position(0, 0, 100000, 5000) # Build flight plan plan = prepare_flight_plan(position, target, amax=rnd.uniform(0.1, 1000)) # Predict flight plan result arrive = plan.apply_to(position) self.assertTrue( positions_equal( one=target, other=arrive, ds=max(0.01, position.distance_to(target) * 0.001), dv=max(0.01, (target.velocity - position.velocity).abs() * 0.001)), f"Case seed: {case_seed}")
def random_positions_1D( x: float, y: float, radius: float, max_start_speed: float, max_stop_speed: float, start_timestamp: int = 0) -> Tuple[types.Position, types.Position]: start = random_position(x, y, radius, 0) stop = random_position(x, y, radius, 0) start_velocity = start.vector_to(stop).set_length(max_start_speed * rnd.random()) stop_velocity = start.vector_to(stop).set_length(max_stop_speed * rnd.random()) return types.Position(start.x, start.y, start_velocity, types.TimePoint(start_timestamp, static=True)), \ types.Position(stop.x, stop.y, stop_velocity, None)
def create_target(self, position: types.Position, distance: float): vector_to_target = position.velocity.set_length(distance, inplace=False) return types.Position(x=position.x + vector_to_target.x, y=position.y + vector_to_target.y, velocity=position.velocity.set_length( rnd.random() * 10000, inplace=False))
def random_position(x: float, y: float, deviation: float, speed_max: float, timestamp: Optional[int] = None) -> types.Position: alfa = rnd.random() * math.tau r = rnd.random() * deviation timestamp = types.TimePoint(timestamp, static=True) if timestamp else None return types.Position(x=r * math.cos(alfa), y=r * math.sin(alfa), velocity=random_vector(rnd.random() * speed_max), timestamp=timestamp)
async def wait_update(self, timeout: float = 1) \ -> Tuple[Status, List[types.PhysicalObject]]: response, timestamp = await self.wait_message(timeout=timeout) if not response: return PassiveScannerI.Status.SUCCESS, list() update = api.get_message_field( response, ["passive_scanner", "update"]) if not update: return PassiveScannerI.Status.UNEXPECTED_RESPONSE, list() return PassiveScannerI.Status.SUCCESS, [ types.PhysicalObject( object_type=types.ObjectType.from_protobuf(item.object_type), object_id=item.id, position=types.Position( x=item.x, y=item.y, velocity=types.Vector(x=item.vx, y=item.vy), timestamp=types.TimePoint(timestamp) ), radius=item.r ) for item in update.items ]