Пример #1
0
 def get_proj(self):
     """Create the projection matrix from the current viewing
     position.
     elev stores the elevation angle in the z plane
     azim stores the azimuth angle in the x,y plane
     dist is the distance of the eye viewing point from the object
     point.
     """
     relev, razim = np.pi * self.elev/180, np.pi * self.azim/180
     xmin, xmax = self.get_xlim3d()
     ymin, ymax = self.get_ylim3d()
     zmin, zmax = self.get_zlim3d()
     worldM = proj3d.world_transformation(xmin, xmax,
                                          ymin, ymax,
                                          zmin, zmax)
     R = np.array([0.5, 0.5, 0.5])
     xp = R[0] + np.cos(razim) * np.cos(relev) * self.dist
     yp = R[1] + np.sin(razim) * np.cos(relev) * self.dist
     zp = R[2] + np.sin(relev) * self.dist
     E = np.array((xp, yp, zp))
     self.eye = E
     self.vvec = R - E
     self.vvec = self.vvec / proj3d.mod(self.vvec)
     if abs(relev) > np.pi/2:
         V = np.array((0, 0, -1))
     else:
         V = np.array((0, 0, 1))
     zfront, zback = -self.dist, self.dist
     viewM = proj3d.view_transformation(E, R, V)
     perspM = proj3d.persp_transformation(zfront, zback)
     M0 = np.dot(viewM, worldM)
     M = np.dot(perspM, M0)
     return M
Пример #2
0
    def _shade_colors(self, color, normals):
        """
        Shade *color* using normal vectors given by *normals*.
        *color* can also be an array of the same length as *normals*.
        """

        shade = []
        for n in normals:
            n = n / proj3d.mod(n)
            shade.append(np.dot(n, [-1, -1, 0.5]))

        shade = np.array(shade)
        mask = ~np.isnan(shade)

        if len(shade[mask]) > 0:
            norm = Normalize(min(shade[mask]), max(shade[mask]))
            if art3d.iscolor(color):
                color = color.copy()
                color[3] = 1
                colors = [color * (0.5 + norm(v) * 0.5) for v in shade]
            else:
                colors = [np.array(colorConverter.to_rgba(c)) * (0.5 + norm(v) * 0.5) for c, v in zip(color, shade)]
        else:
            colors = color.copy()

        return colors
Пример #3
0
    def _shade_colors(self, color, normals):
        '''
        Shade *color* using normal vectors given by *normals*.
        *color* can also be an array of the same length as *normals*.
        '''

        shade = []
        for n in normals:
            n = n / proj3d.mod(n)
            shade.append(np.dot(n, [-1, -1, 0.5]))

        shade = np.array(shade)
        mask = ~np.isnan(shade)

        if len(shade[mask]) > 0:
            norm = Normalize(min(shade[mask]), max(shade[mask]))
            if art3d.iscolor(color):
                color = color.copy()
                color[3] = 1
                colors = [color * (0.5 + norm(v) * 0.5) for v in shade]
            else:
                colors = [np.array(colorConverter.to_rgba(c)) * \
                            (0.5 + norm(v) * 0.5) \
                            for c, v in zip(color, shade)]
        else:
            colors = color.copy()

        return colors
Пример #4
0
    def plot_surface(self, X, Y, Z, *args, **kwargs):
        had_data = self.has_data()

        rows, cols = Z.shape
        tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
        rstride = cbook.popd(kwargs, 'rstride', 10)
        cstride = cbook.popd(kwargs, 'cstride', 10)
        #
        polys = []
        boxes = []
        for rs in np.arange(0, rows - 1, rstride):
            for cs in np.arange(0, cols - 1, cstride):
                ps = []
                corners = []
                for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
                    ztop = a[rs][cs:min(cols, cs + cstride + 1)]
                    zleft = ta[min(cols - 1, cs +
                                   cstride)][rs:min(rows, rs + rstride + 1)]
                    zbase = a[min(rows - 1, rs +
                                  rstride)][cs:min(cols, cs + cstride + 1):]
                    zbase = zbase[::-1]
                    zright = ta[cs][rs:min(rows, rs + rstride + 1):]
                    zright = zright[::-1]
                    corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
                    z = np.concatenate((ztop, zleft, zbase, zright))
                    ps.append(z)
                boxes.append(map(np.array, zip(*corners)))
                polys.append(zip(*ps))
        #
        lines = []
        shade = []
        for box in boxes:
            n = proj3d.cross(box[0] - box[1], box[0] - box[2])
            n = n / proj3d.mod(n) * 5
            shade.append(np.dot(n, [-1, -1, 0.5]))
            lines.append((box[0], n + box[0]))
        #
        color = np.array([0, 0, 1, 1])
        norm = Normalize(min(shade), max(shade))
        colors = [color * (0.5 + norm(v) * 0.5) for v in shade]
        for c in colors:
            c[3] = 1
        polyc = art3d.Poly3DCollection(polys,
                                       facecolors=colors,
                                       *args,
                                       **kwargs)
        polyc._zsort = 1
        self.add_collection(polyc)
        #
        self.auto_scale_xyz(X, Y, Z, had_data)
        return polyc
