| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
class SVGProperties |
| 78 |
{ |
| 79 |
|
| 80 |
public: |
| 81 |
|
| 82 |
|
| 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 |
|
| 92 |
enum StrokeLineCap |
| 93 |
{ |
| 94 |
BUTT, ROUND, SQUARE |
| 95 |
}; |
| 96 |
|
| 97 |
static const int DEFAULT_LINE_WIDTH=2; |
| 98 |
|
| 99 |
|
| 100 |
int numCol; |
| 101 |
int numRow; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private: |
| 106 |
|
| 107 |
|
| 108 |
int lwidth; |
| 109 |
|
| 110 |
|
| 111 |
std::string stroke_color; |
| 112 |
|
| 113 |
|
| 114 |
std::string stroke_linecap; |
| 115 |
|
| 116 |
|
| 117 |
Plane3d proj; |
| 118 |
|
| 119 |
|
| 120 |
float scale; |
| 121 |
|
| 122 |
|
| 123 |
Point2d ViewBox; |
| 124 |
|
| 125 |
int width,height; |
| 126 |
|
| 127 |
|
| 128 |
Point2d position; |
| 129 |
|
| 130 |
|
| 131 |
bool showDetails; |
| 132 |
|
| 133 |
|
| 134 |
float Xmin, Ymin; |
| 135 |
Point2f* minPos; |
| 136 |
Point2f* maxPos; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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 |
|
| 154 |
Point3d n(0.0, 1.0, 0.0); |
| 155 |
proj.SetDirection(n); |
| 156 |
proj.SetOffset(0.0); |
| 157 |
|
| 158 |
scale=0; |
| 159 |
ViewBox=Point2d(1000, 1000); |
| 160 |
position=Point2d(0, 0); |
| 161 |
width=10; |
| 162 |
height=10; |
| 163 |
showDetails=true; |
| 164 |
Xmin=0; |
| 165 |
Ymin=0; |
| 166 |
minPos= new Point2f(0,0); |
| 167 |
maxPos= new Point2f(0,0); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
public: |
| 172 |
|
| 173 |
|
| 174 |
void setLineWidth(int width) |
| 175 |
{ |
| 176 |
lwidth = width; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 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 |
|
| 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; } |
| 217 |
|
| 218 |
void setViewBox(Point2d x) { ViewBox=x; } |
| 219 |
|
| 220 |
void setPosition(Point2d x) { position=x;} |
| 221 |
void setTextDetails(bool x){showDetails=x;} |
| 222 |
void setDimension(int width, int height){ this->width=width; this->height=height;} |
| 223 |
|
| 224 |
|
| 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 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
template <class EdgeMeshType> |
| 261 |
class ExporterSVG |
| 262 |
{ |
| 263 |
|
| 264 |
public: |
| 265 |
|
| 266 |
|
| 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 |
|
| 274 |
FILE * o = fopen(filename,"w"); |
| 275 |
if (o==NULL) |
| 276 |
return false; |
| 277 |
int num = (*vp).size(); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 388 |
|
| 389 |
Point3d p1proj = props.projPlane()->Projection(p1d); |
| 390 |
Point3d p2proj = props.projPlane()->Projection(p2d); |
| 391 |
|
| 392 |
|
| 393 |
|
| 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 |
|
| 409 |
if(!preCal){ |
| 410 |
float scale=props.getScale(); |
| 411 |
|
| 412 |
|
| 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 |
|
| 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 |
}; |
| 514 |
}; |
| 515 |
}; |
| 516 |
#endif |