1.9.2. Transforming volumes

In this section we discuss the various options for transforming volume data.

This class defines a transform function to be applied to each tile of data after it is loaded (returned from the volume reader) but before it is stored in system memory (LDM data cache). This allows you to modify the original data (for example, scaling or filtering) before it is displayed. It can also be used to create multiple data sets in memory from a single data set on disk. Note that the transform is only applied once (to each tile) and the original data is not kept in memory. The transform function executes on the CPU and usually applies its algorithm on the CPU. While it could transfer the tile to a GPU to apply a CUDA or OpenCL kernel, one tile is usually too small to justify the data transfer time.

Derive a new class from this one and implement the transformFunction() method. Create an instance of the new class and set it in the dataTransform field of the SoDataSet( C++ | Java | .NET ) node. It is not currently possible to access other tiles of data (for example using the data access API) from this function. Note that the function will be called from LDM data loader threads, so multiple threads may be executing in this function at the same time (on different tiles).

The SoVolumeTransform( C++ | Java | .NET ) node (discussed in the next section) also allows a transform function to be applied to each LDM tile, but the transform is applied immediately before the tile is loaded onto the graphics board. SoVolumeTransform( C++ | Java | .NET ) does allow the Data Access API to be used in the transform function.

The SoDataCompositor( C++ | Java | .NET ) node (discussed in the volume combining section) allows a transform function to be applied in the special case where multiple volumes should be combined into a single volume.


C++
class TransformClass : public SoLDMDataTransform
{
  virtual void transformFunction(
    SoDataSet* ds, // current dataset
    const SbVec3i32& bufferDimension, // tile dim
    SoBufferObject*  bufferToTransform, // buffer to transform
    const SbBox3i32& dataBox, // position of tile in data space
    int resolutionLevel // resolution level of the tile 
  );
};

void
TransformClass::transformFunction(
    SoDataSet* ds, // current dataset
    const SbVec3i32& bufferDimension, // tile dim
    SoBufferObject*  bufferToTransform, // buffer to transform
    const SbBox3i32& dataBox, // position of tile in data space
    int resolutionLevel // resolution level of the tile 
    )
{
  SoRef<SoCpuBufferObject> cpuObj = new SoCpuBufferObject;
  bufferToTransform->map( cpuObj.ptr(), SoCpuBufferObject::READ_WRITE );
  unsigned char* buffer =
                 (unsigned char*)cpuObj->map( SoCpuBufferObject::READ_WRITE );

  // Do the real work
  if(resolutionLevel == 1 || resolutionLevel == 0) {
    int ijk = 0;
    for(int k = 0; k < bufferDimension[2]; k++) {
      for(int j = 0; j < bufferDimension[1]; j++) {
        for(int i = 0; i < bufferDimension[0]; i++) {
          buffer[ijk] *= 2;
          ijk++;
        }
      }
    }
  }
  cpuObj->unmap();
  bufferToTransform->unmap( cpuObj.ptr() );
}

.NET
public class TransformClass : SoLDMDataTransform
{
    new public void TransformFunction(
        SoDataSet ds,
        SbVec3i32 bufferDimension,
        SoBufferObject bufferToTransform,
        SbBox3i32 dataBox,
        int resolutionLevel)
    {
        SoCpuBufferObject cpuObj = new SoCpuBufferObject();
        bufferToTransform.Map(cpuObj, SoCpuBufferObject.AccessModes.READ_WRITE);
        SbNativeArray<byte> buffer = cpuObj.Map(SoCpuBufferObject.AccessModes.READ_WRITE);

        // Do the real work
        if (resolutionLevel == 1 || resolutionLevel == 0)
        {
            int ijk = 0;
            for (int k = 0; k < bufferDimension[2]; k++)
            {
                for (int j = 0; j < bufferDimension[1]; j++)
                {
                    for (int i = 0; i < bufferDimension[0]; i++)
                    {
                        buffer[ijk] *= 2;
                        ijk++;
                    }
                }
            }
        }
        cpuObj.Unmap();
        bufferToTransform.Unmap(cpuObj);
    }
}

