vtkCellCenters.cxx 4.03 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkCellCenters.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
13 14 15

=========================================================================*/
#include "vtkCellCenters.h"
16 17

#include "vtkCell.h"
Mathieu Westphal's avatar
Mathieu Westphal committed
18
#include "vtkCellArray.h"
19 20
#include "vtkCellData.h"
#include "vtkDataSet.h"
21 22
#include "vtkInformation.h"
#include "vtkInformationVector.h"
23
#include "vtkObjectFactory.h"
24 25 26
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
27

Brad King's avatar
Brad King committed
28
vtkStandardNewMacro(vtkCellCenters);
29

30
//----------------------------------------------------------------------------
31
// Generate points
Mathieu Westphal's avatar
Mathieu Westphal committed
32 33
int vtkCellCenters::RequestData(vtkInformation* vtkNotUsed(request),
  vtkInformationVector** inputVector, vtkInformationVector* outputVector)
34
{
35
  // get the input and output
Mathieu Westphal's avatar
Mathieu Westphal committed
36 37 38 39 40 41 42 43 44
  vtkDataSet* input = vtkDataSet::GetData(inputVector[0]);
  vtkPolyData* output = vtkPolyData::GetData(outputVector);

  vtkCellData* inCD = input->GetCellData();
  vtkPointData* outPD = output->GetPointData();
  vtkCellData* outCD = output->GetCellData();
  vtkIdType numCells = input->GetNumberOfCells();

  if (numCells == 0)
45
  {
Mathieu Westphal's avatar
Mathieu Westphal committed
46
    vtkDebugMacro(<< "No cells to generate center points for");
47
    return 1;
48
  }
49

Mathieu Westphal's avatar
Mathieu Westphal committed
50
  vtkNew<vtkPoints> newPts;
51 52
  newPts->SetNumberOfPoints(numCells);

Mathieu Westphal's avatar
Mathieu Westphal committed
53 54 55 56 57 58 59 60 61 62 63 64 65
  vtkNew<vtkIdList> pointIdList;
  pointIdList->SetNumberOfIds(numCells);

  vtkNew<vtkIdList> cellIdList;
  cellIdList->SetNumberOfIds(numCells);

  vtkIdType pointId = 0;
  vtkIdType numPoints = numCells;
  std::vector<double> weights(input->GetMaxCellSize());
  bool hasEmptyCells = false;
  vtkTypeBool abort = 0;
  vtkIdType progressInterval = numCells / 10 + 1;
  for (vtkIdType cellId = 0; cellId < numCells && !abort; cellId++)
66
  {
Mathieu Westphal's avatar
Mathieu Westphal committed
67
    if (!(cellId % progressInterval))
68
    {
Mathieu Westphal's avatar
Mathieu Westphal committed
69 70
      vtkDebugMacro(<< "Processing #" << cellId);
      this->UpdateProgress(0.9 * cellId / numCells);
71
      abort = this->GetAbortExecute();
72
    }
73

Mathieu Westphal's avatar
Mathieu Westphal committed
74
    vtkCell* cell = input->GetCell(cellId);
75
    if (cell->GetCellType() != VTK_EMPTY_CELL)
76
    {
Mathieu Westphal's avatar
Mathieu Westphal committed
77 78 79 80 81 82 83
      double x[3], pcoords[3];
      int subId = cell->GetParametricCenter(pcoords);
      cell->EvaluateLocation(subId, pcoords, x, weights.data());
      newPts->SetPoint(pointId, x);
      pointIdList->SetId(pointId, pointId);
      cellIdList->SetId(pointId, cellId);
      pointId++;
84
    }
85
    else
86
    {
Mathieu Westphal's avatar
Mathieu Westphal committed
87 88
      hasEmptyCells = true;
      numPoints--;
89
    }
90
  }
Mathieu Westphal's avatar
Mathieu Westphal committed
91
  if(abort)
92
  {
Mathieu Westphal's avatar
Mathieu Westphal committed
93
    return 0;
94
  }
95

Mathieu Westphal's avatar
Mathieu Westphal committed
96 97 98
  newPts->Resize(numPoints);
  pointIdList->Resize(numPoints);
  cellIdList->Resize(numPoints);
99 100
  output->SetPoints(newPts);

Mathieu Westphal's avatar
Mathieu Westphal committed
101 102 103 104 105 106
  if (hasEmptyCells)
  {
    outPD->CopyAllocate(inCD, numPoints);
    outPD->CopyData(inCD, cellIdList, pointIdList);
  }
  else
107
  {
Mathieu Westphal's avatar
Mathieu Westphal committed
108
    outPD->PassData(inCD); // because number of points == number of cells
109
  }
110

Mathieu Westphal's avatar
Mathieu Westphal committed
111 112 113 114 115
  if (this->VertexCells)
  {
    vtkNew<vtkIdTypeArray> iArray;
    iArray->SetNumberOfComponents(1);
    iArray->SetNumberOfTuples(numPoints * 2);
116 117 118 119 120
    for (vtkIdType i = 0; i < numPoints; i++)
    {
      iArray->SetValue(2 * i, 1);
      iArray->SetValue(2 * i + 1, i);
    }
Mathieu Westphal's avatar
Mathieu Westphal committed
121 122 123 124 125 126 127 128 129

    vtkNew<vtkCellArray> verts;
    verts->SetCells(numPoints, iArray);
    output->SetVerts(verts);
    outCD->ShallowCopy(outPD);
  }

  output->Squeeze();
  this->UpdateProgress(1.0);
130 131 132
  return 1;
}

133
//----------------------------------------------------------------------------
Mathieu Westphal's avatar
Mathieu Westphal committed
134
int vtkCellCenters::FillInputPortInformation(int, vtkInformation* info)
135 136 137 138 139
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
  return 1;
}

140
//----------------------------------------------------------------------------
141
void vtkCellCenters::PrintSelf(ostream& os, vtkIndent indent)
142
{
Mathieu Westphal's avatar
Mathieu Westphal committed
143
  this->Superclass::PrintSelf(os, indent);
144 145
  os << indent << "Vertex Cells: " << (this->VertexCells ? "On\n" : "Off\n");
}