Commit a2b1046d authored by Sylvain Thery's avatar Sylvain Thery

moving svg utils fonctions in Utils

parent 1c459e9d
/*******************************************************************************
* 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>
#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
{
class SvgObj
{
protected:
std::vector<Geom::Vec3f> m_vertices;
std::vector<Geom::Vec3f> m_colors;
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);
void setColor(const Geom::Vec3f& c);
void setWidth(float w);
void close();
virtual void save(std::ofstream& out)=0;
unsigned int nbv() const;
const Geom::Vec3f& P(unsigned int i) const;
Geom::Vec3f normal();
};
class SvgPoints: public SvgObj
{
public:
void save(std::ofstream& out);
};
class SvgLines: public SvgObj
{
public:
void save(std::ofstream& out);
};
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);
};
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;
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);
};
} // namespace SVG
} // namespace Utils
} // namespace CGogN
#endif
......@@ -21,31 +21,22 @@
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#include "Algo/Render/SVG/mapSVGRender.h"
#include "Utils/svg.h"
#include "Utils/cgognStream.h"
#include <algorithm>
#include <typeinfo>
#include <GL/glew.h>
/**
* A set of functions that allow the creation of rendering
* object using Vertex-Buffer-Object.
* Function are made for dual-2-map and can be used on
* any subset of a dual-N-map which is a 2-map
*/
namespace CGoGN
{
namespace Algo
{
namespace Render
namespace Utils
{
namespace SVG
{
void SvgObj::addVertex(const Geom::Vec3f& v)
{
m_vertices.push_back(v);
......@@ -61,16 +52,23 @@ void SvgObj::addVertex3D(const Geom::Vec3f& v)
void SvgObj::addVertex(const Geom::Vec3f& v, const Geom::Vec3f& c)
{
m_vertices.push_back(v);
m_colors.push_back(c);
if (m_colors.size() < m_vertices.size())
m_colors.push_back(c);
}
void SvgObj::addVertex3D(const Geom::Vec3f& v, const Geom::Vec3f& c)
{
m_vertices3D.push_back(v);
m_colors.push_back(c);
if (m_colors.size() < m_vertices.size())
m_colors.push_back(c);
}
void SvgObj::setWidth(float w)
{
m_width=w;
}
void SvgObj::setColor(const Geom::Vec3f& c)
{
......@@ -83,6 +81,16 @@ void SvgObj::close()
m_vertices.push_back(m_vertices.front());
}
unsigned int SvgObj::nbv() const
{
return m_vertices3D.size();
}
const Geom::Vec3f& SvgObj::P(unsigned int i) const
{
return m_vertices3D[i];
}
Geom::Vec3f SvgObj::normal()
{
......@@ -107,7 +115,6 @@ void SvgPoints::save(std::ofstream& out)
{
std::stringstream ss;
// for (std::vector<Geom::Vec3f>::iterator it =m_vertices.begin(); it != m_vertices.end(); ++it)
unsigned int nb = m_vertices.size();
for (unsigned int i=0; i<nb; ++i)
{
......@@ -160,10 +167,10 @@ void SvgLines::save(std::ofstream& out)
{
std::stringstream ss;
// for (std::vector<Geom::Vec3f>::iterator it =m_vertices.begin(); it != m_vertices.end(); ++it)
unsigned int nb = m_vertices.size();
for (unsigned int i=0; i<nb; ++i)
{
out << "<polyline fill=\"none\" stroke=\"#";
out << std::hex;
unsigned int wp = out.width(2);
......@@ -281,11 +288,6 @@ void SVGOut::setWidth(float w)
void SVGOut::closeFile()
{
// here do the sort in necessary
// compNormObj cmp;
// std::sort(m_objs.begin(),m_objs.end(),cmp);
std::cout << "CLOSE"<< std::endl;
// std::list<SvgObj*> primitives;
for (std::vector<SvgObj*>::iterator it = m_objs.begin(); it != m_objs.end(); ++it)
{
......@@ -299,123 +301,6 @@ void SVGOut::closeFile()
// all points behind the plane +1
// all points before the plane -1
// all points colinear to the plane 0
// undefined 999
int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane, float& averageZ)
{
Geom::Vec3f N = pol_plane->normal();
if (N[2] > 0.0f)
N = -1.0f*N;
unsigned int nb = pol_points->nbv();
unsigned int nbback=0;
unsigned int nbfront=0;
unsigned int nb_col=0;
averageZ=0.0f;
for (unsigned int i=0; i< nb; ++i)
{
const Geom::Vec3f& Q = pol_points->P(i);
averageZ += Q[2];
Geom::Vec3f U = Q - pol_plane->P(0);
float ps = U*N;
if (fabs(ps) < 0.0001f)
nb_col++;
else
{
if (ps <0)
nbback++;
else
nbfront++;
}
}
averageZ /= float(nb);
if (nbfront==0)
return 1;
if (nbback==0)
return -1;
if (nb_col==nb)
return 0;
return 999;
}
bool compSvgObj::operator() (SvgObj* a, SvgObj*b)
{
SvgPolygon* p_a = dynamic_cast<SvgPolygon*>(a);
SvgPolygon* p_b = dynamic_cast<SvgPolygon*>(b);
if ((p_a!= NULL) && (p_b!=NULL)) // first case polygon/polygon
{
float avz_a;
int t1 = points_plane(p_a,p_b,avz_a);
if (t1==0) // colinear choose farthest
{
float za = p_a->P(0)[2];
float zb = p_b->P(0)[2];
return za > zb;
}
float avz_b;
int t2 = points_plane(p_b,p_a,avz_b);
// all point of a behind b
if ((t1 == 1)&&(t2==999))
return true;
// all point of b infront of a
if ((t2 == -1) && (t1==999))
return true;
if ((t1 == t2 )&& (t2!=999))
{
return avz_a > avz_b;
}
// all other cases ??
return false;
}
std::cout << "Cas non traite !!"<< std::endl;
return false;
}
bool compNormObj::operator() (SvgObj* a, SvgObj*b)
{
SvgPolygon* p_a = dynamic_cast<SvgPolygon*>(a);
SvgPolygon* p_b = dynamic_cast<SvgPolygon*>(b);
if ((p_a!= NULL) && (p_b!=NULL)) // first case polygon/polygon
{
Geom::Vec3f Na = p_a->normal();
Geom::Vec3f Nb = p_b->normal();
return fabs(Na[2]) > fabs(Nb[2]);
}
if ((p_a!= NULL)) // second case polygon/other
{
return true; // all polygon before segments.
}
std::cout << "Cas non traite !!"<< std::endl;
return false;
}
void SVGOut::beginPoints()
{
glm::i32vec4 viewport;
......@@ -429,6 +314,7 @@ void SVGOut::beginPoints()
void SVGOut::endPoints()
{
m_objs.push_back(m_current);
m_current = NULL;
}
void SVGOut::addPoint(const Geom::Vec3f& P)
......@@ -462,6 +348,7 @@ void SVGOut::beginLines()
void SVGOut::endLines()
{
m_objs.push_back(m_current);
m_current = NULL;
}
void SVGOut::addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2)
......@@ -500,8 +387,7 @@ void SVGOut::addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2, const Geom::Ve
} // namespace SVG
} // namespace Render
} // namespace Algo
} // namespace Utils
} // namespace CGoGN
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment