Commit 8df357b8 authored by Timothy M. Shead's avatar Timothy M. Shead

ENH: Finish adding sample N-Way array filters and tests.

parent 4ca31006
......@@ -31,6 +31,10 @@ class vtkInformationVector;
// .NAME vtkArrayData - Pipeline data object that acts as a container
// for a single vtkArray
// .SECTION Thanks
// Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
class VTK_FILTERING_EXPORT vtkArrayData : public vtkDataObject
{
public:
......
......@@ -39,6 +39,9 @@
// signature. For even older filters that don't implement ExecuteData the
// default implementation calls the even older Execute() signature.
// .SECTION Thanks
// Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
#ifndef __vtkArrayDataAlgorithm_h
#define __vtkArrayDataAlgorithm_h
......
......@@ -31,6 +31,9 @@ class vtkArray;
// collection of vtkArray instances. Used to store the "factored"
// representation of a larger array.
// .SECTION Thanks
// Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
class VTK_FILTERING_EXPORT vtkFactoredArrayData : public vtkDataObject
{
public:
......
......@@ -39,6 +39,9 @@
// signature. For even older filters that don't implement ExecuteData the
// default implementation calls the even older Execute() signature.
// .SECTION Thanks
// Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
#ifndef __vtkFactoredArrayDataAlgorithm_h
#define __vtkFactoredArrayDataAlgorithm_h
......
......@@ -75,6 +75,7 @@ vtkXMLTreeReader.cxx
# Add these classes only if using Boost
IF(VTK_USE_BOOST)
SET(Kit_SRCS
${Kit_SRCS}
vtkBoostBiconnectedComponents.cxx
......@@ -86,8 +87,36 @@ IF(VTK_USE_BOOST)
vtkBoostPrimMinimumSpanningTree.cxx
vtkBoostSplitTableField.cxx
)
ENDIF(VTK_USE_BOOST)
IF(VTK_USE_N_WAY_ARRAYS)
SET(Kit_SRCS
${Kit_SRCS}
vtkAdjacencyMatrixToEdgeTable.cxx
vtkArrayToTable.cxx
vtkCosineSimilarity.cxx
vtkDiagonalMatrixSource.cxx
vtkExtractFactoredArray.cxx
vtkMatricizeArray.cxx
vtkMatrixColumnNorm.cxx
vtkNormalizeMatrixVectors.cxx
vtkTableToSparseArray.cxx
vtkTransposeMatrix.cxx
)
IF(VTK_USE_BOOST)
SET(Kit_SRCS
${Kit_SRCS}
vtkBoostLogWeighting.cxx
vtkBoostRandomSparseArraySource.cxx
)
ENDIF(VTK_USE_BOOST)
ENDIF(VTK_USE_N_WAY_ARRAYS)
# Add these classes only if vtkTypeUInt64Array exists
SET(VTK_HAS_UINT64_ARRAY 0)
IF(VTK_SIZEOF_LONG_LONG MATCHES "^8$")
......
/*=========================================================================
Program: Visualization Toolkit
Module: ArrayAPIDenseCoordinates.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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 <vtkArrayData.h>
#include <vtkArrayPrint.h>
#include <vtkDenseArray.h>
#include <vtkDiagonalMatrixSource.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>
#include <vtksys/ios/iostream>
#include <vtksys/stl/stdexcept>
#define test_expression(expression) \
{ \
if(!(expression)) \
throw vtkstd::runtime_error("Expression failed: " #expression); \
}
int ArrayAPIDenseCoordinates(int argc, char* argv[])
{
try
{
vtkSmartPointer<vtkDiagonalMatrixSource> source = vtkSmartPointer<vtkDiagonalMatrixSource>::New();
source->SetExtents(3);
source->SetArrayType(vtkDiagonalMatrixSource::DENSE);
source->SetDiagonal(1.0);
source->SetSuperDiagonal(0.5);
source->SetSubDiagonal(-0.5);
source->Update();
vtkDenseArray<double>* const array = vtkDenseArray<double>::SafeDownCast(source->GetOutput()->GetArray());
cout << "dense diagonal matrix:\n";
vtkPrintMatrixFormat(cout, array);
cout << "dense diagonal coordinates:\n";
vtkPrintCoordinateFormat(cout, array);
test_expression(array);
test_expression(array->GetValue(vtkArrayCoordinates(0, 0)) == 1.0);
test_expression(array->GetValue(vtkArrayCoordinates(1, 0)) == -0.5);
test_expression(array->GetValue(vtkArrayCoordinates(2, 0)) == 0.0);
test_expression(array->GetValue(vtkArrayCoordinates(0, 1)) == 0.5);
test_expression(array->GetValue(vtkArrayCoordinates(1, 1)) == 1.0);
test_expression(array->GetValue(vtkArrayCoordinates(2, 1)) == -0.5);
test_expression(array->GetValue(vtkArrayCoordinates(0, 2)) == 0.0);
test_expression(array->GetValue(vtkArrayCoordinates(1, 2)) == 0.5);
test_expression(array->GetValue(vtkArrayCoordinates(2, 2)) == 1.0);
for(vtkIdType n = 0; n != array->GetNonNullSize(); ++n)
{
vtkArrayCoordinates coordinates;
array->GetCoordinatesN(n, coordinates);
if(coordinates[0] == 0 && coordinates[1] == 0)
{
test_expression(array->GetValueN(n) == 1.0);
}
else if(coordinates[0] == 0 && coordinates[1] == 1)
{
test_expression(array->GetValueN(n) == 0.5);
}
else if(coordinates[0] == 1 && coordinates[1] == 0)
{
test_expression(array->GetValueN(n) == -0.5);
}
}
return 0;
}
catch(vtkstd::exception& e)
{
cerr << e.what() << endl;
return 1;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: ArrayCosineSimilarity.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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 <vtkArrayData.h>
#include <vtkArrayPrint.h>
#include <vtkCosineSimilarity.h>
#include <vtkDenseArray.h>
#include <vtkSmartPointer.h>
#include <vtkTable.h>
#include <vtkVariant.h>
#include <vtksys/ios/iostream>
#include <vtksys/stl/stdexcept>
#define test_expression(expression) \
{ \
if(!(expression)) \
throw vtkstd::runtime_error("Expression failed: " #expression); \
}
static const bool close_enough(const double lhs, const double rhs)
{
return fabs(lhs - rhs) < 1.0e-12;
}
int ArrayCosineSimilarity(int argc, char* argv[])
{
cout << setprecision(17);
try
{
vtkSmartPointer<vtkDenseArray<double> > source = vtkSmartPointer<vtkDenseArray<double> >::New();
source->Resize(vtkArrayExtents(2, 4));
source->SetValue(vtkArrayCoordinates(0, 0), 1);
source->SetValue(vtkArrayCoordinates(1, 0), 0);
source->SetValue(vtkArrayCoordinates(0, 1), 1);
source->SetValue(vtkArrayCoordinates(1, 1), 1);
source->SetValue(vtkArrayCoordinates(0, 2), 0);
source->SetValue(vtkArrayCoordinates(1, 2), 1);
source->SetValue(vtkArrayCoordinates(0, 3), -1);
source->SetValue(vtkArrayCoordinates(1, 3), 1);
cout << "source matrix:\n";
vtkPrintMatrixFormat(cout, source.GetPointer());
vtkSmartPointer<vtkArrayData> source_data = vtkSmartPointer<vtkArrayData>::New();
source_data->SetArray(source);
vtkSmartPointer<vtkCosineSimilarity> similarity = vtkSmartPointer<vtkCosineSimilarity>::New();
similarity->AddInputConnection(source_data->GetProducerPort());
similarity->SetVectorDimension(1);
similarity->Update();
vtkTable* const table = similarity->GetOutput();
test_expression(table->GetNumberOfColumns() == 3);
cout << "similarity table:\n";
for(vtkIdType row = 0; row < table->GetNumberOfRows(); ++row)
{
cout
<< table->GetValue(row, 0).ToInt()
<< " -> "
<< table->GetValue(row, 1).ToInt()
<< ": "
<< table->GetValue(row, 2).ToDouble()
<< "\n";
}
return 0;
}
catch(vtkstd::exception& e)
{
cerr << e.what() << endl;
return 1;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: ArrayDiagonalMatrixSource.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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 <vtkArrayData.h>
#include <vtkArrayPrint.h>
#include <vtkDenseArray.h>
#include <vtkDiagonalMatrixSource.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>
#include <vtksys/ios/iostream>
#include <vtksys/stl/stdexcept>
#define test_expression(expression) \
{ \
if(!(expression)) \
throw vtkstd::runtime_error("Expression failed: " #expression); \
}
int ArrayDiagonalMatrixSource(int argc, char* argv[])
{
try
{
vtkSmartPointer<vtkDiagonalMatrixSource> source = vtkSmartPointer<vtkDiagonalMatrixSource>::New();
source->SetExtents(3);
source->SetArrayType(vtkDiagonalMatrixSource::SPARSE);
source->SetDiagonal(1.0);
source->SetSuperDiagonal(0.5);
source->SetSubDiagonal(-0.5);
source->Update();
vtkSparseArray<double>* const sparse_array = vtkSparseArray<double>::SafeDownCast(source->GetOutput()->GetArray());
cout << "sparse diagonal matrix:\n";
vtkPrintMatrixFormat(cout, sparse_array);
test_expression(sparse_array);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(0, 0)) == 1.0);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(1, 0)) == -0.5);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(2, 0)) == 0.0);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(0, 1)) == 0.5);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(1, 1)) == 1.0);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(2, 1)) == -0.5);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(0, 2)) == 0.0);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(1, 2)) == 0.5);
test_expression(sparse_array->GetValue(vtkArrayCoordinates(2, 2)) == 1.0);
source->SetArrayType(vtkDiagonalMatrixSource::DENSE);
source->Update();
vtkDenseArray<double>* const dense_array = vtkDenseArray<double>::SafeDownCast(source->GetOutput()->GetArray());
cout << "dense diagonal matrix:\n";
vtkPrintMatrixFormat(cout, dense_array);
test_expression(dense_array);
test_expression(dense_array->GetValue(vtkArrayCoordinates(0, 0)) == 1.0);
test_expression(dense_array->GetValue(vtkArrayCoordinates(1, 0)) == -0.5);
test_expression(dense_array->GetValue(vtkArrayCoordinates(2, 0)) == 0.0);
test_expression(dense_array->GetValue(vtkArrayCoordinates(0, 1)) == 0.5);
test_expression(dense_array->GetValue(vtkArrayCoordinates(1, 1)) == 1.0);
test_expression(dense_array->GetValue(vtkArrayCoordinates(2, 1)) == -0.5);
test_expression(dense_array->GetValue(vtkArrayCoordinates(0, 2)) == 0.0);
test_expression(dense_array->GetValue(vtkArrayCoordinates(1, 2)) == 0.5);
test_expression(dense_array->GetValue(vtkArrayCoordinates(2, 2)) == 1.0);
return 0;
}
catch(vtkstd::exception& e)
{
cerr << e.what() << endl;
return 1;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: ArrayExtractFactoredArray.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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 <vtkArrayData.h>
#include <vtkArrayPrint.h>
#include <vtkExtractFactoredArray.h>
#include <vtkFactoredArrayData.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>
#include <vtksys/ios/iostream>
#include <vtksys/stl/stdexcept>
#define test_expression(expression) \
{ \
if(!(expression)) \
throw vtkstd::runtime_error("Expression failed: " #expression); \
}
int ArrayExtractFactoredArray(int argc, char* argv[])
{
try
{
vtkSmartPointer<vtkSparseArray<double> > a = vtkSmartPointer<vtkSparseArray<double> >::New();
vtkSmartPointer<vtkSparseArray<double> > b = vtkSmartPointer<vtkSparseArray<double> >::New();
vtkSmartPointer<vtkFactoredArrayData> factored = vtkSmartPointer<vtkFactoredArrayData>::New();
factored->AddArray(a);
factored->AddArray(b);
vtkSmartPointer<vtkExtractFactoredArray> extract = vtkSmartPointer<vtkExtractFactoredArray>::New();
extract->AddInputConnection(factored->GetProducerPort());
extract->SetIndex(0);
extract->Update();
test_expression(extract->GetOutput()->GetArray() == a.GetPointer());
extract->SetIndex(1);
extract->Update();
test_expression(extract->GetOutput()->GetArray() == b.GetPointer());
return 0;
}
catch(vtkstd::exception& e)
{
cout << e.what() << endl;
return 1;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: ArrayMatricizeArray.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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 <vtkArrayData.h>
#include <vtkArrayPrint.h>
#include <vtkMatricizeArray.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>
#include <vtksys/ios/iostream>
#include <vtksys/stl/stdexcept>
#define test_expression(expression) \
{ \
if(!(expression)) \
throw vtkstd::runtime_error("Expression failed: " #expression); \
}
int ArrayMatricizeArray(int argc, char* argv[])
{
try
{
// Create an array ...
vtkSmartPointer<vtkSparseArray<double> > array = vtkSmartPointer<vtkSparseArray<double> >::New();
array->Resize(vtkArrayExtents(2, 2, 2));
double value = 0;
const vtkArrayExtents extents = array->GetExtents();
for(int i = 0; i != extents[0]; ++i)
{
for(int j = 0; j != extents[1]; ++j)
{
for(int k = 0; k != extents[2]; ++k)
{
array->AddValue(vtkArrayCoordinates(i, j, k), value++);
}
}
}
cout << "array source:\n";
vtkPrintCoordinateFormat(cout, array.GetPointer());
// Create an array data object to hold it ...
vtkSmartPointer<vtkArrayData> array_data = vtkSmartPointer<vtkArrayData>::New();
array_data->SetArray(array);
// Matricize it ...
vtkSmartPointer<vtkMatricizeArray> matricize = vtkSmartPointer<vtkMatricizeArray>::New();
matricize->SetInput(array_data);
matricize->SetSliceDimension(0);
matricize->Update();
vtkSparseArray<double>* const matricized_array = vtkSparseArray<double>::SafeDownCast(matricize->GetOutput()->GetArray());
test_expression(matricized_array);
cout << "matricize output:\n";
vtkPrintCoordinateFormat(cout, matricized_array);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(0, 0)) == 0);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(0, 1)) == 1);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(0, 2)) == 2);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(0, 3)) == 3);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(1, 0)) == 4);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(1, 1)) == 5);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(1, 2)) == 6);
test_expression(matricized_array->GetValue(vtkArrayCoordinates(1, 3)) == 7);
return 0;
}
catch(vtkstd::exception& e)
{
cout << e.what() << endl;
return 1;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: ArrayMatrixColumnNorm.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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 <vtkArrayData.h>
#include <vtkArrayPrint.h>
#include <vtkMatrixColumnNorm.h>
#include <vtkDenseArray.h>
#include <vtkDiagonalMatrixSource.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>
#include <vtksys/ios/iostream>
#include <vtksys/stl/stdexcept>
#define test_expression(expression) \
{ \
if(!(expression)) \
throw vtkstd::runtime_error("Expression failed: " #expression); \
}
static const bool close_enough(const double lhs, const double rhs)
{
return fabs(lhs - rhs) < 1.0e-12;
}
int ArrayMatrixColumnNorm(int argc, char* argv[])
{
cout << setprecision(17);
try
{
vtkSmartPointer<vtkDiagonalMatrixSource> source = vtkSmartPointer<vtkDiagonalMatrixSource>::New();
source->SetExtents(3);
source->SetArrayType(vtkDiagonalMatrixSource::SPARSE);