1 #ifndef POLYTOQIMAGE_H
2 #define POLYTOQIMAGE_H
3 #include <QImage>
4 #include <QSvgGenerator>
5 #include <QPainter>
6 #include <vcg/space/point2.h>
7 #include <vcg/space/color4.h>
8 #include <vcg/space/box2.h>
9 #include <vcg/math/similarity2.h>
10 
11 ///this class is used to pass global
12 ///parameters to the polygonal dumper
13 
14 ///this class is used to draw polygons on an image could be vectorial or not
15 class Outline2Dumper
16 {
17 public:
MM2PT(const float valueMM,float dpi)18   static float MM2PT(const float valueMM, float dpi)
19   {
20     float valueInch = valueMM / 25.4f;
21     return valueInch * dpi;
22   }
23 
24   class Param
25   {
26   public:
27       /// the backgrround color
28       vcg::Color4b backgroundColor;
29       /// true if the polygons must be filled with color
30       bool fill;
31       /// true if the filling color is random
32       bool randomColor;
33       /// the filling color of polygons, used only if randomColor==false
34       vcg::Color4b FillColor;
35       /// The size of the font. If zero (default) it is automatically chosen.
36       int fontSize;
37       /// dimension of the image (in PNG are pixels, while in SVG is the workspace in points)
38       int width;
39       int height;
40       vcg::Color4b labelColor;
41       vcg::Color4b lineColor;
42 
43       /// DPI resolution, used only for SVG
44       int dpi;
45       float penWidth;
46 
SetSVGPenInMM(float widthMM)47       void SetSVGPenInMM(float widthMM)
48       {
49         float widthInch = widthMM/25.4f;
50         penWidth = widthInch*dpi;
51       }
52 
53       ///Handy function for setting the size of the drawing
54       void SetSVGDimInMm(float widthMM,float heightMM,float _dpi=72)
55       {
56         dpi=_dpi;
57         width = MM2PT(widthMM,dpi);
58         height = MM2PT(heightMM,dpi);
59       }
60 
61       ///default constructor
Param()62       Param()
63       {
64           backgroundColor = vcg::Color4b::Gray;
65           width=1024;
66           height=1024;
67           dpi=72;
68           fontSize=0;
69           fill=false;
70           randomColor=true;
71           FillColor=vcg::Color4b(0,0,0,255);
72           lineColor=vcg::Color4b::Black;
73           labelColor=vcg::Color4b::Black;
74 
75       }
76   };
77 private:
78 	///this class draw a black mask fora given polygon, cenetered and scaled to fit with
79 	///the image size, it return the transformation to tranform back the polygon to 2D space
80 	static void DrawPolygonMask(const std::vector< std::vector<vcg::Point2f> > &polyVec,QImage &img,
81 								vcg::Similarity2f &ret,const vcg::Similarity2f &trans);
82 
83 	///return the max radius of a point inside a polygon ,given the mask image
84 	///actually it evaluate the maximum bounding box
85 	static int getMaxMaskRadius(int x,int y,QImage &img);
86 public:
87 
88 	///return the point inside the polygon with the bigger distance to the border,
89 	///this is used to write labels within the polygon, it handle polygons with holes too
90 	static vcg::Point2f GetIncenter(const std::vector< std::vector<vcg::Point2f> > &polyVec,
91 										const vcg::Similarity2f &tra1, float &radius, int resolution=100);
92 
93 	static void rectSetToOutline2Vec(std::vector< vcg::Box2f > &rectVec, std::vector< std::vector<vcg::Point2f> > &polyVec);
94 	static void multiRectSetToSingleOutline2Vec(std::vector< vcg::Box2f > &rectVec, std::vector<vcg::Similarity2f> &trVec, std::vector<int> &indVec,
95 											int ind, std::vector< std::vector<vcg::Point2f> > &polyVec, std::vector<vcg::Similarity2f> &trPolyVec);
96 	static void multiOutline2VecToSingleOutline2Vec(const std::vector<std::vector<vcg::Point2f> > &multipolyVec,  const std::vector<vcg::Similarity2f> &trVec, const std::vector<int> &indVec,
97 												 int ind, std::vector< std::vector< vcg::Point2f> > &polyVec, std::vector< vcg::Similarity2f> &trPolyVec);
98 
99 	///write a polygon on a PNG file, format of the polygon is vector of vector of contours...nested contours are holes
100 	///takes the name of the image in input, the set of polygons, the set of per polygons transformation,
101 	///the label to be written and the global parameter for drawing style
102 	static void dumpOutline2VecPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
103 							std::vector<vcg::Similarity2f> &trVec, std::vector<std::vector<std::string> > &labelVecVec, Param &pp);
104 	//write a polygon on a SVG file, format of the polygon is vector of vector of contours...nested contours are holes
105 	///takes the name of the image in input, the set of polygons, the set of per polygons transformation,
106 	///the label to be written and the global parameter for drawing style
107 	static void dumpOutline2VecSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
108 							   std::vector<vcg::Similarity2f> &trVec, std::vector< std::vector<std::string> > &labelVecVec,
109 							   std::vector<std::vector<vcg::Similarity2f> > &labelTrVecVec, std::vector<std::vector<float> > &labelRadVec, Param &pp);
110 
111 	static void dumpOutline2VecPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
112 							std::vector<vcg::Similarity2f> &trVec, std::vector<std::string> &labelVec, Param &pp);
113 	static void dumpOutline2VecSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
114 							   std::vector<vcg::Similarity2f> &trVec, std::vector<std::string> &labelVec, Param &pp);
115 
116 	static void dumpOutline2VecPNG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
117 							std::vector<vcg::Similarity2f> &trVec, Param &pp);
118 	static void dumpOutline2VecSVG(const char * imageName, std::vector< std::vector< std::vector<vcg::Point2f> > > &polyVecVec,
119 							std::vector<vcg::Similarity2f> &trVec, Param &pp);
120 
121 	static void dumpOutline2VecPNG(const char * imageName, std::vector<  std::vector<vcg::Point2f> > &polyVecVec,
122 							std::vector<vcg::Similarity2f> &trVec, Param &pp);
123 	static void dumpOutline2VecSVG(const char * imageName, std::vector<  std::vector<vcg::Point2f> > &outline2Vec,
124 							std::vector<vcg::Similarity2f> &trVec, Param &pp);
125 };
126 #endif // POLYTOQIMAGE_H
127