vtkTensorReduceFilter.C 7.4 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkTensorReduceFilter.cxx,v $
  Language:  C++
  Date:      $Date: 2001/03/20 14:10:58 $
  Version:   $Revision: 1.1 $
  Thanks:    Hank Childs, B Division, Lawrence Livermore Nat'l Laboratory

Copyright (c) 1993-2000 Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
   of any contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

 * Modified source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#include "vtkTensorReduceFilter.h"
hrchilds's avatar
hrchilds committed
44 45 46

#include <vtkCell.h>
#include <vtkCellData.h>
47
#include <vtkDataSet.h>
bonnell's avatar
bonnell committed
48
#include <vtkDoubleArray.h>
hrchilds's avatar
hrchilds committed
49
#include <vtkFloatArray.h>
50 51
#include <vtkInformation.h>
#include <vtkInformationVector.h>
hrchilds's avatar
hrchilds committed
52 53
#include <vtkObjectFactory.h>
#include <vtkPointData.h>
54
#include <vtkPolyData.h>
bonnell's avatar
bonnell committed
55
#include <vtkVisItUtility.h>
56

hrchilds's avatar
hrchilds committed
57

58 59 60 61 62 63 64 65 66
vtkStandardNewMacro(vtkTensorReduceFilter);


vtkTensorReduceFilter::vtkTensorReduceFilter()
{
  stride = 10;
  numEls = -1;
}

67 68
void
vtkTensorReduceFilter::SetStride(int s)
69 70 71 72 73
{
  numEls = -1;
  stride = s;
}

74 75
void
vtkTensorReduceFilter::SetNumberOfElements(int n)
76 77 78 79 80
{
  stride = -1;
  numEls = n;
}

hrchilds's avatar
hrchilds committed
81
// ****************************************************************************
82
// Method: vtkTensorReduceFilter::RequestData
hrchilds's avatar
hrchilds committed
83
//
84
// Modifications:
hrchilds's avatar
hrchilds committed
85 86 87
//    Kathleen Bonnell, Tue Aug 30 11:11:56 PDT 2005 
//    Copy other Point and Cell data. 
//
bonnell's avatar
bonnell committed
88 89 90
//    Kathleen Biagas, Wed Sep 5 13:10:18 MST 2012 
//    Preserve coordinate and tensor data types.
//
91 92 93
//    Eric Brugger, Thu Jan 10 12:05:20 PST 2013
//    Modified to inherit from vtkPolyDataAlgorithm.
//
hrchilds's avatar
hrchilds committed
94 95
// ****************************************************************************

96 97 98 99 100
int
vtkTensorReduceFilter::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
101
{
102 103 104 105 106 107 108 109 110 111 112 113 114
  vtkDebugMacro(<<"Executing vtkTensorReduceFilter");

  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  //
  // Initialize some frequently used values.
  //
  vtkDataSet   *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));
115 116 117

  vtkCellData *inCd = input->GetCellData();
  vtkPointData *inPd = input->GetPointData();
hrchilds's avatar
hrchilds committed
118 119
  vtkCellData *outCd = output->GetCellData();
  vtkPointData *outPd = output->GetPointData();
120 121 122 123 124 125 126 127 128 129

  vtkDataArray *inCtensors = inCd->GetTensors();
  vtkDataArray *inPtensors = inPd->GetTensors();

  int npts = input->GetNumberOfPoints();
  int ncells = input->GetNumberOfCells();

  if (inPtensors == NULL && inCtensors == NULL)
    {
    vtkErrorMacro(<<"No tensors to reduce");
130
    return 1;
131 132
    }

bonnell's avatar
bonnell committed
133 134 135
  int inPType = (inPtensors ? inPtensors->GetDataType() : VTK_FLOAT);
  int inCType = (inCtensors ? inCtensors->GetDataType() : VTK_FLOAT);

136 137 138 139
  // Determine what the stride is.
  if (stride <= 0 && numEls <= 0)
    {
    vtkErrorMacro(<<"Invalid stride");
140
    return 1;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    }

  float actingStride = stride;
  if (actingStride <= 0)
    {
    int totalTensors = 0;
    if (inPtensors != NULL)
    {
        totalTensors += npts;
    }
    if (inCtensors != NULL)
    {
        totalTensors += ncells;
    }
    actingStride = ceil(((float) totalTensors) / ((float) numEls));
    }

