Beispiel #1
0
 def setUp(self):
     default_syris_init()
     lens = Lens(3.0, f_number=1.4, focal_length=100.0 * q.mm)
     camera = Camera(1 * q.um, 0.1, 10, 1.0, 12, (64, 64))
     detector = Detector(None, lens, camera)
     ps = detector.pixel_size
     t = np.linspace(0, 1, 10) * q.mm
     x = t
     y = np.zeros(len(t))
     z = np.zeros(len(t))
     points = list(zip(x, y, z)) * q.mm
     mb_0 = MetaBall(
         Trajectory(points,
                    pixel_size=ps,
                    furthest_point=1 * q.um,
                    velocity=1 * q.mm / q.s),
         1 * q.um,
     )
     mb_1 = MetaBall(
         Trajectory(points,
                    pixel_size=ps,
                    furthest_point=1 * q.um,
                    velocity=2 * q.mm / q.s),
         1 * q.um,
     )
     self.experiment = Experiment([mb_0, mb_1], None, detector, 0 * q.m,
                                  None)
Beispiel #2
0
 def test_static_composite(self):
     ps = 1 * q.um
     traj = Trajectory([(0, 0, 0)] * q.m)
     mb_0 = MetaBall(traj, 10 * q.um)
     mb_1 = MetaBall(traj, 10 * q.um)
     comp = CompositeBody(traj, bodies=[mb_0, mb_1])
     self.assertEqual(np.inf * q.s, comp.get_next_time(0 * q.s, ps))
Beispiel #3
0
    def test_composite_furthest_point(self):
        mb_0 = MetaBall(Trajectory([(0, 0, 0)] * q.m), 1 * q.m)
        mb_1 = MetaBall(Trajectory([(0, 0, 0)] * q.m), 2 * q.m)
        composite = CompositeBody(Trajectory([(0, 0, 0)] * q.m), bodies=[mb_0, mb_1])

        # Metaball's furthest point is twice the radius
        self.assertAlmostEqual(4 * q.m, composite.furthest_point)
Beispiel #4
0
    def test_get_next_time(self):
        n = 100
        ps = 100 * q.mm
        psm = ps.simplified.magnitude
        p = np.linspace(0, np.pi, n)
        sin = np.sin(p) * 1e-3
        cos = np.cos(p) * 1e-3
        zeros = np.zeros(n)

        traj_m_0 = Trajectory(zip(p * 1e-3, zeros, zeros) * q.m,
                              velocity=1 * q.mm / q.s)
        traj_m_1 = Trajectory([(0, 0, 0)] * q.m)
        ball_0 = MetaBall(traj_m_0, .5 * q.m)
        ball_1 = MetaBall(traj_m_1, .5 * q.m)
        traj = Trajectory(zip(cos, sin, zeros) * q.m, velocity=1 * q.mm / q.s)
        comp = CompositeBody(traj, bodies=[ball_0, ball_1])
        dt = comp.get_maximum_dt(ps)

        # Normal trajectories
        # Test the beginning, middle and end because of time complexity
        for t_0 in [0 * q.s, comp.time / 2, comp.time - 10 * dt]:
            t_1 = comp.get_next_time(t_0, ps)

            d = comp.get_distance(t_0, t_1).simplified.magnitude
            np.testing.assert_almost_equal(psm, d)

        # Trajectories which sum up to no movement
        traj_m = Trajectory(zip(zeros, -p, zeros) * q.m,
                            velocity=1 * q.mm / q.s)
        ball = MetaBall(traj_m, .5 * q.m)
        traj = Trajectory(zip(p, zeros, zeros) * q.m, velocity=1 * q.mm / q.s)
        comp = CompositeBody(traj, bodies=[ball])

        self.assertEqual(np.inf * q.s, comp.get_next_time(0 * q.s, ps))
Beispiel #5
0
    def test_composite_bounding_box(self):
        mb_0 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 0.5 * q.mm)
        mb_1 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 1.5 * q.mm)
        composite = CompositeBody(Trajectory([(0, 0, 0)] * q.mm, ),
                                  bodies=[mb_0, mb_1])

        transformed_0 = self._get_moved_bounding_box(mb_0, 90 * q.deg)
        transformed_1 = self._get_moved_bounding_box(mb_1, -90 * q.deg)

        def get_concatenated(t_0, t_1, index):
            return np.concatenate((zip(*t_0)[index], zip(*t_1)[index])) * \
                t_0[0].units

        x_points = get_concatenated(transformed_0, transformed_1, 0)
        y_points = get_concatenated(transformed_0, transformed_1, 1)
        z_points = get_concatenated(transformed_0, transformed_1, 2)

        x_min_max = min(x_points), max(x_points)
        y_min_max = min(y_points), max(y_points)
        z_min_max = min(z_points), max(z_points)

        ground_truth = np.array(
            list(itertools.product(x_min_max, y_min_max, z_min_max))) * q.m

        np.testing.assert_almost_equal(ground_truth,
                                       composite.bounding_box.points)
