diff --git a/MainWindow.cpp b/MainWindow.cpp index fd49e41..9a05c22 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -445,7 +445,7 @@ uid MainWindow::createColorPaint(uid paintId, QColor color) uid MainWindow::createMeshTextureMapping(uid mappingId, uid paintId, int nColumns, int nRows, - const QList &src, const QList &dst) + const QVector &src, const QVector &dst) { // Cannot create element with already existing id or element for which no paint exists. if (Mapping::getUidAllocator().exists(mappingId) || @@ -477,7 +477,7 @@ uid MainWindow::createMeshTextureMapping(uid mappingId, uid MainWindow::createTriangleTextureMapping(uid mappingId, uid paintId, - const QList &src, const QList &dst) + const QVector &src, const QVector &dst) { // Cannot create element with already existing id or element for which no paint exists. if (Mapping::getUidAllocator().exists(mappingId) || @@ -507,7 +507,7 @@ uid MainWindow::createTriangleTextureMapping(uid mappingId, uid MainWindow::createQuadColorMapping(uid mappingId, uid paintId, - const QList &dst) + const QVector &dst) { // Cannot create element with already existing id or element for which no paint exists. if (Mapping::getUidAllocator().exists(mappingId) || @@ -536,7 +536,7 @@ uid MainWindow::createQuadColorMapping(uid mappingId, uid MainWindow::createTriangleColorMapping(uid mappingId, uid paintId, - const QList &dst) + const QVector &dst) { // Cannot create element with already existing id or element for which no paint exists. if (Mapping::getUidAllocator().exists(mappingId) || diff --git a/MainWindow.h b/MainWindow.h index 7769d6e..622753a 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -105,14 +105,14 @@ public slots: uid createMeshTextureMapping(uid mappingId, uid paintId, int nColumns, int nRows, - const QList &src, const QList &dst); + const QVector &src, const QVector &dst); /** * Creates a textured triangle. */ uid createTriangleTextureMapping(uid mappingId, uid paintId, - const QList &src, const QList &dst); + const QVector &src, const QVector &dst); /** @@ -120,14 +120,14 @@ public slots: */ uid createQuadColorMapping(uid mappingId, uid paintId, - const QList &dst); + const QVector &dst); /** * Creates a color triangle. */ uid createTriangleColorMapping(uid mappingId, uid paintId, - const QList &dst); + const QVector &dst); /** * Creates a color ellipse. diff --git a/Mapper.cpp b/Mapper.cpp index c3cdc5e..3f557d5 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -64,6 +64,7 @@ QWidget* Mapper::getPropertiesEditor() void Mapper::drawShapeContour(QPainter* painter, const Shape& shape, int lineWidth, const QColor& color) { + Q_UNUSED(painter); QColor rgbColor = color.toRgb(); glColor4f(rgbColor.redF(), rgbColor.greenF(), rgbColor.blueF(), 1.0f); @@ -72,10 +73,8 @@ void Mapper::drawShapeContour(QPainter* painter, const Shape& shape, int lineWid glBegin (GL_LINE_STRIP); for (int i = 0; i < shape.nVertices()+1; i++) { - glVertex2f( - shape.getVertex(i % shape.nVertices())->x(), - shape.getVertex(i % shape.nVertices())->y() - ); + const QPointF& v = shape.getVertex(i % shape.nVertices()); + glVertex2f( v.x(), v.y() ); } glEnd(); } @@ -106,10 +105,10 @@ void Mapper::setValue(QtProperty* property, const QVariant& value) std::map >::iterator it = _propertyToVertex.find(property); if (it != _propertyToVertex.end()) { - QPointF p = value.toPointF(); + const QPointF& p = value.toPointF(); Shape* shape = it->second.first; int v = it->second.second; - if (*shape->getVertex(v) != p) + if (shape->getVertex(v) != p) { shape->setVertex(v, p); emit valueChanged(); @@ -125,8 +124,8 @@ void Mapper::_buildShapeProperty(QtProperty* shapeItem, Shape* shape) QtVariantProperty* pointItem = _variantManager->addProperty(QVariant::PointF, QObject::tr("Point %1").arg(i)); - Point *p = shape->getVertex(i); - pointItem->setValue(*p); + const QPointF& p = shape->getVertex(i); + pointItem->setValue(p); shapeItem->addSubProperty(pointItem); _propertyToVertex[pointItem] = std::make_pair(shape, i); @@ -143,8 +142,8 @@ void Mapper::_updateShapeProperty(QtProperty* shapeItem, Shape* shape) if (i < pointItems.size()) { QtVariantProperty* pointItem = (QtVariantProperty*)pointItems[i]; - Point *p = shape->getVertex(i); - pointItem->setValue(*p); + const QPointF& p = shape->getVertex(i); + pointItem->setValue(p); } } } @@ -188,7 +187,7 @@ void MeshColorMapper::draw(QPainter* painter) painter->setBrush(color->getColor()); std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - std::vector > outputQuads = outputMesh->getQuads2d(); + QVector > outputQuads = outputMesh->getQuads2d(); for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) { for (int y = 0; y < outputMesh->nVerticalQuads(); y++) @@ -202,8 +201,8 @@ void MeshColorMapper::draw(QPainter* painter) void MeshColorMapper::drawControls(QPainter* painter) { std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - std::vector outputQuads = outputMesh->getQuads(); - for (std::vector::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it) + QVector outputQuads = outputMesh->getQuads(); + for (QVector::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it) { drawShapeContour(painter, *it, 1, QColor(0, 0, 255)); } @@ -352,13 +351,16 @@ void TriangleTextureMapper::_doDraw(QPainter* painter) { for (int i = 0; i < inputShape->nVertices(); i++) { + const QPointF& inputPoint = inputShape->getVertex(i); + const QPointF& outputPoint = outputShape->getVertex(i); + Util::correctGlTexCoord( - (inputShape->getVertex(i)->x() - texture->getX()) / (GLfloat) texture->getWidth(), - (inputShape->getVertex(i)->y() - texture->getY()) / (GLfloat) texture->getHeight()); + (inputPoint.x() - texture->getX()) / (GLfloat) texture->getWidth(), + (inputPoint.y() - texture->getY()) / (GLfloat) texture->getHeight()); glVertex2f( - outputShape->getVertex(i)->x(), - outputShape->getVertex(i)->y() - ); + outputPoint.x(), + outputPoint.y() + ); } } glEnd(); @@ -400,8 +402,8 @@ void MeshTextureMapper::setValue(QtProperty* property, const QVariant& value) void MeshTextureMapper::drawControls(QPainter* painter) { std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - std::vector outputQuads = outputMesh->getQuads(); - for (std::vector::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it) + QVector outputQuads = outputMesh->getQuads(); + for (QVector::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it) { drawShapeContour(painter, *it, 1, QColor(0, 0, 255)); } @@ -410,8 +412,8 @@ void MeshTextureMapper::drawControls(QPainter* painter) void MeshTextureMapper::drawInputControls(QPainter* painter) { std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(inputShape); - std::vector inputQuads = inputMesh->getQuads(); - for (std::vector::const_iterator it = inputQuads.begin(); it != inputQuads.end(); ++it) + QVector inputQuads = inputMesh->getQuads(); + for (QVector::const_iterator it = inputQuads.begin(); it != inputQuads.end(); ++it) { drawShapeContour(painter, *it, 1, QColor(0, 0, 255)); } @@ -421,8 +423,8 @@ void MeshTextureMapper::_doDraw(QPainter* painter) { std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(inputShape); - std::vector > outputQuads = outputMesh->getQuads2d(); - std::vector > inputQuads = inputMesh->getQuads2d(); + QVector > outputQuads = outputMesh->getQuads2d(); + QVector > inputQuads = inputMesh->getQuads2d(); for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) { for (int y = 0; y < outputMesh->nVerticalQuads(); y++) @@ -432,13 +434,16 @@ void MeshTextureMapper::_doDraw(QPainter* painter) glBegin(GL_QUADS); for (int i = 0; i < 4; i++) { + const QPointF& inputPoint = inputQuad.getVertex(i); + const QPointF& outputPoint = outputQuad.getVertex(i); + Util::correctGlTexCoord( - (inputQuad.getVertex(i)->x() - texture->getX()) / (GLfloat) texture->getWidth(), - (inputQuad.getVertex(i)->y() - texture->getY()) / (GLfloat) texture->getHeight()); + (inputPoint.x() - texture->getX()) / (GLfloat) texture->getWidth(), + (inputPoint.y() - texture->getY()) / (GLfloat) texture->getHeight()); glVertex2f( - outputQuad.getVertex(i)->x(), - outputQuad.getVertex(i)->y() - ); + outputPoint.x(), + outputPoint.y() + ); } glEnd(); } diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 817fb88..bfa3a08 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -113,8 +113,8 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) { for (i = 0; i < shape->nVertices(); i++) { - Point *p = shape->getVertex(i); - dist = abs(xmouse - p->x()) + abs(ymouse - p->y()); + const QPointF& p = shape->getVertex(i); + dist = qAbs(xmouse - p.x()) + qAbs(ymouse - p.y()); if (dist < maxdist && dist < mindist) { _active_vertex = i; @@ -152,12 +152,14 @@ void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) Shape* shape = getCurrentShape(); if (shape && _active_vertex != NO_VERTEX) { - Point *p = shape->getVertex(_active_vertex); - p->setX(event->x()); - p->setY(event->y()); + QPointF p = shape->getVertex(_active_vertex); + // Set point to mouse coordinates. + p.setX(event->x()); + p.setY(event->y()); - glueVertex(shape, p); - shape->setVertex(_active_vertex, *p); + // Stick to vertices. + glueVertex(shape, &p); + shape->setVertex(_active_vertex, p); update(); emit shapeChanged(getCurrentShape()); @@ -167,20 +169,22 @@ void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) { // std::cout << "Move event " << std::endl; Shape* shape = getCurrentShape(); - static Point p(0,0); + static QPointF prevMousePosition(0,0); // point that keeps track of last position of the mouse if (shape) { if (_shapefirstgrab == false) { - shape->translate(event->x() - p.x(), event->y() - p.y()); + shape->translate(event->x() - prevMousePosition.x(), event->y() - prevMousePosition.y()); update(); emit shapeChanged(getCurrentShape()); } else _shapefirstgrab = false; } - p.setX( event->x() ); - p.setY( event->y() ); + + // Update previous mouse position. + prevMousePosition.setX( event->x() ); + prevMousePosition.setY( event->y() ); } } @@ -277,7 +281,7 @@ void MapperGLCanvas::updateCanvas() /* Stick vertex p of Shape orig to another Shape's vertex, if the 2 vertices are * close enough. The distance per coordinate is currently set in dist_stick * variable. Perhaps the sticky-sensitivity should be configurable through GUI */ -void MapperGLCanvas::glueVertex(Shape *orig, Point *p) +void MapperGLCanvas::glueVertex(Shape *orig, QPointF *p) { MappingManager m = MainWindow::getInstance().getMappingManager(); int dist_stick = 10; /*this parameter may*/ @@ -288,12 +292,12 @@ void MapperGLCanvas::glueVertex(Shape *orig, Point *p) { for (int vertex = 0; vertex < shape->nVertices(); vertex++) { - Point *v = shape->getVertex(vertex); - if ((abs(v->x() - p->x()) < dist_stick) && - (abs(v->y() - p->y()) < dist_stick)) + const QPointF& v = shape->getVertex(vertex); + if ((qAbs(v.x() - p->x()) < dist_stick) && + (qAbs(v.y() - p->y()) < dist_stick)) { - p->setX(v->x()); - p->setY(v->y()); + p->setX(v.x()); + p->setY(v.y()); } } } diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index 6976ea9..f308015 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -49,7 +49,7 @@ public: // QSize sizeHint() const; // QSize minimumSizeHint() const; Shape* getCurrentShape(); - void glueVertex(Shape *, Point *); + void glueVertex(Shape *, QPointF *); protected: void initializeGL(); diff --git a/ProjectReader.cpp b/ProjectReader.cpp index d8cbbb1..69b37d1 100644 --- a/ProjectReader.cpp +++ b/ProjectReader.cpp @@ -121,7 +121,7 @@ void ProjectReader::parseMapping(const QDomElement& mapping) // Get destination shape. QDomElement dst = mapping.firstChildElement("destination"); - QList dstPoints; + QVector dstPoints; if (mappingAttrType == "triangle_texture") { @@ -130,7 +130,7 @@ void ProjectReader::parseMapping(const QDomElement& mapping) // Get / parse source shape. QDomElement src = mapping.firstChildElement("source"); - QList srcPoints; + QVector srcPoints; _parseTriangle(src, srcPoints); uid id = _window->createTriangleTextureMapping(mappingAttrId.toInt(), mappingAttrPaintId.toInt(), srcPoints, dstPoints); @@ -147,7 +147,7 @@ void ProjectReader::parseMapping(const QDomElement& mapping) // Get / parse source shape. QDomElement src = mapping.firstChildElement("source"); - QList srcPoints; + QVector srcPoints; _parseMesh(src, srcPoints, nColumns, nRows); uid id = _window->createMeshTextureMapping(mappingAttrId.toInt(), mappingAttrPaintId.toInt(), nColumns, nRows, srcPoints, dstPoints); @@ -180,7 +180,7 @@ void ProjectReader::parseMapping(const QDomElement& mapping) _xml.raiseError(QObject::tr("Unsupported mapping type: %1.").arg(mappingAttrType)); } -void ProjectReader::_parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QList& points) +void ProjectReader::_parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QVector& points) { // Check that the element is really a triangle. QString typeAttr = shape.attribute("shape", ""); @@ -202,18 +202,18 @@ void ProjectReader::_parseStandardShape(const QString& type, int nVertices, cons _xml.raiseError(QObject::tr("Shape has %1 vertices: expected %2.").arg(points.size()).arg(nVertices)); } -void ProjectReader::_parseQuad(const QDomElement& quad, QList& points) +void ProjectReader::_parseQuad(const QDomElement& quad, QVector& points) { _parseStandardShape("quad", 4, quad, points); } -void ProjectReader::_parseTriangle(const QDomElement& triangle, QList& points) +void ProjectReader::_parseTriangle(const QDomElement& triangle, QVector& points) { _parseStandardShape("triangle", 3, triangle, points); } -void ProjectReader::_parseMesh(const QDomElement& mesh, QList& points, int& nColumns, int& nRows) +void ProjectReader::_parseMesh(const QDomElement& mesh, QVector& points, int& nColumns, int& nRows) { // Check that the element is really a mash. QString type = mesh.attribute("shape", ""); diff --git a/ProjectReader.h b/ProjectReader.h index d42a762..747828e 100644 --- a/ProjectReader.h +++ b/ProjectReader.h @@ -37,10 +37,10 @@ private: void parsePaint(const QDomElement& paint); void parseMapping(const QDomElement& mapping); - void _parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QList& points); - void _parseQuad(const QDomElement& quad, QList& points); - void _parseTriangle(const QDomElement& triangle, QList& points); - void _parseMesh(const QDomElement& mesh, QList& points, int& nColumns, int& nRows); + void _parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QVector& points); + void _parseQuad(const QDomElement& quad, QVector& points); + void _parseTriangle(const QDomElement& triangle, QVector& points); + void _parseMesh(const QDomElement& mesh, QVector& points, int& nColumns, int& nRows); QPointF _parseVertex(const QDomElement& vertex); // void readPaint(); //Paint *item); diff --git a/ProjectWriter.cpp b/ProjectWriter.cpp index 742f872..0b89e3f 100644 --- a/ProjectWriter.cpp +++ b/ProjectWriter.cpp @@ -102,10 +102,10 @@ void ProjectWriter::writeShapeVertices(Shape *shape) for (int i = 0; i < shape->nVertices(); i++) { - Point *point = shape->getVertex(i); + const QPointF& point = shape->getVertex(i); _xml.writeStartElement("vertex"); - _xml.writeAttribute("x", QString::number(point->x())); - _xml.writeAttribute("y", QString::number(point->y())); + _xml.writeAttribute("x", QString::number(point.x())); + _xml.writeAttribute("y", QString::number(point.y())); _xml.writeEndElement(); // vertex } } diff --git a/Shape.cpp b/Shape.cpp index 69bb2e6..3188703 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -21,65 +21,64 @@ bool Shape::includesPoint(int x, int y) { - Point *prev = NULL, *cur; - int left = 0, right = 0, maxy, miny; - for (std::vector::iterator it = vertices.begin() ; - it != vertices.end(); it++) - { - if (!prev) { - prev = vertices.back(); - } - cur = *it; - miny = std::min(cur->y(), prev->y()); - maxy = std::max(cur->y(), prev->y()); - - if (y > miny && y < maxy) { - if (prev->x() == cur->x()) - { - if (x < cur->x()) - right++; - else left++; - } - else - { - double slope = (cur->y() - prev->y()) / (cur->x() - prev->x()); - double offset = cur->y() - slope * cur->x(); - int xintersect = int((y - offset ) / slope); - if (x < xintersect) - right++; - else left++; - } - } - prev = *it; - } - if (right % 2 && left % 2) - return true; - return false; + return toPolygon().containsPoint(QPointF(x, y), Qt::OddEvenFill); +// QVector::iterator prev; +// int left = 0, right = 0, maxy, miny; +// for (QVector::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 (std::vector::iterator it = vertices.begin() ; + for (QVector::iterator it = vertices.begin() ; it != vertices.end(); ++it) { - (*it)->setX((*it)->x() + x); - (*it)->setY((*it)->y() + y); + it->setX(it->x() + x); + it->setY(it->y() + y); } } QPolygonF Shape::toPolygon() const { QPolygonF polygon; - for (std::vector::const_iterator it = vertices.begin() ; + for (QVector::const_iterator it = vertices.begin() ; it != vertices.end(); ++it) { - polygon.append((*it)->toPoint()); + polygon.append(*it); } return polygon; } - Mesh::Mesh(QPointF p1, QPointF p2, QPointF p3, QPointF p4, int nColumns, int nRows) : Quad(p1, p2, p3, p4), _nColumns(0), _nRows(0) { @@ -87,7 +86,7 @@ Mesh::Mesh(QPointF p1, QPointF p2, QPointF p3, QPointF p4, int nColumns, int nRo init(nColumns, nRows); } -Mesh::Mesh(const QList& points, int nColumns, int nRows) +Mesh::Mesh(const QVector& points, int nColumns, int nRows) : Quad(), _nColumns(nColumns), _nRows(nRows) { Q_ASSERT(nColumns >= 2 && nRows >= 2); @@ -101,7 +100,7 @@ Mesh::Mesh(const QList& points, int nColumns, int nRows) for (int x=0; x<_nColumns; x++) for (int y=0; y<_nRows; y++) { - vertices.push_back( new Point(points[k].x(), points[k].y()) ); + vertices.push_back( points[k] ); _vertices2d[x][y] = k; k++; } @@ -110,14 +109,14 @@ Mesh::Mesh(const QList& points, int nColumns, int nRows) QPolygonF Mesh::toPolygon() const { QPolygonF polygon; - polygon.append(getVertex2d(0, 0)->toPoint()); - polygon.append(getVertex2d(nColumns()-1, 0)->toPoint()); - polygon.append(getVertex2d(nColumns()-1, nRows()-1)->toPoint()); - polygon.append(getVertex2d(0, nRows()-1)->toPoint()); + polygon.append(getVertex2d(0, 0)); + polygon.append(getVertex2d(nColumns()-1, 0)); + polygon.append(getVertex2d(nColumns()-1, nRows()-1)); + polygon.append(getVertex2d(0, nRows()-1)); return polygon; } -void Mesh::resizeVertices2d(std::vector< std::vector >& vertices2d, int nColumns, int nRows) +void Mesh::resizeVertices2d(IndexVector2d& vertices2d, int nColumns, int nRows) { vertices2d.resize(nColumns); for (int i=0; i > newVertices2d; + IndexVector2d newVertices2d; resizeVertices2d(newVertices2d, nColumns()+1, nRows()); // Left displacement of points already there. @@ -159,14 +158,14 @@ void Mesh::addColumn() for (int y=0; y > newVertices2d; + IndexVector2d newVertices2d; resizeVertices2d(newVertices2d, nColumns(), nRows()+1); // Top displacement of points already there. @@ -212,14 +211,14 @@ void Mesh::addRow() for (int x=0; x Mesh::getQuads() const +QVector Mesh::getQuads() const { - std::vector quads; + QVector quads; for (int i=0; i Mesh::getQuads() const return quads; } -std::vector< std::vector > Mesh::getQuads2d() const +QVector< QVector > Mesh::getQuads2d() const { - std::vector< std::vector > quads2d; + QVector< QVector > quads2d; for (int i=0; i column; + QVector column; for (int j=0; j > Mesh::getQuads2d() const void Mesh::_reorderVertices() { // Populate new vertices vector. - std::vector newVertices(vertices.size()); + QVector newVertices(vertices.size()); int k = 0; for (int x=0; x +#include + #include +#include #include #include -#include -#include - -#include - -#include -#include +#include +#include #include -#include #include -#include + /** * Point (or vertex) on the 2-D canvas. */ - -Q_DECLARE_METATYPE (qreal) - -class Point: public QObject, public QPointF -{ - Q_OBJECT - -public: - Q_PROPERTY(qreal x READ x WRITE setX NOTIFY xChanged) - Q_PROPERTY(qreal y READ y WRITE setY NOTIFY yChanged) - Q_INVOKABLE Point(qreal x, qreal y): QPointF(x,y) {} - Q_INVOKABLE Point(): QPointF(0,0) {} - - static qreal dist(const QPointF& p1, const QPointF& p2){ - return pow(p2.x() - p1.x(),2) + pow(p2.y() - p1.y(),2); - } - -signals: - void xChanged(); - void yChanged(); - -public slots: - void setX(qreal x) - { - QPointF::setX(x); - emit xChanged(); - } - void setY(qreal y) - { - QPointF::setY(y); - emit yChanged(); - } - void setValue(const QPointF& p) - { - setX(p.x()); - setY(p.y()); - } - -}; /** * Series of vertices. (points) */ @@ -86,7 +45,7 @@ public: typedef std::tr1::shared_ptr ptr; Shape() {} - Shape(std::vector vertices_) : + Shape(QVector vertices_) : vertices(vertices_) {} virtual ~Shape() {} @@ -95,20 +54,20 @@ public: int nVertices() const { return vertices.size(); } - Point* getVertex(int i) const + QPointF getVertex(int i) const { return vertices[i]; } - void setVertex(int i, QPointF v) + virtual void setVertex(int i, const QPointF& v) { - vertices[i]->setValue(v); + vertices[i] = v; } - void setVertex(int i, double x, double y) + virtual void setVertex(int i, double x, double y) { - vertices[i]->setX(x); - vertices[i]->setY(y); + vertices[i].setX(x); + vertices[i].setY(y); } virtual QString getType() const = 0; @@ -117,27 +76,19 @@ public: * Algorithm should work for all polygons, including non-convex * Found at http://www.cs.tufts.edu/comp/163/notes05/point_inclusion_handout.pdf */ - bool includesPoint(int x, int y); + virtual bool includesPoint(int x, int y); /* Translate all vertices of shape by the vector (x,y) */ - void translate(int x, int y); - - int nVertices() - { - return vertices.size(); - } + virtual void translate(int x, int y); virtual QPolygonF toPolygon() const; protected: - std::vector vertices; + QVector vertices; void _addVertex(const QPointF& vertex) { - Point* v = new Point(); - v->setValue(vertex); - - vertices.push_back(v); + vertices.push_back(vertex); } }; @@ -178,36 +129,37 @@ public: }; class Mesh : public Quad { + + typedef QVector > IndexVector2d; + public: Mesh() : _nColumns(0), _nRows(0) { init(1, 1); } Mesh(QPointF p1, QPointF p2, QPointF p3, QPointF p4, int nColumns=2, int nRows=2); - Mesh(const QList& points, int nColumns, int nRows); + Mesh(const QVector& points, int nColumns, int nRows); virtual ~Mesh() {} virtual QString getType() const { return "mesh"; } virtual QPolygonF toPolygon() const; - Point* getVertex2d(int i, int j) const + QPointF getVertex2d(int i, int j) const { return vertices[_vertices2d[i][j]]; } - void setVertex2d(int i, int j, QPointF v) + void setVertex2d(int i, int j, const QPointF& v) { - vertices[_vertices2d[i][j]]->setValue(v); + vertices[_vertices2d[i][j]] = v; // copy } void setVertex2d(int i, int j, double x, double y) { - Point* p = vertices[_vertices2d[i][j]]; - p->setX(x); - p->setY(y); + vertices[_vertices2d[i][j]] = QPointF(x, y); } - void resizeVertices2d(std::vector< std::vector >& vertices2d, int nColumns, int nRows); + void resizeVertices2d(IndexVector2d& vertices2d, int nColumns, int nRows); void init(int nColumns, int nRows); @@ -219,8 +171,8 @@ public: // void removeColumn(int columnId); - std::vector getQuads() const; - std::vector< std::vector > getQuads2d() const; + QVector getQuads() const; + QVector > getQuads2d() const; int nColumns() const { return _nColumns; } int nRows() const { return _nRows; } @@ -232,9 +184,7 @@ protected: int _nColumns; int _nRows; // _vertices[i][j] contains vertex id of vertex at position (i,j) where i = 0..nColumns and j = 0..nRows - std::vector< std::vector > _vertices2d; - // Maps a vertex id to the pair of vertex ids it "splits". - std::map > _splitVertices; + IndexVector2d _vertices2d; /** * Reorder vertices in a standard order: diff --git a/Util.cpp b/Util.cpp index 044c25d..c84d438 100644 --- a/Util.cpp +++ b/Util.cpp @@ -60,19 +60,19 @@ int map_int(int value, int istart, int istop, int ostart, int ostop) Mesh* createMeshForTexture(Texture* texture, int frameWidth, int frameHeight) { return new Mesh( - Point(texture->getX(), texture->getY()), - Point(texture->getX() + texture->getWidth(), texture->getY()), - Point(texture->getX() + texture->getWidth(), texture->getY() + texture->getHeight()), - Point(texture->getX(), texture->getY() + texture->getHeight()) + QPointF(texture->getX(), texture->getY()), + QPointF(texture->getX() + texture->getWidth(), texture->getY()), + QPointF(texture->getX() + texture->getWidth(), texture->getY() + texture->getHeight()), + QPointF(texture->getX(), texture->getY() + texture->getHeight()) ); } Triangle* createTriangleForTexture(Texture* texture, int frameWidth, int frameHeight) { return new Triangle( - Point(texture->getX(), texture->getY() + texture->getHeight()), - Point(texture->getX() + texture->getWidth(), texture->getY() + texture->getHeight()), - Point(texture->getX() + texture->getWidth() / 2, texture->getY()) + QPointF(texture->getX(), texture->getY() + texture->getHeight()), + QPointF(texture->getX() + texture->getWidth(), texture->getY() + texture->getHeight()), + QPointF(texture->getX() + texture->getWidth() / 2, texture->getY()) ); } @@ -95,19 +95,19 @@ Ellipse* createEllipseForTexture(Texture* texture, int frameWidth, Quad* createQuadForColor(int frameWidth, int frameHeight) { return new Quad( - Point(frameWidth / 4, frameHeight / 4), - Point(frameWidth * 3 / 4, frameHeight / 4), - Point(frameWidth * 3 / 4, frameHeight * 3/ 4), - Point(frameWidth / 4, frameHeight * 3 / 4) + QPointF(frameWidth / 4, frameHeight / 4), + QPointF(frameWidth * 3 / 4, frameHeight / 4), + QPointF(frameWidth * 3 / 4, frameHeight * 3/ 4), + QPointF(frameWidth / 4, frameHeight * 3 / 4) ); } Triangle* createTriangleForColor(int frameWidth, int frameHeight) { return new Triangle( - Point(frameWidth / 4, frameHeight * 3 / 4), - Point(frameWidth * 3 / 4, frameHeight * 3 / 4), - Point(frameWidth / 2, frameHeight / 4) + QPointF(frameWidth / 4, frameHeight * 3 / 4), + QPointF(frameWidth * 3 / 4, frameHeight * 3 / 4), + QPointF(frameWidth / 2, frameHeight / 4) ); }