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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkImageToStructuredPoints.cxx,v $
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 "vtkImageToStructuredPoints.h"
#include "vtkCellData.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStructuredPoints.h"
#include <math.h>
vtkCxxRevisionMacro(vtkImageToStructuredPoints, "$Revision: 1.62 $");
vtkStandardNewMacro(vtkImageToStructuredPoints);
//----------------------------------------------------------------------------
vtkImageToStructuredPoints::vtkImageToStructuredPoints()
{
this->SetNumberOfInputPorts(2);
this->Translate[0] = this->Translate[1] = this->Translate[2] = 0;
}
//----------------------------------------------------------------------------
vtkImageToStructuredPoints::~vtkImageToStructuredPoints()
{
}
//----------------------------------------------------------------------------
void vtkImageToStructuredPoints::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
void vtkImageToStructuredPoints::SetVectorInput(vtkImageData *input)
{
this->SetInput(1, input);
}
//----------------------------------------------------------------------------
vtkImageData *vtkImageToStructuredPoints::GetVectorInput()
{
if (this->GetNumberOfInputConnections(1) < 1)
{
return NULL;
}
return vtkImageData::SafeDownCast(this->GetExecutive()->GetInputData(1, 0));
}
//----------------------------------------------------------------------------
int vtkImageToStructuredPoints::RequestData(
vtkInformation *,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *vectorInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int uExtent[6];
int *wExtent;
int idxX, idxY, idxZ;
int maxX = 0;
int maxY = 0;
int maxZ = 0;;
vtkIdType inIncX, inIncY, inIncZ;
int rowLength;
unsigned char *inPtr1, *inPtr, *outPtr;
vtkStructuredPoints *output = vtkStructuredPoints::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkImageData *data = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkImageData *vData = 0;
if (vectorInfo)
{
vData = vtkImageData::SafeDownCast(
vectorInfo->Get(vtkDataObject::DATA_OBJECT()));
}
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
uExtent);
output->SetExtent(uExtent);
uExtent[0] += this->Translate[0];
uExtent[1] += this->Translate[0];
uExtent[2] += this->Translate[1];
uExtent[3] += this->Translate[1];
uExtent[4] += this->Translate[2];
uExtent[5] += this->Translate[2];
// if the data extent matches the update extent then just pass the data
// otherwise we must reformat and copy the data
if (data)
{
wExtent = data->GetExtent();
if (wExtent[0] == uExtent[0] && wExtent[1] == uExtent[1] &&
wExtent[2] == uExtent[2] && wExtent[3] == uExtent[3] &&
wExtent[4] == uExtent[4] && wExtent[5] == uExtent[5])
{
if (data->GetPointData())
{
output->GetPointData()->PassData(data->GetPointData());
}
if (data->GetCellData())
{
output->GetCellData()->PassData(data->GetCellData());
}
if (data->GetFieldData())
{
output->GetFieldData()->ShallowCopy(data->GetFieldData());
}
}
else
{
inPtr = (unsigned char *) data->GetScalarPointerForExtent(uExtent);
outPtr = (unsigned char *) output->GetScalarPointer();
// Make sure there are data.
if(!inPtr || !outPtr)
{
output->Initialize();
return 1;
}
// Get increments to march through data
data->GetIncrements(inIncX, inIncY, inIncZ);
// find the region to loop over
rowLength = (uExtent[1] - uExtent[0]+1)*inIncX*data->GetScalarSize();
maxX = uExtent[1] - uExtent[0];
maxY = uExtent[3] - uExtent[2];
maxZ = uExtent[5] - uExtent[4];
inIncY *= data->GetScalarSize();
inIncZ *= data->GetScalarSize();
// Loop through output pixels
for (idxZ = 0; idxZ <= maxZ; idxZ++)
{
inPtr1 = inPtr + idxZ*inIncZ;
for (idxY = 0; idxY <= maxY; idxY++)
{
memcpy(outPtr,inPtr1,rowLength);
inPtr1 += inIncY;
outPtr += rowLength;
}
}
}
}
if (vData)
{
// if the data extent matches the update extent then just pass the data
// otherwise we must reformat and copy the data
wExtent = vData->GetExtent();
if (wExtent[0] == uExtent[0] && wExtent[1] == uExtent[1] &&
wExtent[2] == uExtent[2] && wExtent[3] == uExtent[3] &&
wExtent[4] == uExtent[4] && wExtent[5] == uExtent[5])
{
output->GetPointData()->SetVectors(vData->GetPointData()->GetScalars());
}
else
{
vtkDataArray *fv = vtkDataArray::CreateDataArray(vData->GetScalarType());
float *inPtr2 = (float *)(vData->GetScalarPointerForExtent(uExtent));
// Make sure there are data.
if(!inPtr2)
{
output->Initialize();
return 1;
}
fv->SetNumberOfComponents(3);
fv->SetNumberOfTuples((maxZ+1)*(maxY+1)*(maxX+1));
vData->GetContinuousIncrements(uExtent, inIncX, inIncY, inIncZ);
int numComp = vData->GetNumberOfScalarComponents();
int idx = 0;
// Loop through ouput pixels
for (idxZ = 0; idxZ <= maxZ; idxZ++)
{
for (idxY = 0; idxY <= maxY; idxY++)
{
for (idxX = 0; idxX <= maxX; idxX++)
{
fv->SetTuple(idx,inPtr2);
inPtr2 += numComp;
idx++;
}
inPtr2 += inIncY;
}
inPtr2 += inIncZ;
}
output->GetPointData()->SetVectors(fv);
fv->Delete();
}
}
return 1;
}
//----------------------------------------------------------------------------
// Copy WholeExtent, Spacing and Origin.
int vtkImageToStructuredPoints::RequestInformation (
vtkInformation * vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *vInfo = inputVector[1]->GetInformationObject(0);
int whole[6], *tmp;
double *spacing, origin[3];
vtkInformation *inScalarInfo = vtkDataObject::GetActiveFieldInformation(inInfo,
vtkDataObject::FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes::SCALARS);
if (!inScalarInfo)
{
vtkErrorMacro("Missing scalar field on input information!");
return 0;
}
vtkDataObject::SetPointDataActiveScalarInfo(outInfo,
inScalarInfo->Get( vtkDataObject::FIELD_ARRAY_TYPE() ),
inScalarInfo->Get( vtkDataObject::FIELD_NUMBER_OF_COMPONENTS() ) );
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),whole);
spacing = inInfo->Get(vtkDataObject::SPACING());
inInfo->Get(vtkDataObject::ORIGIN(), origin);
// intersections for whole extent
if (vInfo)
{
tmp = vInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
if (tmp[0] > whole[0]) {whole[0] = tmp[0];}
if (tmp[2] > whole[2]) {whole[2] = tmp[2];}
if (tmp[4] > whole[4]) {whole[4] = tmp[4];}
if (tmp[1] < whole[1]) {whole[1] = tmp[1];}
if (tmp[3] < whole[1]) {whole[3] = tmp[3];}
if (tmp[5] < whole[1]) {whole[5] = tmp[5];}
}
// slide min extent to 0,0,0 (I Hate this !!!!)
this->Translate[0] = whole[0];
this->Translate[1] = whole[2];
this->Translate[2] = whole[4];
origin[0] += spacing[0] * whole[0];
origin[1] += spacing[1] * whole[2];
origin[2] += spacing[2] * whole[4];
whole[1] -= whole[0];
whole[3] -= whole[2];
whole[5] -= whole[4];
whole[0] = whole[2] = whole[4] = 0;
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),whole,6);
// Now should Origin and Spacing really be part of information?
// How about xyx arrays in RectilinearGrid of Points in StructuredGrid?
outInfo->Set(vtkDataObject::ORIGIN(),origin,3);
outInfo->Set(vtkDataObject::SPACING(),spacing,3);
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToStructuredPoints::RequestUpdateExtent(
vtkInformation *,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *vInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int ext[6];
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), ext);
ext[0] += this->Translate[0];
ext[1] += this->Translate[0];
ext[2] += this->Translate[1];
ext[3] += this->Translate[1];
ext[4] += this->Translate[2];
ext[5] += this->Translate[2];
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), ext, 6);
if (vInfo)
{
vInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), ext, 6);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToStructuredPoints::FillOutputPortInformation(int port,
vtkInformation* info)
{
if(!this->Superclass::FillOutputPortInformation(port, info))
{
return 0;
}
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkStructuredPoints");
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToStructuredPoints::FillInputPortInformation(int port,
vtkInformation *info)
{
if (!this->Superclass::FillInputPortInformation(port, info))
{
return 0;
}
if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
}
return 1;
}
|