svg.cpp 17 KB
Newer Older
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
21
22
23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
24
25
#include "Utils/svg.h"
#include "Utils/cgognStream.h"
Sylvain Thery's avatar
Sylvain Thery committed
26
27
#include <algorithm>
#include <typeinfo>
28
#include <GL/glew.h>
29
#include <Geometry/vector_gen.h>
30
31
32
33

namespace CGoGN
{

34
namespace Utils
35
36
37
38
39
{

namespace SVG
{

40

Sylvain Thery's avatar
Sylvain Thery committed
41
42
43
44
45
const std::vector<Geom::Vec3f>& SvgObj::vertices() const
{
	return  m_vertices;
}

Sylvain Thery's avatar
Sylvain Thery committed
46
void SvgObj::addVertex(const Geom::Vec3f& v)
47
48
{
	m_vertices.push_back(v);
49
	m_colors.push_back(m_color);
50
51
}

52
53
54
void SvgObj::addVertex3D(const Geom::Vec3f& v)
{
	m_vertices3D.push_back(v);
55
56
57
58
59
60
	m_colors.push_back(m_color);
}

void SvgObj::addVertex(const Geom::Vec3f& v, const Geom::Vec3f& c)
{
	m_vertices.push_back(v);
61
62
	if (m_colors.size() < m_vertices.size())
		m_colors.push_back(c);
63
64
65
66
67
}

void SvgObj::addVertex3D(const Geom::Vec3f& v, const Geom::Vec3f& c)
{
	m_vertices3D.push_back(v);
68
69
	if (m_colors.size() < m_vertices.size())
		m_colors.push_back(c);
70
71
72
}


Sylvain Thery's avatar
Sylvain Thery committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void SvgObj::addString(const Geom::Vec3f& v, const std::string& str)
{
	m_vertices.push_back(v);
	m_strings.push_back(str);
	m_colors.push_back(m_color);
}

void SvgObj::addString(const Geom::Vec3f& v, const std::string& str, const Geom::Vec3f& c)
{
	m_vertices.push_back(v);
	m_strings.push_back(str);
	m_colors.push_back(c);
}


88
89
90
91
92
void SvgObj::setWidth(float w)
{
	m_width=w;
}

Sylvain Thery's avatar
Sylvain Thery committed
93
94
95
96
97
98
99
100

void SvgObj::setColor(const Geom::Vec3f& c)
{
	m_color=c;
}


void SvgObj::close()
101
102
103
104
{
	m_vertices.push_back(m_vertices.front());
}

105
106
unsigned int SvgObj::nbv() const
{
107
	return m_vertices.size();
108
109
110
111
}

const Geom::Vec3f& SvgObj::P(unsigned int i) const
{
112
	return m_vertices[i];
113
114
}

Sylvain Thery's avatar
Sylvain Thery committed
115
116
117
118
119
120
121
122
123

Geom::Vec3f SvgObj::normal()
{
	if (m_vertices.size()<3)
	{
		CGoGNerr << "Error SVG normal computing (not enough points)"<<CGoGNendl;
		return Geom::Vec3f(0.0f,0.0f,0.0f);
	}

124
125
	Geom::Vec3f U = m_vertices3D[2] - m_vertices3D[1];
	Geom::Vec3f V = m_vertices3D[0] - m_vertices3D[1];
Sylvain Thery's avatar
Sylvain Thery committed
126
127
128
129
130
131
132
133
134

	Geom::Vec3f N = U^V;

	N.normalize(); // TO DO verify that is necessary

	return N;
}


135
void SvgPoints::save(std::ofstream& out) const
Sylvain Thery's avatar
Sylvain Thery committed
136
{
137
//	std::stringstream ss;
Sylvain Thery's avatar
Sylvain Thery committed
138

139
140
	unsigned int nb = m_vertices.size();
	for (unsigned int i=0; i<nb; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
141
	{
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
		saveOne(out,i);
//		out << "<circle cx=\""<< m_vertices[i][0];
//		out << "\" cy=\""<< m_vertices[i][1];
//		out << "\" r=\""<< m_width;
//		out << "\" style=\"stroke: none; fill: #";
//
//		out << std::hex;
//		unsigned int wp = out.width(2);
//		char prev = out.fill('0');
//		out << int(m_colors[i][0]*255);
//		out.width(2); out.fill('0');
//		out<< int(m_colors[i][1]*255);
//		out.width(2); out.fill('0');
//		out << int(m_colors[i][2]*255)<<std::dec;
//		out.fill(prev);
//		out.width(wp);
//
//		out <<"\"/>"<< std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
160
161
162
	}
}

163

Sylvain Thery's avatar
Sylvain Thery committed
164
void SvgPoints::saveOne(std::ofstream& out, unsigned int i, unsigned int bbl) const
165
166
167
{
	std::stringstream ss;

168
169
170
171
172
	out << "<circle cx=\""<< m_vertices[i][0];
	out << "\" cy=\""<< m_vertices[i][1];
	out << "\" r=\""<< m_width;
	out << "\" style=\"stroke: none; fill: #";

173
174
175
	out << std::hex;
	unsigned int wp = out.width(2);
	char prev = out.fill('0');
176
	out << int(m_colors[i][0]*255);
177
	out.width(2); out.fill('0');
178
	out<< int(m_colors[i][1]*255);
179
	out.width(2); out.fill('0');
180
	out << int(m_colors[i][2]*255)<<std::dec;
181
182
	out.fill(prev);
	out.width(wp);
183

184
	out <<"\"/>"<< std::endl;
185
}
186

187

188

189
190
191
unsigned int SvgPoints::nbPrimtives() const
{
	return m_vertices.size();
192
193
}

194

195
void SvgPoints::fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const
Sylvain Thery's avatar
Sylvain Thery committed
196
{
197
198
199
200
	for (unsigned int i = 0; i< m_vertices.size(); ++i)
	{
		vds.push_back(DepthSort(idObj,i,m_vertices[i][2]));
	}
Sylvain Thery's avatar
Sylvain Thery committed
201
202
}

203
204
205
void SvgLines::save(std::ofstream& out) const
{
	std::stringstream ss;
Sylvain Thery's avatar
Sylvain Thery committed
206

207
208
209
210
211
	unsigned int nb = m_vertices.size()/2;
	for (unsigned int i=0; i<nb; ++i)
		saveOne(out,i);
}

Sylvain Thery's avatar
Sylvain Thery committed
212
void SvgLines::saveOne(std::ofstream& out, unsigned int i, unsigned int bbl) const
Sylvain Thery's avatar
Sylvain Thery committed
213
214
215
{
	std::stringstream ss;

216
	out << "<polyline fill=\"none\" stroke=\"#";
Sylvain Thery's avatar
Sylvain Thery committed
217
218
219
	out << std::hex;
	unsigned int wp = out.width(2);
	char prev = out.fill('0');
Sylvain Thery's avatar
Sylvain Thery committed
220
	out << int(m_colors[2*i][0]*255);
Sylvain Thery's avatar
Sylvain Thery committed
221
	out.width(2); out.fill('0');
Sylvain Thery's avatar
Sylvain Thery committed
222
	out<< int(m_colors[2*i][1]*255);
Sylvain Thery's avatar
Sylvain Thery committed
223
	out.width(2); out.fill('0');
Sylvain Thery's avatar
Sylvain Thery committed
224
	out << int(m_colors[2*i][2]*255)<<std::dec;
Sylvain Thery's avatar
Sylvain Thery committed
225
226
227
	out <<"\" stroke-width=\""<<m_width<<"\" points=\"";
	out.fill(prev);
	out.width(wp);
228
229
	out << m_vertices[2*i][0] << ","<< m_vertices[2*i][1]<< " ";
	out << m_vertices[2*i+1][0] << ","<< m_vertices[2*i+1][1];
Sylvain Thery's avatar
Sylvain Thery committed
230
	out <<"\"/>"<< std::endl;
231
}
Sylvain Thery's avatar
Sylvain Thery committed
232

233
234
235
unsigned int SvgLines::nbPrimtives() const
{
	return m_vertices.size()/2;
Sylvain Thery's avatar
Sylvain Thery committed
236
237
}

238
239
240
241
242
243
244
245
246
247
248
void SvgLines::fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const
{
	unsigned int nb = m_vertices.size()/2;
	for (unsigned int i = 0; i<nb; ++i)
	{
		float depth = (m_vertices[2*i][2] + m_vertices[2*i+1][2])/2.0f;
		vds.push_back(DepthSort(idObj,i,depth));
	}
}


Sylvain Thery's avatar
Sylvain Thery committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301


void SvgStrings::save(std::ofstream& out) const
{
	unsigned int nb = m_vertices.size();
	for (unsigned int i=0; i<nb; ++i)
	{
		saveOne(out,i);
	}
}


void SvgStrings::saveOne(std::ofstream& out, unsigned int i, unsigned int bbl) const
{

	float scale = m_sf * sqrt(1.0f - m_vertices[i][2])*0.007f*float(bbl);

	out << "<text style=\"font-size:24px;";
	out << "font-family:Bitstream Vera Sans\" ";
	 out << "transform=\"scale(" << scale <<"," << scale <<")\""<< std::endl;
	out << "x=\""<< (m_vertices[i][0])/scale << "\" y=\""<< (m_vertices[i][1])/scale<< "\">";
	out << "<tspan style=\"fill:#";

	out << std::hex;
	unsigned int wp = out.width(2);
	char prev = out.fill('0');
	out << int(m_colors[i][0]*255);
	out.width(2); out.fill('0');
	out<< int(m_colors[i][1]*255);
	out.width(2); out.fill('0');
	out << int(m_colors[i][2]*255)<<std::dec;
	out.fill(prev);
	out.width(wp);

	out <<"\">"<< m_strings[i]<< "</tspan></text>"<< std::endl;
}


unsigned int SvgStrings::nbPrimtives() const
{
	return m_vertices.size();
}

void SvgStrings::fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const
{
	for (unsigned int i = 0; i< m_vertices.size(); ++i)
	{
		vds.push_back(DepthSort(idObj,i,m_vertices[i][2]));
	}
}



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//void SvgPolyline::save(std::ofstream& out)
//{
//	std::stringstream ss;
//
//	out << "<polyline fill=\"none\" stroke=\"#";
//	out << std::hex;
//	unsigned int wp = out.width(2);
//	char prev = out.fill('0');
//	out << int(m_color[0]*255);
//	out.width(2); out.fill('0');
//	out<< int(m_color[1]*255);
//	out.width(2); out.fill('0');
//	out << int(m_color[2]*255)<<std::dec;
//	out <<"\" stroke-width=\""<<m_width<<"\" points=\"";
//	out.fill(prev);
//	out.width(wp);
//	for (std::vector<Geom::Vec3f>::iterator it =m_vertices.begin(); it != m_vertices.end(); ++it)
//	{
//		out << (*it)[0] << ","<< (*it)[1]<< " ";
//	}
//	out <<"\"/>"<< std::endl;
//}
//
//void SvgPolygon::setColorFill(const Geom::Vec3f& c)
//{
//	m_colorFill=c;
//}
//
//
//void SvgPolygon::save(std::ofstream& out)
//{
//	std::stringstream ss;
//
//	out << "<polyline fill=\"";
//	out << std::hex;
//	unsigned int wp = out.width(2);
//	char prev = out.fill('0');
//	out << int(m_colorFill[0]*255);
//	out.width(2); out.fill('0');
//	out<< int(m_colorFill[1]*255);
//	out.width(2); out.fill('0');
//	out << int(m_colorFill[2]*255);
//
//	out << "none\" stroke=\"#";
//	wp = out.width(2);
//	prev = out.fill('0');
//	out << int(m_color[0]*255);
//	out.width(2); out.fill('0');
//	out<< int(m_color[1]*255);
//	out.width(2); out.fill('0');
//	out << int(m_color[2]*255)<<std::dec;
//
//	out <<"\" stroke-width=\""<<m_width<<"\" points=\"";
//	out.fill(prev);
//	out.width(wp);
//	for (std::vector<Geom::Vec3f>::iterator it =m_vertices.begin(); it != m_vertices.end(); ++it)
//	{
//		out << (*it)[0] << ","<< (*it)[1]<< " ";
//	}
//	out <<"\"/>"<< std::endl;
//
//}

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383




SVGOut::SVGOut(const std::string& filename, const glm::mat4& model, const glm::mat4& proj):
		m_model(model),m_proj(proj),global_color(Geom::Vec3f(0.0f,0.0f,0.0f)), global_width(2.0f)
{
	m_objs.reserve(1000);

	m_out = new std::ofstream(filename.c_str()) ;
	if (!m_out->good())
	{
		CGoGNerr << "Unable to open file " << CGoGNendl ;
		// ????
	}

	glGetIntegerv(GL_VIEWPORT, &(m_viewport[0]));
}

Sylvain Thery's avatar
Sylvain Thery committed
384
385
386
387
388
389
390
391
392
393
394
395

SVGOut::SVGOut(const glm::mat4& model, const glm::mat4& proj):
		m_model(model),m_proj(proj),global_color(Geom::Vec3f(0.0f,0.0f,0.0f)), global_width(2.0f)
{
	m_objs.reserve(1000);

	m_out = NULL;

	glGetIntegerv(GL_VIEWPORT, &(m_viewport[0]));
}


396
397
SVGOut::~SVGOut()
{
Sylvain Thery's avatar
Sylvain Thery committed
398
	if (m_out && (m_out->good()))
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
	{
		closeFile();
	}
	delete m_out;

	for (std::vector<SvgObj*>::iterator it = m_objs.begin(); it != m_objs.end(); ++it)
		delete (*it);
}

void SVGOut::setColor(const Geom::Vec3f& col)
{
	global_color = col;
}

void SVGOut::setWidth(float w)
{
	global_width = w;
}

void SVGOut::closeFile()
{
Sylvain Thery's avatar
Sylvain Thery committed
420
421
422
423
	m_bbX0 = 100000;
	m_bbY0 = 100000;
	m_bbX1 = 0;
	m_bbY1 = 0;
Sylvain Thery's avatar
Sylvain Thery committed
424
	computeBB(m_bbX0, m_bbY0, m_bbX1, m_bbY1);
Sylvain Thery's avatar
Sylvain Thery committed
425
426
427
428

	*m_out << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"<< std::endl;
	*m_out << "<svg xmlns=\"http://www.w3.org/2000/svg\""<< std::endl;
	*m_out << " xmlns:xlink=\"http://www.w3.org/1999/xlink\""<< std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
429
	*m_out << "viewBox=\""<< m_bbX0 <<" "<< m_bbY0 <<" "<< m_bbX1-m_bbX0 << " " << m_bbY1-m_bbY0 <<"\">"<< std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
430
431
432
433
434
435
436
437
438
	*m_out << "<title>test</title>"<< std::endl;
	*m_out << "<desc>"<< std::endl;
	*m_out << "Rendered from CGoGN"<< std::endl;

	*m_out << "</desc>"<< std::endl;
	*m_out << "<defs>"<< std::endl;
	*m_out << "</defs>"<< std::endl;
	*m_out << "<g shape-rendering=\"crispEdges\">" << std::endl;

439
440
	std::vector<DepthSort> vds;
	sortSimpleDepth(vds);
441

442
443

	for (std::vector<DepthSort>::iterator it = vds.begin(); it != vds.end(); ++it)
444
	{
Sylvain Thery's avatar
Sylvain Thery committed
445
		m_objs[it->obj]->saveOne(*m_out,it->id, m_bbX1-m_bbX0);
446
447
	}

448

449
450
451
452
453
454
	*m_out << "</g>" << std::endl;
	*m_out << "</svg>" << std::endl;
	m_out->close();
}


Sylvain Thery's avatar
Sylvain Thery committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
void SVGOut::computeBB(unsigned int& a, unsigned int& b, unsigned int& c, unsigned& d)
{

	for (std::vector<SvgObj*>::iterator it = m_objs.begin(); it != m_objs.end(); ++it)
	{
		const std::vector<Geom::Vec3f>& vert = (*it)->vertices();
		for (std::vector<Geom::Vec3f>::const_iterator j = vert.begin(); j != vert.end(); ++j)
		{
			const Geom::Vec3f& P = *j;
			if (P[0]<a)
				a = (unsigned int)(P[0]);
			if (P[1]<b)
				b = (unsigned int)(P[1]);
			if (P[0]>c)
				c = (unsigned int)(P[0]);
			if (P[1]>d)
				d = (unsigned int)(P[1]);
		}

		if (a>10)
			a-=10;
		if (b>10)
			b-=10;
		c+=10;
		d+=10;
	}
}


484

485
486
487
488
489
490
491
492
493
494
void SVGOut::beginPoints()
{
	m_current = new SvgPoints();
	m_current->setColor(global_color);
	m_current->setWidth(global_width);
}

void SVGOut::endPoints()
{
	m_objs.push_back(m_current);
495
	m_current = NULL;
496
497
498
499
500
}

void SVGOut::addPoint(const Geom::Vec3f& P)
{
	glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport);
Thery Sylvain's avatar
Thery Sylvain committed
501
//	glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),m_viewport);
502
	m_current->addVertex(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])));
503
504
505
506
507
}

