vtkVisItCellDataToPointData.C 9.29 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkVisItCellDataToPointData.cxx,v $
  Language:  C++
  Date:      $Date: 2002/12/17 02:05:38 $
  Version:   $Revision: 1.24 $

  Copyright (c) 1993-2002 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 "vtkVisItCellDataToPointData.h"

bonnell's avatar
bonnell committed
20
#include "vtkAccessors.h"
hrchilds's avatar
hrchilds committed
21 22 23
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkIdList.h"
24 25
#include "vtkInformation.h"
#include "vtkInformationVector.h"
hrchilds's avatar
hrchilds committed
26 27 28 29 30 31 32 33 34 35
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkRectilinearGrid.h"
#include "vtkStructuredGrid.h"

vtkStandardNewMacro(vtkVisItCellDataToPointData);

// Instantiate object so that cell data is not passed to output.
vtkVisItCellDataToPointData::vtkVisItCellDataToPointData()
{
bonnell's avatar
bonnell committed
36
  this->PassCellData = false;
hrchilds's avatar
hrchilds committed
37 38 39 40
}

#define VTK_MAX_CELLS_PER_POINT 4096

hrchilds's avatar
hrchilds committed
41 42 43 44 45
// **************************************************************************** 
//  Modifications:
//    Hank Childs, Wed Mar  9 16:23:01 PST 2005
//    Fix minor UMR.
//
46 47 48 49
//    Dave Pugmire, Wed Jul  30 16:57:23 EST 2008
//    Avoid using fastTrack code for non-scalar data. The fastTrack code does
//    not handle it right.
//
bonnell's avatar
bonnell committed
50 51 52
//    Kathleen Biagas, Thu Sep 6 11:05:01 MST 2012
//    Added templatized helper method to handle double-precision.
//
hrchilds's avatar
hrchilds committed
53
// **************************************************************************** 
54

bonnell's avatar
bonnell committed
55 56
template <class Accessor> inline void
vtkVisItCellDataToPointData_Copy(int ptId, double weight, int *ids, int nids,
57
    Accessor var_out, Accessor var_in)
bonnell's avatar
bonnell committed
58 59 60 61 62 63 64 65
{
    double val = 0.;
    for (int m = 0 ; m < nids ; ++m)
    {
        val += weight * var_in.GetComponent(ids[m]);
    }
    var_out.SetComponent(ptId, val);
}
hrchilds's avatar
hrchilds committed
66

67 68 69 70 71 72 73 74 75 76 77
// **************************************************************************** 
//  Modifications:
//    Eric Brugger, Wed Jan  9 14:48:17 PST 2013
//    Modified to inherit from vtkDataSetAlgorithm.
//
// **************************************************************************** 

