00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VCG_LIB_EXPORTER_SVG
00025 #define __VCG_LIB_EXPORTER_SVG
00026
00027 namespace vcg
00028 {
00029 namespace edg
00030 {
00031 namespace io
00032 {
00033
00039 class SVGProperties
00040 {
00041
00042 public:
00043
00044
00045
00046 int numCol;
00047 int numRow;
00048
00049 Point2f sizeCm;
00050
00051 Point2f marginCm;
00052
00053 Point2f pageSizeCm()
00054 {
00055 float xSize = numCol*sizeCm[0] + numCol*marginCm[0] + marginCm[0];
00056 float ySize = numRow*sizeCm[1] + numRow*marginCm[1] + marginCm[1];
00057 return Point2f(xSize,ySize);
00058 }
00059
00060
00061 Point3f projDir;
00062 Point3f projUp;
00063 Point3f projCenter;
00064
00065
00066
00067
00068
00069 float scale;
00070
00071
00072 int lineWidthPt;
00073 std::string strokeColor;
00074 std::string strokeLineCap;
00075
00076
00077 bool showTextDetails;
00078
00079 public:
00080
00081 SVGProperties()
00082 {
00083 lineWidthPt = 1;
00084 strokeColor = "black";
00085 strokeLineCap = "round";
00086
00087
00088 projDir= Point3f(0.0, 1.0, 0.0);
00089 projUp = Point3f(0.0, 0.0, 1.0);
00090 scale=0;
00091
00092 projCenter=Point3f(0, 0, 0);
00093 sizeCm=Point2f(10,10);
00094 marginCm=Point2f(1,1);
00095 showTextDetails=true;
00096 numCol=2;
00097 numRow=10;
00098 }
00099
00100 };
00101
00102
00111 template <class EdgeMeshType>
00112 class ExporterSVG
00113 {
00114
00115 public:
00116
00117
00118
00119 static bool Save(EdgeMeshType &m, const char *filename, SVGProperties & pro)
00120 {
00121 std::vector<EdgeMeshType*> MeshVec;
00122 MeshVec.push_back(&m);
00123 return Save(MeshVec,filename,pro);
00124 }
00125
00126
00127
00128
00129 static bool Save(std::vector<EdgeMeshType*> &meshVec, const char *filename, SVGProperties & pro)
00130 {
00131 FILE * fpo = fopen(filename,"w");
00132 if (fpo==NULL) return false;
00133
00134 WriteXmlHead(fpo, pro);
00135 for(int i=0;i<meshVec.size();++i)
00136 {
00137 WriteXmlBody(fpo, *meshVec[i], pro, i );
00138 }
00139 fprintf(fpo, "</svg>");
00140 fclose(fpo);
00141 return true;
00142 }
00143
00144
00145 static void WriteXmlHead(FILE *o, SVGProperties & pro)
00146 {
00147 fprintf(o, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
00148 fprintf(o, "<!-- Created with vcg library -->\n");
00149 fprintf(o, "<svg width=\"%fcm\" height=\"%fcm\" \n",pro.pageSizeCm()[0], pro.pageSizeCm()[1]);
00150 fprintf(o, " xmlns=\"http://www.w3.org/2000/svg\" \n");
00151 fprintf(o, " xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n");
00152 fprintf(o, " xmlns:dc=\"http://purl.org/dc/elements/1.1/\" \n");
00153 fprintf(o, " xmlns:cc=\"http://web.resource.org/cc/\" \n");
00154 fprintf(o, " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n");
00155 fprintf(o, " xmlns:svg=\"http://www.w3.org/2000/svg\" \n \n");
00156 fprintf(o, "id=\"svg2\"> \n");
00157 fprintf(o, " <defs id=\"defs4\"/> \n");
00158 fprintf(o, " <metadata id=\"metadata7\"> \n");
00159 fprintf(o, " <rdf:RDF> \n");
00160 fprintf(o, " <cc:Work rdf:about=\"\"> \n");
00161 fprintf(o, " <dc:format>image/svg+xml</dc:format> \n");
00162 fprintf(o, " <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" /> \n");
00163 fprintf(o, " </cc:Work> \n");
00164 fprintf(o, " </rdf:RDF> \n");
00165 fprintf(o, "</metadata> \n \n");
00166 }
00167
00168 static void WriteXmlBody(FILE* fpo, EdgeMeshType &mp, SVGProperties &pro, int meshIndex)
00169 {
00170 int rowInd = meshIndex / pro.numCol;
00171 int colInd = meshIndex % pro.numCol;
00172
00173 fprintf(fpo, " <rect width= \" %fcm \" height= \" %fcm \" x=\"%fcm \" y=\"%fcm \" "
00174 " style= \" stroke-width:1pt; fill-opacity:0.0; stroke:rgb(0,0,0)\" /> \n",
00175 pro.sizeCm[0], pro.sizeCm[1], pro.marginCm[0]+colInd*(pro.sizeCm[0]+pro.marginCm[0]), pro.marginCm[1]+rowInd*(pro.sizeCm[1]+pro.marginCm[1]));
00176 fprintf(fpo, "<g stroke=\"%s\" stroke-linecap=\"%s\" stroke-width = \"%fpt\" > \n", pro.strokeColor.c_str(), pro.strokeLineCap.c_str(),pro.lineWidthPt/100.0f);
00177 fprintf(fpo, " <svg id = \"SliceNum%d\" viewBox=\"-1000 -1000 2000 2000\" width=\"%fcm\" height=\"%fcm\" x=\"%fcm\" y=\"%fcm\" >\n", meshIndex,pro.sizeCm[0],pro.sizeCm[1],
00178 pro.marginCm[0]+colInd*(pro.sizeCm[0]+pro.marginCm[0]), pro.marginCm[1]+rowInd*(pro.sizeCm[1]+pro.marginCm[1]) );
00179
00180
00181
00182 typename EdgeMeshType::EdgeIterator i;
00183
00184
00185
00186
00187
00188
00189
00190
00191 Matrix33f rotM = RotationMatrix(pro.projDir,Point3f(0,0,1),false);
00192 Point3f rotatedUp = rotM * pro.projUp;
00193 Point3f rotCenter = rotM * pro.projCenter;
00194 float scale = pro.scale;
00195 if(scale==0) scale = 2.0/mp.bbox.Diag();
00196
00197 for (i = mp.edges.begin(); i != mp.edges.end(); ++i)
00198 {
00199 Point3f p0 = (-rotCenter + rotM * ((*i).V(0)->P()))*scale*1000;
00200 Point3f p1 = (-rotCenter + rotM * ((*i).V(1)->P()))*scale*1000;
00201 fprintf(fpo, " <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" />\n", p0[0],p0[1],p1[0],p1[1]);
00202 }
00203
00204 fprintf(fpo, " </svg>\n");
00205 fprintf(fpo, "</g>\n");
00206
00207 }
00208
00209 };
00210
00211
00212 };
00213 };
00214 };
00215 #endif // __VCG_LIB_EXPORTER_SVG