Commit ceb86646 authored by Will Schroeder's avatar Will Schroeder
Browse files

Separated templated code; created wrappable class

A templated static cell links class was created for performance
and memory reasons. However, to enhance usability, a instantiated
version based on vtkIdType was created so that it could be accessed
from wrapped languages (e.g., Python).
parent 340b7506
set(Module_SRCS
vtkAbstractCellLinks.cxx
vtkAbstractCellLocator.cxx
vtkAbstractPointLocator.cxx
vtkAdjacentVertexIterator.cxx
......@@ -157,6 +158,8 @@ set(Module_SRCS
vtkSphere.cxx
vtkSpline.cxx
vtkStaticCellLinks.txx
vtkStaticCellLinks.cxx
vtkStaticCellLinksTemplate.txx
vtkStaticPointLocator.cxx
vtkStructuredData.cxx
vtkStructuredExtent.cxx
......@@ -214,10 +217,11 @@ set(${vtk-module}_HDRS
vtkCellType.h
vtkMappedUnstructuredGrid.h
vtkMappedUnstructuredGridCellIterator.h
vtkStaticCellLinks.h
vtkStaticCellLinksTemplate.h
)
set_source_files_properties(
vtkAbstractCellLinks
vtkAbstractCellLocator
vtkAbstractPointLocator
vtkCell
......@@ -273,7 +277,7 @@ set_source_files_properties(
vtkImageProgressIterator
vtkPixelExtent.cxx
vtkPixelTransfer.cxx
vtkStaticCellLinks.txx
vtkStaticCellLinksTemplate.txx
vtkVector
vtkColor
vtkRect
......
......@@ -12,7 +12,7 @@
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkStaticCellLinks.h"
#include "vtkStaticCellLinksTemplate.h"
#include "vtkSmartPointer.h"
#include "vtkImageData.h"
#include "vtkUnstructuredGrid.h"
......@@ -51,7 +51,7 @@ int TestStaticCellLinks( int, char *[] )
vtkSmartPointer<vtkUnstructuredGrid>::New();
ugrid = extract->GetOutput();
vtkStaticCellLinks<int> slinks;
vtkStaticCellLinksTemplate<int> slinks;
slinks.BuildLinks(ugrid);
int numCells = slinks.GetNumberOfCells(0);
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAbstractCellLinks.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 "vtkAbstractCellLinks.h"
#include "vtkObjectFactory.h"
#include "vtkCellArray.h"
//----------------------------------------------------------------------------
vtkAbstractCellLinks::vtkAbstractCellLinks()
{
}
//----------------------------------------------------------------------------
vtkAbstractCellLinks::~vtkAbstractCellLinks()
{
}
//----------------------------------------------------------------------------
int vtkAbstractCellLinks::
GetIdType(vtkIdType maxPtId, vtkIdType maxCellId, vtkCellArray *ca)
{
vtkIdType numEntries = ca->GetNumberOfConnectivityEntries();
vtkIdType max = maxPtId;
max = (maxCellId > max ? maxCellId : max);
max = (numEntries > max ? numEntries : max);
if ( max >= VTK_INT_MAX )
{
return VTK_ID_TYPE;
}
else if ( max >= VTK_SHORT_MAX )
{
return VTK_INT;
}
else
{
return VTK_SHORT;
}
}
//----------------------------------------------------------------------------
void vtkAbstractCellLinks::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAbstractCellLinks.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 vtkAbstractCellLinks - an abstract base class for classes that build
// topological links from points to cells
// .SECTION Description
// vtkAbstractCellLinks is a family of supplemental objects to vtkCellArray and
// vtkCellTypes, enabling fast access from points to the cells using the
// points. vtkStaticCellLinksTemplate is an array of links, each link represents a
// list of cell id's using a particular point. The information provided by
// this object can be used to determine neighbors and construct other local
// topological information.
//
// .SECTION See Also
// vtkCellLinks vtkStaticCellLinks vtkStaticCellLinksTemplate
#ifndef vtkAbstractCellLinks_h
#define vtkAbstractCellLinks_h
#include "vtkCommonDataModelModule.h" // For export macro
#include "vtkObject.h"
class vtkPolyData;
class vtkUnstructuredGrid;
class vtkCellArray;
class VTKCOMMONDATAMODEL_EXPORT vtkAbstractCellLinks : public vtkObject
{
public:
// Description:
// Standard type and print methods.
vtkTypeMacro(vtkAbstractCellLinks,vtkObject);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Build the link list array. All subclasses must implement this method.
virtual void BuildLinks(vtkDataSet *data) = 0;
// Description:
// Based on the input (i.e., number of points, number of cells, and length
// of connectivity array) this helper method returns the integral type to
// use when instantiating the class in order to properly represent the data.
// The return value is one of the types (VTK_ID_TYPE,VTK_INT,VTK_SHORT) defined
// in the file vtkType.h. Subclasses may choose to instantiate themselves with
// different integral types for performance and/or memory reasons.
static int GetIdType(vtkIdType maxPtId, vtkIdType maxCellId, vtkCellArray *ca);
protected:
vtkAbstractCellLinks();
~vtkAbstractCellLinks();
private:
vtkAbstractCellLinks(const vtkAbstractCellLinks&); // Not implemented.
void operator=(const vtkAbstractCellLinks&); // Not implemented.
};
#endif
......@@ -26,11 +26,12 @@
#define vtkCellLinks_h
#include "vtkCommonDataModelModule.h" // For export macro
#include "vtkObject.h"
#include "vtkAbstractCellLinks.h"
class vtkDataSet;
class vtkCellArray;
class VTKCOMMONDATAMODEL_EXPORT vtkCellLinks : public vtkObject
class VTKCOMMONDATAMODEL_EXPORT vtkCellLinks : public vtkAbstractCellLinks
{
public:
......@@ -42,10 +43,21 @@ public:
};
//ETX
// Description:
// Standard methods to instantiate, print, and obtain type information.
static vtkCellLinks *New();
vtkTypeMacro(vtkCellLinks,vtkObject);
vtkTypeMacro(vtkCellLinks,vtkAbstractCellLinks);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Build the link list array. All subclasses of vtkAbstractCellLinks
// must support this method.
virtual void BuildLinks(vtkDataSet *data);
// Description:
// Build the link list array.
void BuildLinks(vtkDataSet *data, vtkCellArray *Connectivity);
// Description:
// Allocate the specified number of links (i.e., number of points) that
// will be built.
......@@ -59,14 +71,6 @@ public:
// Get the number of cells using the point specified by ptId.
unsigned short GetNcells(vtkIdType ptId) { return this->Array[ptId].ncells;};
// Description:
// Build the link list array.
void BuildLinks(vtkDataSet *data);
// Description:
// Build the link list array.
void BuildLinks(vtkDataSet *data, vtkCellArray *Connectivity);
// Description:
// Return a list of cell ids using the point.
vtkIdType *GetCells(vtkIdType ptId) {return this->Array[ptId].cells;};
......@@ -214,4 +218,3 @@ inline void vtkCellLinks::ResizeCellList(vtkIdType ptId, int size)
}
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStaticCellLinks.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 "vtkStaticCellLinks.h"
#include "vtkObjectFactory.h"
#include "vtkCellArray.h"
#include "vtkDataSet.h"
#include "vtkPolyData.h"
#include "vtkUnstructuredGrid.h"
vtkStandardNewMacro(vtkStaticCellLinks);
//----------------------------------------------------------------------------
vtkStaticCellLinks::vtkStaticCellLinks()
{
}
//----------------------------------------------------------------------------
vtkStaticCellLinks::~vtkStaticCellLinks()
{
}
//----------------------------------------------------------------------------
// Build the link list array.
void vtkStaticCellLinks::BuildLinks(vtkDataSet *data)
{
}
//----------------------------------------------------------------------------
// Build the link list array.
void vtkStaticCellLinks::BuildLinks(vtkDataSet *data, vtkCellArray *Connectivity)
{
}
//----------------------------------------------------------------------------
void vtkStaticCellLinks::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
......@@ -26,74 +26,63 @@
// to be constructed once (statically) and must be rebuilt if the cells
// change.
// .SECTION Caveats
// This is a drop-in replacement for vtkCellLinks using static link
// construction. It uses the templated vtkStaticCellLinksTemplate class,
// instantiating vtkStaticCellLinksTemplate with a vtkIdType template
// parameter. Note that for best performance, the vtkStaticCellLinksTemplate
// class may be used directly, instantiating it with the appropriate id
// type. This class is also wrappable and can be used from an interpreted
// language such as Python.
// .SECTION See Also
// vtkCellLinks
// vtkCellLinks vtkStaticCellLinksTemplate
#ifndef vtkStaticCellLinks_h
#define vtkStaticCellLinks_h
#include "vtkObject.h"
#include "vtkSmartPointer.h" // For vtkSmartPointer
#include "vtkTypeTemplate.h" // For vtkTypeTemplate
#include "vtkCommonDataModelModule.h" // For export macro
#include "vtkAbstractCellLinks.h"
#include "vtkStaticCellLinksTemplate.h" // For implementations
class vtkDataSet;
class vtkCellArray;
class vtkPolyData;
class vtkUnstructuredGrid;
template <typename TIds>
class vtkStaticCellLinks
class VTKCOMMONDATAMODEL_EXPORT vtkStaticCellLinks : public vtkAbstractCellLinks
{
public:
// Description:
// Default constructor. BuildLinks() does most of the work.
vtkStaticCellLinks() :
LinksSize(0), NumPts(0), NumCells(0), Links(NULL), Offsets(NULL)
{
}
// Standard methods for instantiation, type manipulation and printing.
static vtkStaticCellLinks *New();
vtkTypeMacro(vtkStaticCellLinks,vtkAbstractCellLinks);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Release memory if necessary.
~vtkStaticCellLinks()
{
if ( this->Links )
{
delete [] this->Links;
}
if ( this->Offsets )
{
delete [] this->Offsets;
}
}
// Build the link list array. Satisfy the superclass API.
virtual void BuildLinks(vtkDataSet *data);
// Description:
// Build the link list array.
void BuildLinks(vtkPolyData *pd);
// Get the number of cells using the point specified by ptId.
vtkIdType GetNumberOfCells(vtkIdType ptId)
{ return 0;}
// Description:
// Build the link list array.
void BuildLinks(vtkUnstructuredGrid *ugrid);
// Get the number of cells using the point specified by ptId. This is an
// alias for GetNumberOfCells(); consistent with the vtkCellLinks API.
unsigned short GetNcells(vtkIdType ptId)
{ return static_cast<unsigned short>(this->GetNumberOfCells()); }
// Description:
// Get the number of cells using the point specified by ptId.
TIds GetNumberOfCells(vtkIdType ptId)
{
return (this->Offsets[ptId+1] - this->Offsets[ptId]);
}
// Return a list of cell ids using the specified point.
vtkIdType *GetCells(vtkIdType ptId)
{return NULL;}
// Description:
// Return a list of cell ids using the point.
const TIds *GetCells(vtkIdType ptId)
{
return this->Links + this->Offsets[ptId];
}
protected:
// Okay the various ivars
TIds LinksSize;
TIds NumPts;
TIds NumCells;
vtkStaticCellLinks();
~vtkStaticCellLinks();
TIds *Links; //contiguous runs of cells
TIds *Offsets; //offsets for each point into the link array
private:
vtkStaticCellLinks(const vtkStaticCellLinks&); // Not implemented.
......@@ -101,7 +90,5 @@ private:
};
#include "vtkStaticCellLinks.txx"
#endif
// VTK-HeaderTest-Exclude: vtkStaticCellLinks.h
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStaticCellLinksTemplate.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 vtkStaticCellLinksTemplate - object represents upward pointers from points
// to list of cells using each point (template implementation)
// .SECTION Description
// vtkStaticCellLinksTemplate is a supplemental object to vtkCellArray and
// vtkCellTypes, enabling access from points to the cells using the
// points. vtkStaticCellLinksTemplate is an array of links, each link represents a
// list of cell id's using a particular point. The information provided by
// this object can be used to determine neighbors and construct other local
// topological information. This class is a faster implementation of
// vtkCellLinks. However, it cannot be incrementally constructed; it is meant
// to be constructed once (statically) and must be rebuilt if the cells
// change.
//
// This is a templated implementation for vtkStaticCellLinks. The reason for
// the templating is to gain performance and reduce memory by using smaller
// integral types to represent ids. For example, if the maximum id can be
// represented by an int (as compared to a vtkIdType), it is possible to
// reduce memory requirements by half and increase performance up to
// 30%. This templated class can be used directly; alternatively the
// non-templated class vtkStaticCellLinks can be used for convenience;
// although it uses vtkIdType and thereby loses some speed and memory
// advantage.
// .SECTION See Also
// vtkCellLinks vtkStaticCellLinks
#ifndef vtkStaticCellLinksTemplate_h
#define vtkStaticCellLinksTemplate_h
#include "vtkAbstractCellLinks.h"
class vtkPolyData;
class vtkUnstructuredGrid;
class vtkCellArray;
template <typename TIds>
class vtkStaticCellLinksTemplate : vtkAbstractCellLinks
{
public:
// Description:
// Default constructor. BuildLinks() does most of the work.
vtkStaticCellLinksTemplate() :
LinksSize(0), NumPts(0), NumCells(0), Links(NULL), Offsets(NULL)
{
}
// Description:
// Release memory if necessary.
~vtkStaticCellLinksTemplate()
{
if ( this->Links )
{
delete [] this->Links;
}
if ( this->Offsets )
{
delete [] this->Offsets;
}
}
// Description:
// Build the link list array. Satisfy superclass' API.
virtual void BuildLinks(vtkDataSet *ds);
// Description:
// Build the link list array for vtkPolyData.
void BuildLinks(vtkPolyData *pd);
// Description:
// Build the link list array for vtkUnstructuredGrid.
void BuildLinks(vtkUnstructuredGrid *ugrid);
// Description:
// Get the number of cells using the point specified by ptId.
TIds GetNumberOfCells(vtkIdType ptId)
{
return (this->Offsets[ptId+1] - this->Offsets[ptId]);
}
// Description:
// Return a list of cell ids using the point.
const TIds *GetCells(vtkIdType ptId)
{
return this->Links + this->Offsets[ptId];
}
protected:
// Okay the various ivars
TIds LinksSize;
TIds NumPts;
TIds NumCells;
TIds *Links; //contiguous runs of cells
TIds *Offsets; //offsets for each point into the link array
private:
vtkStaticCellLinksTemplate(const vtkStaticCellLinksTemplate&); // Not implemented.
void operator=(const vtkStaticCellLinksTemplate&); // Not implemented.
};
#include "vtkStaticCellLinksTemplate.txx"
#endif
// VTK-HeaderTest-Exclude: vtkStaticCellLinksTemplate.h
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStaticCellLinksTemplate.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 "vtkStaticCellLinksTemplate.h"
#ifndef vtkStaticCellLinksTemplate_txx
#define vtkStaticCellLinksTemplate_txx
#include "vtkCellArray.h"
#include "vtkDataSet.h"
#include "vtkPolyData.h"
#include "vtkUnstructuredGrid.h"
//----------------------------------------------------------------------------
// Build the link list array for any dataset type
template <typename TIds> void vtkStaticCellLinksTemplate<TIds>::
BuildLinks(vtkDataSet *ds)
{
// Use a fast path if polydata or unstructured grid
if ( ds->GetDataObjectType() == VTK_POLY_DATA )
{
return this->BuildLinks(static_cast<vtkPolyData*>(ds));
}
else if ( ds->GetDataObjectType() == VTK_UNSTRUCTURED_GRID )
{
return this->BuildLinks(static_cast<vtkUnstructuredGrid*>(ds));
}
// Any other type of dataset. Generally this is not called as there are
// more efficient ways of getting similar information.
this->NumCells = ds->GetNumberOfCells();
this->NumPts = ds->GetNumberOfPoints();
vtkIdType numberOfPoints, ptId;
vtkGenericCell *cell=vtkGenericCell::New();
vtkIdType cellId, j;
// traverse data to determine number of uses of each point
for (cellId=0; cellId < numCells; cellId++)
{
data->GetCell(cellId,cell);
numberOfPoints = cell->GetNumberOfPoints();
for (j=0; j < numberOfPoints; j++)
{
this->IncrementLinkCount(cell->PointIds->GetId(j));
}
}
// now allocate storage for the links
this->AllocateLinks(numPts);
this->MaxId = numPts - 1;
for (cellId=0; cellId < numCells; cellId++)
{
data->GetCell(cellId,cell);
numberOfPoints = cell->GetNumberOfPoints();
for (j=0; j < numberOfPoints; j++)
{
ptId = cell->PointIds->GetId(j);
this->InsertCellReference(ptId, (linkLoc[ptId])++, cellId);
}
}
cell->Delete();
}
//----------------------------------------------------------------------------
// Build the link list array for unstructured grids
template <typename TIds> void vtkStaticCellLinksTemplate<TIds>::
BuildLinks(vtkUnstructuredGrid *ugrid)
{
// Basic information about the grid
this->NumCells = ugrid->GetNumberOfCells();
this->NumPts = ugrid->GetNumberOfPoints();
// We're going to get into the guts of the class