vtkStructuredGridFacelistFilter.C 9.99 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2014, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* 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 disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* 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
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* 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.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39
#include "vtkStructuredGridFacelistFilter.h"
hrchilds's avatar
hrchilds committed
40

hrchilds's avatar
hrchilds committed
41 42
#include <vtkCellArray.h>
#include <vtkCellData.h>
43 44
#include <vtkInformation.h>
#include <vtkInformationVector.h>
hrchilds's avatar
hrchilds committed
45
#include <vtkObjectFactory.h>
hrchilds's avatar
hrchilds committed
46
#include <vtkPointData.h>
hrchilds's avatar
hrchilds committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <vtkPolyData.h>
#include <vtkStructuredGrid.h>


//------------------------------------------------------------------------------
vtkStructuredGridFacelistFilter* vtkStructuredGridFacelistFilter::New()
{
  // First try to create the object from the vtkObjectFactory
  vtkObject* ret = vtkObjectFactory::CreateInstance("vtkStructuredGridFacelistFilter");
  if(ret)
    {
    return (vtkStructuredGridFacelistFilter*)ret;
    }
  // If the factory was unable to create the object, then create it here.
  return new vtkStructuredGridFacelistFilter;
}

inline int
PointIndex(int x, int y, int z, int nX, int nY, int nZ)
{
    return z*nY*nX + y*nX + x;
}

hrchilds's avatar
hrchilds committed
70 71 72 73 74 75 76 77
// ****************************************************************************
//  Modifications:
//
//    Hank Childs, Wed Aug 25 16:28:52 PDT 2004
//    Account for degenerate meshes.
//
// ****************************************************************************

hrchilds's avatar
hrchilds committed
78 79 80
inline int
CellIndex(int x, int y, int z, int nX, int nY, int nZ)
{
hrchilds's avatar
hrchilds committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    int cellYBase = 1;
    if (nX > 1)
       cellYBase = (nX-1);
    else
       cellYBase = 1;

    int cellZBase = 1;
    if (nY > 1 && nX > 1)
       cellZBase = (nY-1)*(nX-1);
    else if (nY > 1)
       cellZBase = (nY-1);
    else if (nX > 1)
       cellZBase = (nX-1);
    else
       cellZBase = 1;

    return z*cellZBase + y*cellYBase + x;
hrchilds's avatar
hrchilds committed
98 99 100
}

// ***************************************************************************
101
//  Method: vtkStructuredGridFacelistFilter::RequestData
hrchilds's avatar
hrchilds committed
102
//
103
//  Modifications:
hrchilds's avatar
hrchilds committed
104 105 106 107 108 109 110 111 112 113
//    Kathleen Bonnell, Mon Oct 29 13:22:36 PST 2001
//    Make quad of type vtkIdType to match VTK 4.0 API.
//
//    Jeremy Meredith, Mon Jun 24 13:58:42 PDT 2002
//    Changed the ordering of 3 of the 6 sides so they are always ccw.
//    Relabeled the comments for the six faces to match conventions.
//
//    Hank Childs, Thu Jul 25 18:00:48 PDT 2002
//    Handle meshes where one dimension is '1' better.
//
hrchilds's avatar
hrchilds committed
114 115 116
//    Hank Childs, Fri Jan 30 08:31:44 PST 2004
//    Use pointer arithmetic to construct poly data output.
//
hrchilds's avatar
hrchilds committed
117 118 119
//    Hank Childs, Sun Feb  1 22:02:51 PST 2004
//    Do a better job of estimating the number of cells in the 2D case.
//
hrchilds's avatar
hrchilds committed
120 121 122
//    Hank Childs, Mon Aug  9 07:24:52 PDT 2004
//    Do a better job of handling degenerate meshes with dimensions 1xJxK.
//
hrchilds's avatar
hrchilds committed
123 124 125
//    Hank Childs, Wed Aug 25 16:32:08 PDT 2004
//    Do a better job of assessing the output cell count.
//
hrchilds's avatar
hrchilds committed
126 127
// ****************************************************************************

