Refactored classes to dump a set of outlines onto a png or a svg. Cleared naming and parameters. At least partially...

This commit is contained in:
Paolo Cignoni 2013-03-25 13:12:22 +00:00
parent ea5c88be1a
commit 013857b8fe
2 changed files with 178 additions and 171 deletions

View File

@ -5,42 +5,47 @@
using namespace vcg; using namespace vcg;
using namespace std; using namespace std;
void PolyDumper::rectSetToPolySet(vector< Box2f > &rectVec, vector< vector<Point2f> > &polyVec) void PolyDumper::rectSetToOutline2Vec(vector< Box2f > &rectVec, vector< vector<Point2f> > &outline2Vec)
{ {
polyVec.clear(); outline2Vec.clear();
for(size_t i=0;i<rectVec.size();++i) for(size_t i=0;i<rectVec.size();++i)
{ {
Box2f &b=rectVec[i]; Box2f &b=rectVec[i];
polyVec.resize(polyVec.size()+1); outline2Vec.resize(outline2Vec.size()+1);
polyVec.back().push_back(b.min); outline2Vec.back().push_back(b.min);
polyVec.back().push_back(Point2f(b.max[0],b.min[1])); outline2Vec.back().push_back(Point2f(b.max[0],b.min[1]));
polyVec.back().push_back(b.max); outline2Vec.back().push_back(b.max);
polyVec.back().push_back(Point2f(b.min[0],b.max[1])); outline2Vec.back().push_back(Point2f(b.min[0],b.max[1]));
} }
} }
void PolyDumper::multiRectSetToSinglePolySet(vector< Box2f > &rectVec, vector<Similarity2f> &trVec, vector<int> &indVec, void PolyDumper::multiRectSetToSingleOutline2Vec(vector< Box2f > &rectVec,
int ind, vector< vector<Point2f> > &polyVec, vector<Similarity2f> &trPolyVec) vector<Similarity2f> &trVec, vector<int> &indVec,
int ind, vector< vector<Point2f> > &outline2Vec,
vector<Similarity2f> &trOutline2Vec)
{ {
polyVec.clear(); outline2Vec.clear();
trPolyVec.clear(); trOutline2Vec.clear();
for(size_t i=0;i<rectVec.size();++i) for(size_t i=0;i<rectVec.size();++i)
if(indVec[i]==ind) if(indVec[i]==ind)
{ {
trPolyVec.push_back(trVec[i]); trOutline2Vec.push_back(trVec[i]);
Box2f &b=rectVec[i]; Box2f &b=rectVec[i];
polyVec.resize(polyVec.size()+1); outline2Vec.resize(outline2Vec.size()+1);
polyVec.back().push_back(b.min); outline2Vec.back().push_back(b.min);
polyVec.back().push_back(Point2f(b.max[0],b.min[1])); outline2Vec.back().push_back(Point2f(b.max[0],b.min[1]));
polyVec.back().push_back(b.max); outline2Vec.back().push_back(b.max);
polyVec.back().push_back(Point2f(b.min[0],b.max[1])); outline2Vec.back().push_back(Point2f(b.min[0],b.max[1]));
} }
} }
void PolyDumper::multiPolySetToSinglePolySet(std::vector< std::vector<Point2f> > &multiPolyVec, std::vector<Similarity2f> &multiTrVec, std::vector<int> &indVec, void PolyDumper::multiOutline2VecToSingleOutline2Vec(std::vector< std::vector<Point2f> > &multiPolyVec,
int ind, std::vector< std::vector<Point2f> > &singlePolyVec, std::vector<Similarity2f> &singleTrVec) std::vector<Similarity2f> &multiTrVec,
std::vector<int> &indVec, int ind,
std::vector< std::vector<Point2f> > &singlePolyVec,
std::vector<Similarity2f> &singleTrVec)
{ {
singlePolyVec.clear(); singlePolyVec.clear();
singleTrVec.clear(); singleTrVec.clear();
@ -55,7 +60,7 @@ void PolyDumper::multiPolySetToSinglePolySet(std::vector< std::vector<Point2f> >
} }
void PolyDumper::dumpPolySetSVG(const char * imageName, vector< vector<Point2f> > &polyVec, vector<Similarity2f> &trVec, PolyDumperParam &pp) void PolyDumper::dumpOutline2VecSVG(const char * imageName, vector< vector<Point2f> > &polyVec, vector<Similarity2f> &trVec, PolyDumper::Param &pp)
{ {
vector< vector< vector<Point2f> > > polyVecVec(polyVec.size()); vector< vector< vector<Point2f> > > polyVecVec(polyVec.size());
for(size_t i=0;i<polyVec.size();++i) for(size_t i=0;i<polyVec.size();++i)
@ -63,10 +68,10 @@ void PolyDumper::dumpPolySetSVG(const char * imageName, vector< vector<Point2f>
polyVecVec[i].resize(1); polyVecVec[i].resize(1);
polyVecVec[i][0]=polyVec[i]; polyVecVec[i][0]=polyVec[i];
} }
dumpPolySetSVG(imageName,polyVecVec,trVec,pp); dumpOutline2VecSVG(imageName,polyVecVec,trVec,pp);
} }
void PolyDumper::dumpPolySetPNG(const char * imageName, vector< vector<Point2f> > &polyVec, vector<Similarity2f> &trVec, PolyDumperParam &pp) void PolyDumper::dumpOutline2VecPNG(const char * imageName, vector< vector<Point2f> > &polyVec, vector<Similarity2f> &trVec, PolyDumper::Param &pp)
{ {
vector< vector< vector<Point2f> > > polyVecVec(polyVec.size()); vector< vector< vector<Point2f> > > polyVecVec(polyVec.size());
for(size_t i=0;i<polyVec.size();++i) for(size_t i=0;i<polyVec.size();++i)
@ -74,19 +79,19 @@ void PolyDumper::dumpPolySetPNG(const char * imageName, vector< vector<Point2f>
polyVecVec[i].resize(1); polyVecVec[i].resize(1);
polyVecVec[i][0]=polyVec[i]; polyVecVec[i][0]=polyVec[i];
} }
dumpPolySetPNG(imageName,polyVecVec,trVec,pp); dumpOutline2VecPNG(imageName,polyVecVec,trVec,pp);
} }
void PolyDumper::dumpPolySetPNG(const char * imageName, vector< vector< vector<Point2f> > > &polyVecVec, vector<Similarity2f> &trVec, PolyDumperParam &pp) void PolyDumper::dumpOutline2VecPNG(const char * imageName, vector< vector< vector<Point2f> > > &polyVecVec, vector<Similarity2f> &trVec, PolyDumper::Param &pp)
{ {
vector<string> labelVec(polyVecVec.size()); vector<string> labelVec;
dumpPolySetPNG(imageName,polyVecVec,trVec,labelVec,pp); dumpOutline2VecPNG(imageName,polyVecVec,trVec,labelVec,pp);
} }
void PolyDumper::dumpPolySetSVG(const char * imageName, vector< vector< vector<Point2f> > > &polyVecVec, vector<Similarity2f> &trVec, PolyDumperParam &pp) void PolyDumper::dumpOutline2VecSVG(const char * imageName, vector< vector< vector<Point2f> > > &polyVecVec, vector<Similarity2f> &trVec, PolyDumper::Param &pp)
{ {
vector<string> labelVec(polyVecVec.size()); vector<string> labelVec(polyVecVec.size());
dumpPolySetSVG(imageName,polyVecVec,trVec,labelVec,pp); dumpOutline2VecSVG(imageName,polyVecVec,trVec,labelVec,pp);
} }
///this class draw a black mask fora given polygon, cenetered and scaled to fit with ///this class draw a black mask fora given polygon, cenetered and scaled to fit with
@ -199,7 +204,7 @@ vcg::Point2f PolyDumper::GetIncenter(const vector< vector<Point2f> > &polyVec,
{ {
///INITIALIZE THE IMAGE ///INITIALIZE THE IMAGE
QImage img(resolution,resolution,QImage::Format_RGB32); QImage img(resolution,resolution,QImage::Format_RGB32);
img.fill(vcg::ColorConverter::ToQColor(vcg::Color4b(255,255,255,255)).rgb()); img.fill(vcg::ColorConverter::ToQColor(vcg::Color4b::White));
Similarity2f tra0; Similarity2f tra0;
///DRAW THE MASK ///DRAW THE MASK
DrawPolygonMask(polyVec,img,tra0,tra1); DrawPolygonMask(polyVec,img,tra0,tra1);
@ -258,11 +263,11 @@ vcg::Point2f PolyDumper::GetIncenter(const vector< vector<Point2f> > &polyVec,
///write a polygon on a PNG file, format of the polygon is vector of vector of contours...nested contours are holes ///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, ///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 ///the label to be written and the global parameter for drawing style
void PolyDumper::dumpPolySetPNG(const char * imageName, void PolyDumper::dumpOutline2VecPNG(const char * imageName,
vector< vector< vector<Point2f> > > &polyVecVec, vector< vector< vector<Point2f> > > &polyVecVec,
vector<Similarity2f> &trVec, vector<Similarity2f> &trVec,
vector<string> &labelVec, vector<string> &labelVec,
PolyDumperParam &pp) Param &pp)
{ {
///SET THE FONT ///SET THE FONT
assert(polyVecVec.size() == trVec.size()); assert(polyVecVec.size() == trVec.size());
@ -302,10 +307,6 @@ void PolyDumper::dumpPolySetPNG(const char * imageName,
ppQ.push_back(QPointF(polyVecVec[i][jj][0][0],polyVecVec[i][jj][0][1])); ppQ.push_back(QPointF(polyVecVec[i][jj][0][0],polyVecVec[i][jj][0][1]));
QPP.addPolygon(QPolygonF(ppQ)); QPP.addPolygon(QPolygonF(ppQ));
} }
///FIND THE BARYCENTER
int radius;
Point2f bc;
bc=GetIncenter(polyVecVec[i],trVec[i],radius,10);
if (pp.randomColor) if (pp.randomColor)
bb.setColor(vcg::ColorConverter::ToQColor(Color4b::Scatter(polyVecVec.size(),i))); bb.setColor(vcg::ColorConverter::ToQColor(Color4b::Scatter(polyVecVec.size(),i)));
@ -315,14 +316,18 @@ void PolyDumper::dumpPolySetPNG(const char * imageName,
painter.setBrush(bb); painter.setBrush(bb);
painter.setPen(qp); painter.setPen(qp);
painter.drawPath(QPP); painter.drawPath(QPP);
if(!labelVec.empty())
{
///FIND THE BARYCENTER
int radius;
Point2f bc=GetIncenter(polyVecVec[i],trVec[i],radius,10);
///DRAW THE TEXT ///DRAW THE TEXT
painter.setFont(qf); painter.setFont(qf);
painter.resetTransform(); painter.resetTransform();
painter.translate(trVec[i].tra[0],trVec[i].tra[1]); painter.translate(trVec[i].tra[0],trVec[i].tra[1]);
painter.drawText(bc[0]-radius,bc[1]-radius,radius*2,radius*2,Qt::AlignHCenter|Qt::AlignCenter,QString(labelVec[i].c_str())); painter.drawText(bc[0]-radius,bc[1]-radius,radius*2,radius*2,Qt::AlignHCenter|Qt::AlignCenter,QString(labelVec[i].c_str()));
} }
}
painter.end(); painter.end();
img.save(imageName); img.save(imageName);
} }
@ -331,13 +336,13 @@ void PolyDumper::dumpPolySetPNG(const char * imageName,
///write a polygon on a SVG file, format of the polygon is vector of vector of contours...nested contours are holes ///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, ///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 ///the label to be written and the global parameter for drawing style
void PolyDumper::dumpPolySetSVG(const char * imageName, void PolyDumper::dumpOutline2VecSVG(const char * imageName,
vector< vector< vector<Point2f> > > &polyVecVec, vector< vector< vector<Point2f> > > &polyVecVec,
vector<Similarity2f> &trVec, vector<Similarity2f> &trVec,
vector< vector< string> > &labelVecVec, vector< vector< string> > &labelVecVec,
vector<vector<Point2f> > &labelPosVecVec, vector<vector<Point2f> > &labelPosVecVec,
vector<vector<float> >&labelRadVecVec, vector<vector<float> >&labelRadVecVec,
PolyDumperParam &pp) PolyDumper::Param &pp)
{ {
assert(polyVecVec.size() == trVec.size()); assert(polyVecVec.size() == trVec.size());
@ -425,11 +430,11 @@ void PolyDumper::dumpPolySetSVG(const char * imageName,
painter.end(); painter.end();
} }
void PolyDumper::dumpPolySetSVG(const char * imageName, void PolyDumper::dumpOutline2VecSVG(const char * imageName,
vector< vector< vector<Point2f> > > &polyVecVec, vector< vector< vector<Point2f> > > &polyVecVec,
vector<Similarity2f> &trVec, vector<Similarity2f> &trVec,
vector< string > &labelVec, vector< string > &labelVec,
PolyDumperParam &pp) PolyDumper::Param &pp)
{ {
vector< vector< string> > labelVecVec(labelVec.size()); vector< vector< string> > labelVecVec(labelVec.size());
vector< vector<Point2f> > labelPosVec; vector< vector<Point2f> > labelPosVec;
@ -438,6 +443,6 @@ void PolyDumper::dumpPolySetSVG(const char * imageName,
{ {
labelVecVec[i].push_back(labelVec[i]); labelVecVec[i].push_back(labelVec[i]);
} }
dumpPolySetSVG(imageName,polyVecVec,trVec,labelVecVec,labelPosVec,labelRadVec,pp); dumpOutline2VecSVG(imageName,polyVecVec,trVec,labelVecVec,labelPosVec,labelRadVec,pp);
} }

View File

@ -10,7 +10,12 @@
///this class is used to pass global ///this class is used to pass global
///parameters to the polygonal dumper ///parameters to the polygonal dumper
class PolyDumperParam
///this class is used to draw polygons on an image could be vectorial or not
class PolyDumper
{
public:
class Param
{ {
public: public:
/// the backgrround color /// the backgrround color
@ -41,7 +46,7 @@ public:
} }
///default constructor ///default constructor
PolyDumperParam() Param()
{ {
backgroundColor = vcg::Color4b::Gray; backgroundColor = vcg::Color4b::Gray;
width=1024; width=1024;
@ -53,10 +58,7 @@ public:
FillColor=vcg::Color4b(0,0,0,255); FillColor=vcg::Color4b(0,0,0,255);
} }
}; };
private:
///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 ///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 ///the image size, it return the transformation to tranform back the polygon to 2D space
static void DrawPolygonMask(const std::vector< std::vector<vcg::Point2f> > &polyVec,QImage &img, static void DrawPolygonMask(const std::vector< std::vector<vcg::Point2f> > &polyVec,QImage &img,
@ -72,37 +74,37 @@ class PolyDumper
const vcg::Similarity2f &tra1,int &radius,int resolution=100); const vcg::Similarity2f &tra1,int &radius,int resolution=100);
public: public:
static void rectSetToPolySet(std::vector< vcg::Box2f > &rectVec, std::vector< std::vector<vcg::Point2f> > &polyVec); static void rectSetToOutline2Vec(std::vector< vcg::Box2f > &rectVec, std::vector< std::vector<vcg::Point2f> > &polyVec);
static void multiRectSetToSinglePolySet(std::vector< vcg::Box2f > &rectVec, std::vector<vcg::Similarity2f> &trVec, std::vector<int> &indVec, static void multiRectSetToSingleOutline2Vec(std::vector< vcg::Box2f > &rectVec, std::vector<vcg::Similarity2f> &trVec, std::vector<int> &indVec,
int ind, std::vector< std::vector<vcg::Point2f> > &polyVec, std::vector<vcg::Similarity2f> &trPolyVec); int ind, std::vector< std::vector<vcg::Point2f> > &polyVec, std::vector<vcg::Similarity2f> &trPolyVec);
static void multiPolySetToSinglePolySet(std::vector< std::vector< vcg::Point2f> > &multipolyVec, std::vector< vcg::Similarity2f> &trVec, std::vector<int> &indVec, static void multiOutline2VecToSingleOutline2Vec(std::vector< std::vector< vcg::Point2f> > &multipolyVec, std::vector< vcg::Similarity2f> &trVec, std::vector<int> &indVec,
int ind, std::vector< std::vector< vcg::Point2f> > &polyVec, std::vector< vcg::Similarity2f> &trPolyVec); int ind, std::vector< std::vector< vcg::Point2f> > &polyVec, std::vector< vcg::Similarity2f> &trPolyVec);
///write a polygon on a PNG file, format of the polygon is vector of vector of contours...nested contours are holes ///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, ///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 ///the label to be written and the global parameter for drawing style
static void dumpPolySetPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec, static void dumpOutline2VecPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, std::vector<std::vector<std::string> > &labelVecVec, PolyDumperParam &pp); std::vector<vcg::Similarity2f> &trVec, std::vector<std::vector<std::string> > &labelVecVec, Param &pp);
//write a polygon on a SVG file, format of the polygon is vector of vector of contours...nested contours are holes //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, ///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 ///the label to be written and the global parameter for drawing style
static void dumpPolySetSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec, static void dumpOutline2VecSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, std::vector< std::vector<std::string> > &labelVecVec, std::vector<vcg::Similarity2f> &trVec, std::vector< std::vector<std::string> > &labelVecVec,
std::vector<std::vector< vcg::Point2f> > &labelPosVec, std::vector<std::vector<float> > &labelRadVec, PolyDumperParam &pp); std::vector<std::vector< vcg::Point2f> > &labelPosVec, std::vector<std::vector<float> > &labelRadVec, Param &pp);
static void dumpPolySetPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec, static void dumpOutline2VecPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, std::vector<std::string> &labelVec, PolyDumperParam &pp); std::vector<vcg::Similarity2f> &trVec, std::vector<std::string> &labelVec, Param &pp);
static void dumpPolySetSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec, static void dumpOutline2VecSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, std::vector<std::string> &labelVec, PolyDumperParam &pp); std::vector<vcg::Similarity2f> &trVec, std::vector<std::string> &labelVec, Param &pp);
static void dumpPolySetPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec, static void dumpOutline2VecPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, PolyDumperParam &pp); std::vector<vcg::Similarity2f> &trVec, Param &pp);
static void dumpPolySetSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec, static void dumpOutline2VecSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, PolyDumperParam &pp); std::vector<vcg::Similarity2f> &trVec, Param &pp);
static void dumpPolySetPNG(const char * imageName, std::vector< std::vector<vcg::Point2f> > &polyVecVec, static void dumpOutline2VecPNG(const char * imageName, std::vector< std::vector<vcg::Point2f> > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, PolyDumperParam &pp); std::vector<vcg::Similarity2f> &trVec, Param &pp);
static void dumpPolySetSVG(const char * imageName, std::vector< std::vector<vcg::Point2f> > &polyVecVec, static void dumpOutline2VecSVG(const char * imageName, std::vector< std::vector<vcg::Point2f> > &polyVecVec,
std::vector<vcg::Similarity2f> &trVec, PolyDumperParam &pp); std::vector<vcg::Similarity2f> &trVec, Param &pp);
}; };
#endif // POLYTOQIMAGE_H #endif // POLYTOQIMAGE_H