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
|
#include "vtkKMeansDistanceFunctorCalculator.h"
#include "vtkObjectFactory.h"
#include "vtkDoubleArray.h"
#include "vtkFunctionParser.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"
#include "vtkIntArray.h"
#include "vtkIdTypeArray.h"
#include <sstream>
vtkStandardNewMacro(vtkKMeansDistanceFunctorCalculator);
vtkCxxSetObjectMacro(vtkKMeansDistanceFunctorCalculator,FunctionParser,vtkFunctionParser);
// ----------------------------------------------------------------------
vtkKMeansDistanceFunctorCalculator::vtkKMeansDistanceFunctorCalculator()
{
this->FunctionParser = vtkFunctionParser::New();
this->DistanceExpression = 0;
this->TupleSize = -1;
}
// ----------------------------------------------------------------------
vtkKMeansDistanceFunctorCalculator::~vtkKMeansDistanceFunctorCalculator()
{
this->SetFunctionParser( 0 );
this->SetDistanceExpression( 0 );
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctorCalculator::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "FunctionParser: " << this->FunctionParser << "\n";
os << indent << "DistanceExpression: "
<< ( this->DistanceExpression && this->DistanceExpression[0] ? this->DistanceExpression : "NULL" )
<< "\n";
os << indent << "TupleSize: " << this->TupleSize << "\n";
}
// ----------------------------------------------------------------------
void vtkKMeansDistanceFunctorCalculator::operator() (
double& distance, vtkVariantArray* clusterCoord, vtkVariantArray* dataCoord )
{
distance = 0.0;
vtkIdType nv = clusterCoord->GetNumberOfValues();
if ( nv != dataCoord->GetNumberOfValues() )
{
cout << "The dimensions of the cluster and data do not match." << endl;
distance = -1;
return;
}
if ( ! this->DistanceExpression )
{
distance = -1;
return;
}
this->FunctionParser->SetFunction( this->DistanceExpression );
if ( this->TupleSize != nv )
{ // Need to update the scalar variable names as well as values...
this->FunctionParser->RemoveScalarVariables();
for ( vtkIdType i = 0; i < nv; ++ i )
{
std::ostringstream xos;
std::ostringstream yos;
xos << "x" << i;
yos << "y" << i;
this->FunctionParser->SetScalarVariableValue( xos.str().c_str(), clusterCoord->GetValue( i ).ToDouble() );
this->FunctionParser->SetScalarVariableValue( yos.str().c_str(), dataCoord->GetValue( i ).ToDouble() );
}
}
else
{ // Use faster integer comparisons to set values...
for ( vtkIdType i = 0; i < nv; ++ i )
{
this->FunctionParser->SetScalarVariableValue( 2 * i, clusterCoord->GetValue( i ).ToDouble() );
this->FunctionParser->SetScalarVariableValue( 2 * i + 1, dataCoord->GetValue( i ).ToDouble() );
}
}
distance = this->FunctionParser->GetScalarResult();
/*
cout << "f([";
for ( vtkIdType i = 0; i < nv; ++ i )
cout << " " << dataCoord->GetValue( i ).ToDouble();
cout << " ],[";
for ( vtkIdType i = 0; i < nv; ++ i )
cout << " " << clusterCoord->GetValue( i ).ToDouble();
cout << " ]) = " << distance << "\n";
*/
}
|