1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include "vtkKMeansDistanceFunctor.h"
#include "vtkObjectFactory.h"
#include "vtkDoubleArray.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"
#include "vtkIntArray.h"
#include "vtkIdTypeArray.h"
vtkStandardNewMacro(vtkKMeansDistanceFunctor);
// ----------------------------------------------------------------------
vtkKMeansDistanceFunctor::vtkKMeansDistanceFunctor()
{
this->EmptyTuple = vtkVariantArray::New();
}
// ----------------------------------------------------------------------
vtkKMeansDistanceFunctor::~vtkKMeansDistanceFunctor()
{
this->EmptyTuple->Delete();
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "EmptyTuple: " << this->EmptyTuple << "\n";
}
// ----------------------------------------------------------------------
vtkVariantArray* vtkKMeansDistanceFunctor::GetEmptyTuple( vtkIdType dimension )
{
if ( this->EmptyTuple->GetNumberOfValues() != dimension )
{
this->EmptyTuple->SetNumberOfValues( dimension );
for( vtkIdType i = 0; i < dimension; ++ i )
{
this->EmptyTuple->SetValue( i, 0.0 );
}
}
return this->EmptyTuple;
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::operator() (
double& distance, vtkVariantArray* clusterCoord, vtkVariantArray* dataCoord )
{
distance = 0.0;
if(clusterCoord->GetNumberOfValues() != dataCoord->GetNumberOfValues() )
{
cout << "The dimensions of the cluster and data do not match." << endl;
distance = -1;
}
for(vtkIdType i = 0; i < clusterCoord->GetNumberOfValues(); i++ )
{
distance += ( clusterCoord->GetValue( i ).ToDouble() - dataCoord->GetValue( i ).ToDouble() ) *
( clusterCoord->GetValue( i ).ToDouble() - dataCoord->GetValue( i ).ToDouble() );
}
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::PairwiseUpdate(
vtkTable* clusterCoords, vtkIdType rowIndex, vtkVariantArray* dataCoord,
vtkIdType dataCoordCardinality, vtkIdType totalCardinality )
{
if ( clusterCoords->GetNumberOfColumns() != dataCoord->GetNumberOfValues() )
{
cout << "The dimensions of the cluster and/or data do not match." << endl;
return;
}
if ( totalCardinality > 0 )
{
for ( vtkIdType i = 0; i < clusterCoords->GetNumberOfColumns(); ++ i )
{
double curCoord = clusterCoords->GetValue( rowIndex, i ).ToDouble();
clusterCoords->SetValue( rowIndex, i,
curCoord + static_cast<double>(dataCoordCardinality)*
(dataCoord->GetValue( i ).ToDouble() - curCoord)/
static_cast<double>( totalCardinality ) );
}
}
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::PerturbElement(
vtkTable* newClusterElements, vtkTable* curClusterElements,
vtkIdType changeID, vtkIdType startRunID, vtkIdType endRunID,
double alpha )
{
double numInRange = static_cast<double>( endRunID-startRunID );
vtkIdType dimension = newClusterElements->GetNumberOfColumns();
std::vector<double> perturbedValues( dimension );
for( vtkIdType i = startRunID; i < endRunID; i++)
{
for( vtkIdType j = 0; j < dimension; j++ )
{
if( i == changeID )
{
perturbedValues[j] = alpha*curClusterElements->GetValue( i, j ).ToDouble();
}
else
{
if ( numInRange > 1.0)
{
perturbedValues[j] = ( 1.0-alpha )/( numInRange-1.0 )*
curClusterElements->GetValue( i, j ).ToDouble();
}
else
{
perturbedValues[j] = ( 1.0-alpha )/( numInRange )*
curClusterElements->GetValue( i, j ).ToDouble();
}
}
}
}
}
// ----------------------------------------------------------------------
void* vtkKMeansDistanceFunctor::AllocateElementArray( vtkIdType size )
{
return new double[size];
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::DeallocateElementArray( void* array )
{
delete [] static_cast< double* >( array );
}
// ----------------------------------------------------------------------
vtkAbstractArray* vtkKMeansDistanceFunctor::CreateCoordinateArray( )
{
return vtkDoubleArray::New();
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::PackElements( vtkTable* curTable, void* vElements )
{
vtkIdType numCols = curTable->GetNumberOfColumns();
vtkIdType numRows = curTable->GetNumberOfRows();
double* localElements = static_cast< double* >( vElements );
for( vtkIdType col = 0; col < numCols; col++ )
{
vtkDoubleArray* doubleArr = vtkArrayDownCast<vtkDoubleArray>( curTable->GetColumn( col ) );
memcpy( &(localElements[col*numRows]), doubleArr->GetPointer( 0 ), numRows*sizeof( double ) );
}
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::UnPackElements( vtkTable* curTable, void* vLocalElements, vtkIdType numRows, vtkIdType numCols )
{
double *localElements = static_cast< double* >( vLocalElements );
for ( vtkIdType i = 0; i < numRows; i++ )
{
vtkVariantArray *curRow = vtkVariantArray::New();
for ( int j = 0; j < numCols; j++ )
{
curRow->InsertNextValue( localElements[ j*numRows + i ] );
}
curTable->InsertNextRow( curRow );
curRow->Delete();
}
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctor::UnPackElements( vtkTable* curTable, vtkTable* newTable, void* vLocalElements, void* vGlobalElements, int np )
{
double *globalElements = static_cast< double* >( vGlobalElements );
double *localElements = static_cast< double* >( vLocalElements );
vtkIdType numCols = curTable->GetNumberOfColumns();
vtkIdType numRows = curTable->GetNumberOfRows();
vtkIdType numElements = numCols*numRows;
for( vtkIdType col = 0; col < numCols; col++ )
{
vtkDoubleArray *doubleArr = vtkDoubleArray::New();
doubleArr->SetName( curTable->GetColumnName( col ) );
doubleArr->SetNumberOfComponents( 1 );
doubleArr->SetNumberOfTuples( numRows*np );
for( int j = 0; j < np; j++ )
{
double *ptr = doubleArr->GetPointer(j*numRows);
memcpy(ptr, &(globalElements[j*numElements+col*numRows]), numRows*sizeof( double ) );
}
newTable->AddColumn(doubleArr);
doubleArr->Delete();
}
delete [] localElements;
delete [] globalElements;
}
// ----------------------------------------------------------------------
int vtkKMeansDistanceFunctor::GetDataType()
{
return VTK_DOUBLE;
}
|