Added a Polygon shape and corresponding mappers (just used as a superclass of Quad and Triangle for now).

This commit is contained in:
Tats
2014-03-19 22:34:34 -04:00
parent 7602a77166
commit 441664e6f0
6 changed files with 63 additions and 64 deletions
+2 -2
View File
@@ -1224,7 +1224,7 @@ void MainWindow::addMappingItem(uid mappingId)
icon = QIcon(":/images/draw-triangle.png");
if (paintType == "color")
mapper = Mapper::ptr(new ColorMapper(mapping));
mapper = Mapper::ptr(new PolygonColorMapper(mapping));
else
mapper = Mapper::ptr(new TriangleTextureMapper(textureMapping));
}
@@ -1234,7 +1234,7 @@ void MainWindow::addMappingItem(uid mappingId)
label = QString(shapeType == "mesh" ? "Mesh %1" : "Quad %1").arg(mappingId);
icon = QIcon(":/images/draw-rectangle-2.png");
if (paintType == "color")
mapper = Mapper::ptr(new ColorMapper(mapping));
mapper = Mapper::ptr(new PolygonColorMapper(mapping));
else
mapper = Mapper::ptr(new MeshTextureMapper(textureMapping));
}
+5 -4
View File
@@ -19,6 +19,7 @@
*/
#include "Mapper.h"
#include "MainWindow.h"
#include "unused.h"
Mapper::Mapper(Mapping::ptr mapping)
@@ -157,18 +158,18 @@ ColorMapper::ColorMapper(Mapping::ptr mapping)
Q_CHECK_PTR(color);
}
#include "MainWindow.h"
void ColorMapper::draw(QPainter* painter)
void PolygonColorMapper::draw(QPainter* painter)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::NoPen);
painter->setBrush(color->getColor());
// Draw shape as polygon.
painter->drawPolygon(_mapping->getShape()->toPolygon());
Polygon* poly = (Polygon*)_mapping->getShape().get();
painter->drawPolygon(poly->toPolygon());
}
void ColorMapper::drawControls(QPainter* painter)
void PolygonColorMapper::drawControls(QPainter* painter)
{
UNUSED(painter);
}
+18 -8
View File
@@ -61,15 +61,16 @@ class Mapper : public QObject
{
Q_OBJECT
protected:
Mapping::ptr _mapping;
public:
typedef std::tr1::shared_ptr<Mapper> ptr;
protected:
Mapping::ptr _mapping;
Mapper(Mapping::ptr mapping);
virtual ~Mapper();
public:
virtual QWidget* getPropertiesEditor();
virtual void draw(QPainter* painter) = 0;
virtual void drawControls(QPainter* painter) = 0;
@@ -110,16 +111,24 @@ class ColorMapper : public Mapper
{
Q_OBJECT
public:
protected:
ColorMapper(Mapping::ptr mapping);
virtual ~ColorMapper() {}
virtual void draw(QPainter* painter);
virtual void drawControls(QPainter* painter);
protected:
std::tr1::shared_ptr<Color> color;
};
class PolygonColorMapper : public ColorMapper
{
Q_OBJECT
public:
PolygonColorMapper(Mapping::ptr mapping) : ColorMapper(mapping) {}
virtual ~PolygonColorMapper() {}
virtual void draw(QPainter* painter);
virtual void drawControls(QPainter* painter);
};
class MeshColorMapper : public ColorMapper
@@ -140,7 +149,8 @@ private:
QtVariantProperty* _meshItem;
};
class EllipseColorMapper : public ColorMapper {
class EllipseColorMapper : public ColorMapper
{
Q_OBJECT
public:
+3
View File
@@ -28,4 +28,7 @@ inline double degreesToRadians(double degrees) { return degrees / 180.0 * M_PI;
inline float radiansToDegrees(float radians) { return radians / M_PI * 180.0f; }
inline double radiansToDegrees(double radians) { return radians / M_PI * 180.0; }
// Wrap value around ie. wrapAround(-1, 3) ==> 2
inline int wrapAround(int index, int max) { return (index + max) % max; }
#endif /* MATH_H_ */
+1 -40
View File
@@ -19,45 +19,6 @@
#include "Shape.h"
bool Shape::includesPoint(qreal x, qreal y)
{
return toPolygon().containsPoint(QPointF(x, y), Qt::OddEvenFill);
// QVector<QPointF>::iterator prev;
// int left = 0, right = 0, maxy, miny;
// for (QVector<QPointF>::iterator it = vertices.begin() ;
// it != vertices.end(); it++)
// {
// if (!prev) {
// prev = vertices.back();
// }
// miny = qMin(it->y(), prev->y());
// maxy = qMax(it->y(), prev->y());
//
// if (y > miny && y < maxy) {
// if (prev->x() == it->x())
// {
// if (x < it->x())
// right++;
// else left++;
// }
// else
// {
// double slope = (it->y() - prev->y()) / (it->x() - prev->x());
// double offset = it->y() - slope * it->x();
// int xintersect = int((y - offset ) / slope);
// if (x < xintersect)
// right++;
// else left++;
// }
// }
// prev = *it;
// }
// if (right % 2 && left % 2)
// return true;
// return false;
}
void Shape::translate(int x, int y)
{
for (QVector<QPointF>::iterator it = vertices.begin() ;
@@ -68,7 +29,7 @@ void Shape::translate(int x, int y)
}
}
QPolygonF Shape::toPolygon() const
QPolygonF Polygon::toPolygon() const
{
QPolygonF polygon;
for (QVector<QPointF>::const_iterator it = vertices.begin() ;
+34 -10
View File
@@ -83,17 +83,15 @@ public:
* Algorithm should work for all polygons, including non-convex
* Found at http://www.cs.tufts.edu/comp/163/notes05/point_inclusion_handout.pdf
*/
virtual bool includesPoint(qreal x, qreal y);
virtual bool includesPoint(const QPointF& p) {
return includesPoint(p.x(), p.y());
virtual bool includesPoint(qreal x, qreal y) {
return includesPoint(QPoint(x, y));
}
virtual bool includesPoint(const QPointF& p) = 0;
/* Translate all vertices of shape by the vector (x,y) */
virtual void translate(int x, int y);
virtual QPolygonF toPolygon() const;
protected:
QVector<QPointF> vertices;
@@ -104,10 +102,33 @@ protected:
};
/**
* This class represents a simple polygon (ie. the control points are vertices).
*/
class Polygon : public Shape {
public:
Polygon() {}
Polygon(QVector<QPointF> vertices_) : Shape(vertices_) {}
virtual ~Polygon() {}
virtual QPolygonF toPolygon() const;
virtual bool includesPoint(const QPointF& p) {
return toPolygon().containsPoint(p, Qt::OddEvenFill);
}
// Override the parent, checking to make sure the vertices are displaced correctly.
virtual void setVertex(int i, const QPointF& v);
protected:
// Returns all line segments of the polygon.
QVector<QLineF> _getSegments() const;
};
/**
* Four-vertex shape.
*/
class Quad : public Shape
class Quad : public Polygon
{
public:
Quad() {}
@@ -126,7 +147,7 @@ public:
/**
* Triangle shape.
*/
class Triangle : public Shape
class Triangle : public Polygon
{
public:
Triangle() {}
@@ -140,8 +161,8 @@ public:
virtual QString getType() const { return "triangle"; }
};
class Mesh : public Quad {
class Mesh : public Quad
{
typedef QVector<QVector<int> > IndexVector2d;
public:
@@ -156,6 +177,9 @@ public:
virtual QPolygonF toPolygon() const;
// Override the parent, checking to make sure the vertices are displaced correctly.
virtual void setVertex(int i, const QPointF& v);
QPointF getVertex2d(int i, int j) const
{
return vertices[_vertices2d[i][j]];