Beispiel #6
0
    def setUp(self):
        syris.init(device_index=0)
        self.pixel_size = 1 * q.um

        control_points = get_linear_points(geom.X, start=(1, 1, 1))
        traj = Trajectory(control_points, velocity=1 * q.mm / q.s)
        self.metaball = MetaBall(traj, 1 * q.mm)
        self.metaball_2 = MetaBall(Trajectory(get_linear_points(geom.Z)), 2 * q.mm)
        self.composite = CompositeBody(traj, bodies=[self.metaball, self.metaball_2])
Beispiel #7
0
    def _run_parallel_or_opposite(self, sgn, gt):
        n = 64
        ps = 1 * q.um
        y = np.linspace(0, sgn * n, num=10)
        x = z = np.zeros(y.shape)
        traj = Trajectory(zip(x, y, z) * ps, velocity=ps / q.s, pixel_size=ps)
        mb = MetaBall(traj, n * ps / 16, orientation=geom.Y_AX)

        mb.move(0 * q.s)
        np.testing.assert_almost_equal(gt, mb.transform_matrix)
Beispiel #8
0
def create_metaballs(params, pixel_size):
    x, y, z, r = list(zip(*params))

    objects = b""
    metaballs = []
    for i in range(len(params)):
        c_points = [(x[i], y[i], z[i])] * q.um
        trajectory = Trajectory(c_points, pixel_size=pixel_size)
        metaball = MetaBall(trajectory, r[i] * q.um)
        metaball.move(0 * q.s)
        metaballs.append(metaball)
        objects += metaball.pack()

    return metaballs, objects
Beispiel #9
0
def create_metaballs(params):
    x, y, z, r = zip(*params)

    objects = ""
    metaballs = []
    for i in range(len(params)):
        c_points = [(x[i], y[i], z[i])] * q.um
        trajectory = Trajectory(c_points)
        metaball = MetaBall(trajectory, r[i] * q.um)
        metaball.move(0 * q.s)
        metaballs.append(metaball)
        objects += metaball.pack()

    return metaballs, objects
Beispiel #10
0
    def test_get_distance(self):
        n = 100
        ps = 100 * q.mm
        p = np.linspace(0, np.pi, n)
        sin = np.sin(p)
        cos = np.cos(p)
        zeros = np.zeros(n)

        # Simple body
        # -----------
        traj = Trajectory(zip(cos, sin, zeros) * q.m, velocity=1 * q.m / q.s)
        ball = MetaBall(traj, .5 * q.m)
        ball.bind_trajectory(ps)
        dist = ball.get_distance(0 * q.s, ball.trajectory.time)
        # Maximum along the x axis where the body travels 2 m by translation and rotates by 180
        # degrees compared to position at t0, so the rotational displacement is 2 * furthest point,
        # in this case 2 m, so altogether 4 m
        self.assertAlmostEqual(dist.simplified.magnitude, 4)

        # Composite body
        # --------------
        traj_m = Trajectory([(0, 0, 0)] * q.m)
        ball = MetaBall(traj_m, .5 * q.m)
        comp = CompositeBody(traj, bodies=[ball])
        comp.bind_trajectory(ps)

        d = comp.get_distance(0 * q.s, comp.time).simplified.magnitude
        # 2 by translation and 180 degrees means 2 * furthest
        gt = 2 * ball.furthest_point.simplified.magnitude + 2
        self.assertAlmostEqual(gt, d, places=4)

        d = comp.get_distance(0 * q.s, comp.time / 2).simplified.magnitude
        # 1 by translation by either x or y and sqrt(2) * furthest by rotation
        gt = 1 + comp.furthest_point.simplified.magnitude * np.sqrt(2)
        self.assertAlmostEqual(gt, d, places=4)
Beispiel #11
0
    def test_project_composite(self):
        n = 64
        shape = (n, n)
        ps = 1 * q.um
        x = np.linspace(0, n, num=10)
        y = z = np.zeros(x.shape)
        traj_x = Trajectory(zip(x, y, z) * ps, velocity=ps / q.s)
        traj_y = Trajectory(zip(y, x, z) * ps, velocity=ps / q.s)
        traj_xy = Trajectory(zip(n - x, x, z) * ps, velocity=ps / q.s)
        mb = MetaBall(traj_x, n * ps / 16)
        cube = make_cube() / q.m * 16 * ps / 4
        mesh = Mesh(cube, traj_xy)
        composite = CompositeBody(traj_y, bodies=[mb, mesh])
        composite.bind_trajectory(ps)

        composite.move(n / 2 * q.s)
        p = composite.project(shape, ps).get()
        composite.clear_transformation()

        # Compute
        composite.move(n / 2 * q.s)
        p_separate = (mb.project(shape, ps) + mesh.project(shape, ps)).get()

        np.testing.assert_almost_equal(p, p_separate)
