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
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkThreshold.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 "vtkThreshold.h"
#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkUnstructuredGrid.h"
vtkCxxRevisionMacro(vtkThreshold, "$Revision: 1.68 $");
vtkStandardNewMacro(vtkThreshold);
// Construct with lower threshold=0, upper threshold=1, and threshold
// function=upper AllScalars=1.
vtkThreshold::vtkThreshold()
{
this->LowerThreshold = 0.0;
this->UpperThreshold = 1.0;
this->AllScalars = 1;
this->AttributeMode = -1;
this->ThresholdFunction = &vtkThreshold::Upper;
this->ComponentMode = VTK_COMPONENT_MODE_USE_SELECTED;
this->SelectedComponent = 0;
// by default process active point scalars
this->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS,
vtkDataSetAttributes::SCALARS);
}
vtkThreshold::~vtkThreshold()
{
}
// Criterion is cells whose scalars are less or equal to lower threshold.
void vtkThreshold::ThresholdByLower(double lower)
{
if ( this->LowerThreshold != lower ||
this->ThresholdFunction != &vtkThreshold::Lower)
{
this->LowerThreshold = lower;
this->ThresholdFunction = &vtkThreshold::Lower;
this->Modified();
}
}
// Criterion is cells whose scalars are greater or equal to upper threshold.
void vtkThreshold::ThresholdByUpper(double upper)
{
if ( this->UpperThreshold != upper ||
this->ThresholdFunction != &vtkThreshold::Upper)
{
this->UpperThreshold = upper;
this->ThresholdFunction = &vtkThreshold::Upper;
this->Modified();
}
}
// Criterion is cells whose scalars are between lower and upper thresholds.
void vtkThreshold::ThresholdBetween(double lower, double upper)
{
if ( this->LowerThreshold != lower || this->UpperThreshold != upper ||
this->ThresholdFunction != &vtkThreshold::Between)
{
this->LowerThreshold = lower;
this->UpperThreshold = upper;
this->ThresholdFunction = &vtkThreshold::Between;
this->Modified();
}
}
int vtkThreshold::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and ouptut
vtkDataSet *input = vtkDataSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType cellId, newCellId;
vtkIdList *cellPts, *pointMap;
vtkIdList *newCellPts;
vtkCell *cell;
vtkPoints *newPoints;
int i, ptId, newId, numPts;
int numCellPts;
double x[3];
vtkPointData *pd=input->GetPointData(), *outPD=output->GetPointData();
vtkCellData *cd=input->GetCellData(), *outCD=output->GetCellData();
int keepCell, usePointScalars;
vtkDebugMacro(<< "Executing threshold filter");
if (this->AttributeMode != -1)
{
vtkErrorMacro(<<"You have set the attribute mode on vtkThreshold. This method is deprecated, please use SetInputArrayToProcess instead.");
return 1;
}
vtkDataArray *inScalars = this->GetInputArrayToProcess(0,inputVector);
if (!inScalars)
{
vtkDebugMacro(<<"No scalar data to threshold");
return 1;
}
outPD->CopyAllocate(pd);
outCD->CopyAllocate(cd);
numPts = input->GetNumberOfPoints();
output->Allocate(input->GetNumberOfCells());
newPoints = vtkPoints::New();
newPoints->Allocate(numPts);
pointMap = vtkIdList::New(); //maps old point ids into new
pointMap->SetNumberOfIds(numPts);
for (i=0; i < numPts; i++)
{
pointMap->SetId(i,-1);
}
newCellPts = vtkIdList::New();
// are we using pointScalars?
usePointScalars = (inScalars->GetNumberOfTuples() == numPts);
// Check that the scalars of each cell satisfy the threshold criterion
for (cellId=0; cellId < input->GetNumberOfCells(); cellId++)
{
cell = input->GetCell(cellId);
cellPts = cell->GetPointIds();
numCellPts = cell->GetNumberOfPoints();
if ( usePointScalars )
{
if (this->AllScalars)
{
keepCell = 1;
for ( i=0; keepCell && (i < numCellPts); i++)
{
ptId = cellPts->GetId(i);
keepCell = this->EvaluateComponents( inScalars, ptId );
}
}
else
{
keepCell = 0;
for ( i=0; (!keepCell) && (i < numCellPts); i++)
{
ptId = cellPts->GetId(i);
keepCell = this->EvaluateComponents( inScalars, ptId );
}
}
}
else //use cell scalars
{
keepCell = this->EvaluateComponents( inScalars, cellId );
}
if ( numCellPts > 0 && keepCell )
{
// satisfied thresholding (also non-empty cell, i.e. not VTK_EMPTY_CELL)
for (i=0; i < numCellPts; i++)
{
ptId = cellPts->GetId(i);
if ( (newId = pointMap->GetId(ptId)) < 0 )
{
input->GetPoint(ptId, x);
newId = newPoints->InsertNextPoint(x);
pointMap->SetId(ptId,newId);
outPD->CopyData(pd,ptId,newId);
}
newCellPts->InsertId(i,newId);
}
newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
outCD->CopyData(cd,cellId,newCellId);
newCellPts->Reset();
} // satisfied thresholding
} // for all cells
vtkDebugMacro(<< "Extracted " << output->GetNumberOfCells()
<< " number of cells.");
// now clean up / update ourselves
pointMap->Delete();
newCellPts->Delete();
output->SetPoints(newPoints);
newPoints->Delete();
output->Squeeze();
return 1;
}
int vtkThreshold::EvaluateComponents( vtkDataArray *scalars, vtkIdType id )
{
int keepCell = 0;
int numComp = scalars->GetNumberOfComponents();
int c;
switch ( this->ComponentMode )
{
case VTK_COMPONENT_MODE_USE_SELECTED:
c = (this->SelectedComponent < numComp)?(this->SelectedComponent):(0);
keepCell =
(this->*(this->ThresholdFunction))(scalars->GetComponent(id,c));
break;
case VTK_COMPONENT_MODE_USE_ANY:
keepCell = 0;
for ( c = 0; (!keepCell) && (c < numComp); c++ )
{
keepCell =
(this->*(this->ThresholdFunction))(scalars->GetComponent(id,c));
}
break;
case VTK_COMPONENT_MODE_USE_ALL:
keepCell = 1;
for ( c = 0; keepCell && (c < numComp); c++ )
{
keepCell =
(this->*(this->ThresholdFunction))(scalars->GetComponent(id,c));
}
break;
}
return keepCell;
}
// Return the method for manipulating scalar data as a string.
const char *vtkThreshold::GetAttributeModeAsString(void)
{
if ( this->AttributeMode == VTK_ATTRIBUTE_MODE_DEFAULT )
{
return "Default";
}
else if ( this->AttributeMode == VTK_ATTRIBUTE_MODE_USE_POINT_DATA )
{
return "UsePointData";
}
else
{
return "UseCellData";
}
}
// Return a string representation of the component mode
const char *vtkThreshold::GetComponentModeAsString(void)
{
if ( this->ComponentMode == VTK_COMPONENT_MODE_USE_SELECTED )
{
return "UseSelected";
}
else if ( this->ComponentMode == VTK_COMPONENT_MODE_USE_ANY )
{
return "UseAny";
}
else
{
return "UseAll";
}
}
int vtkThreshold::FillInputPortInformation(int, vtkInformation *info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
return 1;
}
void vtkThreshold::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Attribute Mode: " << this->GetAttributeModeAsString() << endl;
os << indent << "Component Mode: " << this->GetComponentModeAsString() << endl;
os << indent << "Selected Component: " << this->SelectedComponent << endl;
os << indent << "All Scalars: " << this->AllScalars << "\n";
if ( this->ThresholdFunction == &vtkThreshold::Upper )
{
os << indent << "Threshold By Upper\n";
}
else if ( this->ThresholdFunction == &vtkThreshold::Lower )
{
os << indent << "Threshold By Lower\n";
}
else if ( this->ThresholdFunction == &vtkThreshold::Between )
{
os << indent << "Threshold Between\n";
}
os << indent << "Lower Threshold: " << this->LowerThreshold << "\n";
os << indent << "Upper Threshold: " << this->UpperThreshold << "\n";
}
|