void SVGOut::addPoint(const Geom::Vec3f& P, const Geom::Vec3f& C)
{
	glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport);
Thery Sylvain's avatar
Thery Sylvain committed
508
//	glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),m_viewport);
509
	m_current->addVertex(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])),C);
510
511
512
513
514
515
516
517
518
519
520
521
522
523
}


void SVGOut::beginLines()
{
	m_current = new SvgLines();
	m_current->setColor(global_color);
	m_current->setWidth(global_width);
}


void SVGOut::endLines()
{
	m_objs.push_back(m_current);
524
	m_current = NULL;
525
526
527
528
529
}

void SVGOut::addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2)
{
	glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport);
Thery Sylvain's avatar
Thery Sylvain committed
530
//	glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),m_viewport);
531
532

	glm::vec3 Q2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,m_proj,m_viewport);
Thery Sylvain's avatar
Thery Sylvain committed
533
//	glm::vec3 R2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,glm::mat4(1.0),m_viewport);
534

535
536
	m_current->addVertex(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])));
	m_current->addVertex(Geom::Vec3f(float(Q2[0]),float(m_viewport[3])-float(Q2[1]),float(Q2[2])));
537
538
539
540
541
542
543
}



void SVGOut::addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2, const Geom::Vec3f& C)
{
	glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport);
