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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSplitColumnComponents.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 "vtkSplitColumnComponents.h"
#include "vtkAbstractArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include <sstream>
#include "math.h"
vtkStandardNewMacro(vtkSplitColumnComponents);
//---------------------------------------------------------------------------
vtkSplitColumnComponents::vtkSplitColumnComponents()
{
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
this->CalculateMagnitudes = true;
this->NamingMode = NUMBERS_WITH_PARENS;
}
//---------------------------------------------------------------------------
vtkSplitColumnComponents::~vtkSplitColumnComponents()
{
}
//---------------------------------------------------------------------------
// Templated function in an anonymous namespace to copy the data from the
// specified component in one column to a single component column
namespace {
template<typename T>
void CopyArrayData(T* source, T* destination, int components, int c,
unsigned int length)
{
for (unsigned int i = 0; i < length; ++i)
{
destination[i] = source[i*components + c];
}
}
template<typename T>
void CalculateMagnitude(T* source, T* destination, int components,
unsigned int length)
{
for (unsigned int i = 0; i < length; ++i)
{
double tmp = 0.0;
for (int j = 0; j < components; ++j)
{
tmp += static_cast<double>(source[i*components + j]
* source[i*components + j]);
}
destination[i] = static_cast<T>(sqrt(tmp));
}
}
} // End of anonymous namespace
//---------------------------------------------------------------------------
int vtkSplitColumnComponents::RequestData(
vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// Get input tables
vtkInformation* table1Info = inputVector[0]->GetInformationObject(0);
vtkTable* table = vtkTable::SafeDownCast(
table1Info->Get(vtkDataObject::DATA_OBJECT()));
// Get output table
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkTable* output = vtkTable::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Add columns from table, split multiple component columns as necessary
for (int i = 0; i < table->GetNumberOfColumns(); ++i)
{
vtkAbstractArray* col = table->GetColumn(i);
int components = col->GetNumberOfComponents();
if (components == 1)
{
output->AddColumn(col);
}
else if (components > 1)
{
// Split the multicomponent column up into individual columns
int colSize = col->GetNumberOfTuples();
for (int j = 0; j < components; ++j)
{
vtkStdString component_label = this->GetComponentLabel(col, j);
vtkAbstractArray* newCol = vtkAbstractArray::CreateArray(col->GetDataType());
newCol->SetName(component_label.c_str());
newCol->SetNumberOfTuples(colSize);
// Now copy the components into their new columns
switch(col->GetDataType())
{
vtkExtraExtendedTemplateMacro(
CopyArrayData(static_cast<VTK_TT*>(col->GetVoidPointer(0)),
static_cast<VTK_TT*>(newCol->GetVoidPointer(0)),
components, j, colSize));
}
output->AddColumn(newCol);
newCol->Delete();
}
// Add a magnitude column and calculate values if requested
if (this->CalculateMagnitudes && col->IsA("vtkDataArray"))
{
vtkStdString component_label = this->GetComponentLabel(
col, -1 /* for magnitude */);
vtkAbstractArray* newCol = vtkAbstractArray::CreateArray(col->GetDataType());
newCol->SetName(component_label.c_str());
newCol->SetNumberOfTuples(colSize);
// Now calculate the magnitude column
switch(col->GetDataType())
{
vtkTemplateMacro(
CalculateMagnitude(static_cast<VTK_TT*>(col->GetVoidPointer(0)),
static_cast<VTK_TT*>(newCol->GetVoidPointer(0)),
components, colSize));
}
output->AddColumn(newCol);
newCol->Delete();
}
}
}
return 1;
}
namespace
{
//----------------------------------------------------------------------------
std::string vtkDefaultComponentName(int componentNumber, int componentCount)
{
if (componentCount <= 1)
{
return "";
}
else if (componentNumber == -1)
{
return "Magnitude";
}
else if (componentCount <= 3 && componentNumber < 3)
{
const char* titles[] = {"X", "Y", "Z"};
return titles[componentNumber];
}
else if (componentCount == 6)
{
const char* titles[] = {"XX", "YY", "ZZ", "XY", "YZ", "XZ"};
// Assume this is a symmetric matrix.
return titles[componentNumber];
}
else
{
std::ostringstream buffer;
buffer << componentNumber;
return buffer.str();
}
}
std::string vtkGetComponentName(vtkAbstractArray* array, int component_no)
{
const char* name = array->GetComponentName(component_no);
if (name)
{
return name;
}
return vtkDefaultComponentName(component_no, array->GetNumberOfComponents());
}
};
//---------------------------------------------------------------------------
vtkStdString vtkSplitColumnComponents::GetComponentLabel(
vtkAbstractArray* array, int component_no)
{
std::ostringstream stream;
switch (this->NamingMode)
{
case NUMBERS_WITH_PARENS:
stream << array->GetName() << " (";
if (component_no == -1)
{
stream << "Magnitude)";
}
else
{
stream << component_no << ")";
}
break;
case NUMBERS_WITH_UNDERSCORES:
stream << array->GetName() << "_";
if (component_no == -1)
{
stream << "Magnitude";
}
else
{
stream << component_no;
}
break;
case NAMES_WITH_PARENS:
stream << array->GetName() << " ("
<< vtkGetComponentName(array, component_no).c_str() << ")";
break;
case NAMES_WITH_UNDERSCORES:
default:
stream << array->GetName() << "_"
<< vtkGetComponentName(array, component_no).c_str();;
break;
}
return stream.str();
}
//---------------------------------------------------------------------------
void vtkSplitColumnComponents::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "CalculateMagnitudes: " << this->CalculateMagnitudes << endl;
os << indent << "NamingMode: ";
switch(this->NamingMode)
{
case NAMES_WITH_UNDERSCORES:
os << "NAMES_WITH_UNDERSCORES" << endl;
break;
case NAMES_WITH_PARENS:
os << "NAMES_WITH_PARENS" << endl;
break;
case NUMBERS_WITH_UNDERSCORES:
os << "NUMBERS_WITH_UNDERSCORES" << endl;
break;
case NUMBERS_WITH_PARENS:
os << "NUMBERS_WITH_PARENS" << endl;
break;
default:
os << "INVALID" << endl;
}
}
|