svg.h 6.22 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 51

#include "Geometry/vector_gen.h"

#include "glm/gtc/matrix_transform.hpp"
#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
{


52 53 54 55 56 57 58 59 60 61 62 63
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*/}

};


64 65 66 67 68
class SvgObj
{
protected:
	std::vector<Geom::Vec3f> m_vertices;
	std::vector<Geom::Vec3f> m_colors;
Sylvain Thery's avatar
Sylvain Thery committed
69
	std::vector<std::string> m_strings;
70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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
84 85 86 87
	void addString(const Geom::Vec3f& v, const std::string& str);

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

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

	void setWidth(float w);

	void close();

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

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

	unsigned int nbv() const;

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

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

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

	Geom::Vec3f normal();

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

110 111 112 113 114
};

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

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

143 144 145 146 147 148 149 150 151 152 153 154 155 156
//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);
//};
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175




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

	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
176 177 178 179 180 181 182 183

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



Sylvain Thery's avatar
Sylvain Thery committed
184 185 186
protected:
	void computeBB(unsigned int& a, unsigned int& b, unsigned int& c, unsigned& d);

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
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);

	/**
	 * destructor
	 * flush and close the file
	 */
	~SVGOut();

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

	void setWidth(float w);

	void closeFile();

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

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

227 228
	void sortSimpleDepth( std::vector<DepthSort>& vds);

229 230 231 232 233 234 235 236 237 238 239
};




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


#endif