Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5885979

Browse files
committed
Slight speed improvement in draw_quad_mesh.
svn path=/trunk/matplotlib/; revision=4396
1 parent 059bc58 commit 5885979

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

src/_backend_agg.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,10 @@ int RendererAgg::inPolygon(int row, const double xs[4], const double ys[4], int
956956
return numIntersect;
957957
}
958958

959-
void RendererAgg::DrawQuadMesh(int meshWidth, int meshHeight, const agg::rgba8 colorArray[], const double xCoords[], const double yCoords[])
959+
void RendererAgg::DrawQuadMesh(int meshWidth, int meshHeight, void* colors_void, const double xCoords[], const double yCoords[])
960960
{
961+
PyArrayObject* colors = (PyArrayObject*)colors_void;
962+
961963
/* draw each quadrilateral */
962964
// agg::renderer_primitives<agg::renderer_base<agg::pixfmt_rgba32> > lineRen(*rendererBase);
963965
int i = 0;
@@ -992,18 +994,25 @@ void RendererAgg::DrawQuadMesh(int meshWidth, int meshHeight, const agg::rgba8 c
992994
//currTime = clock();
993995
//timer2 += (clock() - currTime);
994996
//currTime = clock();
997+
size_t color_index = (i * meshWidth) + j;
998+
agg::rgba color(*(double*)PyArray_GETPTR2(colors, color_index, 0),
999+
*(double*)PyArray_GETPTR2(colors, color_index, 1),
1000+
*(double*)PyArray_GETPTR2(colors, color_index, 2),
1001+
*(double*)PyArray_GETPTR2(colors, color_index, 3));
1002+
9951003
for(k = firstRow; k <= lastRow; k++)
9961004
{
9971005
numCol = inPolygon(k, xs, ys, col);
998-
if (numCol >= 2) rendererBase->copy_hline(col[0], k, col[1] - 1, colorArray[(i * meshWidth) + j]);
999-
if (numCol == 4) rendererBase->copy_hline(col[2], k, col[3] - 1, colorArray[(i * meshWidth) + j]);
1006+
1007+
if (numCol >= 2) rendererBase->copy_hline(col[0], k, col[1] - 1, color);
1008+
if (numCol == 4) rendererBase->copy_hline(col[2], k, col[3] - 1, color);
10001009
}
10011010
}
10021011
}
10031012
return;
10041013
}
10051014

1006-
void RendererAgg::DrawQuadMeshEdges(int meshWidth, int meshHeight, const agg::rgba8 colorArray[], const double xCoords[], const double yCoords[])
1015+
void RendererAgg::DrawQuadMeshEdges(int meshWidth, int meshHeight, const double xCoords[], const double yCoords[])
10071016
{
10081017
int i, j;
10091018
agg::renderer_primitives<agg::renderer_base<agg::pixfmt_rgba32> > lineRen(*rendererBase);
@@ -1027,15 +1036,13 @@ void RendererAgg::DrawQuadMeshEdges(int meshWidth, int meshHeight, const agg::rg
10271036

10281037
Py::Object
10291038
RendererAgg::draw_quad_mesh(const Py::Tuple& args){
1030-
10311039
//printf("#1: %d\n", clock());
10321040
Py::Object colorsi = args[2];
10331041
Py::Object xCoordsi = args[3];
10341042
Py::Object yCoordsi = args[4];
10351043
int meshWidth = Py::Int(args[0]);
10361044
int meshHeight = Py::Int(args[1]);
10371045
int showedges = Py::Int(args[9]);
1038-
int numQuads = (meshWidth * meshHeight);
10391046
PyArrayObject *colors = (PyArrayObject *) PyArray_ContiguousFromObject(colorsi.ptr(), PyArray_DOUBLE, 2, 2);
10401047
PyArrayObject *xCoords = (PyArrayObject *) PyArray_ContiguousFromObject(xCoordsi.ptr(), PyArray_DOUBLE, 1, 1);
10411048
PyArrayObject *yCoords = (PyArrayObject *) PyArray_ContiguousFromObject(yCoordsi.ptr(), PyArray_DOUBLE, 1, 1);
@@ -1117,30 +1124,14 @@ RendererAgg::draw_quad_mesh(const Py::Tuple& args){
11171124

11181125
/**** End of transformations ****/
11191126

1120-
/* convert colors */
1121-
double r;
1122-
double g;
1123-
double b;
1124-
double a;
1125-
int i;
1126-
agg::rgba8* colorArray = new agg::rgba8[numQuads];
1127-
for(i=0; i < numQuads; i++)
1128-
{
1129-
r = *(double *)(colors -> data + i*(colors -> strides[0]));
1130-
g = *(double *)(colors -> data + i*(colors -> strides[0]) + (colors -> strides[1]));
1131-
b = *(double *)(colors -> data + i*(colors -> strides[0]) + 2*(colors -> strides[1]));
1132-
a = *(double *)(colors -> data + i*(colors -> strides[0]) + 3*(colors -> strides[1]));
1133-
colorArray[i] = agg::rgba8((int)(255.0 * r), (int)(255.0 * g), (int)(255.0 * b), (int)(255.0 * a));
1134-
}
1135-
DrawQuadMesh(meshWidth, meshHeight, colorArray, &(newXCoords[0]), &(newYCoords[0]));
1127+
DrawQuadMesh(meshWidth, meshHeight, colors, &(newXCoords[0]), &(newYCoords[0]));
11361128
if(showedges)
1137-
DrawQuadMeshEdges(meshWidth, meshHeight, colorArray, &(newXCoords[0]), &(newYCoords[0]));
1129+
DrawQuadMeshEdges(meshWidth, meshHeight, &(newXCoords[0]), &(newYCoords[0]));
11381130
Py_XDECREF(xCoords);
11391131
Py_XDECREF(yCoords);
11401132
Py_XDECREF(colors);
11411133
delete newXCoords;
11421134
delete newYCoords;
1143-
delete colorArray;
11441135
//printf("#2: %d\n", clock());
11451136
return Py::Object();
11461137
}

src/_backend_agg.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ class RendererAgg: public Py::PythonExtension<RendererAgg> {
215215
agg::rect bbox_to_rect( const Py::Object& o);
216216
double points_to_pixels( const Py::Object& points);
217217
double points_to_pixels_snapto( const Py::Object& points);
218-
void DrawQuadMesh(int, int, const agg::rgba8[], const double[], const double[]);
219-
void DrawQuadMeshEdges(int, int, const agg::rgba8[], const double[], const double[]);
218+
void DrawQuadMesh(int, int, void* colors, const double[], const double[]);
219+
void DrawQuadMeshEdges(int, int, const double[], const double[]);
220220
int intersectCheck(double, double, double, double, double, int*);
221221
int inPolygon(int, const double[4], const double[4], int[4]);
222222
void set_clip_from_bbox(const Py::Object& o);

0 commit comments

Comments
 (0)