Beispiel #12
0
    def test_composite_subbodies(self):
        m_1 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 1 * q.mm)
        m_2 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 2 * q.mm)
        m_3 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 3 * q.mm)
        m_4 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 4 * q.mm)
        m_5 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 5 * q.mm)
        m_6 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 6 * q.mm)
        m_7 = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 7 * q.mm)

        c_1 = CompositeBody(Trajectory([(0, 0, 0)] * q.mm),
                              bodies=[m_1, m_2])
        c_2 = CompositeBody(Trajectory([(0, 0, 0)] * q.mm),
                              bodies=[m_3])
        c_3 = CompositeBody(Trajectory([(0, 0, 0)] * q.mm),
                              bodies=[c_1, c_2])
        c_4 = CompositeBody(Trajectory([(0, 0, 0)] * q.mm),
                              bodies=[m_4, m_5])
        c_5 = CompositeBody(Trajectory([(0, 0, 0)] * q.mm),
                              bodies=[c_3, c_4, m_6, m_7])

        # Empty composit body
        CompositeBody(Trajectory([(0, 0, 0)] * q.mm))

        # Empty composite body.
        c_empty = CompositeBody(Trajectory([(0, 0, 0)] * q.mm))
        self.assertEqual(c_empty.direct_primitive_bodies, [])

        # Test direct subbodies
        self.assertEqual(c_5.direct_primitive_bodies, [m_6, m_7])
        self.assertEqual(c_3.direct_primitive_bodies, [])

        # Add self.
        with self.assertRaises(ValueError) as ctx:
            c_5.add(c_5)
        self.assertEqual("Cannot add self", ctx.exception.message)

        # Add already contained primitive body.
        with self.assertRaises(ValueError) as ctx:
            c_5.add(m_1)
        self.assertTrue(ctx.exception.message.endswith("already contained"))

        # Add already contained composite body.
        with self.assertRaises(ValueError) as ctx:
            c_5.add(c_2)
        self.assertTrue(ctx.exception.message.endswith("already contained"))

        # Test all subbodies.
        self.assertEqual(set(c_3.all_bodies),
                         set([m_1, m_2, m_3, c_1, c_2, c_3]))
        self.assertEqual(set(c_1.all_bodies), set([c_1, m_1, m_2]))
        self.assertEqual(c_empty.all_bodies, (c_empty,))
Beispiel #13
0
def _make_metaballs(args):
    metaballs = []
    radius = args.n / 16.
    shift = 3 * radius
    for i in range(-1, 2):
        dx = i * 3 * radius
        for j in range(-1, 2):
            index = (i + 1) * 3 + (j + 1)
            # Scale the metaballs to be [0.5, 1.0] radius large.
            coeff = 0.5 + 0.5 / 8 * index
            current_radius = coeff * radius
            dy = j * 3 * radius
            traj = Trajectory([(shift + dx, shift + dy, 0)] * args.ps,
                              pixel_size=args.ps)
            metaballs.append(MetaBall(traj, current_radius * args.ps))

    return metaballs
Beispiel #14
0
def create_sample(n, ps, radius=None, velocity=None, x_ends=None, y_ends=None):
    """Crete a metaball with a sine trajectory."""
    fov = n * ps
    if radius is None:
        radius = n / 16 * ps
    if x_ends is None:
        radius_m = radius.simplified.magnitude
        fov_m = fov.simplified.magnitude
        x_ends = (radius_m, fov_m - radius_m) * q.m
    if y_ends is None:
        y_ends = (n / 4, 3 * n / 4) * ps

    cp = make_sine(n=32, x_ends=x_ends, y_ends=y_ends)
    if velocity is None:
        velocity = 1 * q.mm / q.s
    tr = Trajectory(cp, velocity=velocity)
    mb = MetaBall(tr, radius)

    return mb
Beispiel #15
0
def main():
    args = parse_args()
    syris.init(device_index=0)
    n = 512
    shape = (n, n)
    ps = 1 * q.um

    x = np.linspace(0, n, num=10)
    y = z = np.zeros(x.shape)
    traj_x = Trajectory(zip(x, y, z) * ps, velocity=ps / q.s)
    traj_y = Trajectory(zip(y, x, z) * ps, velocity=ps / q.s)
    traj_xy = Trajectory(zip(n - x, x, z) * ps, velocity=ps / q.s)
    mb = MetaBall(traj_x, n * ps / 16)
    cube = make_cube() / q.m * 16 * ps
    mesh = Mesh(cube, traj_xy)
    composite = CompositeBody(traj_y, bodies=[mb, mesh])
    composite.bind_trajectory(ps)
    t = args.t * n * q.s

    composite.move(t)
    p = composite.project(shape, ps).get()
    show(p, title='Projection')

    plt.show()
Beispiel #16
0
    def test_metaball_bounding_box(self):
        """Bounding box moves along with its body."""
        mb = MetaBall(Trajectory([(0, 0, 0)] * q.mm), 0.5 * q.mm)
        transformed = self._get_moved_bounding_box(mb, 90 * q.deg)

        np.testing.assert_almost_equal(transformed, mb.bounding_box.points)