Commit e0f1b877 authored by Sylvain Thery's avatar Sylvain Thery
Browse files

Add basic depth sort to svg edge/points

parent c2731df0
......@@ -48,6 +48,18 @@ namespace SVG
{
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*/}
};
class SvgObj
{
protected:
......@@ -73,10 +85,16 @@ public:
void close();
virtual void save(std::ofstream& out)=0;
virtual void save(std::ofstream& out) const = 0;
virtual void saveOne(std::ofstream& out, unsigned int i) const = 0;
unsigned int nbv() const;
virtual unsigned int nbPrimtives() const = 0;
virtual void fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const = 0;
const Geom::Vec3f& P(unsigned int i) const;
Geom::Vec3f normal();
......@@ -88,29 +106,36 @@ public:
class SvgPoints: public SvgObj
{
public:
void save(std::ofstream& out);
void save(std::ofstream& out) const;
void saveOne(std::ofstream& out, unsigned int i) const;
unsigned int nbPrimtives() const;
void fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const;
};
class SvgLines: public SvgObj
{
public:
void save(std::ofstream& out);
void save(std::ofstream& out) const;
void saveOne(std::ofstream& out, unsigned int i) const;
unsigned int nbPrimtives() const;
void fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const;
};
class SvgPolyline: 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 SvgPolygon: public SvgObj
{
protected:
Geom::Vec3f m_colorFill;
public:
void setColorFill(const Geom::Vec3f& c);
void save(std::ofstream& out);
};
......@@ -165,6 +190,8 @@ public:
void addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2);
void addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2, const Geom::Vec3f& C);
void sortSimpleDepth( std::vector<DepthSort>& vds);
};
......
......@@ -26,6 +26,7 @@
#include <algorithm>
#include <typeinfo>
#include <GL/glew.h>
#include <Geometry/vector_gen.h>
namespace CGoGN
{
......@@ -88,12 +89,12 @@ void SvgObj::close()
unsigned int SvgObj::nbv() const
{
return m_vertices3D.size();
return m_vertices.size();
}
const Geom::Vec3f& SvgObj::P(unsigned int i) const
{
return m_vertices3D[i];
return m_vertices[i];
}
......@@ -116,126 +117,201 @@ Geom::Vec3f SvgObj::normal()
}
void SvgPoints::save(std::ofstream& out)
void SvgPoints::save(std::ofstream& out) const
{
std::stringstream ss;
// std::stringstream ss;
unsigned int nb = m_vertices.size();
for (unsigned int i=0; i<nb; ++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;
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;
}
}
void SvgPolyline::save(std::ofstream& out)
void SvgPoints::saveOne(std::ofstream& out, unsigned int i) const
{
std::stringstream ss;
out << "<polyline fill=\"none\" stroke=\"#";
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_color[0]*255);
out << int(m_colors[i][0]*255);
out.width(2); out.fill('0');
out<< int(m_color[1]*255);
out<< int(m_colors[i][1]*255);
out.width(2); out.fill('0');
out << int(m_color[2]*255)<<std::dec;
out <<"\" stroke-width=\""<<m_width<<"\" points=\"";
out << int(m_colors[i][2]*255)<<std::dec;
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 SvgLines::save(std::ofstream& out)
{
std::stringstream ss;
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);
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 <<"\" stroke-width=\""<<m_width<<"\" points=\"";
out.fill(prev);
out.width(wp);
out << m_vertices[i][0] << ","<< m_vertices[i][1]<< " ";
i++;
out << m_vertices[i][0] << ","<< m_vertices[i][1];
out <<"\"/>"<< std::endl;
}
unsigned int SvgPoints::nbPrimtives() const
{
return m_vertices.size();
}
void SvgPolygon::setColorFill(const Geom::Vec3f& c)
void SvgPoints::fillDS(std::vector<DepthSort>& vds, unsigned int idObj) const
{
m_colorFill=c;
for (unsigned int i = 0; i< m_vertices.size(); ++i)
{
vds.push_back(DepthSort(idObj,i,m_vertices[i][2]));
}
}
void SvgLines::save(std::ofstream& out) const
{
std::stringstream ss;
void SvgPolygon::save(std::ofstream& out)
unsigned int nb = m_vertices.size()/2;
for (unsigned int i=0; i<nb; ++i)
{
saveOne(out,i);
// out << "<polyline fill=\"none\" stroke=\"#";
// 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 <<"\" stroke-width=\""<<m_width<<"\" points=\"";
// out.fill(prev);
// out.width(wp);
// out << m_vertices[i][0] << ","<< m_vertices[i][1]<< " ";
// i++;
// out << m_vertices[i][0] << ","<< m_vertices[i][1];
// out <<"\"/>"<< std::endl;
}
}
void SvgLines::saveOne(std::ofstream& out, unsigned int i) const
{
std::stringstream ss;
out << "<polyline fill=\"";
out << "<polyline fill=\"none\" stroke=\"#";
out << std::hex;
unsigned int wp = out.width(2);
char prev = out.fill('0');
out << int(m_colorFill[0]*255);
out << int(m_colors[i][0]*255);
out.width(2); out.fill('0');
out<< int(m_colorFill[1]*255);
out<< int(m_colors[i][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 << int(m_colors[i][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 << m_vertices[2*i][0] << ","<< m_vertices[2*i][1]<< " ";
out << m_vertices[2*i+1][0] << ","<< m_vertices[2*i+1][1];
out <<"\"/>"<< std::endl;
}
unsigned int SvgLines::nbPrimtives() const
{
return m_vertices.size()/2;
}
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));
}
}
//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;
//
//}
......@@ -312,13 +388,21 @@ void SVGOut::closeFile()
*m_out << "</defs>"<< std::endl;
*m_out << "<g shape-rendering=\"crispEdges\">" << std::endl;
// here do the sort in necessary
// here do the sort in necessary ?
std::vector<DepthSort> vds;
sortSimpleDepth(vds);
for (std::vector<SvgObj*>::iterator it = m_objs.begin(); it != m_objs.end(); ++it)
for (std::vector<DepthSort>::iterator it = vds.begin(); it != vds.end(); ++it)
{
(*it)->save(*m_out);
m_objs[it->obj]->saveOne(*m_out,it->id);
}
// for (std::vector<SvgObj*>::iterator it = m_objs.begin(); it != m_objs.end(); ++it)
// {
// (*it)->save(*m_out);
// }
*m_out << "</g>" << std::endl;
*m_out << "</svg>" << std::endl;
m_out->close();
......@@ -362,8 +446,8 @@ void SVGOut::computeBB(unsigned int& a, unsigned int& b, unsigned int& c, unsign
void SVGOut::beginPoints()
{
glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
// glm::i32vec4 viewport;
// glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
m_current = new SvgPoints();
m_current->setColor(global_color);
......@@ -380,23 +464,24 @@ 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);
glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),m_viewport);
m_current->addVertex(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]));
m_current->addVertex3D(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]));
m_current->addVertex(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])));
// m_current->addVertex3D(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]));
}
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);
glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),m_viewport);
m_current->addVertex(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]),C);
m_current->addVertex3D(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]),C);
// m_current->addVertex(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]),C);
m_current->addVertex(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])),C);
// m_current->addVertex3D(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]),C);
}
void SVGOut::beginLines()
{
glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
// glm::i32vec4 viewport;
// glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
m_current = new SvgLines();
m_current->setColor(global_color);
......@@ -418,11 +503,11 @@ void SVGOut::addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2)
glm::vec3 Q2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,m_proj,m_viewport);
glm::vec3 R2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,glm::mat4(1.0),m_viewport);
m_current->addVertex(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]));
m_current->addVertex(Geom::Vec3f(Q2[0],float(m_viewport[3])-Q2[1],Q2[2]));
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])));
m_current->addVertex3D(Geom::Vec3f(R[0],float(m_viewport[3])-R[1],R[2]));
m_current->addVertex3D(Geom::Vec3f(R2[0],float(m_viewport[3])-R2[1],R2[2]));
// m_current->addVertex3D(Geom::Vec3f(R[0],float(m_viewport[3])-R[1],R[2]));
// m_current->addVertex3D(Geom::Vec3f(R2[0],float(m_viewport[3])-R2[1],R2[2]));
}
......@@ -435,14 +520,29 @@ void SVGOut::addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2, const Geom::Ve
glm::vec3 Q2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,m_proj,m_viewport);
glm::vec3 R2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,glm::mat4(1.0),m_viewport);
m_current->addVertex(Geom::Vec3f(Q[0],float(m_viewport[3])-Q[1],Q[2]),C);
m_current->addVertex(Geom::Vec3f(Q2[0],float(m_viewport[3])-Q2[1],Q2[2]),C);
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);
m_current->addVertex3D(Geom::Vec3f(R[0],float(m_viewport[3])-R[1],R[2]),C);
m_current->addVertex3D(Geom::Vec3f(R2[0],float(m_viewport[3])-R2[1],R2[2]),C);
// m_current->addVertex3D(Geom::Vec3f(R[0],float(m_viewport[3])-R[1],R[2]),C);
// m_current->addVertex3D(Geom::Vec3f(R2[0],float(m_viewport[3])-R2[1],R2[2]),C);
}
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());
}
} // namespace SVG
......
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