Thery Sylvain's avatar
Thery Sylvain committed
544
//	glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),m_viewport);
545
546

	glm::vec3 Q2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,m_proj,m_viewport);
Thery Sylvain's avatar
Thery Sylvain committed
547
//	glm::vec3 R2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,glm::mat4(1.0),m_viewport);
548

549
550
	m_current->addVertex(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])),C);
	m_current->addVertex(Geom::Vec3f(float(Q2[0]),float(m_viewport[3])-float(Q2[1]),float(Q2[2])),C);
551
552
553
}


Sylvain Thery's avatar
Sylvain Thery committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
void SVGOut::beginStrings(float scalefactor)
{
	m_current = new SvgStrings(scalefactor);
	m_current->setColor(global_color);
	m_current->setWidth(global_width);
}

void SVGOut::endStrings()
{
	m_objs.push_back(m_current);
	m_current = NULL;
}

void SVGOut::addString(const Geom::Vec3f& P, const std::string& str)
{
	glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport);
	m_current->addString(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])), str);
}


void SVGOut::addString(const Geom::Vec3f& P, const std::string& str, const Geom::Vec3f& C)
{
	glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport);
	m_current->addString(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])), str, C);
}


581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
void SVGOut::sortSimpleDepth(std::vector<DepthSort>& vds)
{
	unsigned int nb=0;
	for (std::vector<SvgObj*>::iterator it = m_objs.begin(); it != m_objs.end(); ++it)
		nb += (*it)->nbPrimtives();

	vds.reserve(nb);

	for (unsigned int i=0; i< m_objs.size(); ++i)
	{
		m_objs[i]->fillDS(vds,i);
	}
	std::sort(vds.begin(),vds.end());
}

