diff --git a/Mapper.cpp b/Mapper.cpp index f3a19ce..8519c47 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); } } } @@ -306,13 +305,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(); @@ -375,8 +377,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++) @@ -386,13 +388,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/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..1a417eb 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -101,7 +101,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 +110,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 Mesh::getQuads() const for (int j=0; j > Mesh::getQuads2d() const for (int j=0; jsetValue(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; @@ -111,27 +111,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); } }; @@ -184,21 +176,19 @@ public: 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); diff --git a/Util.cpp b/Util.cpp index 2aa2ae1..4dbcf4e 100644 --- a/Util.cpp +++ b/Util.cpp @@ -60,38 +60,38 @@ 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()) ); } 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) ); }