#include #include #include "stdio.h" #include "math.h" #include #include using namespace vcg; using namespace std; void QtOutline2Rasterizer::rasterize(RasterizedOutline2 &poly, float scale, int rast_i, int rotationNum, int cellSize) { float rotRad = M_PI*2.0f*float(rast_i) / float(rotationNum); //get polygon's BB, rotated according to the input parameter Box2f bb; vector pointvec = poly.getPoints(); for(size_t i=0;i points; vector newpoints = poly.getPoints(); for (int i = 0; i < newpoints.size(); i++) { points.push_back(QPointF(newpoints[i].X(), newpoints[i].Y())); } painter.drawPolygon(QPolygonF(points)); //CROPPING: it is enough to check for the (end - cellSize - 1)th row/col of pixels, if they're all black we can eliminate the last 8columns/rows of pixels bool cropX = true; bool cropY = true; for (int j=0; j points2; vector newpoints2 = poly.getPoints(); for (int i = 0; i < newpoints2.size(); i++) { points2.push_back(QPointF(newpoints2[i].X(), newpoints2[i].Y())); } painter.drawPolygon(QPolygonF(points2)); //create the first grid, which will then be rotated 3 times. //we will reuse this grid to create the rasterizations corresponding to this one rotated by 90/180/270° vector > tetrisGrid; QRgb yellow = QColor(Qt::yellow).rgb(); int gridWidth = img.width() / cellSize; int gridHeight = img.height() / cellSize; int x = 0; tetrisGrid.resize(gridHeight); for (int k = 0; k < gridHeight; k++) { tetrisGrid[k].resize(gridWidth, 0); } for (int y = 0; y < img.height(); y++) { int gridY = y / cellSize; const uchar* line = img.scanLine(y); x = 0; int gridX = 0; while(x < img.width()) { gridX = x/cellSize; if (tetrisGrid[gridY][gridX] == 1) { x+= cellSize - (x % cellSize); //align with the next x continue; } if (((QRgb*)line)[x] == yellow) tetrisGrid[gridY][gridX] = 1; ++x; } } //create the 4 rasterizations (one every 90°) using the discrete representation grid we've just created int rotationOffset = rotationNum/4; for (int j = 0; j < 4; j++) { if (j != 0) { tetrisGrid = rotateGridCWise(tetrisGrid); } //add the grid to the poly's vector of grids poly.getGrids(rast_i + rotationOffset*j) = tetrisGrid; //initializes bottom/left/deltaX/deltaY vectors of the poly, for the current rasterization poly.initFromGrid(rast_i + rotationOffset*j); } painter.end(); } // rotates the grid 90 degree clockwise (by simple swap) // used to lower the cost of rasterization. vector > QtOutline2Rasterizer::rotateGridCWise(vector< vector >& inGrid) { vector > outGrid(inGrid[0].size()); for (int i = 0; i < inGrid[0].size(); i++) { outGrid[i].reserve(inGrid.size()); for (int j = 0; j < inGrid.size(); j++) { outGrid[i].push_back(inGrid[inGrid.size() - j - 1][i]); } } return outGrid; }