Permalink
Browse files

Preliminary bezier-support

  • Loading branch information...
1 parent 287d112 commit 3f9edd0a9871d6297bf4d4259383ac38d95c4338 @Detegr committed Feb 16, 2013
Showing with 127 additions and 20 deletions.
  1. +26 −5 dataeditor.h
  2. +8 −5 editor.cpp
  3. +14 −2 editor_gl.cpp
  4. +43 −0 polygon.cpp
  5. +7 −1 polygon.h
  6. +15 −4 vertex.cpp
  7. +14 −3 vertex.h
View
@@ -4,27 +4,38 @@
class C_DataEditor : public QDialog
{
Q_OBJECT
+ friend class C_Editor;
private:
QString m_Data;
- QPushButton* m_OkButton;
- QLineEdit* m_Editor;
- QModelIndex m_ModelIndex;
+ QPushButton* m_OkButton;
+ QLineEdit* m_Editor;
+ QModelIndex m_ModelIndex;
+ QCheckBox* m_BezierButton;
public:
C_DataEditor(QWidget* parent=NULL) : QDialog(parent)
{
setFixedWidth(250);
- setFixedHeight(80);
+ setFixedHeight(100);
setModal(true);
m_OkButton = new QPushButton(tr("Ok"));
m_OkButton->setDefault(true);
QObject::connect(m_OkButton, SIGNAL(clicked()), this, SLOT(ok()));
m_Editor = new QLineEdit;
- QVBoxLayout* layout = new QVBoxLayout;
+ QVBoxLayout* layout = new QVBoxLayout(this);
+ QHBoxLayout* bezier_layout = new QHBoxLayout(this);
+ m_BezierButton = new QCheckBox(this);
+ QLabel* beziertext = new QLabel(this);
+
+ bezier_layout->addWidget(m_BezierButton);
+ beziertext->setText(tr("Is bezier?"));
+ bezier_layout->addWidget(beziertext);
+
layout->addWidget(m_Editor);
+ layout->addLayout(bezier_layout);
layout->addWidget(m_OkButton);
setLayout(layout);
hide();
@@ -42,6 +53,16 @@ class C_DataEditor : public QDialog
return m_Data;
}
+ bool getIsBezier() const
+ {
+ return m_BezierButton->isChecked();
+ }
+
+ void setIsBezier(bool b)
+ {
+ m_BezierButton->setChecked(b);
+ }
+
const QModelIndex& getModelIndex() const
{
return m_ModelIndex;
View
@@ -284,7 +284,7 @@ void C_Editor::S_AddToList(QStandardItem* obj, float x, float y, int pos, const
m_Editor->m_ActivePoly->M_Add(x,y,pos);
if(data.length())
{
- m_Editor->m_ActivePoly->M_Last().M_SetData(data);
+ m_Editor->m_ActivePoly->M_Last().M_SetStrData(data);
}
}
@@ -365,9 +365,9 @@ void C_Editor::S_SaveFile(const QString& path)
for(C_Polygon::const_iterator pi=(*it)->begin(); pi!=(*it)->end(); ++pi)
{
out << pi->M_Pos().first << "\n" << pi->M_Pos().second << "\n";
- if(pi->M_GetData().length())
+ if(pi->M_GetStrData().length())
{
- out << "< DATA: " << pi->M_GetData().toStdString() << "\n";
+ out << "< DATA: " << pi->M_GetStrData().toStdString() << "\n";
}
}
}
@@ -423,15 +423,18 @@ void C_Editor::S_OpenDataDialog(const QModelIndex& i)
if(i.column() == 2) // Data column
{
m_List->setEditTriggers(QAbstractItemView::NoEditTriggers);
- m_DataEditor->setData(i, m_Editor->m_ActivePoly->M_Vertex(i.row()).M_GetData());
+ m_DataEditor->setData(i, m_Editor->m_ActivePoly->M_Vertex(i.row()).M_GetStrData());
+ m_DataEditor->m_BezierButton->setChecked(m_Editor->m_ActivePoly->M_Vertex(i.row()).M_GetIsBezier());
m_DataEditor->setVisible(true);
}
}
void C_Editor::S_SetData()
{
QString data=m_DataEditor->getData();
- m_Editor->m_ActivePoly->M_Vertex(m_DataEditor->getModelIndex().row()).M_SetData(data);
+ bool b=m_DataEditor->getIsBezier();
+ m_Editor->m_ActivePoly->M_Vertex(m_DataEditor->getModelIndex().row()).M_SetStrData(data);
+ m_Editor->m_ActivePoly->M_Vertex(m_DataEditor->getModelIndex().row()).M_SetIsBezier(b);
QStandardItem* item=m_Model->itemFromIndex(m_DataEditor->getModelIndex());
if(data.length()) item->setText("*");
else item->setText("");
View
@@ -70,9 +70,21 @@ void C_GLEditor::M_PaintPolygon(const C_Polygon& p)
glBegin(m_DrawMode);
for(C_Polygon::const_iterator it=p.begin(); it!=p.end(); ++it)
{
- std::pair<float, float> pos=it->M_Pos();
qglColor(it->M_Color());
- glVertex2f(m_ViewPortX+pos.first, m_ViewPortY+pos.second);
+ if(it->M_GetIsBezier() && (it+1) != p.end() && (it+2) != p.end())
+ {
+ std::vector<std::pair<float, float> > bezierpoints=C_Polygon::M_CalculateSplinePoints(it->M_Pos(), (it+1)->M_Pos(), (it+2)->M_Pos(), 8);
+ it+=2;
+ for(std::vector<std::pair<float, float> >::iterator itt=bezierpoints.begin(); itt!=bezierpoints.end(); ++itt)
+ {
+ glVertex2f(m_ViewPortX+itt->first, m_ViewPortY+itt->second);
+ }
+ }
+ else
+ {
+ std::pair<float, float> pos=it->M_Pos();
+ glVertex2f(m_ViewPortX+pos.first, m_ViewPortY+pos.second);
+ }
}
glEnd();
}
View
@@ -71,3 +71,46 @@ void C_Polygon::M_Replace(int index, const std::vector<C_Vertex>& vec)
m_Verts.erase(m_Verts.begin()+index);
m_Verts.insert(m_Verts.begin()+index, vec.begin(), vec.end());
}
+
+/*
+std::pair<float, float> C_Polygon::M_Middlepoint(const std::pair<float, float>& a, const std::pair<float, float>& b)
+{
+ return std::make_pair(0.5f * (a.first + b.first), 0.5f * (a.second + b.second));
+}
+
+std::pair<float, float> C_Polygon::M_Splitpoint(const std::pair<float,float>& a, const std::pair<float, float>& b, float t)
+{
+ return std::make_pair(((1.0f-t) * a.first) + (t * b.first), ((1.0f-t) * a.second) + (t * b.second));
+}
+
+std::pair<float,float> C_Polygon::M_CalculateSplinePoint(const std::pair<float, float>& a, const std::pair<float, float>& b, const std::pair<float, float>& c, float t)
+{
+ return M_Splitpoint(M_Splitpoint(a, b, t), M_Splitpoint(b, c, t), t);
+}
+std::vector<std::pair<float, float> > C_Polygon::M_CalculateSplinePoints(const std::pair<float, float>& a, const std::pair<float, float>& b, const std::pair<float, float>& c, int iterations)
+{
+ std::vector<std::pair<float, float> > out;
+ out.push_back(M_Middlepoint(a,b));
+ std::pair<float, float> d = M_Middlepoint(c,b);
+ for (int i=1; i<iterations-1; i++)
+ {
+ out.push_back(M_CalculateSplinePoint(a, b, d, (float) i / (float)(iterations - 1)));
+ }
+ out.push_back(b);
+
+ return out;
+}
+*/
+std::vector<std::pair<float, float> > C_Polygon::M_CalculateSplinePoints(const std::pair<float, float>& a, const std::pair<float, float>& b, const std::pair<float, float>& c, int iterations)
+{
+ std::vector<std::pair<float, float> > ret;
+
+ for(int i=0; i<=100; i+=100/iterations)
+ {
+ double t = i*0.01;
+ float x=(1-t)*(1-t)*a.first + 2 * (1-t) * t * b.first + (t*t) * c.first;
+ float y=(1-t)*(1-t)*a.second + 2 * (1-t) * t * b.second + (t*t) * c.second;
+ ret.push_back(std::make_pair(x,y));
+ }
+ return ret;
+}
View
@@ -13,6 +13,10 @@ class C_Polygon
std::vector<C_Vertex> m_Verts;
QColor m_BaseColor;
+ static std::pair<float, float> M_Middlepoint(const std::pair<float, float>& a, const std::pair<float, float>& b);
+ static std::pair<float, float> M_Splitpoint(const std::pair<float,float>& a, const std::pair<float, float>& b, float t);
+ static std::pair<float,float> M_CalculateSplinePoint(const std::pair<float, float>& a, const std::pair<float, float>& b, const std::pair<float, float>& c, float t);
+
public:
typedef std::vector<C_Vertex>::iterator iterator;
typedef std::vector<C_Vertex>::const_iterator const_iterator;
@@ -33,4 +37,6 @@ class C_Polygon
void M_Reverse();
int M_Size() const { return m_Verts.size(); }
void M_Replace(int index, const std::vector<C_Vertex>& vec);
-};
+
+ static std::vector<std::pair<float, float> > M_CalculateSplinePoints(const std::pair<float, float>& a, const std::pair<float, float>& b, const std::pair<float, float>& c, int iterations);
+};
View
@@ -2,6 +2,8 @@
#include "polygon.h"
C_Vertex::C_Vertex(C_Polygon* p) : m_Parent(p), x(0), y(0), m_Color(QColor::fromRgbF(0.0f, 0.0f, 0.0f, 1.0f)), m_Selected(false), m_Hovering(false) {}
+C_Vertex::C_Vertex(C_Polygon* p, float x, float y) :
+ m_Parent(p), x(x), y(y), m_Color(QColor::fromRgbF(0.0f, 0.0f, 0.0f, 1.0f)), m_Selected(false), m_Hovering(false) {}
C_Vertex::C_Vertex(float newx, float newy, const QColor& c, C_Polygon* p) :
m_Parent(p), x(newx), y(newy), m_Color(c), m_Selected(false), m_Hovering(false) {}
@@ -20,12 +22,21 @@ std::pair<float, float> C_Vertex::M_Pos() const
return std::make_pair(x,y);
}
-void C_Vertex::M_SetData(const QString& data)
+void C_Vertex::M_SetStrData(const QString& data)
{
- m_Data=data;
+ m_Data.str=data;
}
-QString C_Vertex::M_GetData() const
+QString C_Vertex::M_GetStrData() const
{
- return m_Data;
+ return m_Data.str;
+}
+
+void C_Vertex::M_SetIsBezier(bool b)
+{
+ m_Data.isbezier=b;
+}
+bool C_Vertex::M_GetIsBezier() const
+{
+ return m_Data.isbezier;
}
View
@@ -3,17 +3,26 @@
#include <QtGui/QColor>
class C_Polygon;
+struct C_Data
+{
+ QString str;
+ bool isbezier;
+
+ C_Data() : str(""), isbezier(false) {}
+};
+
class C_Vertex
{
private:
C_Polygon* m_Parent;
float x,y;
- QString m_Data;
+ C_Data m_Data;
QColor m_Color;
bool m_Selected;
bool m_Hovering;
public:
C_Vertex(C_Polygon* p);
+ C_Vertex(C_Polygon* p, float x, float y);
C_Vertex(float newx, float newy, const QColor& c, C_Polygon* p);
void M_SetColor(const QColor& c);
const QColor& M_Color() const { return m_Color; }
@@ -22,7 +31,9 @@ class C_Vertex
void M_SetHovering(bool h) { m_Hovering=h; }
bool M_Hovering() const { return m_Hovering; }
void M_SetPos(float x, float y);
- void M_SetData(const QString& data);
- QString M_GetData() const;
+ void M_SetStrData(const QString& data);
+ QString M_GetStrData() const;
+ void M_SetIsBezier(bool b);
+ bool M_GetIsBezier() const;
std::pair<float, float> M_Pos() const;
};

0 comments on commit 3f9edd0

Please sign in to comment.