From f786569888ace163d21e24d565f2677cfba2e982 Mon Sep 17 00:00:00 2001 From: nicopietroni Date: Thu, 9 Feb 2012 14:00:00 +0000 Subject: [PATCH] completely restructured the class, corrected several bugs and added several features --- wrap/qt/PolyToQImage.cpp | 451 ++++++++++++++++++++++++++++++--------- wrap/qt/PolyToQImage.h | 92 ++++++-- 2 files changed, 420 insertions(+), 123 deletions(-) diff --git a/wrap/qt/PolyToQImage.cpp b/wrap/qt/PolyToQImage.cpp index 6ecb5329..f2835f23 100644 --- a/wrap/qt/PolyToQImage.cpp +++ b/wrap/qt/PolyToQImage.cpp @@ -1,119 +1,370 @@ -#include -#include -#include + #include "PolyToQImage.h" #include using namespace vcg; using namespace std; -void rectSetToPolySet(vector< Box2f > &rectVec, vector< vector > &polyVec) +void PolyDumper::rectSetToPolySet(vector< Box2f > &rectVec, vector< vector > &polyVec) { - polyVec.clear(); - for(size_t i=0;i > &polyVec, PolyDumperParam &pp) -{ - Box2f bb; - for(size_t i=0;i trVec(polyVec.size(),sim); - - dumpPolySet(imageName,polyVec,trVec,pp); + polyVec.clear(); + for(size_t i=0;i > &polyVec, vector &trVec, PolyDumperParam &pp) +void PolyDumper::dumpPolySetSVG(const char * imageName, vector< vector > &polyVec, vector &trVec, PolyDumperParam &pp) { - vector< vector< vector > > polyVecVec(polyVec.size()); - for(size_t i=0;i > > polyVecVec(polyVec.size()); + for(size_t i=0;i > > &polyVecVec, vector &trVec, PolyDumperParam &pp) +void PolyDumper::dumpPolySetPNG(const char * imageName, vector< vector > &polyVec, vector &trVec, PolyDumperParam &pp) { - vector labelVec(polyVecVec.size()); - dumpPolySet(imageName,polyVecVec,trVec,labelVec,pp); + vector< vector< vector > > polyVecVec(polyVec.size()); + for(size_t i=0;i > > &polyVecVec, vector &trVec, vector &labelVec, PolyDumperParam &pp) +void PolyDumper::dumpPolySetPNG(const char * imageName, vector< vector< vector > > &polyVecVec, vector &trVec, PolyDumperParam &pp) { - assert(polyVecVec.size() == trVec.size()); - QFont qf("courier",1); - QSvgGenerator svg; - svg.setFileName(imageName); -// pp.widthPx = pp.widthDot; -// pp.heightPx = pp.heightDot; - svg.setSize(QSize(pp.widthMm,pp.heightMm)); - svg.setResolution(pp.dpi); // ?? dpm or dpi - - QImage img(pp.widthPx,pp.heightPx,QImage::Format_RGB32); - img.fill(vcg::ColorConverter::ToQColor( pp.backgroundColor).rgb()); - - QPainter painter; - - if(QString(imageName).endsWith("svg",Qt::CaseInsensitive)) - painter.begin(&svg); - else painter.begin(&img); - QBrush bb; - QPen qp; - qp.setWidth(0); -// printf("polyVecVec.size() = %i \n",polyVecVec.size()); - for(size_t i=0;i ppQ; - for(int j=0;j labelVec(polyVecVec.size()); + dumpPolySetPNG(imageName,polyVecVec,trVec,labelVec,pp); } +void PolyDumper::dumpPolySetSVG(const char * imageName, vector< vector< vector > > &polyVecVec, vector &trVec, PolyDumperParam &pp) +{ + vector labelVec(polyVecVec.size()); + dumpPolySetSVG(imageName,polyVecVec,trVec,labelVec,pp); +} + +///this class draw a black mask fora given polygon, cenetered and scaled to fit with +///the image size, it return the transformation to tranform back the polygon to 2D space +void PolyDumper::DrawPolygonMask(const vector< vector > &polyVec, + QImage &img, + Similarity2f &ret, + const Similarity2f &trans) +{ + + ///RASTERIZE THE POLYGON + QPainter painter; + painter.begin(&img); + QBrush bb; + bb.setStyle(Qt::SolidPattern); + + int resolution=img.width(); + + QPen qp; + qp.setWidthF(0); + ///find the BB + vcg::Box2f bbox; + for(int i=0;i ppQ; + for(int j=0;j > &polyVec, + const Similarity2f &tra1, + int &radius, + int resolution) +{ + ///INITIALIZE THE IMAGE + QImage img(resolution,resolution,QImage::Format_RGB32); + img.fill(vcg::ColorConverter::ToQColor(vcg::Color4b(255,255,255,255)).rgb()); + Similarity2f tra0; + ///DRAW THE MASK + DrawPolygonMask(polyVec,img,tra0,tra1); + //img = img.mirrored(false,true); + + ///THEN FIND THE CENTROID + float Maxradius=-1; + vcg::Point2i incenter=vcg::Point2i(0,0); + int sizeY=img.size().height(); + int sizeX=img.size().width(); + ///THEN GO OVER ALL VERTICES + for (int x=0;xMaxradius) + { + Maxradius=curr_radius; + incenter=vcg::Point2i(x,y); + } + } + + vcg::Point2f incenterf; + incenterf.Import(incenter); + ///FIRST TRASNFORMATION + //QPainter painter; + //painter.begin(&img); + ////painter.fillRect(incenter.V(0)-Maxradius,incenter.V(1)-Maxradius,Maxradius*2,Maxradius*2,Qt::red); + //painter.fillRect(incenter.V(0)-1,incenter.V(1)-1,2,2,Qt::red); + ////painter.drawPoint(incenter.V(0),incenter.V(1)); + //painter.end(); + /*static int num=0; + num++; + char path[100]; + sprintf(path,"mask%d.png",num); + img.save(path);*/ + + + incenterf.X()-=tra0.tra[0]; + incenterf.Y()-=tra0.tra[1]; + incenterf*=1.0/tra0.sca; + incenterf*=tra1.sca; + + ///SECOND TRANSFORMATION + assert(Maxradius>0); + radius=Maxradius; + return incenterf; +} + + +///write a polygon on a PNG file, format of the polygon is vector of vector of contours...nested contours are holes +///takes the name of the image in input, the set of polygons, the set of per polygons transformation, +///the label to be written and the global parameter for drawing style +void PolyDumper::dumpPolySetPNG(const char * imageName, + vector< vector< vector > > &polyVecVec, + vector &trVec, + vector &labelVec, + PolyDumperParam &pp) +{ + ///SET THE FONT + assert(polyVecVec.size() == trVec.size()); + int fontSize=ceil(std::max(pp.width,pp.height)/100.0); + QFont qf("courier",fontSize); + + ///SET THE DRAWING SIZE + QImage img(pp.width,pp.height,QImage::Format_RGB32); + img.fill(vcg::ColorConverter::ToQColor( pp.backgroundColor).rgb()); + + ///SETUP OF DRAWING PROCEDURE + QPainter painter; + painter.begin(&img); + QBrush bb; + if (pp.fill) + bb.setStyle(Qt::SolidPattern); + QPen qp; + qp.setWidthF(0); + + for(size_t i=0;i ppQ; + for(int j=0;j > > &polyVecVec, + vector &trVec, + vector &labelVec, + PolyDumperParam &pp) +{ + assert(polyVecVec.size() == trVec.size()); + + ///SET THE FONT + int fontSize=ceil(std::max(pp.width,pp.height)/100.0); + QFont qf("courier",fontSize); + QSvgGenerator svg; + svg.setFileName(imageName); + + ///SET THE DRAWING SIZE + svg.setSize(QSize(pp.width,pp.height)); + svg.setViewBox(QRect(0, 0, pp.width,pp.height)); + svg.setResolution(pp.dpi);// + + ///SETUP OF DRAWING PROCEDURE + QPainter painter; + painter.begin(&svg); + QBrush bb; + if (pp.fill) + bb.setStyle(Qt::SolidPattern); + + QPen qp; + + ///SET THE GLOBAL SCALING FACTOR + float Scalesvg=1.f/(float)trVec[0].sca; + qp.setWidthF(Scalesvg); + for(size_t i=0;i ppQ; + for(int j=0;j +#include +#include #include #include #include #include +///this class is used to pass global +///parameters to the polygonal dumper class PolyDumperParam { public: - vcg::Color4b backgroundColor; - int widthPx; - int heightPx; - int widthMm; - int heightMm; - int dpi; - bool useDPI; - - PolyDumperParam() - { - backgroundColor = vcg::Color4b::Gray; - widthPx=1024; - heightPx=1024; - dpi=72; - widthMm = 100; - heightMm = 100; - useDPI=false; - } + ///the backgrround color + vcg::Color4b backgroundColor; + ///true if the polygons must be filled with color + bool fill; + ///true if the filling color is random + bool randomColor; + ///the filling color of polygons, used only if randomColor==false + vcg::Color4b FillColor; + + ///dimension of the image (in PNG are pixels, while in SV is the workspace in points) + int width; + int height; + ///DPi resolution, used only for SVG + int dpi; + + ///default contructor + PolyDumperParam() + { + backgroundColor = vcg::Color4b::Gray; + width=1024; + height=1024; + dpi=72; + fill=false; + randomColor=true; + FillColor=vcg::Color4b(0,0,0,255); + } }; - void dumpPolySet(const char * imageName, std::vector< std::vector< std::vector > > &polyVecVec, std::vector &trVec, std::vector &labelVec, PolyDumperParam &pp); - void dumpPolySet(const char * imageName, std::vector< std::vector< std::vector > > &polyVecVec, std::vector &trVec, PolyDumperParam &pp); - void dumpPolySet(const char * imageName, std::vector< std::vector > &polyVec, std::vector &trVec, PolyDumperParam &pp); - void dumpPolySet(const char * imageName, std::vector< std::vector > &polyVec, PolyDumperParam &pp); - void rectSetToPolySet(std::vector< vcg::Box2f > &rectVec, std::vector< std::vector > &polyVec); +///this class is used to draw polygons on an image could be vectorial or not +class PolyDumper +{ + ///this class draw a black mask fora given polygon, cenetered and scaled to fit with + ///the image size, it return the transformation to tranform back the polygon to 2D space + static void DrawPolygonMask(const std::vector< std::vector > &polyVec,QImage &img, + vcg::Similarity2f &ret,const vcg::Similarity2f &trans); + + ///return the max radius of a point inside a polygon ,given the mask image + ///actually it evaluate the maximum bounding box + static int getMaxMaskRadius(int x,int y,QImage &img); + + ///return the point inside the polygon with the bigger distance to the border, + ///this is used to write labels within the polygon, it handle polygons with holes too + static vcg::Point2f GetIncenter(const std::vector< std::vector > &polyVec, + const vcg::Similarity2f &tra1,int &radius,int resolution=100); + static void rectSetToPolySet(std::vector< vcg::Box2f > &rectVec, std::vector< std::vector > &polyVec); + +public: + ///write a polygon on a PNG file, format of the polygon is vector of vector of contours...nested contours are holes + ///takes the name of the image in input, the set of polygons, the set of per polygons transformation, + ///the label to be written and the global parameter for drawing style + static void dumpPolySetPNG(const char * imageName, std::vector< std::vector< std::vector > > &polyVecVec, + std::vector &trVec, std::vector &labelVec, PolyDumperParam &pp); + //write a polygon on a SVG file, format of the polygon is vector of vector of contours...nested contours are holes + ///takes the name of the image in input, the set of polygons, the set of per polygons transformation, + ///the label to be written and the global parameter for drawing style + static void dumpPolySetSVG(const char * imageName, std::vector< std::vector< std::vector > > &polyVecVec, + std::vector &trVec, std::vector &labelVec, PolyDumperParam &pp); + static void dumpPolySetPNG(const char * imageName, std::vector< std::vector< std::vector > > &polyVecVec, + std::vector &trVec, PolyDumperParam &pp); + static void dumpPolySetSVG(const char * imageName, std::vector< std::vector< std::vector > > &polyVecVec, + std::vector &trVec, PolyDumperParam &pp); + static void dumpPolySetPNG(const char * imageName, std::vector< std::vector > &polyVecVec, + std::vector &trVec, PolyDumperParam &pp); + static void dumpPolySetSVG(const char * imageName, std::vector< std::vector > &polyVecVec, + std::vector &trVec, PolyDumperParam &pp); +}; #endif // POLYTOQIMAGE_H