예제 #1
0
def parse_sharptrack(
    atlas,
    probe_points_file,
    name,
    color_by_region=True,
    color="salmon",
    radius=30,
    probe_color="blackboard",
    probe_radius=15,
    probe_alpha=1,
):
    """
            Visualises the position of an implanted probe in the brain. 
            Uses the location of points along the probe extracted with SharpTrack
            [https://github.com/cortex-lab/allenCCF].
            It renders the position of points along the probe and a line fit through them.
            Code contributed by @tbslv on github. 
        """

    # Points params
    params = dict(
        color_by_region=True,
        color="salmon",
        radius=30,
        res=12,
    )

    # Get the position of probe points and render
    probe_points_df = get_probe_points_from_sharptrack(probe_points_file)

    # Fit a line through the points [adapted from SharpTrack by @tbslv]
    r0 = np.mean(probe_points_df.values, axis=0)
    coords = probe_points_df.values - r0
    U, S, V = np.linalg.svd(coords)
    direction = V.T[:, 0]

    # Find intersection with brain surface
    root_mesh = atlas._get_structure_mesh("root")
    p0 = direction * np.array([-1]) + r0
    p1 = (direction * np.array([-15000]) + r0
          )  # end point way outside of brain, on probe trajectory though
    pts = root_mesh.intersectWithLine(p0, p1)

    # Define top/bottom coordinates to render as a cylinder
    top_coord = pts[0]
    length = np.sqrt(np.sum((probe_points_df.values[-1] - top_coord)**2))
    bottom_coord = top_coord + direction * length

    # Render probe as a cylinder
    probe = Cylinder(
        [top_coord, bottom_coord],
        r=probe_radius,
        alpha=probe_alpha,
        c=probe_color,
    )
    probe.name = name

    return probe_points_df, params, probe, color
예제 #2
0
 def makeHandActor(self, f=1):
     a1, a2, a3, c = (10 * f, 0, 0), (0, 7 * f, 0), (0, 0, 3 * f), (.7, 0.3,
                                                                    0.3)
     palm = Ellipsoid(pos=(0, -3, 0),
                      axis1=a1,
                      axis2=a2,
                      axis3=a3,
                      alpha=0.6,
                      c=c)
     wrist = Box(pos=(0, -9, 0),
                 length=6 * f,
                 width=5,
                 height=2,
                 alpha=0.4,
                 c=c)
     arm = Assembly([palm, wrist])
     f1 = Cylinder((-2, 1.5, 0), axis=(0, 1, 0), height=5, r=.8 * f, c=c)
     f2 = Cylinder((-1, 3, 0), axis=(0, 1, 0), height=6, r=.7 * f, c=c)
     f3 = Cylinder((0, 4, 0), axis=(0, 1, 0), height=6.2, r=.75 * f, c=c)
     f4 = Cylinder((1, 3.5, 0), axis=(0, 1, 0), height=6.1, r=.7 * f, c=c)
     f5 = Cylinder((2, 2, 0), axis=(0, 1, 0), height=5, r=.6 * f, c=c)
     self.vp += [arm, f1, f2, f3, f4, f5]  # add actors to internal list
     return [arm, f1, f2, f3, f4, f5]
예제 #3
0
def demo3d_hanoi(**kwargs):
    nr_disks = kwargs.get("nr_disks", 5)
    interactive = kwargs.get("interactive", 1)

    hanoi = Hanoi(nr_disks)
    tower_states = list([hanoi.towers])
    for _ in hanoi.moves():
        tower_states.append(hanoi.towers)

    vp = Plotter(axes=0, interactive=0, bg="w", size=(800, 600))
    vp.camera.SetPosition([18.5, -20.7, 7.93])
    vp.camera.SetFocalPoint([3.0, 0.0, 2.5])
    vp.camera.SetViewUp([-0.1, +0.17, 0.977])

    cols = makePalette("red", "blue", hanoi.nr_disks + 1, hsv=True)
    disks = {
        hanoi.nr_disks - i: Cylinder(pos=[0, 0, 0],
                                     r=0.2 * (hanoi.nr_disks - i + 1),
                                     c=cols[i])
        for i in range(hanoi.nr_disks)
    }
    for k in disks:
        vp += disks[k]
    vp += Box(pos=(3.0, 0, -.5), length=12.0, width=4.0, height=0.1)
    vp.show(zoom=1.2)

    printc("\n Press q to continue, Esc to exit. ", c="y", invert=1)
    pb = ProgressBar(0, len(tower_states), 1, c="b", ETA=False)
    for t in pb.range():
        pb.print()
        state = tower_states[t]
        for tower_nr in range(3):
            for i, disk in enumerate(state[tower_nr]):
                disks[disk].pos([3 * tower_nr, 0, i + 0.5])
        vp.show(resetcam=0, interactive=interactive, rate=10)
    vp.show(resetcam=0, interactive=1)
