From 319f8404ea0bf978fe1c2423a3549f835e541961 Mon Sep 17 00:00:00 2001 From: cignoni Date: Tue, 26 Mar 2013 08:51:32 +0000 Subject: [PATCH] Standardization of names of outlines/poly --- ...{PolyToQImage.cpp => Outline2ToQImage.cpp} | 0 .../qt/{PolyToQImage.h => Outline2ToQImage.h} | 0 wrap/qt/outline2_rasterizer.cpp | 176 ++++++++++++++++++ wrap/qt/outline2_rasterizer.h | 29 +++ 4 files changed, 205 insertions(+) rename wrap/qt/{PolyToQImage.cpp => Outline2ToQImage.cpp} (100%) rename wrap/qt/{PolyToQImage.h => Outline2ToQImage.h} (100%) create mode 100644 wrap/qt/outline2_rasterizer.cpp create mode 100644 wrap/qt/outline2_rasterizer.h diff --git a/wrap/qt/PolyToQImage.cpp b/wrap/qt/Outline2ToQImage.cpp similarity index 100% rename from wrap/qt/PolyToQImage.cpp rename to wrap/qt/Outline2ToQImage.cpp diff --git a/wrap/qt/PolyToQImage.h b/wrap/qt/Outline2ToQImage.h similarity index 100% rename from wrap/qt/PolyToQImage.h rename to wrap/qt/Outline2ToQImage.h diff --git a/wrap/qt/outline2_rasterizer.cpp b/wrap/qt/outline2_rasterizer.cpp new file mode 100644 index 00000000..10213870 --- /dev/null +++ b/wrap/qt/outline2_rasterizer.cpp @@ -0,0 +1,176 @@ +#include "qtpolyrasterizer.h" +#include +#include "stdio.h" +#include "math.h" +#include +#include +#include +#include + +using namespace vcg; +using namespace std; + +void QtPolyRasterizer::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 > QtPolyRasterizer::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; +} diff --git a/wrap/qt/outline2_rasterizer.h b/wrap/qt/outline2_rasterizer.h new file mode 100644 index 00000000..e94336e1 --- /dev/null +++ b/wrap/qt/outline2_rasterizer.h @@ -0,0 +1,29 @@ +#ifndef QTPOLYRASTERIZER_H +#define QTPOLYRASTERIZER_H + +#include +#include +#include +#include +#include +#include +#include + +///this class is used to pass global +///parameters to the polygonal dumper +namespace vcg{ +class RasterizedOutline2; +} + +///this class is used to draw polygons on an image could be vectorial or not +class QtPolyRasterizer +{ +public: + static void rasterize(vcg::RasterizedOutline2 &poly, + float scaleFactor, + int rast_i, int rotationNum, int cellSize); + + static std::vector > rotateGridCWise(std::vector< std::vector >& inGrid); + +}; +#endif // QTPOLYRASTERIZER_H