Exemple #1
0
    def test_move(self):
        # move_to tests
        v0, vt = vec.vec([0, 0]), vec.vec([0, 1])
        vn, ld = vec.move_to(v0, vt, 1)
        self.assertAlmostEqual(ld, 0)
        np.testing.assert_almost_equal(vn, vec.vec([0, 1]))

        vn, ld = vec.move_to(v0, vt, 0.5)
        self.assertAlmostEqual(ld, 0.5)
        np.testing.assert_almost_equal(vn, vec.vec([0, 0.5]))

        vn, ld = vec.move_to(v0, vt, 2)
        self.assertAlmostEqual(ld, -1)
        np.testing.assert_almost_equal(vn, vec.vec([0, 1]))

        # move_step tests
        v0, vt = vec.vec([0, 1]), vec.vec([0, 2])
        vn, ld = vec.move_step(v0, vt, 1)
        self.assertAlmostEqual(ld, 0)
        np.testing.assert_almost_equal(vn, vec.vec([0, 1]))

        vn, ld = vec.move_step(v0, vt, 0.5)
        self.assertAlmostEqual(ld, 0.5)
        np.testing.assert_almost_equal(vn, vec.vec([0, 0.5]))

        vn, ld = vec.move_step(v0, vt, 2)
        self.assertAlmostEqual(ld, -1)
        np.testing.assert_almost_equal(vn, vec.vec([0, 1]))
Exemple #2
0
    def test_mat_ae(self):
        """ 测试 mat_ae """
        v = vec.vec([0, 1, 0])

        m = move.mat_ae([0, 0])
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([0, 1, 0]))

        m = move.mat_ae([90, 0])
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([-1, 0, 0]))

        m = move.mat_ae([180, 0])
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([0, -1, 0]))

        m = move.mat_ae([270, 0])
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([1, 0, 0]))
        """ 测试俯仰. """
        v = vec.vec([0, 2**0.5, 0])
        m = move.mat_ae([0, -45])
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([0, 1, 1]))

        m = move.mat_ae([0, 45])
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([0, 1, -1]))
        """ 综合测试. """
        v = vec.vec([0, 2**0.5, 0])
        m = move.mat_ae([90, 45])
        np.testing.assert_almost_equal(v2, vec.vec([-1, 0, -1]))
Exemple #3
0
def draw_radar(painter, radar):
    if radar.is_active():
        color = (255, 0, 0)
        painter.rect((radar.position[0], radar.position[1]), color, 4)
        for _, ret in radar.results.items():
            d, a = ret[1]
            pt = radar.position + vec.vec([d * math.cos(a), d * math.sin(a)])
            painter.rect(pt, color, 1)
Exemple #4
0
 def access(self, others):
     assert self.parent
     f0 = vec([0, 0])
     _, dt = self.parent.env.time_info
     for obj in others:
         r = dist(self.parent.pos, obj.pos)
         f = unit(obj.pos - self.parent.pos) * G * obj.m / (r ** 2)
         f0 = f0 + f
     self.parent.vel += f0 * dt
Exemple #5
0
    def test_mat_a(self):
        v = vec.vec([0, 1])

        m = move.mat_a(00)
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([0, 1]))

        m = move.mat_a(90)
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([-1, 0]))

        m = move.mat_a(180)
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([0, -1]))

        m = move.mat_a(270)
        v2 = vec.trans(v, m)
        np.testing.assert_almost_equal(v2, vec.vec([1, 0]))
Exemple #6
0
    def test_move_to(self):
        """ 测试移动函数.
        1. move_to
        """
        pt0 = vec.vec([0, 0])
        pt1 = vec.vec([0, 1])

        pt2, d = move.move_to(pt0, pt1, 0.5)
        np.testing.assert_almost_equal(pt2, vec.vec([0, 0.5]))
        self.assertAlmostEqual(d, 0.5)

        pt2, d = move.move_to(pt0, pt1, 1)
        np.testing.assert_almost_equal(pt2, vec.vec([0, 1]))
        self.assertAlmostEqual(d, 0)

        pt2, d = move.move_to(pt0, pt1, 1.5)
        np.testing.assert_almost_equal(pt2, vec.vec([0, 1]))
        self.assertAlmostEqual(d, -0.5)
