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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageToImageStencil.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 "vtkImageToImageStencil.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkImageStencilData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include <math.h>
vtkStandardNewMacro(vtkImageToImageStencil);
//----------------------------------------------------------------------------
vtkImageToImageStencil::vtkImageToImageStencil()
{
this->UpperThreshold = VTK_FLOAT_MAX;
this->LowerThreshold = -VTK_FLOAT_MAX;
}
//----------------------------------------------------------------------------
vtkImageToImageStencil::~vtkImageToImageStencil()
{
}
//----------------------------------------------------------------------------
void vtkImageToImageStencil::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Input: " << this->GetInput() << "\n";
os << indent << "UpperThreshold: " << this->UpperThreshold << "\n";
os << indent << "LowerThreshold: " << this->LowerThreshold << "\n";
}
//----------------------------------------------------------------------------
void vtkImageToImageStencil::SetInputData(vtkImageData *input)
{
this->SetInputDataInternal(0, input);
}
//----------------------------------------------------------------------------
vtkImageData *vtkImageToImageStencil::GetInput()
{
if (this->GetNumberOfInputConnections(0) < 1)
{
return NULL;
}
return vtkImageData::SafeDownCast(
this->GetExecutive()->GetInputData(0, 0));
}
//----------------------------------------------------------------------------
// The values greater than or equal to the value match.
void vtkImageToImageStencil::ThresholdByUpper(double thresh)
{
if (this->LowerThreshold != thresh || this->UpperThreshold < VTK_FLOAT_MAX)
{
this->LowerThreshold = thresh;
this->UpperThreshold = VTK_FLOAT_MAX;
this->Modified();
}
}
//----------------------------------------------------------------------------
// The values less than or equal to the value match.
void vtkImageToImageStencil::ThresholdByLower(double thresh)
{
if (this->UpperThreshold != thresh || this->LowerThreshold > -VTK_FLOAT_MAX)
{
this->UpperThreshold = thresh;
this->LowerThreshold = -VTK_FLOAT_MAX;
this->Modified();
}
}
//----------------------------------------------------------------------------
// The values in a range (inclusive) match
void vtkImageToImageStencil::ThresholdBetween(double lower, double upper)
{
if (this->LowerThreshold != lower || this->UpperThreshold != upper)
{
this->LowerThreshold = lower;
this->UpperThreshold = upper;
this->Modified();
}
}
//----------------------------------------------------------------------------
int vtkImageToImageStencil::RequestData(
vtkInformation *,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkImageData *inData = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkImageStencilData *data = vtkImageStencilData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
int extent[6];
inData->GetExtent(extent);
// output extent is always the input extent
this->AllocateOutputData(data, extent);
vtkDataArray *inScalars = inData->GetPointData()->GetScalars();
double upperThreshold = this->UpperThreshold;
double lowerThreshold = this->LowerThreshold;
// for keeping track of progress
unsigned long count = 0;
unsigned long target = static_cast<unsigned long>(
(extent[5] - extent[4] + 1)*(extent[3] - extent[2] + 1)/50.0);
target++;
for (int idZ = extent[4]; idZ <= extent[5]; idZ++)
{
for (int idY = extent[2]; idY <= extent[3]; idY++)
{
if (count%target == 0)
{
this->UpdateProgress(count/(50.0*target));
}
count++;
int state = 1; // inside or outside, start outside
int r1 = extent[0];
int r2 = extent[1];
// index into scalar array
int idS = ((extent[1] - extent[0] + 1)*
((extent[3] - extent[2] + 1)*(idZ - extent[4]) +
(idY - extent[2])));
for (int idX = extent[0]; idX <= extent[1]; idX++)
{
int newstate = 1;
double value = inScalars->GetComponent(idS++,0);
if (value >= lowerThreshold && value <= upperThreshold)
{
newstate = -1;
if (newstate != state)
{ // sub extent starts
r1 = idX;
}
}
else if (newstate != state)
{ // sub extent ends
r2 = idX - 1;
data->InsertNextExtent(r1, r2, idY, idZ);
}
state = newstate;
} // for idX
if (state < 0)
{ // if inside at end, cap off the sub extent
data->InsertNextExtent(r1, extent[1], idY, idZ);
}
} // for idY
} // for idZ
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToImageStencil::RequestInformation(
vtkInformation *,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int wholeExtent[6];
double spacing[3];
double origin[3];
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
wholeExtent);
inInfo->Get(vtkDataObject::SPACING(), spacing);
inInfo->Get(vtkDataObject::ORIGIN(), origin);
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
wholeExtent, 6);
outInfo->Set(vtkDataObject::SPACING(), spacing, 3);
outInfo->Set(vtkDataObject::ORIGIN(), origin, 3);
outInfo->Set(
vtkStreamingDemandDrivenPipeline::UNRESTRICTED_UPDATE_EXTENT(), 1);
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToImageStencil::FillInputPortInformation(int,
vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToImageStencil::RequestUpdateExtent(
vtkInformation *,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int extent[6], wholeExtent[6];
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), extent);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExtent);
// clip UpdateExtent with WholeExtent
extent[0] = (extent[0] > wholeExtent[0] ? extent[0] : wholeExtent[0]);
extent[1] = (extent[1] < wholeExtent[1] ? extent[1] : wholeExtent[1]);
extent[2] = (extent[2] > wholeExtent[2] ? extent[2] : wholeExtent[2]);
extent[3] = (extent[3] < wholeExtent[3] ? extent[3] : wholeExtent[3]);
extent[4] = (extent[4] > wholeExtent[4] ? extent[4] : wholeExtent[4]);
extent[5] = (extent[5] < wholeExtent[5] ? extent[5] : wholeExtent[5]);
// if invalid, use the current data extent if allocated
if (extent[0] > extent[1] || extent[2] > extent[3] || extent[4] > extent[5])
{
for (int j = 0; j < 6; j += 2)
{
extent[j] = extent[j+1] = wholeExtent[j];
}
vtkImageData *inData = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
if (inData)
{
inData->GetExtent(extent);
}
}
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), extent, 6);
return 1;
}
|