1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVolumeTextureMapper.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkVolumeTextureMapper.h"
#include "vtkDataArray.h"
#include "vtkEncodedGradientShader.h"
#include "vtkFiniteDifferenceGradientEstimator.h"
#include "vtkGarbageCollector.h"
#include "vtkImageData.h"
#include "vtkPointData.h"
#include "vtkRenderer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkVolume.h"
#include "vtkVolumeProperty.h"
vtkVolumeTextureMapper::vtkVolumeTextureMapper()
{
this->GradientOpacityArray = NULL;
this->RGBAArray = NULL;
this->ArraySize = -1;
this->SampleDistance = 1.0;
this->GradientEstimator = vtkFiniteDifferenceGradientEstimator::New();
this->GradientShader = vtkEncodedGradientShader::New();
this->NumberOfComponents = 1;
this->RenderWindow = NULL;
this->DataOrigin[0] = 0.0;
this->DataOrigin[1] = 0.0;
this->DataOrigin[2] = 0.0;
this->DataSpacing[0] = 1.0;
this->DataSpacing[1] = 1.0;
this->DataSpacing[2] = 1.0;
}
vtkVolumeTextureMapper::~vtkVolumeTextureMapper()
{
this->SetGradientEstimator( NULL );
this->GradientShader->Delete();
delete [] this->RGBAArray;
delete [] this->GradientOpacityArray;
}
void vtkVolumeTextureMapper::SetGradientEstimator(
vtkEncodedGradientEstimator *gradest )
{
// If we are setting it to its current value, don't do anything
if ( this->GradientEstimator == gradest )
{
return;
}
// If we already have a gradient estimator, unregister it.
if ( this->GradientEstimator )
{
this->GradientEstimator->UnRegister(this);
this->GradientEstimator = NULL;
}
// If we are passing in a non-NULL estimator, register it
if ( gradest )
{
gradest->Register( this );
}
// Actually set the estimator, and consider the object Modified
this->GradientEstimator = gradest;
this->Modified();
}
void vtkVolumeTextureMapper::Update(int port)
{
if ( this->GetInput() )
{
int inputAlgPort;
vtkAlgorithm* inputAlg = this->GetInputAlgorithm(0, 0, inputAlgPort);
inputAlg->UpdateInformation();
inputAlg->SetUpdateExtentToWholeExtent(inputAlgPort);
inputAlg->Update(port);
}
}
void vtkVolumeTextureMapper::Update()
{
this->Superclass::Update();
}
void vtkVolumeTextureMapper::InitializeRender( vtkRenderer *ren,
vtkVolume *vol )
{
int size, i, j, k;
float *AArray;
float *RGBArray;
float *GArray;
int colorChannels;
float gradientOpacityConstant;
// Hang on to the render window - we'll need it to test for abort
this->RenderWindow = ren->GetRenderWindow();
vol->UpdateTransferFunctions( ren );
vol->UpdateScalarOpacityforSampleSize( ren, this->SampleDistance );
size = static_cast<int>(vol->GetArraySize());
int numComponents = this->GetInput()->
GetPointData()->GetScalars()->GetNumberOfComponents();
if ( this->ArraySize != size ||
this->NumberOfComponents != numComponents )
{
delete [] this->RGBAArray;
delete [] this->GradientOpacityArray;
this->RGBAArray = new unsigned char [4*size*numComponents];
this->GradientOpacityArray = new float [256*numComponents];
this->ArraySize = size;
this->NumberOfComponents = numComponents;
}
float *goPtr;
float *goArray;
for ( int c = 0; c < numComponents; c++ )
{
goPtr = vol->GetGradientOpacityArray(c);
goArray = this->GradientOpacityArray + c;
for ( i = 0; i < 256; i++ )
{
*(goArray) = *(goPtr++);
goArray += numComponents;
}
AArray = vol->GetCorrectedScalarOpacityArray(c);
colorChannels = vol->GetProperty()->GetColorChannels(c);
// Being less than 0.0 implies a transfer function, so just multiply by
// 1.0 here since the transfer function will supply the true opacity
// modulation value
gradientOpacityConstant = vol->GetGradientOpacityConstant(c);
if ( gradientOpacityConstant <= 0.0 )
{
gradientOpacityConstant = 1.0;
}
if ( colorChannels == 3 )
{
RGBArray = vol->GetRGBArray(c);
for ( i=0, j=(c*4), k=0; i < size; i++ )
{
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + (RGBArray[k++]*255.0));
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + (RGBArray[k++]*255.0));
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + (RGBArray[k++]*255.0));
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + AArray[i]*255.0*gradientOpacityConstant);
j += 4*(numComponents-1);
}
}
else if ( colorChannels == 1 )
{
GArray = vol->GetGrayArray(c);
for ( i=0, j=(c*4); i < size; i++ )
{
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + (GArray[i]*255.0));
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + (GArray[i]*255.0));
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + (GArray[i]*255.0));
this->RGBAArray[j++] = static_cast<unsigned char>(
0.5 + AArray[i]*255.0*gradientOpacityConstant);
j += 4*(numComponents-1);
}
}
}
this->Shade = vol->GetProperty()->GetShade();
this->GradientEstimator->SetInputData( this->GetInput() );
if ( this->Shade )
{
this->GradientShader->UpdateShadingTable( ren, vol,
this->GradientEstimator );
this->EncodedNormals =
this->GradientEstimator->GetEncodedNormals();
this->RedDiffuseShadingTable =
this->GradientShader->GetRedDiffuseShadingTable(vol);
this->GreenDiffuseShadingTable =
this->GradientShader->GetGreenDiffuseShadingTable(vol);
this->BlueDiffuseShadingTable =
this->GradientShader->GetBlueDiffuseShadingTable(vol);
this->RedSpecularShadingTable =
this->GradientShader->GetRedSpecularShadingTable(vol);
this->GreenSpecularShadingTable =
this->GradientShader->GetGreenSpecularShadingTable(vol);
this->BlueSpecularShadingTable =
this->GradientShader->GetBlueSpecularShadingTable(vol);
}
else
{
this->EncodedNormals = NULL;
this->RedDiffuseShadingTable = NULL;
this->GreenDiffuseShadingTable = NULL;
this->BlueDiffuseShadingTable = NULL;
this->RedSpecularShadingTable = NULL;
this->GreenSpecularShadingTable = NULL;
this->BlueSpecularShadingTable = NULL;
}
// If we have non-constant opacity on the gradient magnitudes,
// we need to use the gradient magnitudes to look up the opacity
if ( vol->GetGradientOpacityConstant() == -1.0 )
{
this->GradientMagnitudes =
this->GradientEstimator->GetGradientMagnitudes();
}
else
{
this->GradientMagnitudes = NULL;
}
this->GetInput()->GetOrigin( this->DataOrigin );
this->GetInput()->GetSpacing( this->DataSpacing );
this->ConvertCroppingRegionPlanesToVoxels();
}
float vtkVolumeTextureMapper::GetGradientMagnitudeScale()
{
if ( !this->GradientEstimator )
{
vtkErrorMacro( "You must have a gradient estimator set to get the scale" );
return 1.0;
}
return this->GradientEstimator->GetGradientMagnitudeScale();
}
float vtkVolumeTextureMapper::GetGradientMagnitudeBias()
{
if ( !this->GradientEstimator )
{
vtkErrorMacro( "You must have a gradient estimator set to get the bias" );
return 1.0;
}
return this->GradientEstimator->GetGradientMagnitudeBias();
}
// Print the vtkVolumeTextureMapper
void vtkVolumeTextureMapper::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if ( this->GradientEstimator )
{
os << indent << "Gradient Estimator: " << (this->GradientEstimator) <<
endl;
}
else
{
os << indent << "Gradient Estimator: (none)" << endl;
}
if ( this->GradientShader )
{
os << indent << "Gradient Shader: " << (this->GradientShader) << endl;
}
else
{
os << indent << "Gradient Shader: (none)" << endl;
}
// this->Shade is a temporary variable that should not be printed
// this->RenderWindow is a temporary variable that should not be printed
// this->DataSpacing is a temporary variable that should not be printed
// this->DataOrigin is a temporary variable that should not be printed
}
//----------------------------------------------------------------------------
void vtkVolumeTextureMapper::ReportReferences(vtkGarbageCollector* collector)
{
this->Superclass::ReportReferences(collector);
// These filters share our input and are therefore involved in a
// reference loop.
vtkGarbageCollectorReport(collector, this->GradientEstimator,
"GradientEstimator");
}
|