596

597

Sylvain Thery's avatar
Sylvain Thery committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667



void AnimatedSVGOut::add(SVGOut* svg)
{
	m_svgs.push_back(svg);
}

void AnimatedSVGOut::write(const std::string& filename, float timeStep)
{
	std::ofstream outfile(filename.c_str()) ;

	unsigned int bbX0=1000000;
	unsigned int bbY0=1000000;
	unsigned int bbX1=0;
	unsigned int bbY1=0;

	for (unsigned int i=0; i< m_svgs.size(); ++i)
		m_svgs[i]->computeBB(bbX0, bbY0, bbX1, bbY1);

	outfile << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"<< std::endl;
	outfile << "<svg xmlns=\"http://www.w3.org/2000/svg\""<< std::endl;
	outfile << " xmlns:xlink=\"http://www.w3.org/1999/xlink\""<< std::endl;
	outfile << "viewBox=\""<< bbX0 <<" "<< bbY0 <<" "<< bbX1-bbX0 << " " << bbY1-bbY0 <<"\">"<< std::endl;
	outfile << "<title>Animated SVG</title>"<< std::endl;
	outfile << "<desc>"<< std::endl;
	outfile << "Rendered from CGoGN"<< std::endl;
	outfile << "</desc>"<< std::endl;

	outfile << "<defs>"<< std::endl;


	for (unsigned int i=0; i< m_svgs.size(); ++i)
	{
		std::vector<DepthSort> vds;
		m_svgs[i]->sortSimpleDepth(vds);

		outfile << "<g id=\"slice"<<i<< "\">" << std::endl;
		for (std::vector<DepthSort>::iterator it = vds.begin(); it != vds.end(); ++it)
			m_svgs[i]->m_objs[it->obj]->saveOne(outfile,it->id, bbX1-bbX0);
		outfile << "</g>" << std::endl;
	}

	outfile << "</defs>"<< std::endl;


	for (unsigned int i=0; i< m_svgs.size(); ++i)
	{
		unsigned int nbo = m_svgs[i]->m_opacities_animations.size();

		outfile << "<use xlink:href=\"#slice"<<i<<"\" >" << std::endl;
		outfile << "<animate attributeName=\"opacity\" dur=\""<<timeStep*nbo<<"s\" fill=\"freeze\" values=\"";


		for (unsigned int j = 0; j < nbo; ++j)
		{
			outfile << m_svgs[i]->m_opacities_animations[j];
			if ( j != (nbo-1))
				outfile<< ";";
		}

		outfile << "\" calcMode=\"discrete\" repeatCount=\"indefinite\" />" << std::endl;
		outfile << "</use>" << std::endl;
	}

	outfile << "</svg>" << std::endl;
	outfile.close();
}


668
} // namespace SVG
669
} // namespace Utils
670
671
672
} // namespace CGoGN