1.2.4. Loading from memory

In this case the volume data is stored in the application’s memory, i.e. in system memory. All the application has to do is make the volume data available to VolumeViz and set the parameters describing the volume data.

The following code fragment shows how to give VolumeViz access to a volume already stored in system memory. In this example the volume dimensions are 256x256x256 and the data values are unsigned 8-bit integers (bytes). These are the essential volume characteristics. The data and characteristics will be set in the data field of the SoVolumeData( C++ | Java | .NET ) node using the setValue() method. The third parameter to this method is numSigBits (number of significant bits in each data value). In this case we are using all 8 bits so we can set this parameter to zero, meaning use all bits. In the case, for example, of DICOM data that only uses 12 bits in a 16 bit value we would set this value to 12. The last parameter to this method specifies if VolumeViz should make an internal copy of the data. If VolumeViz makes a copy of the data, then the application can (if desired) free its memory. However to conserve memory, the value NO_COPY is usually used. In this case VolumeViz accesses the data from the application memory and the application is responsible for maintaining that memory as long as the corresponding SoVolumeData( C++ | Java | .NET ) node exists.


C++
// Pointer to volume data
unsigned char* pData = new unsigned char[numVoxels];
. . .

// Volume characteristics
SbVec3i32 volDim(256,256,256);
SbDataType dataType = SbDataType::UNSIGNED_BYTE;

SoVolumeData* pVolData = new SoVolumeData();
pVolData->data.setValue( volDim, dataType, 0 (void*)pData, SoSFArray::NO_COPY );
[Important]

Volume data must be wrapped in an SbNativeArray to pass efficiently to VolumeViz.


.NET
// Array containing volume data
byte[] Data = new byte[numVoxels];
. . .

// Volume characteristics
SbVec3i32 volDim = new SbVec3i32(256,256,256);
SbDataType dataType = new SbDataType(SbDataType.DataTypes.UNSIGNED_BYTE);

SoVolumeData VolData = new SoVolumeData();
SbNativeArray<byte> NativeData = new SbNativeArray<byte>(Data);
VolData.data.SetValue( 
    volDim, dataType, 0, NativeData, SoSFArray.CopyPolicies.NO_COPY);
[Important]

Volume data must be stored in a direct buffer with native byte order to pass efficiently to VolumeViz.


Java
// Array containing volume data
ByteBuffer data = ByteBuffer.allocateDirect( numVoxels );
data.order( ByteOrder.nativeOrder() );
. . .

// Volume characteristics
SbVec3i32 volDim = new SbVec3i32(256,256,256);
SbDataType dataType = new SbDataType(SbDataType.DataTypes.UNSIGNED_BYTE);

SoVolumeData volData = new SoVolumeData();
volData.data.setValue( 
    volDim, dataType, 0, data, SoSFArray.CopyPolicies.NO_COPY);
volData.extent.setValue( -1,-1,-1, 1,1,1 );