From: <md...@us...> - 2009-08-07 18:31:59
|
Revision: 7418 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7418&view=rev Author: mdboom Date: 2009-08-07 18:31:45 +0000 (Fri, 07 Aug 2009) Log Message: ----------- Experimental Gouraud shading support in the Agg backend. Modified Paths: -------------- trunk/matplotlib/examples/pylab_examples/quadmesh_demo.py trunk/matplotlib/lib/matplotlib/axes.py trunk/matplotlib/lib/matplotlib/backend_bases.py trunk/matplotlib/lib/matplotlib/backends/backend_agg.py trunk/matplotlib/lib/matplotlib/collections.py trunk/matplotlib/src/_backend_agg.cpp Modified: trunk/matplotlib/examples/pylab_examples/quadmesh_demo.py =================================================================== --- trunk/matplotlib/examples/pylab_examples/quadmesh_demo.py 2009-08-07 17:02:28 UTC (rev 7417) +++ trunk/matplotlib/examples/pylab_examples/quadmesh_demo.py 2009-08-07 18:31:45 UTC (rev 7418) @@ -28,7 +28,7 @@ fig = figure() ax = fig.add_subplot(121) ax.set_axis_bgcolor("#bdb76b") -ax.pcolormesh(Qx,Qz,Z) +ax.pcolormesh(Qx,Qz,Z, shading='gouraud') ax.set_title('Without masked values') ax = fig.add_subplot(122) Modified: trunk/matplotlib/lib/matplotlib/axes.py =================================================================== --- trunk/matplotlib/lib/matplotlib/axes.py 2009-08-07 17:02:28 UTC (rev 7417) +++ trunk/matplotlib/lib/matplotlib/axes.py 2009-08-07 18:31:45 UTC (rev 7418) @@ -6543,7 +6543,7 @@ and max of the color array *C* is used. If you pass a *norm* instance, *vmin* and *vmax* will be ignored. - *shading*: [ 'flat' | 'faceted' ] + *shading*: [ 'flat' | 'faceted' | 'gouraud' ] If 'faceted', a black grid is drawn around each rectangle; if 'flat', edges are not drawn. Default is 'flat', contrary to Matlab(TM). @@ -6584,7 +6584,7 @@ cmap = kwargs.pop('cmap', None) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) - shading = kwargs.pop('shading', 'flat') + shading = kwargs.pop('shading', 'flat').lower() edgecolors = kwargs.pop('edgecolors', 'None') antialiased = kwargs.pop('antialiased', False) @@ -6592,8 +6592,11 @@ Ny, Nx = X.shape # convert to one dimensional arrays - C = ma.ravel(C[0:Ny-1, 0:Nx-1]) # data point in each cell is value at - # lower left corner + if shading != 'gouraud': + C = ma.ravel(C[0:Ny-1, 0:Nx-1]) # data point in each cell is value at + # lower left corner + else: + C = C.ravel() X = X.ravel() Y = Y.ravel() @@ -6608,7 +6611,7 @@ collection = mcoll.QuadMesh( Nx - 1, Ny - 1, coords, showedges, - antialiased=antialiased) # kwargs are not used + antialiased=antialiased, shading=shading) # kwargs are not used collection.set_alpha(alpha) collection.set_array(C) if norm is not None: assert(isinstance(norm, mcolors.Normalize)) Modified: trunk/matplotlib/lib/matplotlib/backend_bases.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backend_bases.py 2009-08-07 17:02:28 UTC (rev 7417) +++ trunk/matplotlib/lib/matplotlib/backend_bases.py 2009-08-07 18:31:45 UTC (rev 7418) @@ -166,6 +166,14 @@ gc, master_transform, paths, [], offsets, offsetTrans, facecolors, edgecolors, linewidths, [], [antialiased], [None]) + def draw_gouraud_triangle(self, gc, points, colors, transform): + """ + Draw a Gouraud-shaded triangle. + + EXPERIMENTAL + """ + raise NotImplementedError + def _iter_collection_raw_paths(self, master_transform, paths, all_transforms): """ Modified: trunk/matplotlib/lib/matplotlib/backends/backend_agg.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2009-08-07 17:02:28 UTC (rev 7417) +++ trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2009-08-07 18:31:45 UTC (rev 7418) @@ -63,6 +63,7 @@ self.draw_markers = self._renderer.draw_markers self.draw_path_collection = self._renderer.draw_path_collection self.draw_quad_mesh = self._renderer.draw_quad_mesh + self.draw_gouraud_triangle = self._renderer.draw_gouraud_triangle self.draw_image = self._renderer.draw_image self.copy_from_bbox = self._renderer.copy_from_bbox self.tostring_rgba_minimized = self._renderer.tostring_rgba_minimized Modified: trunk/matplotlib/lib/matplotlib/collections.py =================================================================== --- trunk/matplotlib/lib/matplotlib/collections.py 2009-08-07 17:02:28 UTC (rev 7417) +++ trunk/matplotlib/lib/matplotlib/collections.py 2009-08-07 18:31:45 UTC (rev 7418) @@ -1073,14 +1073,18 @@ coordinates of the vertex at mesh coordinates (0, 0), then the one at (0, 1), then at (0, 2) .. (0, meshWidth), (1, 0), (1, 1), and so on. + + *shading* may be 'flat', 'faceted' or 'gouraud' """ - def __init__(self, meshWidth, meshHeight, coordinates, showedges, antialiased=True): + def __init__(self, meshWidth, meshHeight, coordinates, showedges, + antialiased=True, shading='flat'): Collection.__init__(self) self._meshWidth = meshWidth self._meshHeight = meshHeight self._coordinates = coordinates self._showedges = showedges self._antialiased = antialiased + self._shading = shading self._bbox = transforms.Bbox.unit() self._bbox.update_from_data_xy(coordinates.reshape( @@ -1125,6 +1129,46 @@ points = points.reshape((meshWidth * meshHeight, 5, 2)) return [Path(x) for x in points] + def convert_mesh_to_triangles(self, meshWidth, meshHeight, coordinates): + """ + Converts a given mesh into a sequence of triangles, each point + with its own color + :class:`matplotlib.path.Path` objects for easier rendering by + backends that do not directly support quadmeshes. + + This function is primarily of use to backend implementers. + """ + Path = mpath.Path + + if ma.isMaskedArray(coordinates): + c = coordinates.data + else: + c = coordinates + + triangles = np.concatenate(( + c[0:-1, 0:-1], + c[0:-1, 1: ], + c[1: , 1: ], + c[1: , 1: ], + c[1: , 0:-1], + c[0:-1, 0:-1] + ), axis=2) + triangles = triangles.reshape((meshWidth * meshHeight * 2, 3, 2)) + + c = self.get_facecolor().reshape((meshHeight + 1, meshWidth + 1, 4)) + colors = np.concatenate(( + c[0:-1, 0:-1], + c[0:-1, 1: ], + c[1: , 1: ], + c[1: , 1: ], + c[1: , 0:-1], + c[0:-1, 0:-1] + ), axis=2) + + colors = colors.reshape((meshWidth * meshHeight * 2, 3, 4)) + + return triangles, colors + def get_datalim(self, transData): return self._bbox @@ -1166,10 +1210,17 @@ gc.set_clip_rectangle(self.get_clip_box()) gc.set_clip_path(self.get_clip_path()) - renderer.draw_quad_mesh( - gc, transform.frozen(), self._meshWidth, self._meshHeight, - coordinates, offsets, transOffset, self.get_facecolor(), - self._antialiased, self._showedges) + if self._shading == 'gouraud': + triangles, colors = self.convert_mesh_to_triangles( + self._meshWidth, self._meshHeight, coordinates) + check = {} + for tri, col in zip(triangles, colors): + renderer.draw_gouraud_triangle(gc, tri, col, transform.frozen()) + else: + renderer.draw_quad_mesh( + gc, transform.frozen(), self._meshWidth, self._meshHeight, + coordinates, offsets, transOffset, self.get_facecolor(), + self._antialiased, self._showedges) renderer.close_group(self.__class__.__name__) Modified: trunk/matplotlib/src/_backend_agg.cpp =================================================================== --- trunk/matplotlib/src/_backend_agg.cpp 2009-08-07 17:02:28 UTC (rev 7417) +++ trunk/matplotlib/src/_backend_agg.cpp 2009-08-07 18:31:45 UTC (rev 7418) @@ -1464,11 +1464,14 @@ typedef agg::span_allocator<color_t> span_alloc_t; //segments, trans, clipbox, colors, linewidths, antialiaseds - GCAgg gc(args[0], dpi); - Py::Object points_obj = args[1]; - Py::Object colors_obj = args[2]; - agg::trans_affine master_transform = py_to_agg_transformation_matrix(args[3].ptr()); + GCAgg gc(args[0], dpi); + Py::Object points_obj = args[1]; + Py::Object colors_obj = args[2]; + agg::trans_affine trans = py_to_agg_transformation_matrix(args[3].ptr()); + trans *= agg::trans_affine_scaling(1.0, -1.0); + trans *= agg::trans_affine_translation(0.0, (double)height); + PyArrayObject* points = (PyArrayObject*)PyArray_ContiguousFromAny (points_obj.ptr(), PyArray_DOUBLE, 2, 2); if (!points || @@ -1489,7 +1492,7 @@ for (int i = 0; i < 6; i += 2) { tpoints[i] = opoints[i]; tpoints[i+1] = opoints[i+1]; - master_transform.transform(&tpoints[i], &tpoints[i+1]); + trans.transform(&tpoints[i], &tpoints[i+1]); } span_alloc_t span_alloc; @@ -1503,7 +1506,7 @@ tpoints[0], tpoints[1], tpoints[2], tpoints[3], tpoints[4], tpoints[5], - 1.0); + 0.5); theRasterizer.add_path(span_gen); agg::render_scanlines_aa( This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |