Merge branch 'master' of github.com:mapmapteam/mapmap

This commit is contained in:
Alexandre Quessy
2014-03-22 16:08:04 -04:00
7 changed files with 128 additions and 73 deletions
+4 -2
View File
@@ -701,6 +701,8 @@ void MainWindow::createLayout()
paintList = new QListWidget;
paintList->setSelectionMode(QAbstractItemView::SingleSelection);
paintList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
paintList->setDefaultDropAction(Qt::MoveAction);
paintList->setDragDropMode(QAbstractItemView::InternalMove);
paintList->setMinimumWidth(PAINT_LIST_MINIMUM_WIDTH);
// Create mapping list.
@@ -1221,7 +1223,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));
}
@@ -1231,7 +1233,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_ */
+60 -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,60 @@ void Shape::translate(int x, int y)
}
}
QPolygonF Shape::toPolygon() const
void Polygon::setVertex(int i, const QPointF& v)
{
// Weird, but nothing to do.
if (nVertices() <= 3)
{
Shape::setVertex(i, v);
return;
}
QPointF realV = v;
// Look at the two adjunct segments to vertex i and see if they
// intersect with any non-adjacent segments.
// Construct the list of segments (with the new candidate vertex).
QVector<QLineF> segments = _getSegments();
int prev = wrapAround(i - 1, segments.size());
int next = wrapAround(i + 1, segments.size());
segments[prev] = QLineF(vertices[prev], v);
segments[i] = QLineF(v, vertices[next]);
// For each adjunct segment.
for (int adj=0; adj<2; adj++)
{
int idx = wrapAround(i + adj - 1, segments.size());
for (int j=0; j<segments.size(); j++)
{
// If the segment to compare to is valid (ie. if it is not
// the segment itself nor an adjacent one) then check for
// intersection.
if (j != idx &&
j != wrapAround(idx-1, segments.size()) &&
j != wrapAround(idx+1, segments.size()))
{
QPointF intersection;
if (segments[idx].intersect(segments[j], &intersection) == QLineF::BoundedIntersection)
realV = intersection;
}
}
}
// Really set the vertex.
Shape::setVertex(i, realV);
}
QVector<QLineF> Polygon::_getSegments() const
{
QVector<QLineF> segments;
for (int i=0; i<vertices.size(); i++)
segments.push_back(QLineF(vertices[i], vertices[ (i+1) % vertices.size() ]));
return segments;
}
QPolygonF Polygon::toPolygon() const
{
QPolygonF polygon;
for (QVector<QPointF>::const_iterator it = vertices.begin() ;
@@ -116,6 +130,12 @@ QPolygonF Mesh::toPolygon() const
return polygon;
}
void Mesh::setVertex(int i, const QPointF& v)
{
// TODO
Shape::setVertex(i, v);
}
void Mesh::resizeVertices2d(IndexVector2d& vertices2d, int nColumns, int nRows)
{
vertices2d.resize(nColumns);
+37 -18
View File
@@ -40,11 +40,7 @@
#include "Math.h"
/**
* Point (or vertex) on the 2-D canvas.
*/
/**
* Series of vertices. (points)
* Shape represented by a series of control points.
*/
class Shape
{
@@ -68,11 +64,10 @@ public:
virtual void setVertex(int i, const QPointF& v)
{
vertices[i].setX(v.x());
vertices[i].setY(v.y());
vertices[i] = v;
}
virtual void setVertex(int i, double x, double y)
virtual void setVertex(int i, qreal x, qreal y)
{
setVertex(i, QPointF(x, y));
}
@@ -83,17 +78,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 +97,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 +142,7 @@ public:
/**
* Triangle shape.
*/
class Triangle : public Shape
class Triangle : public Polygon
{
public:
Triangle() {}
@@ -140,8 +156,8 @@ public:
virtual QString getType() const { return "triangle"; }
};
class Mesh : public Quad {
class Mesh : public Quad
{
typedef QVector<QVector<int> > IndexVector2d;
public:
@@ -156,6 +172,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]];
+1 -1
View File
@@ -5,7 +5,7 @@ Code
----
* MapperGLCanvas has a strange name considering we also have a Mapper class; actually I'm thinking we should rename Mapper to MappingView or something (since a Mapper is actually the View of the Mapping class)
* Override includesPoint() in Mesh
* DONE Override includesPoint() in Mesh
* video gstreamer
Packaging