------------------------------------------------------------------------------
| 206 | |
| 207 | //------------------------------------------------------------------------------ |
| 208 | void vtkReduceTable::ReduceValuesToMedian( |
| 209 | vtkTable* input, vtkTable* output, vtkIdType row, vtkIdType col, std::vector<vtkIdType> oldRows) |
| 210 | { |
| 211 | if (!input->GetValue(0, col).IsNumeric()) |
| 212 | { |
| 213 | vtkErrorMacro(<< "Median is unsupported for non-numerical data"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | // generate a vector of values |
| 218 | std::vector<double> values; |
| 219 | for (std::vector<vtkIdType>::iterator itr = oldRows.begin(); itr != oldRows.end(); ++itr) |
| 220 | { |
| 221 | values.push_back(input->GetValue(*itr, col).ToDouble()); |
| 222 | } |
| 223 | |
| 224 | // sort it |
| 225 | std::sort(values.begin(), values.end()); |
| 226 | |
| 227 | // get the median and store it in the output table |
| 228 | if (values.size() % 2 == 1) |
| 229 | { |
| 230 | output->SetValue(row, col, vtkVariant(values.at((values.size() - 1) / 2))); |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | double d1 = values.at((values.size() - 1) / 2); |
| 235 | double d2 = values.at(values.size() / 2); |
| 236 | output->SetValue(row, col, vtkVariant((d1 + d2) / 2.0)); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | //------------------------------------------------------------------------------ |
| 241 | void vtkReduceTable::ReduceValuesToMode( |