Commit 3cc2b845 authored by David C. Lonie's avatar David C. Lonie
Browse files

Add vtkCountFaces filter.

Counts faces per cell, adds them in a new cell data array.
parent af47eacd
Pipeline #23475 passed with stage
......@@ -17,6 +17,7 @@ set(Module_SRCS
vtkClipVolume.cxx
vtkCoincidentPoints.cxx
vtkContourTriangulator.cxx
vtkCountFaces.cxx
vtkCursor2D.cxx
vtkCursor3D.cxx
vtkCurvatures.cxx
......
......@@ -21,6 +21,7 @@ vtk_add_test_cxx(${vtk-module}CxxTests tests
TestContourTriangulatorCutter.cxx
TestContourTriangulator.cxx
TestContourTriangulatorMarching.cxx
TestCountFaces.cxx,NO_VALID
TestDeformPointSet.cxx
TestDensifyPolyData.cxx
TestDistancePolyDataFilter.cxx
......
/*=========================================================================
Program: Visualization Toolkit
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 "vtkCountFaces.h"
#include "vtkCellData.h"
#include "vtkCellType.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkNew.h"
#include "vtkPoints.h"
#include "vtkUnstructuredGrid.h"
int TestCountFaces(int, char*[])
{
vtkNew<vtkUnstructuredGrid> data;
vtkNew<vtkPoints> points;
vtkNew<vtkIdList> cell;
vtkNew<vtkCountFaces> filter;
// Need 12 points to test all cell types:
for (int i = 0; i < 12; ++i)
{
points->InsertNextPoint(0., 0., 0.);
}
data->SetPoints(points.Get());
// Insert the following cell types and verify the number of faces computed
// by the filter:
// VTK_VERTEX = 0
// VTK_LINE = 0
// VTK_TRIANGLE = 0
// VTK_TETRA = 4
// VTK_PYRAMID = 5
// VTK_WEDGE = 5
// VTK_VOXEL = 6
// VTK_HEXAHEDRON = 6
// VTK_PENTAGONAL_PRISM = 7
// VTK_HEXAGONAL_PRISM = 8
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_VERTEX, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_LINE, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_TRIANGLE, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_TETRA, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_PYRAMID, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_WEDGE, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_VOXEL, cell.Get());
data->InsertNextCell(VTK_HEXAHEDRON, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_PENTAGONAL_PRISM, cell.Get());
cell->InsertNextId(cell->GetNumberOfIds());
cell->InsertNextId(cell->GetNumberOfIds());
data->InsertNextCell(VTK_HEXAGONAL_PRISM, cell.Get());
filter->SetInputData(data.Get());
filter->Update();
vtkUnstructuredGrid *output =
vtkUnstructuredGrid::SafeDownCast(filter->GetOutput());
if (!output)
{
std::cerr << "No output data!\n";
return EXIT_FAILURE;
}
vtkIdTypeArray *faces =
vtkIdTypeArray::SafeDownCast(
output->GetCellData()->GetArray(
filter->GetOutputArrayName()));
if (!faces)
{
std::cerr << "No output array!\n";
return EXIT_FAILURE;
}
if (faces->GetNumberOfComponents() != 1)
{
std::cerr << "Invalid number of components in output array: "
<< faces->GetNumberOfComponents() << "\n";
return EXIT_FAILURE;
}
if (faces->GetNumberOfTuples() != 10)
{
std::cerr << "Invalid number of components in output array: "
<< faces->GetNumberOfTuples() << "\n";
return EXIT_FAILURE;
}
#define TEST_FACES(idx, expected) \
{ \
vtkIdType numFaces = faces->GetTypedComponent(idx, 0); \
if (numFaces != expected) \
{ \
std::cerr << "Expected cell @idx=" << idx << " to have " << expected \
<< " faces, but found " << numFaces << "\n"; \
return EXIT_FAILURE; \
} \
}
int idx = 0;
// VTK_VERTEX = 0
TEST_FACES(idx++, 0);
// VTK_LINE = 0
TEST_FACES(idx++, 0);
// VTK_TRIANGLE = 0
TEST_FACES(idx++, 0);
// VTK_TETRA = 4
TEST_FACES(idx++, 4);
// VTK_PYRAMID = 5
TEST_FACES(idx++, 5);
// VTK_WEDGE = 5
TEST_FACES(idx++, 5);
// VTK_VOXEL = 6
TEST_FACES(idx++, 6);
// VTK_HEXAHEDRON = 6
TEST_FACES(idx++, 6);
// VTK_PENTAGONAL_PRISM = 7
TEST_FACES(idx++, 7);
// VTK_HEXAGONAL_PRISM = 8
TEST_FACES(idx++, 8);
#undef TEST_FACES
return EXIT_SUCCESS;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCountFaces.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 "vtkCountFaces.h"
#include "vtkCellData.h"
#include "vtkCellIterator.h"
#include "vtkDataSet.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkCountFaces)
//------------------------------------------------------------------------------
void vtkCountFaces::PrintSelf(std::ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "OutputArrayName: "
<< (this->OutputArrayName ? this->OutputArrayName : "(NULL)") << "\n";
}
//------------------------------------------------------------------------------
vtkCountFaces::vtkCountFaces()
: OutputArrayName(NULL)
{
this->SetOutputArrayName("Face Count");
}
//------------------------------------------------------------------------------
vtkCountFaces::~vtkCountFaces()
{
this->SetOutputArrayName(NULL);
}
//------------------------------------------------------------------------------
int vtkCountFaces::RequestData(vtkInformation *,
vtkInformationVector **inInfoVec,
vtkInformationVector *outInfoVec)
{
// get the info objects
vtkInformation *inInfo = inInfoVec[0]->GetInformationObject(0);
vtkInformation *outInfo = outInfoVec->GetInformationObject(0);
// get the input and output
vtkDataSet *input = vtkDataSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataSet *output = vtkDataSet::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
assert(input && output);
output->ShallowCopy(input);
vtkNew<vtkIdTypeArray> faceCount;
faceCount->Allocate(input->GetNumberOfCells());
faceCount->SetName(this->OutputArrayName);
output->GetCellData()->AddArray(faceCount.Get());
vtkCellIterator *it = input->NewCellIterator();
for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell())
{
faceCount->InsertNextValue(it->GetNumberOfFaces());
}
it->Delete();
return 1;
}
//------------------------------------------------------------------------------
int vtkCountFaces::FillOutputPortInformation(int, vtkInformation *info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkDataSet");
return 1;
}
//------------------------------------------------------------------------------
int vtkCountFaces::FillInputPortInformation(int, vtkInformation *info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
return 1;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCountFaces.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.
=========================================================================*/
// .NAME vtkCountFaces - Add a cell data array containing the number of faces
// per cell.
//
// .SECTION Description
// This filter adds a cell data array containing the number of faces per cell.
#ifndef vtkCountFaces_h
#define vtkCountFaces_h
#include "vtkFiltersGeneralModule.h" // For export macro
#include "vtkPassInputTypeAlgorithm.h"
class VTKFILTERSGENERAL_EXPORT vtkCountFaces: public vtkPassInputTypeAlgorithm
{
public:
static vtkCountFaces* New();
vtkTypeMacro(vtkCountFaces, vtkPassInputTypeAlgorithm)
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// The name of the new output array containing the face counts.
vtkSetStringMacro(OutputArrayName)
vtkGetStringMacro(OutputArrayName)
protected:
vtkCountFaces();
~vtkCountFaces();
int RequestData(vtkInformation* request, vtkInformationVector **inInfoVec,
vtkInformationVector *outInfoVec);
int FillOutputPortInformation(int port, vtkInformation* info);
int FillInputPortInformation(int port, vtkInformation* info);
char *OutputArrayName;
private:
vtkCountFaces(const vtkCountFaces&) VTK_DELETE_FUNCTION;
void operator=(const vtkCountFaces&) VTK_DELETE_FUNCTION;
};
#endif // vtkCountFaces_h
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