English 中文(简体)
QGraphicsScene,Item Coordinates影响性能?
原标题:QGraphicsScene, Item Coordinates Affect Performance?

With the below code snippet I create a scene with 100.000 rectangles.
The performance is fine; the view responds with no delays.

QGraphicsScene * scene = new QGraphicsScene;
for (int y = -50000; y < 50000; y++) {
   scene->addRect(0, y * 25, 40, 20);
}
...
view->setScene(scene);

现在第二个片段糟透了

for (int y = 0; y < 100000; y++) {
   scene->addRect(0, y * 25, 40, 20);
}

对于场景元素的前半部分,视图会延迟对鼠标和键事件的响应,而对于另一半,它似乎还可以?!?

The former scene has sceneRect (x, y, w, h) = (0, -1250000, 40, 2499995).
The latter scene has sceneRect (x, y, w, h) = (0, 0, 40, 2499995).

我不知道为什么sceneect会影响性能,因为BSP索引是基于相对项目坐标的。

Am I missing something? I didn t find any information on the documentation, plus the Qt demo 40000 Chips also distributes the elements around (0, 0), without explaining the reason for that choice.

 // Populate scene
 int xx = 0;
 int nitems = 0;
 for (int i = -11000; i < 11000; i += 110) {
     ++xx;
     int yy = 0;
     for (int j = -7000; j < 7000; j += 70) {
         ++yy;
         qreal x = (i + 11000) / 22000.0;
         qreal y = (j + 7000) / 14000.0;
         ...
问题回答

I have a solution for you, but promise to not ask me why is this working, because I really don t know :-)

QGraphicsScene * scene = new QGraphicsScene;
// Define a fake symetrical scene-rectangle
scene->setSceneRect(0, -(25*100000+20), 40, 2 * (25*100000+20) );

for (int y = 0; y < 100000; y++) {
    scene->addRect(0, y * 25, 40, 20);
}
view->setScene(scene);
// Tell the view to display only the actual scene-objects area
view->setSceneRect(0, 0, 40, 25*100000+20);

For the common case, the default index method BspTreeIndex works fine. If your scene uses many animations and you are experiencing slowness, you can disable indexing by calling setItemIndexMethod(NoIndex). Qt-doc

插入之前,您需要调用setItemIndexMethod(QGraphicsScene::NoIndex)

scene->setItemIndexMethod(QGraphicsScene::NoIndex);

for (int y = 0; y < 100000; y++) {
   scene->addRect(0, y * 25, 40, 20);
}
//...

这可能是由于float的精度损失。32位浮点具有23位尾数(或有效位)、1位符号和8位指数。这就像科学记数法。您有23个“有效数字”(由于隐式前导1,实际上是24)和一个2^exp的指数,其中指数的范围可以从-126到127(其他数字用于提供NaNInf等信息)。所以你可以表示非常大的数字,比如2^24*2^127,但离这样一个浮点数最近的浮点数是(2^24-1)*2^127或1700亿亿。如果你试着在这个数字上加一个较小的数字(比如1000),它不会改变。它无法代表这一点。

这在计算机图形学中变得很重要,因为你需要一些剩余的有效数字来制作小数部分。当场景的范围达到1250000.00时,可以将其添加0.1,得到1250000.1。如果你取2500000.0+0.1,你得到2500000.0。任何缩放或旋转都会放大问题。如果你真的飞到这些坐标系去看你的场景,这可能会导致明显的视觉问题。

为什么以0为中心有帮助?因为浮点表示中有一个单独的符号位。在浮点中,(-x,+x)之间的数字比(0,2x)中的数字“更多”。如果我是对的,如果你只是将整个场景缩小1/2,它也会起作用。这会向下移动最重要的位,使其在另一端可以自由精确。

为什么这会导致性能不佳?我只能在不阅读Qt源的情况下进行推测,但可以考虑按位置存储对象的数据结构。如果两个对象由于精度损失而接触(或重叠),而当它们不重叠时,你不必这样做,你可能需要采取什么不同的做法?





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签