Java
public class TransformClass extends SoLDMDataTransform {
  
	public void TransformFunction(
        SoDataSet ds,
        SbVec3i32 bufferDimension,
        SoBufferObject bufferToTransform,
        SbBox3i32 dataBox,
        int resolutionLevel )
	{
		SoCpuBufferObject cpuObj = new SoCpuBufferObject();
        bufferToTransform.map(cpuObj, SoCpuBufferObject.AccessModes.READ_WRITE);
        ByteBuffer buffer = cpuObj.map(SoCpuBufferObject.AccessModes.READ_WRITE);

        // Do the real work
        if (resolutionLevel == 1 || resolutionLevel == 0)
        {
            int ijk = 0;
            for (int k = 0; k < bufferDimension.getZ(); k++)
            {
                for (int j = 0; j < bufferDimension.getY(); j++)
                {
                    for (int i = 0; i < bufferDimension.getX(); i++)
                    {
                    	byte b = buffer.get(ijk);
                    	b *= 2;
                    	buffer.put( ijk, b );
                        ijk++;
                    }
                }
            }
        }
        cpuObj.unmap();
        bufferToTransform.unmap(cpuObj);
    }
}

The SoVolumeTransform( C++ | Java | .NET ) node allows an application to apply a defined computation on LDM data tiles just before they are sent to the GPU. There are several other ways to apply a computation to LDM tiles, that are applied at different stages of the pipeline. The SoLDMDataTransform( C++ | Java | .NET ) class for example (see the dataTransform field of SoDataSet( C++ | Java | .NET )) applies a computation to each LDM tile requested from the volume reader before the tile is stored in system memory. This can be used to create multiple data sets from a single input (e.g. on disk) data set, but has the drawback that each resulting data set must be stored in system memory. SoVolumeTransform( C++ | Java | .NET ) can be used to create multiple data sets from a single data set in system memory and does not require storing the created data sets in system memory (only on the GPU).

A cache mechanism, local to each instance of SoVolumeTransform( C++ | Java | .NET ), allows storing the computed tiles for later reuse. The size of this cache (number of tiles to cache) can be specified using the cacheSize field. We recommend setting the cacheSize to the number of tiles needed to display the biggest expected slice. As an example, for a dataset of size 128x512x1024 with a tile size equal to 128, the biggest slice size is 512x1024. So 4x8 = 32 LDM tiles are needed for rendering and therefore a cacheSize of 32 (tiles) is needed to avoid computing the same tile multiple times. The apply method can retrieve the transformed data for other tiles from the cache and can explicitly add the transformed data for other tiles to the cache.

SoVolumeTransform( C++ | Java | .NET ) works on tiles (not slices or volumes). If a compute function needs data from other tiles, the application may use the SoLDMDataAccess( C++ | Java | .NET ) API to get that data. (This is another difference from SoLDMDataTransform( C++ | Java | .NET ), which does not allow using the LDM data access API.)

Multiple SoVolumeTransform( C++ | Java | .NET ) nodes may be applied to the same SoDataSet( C++ | Java | .NET ). The transforms are applied in the order of their appearance in the scene graph. SoVolumeTransform( C++ | Java | .NET ) nodes may also be applied only to a specific SoDataSet( C++ | Java | .NET ) using the volumeTransformId field. If this field is zero (the default), the transform is applied to all subsequent data set nodes. Else the transform is only applied to data sets whose dataSetId field contains the same value as the volumeTransformId.

SoVolumeTransform( C++ | Java | .NET ) is based on the Open Inventor computing framework and uses the SoBufferObject( C++ | Java | .NET ) classes to abstract and manage blocks of memory. The application can use different devices to implement the computation, and manage input tiles and output tiles stored on different devices.

As mentioned previously, SoVolumeTransform( C++ | Java | .NET ) can be used to create multiple data sets on-the-fly from a single data set in system memory. In this case the same data set node will be instanced multiple times in the scene graph (although its data will only be loaded in system memory once), but we still need to assign each data texture a unique id so the shader program can access them uniquely. This is done using SoDataSetId( C++ | Java | .NET ) nodes to specify a different data set id for each instance of the data set node. If an SoDataSetId( C++ | Java | .NET ) node is in the traversal state when a data set node is traversed, the dataSetId field is ignored and the id from the SoDataSetId( C++ | Java | .NET ) node is used.