128 129 130 131 132
int
vtkStructuredGridFacelistFilter::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
hrchilds's avatar
hrchilds committed
133
{
134 135 136 137 138 139 140 141 142 143 144 145
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

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

hrchilds's avatar
hrchilds committed
146 147 148 149 150 151 152 153 154 155
  int   i, j;

  vtkCellData       *inCellData  = input->GetCellData();
  vtkCellData       *outCellData = output->GetCellData();

  int   dims[3];
  input->GetDimensions(dims);
  int   nX = dims[0];
  int   nY = dims[1];
  int   nZ = dims[2];
hrchilds's avatar
hrchilds committed
156 157 158
  int   numOutCells = 0;
  if (nX > 1)
     numOutCells += 2*(nY-1)*(nZ-1);
hrchilds's avatar
hrchilds committed
159
  else
hrchilds's avatar
hrchilds committed
160 161 162 163 164 165 166 167 168 169
     numOutCells += (nY-1)*(nZ-1);
  if (nY > 1)
     numOutCells += 2*(nX-1)*(nZ-1);
  else
     numOutCells += (nX-1)*(nZ-1);
  if (nZ > 1)
     numOutCells += 2*(nX-1)*(nY-1);
  else
     numOutCells += (nX-1)*(nY-1);

hrchilds's avatar
hrchilds committed
170 171 172 173 174 175 176 177 178 179 180 181
  //
  // Copy over the points and the point data.
  //
  output->SetPoints(input->GetPoints());
  output->GetPointData()->PassData(input->GetPointData());

  //
  // Have the cell data allocate memory.
  //
  outCellData->CopyAllocate(inCellData);

  vtkCellArray *polys = vtkCellArray::New();
hrchilds's avatar
hrchilds committed
182 183 184 185
  vtkIdTypeArray *list = vtkIdTypeArray::New();
  list->SetNumberOfValues(numOutCells*(4+1));
  vtkIdType *nl = list->GetPointer(0);
  
hrchilds's avatar
hrchilds committed
186 187 188 189 190 191 192 193
  //
  // Left face
  //
  int cellId = 0;
  for (i = 0 ; i < nY-1 ; i++)
  {
    for (j = 0 ; j < nZ-1 ; j++)
    {
hrchilds's avatar
hrchilds committed
194 195 196 197 198
      *nl++ = 4;
      *nl++ = PointIndex(0, i, j, nX, nY, nZ);
      *nl++ = PointIndex(0, i, j+1, nX, nY, nZ);
      *nl++ = PointIndex(0, i+1, j+1, nX, nY, nZ);
      *nl++ = PointIndex(0, i+1, j, nX, nY, nZ);
hrchilds's avatar
hrchilds committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      int cId = CellIndex(0, i, j, nX, nY, nZ);
      outCellData->CopyData(inCellData, cId, cellId);
      cellId++;
    }
  }
  
  //
  // Right face
  //
  if (nX > 1)
    {
    for (i = 0 ; i < nY-1 ; i++)
    {
      for (j = 0 ; j < nZ-1 ; j++)
      {
hrchilds's avatar
hrchilds committed
214 215 216 217 218
        *nl++ = 4;
        *nl++ = PointIndex(nX-1, i, j, nX, nY, nZ);
        *nl++ = PointIndex(nX-1, i+1, j, nX, nY, nZ);
        *nl++ = PointIndex(nX-1, i+1, j+1, nX, nY, nZ);
        *nl++ = PointIndex(nX-1, i, j+1, nX, nY, nZ);
hrchilds's avatar
hrchilds committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232
        int cId = CellIndex(nX-2, i, j, nX, nY, nZ);
        outCellData->CopyData(inCellData, cId, cellId);
        cellId++;
      }
    }
  }
  
  //
  // Bottom face
  //
  for (i = 0 ; i < nX-1 ; i++)
  {
    for (j = 0 ; j < nZ-1 ; j++)
    {
hrchilds's avatar
hrchilds committed
233 234 235 236 237
      *nl++ = 4;
      *nl++ = PointIndex(i, 0, j, nX, nY, nZ);
      *nl++ = PointIndex(i+1, 0, j, nX, nY, nZ);
      *nl++ = PointIndex(i+1, 0, j+1, nX, nY, nZ);
      *nl++ = PointIndex(i, 0, j+1, nX, nY, nZ);
hrchilds's avatar
hrchilds committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      int cId = CellIndex(i, 0, j, nX, nY, nZ);
      outCellData->CopyData(inCellData, cId, cellId);
      cellId++;
    }
  }
  
  //
  // Top face
  //
  if (nY > 1)
  {
    for (i = 0 ; i < nX-1 ; i++)
    {
      for (j = 0 ; j < nZ-1 ; j++)
      {
hrchilds's avatar
hrchilds committed
253 254 255 256 257
        *nl++ = 4;
        *nl++ = PointIndex(i, nY-1, j, nX, nY, nZ);
        *nl++ = PointIndex(i, nY-1, j+1, nX, nY, nZ);
        *nl++ = PointIndex(i+1, nY-1, j+1, nX, nY, nZ);
        *nl++ = PointIndex(i+1, nY-1, j, nX, nY, nZ);
hrchilds's avatar
hrchilds committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271
        int cId = CellIndex(i, nY-2, j, nX, nY, nZ);
        outCellData->CopyData(inCellData, cId, cellId);
        cellId++;
      }
    }
  }
  
  //
  // Back face
  //
  for (i = 0 ; i < nX-1 ; i++)
  {
    for (j = 0 ; j < nY-1 ; j++)
    {
hrchilds's avatar
hrchilds committed
272 273 274 275 276
      *nl++ = 4;
      *nl++ = PointIndex(i, j, 0, nX, nY, nZ);
      *nl++ = PointIndex(i, j+1, 0, nX, nY, nZ);
      *nl++ = PointIndex(i+1, j+1, 0, nX, nY, nZ);
      *nl++ = PointIndex(i+1, j, 0, nX, nY, nZ);
hrchilds's avatar
hrchilds committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
      int cId = CellIndex(i, j, 0, nX, nY, nZ);
      outCellData->CopyData(inCellData, cId, cellId);
      cellId++;
    }
  }
  
  //
  // Front face
  //
  if (nZ > 1)
  {
    for (i = 0 ; i < nX-1 ; i++)
    {
      for (j = 0 ; j < nY-1 ; j++)
      {
hrchilds's avatar
hrchilds committed
292 293 294 295 296
        *nl++ = 4;
        *nl++ = PointIndex(i, j, nZ-1, nX, nY, nZ);
        *nl++ = PointIndex(i+1, j, nZ-1, nX, nY, nZ);
        *nl++ = PointIndex(i+1, j+1, nZ-1, nX, nY, nZ);
        *nl++ = PointIndex(i, j+1, nZ-1, nX, nY, nZ);
hrchilds's avatar
hrchilds committed
297 298 299 300 301 302 303
        int cId = CellIndex(i, j, nZ-2, nX, nY, nZ);
        outCellData->CopyData(inCellData, cId, cellId);
        cellId++;
      }
    }
  }
  
hrchilds's avatar
hrchilds committed
304 305 306
  polys->SetCells(numOutCells, list);
  list->Delete();

hrchilds's avatar
hrchilds committed
307 308 309
  outCellData->Squeeze();
  output->SetPolys(polys);
  polys->Delete();
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

  return 1;
}

// ****************************************************************************
//  Method: vtkStructuredGridFacelistFilter::FillInputPortInformation
//
// ****************************************************************************

int
vtkStructuredGridFacelistFilter::FillInputPortInformation(int,
  vtkInformation *info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkStructuredGrid");
  return 1;
hrchilds's avatar
hrchilds committed
325 326
}

327 328 329 330
// ****************************************************************************
//  Method: vtkStructuredGridFacelistFilter::PrintSelf
//
// ****************************************************************************
hrchilds's avatar
hrchilds committed
331

332 333
void
vtkStructuredGridFacelistFilter::PrintSelf(ostream& os, vtkIndent indent)
hrchilds's avatar
hrchilds committed
334
{
335
  vtkPolyDataAlgorithm::PrintSelf(os,indent);
hrchilds's avatar
hrchilds committed
336
}