Example #1
0
    def plot(self,
             plot_mesh=True,
             plot_voxels=True,
             width=800,
             height=600,
             voxel_count_offset=0,
             voxel_limit=None,
             use_centroids_instead=False,
             scaling=1,
             mesh_color='red',
             **kwargs):
        """
        This method needs better documentation.

        :param **kwargs: Is used in ipyvolume.pylab.scatter() or ipyvolume.pylab.plot() depending on whether use_centroids_instead
        is set to true or not.
        """
        if plot_mesh:
            if self.triangles is None:
                raise ValueError(
                    "There is no triangle data stored for this mesh!")
            else:
                fig = p3.figure(width=width, height=height)
                p3.plot_trisurf(*self.vertices.T * scaling,
                                self.triangles,
                                color=mesh_color)
                if plot_voxels:
                    self.voxels.plot(
                        width=width,
                        height=height,
                        voxel_count_offset=voxel_count_offset,
                        voxel_limit=voxel_limit,
                        use_centroids_instead=use_centroids_instead,
                        ipyvol_fig=fig,
                        scaling=scaling,
                        **kwargs)
                else:
                    p3.squarelim()
                    p3.show()
        elif plot_voxels:
            try:
                fig = p3.figure(width=width, height=height)
                self.voxels.plot(width=width,
                                 height=height,
                                 voxel_count_offset=voxel_count_offset,
                                 voxel_limit=voxel_limit,
                                 use_centroids_instead=use_centroids_instead,
                                 ipyvol_fig=fig,
                                 scaling=scaling,
                                 **kwargs)
            except AttributeError:
                raise AttributeError(
                    "This object does not have a Voxels object, you must initialize the voxel mesh with a Voxels object or run voxelize() to generate new voxels."
                )
Example #2
0
    def plot(self,
             width=800,
             height=600,
             voxel_count_offset=0,
             voxel_limit=None,
             use_centroids_instead=False,
             ipyvol_fig=None,
             scaling=1,
             **kwargs):
        """
        This method needs better documentation.

        :param **kwargs: Is used in ipyvolume.pylab.scatter() or ipyvolume.pylab.plot() depending on whether use_centroids_instead
        is set to true or not.
        """
        if ipyvol_fig is None:
            p3.figure(width=width, height=height)
        else:
            p3.figure(ipyvol_fig)
        voxels_length = len(self)
        if voxel_count_offset >= voxels_length:
            raise ValueError(
                "voxel_count_offset is greater than the number of voxels!")
        else:
            if voxel_limit is None:
                n_voxels_to_plot = len(self)
            else:
                n_voxels_to_plot = voxel_count_offset + voxel_limit
                if n_voxels_to_plot > voxels_length:
                    n_voxels_to_plot = len(self)

        if use_centroids_instead:
            if 'marker' not in kwargs:
                kwargs['marker'] = 'sphere'
            if 'color' not in kwargs:
                kwargs['color'] = 'blue'
            if 'size' not in kwargs:
                kwargs['size'] = 0.5
            p3.scatter(
                *self.centroids[voxel_count_offset:n_voxels_to_plot].T *
                scaling, **kwargs)
        else:
            voxel_count_offset *= 18
            n_voxels_to_plot *= 18
            drawable_bboxes = self.drawable_bboxes[
                voxel_count_offset:n_voxels_to_plot]
            p3.plot(*drawable_bboxes.T * scaling, **kwargs)

        p3.squarelim()
        p3.show()
Example #3
0
def klein_bottle(draw=True, show=True, figure8=False, endpoint=True, uv=True, wireframe=False, texture=None, both=False, interval=1000):
    """Show one or two Klein bottles"""
    import ipyvolume.pylab as p3
    # http://paulbourke.net/geometry/klein/
    u = np.linspace(0, 2 * pi, num=40, endpoint=endpoint)
    v = np.linspace(0, 2 * pi, num=40, endpoint=endpoint)
    u, v = np.meshgrid(u, v)
    if both:
        x1, y1, z1, u1, v1 = klein_bottle(endpoint=endpoint, draw=False, show=False)
        x2, y2, z2, u2, v2 = klein_bottle(endpoint=endpoint, draw=False, show=False, figure8=True)
        x = [x1, x2]
        y = [y1, y2]
        z = [z1, z2]
    else:
        if figure8:
            #u -= np.pi
            #v -= np.pi
            a = 2
            s = 5
            x = s * (a + cos(u / 2) * sin(v) - sin(u / 2) * sin(2 * v)/2) * cos(u)
            y = s * (a + cos(u / 2) * sin(v) - sin(u / 2) * sin(2 * v)/2) * sin(u)
            z = s * (sin(u / 2) * sin(v) + cos(u / 2) * sin(2 * v)/2)
        else:
            r = 4 * (1 - cos(u) / 2)
            x = 6 * cos(u) * (1 + sin(u)) \
                + r * cos(u) * cos(v) * (u < pi) \
                + r * cos(v + pi) * (u >= pi)
            y = 16 * sin(u) + r * sin(u) * cos(v) * (u < pi)
            z = r * sin(v)
    if draw:
        if texture:
            uv = True
        if uv:
            mesh = p3.plot_mesh(x, y, z, wrapx=not endpoint, wrapy=not endpoint, u=u/(2*np.pi), v=v/(2*np.pi), wireframe=wireframe, texture=texture)
        else:
            mesh = p3.plot_mesh(x, y, z, wrapx=not endpoint, wrapy=not endpoint, wireframe=wireframe, texture=texture)
        if show:
            if both:
                p3.animation_control(mesh, interval=interval)
            p3.squarelim()
            p3.show()
        return mesh
    else:
        return x, y, z, u, v