PDA

View Full Version : Weird behavior of QGraphicsItem mapFromScene



qlands
23rd August 2010, 13:56
Hi,

I have an object in the scene and I use mapFromScene to map a point, which is in the item's scene's coordinate system, to the item's coordinate system.

When the item’s transformation matrix is identity, and I move the object around the scene and then I use mapFromScene with the current scenePos, the result is always (0,0). However, if I apply to the item a transformation that scales the object to double its width, and I move the object around the scene and then I use mapFromScene with the current scenePos, the result is different at different places of the scene. See for example:

The object is at the top left of the scene:
***************** Start Map pivot ***************
ScenePos X: 117
ScenePos Y: 67
Transformed X: 9.88098e-15
Transformed Y: 0
Current transformation Matrix:
|---|---|---|
| 2 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
|---|---|---|
***************** End map pivot ***************

The object is at the top right of the scene:
***************** Start Map pivot ***************
ScenePos X: 623
ScenePos Y: 69
Transformed X: 2.9976e-15
Transformed Y: 0
Current transformation Matrix:
|---|---|---|
| 2 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
|---|---|---|
***************** End map pivot ***************

The object is around the center or the scene
***************** Start Map pivot ***************
ScenePos X: 368
ScenePos Y: 218
Transformed X: -5.15143e-14
Transformed Y: 0
Current transformation Matrix:
|---|---|---|
| 2 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
|---|---|---|
***************** End map pivot ***************

Here is the code:


void tnkdesignscene::setTransPivotPoint()
{
QPointF refCoordinate;
refCoordinate = SelectedItem->scenePos();
qgraphicsitem_cast<tnkitemcontainer *>(SelectedItem)->setTransPivotPoint(QVector2D(refCoordinate));
}

void tnkitemcontainer::setTransPivotPoint(QVector2D vector)
{
qDebug() << "***************** Start Map pivot ***************";

qDebug() << "ScenePos X: " << vector.x();
qDebug() << "ScenePos Y: " << vector.y();

transPivotPoint = QVector2D(this->mapFromScene(vector.toPointF()));

qDebug() << "Transformed X: " << transPivotPoint.x();
qDebug() << "Transformed Y: " << transPivotPoint.y();

printMatrix(this->transform());

qDebug() << "***************** End map pivot ***************";
}


Any idea why?

Thanks,
Carlos.

bootchk
13th June 2013, 16:36
You usually shouldn't compare two floating numbers for equality. You say that without a transform, your code returns (0,0) but it really returns (0.0, 0.0) i.e. two floats. With a transform, it also returns two floats, and they are close to zero

Is this really a problem, in the sense that it causes difficulties for a user? If you really need to compare the results, you should compare for equality within a small tolerance, or epsilon. This is much discussed on the web.

d_stranz
16th June 2013, 03:19
Any coordinate that is effectively zero (and something e-15 *is* effectively zero), as bootchk points out. There are always small rounding and coordinate conversion errors when transformations are applied to floating point numbers. You're seeing one of them.