int vtkVisItCellDataToPointData::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
hrchilds's avatar
hrchilds committed
78
{
79 80 81 82 83 84 85 86 87 88 89 90
  // 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()));
  vtkDataSet *output = vtkDataSet::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

91
  vtkIdType cellId, ptId, i, j, k, l;
hrchilds's avatar
hrchilds committed
92 93 94 95
  vtkIdType numCells, numPts;
  vtkCellData *inPD=input->GetCellData();
  vtkPointData *outPD=output->GetPointData();
  vtkIdList *cellIds;
hrchilds's avatar
hrchilds committed
96 97
  double weight;
  double *weights;
hrchilds's avatar
hrchilds committed
98 99 100 101 102 103 104 105 106 107 108 109 110

  vtkDebugMacro(<<"Mapping cell data to point data");

  // First, copy the input to the output as a starting point
  output->CopyStructure( input );

  cellIds = vtkIdList::New();
  cellIds->Allocate(VTK_MAX_CELLS_PER_POINT);

  if ( (numPts=input->GetNumberOfPoints()) < 1 )
    {
    vtkErrorMacro(<<"No input point data!");
    cellIds->Delete();
111
    return 1;
hrchilds's avatar
hrchilds committed
112
    }
hrchilds's avatar
hrchilds committed
113
  weights = new double[VTK_MAX_CELLS_PER_POINT];
hrchilds's avatar
hrchilds committed
114 115 116 117 118 119 120 121 122 123 124
  
  // Pass the point data first. The fields and attributes
  // which also exist in the cell data of the input will
  // be over-written during CopyAllocate
  output->GetPointData()->PassData(input->GetPointData());

  // notice that inPD and outPD are vtkCellData and vtkPointData; respectively.
  // It's weird, but it works.
  outPD->CopyAllocate(inPD,numPts);

  bool canFastTrackStructured = false;
hrchilds's avatar
hrchilds committed
125
  int dims[3] = { 0, 0, 0 };
hrchilds's avatar
hrchilds committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  if (input->GetDataObjectType() == VTK_RECTILINEAR_GRID)
  {
    ((vtkRectilinearGrid *) input)->GetDimensions(dims);
    canFastTrackStructured = true;
  }
  else if (input->GetDataObjectType() == VTK_STRUCTURED_GRID)
  {
    ((vtkStructuredGrid *) input)->GetDimensions(dims);
    canFastTrackStructured = true;
  }

  // Let's not worry about degenerate cases -- or even 2D.
  if (dims[0] <= 1 || dims[1] <= 1 || dims[2] <= 1)
  {
    canFastTrackStructured = false;
  }

bonnell's avatar
bonnell committed
143
  // Only handle floating point/double and make sure our assumptions about
hrchilds's avatar
hrchilds committed
144 145 146 147 148 149 150 151 152 153 154
  // the variable's names are valid.
  int nvals = inPD->GetNumberOfArrays();
  for (i = 0 ; i < nvals ; i++)
  {
    if (inPD->GetArray(i)->GetName() == NULL)
      canFastTrackStructured = false;
    else if (strcmp(inPD->GetArray(i)->GetName(), "") == 0)
      canFastTrackStructured = false;

    if (outPD->GetArray(inPD->GetArray(i)->GetName()) == NULL)
      canFastTrackStructured = false;
bonnell's avatar
bonnell committed
155 156
    if (!(inPD->GetArray(i)->GetDataType() == VTK_FLOAT ||
          inPD->GetArray(i)->GetDataType() == VTK_DOUBLE))
hrchilds's avatar
hrchilds committed
157
      canFastTrackStructured = false;
158 159 160 161

    // fastTrack code doesn't support non-scalar data, so for now, avoid it.
    if (inPD->GetArray(i)->GetNumberOfComponents() != 1 )
        canFastTrackStructured = false;
hrchilds's avatar
hrchilds committed
162 163 164 165 166 167 168 169 170
  }
       
  int abort=0;
  vtkIdType progressInterval=numPts/20 + 1;
  if (canFastTrackStructured)
    {
    const int iOffset = 1;
    const int jOffset = (dims[0]-1);
    const int kOffset = (dims[0]-1)*(dims[1]-1);
hrchilds's avatar
hrchilds committed
171
    double weights[4] = { 1., 0.5, 0.25, 0.125 };
hrchilds's avatar
hrchilds committed
172 173 174 175
    int nids_array[4] = { 1, 2, 4, 8 };
    int ids[8];

    const int maxArrays = 128;
bonnell's avatar
bonnell committed
176 177
    vtkDataArray *vars_in[maxArrays];
    vtkDataArray *vars_out[maxArrays];
hrchilds's avatar
hrchilds committed
178 179 180 181 182 183
    for (i = 0 ; i < nvals ; i++)
    {
       // This should be automatic, but some arrays do not seem to correctly
       // know how many tuples they have.
       outPD->GetArray(inPD->GetArray(i)->GetName())
                              ->SetNumberOfTuples(output->GetNumberOfPoints());
bonnell's avatar
bonnell committed
184 185
       vars_in[i] = inPD->GetArray(i);
       vars_out[i] = outPD->GetArray(inPD->GetArray(i)->GetName());
hrchilds's avatar
hrchilds committed
186
    }
187 188 189 190 191
    for (i = nvals ; i < maxArrays ; i++)
    {
        vars_in[i] = NULL;
        vars_out[i] = NULL;
    }
hrchilds's avatar
hrchilds committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    for (k = 0 ; k < dims[2] ; k++)
    {
      for (j = 0 ; j < dims[1] ; j++)
      {
        for (i = 0 ; i < dims[0] ; i++)
        {
          int numBad = 0;
          if (i == 0 || i == dims[0]-1)
            numBad++;
          if (j == 0 || j == dims[1]-1)
            numBad++;
          if (k == 0 || k == dims[2]-1)
            numBad++;

hrchilds's avatar
hrchilds committed
207
          double weight = weights[3-numBad];
hrchilds's avatar
hrchilds committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
          int nids = nids_array[3-numBad];

          int id = 0;
          if (i > 0 && j > 0 && k > 0)
             ids[id++] = (k-1)*kOffset+(j-1)*jOffset+(i-1)*iOffset;
          if (i < (dims[0]-1) && j > 0 && k > 0)
             ids[id++] = (k-1)*kOffset+(j-1)*jOffset+i*iOffset;
          if (i > 0 && j < (dims[1]-1) && k > 0)
             ids[id++] = (k-1)*kOffset+j*jOffset+(i-1)*iOffset;
          if (i < (dims[0]-1) && j < (dims[1]-1) && k > 0)
             ids[id++] = (k-1)*kOffset+j*jOffset+i*iOffset;
          if (i > 0 && j > 0 && k < (dims[2]-1))
             ids[id++] = k*kOffset+(j-1)*jOffset+(i-1)*iOffset;
          if (i < (dims[0]-1) && j > 0 && k < (dims[2]-1))
             ids[id++] = k*kOffset+(j-1)*jOffset+i*iOffset;
          if (i > 0 && j < (dims[1]-1) && k < (dims[2]-1))
             ids[id++] = k*kOffset+j*jOffset+(i-1)*iOffset;
          if (i < (dims[0]-1) && j < (dims[1]-1) && k < (dims[2]-1))
             ids[id++] = k*kOffset+j*jOffset+i*iOffset;
          
          int ptId = k*dims[0]*dims[1] + j*dims[0] + i;
          for (l = 0 ; l < nvals ; l++)
          {
bonnell's avatar
bonnell committed
231 232 233 234 235 236 237 238 239 240 241 242 243
              // only floats/doubles take the fast-path.
              if (vars_in[l]->GetDataType() == VTK_FLOAT)
              {
                  vtkVisItCellDataToPointData_Copy(ptId, weight, ids, nids,
                      vtkDirectAccessor<float>(vars_out[l]), 
                      vtkDirectAccessor<float>(vars_in[l]));
              }
              else if (vars_in[l]->GetDataType() == VTK_DOUBLE)
              {
                  vtkVisItCellDataToPointData_Copy(ptId, weight, ids, nids,
                      vtkDirectAccessor<double>(vars_out[l]), 
                      vtkDirectAccessor<double>(vars_in[l]));
              }
hrchilds's avatar
hrchilds committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
          }
        }
      }
    } // End for

    } // End if
  else
    {
    for (ptId=0; ptId < numPts && !abort; ptId++)
      {
      if ( !(ptId % progressInterval) )
        {
        this->UpdateProgress((float)ptId/numPts);
        abort = GetAbortExecute();
        }
  
      input->GetPointCells(ptId, cellIds);
      numCells = cellIds->GetNumberOfIds();
      if ( numCells > 0 )
        {
        weight = 1.0 / numCells;
        for (cellId=0; cellId < numCells; cellId++)
          {
          weights[cellId] = weight;
          }
        outPD->InterpolatePoint(inPD, ptId, cellIds, weights);
        }
      else
        {
        outPD->NullPoint(ptId);
        }
      }
    }

  if ( this->PassCellData )
    {
    output->GetCellData()->PassData(input->GetCellData());
    }

  cellIds->Delete();
  delete [] weights;
285 286

  return 1;
hrchilds's avatar
hrchilds committed
287 288 289 290 291 292 293 294
}

void vtkVisItCellDataToPointData::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "Pass Cell Data: " << (this->PassCellData ? "On\n" : "Off\n");
}