Commit 37a8d3c7 authored by Cory Quammen's avatar Cory Quammen Committed by Kitware Robot

Merge topic 'robust-threshold-selection'

b304e92d TextExtraction: modernize layout of threshold array
39ef92b6 TestExtraction: Change test data arrays to vtkFloatArray
e70bd603 Handle selection of non-vtkDoubleArrays
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Utkarsh Ayachit's avatarUtkarsh Ayachit <utkarsh.ayachit@kitware.com>
Merge-request: !4711
parents 26e37c69 b304e92d
......@@ -29,6 +29,7 @@
#include <vtkCellData.h>
#include <vtkDataSetMapper.h>
#include <vtkDoubleArray.h>
#include <vtkFloatArray.h>
#include <vtkIdList.h>
#include <vtkIdTypeArray.h>
#include <vtkImageData.h>
......@@ -159,7 +160,7 @@ int TestExtraction(int argc, char *argv[])
sampleData->SetSpacing(1.0,1.0,1.0);
sampleData->SetOrigin(0.0,0.0,0.0);
sampleData->SetDimensions(XCELLS+1,YCELLS+1,ZCELLS+1);
sampleData->AllocateScalars(VTK_DOUBLE, 1);
sampleData->AllocateScalars(VTK_FLOAT, 1);
vtkIdTypeArray *pia = vtkIdTypeArray::New();
pia->SetNumberOfComponents(1);
......@@ -176,17 +177,17 @@ int TestExtraction(int argc, char *argv[])
piaR->SetName("Reverse Point Ids");
sampleData->GetPointData()->AddArray(piaR);
vtkDoubleArray *pxa = vtkDoubleArray::New();
vtkFloatArray *pxa = vtkFloatArray::New();
pxa->SetNumberOfComponents(1);
pxa->SetName("Point X");
sampleData->GetPointData()->AddArray(pxa);
vtkDoubleArray *pya = vtkDoubleArray::New();
vtkFloatArray *pya = vtkFloatArray::New();
pya->SetNumberOfComponents(1);
pya->SetName("Point Y");
sampleData->GetPointData()->AddArray(pya);
vtkDoubleArray *pza = vtkDoubleArray::New();
vtkFloatArray *pza = vtkFloatArray::New();
pza->SetNumberOfComponents(1);
pza->SetName("Point Z");
sampleData->GetPointData()->AddArray(pza);
......@@ -745,10 +746,10 @@ int TestExtraction(int argc, char *argv[])
vtkSelectionNode::CONTENT_TYPE(), vtkSelectionNode::THRESHOLDS);
sel->SetFieldType(vtkSelectionNode::CELL);
vtkDoubleArray *cellThresh = vtkDoubleArray::New();
cellThresh->SetNumberOfComponents(1);
cellThresh->SetNumberOfTuples(2);
cellThresh->SetTuple1(0, 1.9); //the nine rightmost(+X) cells are in here
cellThresh->SetTuple1(1, 3.1);
cellThresh->SetNumberOfComponents(2);
cellThresh->SetNumberOfTuples(1);
cellThresh->SetComponent(0, 0, 1.9); //the nine rightmost(+X) cells are in here
cellThresh->SetComponent(0, 1, 3.1);
sel->SetSelectionList(cellThresh);
cellThresh->Delete();
......@@ -794,10 +795,10 @@ int TestExtraction(int argc, char *argv[])
vtkSelectionNode::CONTENT_TYPE(), vtkSelectionNode::THRESHOLDS);
sel->GetProperties()->Set(vtkSelectionNode::FIELD_TYPE(), vtkSelectionNode::POINT);
vtkDoubleArray *pointThresh = vtkDoubleArray::New();
pointThresh->SetNumberOfComponents(1);
pointThresh->SetNumberOfTuples(2);
pointThresh->SetTuple1(0, 0.9); //the 18 leftmost cells have points in here
pointThresh->SetTuple1(1, 1.1);
pointThresh->SetNumberOfComponents(2);
pointThresh->SetNumberOfTuples(1);
pointThresh->SetComponent(0, 0, 0.9); //the 18 leftmost cells have points in here
pointThresh->SetComponent(0, 1, 1.1);
sel->SetSelectionList(pointThresh);
pointThresh->Delete();
......
......@@ -374,11 +374,54 @@ private:
if (!vtkArrayDispatch::Dispatch2BySameValueType<vtkArrayDispatch::AllTypes>::Execute(
darray, vtkDataArray::SafeDownCast(this->SelectionList), worker))
{
// should we use slow data array API?
vtkGenericWarningMacro("Type mismatch in selection list ("
<< this->SelectionList->GetClassName() << ") and field array ("
<< darray->GetClassName() << ").");
return false;
// Use the slow data array API for threshold-based selection. Thresholds
// are assumed to be stored in a vtkDoubleArray, which may very well not be the
// same as the data array type.
vtkDataArray* selList = vtkDataArray::SafeDownCast(this->SelectionList);
const int comp = darray->GetNumberOfComponents() == 1 ? 0 : this->ComponentNo;
const vtkIdType numRanges = selList->GetNumberOfTuples();
if (comp >= 0)
{
vtkSMPTools::For(0, darray->GetNumberOfTuples(), [=](vtkIdType begin, vtkIdType end) {
for (vtkIdType cc = begin; cc < end; ++cc)
{
const auto val = darray->GetComponent(cc, comp);
bool match = false;
for (vtkIdType r = 0; r < numRanges && !match; ++r)
{
match = (val >= selList->GetComponent(r, 0) &&
val <= selList->GetComponent(r, 1));
}
insidednessArray->SetValue(cc, match ? 1 : 0);
}
});
}
else
{
const int num_components = darray->GetNumberOfComponents();
// compare vector magnitude.
vtkSMPTools::For(0, darray->GetNumberOfTuples(), [=](vtkIdType begin, vtkIdType end) {
for (vtkIdType cc = begin; cc < end; ++cc)
{
double val = double(0);
for (int kk = 0; kk < num_components; ++kk)
{
const auto valKK = darray->GetComponent(cc, comp);
val += valKK * valKK;
}
const auto magnitude = std::sqrt(val);
bool match = false;
for (vtkIdType r = 0; r < numRanges && !match; ++r)
{
match = (magnitude >= selList->GetComponent(r, 0) &&
magnitude <= selList->GetComponent(r, 1));
}
insidednessArray->SetValue(cc, match ? 1 : 0);
}
});
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment