VTK/VTK 6 Migration/Changes to Scalars Manipulation Functions

From KitwarePublic
< VTK
Revision as of 19:24, 6 April 2012 by Berk (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Changes to Scalars Manipulation Functions in vtkImageData

VTK 6 introduces a number of backwards-incompatible changes. The reasons behind these changes are described in more detail here. Among these are changes to several vtkImageData methods that were used to facilitate meta-data management and memory allocation using pipeline meta-data. Since vtkImageData no longer has direct access to the pipeline information (see this wiki document), these methods were changed to behave differently or to accept additional arguments for dealing with meta-data. These methods are as follows.

  • GetScalarTypeMin()
  • GetScalarTypeMax()
  • GetScalarType()
  • SetScalarType(int scalar_type)
  • GetNumberOfScalarComponents()
  • SetNumberOfScalarComponents(int n)
  • AllocateScalars()

Note that these methods were all designed to work with pipeline meta-data (aka PipelineInformation). For example, SetScalarType() was implemented as follows:

<source lang="cpp"> void vtkImageData::SetScalarType(int type) {

 this->GetProducerPort();
 if(vtkInformation* info = this->GetPipelineInformation())
   {
   vtkDataObject::SetPointDataActiveScalarInfo(info, type, -1);
   }
 else
   {
   vtkErrorMacro("SetScalarType called with no "
                 "executive producing this image data object.");
   }

} </source>

Since data objects can no longer be used to manipulate meta-data directly, these methods were changed. Specific changes are as follows.

GetNumberOfScalarComponents(), GetScalarType(), GetScalarTypeMin() and GetScalarTypeMax()

These methods were changed to return the number of scalar components, the scalar type or the min/max value for that scalar type of the actual vtkImageData. Therefore, they will no longer return the correct value if called before the scalars are allocated (in RequestInformation for example). If you need to access the scalar type before RequestData, you can still do it by passing the pipeline information to GetScalarType().

Example 1

Replace

<source lang="cpp"> int vtkMyAlg::RequestInformation(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

  vtkImageData* output = this->GetOutput();
  output->GetScalarType();
  output->GetNumberOfScalarComponents();

</source>

with

<source lang="cpp"> int vtkMyAlg::RequestInformation(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

   vtkInformation* outInfo = outInfoVec->GetInformationObject(0);
   vtkImageData::GetScalarType(outInfo);
   vtkImageData::GetNumberOfScalarComponents(outInfo);

</source>

Example 2

<source lang="cpp"> int vtkMyAlg::RequestData(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{ vtkImageData* output = vtkImageData::GetData(outInfoVec); // Allocate output scalars here output->GetScalarType(); output->GetNumberOfScalarComponents(); </source>

This code does not need to be changed.

SetScalarType() and SetNumberOfScalarComponents()

SetScalarType() and SetNumberOfScalarComponets() were previously used to populate pipeline information with scalar meta-data. In VTK 6, SetPointDataActiveScalarInfo() can be used to perform the same task.

Example 1

Replace

<source lang="cpp"> int vtkMyAlg::RequestInformation(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

  vtkImageData* output = this->GetOutput();
  output->SetScalarType(VTK_UNSIGNED_CHAR);
  output->SetNumberOfScalarComponents(3);
  return 1;

} </source>

with

<source lang="cpp"> int vtkMyAlg::RequestInformation(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

  vtkInformation* outInfo = outInfoVec->GetInformationObject(0);
  vtkDataObject::SetPointDataActiveScalarInfo(
      outInfo, VTK_UNSIGNED_CHAR, 3);
  return 1;

} </source>

AllocateScalars()

Before VTK 6, AllocateScalars() was used in conjunction with SetScalarType() and SetNumberOfScalarComponents(). The latter two stored meta-data in the pipeline information and AllocateScalars() allocated data by using this meta-data. Since AllocateScalars() can no longer access the pipeline information, it needs to be told what scalar type and how many components to allocate.

Example 1

Replace

<source lang="cpp"> // set the extent of the image data first imageData->SetScalarTypeToFloat(); imageData->SetNumberOfScalarComponents(3); imageData->AllocateScalars(); </source>

with

<source lang="cpp"> // set the extent of the image data first imageData->AllocateScalars(VTK_FLOAT, 3); </source>

Example 2

Replace

<source lang="cpp"> int vtkMyAlg::RequestInformation(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

  vtkImageData* output = this->GetOutput();
  output->SetScalarType(VTK_UNSIGNED_CHAR);
  output->SetNumberOfScalarComponents(3);
  return 1;

}

int vtkMyAlg::RequestData(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

  vtkImageData* output = this->GetOutput();
  output->AllocateScalars();

</source>

with

<source lang="cpp"> int vtkMyAlg::RequestInformation(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

  vtkInformation* outInfo = outInfoVec->GetInformationObject(0);
  vtkDataObject::SetPointDataActiveScalarInfo(
      outInfo, VTK_UNSIGNED_CHAR, 3);
  return 1;

}

int vtkMyAlg::RequestData(vtkInformation*, vtkInformationVector**,

      vtkInformationVector* outInfoVec)

{

  vtkInformation* outInfo = outInfoVec->GetInformationObject(0);
  vtkImageData* output = vtkImageData::GetData(outInfoVec);
  output->AllocateScalars(outInfo);

</source>