mirror of
https://github.com/mapmapteam/mapmap.git
synced 2026-06-16 12:33:19 +02:00
Merge branch 'develop' into ellipseshape
Conflicts: Shape.h
This commit is contained in:
+4
-4
@@ -445,7 +445,7 @@ uid MainWindow::createColorPaint(uid paintId, QColor color)
|
||||
uid MainWindow::createMeshTextureMapping(uid mappingId,
|
||||
uid paintId,
|
||||
int nColumns, int nRows,
|
||||
const QList<QPointF> &src, const QList<QPointF> &dst)
|
||||
const QVector<QPointF> &src, const QVector<QPointF> &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<QPointF> &src, const QList<QPointF> &dst)
|
||||
const QVector<QPointF> &src, const QVector<QPointF> &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<QPointF> &dst)
|
||||
const QVector<QPointF> &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<QPointF> &dst)
|
||||
const QVector<QPointF> &dst)
|
||||
{
|
||||
// Cannot create element with already existing id or element for which no paint exists.
|
||||
if (Mapping::getUidAllocator().exists(mappingId) ||
|
||||
|
||||
+4
-4
@@ -105,14 +105,14 @@ public slots:
|
||||
uid createMeshTextureMapping(uid mappingId,
|
||||
uid paintId,
|
||||
int nColumns, int nRows,
|
||||
const QList<QPointF> &src, const QList<QPointF> &dst);
|
||||
const QVector<QPointF> &src, const QVector<QPointF> &dst);
|
||||
|
||||
/**
|
||||
* Creates a textured triangle.
|
||||
*/
|
||||
uid createTriangleTextureMapping(uid mappingId,
|
||||
uid paintId,
|
||||
const QList<QPointF> &src, const QList<QPointF> &dst);
|
||||
const QVector<QPointF> &src, const QVector<QPointF> &dst);
|
||||
|
||||
|
||||
/**
|
||||
@@ -120,14 +120,14 @@ public slots:
|
||||
*/
|
||||
uid createQuadColorMapping(uid mappingId,
|
||||
uid paintId,
|
||||
const QList<QPointF> &dst);
|
||||
const QVector<QPointF> &dst);
|
||||
|
||||
/**
|
||||
* Creates a color triangle.
|
||||
*/
|
||||
uid createTriangleColorMapping(uid mappingId,
|
||||
uid paintId,
|
||||
const QList<QPointF> &dst);
|
||||
const QVector<QPointF> &dst);
|
||||
|
||||
/**
|
||||
* Creates a color ellipse.
|
||||
|
||||
+34
-29
@@ -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<QtProperty*, std::pair<Shape*, int> >::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<Mesh> outputMesh = std::tr1::static_pointer_cast<Mesh>(outputShape);
|
||||
std::vector<std::vector<Quad> > outputQuads = outputMesh->getQuads2d();
|
||||
QVector<QVector<Quad> > 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<Mesh> outputMesh = std::tr1::static_pointer_cast<Mesh>(outputShape);
|
||||
std::vector<Quad> outputQuads = outputMesh->getQuads();
|
||||
for (std::vector<Quad>::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it)
|
||||
QVector<Quad> outputQuads = outputMesh->getQuads();
|
||||
for (QVector<Quad>::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<Mesh> outputMesh = std::tr1::static_pointer_cast<Mesh>(outputShape);
|
||||
std::vector<Quad> outputQuads = outputMesh->getQuads();
|
||||
for (std::vector<Quad>::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it)
|
||||
QVector<Quad> outputQuads = outputMesh->getQuads();
|
||||
for (QVector<Quad>::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<Mesh> inputMesh = std::tr1::static_pointer_cast<Mesh>(inputShape);
|
||||
std::vector<Quad> inputQuads = inputMesh->getQuads();
|
||||
for (std::vector<Quad>::const_iterator it = inputQuads.begin(); it != inputQuads.end(); ++it)
|
||||
QVector<Quad> inputQuads = inputMesh->getQuads();
|
||||
for (QVector<Quad>::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<Mesh> outputMesh = std::tr1::static_pointer_cast<Mesh>(outputShape);
|
||||
std::tr1::shared_ptr<Mesh> inputMesh = std::tr1::static_pointer_cast<Mesh>(inputShape);
|
||||
std::vector<std::vector<Quad> > outputQuads = outputMesh->getQuads2d();
|
||||
std::vector<std::vector<Quad> > inputQuads = inputMesh->getQuads2d();
|
||||
QVector<QVector<Quad> > outputQuads = outputMesh->getQuads2d();
|
||||
QVector<QVector<Quad> > 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();
|
||||
}
|
||||
|
||||
+21
-17
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ public:
|
||||
// QSize sizeHint() const;
|
||||
// QSize minimumSizeHint() const;
|
||||
Shape* getCurrentShape();
|
||||
void glueVertex(Shape *, Point *);
|
||||
void glueVertex(Shape *, QPointF *);
|
||||
|
||||
protected:
|
||||
void initializeGL();
|
||||
|
||||
+7
-7
@@ -121,7 +121,7 @@ void ProjectReader::parseMapping(const QDomElement& mapping)
|
||||
|
||||
// Get destination shape.
|
||||
QDomElement dst = mapping.firstChildElement("destination");
|
||||
QList<QPointF> dstPoints;
|
||||
QVector<QPointF> 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<QPointF> srcPoints;
|
||||
QVector<QPointF> 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<QPointF> srcPoints;
|
||||
QVector<QPointF> 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<QPointF>& points)
|
||||
void ProjectReader::_parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QVector<QPointF>& 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<QPointF>& points)
|
||||
void ProjectReader::_parseQuad(const QDomElement& quad, QVector<QPointF>& points)
|
||||
{
|
||||
_parseStandardShape("quad", 4, quad, points);
|
||||
}
|
||||
|
||||
|
||||
void ProjectReader::_parseTriangle(const QDomElement& triangle, QList<QPointF>& points)
|
||||
void ProjectReader::_parseTriangle(const QDomElement& triangle, QVector<QPointF>& points)
|
||||
{
|
||||
_parseStandardShape("triangle", 3, triangle, points);
|
||||
}
|
||||
|
||||
void ProjectReader::_parseMesh(const QDomElement& mesh, QList<QPointF>& points, int& nColumns, int& nRows)
|
||||
void ProjectReader::_parseMesh(const QDomElement& mesh, QVector<QPointF>& points, int& nColumns, int& nRows)
|
||||
{
|
||||
// Check that the element is really a mash.
|
||||
QString type = mesh.attribute("shape", "");
|
||||
|
||||
+4
-4
@@ -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<QPointF>& points);
|
||||
void _parseQuad(const QDomElement& quad, QList<QPointF>& points);
|
||||
void _parseTriangle(const QDomElement& triangle, QList<QPointF>& points);
|
||||
void _parseMesh(const QDomElement& mesh, QList<QPointF>& points, int& nColumns, int& nRows);
|
||||
void _parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QVector<QPointF>& points);
|
||||
void _parseQuad(const QDomElement& quad, QVector<QPointF>& points);
|
||||
void _parseTriangle(const QDomElement& triangle, QVector<QPointF>& points);
|
||||
void _parseMesh(const QDomElement& mesh, QVector<QPointF>& points, int& nColumns, int& nRows);
|
||||
QPointF _parseVertex(const QDomElement& vertex);
|
||||
|
||||
// void readPaint(); //Paint *item);
|
||||
|
||||
+3
-3
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Point*>::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<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 (std::vector<Point*>::iterator it = vertices.begin() ;
|
||||
for (QVector<QPointF>::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<Point*>::const_iterator it = vertices.begin() ;
|
||||
for (QVector<QPointF>::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<QPointF>& points, int nColumns, int nRows)
|
||||
Mesh::Mesh(const QVector<QPointF>& points, int nColumns, int nRows)
|
||||
: Quad(), _nColumns(nColumns), _nRows(nRows)
|
||||
{
|
||||
Q_ASSERT(nColumns >= 2 && nRows >= 2);
|
||||
@@ -101,7 +100,7 @@ Mesh::Mesh(const QList<QPointF>& 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<QPointF>& 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<int> >& vertices2d, int nColumns, int nRows)
|
||||
void Mesh::resizeVertices2d(IndexVector2d& vertices2d, int nColumns, int nRows)
|
||||
{
|
||||
vertices2d.resize(nColumns);
|
||||
for (int i=0; i<nColumns; i++)
|
||||
@@ -148,7 +147,7 @@ void Mesh::init(int nColumns, int nRows)
|
||||
void Mesh::addColumn()
|
||||
{
|
||||
// Create new vertices 2d (temporary).
|
||||
std::vector< std::vector<int> > 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<nRows(); y++)
|
||||
{
|
||||
// Get left and right vertices.
|
||||
QPointF left = *getVertex2d( 0, y );
|
||||
QPointF right = *getVertex2d( nColumns()-1, y );
|
||||
QPointF diff = right - left;
|
||||
QPointF left = getVertex2d( 0, y );
|
||||
QPointF right = getVertex2d( nColumns()-1, y );
|
||||
QPointF diff = right - left;
|
||||
|
||||
// First pass: move middle points.
|
||||
for (int x=1; x<nColumns()-1; x++)
|
||||
{
|
||||
QPointF p = *getVertex( _vertices2d[x][y] );
|
||||
QPointF p = getVertex( _vertices2d[x][y] );
|
||||
p -= diff * x * leftMoveProp;
|
||||
setVertex( _vertices2d[x][y], p );
|
||||
}
|
||||
@@ -201,7 +200,7 @@ void Mesh::addColumn()
|
||||
void Mesh::addRow()
|
||||
{
|
||||
// Create new vertices 2d (temporary).
|
||||
std::vector< std::vector<int> > 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<nColumns(); x++)
|
||||
{
|
||||
// Get left and right vertices.
|
||||
QPointF top = *getVertex( _vertices2d[x][0] );
|
||||
QPointF bottom = *getVertex( _vertices2d[x][nRows()-1] );
|
||||
QPointF diff = bottom - top;
|
||||
QPointF top = getVertex( _vertices2d[x][0] );
|
||||
QPointF bottom = getVertex( _vertices2d[x][nRows()-1] );
|
||||
QPointF diff = bottom - top;
|
||||
|
||||
// First pass: move middle points.
|
||||
for (int y=1; y<nRows()-1; y++)
|
||||
{
|
||||
QPointF p = *getVertex( _vertices2d[x][y] );
|
||||
QPointF p = getVertex( _vertices2d[x][y] );
|
||||
p -= diff * y * topMoveProp;
|
||||
setVertex( _vertices2d[x][y], p );
|
||||
}
|
||||
@@ -313,18 +312,18 @@ void Mesh::resize(int nColumns_, int nRows_)
|
||||
//
|
||||
// }
|
||||
|
||||
std::vector<Quad> Mesh::getQuads() const
|
||||
QVector<Quad> Mesh::getQuads() const
|
||||
{
|
||||
std::vector<Quad> quads;
|
||||
QVector<Quad> quads;
|
||||
for (int i=0; i<nHorizontalQuads(); i++)
|
||||
{
|
||||
for (int j=0; j<nVerticalQuads(); j++)
|
||||
{
|
||||
Quad quad(
|
||||
*vertices[ _vertices2d[i] [j] ],
|
||||
*vertices[ _vertices2d[i+1][j] ],
|
||||
*vertices[ _vertices2d[i+1][j+1] ],
|
||||
*vertices[ _vertices2d[i] [j+1] ]
|
||||
getVertex2d(i, j ),
|
||||
getVertex2d(i+1, j ),
|
||||
getVertex2d(i+1, j+1),
|
||||
getVertex2d(i, j+1)
|
||||
);
|
||||
quads.push_back(quad);
|
||||
}
|
||||
@@ -333,19 +332,19 @@ std::vector<Quad> Mesh::getQuads() const
|
||||
return quads;
|
||||
}
|
||||
|
||||
std::vector< std::vector<Quad> > Mesh::getQuads2d() const
|
||||
QVector< QVector<Quad> > Mesh::getQuads2d() const
|
||||
{
|
||||
std::vector< std::vector<Quad> > quads2d;
|
||||
QVector< QVector<Quad> > quads2d;
|
||||
for (int i=0; i<nHorizontalQuads(); i++)
|
||||
{
|
||||
std::vector<Quad> column;
|
||||
QVector<Quad> column;
|
||||
for (int j=0; j<nVerticalQuads(); j++)
|
||||
{
|
||||
Quad quad(
|
||||
*vertices[ _vertices2d[i] [j] ],
|
||||
*vertices[ _vertices2d[i+1][j] ],
|
||||
*vertices[ _vertices2d[i+1][j+1] ],
|
||||
*vertices[ _vertices2d[i] [j+1] ]
|
||||
getVertex2d(i, j ),
|
||||
getVertex2d(i+1, j ),
|
||||
getVertex2d(i+1, j+1),
|
||||
getVertex2d(i, j+1)
|
||||
);
|
||||
column.push_back(quad);
|
||||
}
|
||||
@@ -357,7 +356,7 @@ std::vector< std::vector<Quad> > Mesh::getQuads2d() const
|
||||
void Mesh::_reorderVertices()
|
||||
{
|
||||
// Populate new vertices vector.
|
||||
std::vector<Point*> newVertices(vertices.size());
|
||||
QVector<QPointF> newVertices(vertices.size());
|
||||
int k = 0;
|
||||
for (int x=0; x<nColumns(); x++)
|
||||
for (int y=0; y<nRows(); y++)
|
||||
|
||||
@@ -20,63 +20,21 @@
|
||||
#ifndef SHAPE_H_
|
||||
#define SHAPE_H_
|
||||
|
||||
#include <tr1/memory>
|
||||
#include <iostream>
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QPointF>
|
||||
#include <QPolygonF>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <tr1/memory>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QPointF>
|
||||
#include <QMetaType>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 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 +44,7 @@ public:
|
||||
typedef std::tr1::shared_ptr<Shape> ptr;
|
||||
|
||||
Shape() {}
|
||||
Shape(std::vector<Point*> vertices_) :
|
||||
Shape(QVector<QPointF> vertices_) :
|
||||
vertices(vertices_)
|
||||
{}
|
||||
virtual ~Shape() {}
|
||||
@@ -95,20 +53,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 +75,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<Point*> vertices;
|
||||
QVector<QPointF> vertices;
|
||||
|
||||
void _addVertex(const QPointF& vertex)
|
||||
{
|
||||
Point* v = new Point();
|
||||
v->setValue(vertex);
|
||||
|
||||
vertices.push_back(v);
|
||||
vertices.push_back(vertex);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -178,36 +128,37 @@ public:
|
||||
};
|
||||
|
||||
class Mesh : public Quad {
|
||||
|
||||
typedef QVector<QVector<int> > 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<QPointF>& points, int nColumns, int nRows);
|
||||
Mesh(const QVector<QPointF>& 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<int> >& vertices2d, int nColumns, int nRows);
|
||||
void resizeVertices2d(IndexVector2d& vertices2d, int nColumns, int nRows);
|
||||
|
||||
void init(int nColumns, int nRows);
|
||||
|
||||
@@ -219,8 +170,8 @@ public:
|
||||
|
||||
// void removeColumn(int columnId);
|
||||
|
||||
std::vector<Quad> getQuads() const;
|
||||
std::vector< std::vector<Quad> > getQuads2d() const;
|
||||
QVector<Quad> getQuads() const;
|
||||
QVector<QVector<Quad> > getQuads2d() const;
|
||||
|
||||
int nColumns() const { return _nColumns; }
|
||||
int nRows() const { return _nRows; }
|
||||
@@ -232,9 +183,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<int> > _vertices2d;
|
||||
// Maps a vertex id to the pair of vertex ids it "splits".
|
||||
std::map<int, std::pair<int, int> > _splitVertices;
|
||||
IndexVector2d _vertices2d;
|
||||
|
||||
/**
|
||||
* Reorder vertices in a standard order:
|
||||
@@ -248,47 +197,5 @@ protected:
|
||||
void _reorderVertices();
|
||||
};
|
||||
|
||||
class Ellipse : public Shape {
|
||||
public:
|
||||
Ellipse() {}
|
||||
Ellipse(QPointF p1, QPointF p2, QPointF p3, QPointF p4)
|
||||
{
|
||||
_addVertex(p1);
|
||||
_addVertex(p2);
|
||||
_addVertex(p3);
|
||||
_addVertex(p4);
|
||||
}
|
||||
virtual ~Ellipse() {}
|
||||
|
||||
virtual QString getType() const { return "ellipse"; }
|
||||
|
||||
qreal getRotation() const {
|
||||
QPointF hAxis = getHorizontalAxis();
|
||||
return atan2(hAxis.y(), hAxis.x()) * 180.0 / M_PI;
|
||||
}
|
||||
|
||||
QRect getBoundingRect() const {
|
||||
return QRect(0, getVerticalAxis().manhattanLength(), getHorizontalAxis().manhattanLength(), getVerticalAxis().manhattanLength());
|
||||
}
|
||||
|
||||
QPointF getCenter() const {
|
||||
return getVertex(0)->toPoint() + (getHorizontalAxis() / 2);
|
||||
}
|
||||
|
||||
QPointF getHorizontalAxis() const {
|
||||
return getVertex(2)->toPoint() - getVertex(0)->toPoint();
|
||||
}
|
||||
|
||||
QPointF getVerticalAxis() const {
|
||||
return getVertex(1)->toPoint() - getVertex(3)->toPoint();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void _vertexChanged(int i, Point* p=NULL) {
|
||||
// Get horizontal and vertical axis length.
|
||||
qreal hAxisLength = Point::dist(getVertex(0)->toPoint(), getVertex(2)->toPoint());
|
||||
qreal vAxisLength = Point::dist(getVertex(1)->toPoint(), getVertex(3)->toPoint());
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SHAPE_H_ */
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user