Skip to content
Snippets Groups Projects
Commit d950f4ba authored by Cory Quammen's avatar Cory Quammen
Browse files

Add vtkAppendLocationAttributes filter

This filter appends (optionally) cell centers as a cell data array and
point locations as a point data array. Added a test for the filter as
well.
parent f80f4fde
No related branches found
No related tags found
No related merge requests found
set(classes
vtkAnnotationLink
vtkAppendLocationAttributes
vtkAppendPoints
vtkApproximatingSubdivisionFilter
vtkAreaContourSpectrumFilter
......
......@@ -7,6 +7,7 @@ vtk_add_test_cxx(vtkFiltersGeneralCxxTests tests
ArrayMatricizeArray.cxx,NO_VALID
ArrayNormalizeMatrixVectors.cxx,NO_VALID
CellTreeLocator.cxx,NO_VALID
TestAppendLocationAttributes.cxx,NO_VALID
TestPassArrays.cxx,NO_VALID
TestPassSelectedArrays.cxx,NO_VALID
TestPassThrough.cxx,NO_VALID
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestAppendLocationAttributes.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 <vtkAppendLocationAttributes.h>
#include <vtkCellCenters.h>
#include <vtkCellData.h>
#include <vtkCellTypeSource.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkUnstructuredGrid.h>
#include <cstdlib>
int TestAppendLocationAttributes(int, char*[])
{
// Reference dataset
vtkNew<vtkCellTypeSource> cellTypeSource;
cellTypeSource->SetBlocksDimensions(10, 10, 10);
cellTypeSource->Update();
vtkUnstructuredGrid* inputUG = cellTypeSource->GetOutput();
// Create a vtkCellCenters object and use it to test the cell centers calculation in
// vtkAppendLocationAttributes.
vtkNew<vtkCellCenters> cellCenters;
cellCenters->SetInputConnection(cellTypeSource->GetOutputPort());
cellCenters->Update();
vtkPointSet* cellCentersOutput = cellCenters->GetOutput();
vtkNew<vtkAppendLocationAttributes> locationAttributes;
locationAttributes->SetInputConnection(cellTypeSource->GetOutputPort());
locationAttributes->Update();
vtkPointSet* appendLocationOutput = locationAttributes->GetOutput();
vtkIdType numCells = appendLocationOutput->GetNumberOfCells();
vtkIdType numPoints = appendLocationOutput->GetNumberOfPoints();
if (numCells != inputUG->GetNumberOfCells())
{
std::cerr << "Output number of cells is incorrect" << std::endl;
return EXIT_FAILURE;
}
if (numPoints != inputUG->GetNumberOfPoints())
{
std::cerr << "Output number of points is incorrect" << std::endl;
return EXIT_FAILURE;
}
vtkPoints* cellCenterPoints = cellCentersOutput->GetPoints();
vtkDataArray* cellCentersArray = appendLocationOutput->GetCellData()->GetArray("CellCenters");
vtkDataArray* pointLocationsArray = appendLocationOutput->GetPointData()->GetArray("PointLocations");
for (vtkIdType i = 0; i < numCells; ++i)
{
double cellCenter[3];
cellCenterPoints->GetPoint(i, cellCenter);
double appendLocationCenter[3];
cellCentersArray->GetTuple(i, appendLocationCenter);
double dist2 = vtkMath::Distance2BetweenPoints(cellCenter, appendLocationCenter);
if (dist2 > 1e-9)
{
std::cerr << "Cell center mismatch for cell " << i << std::endl;
return EXIT_FAILURE;
}
}
for (vtkIdType i = 0; i < numPoints; ++i)
{
double inputPoint[3];
inputUG->GetPoints()->GetPoint(i, inputPoint);
double appendLocationPoint[3];
pointLocationsArray->GetTuple(i, appendLocationPoint);
double dist2 = vtkMath::Distance2BetweenPoints(inputPoint, appendLocationPoint);
if (dist2 > 1e-9)
{
std::cerr << "Point location mismatch for point " << i << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAppendLocationAttributes.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 "vtkAppendLocationAttributes.h"
#include "vtkCell.h"
#include "vtkCellArray.h"
#include "vtkCellCenters.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
vtkStandardNewMacro(vtkAppendLocationAttributes);
//----------------------------------------------------------------------------
// Generate points
int vtkAppendLocationAttributes::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
// get the input and output
vtkPointSet* input = vtkPointSet::GetData(inputVector[0]);
vtkPointSet* output = vtkPointSet::GetData(outputVector);
output->ShallowCopy(input);
vtkTypeBool abort = 0;
// Create cell centers array
vtkNew<vtkDoubleArray> cellCenterArray;
if (this->AppendCellCenters)
{
vtkIdType numCells = input->GetNumberOfCells();
cellCenterArray->SetName("CellCenters");
cellCenterArray->SetNumberOfComponents(3);
cellCenterArray->SetNumberOfTuples(numCells);
vtkCellCenters::ComputeCellCenters(input, cellCenterArray);
}
if (abort)
{
vtkWarningMacro(<< "Aborting execution");
return 0;
}
if (this->AppendPointLocations)
{
vtkPointData* outPD = output->GetPointData();
vtkDataArray* pointArray = output->GetPoints()->GetData();
vtkSmartPointer<vtkDataArray> arrayCopy;
arrayCopy.TakeReference(pointArray->NewInstance());
arrayCopy->ShallowCopy(pointArray);
arrayCopy->SetName("PointLocations");
outPD->AddArray(arrayCopy);
}
if (this->AppendCellCenters)
{
vtkCellData* outCD = output->GetCellData();
outCD->AddArray(cellCenterArray);
}
this->UpdateProgress(1.0);
return 1;
}
//----------------------------------------------------------------------------
int vtkAppendLocationAttributes::FillInputPortInformation(int, vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
return 1;
}
//----------------------------------------------------------------------------
void vtkAppendLocationAttributes::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "AppendPointLocations: " << (this->AppendPointLocations ? "On\n" : "Off\n");
os << indent << "AppendCellCenters: " << (this->AppendCellCenters ? "On" : "Off") << endl;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAppendLocationAttributes.h
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.
=========================================================================*/
/**
* @class vtkAppendLocationAttributes
* @brief add point locations to point data and/or cell centers cell data, respectively
*
* vtkAppendLocationAttributes is a filter that takes as input any dataset and
* optionally adds points as point data and optionally adds cell center locations as
* cell data in the output. The center of a cell is its parametric center, not necessarily
* the geometric or bounding box center. Point and cell attributes in the input can optionally
* be copied to the output.
*
* @note
* Empty cells will have their center set to (0, 0, 0).
*
* @sa
* vtkCellCenters
*/
#ifndef vtkAppendLocationAttributes_h
#define vtkAppendLocationAttributes_h
#include "vtkFiltersGeneralModule.h" // For export macro
#include "vtkPointSetAlgorithm.h"
class VTKFILTERSGENERAL_EXPORT vtkAppendLocationAttributes : public vtkPointSetAlgorithm
{
public:
static vtkAppendLocationAttributes* New();
vtkTypeMacro(vtkAppendLocationAttributes, vtkPointSetAlgorithm);
void PrintSelf(ostream& os, vtkIndent indent) override;
/**
* Enable/disable whether input point locations should be saved as a point data array.
* Default is `true` i.e. the points will be propagated as a point data array named
* "PointLocations".
*/
vtkSetMacro(AppendPointLocations, bool);
vtkGetMacro(AppendPointLocations, bool);
vtkBooleanMacro(AppendPointLocations, bool);
//@}
//@{
/**
* Enable/disable whether input cell center locations should be saved as a cell data array.
* Default is `true` i.e. the cell centers will be propagated as a cell data array named
* "CellCenters".
*/
vtkSetMacro(AppendCellCenters, bool);
vtkGetMacro(AppendCellCenters, bool);
vtkBooleanMacro(AppendCellCenters, bool);
//@}
protected:
vtkAppendLocationAttributes() = default;
~vtkAppendLocationAttributes() = default;
int RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector*) override;
int FillInputPortInformation(int port, vtkInformation* info) override;
bool AppendPointLocations = true;
bool AppendCellCenters = true;
private:
vtkAppendLocationAttributes(const vtkAppendLocationAttributes&) = delete;
void operator=(const vtkAppendLocationAttributes&) = delete;
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment