Skip to content

Commit

Permalink
A bit of cleanup around
Browse files Browse the repository at this point in the history
  • Loading branch information
alexp-sssup committed May 11, 2010
1 parent c5b9bf2 commit cbe643d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 162 deletions.
92 changes: 69 additions & 23 deletions geometry.cpp
Expand Up @@ -111,20 +111,6 @@ void GeomShape::Render(int x, int y) const
}
}

/*bool GeomShape::isOutlineClosed(const std::vector< Vector2 >& outline) const
{
return outline
}*/

/*void GeomShape::dumpEdges()
{
ofstream f("edges.dat");
for(unsigned int i=0;i<outline.size();i++)
f << outline[i].x << ' ' << outline[i].y << endl;
f.close();
}*/

void GeomShape::SetStyles(const std::list<FILLSTYLE>* styles)
{
static FILLSTYLE* clearStyle=NULL;
Expand Down Expand Up @@ -172,6 +158,7 @@ void GeomShape::TessellateGLU()
gluTessCallback(tess,GLU_TESS_VERTEX_DATA,(void(*)())GLUCallbackVertex);
gluTessCallback(tess,GLU_TESS_END_DATA,(void(*)())GLUCallbackEnd);
gluTessCallback(tess,GLU_TESS_COMBINE_DATA,(void(*)())GLUCallbackCombine);
gluTessProperty(tess,GLU_TESS_WINDING_RULE,GLU_TESS_WINDING_ODD);

//Let's create a vector of pointers to store temporary coordinates
//passed to GLU
Expand Down Expand Up @@ -261,14 +248,73 @@ void GeomShape::GLUCallbackCombine(GLdouble coords[3], void* vertex_data[4],
*outData=obj->tmpVertices.back();
}

/*//Shape are compared using the minimum vertex
bool GeomShape::operator<(const GeomShape& r) const
void ShapesBuilder::joinOutlines()
{
Vector2 vl=*min_element(outline.begin(),outline.end());
Vector2 vr=*min_element(r.outline.begin(),r.outline.end());
map< unsigned int, vector< vector<Vector2> > >::iterator it=shapesMap.begin();

for(;it!=shapesMap.end();it++)
{
vector< vector<Vector2> >& outlinesForColor=it->second;
//Repack outlines of the same color, avoiding excessive copying
for(int i=0;i<int(outlinesForColor.size());i++)
{
assert(outlinesForColor[i].size()>=2);
//Already closed paths are ok
if(outlinesForColor[i].front()==outlinesForColor[i].back())
continue;
for(int j=outlinesForColor.size()-1;j>=0;j--)
{
if(j==i || outlinesForColor[j].empty())
continue;
if(outlinesForColor[i].front()==outlinesForColor[j].back())
{
//Copy all the vertex but the origin in this one
outlinesForColor[j].insert(outlinesForColor[j].end(),
outlinesForColor[i].begin()+1,
outlinesForColor[i].end());
//Invalidate the origin, but not the high level vector
outlinesForColor[i].clear();
break;
}
}
}
}
}

if(vl<vr)
return true;
else
return false;
}*/
void ShapesBuilder::extendOutlineForColor(unsigned int color, const Vector2& v1, const Vector2& v2)
{
assert(color);
vector< vector<Vector2> >& outlinesForColor=shapesMap[color];
//Search a suitable outline to attach this new vertex
for(unsigned int i=0;i<outlinesForColor.size();i++)
{
assert(outlinesForColor[i].size()>=2);
if(outlinesForColor[i].front()==outlinesForColor[i].back())
continue;
if(outlinesForColor[i].back()==v1)
{
outlinesForColor[i].push_back(v2);
return;
}
}
//No suitable outline found, create one
outlinesForColor.push_back(vector<Vector2>());
outlinesForColor.back().reserve(2);
outlinesForColor.back().push_back(v1);
outlinesForColor.back().push_back(v2);
}

void ShapesBuilder::outputShapes(vector< GeomShape >& shapes)
{
//Let's join shape pieces together
joinOutlines();

map< unsigned int, vector< vector<Vector2> > >::iterator it=shapesMap.begin();

for(;it!=shapesMap.end();it++)
{
shapes.push_back(GeomShape());
shapes.back().outlines.swap(it->second);
shapes.back().color=it->first;
}
}
19 changes: 10 additions & 9 deletions geometry.h
Expand Up @@ -22,9 +22,7 @@
#include <list>
#include <iostream>
#include <vector>
/*#ifdef WIN32
#include <windows.h>
#endif*/
#include <map>
#include <GL/glew.h>

namespace lightspark
Expand Down Expand Up @@ -72,7 +70,6 @@ friend class DefineShape3Tag;
void SetStyles(const std::list<FILLSTYLE>* styles);
const FILLSTYLE* style;
arrayElem* varray;
bool isOutlineClosed(const std::vector<Vector2>& outline) const;
bool hasFill;
public:
GeomShape():curTessTarget(0),style(NULL),varray(NULL),hasFill(false),color(0){}
Expand All @@ -84,14 +81,18 @@ friend class DefineShape3Tag;

unsigned int color;

//DEBUG
void dumpEdges();

void Render(int x=0, int y=0) const;
void BuildFromEdges(const std::list<FILLSTYLE>* styles);
};

bool operator<(const GeomShape& r) const;

class ShapesBuilder
{
private:
std::map< unsigned int, std::vector< std::vector<Vector2> > > shapesMap;
void joinOutlines();
public:
void extendOutlineForColor(unsigned int color, const Vector2& v1, const Vector2& v2);
void outputShapes(std::vector<GeomShape>& shapes);
};

class GlyphShape: public GeomShape
Expand Down
130 changes: 0 additions & 130 deletions tags.cpp
Expand Up @@ -1058,133 +1058,6 @@ void DefineShape3Tag::Render()
glPopMatrix();
}

void FromShaperecordListToDump(SHAPERECORD* cur)
{
ofstream f("record_dump");

int startX=0;
int startY=0;

while(cur)
{
if(cur->TypeFlag)
{
if(cur->StraightFlag)
{
startX+=cur->DeltaX;
startY+=cur->DeltaY;
f << startX << ' ' << startY << endl;
}
else
{
startX+=cur->ControlDeltaX;
startY+=cur->ControlDeltaY;
f << startX << ' ' << startY << endl;

startX+=cur->AnchorDeltaX;
startY+=cur->AnchorDeltaY;
f << startX << ' ' << startY << endl;
}
}
else
{
if(cur->StateMoveTo)
{
startX=cur->MoveDeltaX;
startY=cur->MoveDeltaY;
f << "//Move To" << endl;
f << startX << ' ' << startY << endl;
}
if(cur->StateLineStyle)
{
f << "//LS=" << cur->LineStyle << endl;
}
if(cur->StateFillStyle1)
{
f << "//FS1=" << cur->FillStyle1 << endl;
}
if(cur->StateFillStyle0)
{
f << "//FS0=" << cur->FillStyle0 << endl;
}
}
cur=cur->next;
}
f.close();
}

class ShapesBuilder
{
private:
map< unsigned int, vector< vector<Vector2> > > shapesMap;
public:
void extendOutlineForColor(unsigned int color, const Vector2& v1, const Vector2& v2)
{
assert(color);
vector< vector<Vector2> >& outlinesForColor=shapesMap[color];
//Search a suitable outline to attach this new vertex
for(unsigned int i=0;i<outlinesForColor.size();i++)
{
assert(outlinesForColor[i].size()>=2);
if(outlinesForColor[i].front()==outlinesForColor[i].back())
continue;
if(outlinesForColor[i].back()==v1)
{
outlinesForColor[i].push_back(v2);
return;
}
}
//No suitable outline found, create one
outlinesForColor.push_back(vector<Vector2>());
outlinesForColor.back().reserve(2);
outlinesForColor.back().push_back(v1);
outlinesForColor.back().push_back(v2);
}
void joinOutlines()
{
map< unsigned int, vector< vector<Vector2> > >::iterator it=shapesMap.begin();

for(;it!=shapesMap.end();it++)
{
vector< vector<Vector2> >& outlinesForColor=it->second;
//Repack outlines of the same color, avoiding excessive copying
for(int i=0;i<int(outlinesForColor.size());i++)
{
assert(outlinesForColor[i].size()>=2);
//Already closed paths are ok
if(outlinesForColor[i].front()==outlinesForColor[i].back())
continue;
for(int j=outlinesForColor.size()-1;j>=0;j--)
{
if(j==i || outlinesForColor[j].empty())
continue;
if(outlinesForColor[i].front()==outlinesForColor[j].back())
{
//Copy all the vertex but the origin in this one
outlinesForColor[j].insert(outlinesForColor[j].end(),
outlinesForColor[i].begin()+1,
outlinesForColor[i].end());
//Invalidate the origin, but not the high level vector
outlinesForColor[i].clear();
break;
}
}
}
}
}
void outputShapes(vector<GeomShape>& shapes)
{
map< unsigned int, vector< vector<Vector2> > >::iterator it=shapesMap.begin();

for(;it!=shapesMap.end();it++)
{
shapes.push_back(GeomShape());
shapes.back().outlines.swap(it->second);
shapes.back().color=it->first;
}
}
};

/*! \brief Generate a vector of shapes from a SHAPERECORD list
* * \param cur SHAPERECORD list head
* * \param shapes a vector to be populated with the shapes */
Expand Down Expand Up @@ -1260,9 +1133,6 @@ void lightspark::FromShaperecordListToShapeVector(SHAPERECORD* cur, vector<GeomS
cur=cur->next;
}

//Let's join shape pieces together
shapesBuilder.joinOutlines();

shapesBuilder.outputShapes(shapes);
}

Expand Down

0 comments on commit cbe643d

Please sign in to comment.