vtkNormalizeMatrixVectors.cxx 3.93 KB
Newer Older
1 2 3 4
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkNormalizeMatrixVectors.cxx
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
-------------------------------------------------------------------------
  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 "vtkArrayCoordinates.h"
#include "vtkCommand.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNormalizeMatrixVectors.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkTypedArray.h"

31 32
#include <algorithm>

33 34 35 36 37 38
///////////////////////////////////////////////////////////////////////////////
// vtkNormalizeMatrixVectors

vtkStandardNewMacro(vtkNormalizeMatrixVectors);

vtkNormalizeMatrixVectors::vtkNormalizeMatrixVectors() :
39 40
  VectorDimension(1),
  PValue(2)
41 42 43 44 45 46 47 48 49 50 51
{
}

vtkNormalizeMatrixVectors::~vtkNormalizeMatrixVectors()
{
}

void vtkNormalizeMatrixVectors::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "VectorDimension: " << this->VectorDimension << endl;
52
  os << indent << "PValue: " << this->PValue << endl;
53 54 55
}

int vtkNormalizeMatrixVectors::RequestData(
56 57
  vtkInformation*,
  vtkInformationVector** inputVector,
58 59
  vtkInformationVector* outputVector)
{
60 61
  int vector_dimension = std::min(1, std::max(0, this->VectorDimension));
  double p_value = std::max(1.0, this->PValue);
62

63 64 65 66 67 68 69
  vtkArrayData* const input = vtkArrayData::GetData(inputVector[0]);
  if(input->GetNumberOfArrays() != 1)
    {
    vtkErrorMacro(<< "vtkNormalizeMatrixVectors requires vtkArrayData containing exactly one array as input.");
    return 0;
    }

70 71
  vtkTypedArray<double>* const input_array = vtkTypedArray<double>::SafeDownCast(
    input->GetArray(static_cast<vtkIdType>(0)));
72 73 74 75 76 77 78 79 80 81 82 83 84
  if(!input_array)
    {
    vtkErrorMacro(<< "vtkNormalizeMatrixVectors requires a vtkTypedArray<double> as input.");
    return 0;
    }
  if(input_array->GetDimensions() != 2)
    {
    vtkErrorMacro(<< "vtkNormalizeMatrixVectors requires a matrix as input.");
    return 0;
    }

  vtkTypedArray<double>* const output_array = vtkTypedArray<double>::SafeDownCast(input_array->DeepCopy());

85
  const vtkArrayRange vectors = input_array->GetExtent(vector_dimension);
86
  const vtkIdType value_count = input_array->GetNonNullSize();
87

88
  // Create temporary storage for computed vector weights ...
89
  std::vector<double> weight(vectors.GetSize(), 0.0);
90 91 92 93 94 95

  // Store the sum of the squares of each vector value ...
  vtkArrayCoordinates coordinates;
  for(vtkIdType n = 0; n != value_count; ++n)
    {
    output_array->GetCoordinatesN(n, coordinates);
96
    weight[coordinates[vector_dimension] - vectors.GetBegin()] += pow(output_array->GetValueN(n), p_value);
97 98 99
    }

  // Convert the sums into weights, avoiding divide-by-zero ...
100
  for(vtkIdType i = 0; i != vectors.GetSize(); ++i)
101
    {
102
    const double length = pow(weight[i],1.0/p_value);
103 104 105 106 107 108 109
    weight[i] = length ? 1.0 / length : 0.0;
    }

  // Apply the weights to each vector ...
  for(vtkIdType n = 0; n != value_count; ++n)
    {
    output_array->GetCoordinatesN(n, coordinates);
110
    output_array->SetValueN(n, output_array->GetValueN(n) * weight[coordinates[vector_dimension] - vectors.GetBegin()]);
111
    }
112

113 114 115 116 117
  vtkArrayData* const output = vtkArrayData::GetData(outputVector);
  output->ClearArrays();
  output->AddArray(output_array);
  output_array->Delete();

118 119 120
  return 1;
}