Exemple #7
0
    def test_create(self):
        radar = Radar()
        self.assertTrue(radar is not None)

        radar = Radar(pos=[1, 1])
        np.testing.assert_almost_equal(radar.position, vec.vec([1, 1]))

        radar.set_params(search_dt=2.0, track_dt=2.0)
        self.assertAlmostEqual(radar.search_dt, 2.0)
        self.assertAlmostEqual(radar.track_dt, 2.0)
Exemple #8
0
    def test_sim_clock(self):
        """ 测试仿真时钟. """
        # 测试默认构造
        clock = SimClock()
        self.assertAlmostEqual(clock.start, 0.0)
        self.assertAlmostEqual(clock.end, 10.0)
        self.assertAlmostEqual(clock.dt, 0.1)
        self.assertAlmostEqual(clock.now, 0.0)

        # 测试定制构造
        clock = SimClock(start=1.0, end=20.0, step=0.5)
        self.assertAlmostEqual(clock.start, 1.0)
        self.assertAlmostEqual(clock.end, 20.0)
        self.assertAlmostEqual(clock.dt, 0.5)
        self.assertAlmostEqual(clock.now, 1.0)

        # 测试默认构造下方法.
        clock = SimClock()
        np.testing.assert_almost_equal(
            vec.vec(clock.info()), vec.vec([0.0, 0.0]))

        tt = clock.step()
        self.assertTrue(tt == clock.info())
        np.testing.assert_almost_equal(
            vec.vec(clock.info()), vec.vec([0.1, 0.1]))

        while True:
            tt = clock.step()
            if not tt:
                break
        self.assertTrue(tt is None)
        np.testing.assert_almost_equal(
            vec.vec(clock.info()), vec.vec([10.1, 0.1]))
Exemple #9
0
    def test_view(self):
        fov = Fov()

        obj_pos = vec.vec([0, 0, 1])
        ret = fov.view(obj_pos, pos=vec.vec([0, 0, 0]), dir_=[0, 0])
        self.assertTrue(ret is not None)
        print(ret)

        obj_pos = vec.vec([1, 0, 1])
        ret = fov.view(obj_pos, pos=vec.vec([0, 0, 0]), dir_=[0, 0])
        self.assertTrue(ret is not None)
        print(ret)

        obj_pos = vec.vec([0, 1, 1])
        ret = fov.view(obj_pos, pos=vec.vec([0, 0, 0]), dir_=[0, 0])
        self.assertTrue(ret is not None)
        print(ret)
Exemple #10
0
    def test_scenario_run(self):
        """ 测试场景运行. """
        scene = Scenario()
        np.testing.assert_almost_equal(
            vec.vec(scene.clock_info), vec.vec([0, 0.]))

        time_rec = []

        scene.step_handlers.append(
            lambda s: time_rec.append(s.clock_info[0]))

        tt = scene.step()
        self.assertTrue(tt == scene.clock_info)
        np.testing.assert_almost_equal(
            vec.vec(scene.clock_info), vec.vec([0.1, 0.1]))

        scene.run()
        np.testing.assert_almost_equal(
            vec.vec(scene.clock_info), vec.vec([10.1, 0.1]))
        self.assertAlmostEqual(time_rec[0], 0.0)
        self.assertAlmostEqual(time_rec[-1], 10.0)
Exemple #11
0
 def __init__(self, **kwargs):
     super().__init__(**kwargs)
     self.position = vec.vec(kwargs['pos'])
     self.rcs = 0.1
Exemple #12
0
    def test_create(self):
        jammer = Jammer()
        self.assertTrue(jammer is not None)

        jammer = Jammer(pos=[1, 1])
        np.testing.assert_almost_equal(jammer.position, vec.vec([1, 1]))
