svg.h 8.34 KB
Newer Older
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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef _UTIL_SVG_
#define _UTIL_SVG_

#include <vector>
#include <fstream>
#include <sstream>
Sylvain Thery's avatar
Sylvain Thery committed
31
#include <string>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#include "Geometry/vector_gen.h"

#include "glm/gtc/type_precision.hpp"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_projection.hpp"
#include "glm/gtc/matrix_transform.hpp"


namespace CGoGN
{

namespace Utils
{

namespace SVG
{


51 52 53 54 55 56 57 58 59 60 61 62
struct DepthSort
{
	unsigned int obj;
	unsigned int id;
	float depth;
	DepthSort(unsigned int o, unsigned int i, float d):
		obj(o),id(i),depth(d) {}
	bool operator<(const DepthSort& ds) const { return depth > ds.depth; /* inverse depth sortin*/}

};


63 64 65 66 67
class SvgObj
{
protected:
	std::vector<Geom::Vec3f> m_vertices;
	std::vector<Geom::Vec3f> m_colors;
Sylvain Thery's avatar
Sylvain Thery committed
68
	std::vector<std::string> m_strings;
69 70 71 72 73 74 75 76 77 78 79 80 81 82
	std::vector<Geom::Vec3f> m_vertices3D;
	Geom::Vec3f m_color;
	float m_width;
public:
	virtual ~SvgObj() {}

	void addVertex(const Geom::Vec3f& v);

	void addVertex3D(const Geom::Vec3f& v);

	void addVertex(const Geom::Vec3f& v, const Geom::Vec3f& C);

	void addVertex3D(const Geom::Vec3f& v, const Geom::Vec3f& C);

Sylvain Thery's avatar
Sylvain Thery committed
83 84 85 86
	void addString(const Geom::Vec3f& v, const std::string& str);

	void addString(const Geom::Vec3f& v, const std::string& str, const Geom::Vec3f& C);

87 88 89 90 91 92
	void setColor(const Geom::Vec3f& c);

	void setWidth(float w);

	void close();

93 94
	virtual void save(std::ofstream& out) const = 0;

Sylvain Thery's avatar
Sylvain Thery committed
95
	virtual void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const = 0;
96

97 98
	virtual void saveOneDepthAttenuation(std::ofstream& out, float da, unsigned int i, unsigned int bbl = 0) const = 0;

99 100
	unsigned int nbv() const;

101 102 103 104
	virtual unsigned int nbPrimtives() const = 0;

	virtual void fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const = 0;

105 106 107 108
	const Geom::Vec3f& P(unsigned int i) const;

	Geom::Vec3f normal();

Sylvain Thery's avatar
Sylvain Thery committed
109 110
	const std::vector<Geom::Vec3f>& vertices() const;

111 112 113 114 115
};

class SvgPoints: public SvgObj
{
public:
116
	void save(std::ofstream& out) const;
Sylvain Thery's avatar
Sylvain Thery committed
117
	void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const;
118
	void saveOneDepthAttenuation(std::ofstream& out, float da, unsigned int i, unsigned int bbl = 0) const;
119 120
	unsigned int nbPrimtives() const;
	void fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const;
121 122 123 124 125
};

class SvgLines: public SvgObj
{
public:
126
	void save(std::ofstream& out) const;
Sylvain Thery's avatar
Sylvain Thery committed
127
	void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const;
128
	void saveOneDepthAttenuation(std::ofstream& out, float da, unsigned int i, unsigned int bbl = 0) const;
Sylvain Thery's avatar
Sylvain Thery committed
129 130 131 132 133 134 135 136 137 138 139 140 141
	unsigned int nbPrimtives() const;
	void fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const;
};


class SvgStrings: public SvgObj
{
protected:
	float m_sf;
public:
	SvgStrings(float scalefactor = 1.0f) : m_sf(scalefactor) {}
	void save(std::ofstream& out) const;
	void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const;
142
	void saveOneDepthAttenuation(std::ofstream& out, float da, unsigned int i, unsigned int bbl = 0) const;
143 144
	unsigned int nbPrimtives() const;
	void fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const;
145 146
};

147 148 149 150 151 152 153 154 155 156 157 158 159 160
//class SvgPolyline: public SvgObj
//{
//public:
//	void save(std::ofstream& out);
//};
//
//class SvgPolygon: public SvgObj
//{
//protected:
//	Geom::Vec3f m_colorFill;
//public:
//	void setColorFill(const Geom::Vec3f& c);
//	void save(std::ofstream& out);
//};
161 162


Sylvain Thery's avatar
Sylvain Thery committed
163
class AnimatedSVGOut;
164

165
class SvgGroup
166 167
{

168 169
public:
	std::string m_name;
170 171 172 173 174 175 176 177 178 179
	const glm::mat4& m_model;
	const glm::mat4& m_proj;
	glm::i32vec4 m_viewport;

	Geom::Vec3f global_color;
	float global_width;

	std::vector<SvgObj*> m_objs;
	SvgObj* m_current;

Sylvain Thery's avatar
Sylvain Thery committed
180
	std::vector<float> m_opacities_animations;
Sylvain Thery's avatar
Sylvain Thery committed
181 182


183
	bool m_isLayer;
Sylvain Thery's avatar
Sylvain Thery committed
184

Sylvain Thery's avatar
Sylvain Thery committed
185

186 187 188 189 190 191 192 193
public:

	/**
	 * Object that allow the rendering/exporting in svg file
	 * @param filename file name ended by .svg
	 * @param model the modelview matrix
	 * @param proj the projection matrix
	 */
194
	SvgGroup(const std::string& name, const glm::mat4& model, const glm::mat4& proj);
195

Sylvain Thery's avatar
Sylvain Thery committed
196 197 198 199 200 201
	/**
	 * Object that allow the rendering/exporting in svg file
	 * no file parameter for use in animateSVG
	 * @param model the modelview matrix
	 * @param proj the projection matrix
	 */
202
	//SvgGroup(const glm::mat4& model, const glm::mat4& proj);
Sylvain Thery's avatar
Sylvain Thery committed
203

204 205 206 207
	/**
	 * destructor
	 * flush and close the file
	 */
208
	~SvgGroup();
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

	void setColor(const Geom::Vec3f& col);

	void setWidth(float w);


	void beginPoints();
	void endPoints();
	void addPoint(const Geom::Vec3f& P);
	void addPoint(const Geom::Vec3f& P, const Geom::Vec3f& C);


	void beginLines();
	void endLines();
	void addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2);
	void addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2, const Geom::Vec3f& C);

Sylvain Thery's avatar
Sylvain Thery committed
226 227 228 229 230 231 232