Пример #5
0
 def _shade_colors(self, color, normals):
     shade = []
     for n in normals:
         n = n / proj3d.mod(n) * 5
         shade.append(np.dot(n, [-1, -1, 0.5]))
     shade = np.array(shade)
     mask = ~np.isnan(shade)
 	if len(shade[mask]) > 0:
        norm = Normalize(min(shade[mask]), max(shade[mask]))
        color = color.copy()
        color[3] = 1
        colors = [color * (0.5 + norm(v) * 0.5) for v in shade]
     else:
        colors = color.copy()
     return colors
Пример #6
0
    def get_proj(self):
        """Create the projection matrix from the current viewing
        position.

        elev stores the elevation angle in the z plane
        azim stores the azimuth angle in the x,y plane

        dist is the distance of the eye viewing point from the object
        point.

        """
        relev,razim = npy.pi * self.elev/180, npy.pi * self.azim/180

        xmin,xmax = self.get_w_xlim()
        ymin,ymax = self.get_w_ylim()
        zmin,zmax = self.get_w_zlim()

        # transform to uniform world coordinates 0-1.0,0-1.0,0-1.0
        worldM = proj3d.world_transformation(xmin,xmax,
                                             ymin,ymax,
                                             zmin,zmax)

        # look into the middle of the new coordinates
        R = npy.array([0.5,0.5,0.5])
        #
        xp = R[0] + npy.cos(razim)*npy.cos(relev)*self.dist
        yp = R[1] + npy.sin(razim)*npy.cos(relev)*self.dist
        zp = R[2] + npy.sin(relev)*self.dist

        E = npy.array((xp, yp, zp))
        #
        self.eye = E
        self.vvec = R - E
        self.vvec = self.vvec / proj3d.mod(self.vvec)

        if abs(relev) > npy.pi/2:
            # upside down
            V = npy.array((0,0,-1))
        else:
            V = npy.array((0,0,1))
        zfront,zback = -self.dist,self.dist

        viewM = proj3d.view_transformation(E,R,V)
        perspM = proj3d.persp_transformation(zfront,zback)
        M0 = npy.dot(viewM,worldM)
        M = npy.dot(perspM,M0)
        return M
Пример #7
0
    def plot_surface(self, X, Y, Z, *args, **kwargs):
        had_data = self.has_data()

        rows, cols = Z.shape
        tX,tY,tZ = npy.transpose(X), npy.transpose(Y), npy.transpose(Z)
        rstride = cbook.popd(kwargs, 'rstride', 10)
        cstride = cbook.popd(kwargs, 'cstride', 10)
        #
        polys = []
        boxes = []
        for rs in npy.arange(0,rows-1,rstride):
            for cs in npy.arange(0,cols-1,cstride):
                ps = []
                corners = []
                for a,ta in [(X,tX),(Y,tY),(Z,tZ)]:
                    ztop = a[rs][cs:min(cols,cs+cstride+1)]
                    zleft = ta[min(cols-1,cs+cstride)][rs:min(rows,rs+rstride+1)]
                    zbase = a[min(rows-1,rs+rstride)][cs:min(cols,cs+cstride+1):]
                    zbase = zbase[::-1]
                    zright = ta[cs][rs:min(rows,rs+rstride+1):]
                    zright = zright[::-1]
                    corners.append([ztop[0],ztop[-1],zbase[0],zbase[-1]])
                    z = npy.concatenate((ztop,zleft,zbase,zright))
                    ps.append(z)
                boxes.append(map(npy.array,zip(*corners)))
                polys.append(zip(*ps))
        #
        lines = []
        shade = []
        for box in boxes:
            n = proj3d.cross(box[0]-box[1],
                         box[0]-box[2])
            n = n/proj3d.mod(n)*5
            shade.append(npy.dot(n,[-1,-1,0.5]))
            lines.append((box[0],n+box[0]))
        #
        color = npy.array([0,0,1,1])
        norm = Normalize(min(shade),max(shade))
        colors = [color * (0.5+norm(v)*0.5) for v in shade]
        for c in colors: c[3] = 1
        polyc = art3d.Poly3DCollection(polys, facecolors=colors, *args, **kwargs)
        polyc._zsort = 1
        self.add_collection(polyc)
        #
        self.auto_scale_xyz(X,Y,Z, had_data)
        return polyc
Пример #8
0
    def _shade_colors(self, color, normals):
        shade = []
        for n in normals:
            n = n / proj3d.mod(n) * 5
            shade.append(np.dot(n, [-1, -1, 0.5]))

        shade = np.array(shade)
        mask = ~np.isnan(shade)

        if len(shade[mask]) > 0:
            norm = Normalize(min(shade[mask]), max(shade[mask]))
            color = color.copy()
            color[3] = 1
            colors = [color * (0.5 + norm(v) * 0.5) for v in shade]
        else:
            colors = color.copy()

        return colors
Пример #9
0
    def _shade_colors(self, color, normals):
        '''
        Shade *color* using normal vectors given by *normals*.
        *color* can also be an array of the same length as *normals*.
        '''

        shade = np.array([np.dot(n / proj3d.mod(n), [-1, -1, 0.5])
                          for n in normals])
        mask = ~np.isnan(shade)

        if len(shade[mask]) > 0:
            norm = Normalize(min(shade[mask]), max(shade[mask]))
            color = colorConverter.to_rgba_array(color)
            # shape of color should be (M, 4) (where M is number of faces)
            # shape of shade should be (M,)
            # colors should have final shape of (M, 4)
            alpha = color[:, 3]
            colors = (0.5 + norm(shade)[:, np.newaxis] * 0.5) * color
            colors[:, 3] = alpha
        else:
            colors = color.copy()

        return colors