Exemple #13
0
    def test_basic(self):
        #  测试 dist
        v1, v2, v3 = vec.vec([0, 0]), vec.vec([1, 1]), vec.vec([-1, -1])
        self.assertAlmostEqual(vec.dist(v1), 0.0)
        self.assertAlmostEqual(vec.dist(v2), 2**0.5)
        self.assertAlmostEqual(vec.dist(v3), 2**0.5)
        self.assertAlmostEqual(vec.dist(v1, v2), 2**0.5)
        self.assertAlmostEqual(vec.dist(v2, v3), 2 * (2**0.5))

        # 测试 unit
        v = vec.unit(vec.vec([0, 2]))
        np.testing.assert_almost_equal(v, vec.vec([0, 1]))
        v = vec.unit(vec.vec([0, 0.2]))
        np.testing.assert_almost_equal(v, vec.vec([0, 1]))
        v = vec.unit(vec.vec([0, 0]))
        np.testing.assert_almost_equal(v, vec.vec([0, 0]))

        # 测试 zeros_like
        v = vec.zeros_like(vec.vec([2]))
        np.testing.assert_almost_equal(v, vec.vec([0]))
        v = vec.zeros_like(vec.vec([0, 2]))
        np.testing.assert_almost_equal(v, vec.vec([0, 0]))
        v = vec.zeros_like(vec.vec([0, 2, 3]))
        np.testing.assert_almost_equal(v, vec.vec([0, 0, 0]))
Exemple #14
0
    def test_xyz_aer(self):
        """ 测试 XYZ 和 AER 之间的坐标转换.
        1.xyz_to_aer
        2.aer_to_xyz
        """
        xyz = vec.vec([1, 0, 0])
        aer = move.xyz_to_aer(xyz)
        np.testing.assert_almost_equal(aer, vec.vec([90, 0, 1]))
        xyz2 = move.aer_to_xyz(aer)
        np.testing.assert_almost_equal(xyz, xyz2)

        xyz = vec.vec([0, 1, 0])
        aer = move.xyz_to_aer(xyz)
        np.testing.assert_almost_equal(aer, vec.vec([0, 0, 1]))
        xyz2 = move.aer_to_xyz(aer)
        np.testing.assert_almost_equal(xyz, xyz2)

        xyz = vec.vec([-1, 0, 0])
        aer = move.xyz_to_aer(xyz)
        np.testing.assert_almost_equal(aer, vec.vec([270, 0, 1]))
        xyz2 = move.aer_to_xyz(aer)
        np.testing.assert_almost_equal(xyz, xyz2)

        xyz = vec.vec([0, 1, 1])
        aer = move.xyz_to_aer(xyz)
        np.testing.assert_almost_equal(aer, vec.vec([0, 45, 2**0.5]))
        xyz2 = move.aer_to_xyz(aer)
        np.testing.assert_almost_equal(xyz, xyz2)

        xyz = vec.vec([0, 1, -1])
        aer = move.xyz_to_aer(xyz)
        np.testing.assert_almost_equal(aer, vec.vec([0, -45, 2**0.5]))
        xyz2 = move.aer_to_xyz(aer)
        np.testing.assert_almost_equal(xyz, xyz2)

        xyz = vec.vec([1, 2, 3])
        aer = move.xyz_to_aer(xyz, angle='r')
        xyz2 = move.aer_to_xyz(aer, angle='r')
        np.testing.assert_almost_equal(xyz, xyz2)

        aer = move.xyz_to_aer(xyz, angle='d')
        xyz2 = move.aer_to_xyz(aer, angle='d')
        np.testing.assert_almost_equal(xyz, xyz2)
Exemple #15
0
 def __init__(self):
     self.target = vec.vec([50., 0.])
     self.pos = None
     self.vel = None
Exemple #16
0
    def test_detect(self):
        radar = Radar()
        uav = Target(pos=vec.vec([50, 50]))

        ret = radar.detect(uav)
        self.assertAlmostEqual(ret[0], 50 * 2 ** 0.5)
Exemple #17
0
 def encode_state(self, scene):
     """ 把场景翻译为输入."""
     jammer, uav = scene.find('jammer'), scene.find('target')
     d = vec.dist(uav.position, jammer.position)
     s = vec.dist(uav.velocity)
     return vec.vec([d, s])