bonnell's avatar
bonnell committed
158 159 160 161 162 163
  vtkPoints *outpts = vtkVisItUtility::NewPoints(input);
  vtkDataArray *outTensors;
  if (inPType == VTK_DOUBLE || inCType == VTK_DOUBLE)
      outTensors = vtkDoubleArray::New();
  else 
      outTensors = vtkFloatArray::New();
164 165 166
  outTensors->SetNumberOfComponents(9);

  float nextToTake = 0.;
hrchilds's avatar
hrchilds committed
167
  int count = 0;
168 169
  if (inPtensors != NULL)
    {
hrchilds's avatar
hrchilds committed
170
    outPd->CopyAllocate(inPd, npts);
hrchilds's avatar
hrchilds committed
171
    outTensors->SetName(inPtensors->GetName());
172 173 174 175 176 177
    for (int i = 0 ; i < npts ; i++)
      {
      if (i >= nextToTake)
        {
        nextToTake += actingStride;

hrchilds's avatar
hrchilds committed
178
        double pt[3];
179 180 181
        input->GetPoint(i, pt);
        outpts->InsertNextPoint(pt);

hrchilds's avatar
hrchilds committed
182
        double v[9];
183 184
        inPtensors->GetTuple(i, v);
        outTensors->InsertNextTuple(v);
hrchilds's avatar
hrchilds committed
185
        outPd->CopyData(inPd, i, count++);
186 187
        }
      }
hrchilds's avatar
hrchilds committed
188
      outPd->Squeeze();
189 190 191
    }

  nextToTake = 0.;
hrchilds's avatar
hrchilds committed
192
  count = 0;
193 194
  if (inCtensors != NULL)
    {
hrchilds's avatar
hrchilds committed
195
    outCd->CopyAllocate(inCd, ncells);
hrchilds's avatar
hrchilds committed
196
    outTensors->SetName(inCtensors->GetName());
197 198 199 200 201 202 203
    for (int i = 0 ; i < ncells ; i++)
      {
      if (i >= nextToTake)
        {
        nextToTake += actingStride;

        vtkCell *cell = input->GetCell(i);
hrchilds's avatar
hrchilds committed
204
        double pt[3];
205 206 207
        cell->GetParametricCenter(pt);
        outpts->InsertNextPoint(pt);

hrchilds's avatar
hrchilds committed
208
        double v[9];
209 210
        inCtensors->GetTuple(i, v);
        outTensors->InsertNextTuple(v);
hrchilds's avatar
hrchilds committed
211
        outCd->CopyData(inCd, i, count++);
212 213
        }
      }
hrchilds's avatar
hrchilds committed
214
      outCd->Squeeze();
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    }

  int nOutPts = outpts->GetNumberOfPoints();
  output->SetPoints(outpts);
  outpts->Delete();
  output->GetPointData()->SetTensors(outTensors);
  outTensors->Delete();

  output->Allocate(nOutPts);
  vtkIdType onevertex[1];
  for (int i = 0 ; i < nOutPts ; i++)
    {
    onevertex[0] = i;
    output->InsertNextCell(VTK_VERTEX, 1, onevertex);
    }
230
  return 1;
231 232
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
// ****************************************************************************
//  Method: vtkTensorReduceFilter::FillInputPortInformation
//
// ****************************************************************************

int
vtkTensorReduceFilter::FillInputPortInformation(int, vtkInformation *info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
  return 1;
}

// ****************************************************************************
//  Method: vtkTensorReduceFilter::PrintSelf
//
// ****************************************************************************

void
vtkTensorReduceFilter::PrintSelf(ostream &os, vtkIndent indent)
252 253 254 255 256
{
   this->Superclass::PrintSelf(os, indent);
   os << indent << "Stride: " << this->stride << "\n";
   os << indent << "Target number of tensors: " << this->numEls << "\n";
}