vtkNormalizeMatrixVectors.cxx 3.91 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
vtkNormalizeMatrixVectors::~vtkNormalizeMatrixVectors() = default;
45 46 47 48 49

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

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

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

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

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

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

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

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

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

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

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

116 117 118
  return 1;
}