svg.h 7.7 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

	unsigned int nbv() const;

99 100 101 102
	virtual unsigned int nbPrimtives() const = 0;

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

103 104 105 106
	const Geom::Vec3f& P(unsigned int i) const;

	Geom::Vec3f normal();

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

109 110 111 112 113
};

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

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

142 143 144 145 146 147 148 149 150 151 152 153 154 155
//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);
//};
156 157


Sylvain Thery's avatar
Sylvain Thery committed
158
class AnimatedSVGOut;
159

160
class SvgGroup
161 162
{

163 164
public:
	std::string m_name;
165 166 167 168 169 170 171 172 173 174
	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
175
	std::vector<float> m_opacities_animations;
Sylvain Thery's avatar
Sylvain Thery committed
176 177


178
	bool m_isLayer;
Sylvain Thery's avatar
Sylvain Thery committed
179

Sylvain Thery's avatar
Sylvain Thery committed
180

181 182 183 184 185 186 187 188
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
	 */
189
	SvgGroup(const std::string& name, const glm::mat4& model, const glm::mat4& proj);
190

Sylvain Thery's avatar
Sylvain Thery committed
191 192 193 194 195 196
	/**
	 * 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
	 */
197
	//SvgGroup(const glm::mat4& model, const glm::mat4& proj);
Sylvain Thery's avatar
Sylvain Thery committed
198

199 200 201 202
	/**
	 * destructor
	 * flush and close the file
	 */
203
	~SvgGroup();
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	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
221 222 223 224 225 226 227

	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);

228

229 230
	void sortSimpleDepth( std::vector<DepthSort>& vds);

Sylvain Thery's avatar
Sylvain Thery committed
231 232 233
	void addOpacityAnimation(float val) { m_opacities_animations.push_back(val);}
	void clearpacityAnimation()  { m_opacities_animations.clear();}

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

236
	//static void animateSVG(const std::string& filename, const std::vector<SVGOut*>& outs, float timestep);
237 238
};

239 240 241 242 243 244 245 246 247 248 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

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;

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();
};


Sylvain Thery's avatar
Sylvain Thery committed
285 286 287 288 289 290 291 292
class AnimatedSVGOut
{
protected:
	std::vector<SVGOut*> m_svgs;

public:

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

Sylvain Thery's avatar
Sylvain Thery committed
294 295 296
	void add(SVGOut* svg);

};
297 298 299 300 301 302 303 304


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


#endif