Debian Codegraph @

vcg/wrap/io_edgemesh/export_svg.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2006 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25
26 $Log: export_svg.h,v $
27 Revision 1.12 2008/04/06 06:50:00 cignoni
28 added missing namespaces
29
30 Revision 1.11 2007/07/12 11:02:06 andrenucci
31 Scale in SingleFile mode changed, it have to be calcolated before draw.
32
33 Revision 1.10 2007/07/10 07:48:41 cignoni
34 changed a template >> into > >
35
36 Revision 1.9 2007/07/10 06:58:31 cignoni
37 added a missing typename
38
39 Revision 1.8 2007/07/09 15:36:40 andrenucci
40 fix bug with exporting of translate plans
41
42 Revision 1.7 2007/06/13 09:17:14 andrenucci
43 Fix problem with scale
44
45 Revision 1.5 2007/05/29 10:09:29 cignoni
46 Added a const (and reformatted)
47
48 Revision 1.4 2007/05/21 13:22:40 cignoni
49 Corrected gcc compiling issues
50
51 Revision 1.3 2006/02/16 15:16:51 corsini
52 Add reference plane support
53
54 Revision 1.2 2006/02/15 15:40:06 corsini
55 Decouple SVG properties and exporter for simmetry with the other exporter
56
57 Revision 1.1 2006/02/13 16:18:09 corsini
58 first working version
59
60
61 ****************************************************************************/
62 #ifndef __VCG_LIB_EXPORTER_SVG
63 #define __VCG_LIB_EXPORTER_SVG
64
65 namespace vcg
66 {
67 namespace edge
68 {
69 namespace io
70 {
71
72 /**
73 * SVG Properties.
74 *
75 * Support class to set the properties of the SVG exporter.
76 */
77 class SVGProperties
78 {
79 // definitions
80 public:
81
82 //! Stroke colors.
83 enum StrokeColor
84 {
85 BLACK, SILVER, GRAY, WHITE,
86 MAROON, RED, PURPLE, FUCHSIA,
87 GREEN, LIME, OLIVE, YELLOW,
88 NAVY, BLUE, TEAL, AQUA
89 };
90
91 //! Stroke linecap types.
92 enum StrokeLineCap
93 {
94 BUTT, ROUND, SQUARE
95 };
96
97 static const int DEFAULT_LINE_WIDTH=2;
98
99 //in single-file export make the grid of sessiones
100 int numCol;
101 int numRow;
102
103
104 // private data members
105 private:
106
107 // Line width.
108 int lwidth;
109
110 // Stroke color (see StrokeColor).
111 std::string stroke_color;
112
113 // Stroke linecap (see StrokeLineCap).
114 std::string stroke_linecap;
115
116 // Plane where to project the edge mesh.
117 Plane3d proj;
118
119 // Scale of rdrawing respect coordinates of mesh
120 float scale;
121
122 // Dimension of the drawing square
123 Point2d ViewBox;
124
125 int width,height; //Express in cm
126
127 // Position of the drawing square
128 Point2d position;
129
130 //Text details
131 bool showDetails;
132
133 //Starting offset
134 float Xmin, Ymin;
135 Point2f* minPos;
136 Point2f* maxPos;
137
138
139
140 // construction
141 public:
142
143 SVGProperties()
144 {
145 lwidth = DEFAULT_LINE_WIDTH;
146
147 const char * DEFAULT_LINE_COLOR = "black";
148 const char * DEFAULT_LINE_CAP= "round";
149
150 stroke_color = DEFAULT_LINE_COLOR;
151 stroke_linecap = DEFAULT_LINE_CAP;
152
153 // default projection plane (XZ plane)
154 Point3d n(0.0, 1.0, 0.0);
155 proj.SetDirection(n);
156 proj.SetOffset(0.0);
157
158 scale=0; //scale=0 it means expanded to the boards of the canvas
159 ViewBox=Point2d(1000, 1000);
160 position=Point2d(0, 0);
161 width=10; //width of the windows
162 height=10; //height of the windows
163 showDetails=true;
164 Xmin=0;
165 Ymin=0;
166 minPos= new Point2f(0,0);
167 maxPos= new Point2f(0,0);
168 }
169
170 // public methods
171 public:
172
173 //! Set the line width.
174 void setLineWidth(int width)
175 {
176 lwidth = width;
177 }
178
179 //! Set the stroke color.
180 void setColor(enum StrokeColor color)
181 {
182 switch (color)
183 {
184 case BLACK : stroke_color = "black"; break;
185 case SILVER : stroke_color = "silver"; break;
186 case GRAY : stroke_color = "gray"; break;
187 case WHITE : stroke_color = "white"; break;
188 case MAROON : stroke_color = "maroon"; break;
189 case RED : stroke_color = "red"; break;
190 case PURPLE : stroke_color = "purple"; break;
191 case FUCHSIA: stroke_color = "fuchsia"; break;
192 case GREEN : stroke_color = "green"; break;
193 case OLIVE : stroke_color = "olive"; break;
194 case LIME : stroke_color = "lime"; break;
195 case NAVY : stroke_color = "navy"; break;
196 case TEAL : stroke_color = "teal"; break;
197 case AQUA : stroke_color = "aqua"; break;
198 default: assert(0);
199 }
200 }
201
202 //! Set the line cap style.
203 void setLineCap(enum StrokeLineCap linecap)
204 {
205 if (linecap == BUTT) stroke_linecap = "butt";
206 else if (linecap == ROUND) stroke_linecap = "round";
207 else if (linecap == SQUARE) stroke_linecap = "square";
208 }
209
210 void setPlane(double distance, const Point3d &direction)
211 {
212 proj.SetDirection(direction);
213 proj.SetOffset(distance);
214 }
215
216 void setScale(float x){ scale=x; } //Define the scale between 2d coordinate and mesh
217
218 void setViewBox(Point2d x) { ViewBox=x; }//Define the dimension of the square
219
220 void setPosition(Point2d x) { position=x;}//Define the starting position of the canvas
221 void setTextDetails(bool x){showDetails=x;}
222 void setDimension(int width, int height){ this->width=width; this->height=height;}
223
224 // accessors
225 public:
226
227 int lineWidth(){return lwidth;}
228 const char * lineColor(){return stroke_color.c_str();}
229 const char * lineCapStyle(){return stroke_linecap.c_str();}
230 const Plane3d * projPlane(){return &proj;}
231 float getScale(){return scale;}
232 Point2d getViewBox(){return ViewBox;}
233 Point2d getPosition(){return position;}
234
235 int getWidth(){return width;}
236 bool showTextDetails(){return showDetails;}
237 int getHeight(){return height;}
238
239 void setMinPoint(Point2f* p){
240 minPos = p;
241 }
242 void setMaxPoint(Point2f* p){
243 maxPos = p;
244 }
245 Point2f* getminPoint(){return (minPos);}
246 Point2f* getmaxPoint(){return (maxPos);}
247
248
249 };
250
251
252 /**
253 * SVG exporter.
254 *
255 * This exporter save a mesh of EdgeMesh type in the SVG format.
256 * Most of the features of the SVG format are not supported.
257 * The given EdgeMesh is saved as a set lines. The properties
258 * of the SVG export can be set through the SVGProp class.
259 */
260 template <class EdgeMeshType>
261 class ExporterSVG
262 {
263
264 public:
265
266 //! Save with the default SVG properties.
267 static bool Save(EdgeMeshType *mp, const char *filename)
268 {
269 SVGProperties properties;
270 return Save(mp, filename, properties);
271 }
272 static bool Save(std::vector<EdgeMeshType*> *vp, const char *filename, SVGProperties & pro){
273 //Function that export a vector of EdgeMesh in an unic single SVG file.
274 FILE * o = fopen(filename,"w");
275 if (o==NULL)
276 return false;
277 int num = (*vp).size(); //number of square to draw
278 WriteXmlHead(o,pro.getWidth(),pro.getHeight(),pro.getViewBox(),Point2d(0,0));
279 float scale= pro.getScale();
280 typename std::vector<EdgeMeshType*>::iterator it;
281 int i=0;
282 Point2f pmin(100000000.0f, 100000000.0f);
283 Point2f pmax(-10000000.0f, -10000000.0f);
284 for(it=(*vp).begin(); it!=(*vp).end(); it++){
285 EdgeMeshType* ed;
286 ed=(*it);
287 Save(ed,o,pro, -2);
288 Point2f* pmi=pro.getminPoint();
289 Point2f* pma=pro.getmaxPoint();
290 pmin[0]=min(pmin[0], pmi->X());
291 pmin[1]=min(pmin[1], pmi->Y());
292 pmax[0]=max(pmax[0], pma->X());
293 pmax[1]=max(pmax[1], pma->Y());
294 }
295 float maxEdge=std::max(pmax[0]-pmin[0], pmax[1]-pmin[1]);
296 float scl = (pro.getViewBox().V(0)/pro.numCol) /maxEdge ;
297 pro.setScale(scl);
298 pro.setMinPoint(new Point2f(pmin[0],pmin[1]));
299 pro.setMaxPoint(new Point2f(pmax[0],pmax[1]));
300 for(it=(*vp).begin(); it!=(*vp).end(); it++){
301
302 EdgeMeshType* ed;
303 ed=(*it);
304
305 pro.setPosition(Point2d((pro.getViewBox().V(0)/pro.numCol)*i,40));
306
307 int x=pro.getViewBox().V(0);
308 int y=pro.getViewBox().V(1);
309 /*
310 fprintf(o, "<rect width= \" %d \" height= \" %d \" x=\"%d \" y=\" %d \" style= \" stroke-width:1; fill-opacity:0.0; stroke:rgb(0,0,0)\" /> \n",x/pro.numCol,y, (x/pro.numCol)*i, 40);
311 */
312 Save(ed,o,pro, i);
313
314 if(pro.showTextDetails()){
315 fprintf(o,"<text x= \" %d \" y= \"30 \" font-family= \"Verdana \" font-size= \"30 \" >\n",(x/pro.numCol)*i);
316 fprintf(o,"Slice num:%d </text>\n", i);}
317 i++;
318 }
319 fprintf(o, "</svg>");
320 fclose(o);
321 return true;
322 }
323 //! Save with the given SVG properties.
324 static bool Save(EdgeMeshType *mp, const char *filename, SVGProperties & props )
325 {
326
327 FILE * o = fopen(filename,"w");
328 if (o==NULL)
329 return false;
330
331 WriteXmlHead(o, props.getWidth(),props.getHeight(), props.getViewBox(), props.getPosition());
332
333 props.setPosition(Point2d(0,40));
334 Save(mp, o, props, -1);
335 fprintf(o, "</svg>");
336
337
338 // final xml tags
339
340 fclose(o);
341 return true;
342 }
343
344 static void Save(EdgeMeshType *mp, FILE* o, SVGProperties props, int numSlice)
345 {
346 bool preCal=false;
347
348 if(numSlice==-2) preCal=true;
349 // build vector basis (n, v1, v2)
350 Point3d p1(0.0,0.0,0.0);
351 Point3d p2(1.0,0.0,0.0);
352 Point3d p2less(-1.0,0.0,0.0);
353 Point3d d = props.projPlane()->Direction() - p2;
354 Point3d dless = props.projPlane()->Direction() - p2less;
355 Point3d v1;
356 if ((d.Norm() < 0.5)||(dless.Norm() <0.5))
357 v1 = Point3d(0.0,0.0,1.0) - p1;
358 else
359 v1 = p2 - p1;
360
361 v1.Normalize();
362 Point3d v2 = v1 ^ props.projPlane()->Direction();
363 //Global points
364 std::vector< std::vector<Point2f> >* glb;
365 std::vector<Point2f> pts;
366 pts.clear();
367 Point2f pmin(100000000.0f, 100000000.0f);
368 Point2f pmax(-100000000.0f, -100000000.0f);
369 Point3d bbMin;
370
371 typename EdgeMeshType::EdgeIterator i;
372
373 for (i = mp->edges.begin(); i != mp->edges.end(); ++i)
374 {
375 Point3<typename EdgeMeshType::ScalarType> p1;
376 Point3<typename EdgeMeshType::ScalarType> p2;
377
378 p1 = (*i).V(0)->P();
379 p2 = (*i).V(1)->P();
380
381
382
383 Point3d p1d(p1[0], p1[1], p1[2]);
384 Point3d p2d(p2[0], p2[1], p2[2]);
385
386
387 // Project the line on the reference plane
388
389 Point3d p1proj = props.projPlane()->Projection(p1d);
390 Point3d p2proj = props.projPlane()->Projection(p2d);
391
392
393 // Convert the 3D coordinates of the line to the uv coordinates of the plane
394 Point2f pt1(static_cast<float>(p1proj * v1), static_cast<float>(p1proj * v2));
395 Point2f pt2(static_cast<float>(p2proj * v1), static_cast<float>(p2proj * v2));
396
397 pts.push_back(pt1);
398 pts.push_back(pt2);
399
400 pmin[0]=math::Min(math::Min(pt1[0],pmin[0]), pt2[0]);
401 pmin[1]=math::Min(math::Min(pt1[1],pmin[1]), pt2[1]);
402 pmax[0]=math::Max(math::Max(pt1[0],pmax[0]), pt2[0]);
403 pmax[1]=math::Max(math::Max(pt1[1],pmax[1]), pt2[1]);
404
405
406 }
407
408 //Point2f bbp(static_cast<float>(bbMin * v1), static_cast<float>(bbMin * v2));
409 if(!preCal){
410 float scale=props.getScale();
411
412 // line settings
413 Point2d pos=props.getPosition();
414
415 fprintf(o, " <g stroke=\"%s\" stroke-linecap=\"%s\" > \n",
416 props.lineColor(), props.lineCapStyle());
417 float maxEdges= math::Max((pmax[0]-pmin[0]), (pmax[1]-pmin[1]));
418 if (numSlice==0)
419 Draw_proportions_scale(o,maxEdges, props);
420 fprintf(o, "<svg id = \" %d \">\n", numSlice );
421 int x=props.getViewBox().V(0);
422 int y=props.getViewBox().V(1);
423 if(numSlice>=0)
424 fprintf(o, "<rect width= \" %d \" height= \" %d \" x=\"%d \" y=\" %d \" style= \" stroke-width:1; fill-opacity:0.0; stroke:rgb(0,0,0)\" /> \n",x/props.numCol,y, (x/props.numCol)*numSlice, 40);
425 else
426 fprintf(o, "<rect width= \" %d \" height= \" %d \" x=\"%d \" y=\" %d \" style= \" stroke-width:1; fill-opacity:0.0; stroke:rgb(0,0,0)\" /> \n",x/props.numCol,y, 0, 40);
427
428
429 std::vector<Point2f>::iterator itPoints;
430 for(itPoints = pts.begin(); itPoints != pts.end(); ++itPoints)
431 {
432 Point2f p1 = *itPoints;
433 ++itPoints;
434 Point2f p2 = *itPoints;
435 if(numSlice==-1){
436 fprintf(o, " <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" \n",
437 pos.X()+((p1[0]-pmin[0]) * scale), pos.Y()+((p1[1]-pmin[1]) * scale),
438 pos.X()+((p2[0]-pmin[0]) * scale), pos.Y()+((p2[1]-pmin[1]) * scale ));
439 fprintf(o, " stroke-width = \"%d\" ",props.lineWidth());
440 fprintf(o, "/>\n");
441 }
442 else{
443 fprintf(o, " <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" \n",
444 pos.X()+((p1[0]-props.getminPoint()->X()) * scale), pos.Y()+((p1[1]-props.getminPoint()->Y()) * scale),
445 pos.X()+((p2[0]-props.getminPoint()->X()) * scale), pos.Y()+((p2[1]-props.getminPoint()->Y()) * scale ));
446 fprintf(o, " stroke-width = \"%d\" ",props.lineWidth());
447 fprintf(o, "/>\n");}
448 }
449 fprintf(o, "</svg>");
450 fprintf(o, " </g>\n");
451 }
452 else{
453 Point2f* pmi=props.getminPoint();
454 Point2f* pma=props.getmaxPoint();
455 pmi->X()=pmin[0];
456 pmi->Y()=pmin[1];
457 pma->X()=pmax[0];
458 pma->Y()=pmax[1];
459 props.setMinPoint(pmi);
460 props.setMaxPoint(pma);
461
462 }
463 }
464
465 private:
466
467 static void Draw_proportions_scale(FILE *o, float maxEdge,SVGProperties & prop){
468 if(prop.showTextDetails()){
469 int num_order=log10(maxEdge);
470 int pox= pow(10.0, num_order);
471 int assX=prop.getViewBox()[0];
472 int assY=prop.getViewBox()[1];
473 int nullAss=0;
474 int OffsetXAs=50;
475 float comput= OffsetXAs+(pox*prop.getScale());
476 fprintf(o, " <line x1=\" %d \" y1=\" %d \" x2=\" %f \" y2=\" %d \" \n",(nullAss+OffsetXAs ),(assY+50),comput, (assY+50) );
477 fprintf(o, " stroke-width = \"5\" ");
478 fprintf(o, "/>\n");
479 fprintf(o,"<text x= \" %d \" y= \" %d \" font-family= \"Verdana \" font-size= \"25 \" >",(nullAss+OffsetXAs), (assY+80));
480
481
482 fprintf(o,"%d px -- Scale %f : 1 </text>", pox ,prop.getScale());
483 }
484 }
485 static void WriteXmlHead(FILE *o,int width, int height, Point2d viewBox, Point2d position){
486
487 int Vx=viewBox[0];
488 int Vy=viewBox[1];
489 // initial xml tags
490 fprintf(o, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
491 fprintf(o, "<!-- Created with vcg library -->\n");
492 fprintf(o, "<svg width=\"%d cm\" height=\"%d cm\" viewBox=\"0 0 %d %d \" \n",width, height, Vx, Vy+100);
493 fprintf(o, " xmlns:dc=\"http://purl.org/dc/elements/1.1/\" \n");
494 fprintf(o, " xmlns:cc=\"http://web.resource.org/cc/\" \n");
495 fprintf(o, " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n");
496 fprintf(o, " xmlns:svg=\"http://www.w3.org/2000/svg\" \n");
497 fprintf(o, " xmlns=\"http://www.w3.org/2000/svg\" \n");
498 fprintf(o, " id=\"svg2\"> \n");
499 fprintf(o, " <defs id=\"defs4\"/> \n");
500 fprintf(o, " <metadata id=\"metadata7\"> \n");
501 fprintf(o, " <rdf:RDF> \n");
502 fprintf(o, " <cc:Work rdf:about=\"\"> \n");
503 fprintf(o, " <dc:format>image/svg+xml</dc:format> \n");
504 fprintf(o, " <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" /> \n");
505 fprintf(o, " </cc:Work> \n");
506 fprintf(o, " </rdf:RDF> \n");
507 fprintf(o, " </metadata> \n");
508 }
509
510 };
511
512
513 }; // namespace io
514 }; // namespace edge
515 }; // namespace vcg
516 #endif // __VCG_LIB_EXPORTER_SVG