To implement a computation, the application must derive a new class from SoVolumeTransform( C++ | Java | .NET ) and (at least) implement the apply() method, which performs the actual computation. This method is called with parameters that give access to information about the data set and the specific tile being computed. In a simple case you might only need to access the buffer object.

It may also be useful to implement the isValid() and getTransformedMinMax() methods. The isValid() method will be called for each tile immediately before the apply() method. If this method returns FALSE, then the apply() method will not be called for that tile. If the application can compute or estimate the min and max values of the computed data set, then it should implement the getTransformedMinMax() method so VolumeViz does not do unnecessary work to compute these values.


C++
class VolumeTransformClass : public SoVolumeTransform
{
public:
  void apply(
    SoState *pState,
    const SoLDM::DataSetIdPair& pair,
    SoBufferObject *pBuffer,
    const SoLDMTileID& tileId );
};

void
VolumeTransformClass::apply(
    SoState *pState,
    const SoLDM::DataSetIdPair& pair,
    SoBufferObject* bufferToTransform,
    const SoLDMTileID& tileId )
{
  SoDataSet *pDS = pair.first;
  SoDataSet::DataType type = pDS->getDataType();
  int typeSize = pDS->getDatumSize();
  SbVec3i32 tileDim = pDS->getTileDimension();

  int bufferSize = bufferToTransform->getSize();
  int numVals = bufferSize / typeSize;
  SoRef<SoCpuBufferObject> cpuBuffer = new SoCpuBufferObject;
  bufferToTransform->map( cpuBuffer.ptr(), SoBufferObject::READ_WRITE );
  cpuBuffer->getContext()->bind();
  void *p = cpuBuffer->map( SoBufferObject::READ_WRITE );
  // Modify values
//   . . .
   cpuBuffer->unmap();
   cpuBuffer->getContext()->unbind();
   bufferToTransform->unmap( cpuBuffer.ptr() );
}

.NET
public class VolumeTransformClass : SoVolumeTransform
{
    new public void Apply(
        SoState state,
        SbPair<SoDataSet, int> pair,
        SoBufferObject bufferToTransform,
        SoLDMTileID tileId)
    {
        SoDataSet dataSet = pair.First();
        SoDataSet.DataTypes type = dataSet.GetDataType();

        SoCpuBufferObject cpuObj = new SoCpuBufferObject();
        bufferToTransform.Map(cpuObj, SoCpuBufferObject.AccessModes.READ_WRITE);
        SbNativeArray<byte> buffer = cpuObj.Map(SoCpuBufferObject.AccessModes.READ_WRITE);

        // Modify values
        . . .

        cpuObj.Unmap();
        bufferToTransform.Unmap(cpuObj);
    }
}

Java
            
          
  • pair

    This parameter contains a reference to the SoDataSet( C++ | Java | .NET ) object in pair.first and the dataSetId in pair.second. Using the SoDataSet( C++ | Java | .NET ) object you can obtain, for example, the actual data type of the data:


    C++
    SoDataSet *pDS = pair.first;
    SoDataSet::dataType type = pDS->getDataType();

    .NET
    SoDataSet dataSet = pair.First();
    SoDataSet.DataTypes type = dataSet.GetDataType();
                    

    Java
  • bufferToTransform

    Contains the data to be transformed and replaced. In other words, an “in place” transformation should be done.

  • tileId

    The id of the specific tile to be transformed. Using this id and the SoDataSet( C++ | Java | .NET ) object you can obtain, for example, the actual position of the tile in the volume:


    C++
    SbBox3i32 tilePos = pDS->getNodeFrontManager()->getTilePos(tileId);

    .NET
    SbBox3i32 tilePos = dataSet.GetNodeFrontManager().GetTilePos(tileId);

    Java
  • See the example program: $OIVHOME/src/VolumeViz/examples/volumeTransform.