vtkUnstructuredGridRelevantPointsFilter.C 6.61 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkUnstructuredGridRelevantPointsFilter.C,v $
  Language:  C++
  Date:      $Date: 2000/05/17 16:05:12 $
  Version:   $Revision: 1.54 $


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 "vtkUnstructuredGridRelevantPointsFilter.h"
43
#include <vtkCellArray.h>
hrchilds's avatar
hrchilds committed
44 45
#include <vtkCellData.h>
#include <vtkIdList.h>
46 47
#include <vtkInformation.h>
#include <vtkInformationVector.h>
hrchilds's avatar
hrchilds committed
48 49 50
#include <vtkMergePoints.h>
#include <vtkObjectFactory.h>
#include <vtkPointData.h>
hrchilds's avatar
hrchilds committed
51 52
#include <vtkUnstructuredGrid.h>

53 54
// ****************************************************************************
//  Modifications:
bonnell's avatar
bonnell committed
55
//    Kathleen Bonnell, Wed Mar  6 17:10:03 PST 2002
56 57 58
//    Replace 'New' method with Macro to match VTK 4.0 API.
//
// ****************************************************************************
hrchilds's avatar
hrchilds committed
59 60 61 62

vtkStandardNewMacro(vtkUnstructuredGridRelevantPointsFilter);


63 64
// ****************************************************************************
//  Method: vtkUnstructuredGridRelevantPointsFilter::RequestData
hrchilds's avatar
hrchilds committed
65
//
66 67 68
//  Modifications:
//    Hank Childs, Sun Mar 13 14:12:38 PST 2005
//    Fix memory leak.
hrchilds's avatar
hrchilds committed
69
//
70 71
//    Hank Childs, Sun Mar 29 16:26:27 CDT 2009
//    Remove call to BuildLinks.
72
//
73 74 75 76 77 78
// ****************************************************************************
int
vtkUnstructuredGridRelevantPointsFilter::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
hrchilds's avatar
hrchilds committed
79
{
80 81 82 83 84 85 86 87 88 89 90 91
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  //
  // Initialize some frequently used values.
  //
  vtkUnstructuredGrid  *input = vtkUnstructuredGrid::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

92 93
  int  i, j;

hrchilds's avatar
hrchilds committed
94 95 96 97 98
  vtkDebugMacro(<<"Beginning UnstructuredGrid Relevant Points Filter ");

  if (input == NULL) 
    {
    vtkErrorMacro(<<"Input is NULL");
99
    return 1;
hrchilds's avatar
hrchilds committed
100 101 102 103 104 105 106 107 108 109
    }

  vtkPoints    *inPts  = input->GetPoints();
  int numInPts = input->GetNumberOfPoints();
  int numCells = input->GetNumberOfCells();
  output->Allocate(numCells);
  
  if ( (numInPts<1) || (inPts == NULL ) ) 
    {
    vtkErrorMacro(<<"No data to Operate On!");
110
    return 1;
hrchilds's avatar
hrchilds committed
111 112
    }
  
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  int *pointMap = new int[numInPts];
  for (i = 0 ; i < numInPts ; i++)
    {
    pointMap[i] = -1;
    }
  vtkCellArray *cells = input->GetCells();
  vtkIdType *ptr = cells->GetPointer();
  int numOutPts = 0;
  for (i = 0 ; i < numCells ; i++)
    {
    int npts = *ptr++;
    for (j = 0 ; j < npts ; j++)
      {
      int oldPt = *ptr++;
      if (pointMap[oldPt] == -1)
        pointMap[oldPt] = numOutPts++;
      }
    }

132
  vtkPoints *newPts = vtkPoints::New(input->GetPoints()->GetDataType());
133
  newPts->SetNumberOfPoints(numOutPts);
hrchilds's avatar
hrchilds committed
134 135
  vtkPointData *inputPD  = input->GetPointData();
  vtkPointData *outputPD = output->GetPointData();
136
  outputPD->CopyAllocate(inputPD, numOutPts);
hrchilds's avatar
hrchilds committed
137
  
138
  for (j = 0 ; j < numInPts ; j++)
hrchilds's avatar
hrchilds committed
139
    {
140
    if (pointMap[j] != -1)
hrchilds's avatar
hrchilds committed
141
      {
142 143 144 145
      double pt[3];
      inPts->GetPoint(j, pt);
      newPts->SetPoint(pointMap[j], pt);
      outputPD->CopyData(inputPD, j, pointMap[j]);
hrchilds's avatar
hrchilds committed
146 147 148
      }
    }

149 150 151 152 153 154
  vtkCellData  *inputCD = input->GetCellData();
  vtkCellData  *outputCD = output->GetCellData();
  outputCD->PassData(inputCD);
  
  vtkIdList *cellIds = vtkIdList::New();

hrchilds's avatar
hrchilds committed
155 156 157 158 159 160 161
  output->SetPoints(newPts);

  // now work through cells, changing associated point id to coincide
  // with the new ones as specified in the pointmap;

  vtkIdList *oldIds = vtkIdList::New(); 
  vtkIdList *newIds = vtkIdList::New();
162 163
  int id, cellType;
  ptr = cells->GetPointer();
hrchilds's avatar
hrchilds committed
164 165 166
  for (i = 0; i < numCells; i++) 
    {
    cellType = input->GetCellType(i);
167 168 169 170
    int npts = *ptr++;

    newIds->SetNumberOfIds(npts);
    for (j = 0; j < npts ; j++)
hrchilds's avatar
hrchilds committed
171
      {
172
      id = *ptr++;
hrchilds's avatar
hrchilds committed
173 174 175 176 177 178 179 180
      newIds->SetId(j, pointMap[id]);
      }
      output->InsertNextCell(cellType, newIds);
    }

  newPts->Delete();
  oldIds->Delete();
  newIds->Delete();
hrchilds's avatar
hrchilds committed
181
  cellIds->Delete();
hrchilds's avatar
hrchilds committed
182
  delete [] pointMap;
183 184

  return 1;
hrchilds's avatar
hrchilds committed
185 186
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
// ****************************************************************************
//  Method: vtkUnstructuredGridRelevantPointsFilter::FillInputPortInformation
//
// ****************************************************************************
int
vtkUnstructuredGridRelevantPointsFilter::FillInputPortInformation(int,
  vtkInformation *info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkUnstructuredGrid");
  return 1;
}

// ****************************************************************************
//  Method: vtkUnstructuredGridRelevantPointsFilter::PrintSelf
//
// ****************************************************************************
void
vtkUnstructuredGridRelevantPointsFilter::PrintSelf(ostream& os,
  vtkIndent indent) 
hrchilds's avatar
hrchilds committed
206 207 208
{
  this->Superclass::PrintSelf(os,indent);
}