예제 #4
0
x_dot = np.zeros(N + 1)  # velocities
y_dot = np.zeros(N + 1)

for k in range(1, N + 1):
    alpha = np.pi / 5 * k / 10
    bob_x.append(bob_x[k - 1] + np.cos(alpha) + np.random.normal(0, 0.1))
    bob_y.append(bob_y[k - 1] + np.sin(alpha) + np.random.normal(0, 0.1))

# Create the bobs
plt = Plotter(title="Multiple Pendulum", axes=0, interactive=0, bg2='ly')
plt += Box(pos=(0, -5, 0), length=12, width=12, height=0.7, c="k").wireframe(1)
sph = Sphere(pos=(bob_x[0], bob_y[0], 0), r=R / 2, c="gray")
plt += sph
bob = [sph]
for k in range(1, N + 1):
    c = Cylinder(pos=(bob_x[k], bob_y[k], 0), r=R, height=0.3, c=k)
    plt += c
    bob.append(c)

# Create the springs out of N links
link = [None] * N
for k in range(N):
    p0 = bob[k].pos()
    p1 = bob[k + 1].pos()
    link[k] = Spring(p0, p1, thickness=0.015, r=R / 3, c="gray")
    plt += link[k]

# Create some auxiliary variables
x_dot_m = np.zeros(N + 1)
y_dot_m = np.zeros(N + 1)
dij = np.zeros(N + 1)  # array with distances to previous bob
예제 #5
0
            yield self.move_disk(from_tower, to_tower)
            yield from self.move_disks(n - 1, helper, to_tower)

    def moves(self):
        yield from self.move_disks(self.nr_disks, 0, 1)


nr_disks = 5
hanoi = Hanoi(nr_disks)

tower_states = list([hanoi.towers])
for _ in hanoi.moves():
    tower_states.append(hanoi.towers)

disks = {
    hanoi.nr_disks - i: Cylinder(r=0.2 * (hanoi.nr_disks - i + 1), c=i)
    for i in range(hanoi.nr_disks)
}

plt = Plotter(interactive=False, size=(800, 600), bg='wheat', bg2='lb')
plt += disks.values()
plt += Box(pos=(3, 0, -0.5), size=(12, 4, 0.1))
cam = dict(
    pos=(14.60, -20.56, 7.680),
    focalPoint=(3.067, 0.5583, 1.910),
    viewup=(-0.1043, 0.2088, 0.9724),
)
plt.show(camera=cam)

pb = ProgressBar(0, len(tower_states), 1, c="y")
for t in pb.range():
예제 #6
0
"""The butterfly effect with cylindrical mirrors and a laser"""
# Original idea from "The Action Lab": https://www.youtube.com/watch?v=kBow0kTVn3s
#
from vedo import Plotter, Grid, Cylinder, merge
from optics_base import Ray, Mirror, Detector  # see file ./optics_base.py

grid = Grid(resx=3, resy=4)  # pick a few points in space to place cylinders
pts = grid.points().tolist() + grid.cellCenters().tolist()

# Create the mirror by merging many (y-scaled) cylinders into a single mesh object
cyls = [
    Cylinder(p, r=0.065, height=0.2, res=720).scale([1, 1.5, 1]) for p in pts
]
mirror = Mirror(merge(cyls)).color("silver")

# Create a detector surface as a thin cylinder surrounding the mirror
sd = Cylinder(r=1, height=0.3, cap=False).cutWithPlane([0, -0.95, 0],
                                                       normal='y')
detector = Detector(sd)


def slider(widget, event):  ### callback to shift the beam along x
    dx = widget.GetRepresentation().GetValue()
    ray = Ray([dx, -1.2, -0.1], direction=(0, 1, 0.02))
    ray.maxiterations = 1000  # max nr. of reflections
    ray.trace([mirror, detector])  # cumpute trajectory
    detector.count().cmap("Reds", on='cells', vmax=10)
    line = ray.asLine().lineWidth(4).c('green5')
    if plt.actors[-1].name == "Line":
        plt.pop()  # remove the last Line
    plt.add(line)  # add the new one