	void beginStrings(float scalefactor = 1.0f);
	void endStrings();
	void addString(const Geom::Vec3f& P, const std::string& str);
	void addString(const Geom::Vec3f& P, const Geom::Vec3f& Q, const std::string& str);
	void addString(const Geom::Vec3f& P, const std::string& str, const Geom::Vec3f& C);

233

234 235
	void sortSimpleDepth( std::vector<DepthSort>& vds);

Sylvain Thery's avatar
Sylvain Thery committed
236 237 238
	void addOpacityAnimation(float val) { m_opacities_animations.push_back(val);}
	void clearpacityAnimation()  { m_opacities_animations.clear();}

239
	void setToLayer() { m_isLayer = true; }
Sylvain Thery's avatar
Sylvain Thery committed
240

241
	//static void animateSVG(const std::string& filename, const std::vector<SVGOut*>& outs, float timestep);
242 243
};

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

class SVGOut
{
public:
	std::ofstream* m_out;

	const glm::mat4& m_model;
	const glm::mat4& m_proj;
	glm::i32vec4 m_viewport;

	std::vector<SvgGroup*> m_groups;

	unsigned int m_bbX0;
	unsigned int m_bbY0;
	unsigned int m_bbX1;
	unsigned int m_bbY1;

261 262
	float m_attFact;

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
protected:
	void computeBB(unsigned int& a, unsigned int& b, unsigned int& c, unsigned& d);

public:

	/**
	 * Object that allow the rendering/exporting in svg file
	 * @param filename file name ended by .svg
	 * @param model the modelview matrix
	 * @param proj the projection matrix
	 */
	SVGOut(const std::string& filename, const glm::mat4& model, const glm::mat4& proj);

	/**
	 * Object that allow the rendering/exporting in svg file
	 * no file parameter for use in animateSVG
	 * @param model the modelview matrix
	 * @param proj the projection matrix
	 */
	SVGOut(const glm::mat4& model, const glm::mat4& proj);

	~SVGOut();

	void addGroup(SvgGroup* group) { m_groups.push_back(group); }

	void write();
289 290 291 292 293 294

	/**
	 * @brief set Attenuation Factor of color with depth
	 * @param af 0.0 for none 1.0 for color to white,  more for faster attenuation (^att)
	 */
	void setAttenuationFactor(float af) { m_attFact = af;}
295 296 297
};


Sylvain Thery's avatar
Sylvain Thery committed
298 299 300 301 302 303 304 305
class AnimatedSVGOut
{
protected:
	std::vector<SVGOut*> m_svgs;

public:

	void write(const std::string& filename, float timeStep);
306

Sylvain Thery's avatar
Sylvain Thery committed
307 308 309
	void add(SVGOut* svg);

};
310 311 312 313 314 315 316 317


} // namespace SVG
} // namespace Utils
} // namespace CGogN


#endif