export_svg.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2006                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 #ifndef __VCG_LIB_EXPORTER_SVG
00025 #define __VCG_LIB_EXPORTER_SVG
00026 
00027 namespace vcg 
00028 {
00029   namespace tri
00030         {
00031                 namespace io 
00032                 {
00033 
00039 class SVGProperties
00040 {
00041         // definitions
00042 public:
00043         
00044         // When multiple meshes are passed, they are arranged in a grid according these two values.
00045         // the default is two column and enough row. If numRow is not sufficient it is automatically enlarged.
00046         int numCol;
00047         int numRow;
00048         
00049         Point2f sizeCm; // The size, in the drawing, of each ViewBox (in cm)
00050         
00051         Point2f marginCm; // how much space between each slice box (in cm)
00052   
00053         Point2f pageSizeCm() // This is automatically computed from the above values 
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;          // Direction of the Projection
00062         Point3f projUp;
00063         Point3f projCenter; // the 3d point that after projection will fall exactly in the center of the ViewBox.
00064         
00065         // How the mesh will be scaled. 
00066         // if this value is 0 the bounding box of all the passed meshes will be used to compute the scale and center
00067         // otherwise it is a scaling factor that is used to place the mesh in a unit cube (-1..1) 
00068         // usually it is 2/bbox.Diag
00069         float scale;
00070   
00071         // SVG Style Parameters
00072         int lineWidthPt;        // Line width.
00073         std::string strokeColor;        // Stroke color (see StrokeColor).
00074         std::string strokeLineCap;// Stroke linecap (see StrokeLineCap).
00075                 
00076         //Text details 
00077         bool showTextDetails;
00078 
00079 public:
00080 
00081                 SVGProperties()         
00082         {
00083                 lineWidthPt = 1;
00084                 strokeColor = "black";
00085                 strokeLineCap = "round";
00086 
00087                 // default projection (XZ plane with the z up)
00088                 projDir= Point3f(0.0, 1.0, 0.0);
00089     projUp = Point3f(0.0, 0.0, 1.0);
00090                 scale=0;                
00091                 //viewBox=Point2f(10, 10); 
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         // Standard saving Function
00118         // just a wrapper to the below
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         // Main saving function
00128         // save a Multiple Set of Edge Meshes on a single SVG files
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(size_t 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         // Main loop of edge printing
00182                 typename EdgeMeshType::EdgeIterator i;
00183                 
00184                 // XY projection. 
00185                 // It is a classcial ortho projection 
00186                 // eg it resolves to a rotation Matrix such that 
00187                 // - the passed projDir become the z axis
00188                 // - the passed projUp lie on the upper YZ plane.
00189                 
00190                 // First Step align projDir to Z
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.edge.begin(); i != mp.edge.end(); ++i) if(!(*i).IsD())
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                 };  // namespace io
00213         };  // namespace edge
00214 };  // namespace vcg
00215 #endif  // __VCG_LIB_EXPORTER_SVG


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:30:54