PDA

View Full Version : QVector to store 3d data



Vikram.Saralaya
18th November 2015, 13:01
Hello,

I have a data model and a GUI. In the data model I have a list of image data.
QList<ImageData>

Each image data can hold a 3-dimensional vector of unit's.
QVector< QVector< QVector<uint> > > m_3dData.

From the GUI I want to request a 2-dimensional slice from m_3dData, but in any order.

As soon as GUI requests this, I need to calculate the minimum and maximum pixel values from the specified slice and create a auto scaled image from it and display in the GUI.

The requested slice is typically in the range of 1 x 800 x 800 pixels wide. I access the vector using m_3dData[dim1][dim2][dim3] which results in 160,000 + 160,000 accesses (for getting min and maximum pixels once and for creating auto scaled rgb values from it again). This takes 50 + 50 milliseconds or so which is a lot for me.

My questions are:

Is QVector the right structure for this purpose?
Is there a better way to access this structure?


Thanks you!

Regards
Vikram

StrikeByte
18th November 2015, 16:40
Can you tell a little bit about what it is used for, for me it is not clear what you are trying to achief.

you can store 3d data in many different ways but it depends on the type of data and what you need to extract from the data.

Vikram.Saralaya
18th November 2015, 16:55
Hello StrikeByte,

A image data generator (say 3D scanner) generates 64-bit data stream (each data represents a pixel). This stream has to be stored as a stack of images in the data model. GUI wants to show the building of image in real time at its own rate.

The scanner can move in XYZ, XZY, YZX, YXZ, ZXY or ZYX depending on how it is configured.
The user (GUI) can visualize XY, YX or XZ (which is the 2 dimensional slice I mentioned) as a gray scale QImage.

Thanks for the interest and response!

Regards
Vikram

yeye_olive
18th November 2015, 16:58
With you current approach, accessing an element requires lots of memory loads, because each QVector internally holds a pointer into the actual storage in the heap. In addition, implicit sharing could cost additional indirections, and accessing elements with [] for a non-const QVector means wasting time checking that its storage is not shared with others.

You should try a flat array without sharing, such as an std::vector or QVarLengthArray. If the three dimensions are A, B, C, then the array should have length A * B * C, and the element with coordinates i, j, k will have index ((i * B) + j) * C + k. Make sure you call an accessor without bound checking.

Vikram.Saralaya
18th November 2015, 17:01
@yeye_olive

That is indeed useful! I was just considering the QVarLengthArray and that is a confidence booster :)