vtkRectilinearGridFacelistFilter.C 30.2 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2012, 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 40
#include <vtkRectilinearGridFacelistFilter.h>

bonnell's avatar
bonnell committed
41
#include <vtkAccessors.h>
hrchilds's avatar
hrchilds committed
42 43
#include <vtkCellArray.h>
#include <vtkCellData.h>
44 45
#include <vtkInformation.h>
#include <vtkInformationVector.h>
hrchilds's avatar
hrchilds committed
46
#include <vtkObjectFactory.h>
hrchilds's avatar
hrchilds committed
47
#include <vtkPointData.h>
hrchilds's avatar
hrchilds committed
48 49
#include <vtkPolyData.h>
#include <vtkRectilinearGrid.h>
hrchilds's avatar
hrchilds committed
50
#include <vtkUnsignedCharArray.h>
bonnell's avatar
bonnell committed
51
#include <vtkVisItUtility.h>
hrchilds's avatar
hrchilds committed
52

hrchilds's avatar
hrchilds committed
53 54
#include <ImproperUseException.h>

hrchilds's avatar
hrchilds committed
55
using  std::vector;
hrchilds's avatar
hrchilds committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69

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

70

hrchilds's avatar
hrchilds committed
71 72 73 74 75 76
vtkRectilinearGridFacelistFilter::vtkRectilinearGridFacelistFilter()
{
    ForceFaceConsolidation = 0;
}


hrchilds's avatar
hrchilds committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
//
// The rectilinear grid must create a set of points and it does so by walking
// through the front face, back face, bottom face, top face, left face, right
// face (in that order without repeating points on the edges).  Indexing those 
// points later can be tiresome, so the logic is contained here in the
// "SpecializedIndexer".
//
class SpecializedIndexer
{
  public:
                      SpecializedIndexer(int, int, int);

   inline int         GetFrontFacePoint(int x, int y)
                           { return frontOffset + x*nY+y; };
   inline int         GetBackFacePoint(int x, int y)
                           { return backOffset + x*nY+y; };
   inline int         GetBottomFacePoint(int x, int z)
                           {
                             if (z == 0) return GetFrontFacePoint(x, 0);
                             if (z == nZ-1) return GetBackFacePoint(x, 0);
                             return bottomOffset + x*(nZ-2)+z-1;
                           };
   inline int         GetTopFacePoint(int x, int z)
                           {
                             if (z == 0) return GetFrontFacePoint(x, nY-1);
                             if (z == nZ-1) return GetBackFacePoint(x, nY-1);
                             return topOffset + x*(nZ-2)+z-1; 
                           };
   inline int         GetLeftFacePoint(int y, int z)
                           { 
                             if (z == 0) return GetFrontFacePoint(0, y);
                             if (z == nZ-1) return GetBackFacePoint(0, y);
                             if (y == 0) return GetBottomFacePoint(0, z);
                             if (y == nY-1) return GetTopFacePoint(0, z);
                             return leftOffset + (y-1)*(nZ-2)+z-1;
                           };
   inline int         GetRightFacePoint(int y, int z)
                           { 
                             if (z == 0) return GetFrontFacePoint(nX-1, y);
                             if (z == nZ-1) return GetBackFacePoint(nX-1, y);
                             if (y == 0) return GetBottomFacePoint(nX-1, z);
                             if (y == nY-1) return GetTopFacePoint(nX-1, z);
                             return rightOffset + (y-1)*(nZ-2)+z-1;
                           };
   inline int         GetCellIndex(int x, int y, int z)
                           { return z*cellZBase + y*cellYBase + x; };
 

  protected:
   int                nX, nY, nZ;
   int                frontOffset;
   int                backOffset;
   int                bottomOffset;
   int                topOffset;
   int                leftOffset;
   int                rightOffset;
   int                cellZBase;
   int                cellYBase;
};

137

hrchilds's avatar
hrchilds committed
138 139 140 141 142 143 144
// ****************************************************************************
//  Modifications:
//    Hank Childs, Wed Aug 25 16:28:52 PDT 2004
//    Account for degenerate meshes.
//
// ****************************************************************************

hrchilds's avatar
hrchilds committed
145 146 147 148 149 150 151 152 153 154 155 156 157
SpecializedIndexer::SpecializedIndexer(int x, int y, int z)
{
    nX = x;
    nY = y;
    nZ = z;

    frontOffset = 0;
    backOffset = nX*nY;
    bottomOffset = 2*nX*nY;
    topOffset = 2*nX*nY + nX*(nZ-2);
    leftOffset = 2*nX*nY + 2*nX*(nZ-2);
    rightOffset = 2*nX*nY + 2*nX*(nZ-2) + (nY-2)*(nZ-2);

hrchilds's avatar
hrchilds committed
158 159 160 161 162 163 164 165 166 167 168 169 170
    if (nX > 1)
       cellYBase = (nX-1);
    else
       cellYBase = 1;

    if (nY > 1 && nX > 1)
       cellZBase = (nY-1)*(nX-1);
    else if (nX > 1)
       cellZBase = nX-1;
    else if (nY > 1)
       cellZBase = nY-1;
    else
       cellZBase = 1;
hrchilds's avatar
hrchilds committed
171 172
}

173

bonnell's avatar
bonnell committed
174
// ****************************************************************************
175 176 177
//  Modifications:
//    Kathleen Biagas, Thu Sep 6 11:10:54 MST 2012
//    Templated method to process faces, preserves coordinate data type.
bonnell's avatar
bonnell committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 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
//
// ****************************************************************************

template <class Accessor> inline void
vtkRectilinearGridFacelistFilter_ProcessFaces(int nX, int nY, int nZ,
    vtkPointData *outPointData, vtkPointData *inPointData, 
    Accessor x, Accessor y, Accessor z, Accessor p)
{
  int i, j, pointId = 0;

  p.InitTraversal();

  // Front face.
  for (i = 0 ; i < nX ; ++i)
  {
    for (j = 0 ; j < nY ; ++j)
    {
      p.SetComponent(0, x.GetComponent(i));
      p.SetComponent(1, y.GetComponent(j));
      p.SetComponent(2, z.GetComponent(0));
      ++p;
      outPointData->CopyData(inPointData, j*nX + i, pointId++);
    }
  }

  // Back face
  if (nZ > 1)
  {
    for (i = 0 ; i < nX ; ++i)
    {
      for (j = 0 ; j < nY ; ++j)
      {
        p.SetComponent(0, x.GetComponent(i));
        p.SetComponent(1, y.GetComponent(j));
        p.SetComponent(2, z.GetComponent(nZ-1));
        ++p;
        outPointData->CopyData(inPointData, (nZ-1)*nX*nY + j*nX + i,pointId++);
      }
    }
  }

  // Bottom face
  for (i = 0 ; i < nX ; ++i)
  {
    for (j = 1 ; j < nZ-1 ; ++j)
    {
      p.SetComponent(0, x.GetComponent(i));
      p.SetComponent(1, y.GetComponent(0));
      p.SetComponent(2, z.GetComponent(j));
      ++p;
      outPointData->CopyData(inPointData, j*nX*nY + i, pointId++);
    }
  }

  // Top face
  if (nY > 1)
  {
    for (i = 0 ; i < nX ; ++i)
    {
      for (j = 1 ; j < nZ-1 ; ++j)
      {
        p.SetComponent(0, x.GetComponent(i));
        p.SetComponent(1, y.GetComponent(nY-1));
        p.SetComponent(2, z.GetComponent(j));
        ++p;
        outPointData->CopyData(inPointData, j*nX*nY + (nY-1)*nX + i,pointId++);
      }
    }
  }

  // Left face
  for (i = 1 ; i < nY-1 ; ++i)
  {
    for (j = 1 ; j < nZ-1 ; ++j)
    {
      p.SetComponent(0, x.GetComponent(0));
      p.SetComponent(1, y.GetComponent(i));
      p.SetComponent(2, z.GetComponent(j));
      ++p;
      outPointData->CopyData(inPointData, j*nX*nY + i*nX, pointId++);
    }
  }

  // Right face
  if (nX > 1)
  {
    for (i = 1 ; i < nY-1 ; i++)
    {
      for (j = 1 ; j < nZ-1 ; j++)
      {
        p.SetComponent(0, x.GetComponent(nX-1));
        p.SetComponent(1, y.GetComponent(i));
        p.SetComponent(2, z.GetComponent(j));
        ++p;
        outPointData->CopyData(inPointData, j*nX*nY + i*nX + nX-1, pointId++);
      }
    }
  }
}


hrchilds's avatar
hrchilds committed
279
// ****************************************************************************
280
//  Method: vtkRectilinearGridFacelistFilter::RequestData
hrchilds's avatar
hrchilds committed
281
//
282 283 284
//  Modifications:
//    Hank Childs, Thu Aug 15 21:13:43 PDT 2002
//    Fixed bug where cell data was being copied incorrectly.
hrchilds's avatar
hrchilds committed
285
//
286 287
//    Hank Childs, Wed Oct 15 19:24:56 PDT 2003
//    Added logic for consolidating faces.
hrchilds's avatar
hrchilds committed
288
//
289 290 291 292 293
//    Hank Childs, Sun Nov  9 09:44:04 PST 2003
//    Made logic for consolidating faces when ghost zones are involved a little
//    more sophisticated.  Also re-ordered how some of the quads in the normal
//    execution were stored, to make the consolidation logic easier.  The new
//    ordering is also more consistent.
hrchilds's avatar
hrchilds committed
294
//
295 296
//    Hank Childs, Fri Jan 30 08:31:44 PST 2004
//    Use pointer arithmetic to construct poly data output.
hrchilds's avatar
hrchilds committed
297
//
298 299
//    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
300
//
301 302
//    Hank Childs, Wed Aug 25 16:30:30 PDT 2004
//    Do a better job of handling degenerate meshes.
hrchilds's avatar
hrchilds committed
303
//
304 305
//    Hank Childs, Fri Aug 27 15:15:20 PDT 2004
//    Rename ghost data array.
hrchilds's avatar
hrchilds committed
306
//
307 308
//    Brad Whitlock, Fri Oct 1 17:03:53 PST 2004
//    Pass field data through to the output.
hrchilds's avatar
hrchilds committed
309
//
310 311
//    Hank Childs, Wed Nov 10 11:30:03 PST 2004
//    Correct problem where we are over-allocating number of points for 2D case.
hrchilds's avatar
hrchilds committed
312
//
313 314
//    Hank Childs, Sun Mar 13 11:09:16 PST 2005
//    Fix memory leak.
hrchilds's avatar
hrchilds committed
315
//
316 317
//    Hank Childs, Tue Jan 24 09:53:16 PST 2006
//    Add support for ghost nodes.
hrchilds's avatar
hrchilds committed
318
//
319 320
//    Brad Whitlock, Tue Apr 25 15:13:20 PST 2006
//    Pass the field data after the shallow copy or it does not get passed.
hrchilds's avatar
hrchilds committed
321
//
322 323
//    Kathleen Biagas, Thu Sep 6 11:12:43 MST 2012
//    Use new templated method to process faces, to preserve coordinate type.
bonnell's avatar
bonnell committed
324
//
hrchilds's avatar
hrchilds committed
325 326
// ****************************************************************************

327 328 329 330 331
int
vtkRectilinearGridFacelistFilter::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
hrchilds's avatar
hrchilds committed
332
{
333 334 335 336 337 338 339 340 341 342 343 344
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

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

hrchilds's avatar
hrchilds committed
345 346 347 348 349
  int   i, j;

  //
  // Set up some objects that we will be using throughout the process.
  //
350
  vtkPolyData        *outPD        = vtkPolyData::New();
hrchilds's avatar
hrchilds committed
351 352
  vtkCellData        *inCellData   = input->GetCellData();
  vtkPointData       *inPointData  = input->GetPointData();
353 354
  vtkCellData        *outCellData  = outPD->GetCellData();
  vtkPointData       *outPointData = outPD->GetPointData();
hrchilds's avatar
hrchilds committed
355

hrchilds's avatar
hrchilds committed
356 357 358 359 360 361
  //
  // If there are no ghost zones and we want to consolidate faces, this is a
  // very easy problem.  Just call the routine that will do this simply.
  //
  if (ForceFaceConsolidation)
  {
hrchilds's avatar
hrchilds committed
362 363
     if (inCellData->GetArray("avtGhostZones") == NULL &&
         inPointData->GetArray("avtGhostNodes") == NULL)
hrchilds's avatar
hrchilds committed
364
     {
365 366 367
        ConsolidateFacesWithoutGhostZones(input, output);
        outPD->Delete();
        return 1;
hrchilds's avatar
hrchilds committed
368
     }
hrchilds's avatar
hrchilds committed
369
  }  
hrchilds's avatar
hrchilds committed
370 371 372 373 374 375 376 377 378 379

  //
  // If we are doing face compaction, we will come in after the fact and
  // compact some of the faces.  These variables are used for telling the
  // face compaction routine about how the faces are laid out.
  //
  vector<int> faceStart;
  vector<int> rowSize;
  vector<int> columnSize;

hrchilds's avatar
hrchilds committed
380 381 382 383 384
  //
  // Get the information about X, Y, and Z from the rectilinear grid.
  //
  vtkDataArray *xc = input->GetXCoordinates();
  int nX = xc->GetNumberOfTuples();
bonnell's avatar
bonnell committed
385
  int tX = xc->GetDataType();
hrchilds's avatar
hrchilds committed
386 387
  vtkDataArray *yc = input->GetYCoordinates();
  int nY = yc->GetNumberOfTuples();
bonnell's avatar
bonnell committed
388
  int tY = yc->GetDataType();
hrchilds's avatar
hrchilds committed
389 390
  vtkDataArray *zc = input->GetZCoordinates();
  int nZ = zc->GetNumberOfTuples();
bonnell's avatar
bonnell committed
391 392 393 394 395
  int tZ = zc->GetDataType();
 
  bool same = (tX == tY && tY == tZ); 
  int type = (same ? tX : (tX == VTK_DOUBLE ? tX : (tY == VTK_DOUBLE ? tY : 
             (tZ == VTK_DOUBLE ? tZ : VTK_FLOAT))));
hrchilds's avatar
hrchilds committed
396 397 398 399 400 401

  //
  // Now create the points.  Do this so that the total number of points is
  // minimal -- this requires sharing points along edges and corners and leads
  // to a nasty indexing scheme.  Also be wary of 2D issues.
  //
bonnell's avatar
bonnell committed
402
  vtkPoints *pts = vtkPoints::New(type);
hrchilds's avatar
hrchilds committed
403 404 405
  int npts = 0;
  if (nX <= 1)
  {
hrchilds's avatar
hrchilds committed
406
    npts = nY*nZ;
hrchilds's avatar
hrchilds committed
407 408 409
  }
  else if (nY <= 1)
  {
hrchilds's avatar
hrchilds committed
410
    npts = nX*nZ;
hrchilds's avatar
hrchilds committed
411 412 413
  }
  else if (nZ <= 1)
  {
hrchilds's avatar
hrchilds committed
414
    npts = nX*nY;
hrchilds's avatar
hrchilds committed
415 416 417 418 419 420 421 422 423 424 425 426
  }
  else
  {
    npts = 2*nX*nY + 2*(nZ-2)*nX + 2*(nZ-2)*(nY-2);
  }
  pts->SetNumberOfPoints(npts);

  //
  // We will be copying the point data as we go so we need to set this up.
  //
  outPointData->CopyAllocate(input->GetPointData());

bonnell's avatar
bonnell committed
427
  if (same && type == VTK_FLOAT)
hrchilds's avatar
hrchilds committed
428
  {
bonnell's avatar
bonnell committed
429 430 431 432 433 434
      vtkRectilinearGridFacelistFilter_ProcessFaces(nX, nY, nZ,
          outPointData, inPointData, 
          vtkDirectAccessor<float>(xc),
          vtkDirectAccessor<float>(yc),
          vtkDirectAccessor<float>(zc),
          vtkDirectAccessor<float>(pts->GetData()));
hrchilds's avatar
hrchilds committed
435
  }
bonnell's avatar
bonnell committed
436
  else  if (same && type == VTK_DOUBLE)
hrchilds's avatar
hrchilds committed
437
  {
bonnell's avatar
bonnell committed
438 439 440 441 442 443
      vtkRectilinearGridFacelistFilter_ProcessFaces(nX, nY, nZ,
          outPointData, inPointData, 
          vtkDirectAccessor<double>(xc),
          vtkDirectAccessor<double>(yc),
          vtkDirectAccessor<double>(zc),
          vtkDirectAccessor<double>(pts->GetData()));
hrchilds's avatar
hrchilds committed
444
  }
bonnell's avatar
bonnell committed
445
  else
hrchilds's avatar
hrchilds committed
446
  {
bonnell's avatar
bonnell committed
447 448 449 450 451 452
      vtkRectilinearGridFacelistFilter_ProcessFaces(nX, nY, nZ,
          outPointData, inPointData, 
          vtkGeneralAccessor(xc),
          vtkGeneralAccessor(yc),
          vtkGeneralAccessor(zc),
          vtkGeneralAccessor(pts->GetData()));
hrchilds's avatar
hrchilds committed
453 454
  }

455
  outPD->SetPoints(pts);
hrchilds's avatar
hrchilds committed
456 457 458 459 460 461 462 463 464 465 466
  pts->Delete();

  //
  // Our indexing of points can be nasty, so use the indexer set up 
  // specifically for this class and scheme.
  //
  SpecializedIndexer indexer(nX, nY, nZ);
  
  //
  // Have the cell data allocate memory.
  //
hrchilds's avatar
hrchilds committed
467 468 469 470 471 472 473 474 475
  int   numOutCells = 0;
  if (nX > 1)
     numOutCells += 2*(nY-1)*(nZ-1);
  else
     numOutCells += (nY-1)*(nZ-1);
  if (nY > 1)
     numOutCells += 2*(nX-1)*(nZ-1);
  else
     numOutCells += (nX-1)*(nZ-1);
hrchilds's avatar
hrchilds committed
476
  if (nZ > 1)
hrchilds's avatar
hrchilds committed
477
     numOutCells += 2*(nX-1)*(nY-1);
hrchilds's avatar
hrchilds committed
478
  else
hrchilds's avatar
hrchilds committed
479
     numOutCells += (nX-1)*(nY-1);
hrchilds's avatar
hrchilds committed
480 481 482
  outCellData->CopyAllocate(inCellData);

  vtkCellArray *polys = vtkCellArray::New();
hrchilds's avatar
hrchilds committed
483 484 485
  vtkIdTypeArray *list = vtkIdTypeArray::New();
  list->SetNumberOfValues(numOutCells*(4+1));
  vtkIdType *nl = list->GetPointer(0);
hrchilds's avatar
hrchilds committed
486 487 488 489 490
  
  //
  // Left face
  //
  int cellId = 0;
hrchilds's avatar
hrchilds committed
491 492 493 494
  faceStart.push_back(cellId);
  columnSize.push_back(nZ-1);
  rowSize.push_back(nY-1);
  for (j = 0 ; j < nZ-1 ; j++)
hrchilds's avatar
hrchilds committed
495
  {
hrchilds's avatar
hrchilds committed
496
    for (i = 0 ; i < nY-1 ; i++)
hrchilds's avatar
hrchilds committed
497
    {
hrchilds's avatar
hrchilds committed
498 499 500 501 502
      *nl++ = 4;
      *nl++ = indexer.GetLeftFacePoint(i, j);
      *nl++ = indexer.GetLeftFacePoint(i, j+1);
      *nl++ = indexer.GetLeftFacePoint(i+1, j+1);
      *nl++ = indexer.GetLeftFacePoint(i+1, j);
hrchilds's avatar
hrchilds committed
503 504 505 506 507 508 509 510 511 512 513
      int cId = indexer.GetCellIndex(0, i, j);
      outCellData->CopyData(inCellData, cId, cellId);
      cellId++;
    }
  }
  
  //
  // Right face
  //
  if (nX > 1)
    {
hrchilds's avatar
hrchilds committed
514 515 516
    faceStart.push_back(cellId);
    rowSize.push_back(nZ-1);
    columnSize.push_back(nY-1);
hrchilds's avatar
hrchilds committed
517 518
    for (i = 0 ; i < nY-1 ; i++)
    {
hrchilds's avatar
hrchilds committed
519
      for (j = 0 ; j < nZ-1 ; j++) 
hrchilds's avatar
hrchilds committed
520
      {
hrchilds's avatar
hrchilds committed
521 522 523 524 525
        *nl++ = 4;
        *nl++ = indexer.GetRightFacePoint(i, j);
        *nl++ = indexer.GetRightFacePoint(i+1, j);
        *nl++ = indexer.GetRightFacePoint(i+1, j+1);
        *nl++ = indexer.GetRightFacePoint(i, j+1);
hrchilds's avatar
hrchilds committed
526 527 528 529 530 531 532 533 534 535
        int cId = indexer.GetCellIndex(nX-2, i, j);
        outCellData->CopyData(inCellData, cId, cellId);
        cellId++;
      }
    }
  }
  
  //
  // Bottom face
  //
hrchilds's avatar
hrchilds committed
536 537 538
  faceStart.push_back(cellId);
  rowSize.push_back(nZ-1);
  columnSize.push_back(nX-1);
hrchilds's avatar
hrchilds committed
539 540 541 542
  for (i = 0 ; i < nX-1 ; i++)
  {
    for (j = 0 ; j < nZ-1 ; j++)
    {
hrchilds's avatar
hrchilds committed
543 544 545 546 547
      *nl++ = 4;
      *nl++ = indexer.GetBottomFacePoint(i, j);
      *nl++ = indexer.GetBottomFacePoint(i+1, j);
      *nl++ = indexer.GetBottomFacePoint(i+1, j+1);
      *nl++ = indexer.GetBottomFacePoint(i, j+1);
hrchilds's avatar
hrchilds committed
548 549 550 551 552 553 554 555 556 557 558
      int cId = indexer.GetCellIndex(i, 0, j);
      outCellData->CopyData(inCellData, cId, cellId);
      cellId++;
    }
  }
  
  //
  // Top face
  //
  if (nY > 1)
  {
hrchilds's avatar
hrchilds committed
559 560 561 562
    faceStart.push_back(cellId);
    rowSize.push_back(nX-1);
    columnSize.push_back(nZ-1);
    for (j = 0 ; j < nZ-1 ; j++)
hrchilds's avatar
hrchilds committed
563
    {
hrchilds's avatar
hrchilds committed
564
      for (i = 0 ; i < nX-1 ; i++)
hrchilds's avatar
hrchilds committed
565
      {
hrchilds's avatar
hrchilds committed
566 567 568 569 570
        *nl++ = 4;
        *nl++ = indexer.GetTopFacePoint(i, j);
        *nl++ = indexer.GetTopFacePoint(i, j+1);
        *nl++ = indexer.GetTopFacePoint(i+1, j+1);
        *nl++ = indexer.GetTopFacePoint(i+1, j);
hrchilds's avatar
hrchilds committed
571 572 573 574 575 576 577 578 579 580
        int cId = indexer.GetCellIndex(i, nY-2, j);
        outCellData->CopyData(inCellData, cId, cellId);
        cellId++;
      }
    }
  }
  
  //
  // Back face
  //
hrchilds's avatar
hrchilds committed
581 582 583 584
  faceStart.push_back(cellId);
  rowSize.push_back(nX-1);
  columnSize.push_back(nY-1);
  for (j = 0 ; j < nY-1 ; j++)
hrchilds's avatar
hrchilds committed
585
  {
hrchilds's avatar
hrchilds committed
586
    for (i = 0 ; i < nX-1 ; i++)
hrchilds's avatar
hrchilds committed
587
    {
hrchilds's avatar
hrchilds committed
588 589 590 591 592
      *nl++ = 4;
      *nl++ = indexer.GetFrontFacePoint(i, j);
      *nl++ = indexer.GetFrontFacePoint(i, j+1);
      *nl++ = indexer.GetFrontFacePoint(i+1, j+1);
      *nl++ = indexer.GetFrontFacePoint(i+1, j);
hrchilds's avatar
hrchilds committed
593 594 595 596 597 598 599 600 601 602 603
      int cId = indexer.GetCellIndex(i, j, 0);
      outCellData->CopyData(inCellData, cId, cellId);
      cellId++;
    }
  }
  
  //
  // Front face
  //
  if (nZ > 1)
  {
hrchilds's avatar
hrchilds committed
604 605 606
    faceStart.push_back(cellId);
    rowSize.push_back(nY-1);
    columnSize.push_back(nX-1);
hrchilds's avatar
hrchilds committed
607 608 609 610
    for (i = 0 ; i < nX-1 ; i++)
    {
      for (j = 0 ; j < nY-1 ; j++)
      {
hrchilds's avatar
hrchilds committed
611 612 613 614 615
        *nl++ = 4;
        *nl++ = indexer.GetBackFacePoint(i, j);
        *nl++ = indexer.GetBackFacePoint(i+1, j);
        *nl++ = indexer.GetBackFacePoint(i+1, j+1);
        *nl++ = indexer.GetBackFacePoint(i, j+1);
hrchilds's avatar
hrchilds committed
616 617 618 619 620 621 622
        int cId = indexer.GetCellIndex(i, j, nZ-2);
        outCellData->CopyData(inCellData, cId, cellId);
        cellId++;
      }
    }
  }
  
hrchilds's avatar
hrchilds committed
623 624
  polys->SetCells(numOutCells, list);
  list->Delete();
hrchilds's avatar
hrchilds committed
625
  outCellData->Squeeze();
626
  outPD->SetPolys(polys);
hrchilds's avatar
hrchilds committed
627
  polys->Delete();
hrchilds's avatar
hrchilds committed
628 629 630 631 632 633 634

  if (ForceFaceConsolidation)
  {
     //
     // We only get to this spot if we have ghost zones -- which makes 
     // consolidating faces a harder problem.  Use a sub-routine to do that.
     //
635
     vtkPolyData *new_outPD = ConsolidateFacesWithGhostZones(outPD, list,
hrchilds's avatar
hrchilds committed
636
                                               faceStart, rowSize, columnSize);
637 638
     outPD->Delete();
     outPD = new_outPD;
hrchilds's avatar
hrchilds committed
639 640
  }

641 642 643 644
  output->ShallowCopy(outPD);
  output->GetFieldData()->ShallowCopy(input->GetFieldData());

  outPD->Delete();
hrchilds's avatar
hrchilds committed
645

646
  return 1;
hrchilds's avatar
hrchilds committed
647 648 649
}


hrchilds's avatar
hrchilds committed
650
// ****************************************************************************
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
//  Method: vtkRectilinearGridFacelistFilter::FillInputPortInformation
//
// ****************************************************************************

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


// ****************************************************************************
//  Method: vtkRectilinearGridFacelistFilter::PrintSelf
hrchilds's avatar
hrchilds committed
666
//
667 668 669 670 671 672 673 674 675 676 677 678 679
// ****************************************************************************

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


// ****************************************************************************
//  Method: vtkRectilinearGridFacelistFilter::ConsolidateFacesWithGhostZones
//
//  Modifications:
hrchilds's avatar
hrchilds committed
680 681 682
//    Hank Childs, Fri Aug 27 15:15:20 PDT 2004
//    Rename ghost data array.
//
hrchilds's avatar
hrchilds committed
683 684 685
//    Hank Childs, Tue Jan 24 09:53:16 PST 2006
//    Add support for ghost nodes.
//
hrchilds's avatar
hrchilds committed
686 687
// ****************************************************************************

hrchilds's avatar
hrchilds committed
688 689
vtkPolyData *
vtkRectilinearGridFacelistFilter::ConsolidateFacesWithGhostZones(
690 691
  vtkPolyData *pd, vtkIdTypeArray *list, vector<int> &sideStart,
  vector<int> &rowSize, vector<int> &columnSize)
hrchilds's avatar
hrchilds committed
692 693 694 695 696 697 698 699 700 701 702 703
{
  //
  // The output will have identical point information to our input.  So copy
  // that over now.
  //
  vtkPolyData *cpd = vtkPolyData::New();
  cpd->SetPoints(pd->GetPoints());
  cpd->GetPointData()->ShallowCopy(pd->GetPointData());

  //
  // Set up some useful vars for later.
  //
hrchilds's avatar
hrchilds committed
704 705 706 707
  vtkPointData *inPointData = pd->GetPointData();
  vtkCellData *inCellData   = pd->GetCellData();
  vtkCellData *outCellData  = cpd->GetCellData();
  vtkUnsignedCharArray *gzv = (vtkUnsignedCharArray *) 
hrchilds's avatar
hrchilds committed
708
                                 inCellData->GetArray("avtGhostZones");
hrchilds's avatar
hrchilds committed
709 710 711
  unsigned char *gza        = NULL;
  bool constructGZA = false;
  vector<unsigned char> ghost_zones;
hrchilds's avatar
hrchilds committed
712 713 714 715 716 717 718 719
  vtkUnsignedCharArray *gnv = (vtkUnsignedCharArray *)
                                 inPointData->GetArray("avtGhostNodes");
  if (gzv == NULL && gnv == NULL)
  {
      EXCEPTION0(ImproperUseException);
  }

  if (gnv != NULL)
hrchilds's avatar
hrchilds committed
720 721 722 723 724 725
  {
      unsigned char *gna = gnv->GetPointer(0);
      int nCells = pd->GetNumberOfCells();
      gza = new unsigned char[nCells];
      constructGZA = true;

hrchilds's avatar
hrchilds committed
726 727 728
      unsigned char *gz_val = NULL;
      if (gzv != NULL)
          gz_val = gzv->GetPointer(0);
hrchilds's avatar
hrchilds committed
729 730 731 732 733 734 735 736 737 738 739
      vtkIdType *nl = list->GetPointer(0);
      for (int i = 0 ; i < nCells ; i++)
      {
          int npts = *nl++;
          bool oneOkay = false;
          for (int j = 0 ; j < npts ; j++)
          {
              oneOkay = oneOkay || (gna[*nl] == 0);
              nl++;
          }
          gza[i] = (oneOkay ? 0 : 1);
hrchilds's avatar
hrchilds committed
740 741
          if (gz_val != NULL && gz_val[i] > 0)
              gza[i] = gz_val[i];
hrchilds's avatar
hrchilds committed
742 743 744 745
      }
  }
  else
      gza = gzv->GetPointer(0);
hrchilds's avatar
hrchilds committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

  //
  // We will be modifying the cells.  So set up some of the data structures.
  //
  outCellData->CopyAllocate(inCellData);
  int cellGuess = pd->GetNumberOfCells();
  vtkCellArray *polys = vtkCellArray::New();
  polys->Allocate(cellGuess*(4+1));
  
  //
  // These for loops will walk through the cells and try to identify 
  // neighboring cells that can be compacted.  Although the algorithm appears
  // to be quite slow (there are 5 nested for loops), it should actually run
  // in about O(nfaces) time.
  //
  int nOutputCells = 0;
  int nSides = sideStart.size();
  for (int i = 0 ; i < nSides ; i++)
  {
    int nEntries = rowSize[i]*columnSize[i];
    int startFace = sideStart[i];
    vector<bool> faceUsed(nEntries, false);
    for (int k = 0 ; k < columnSize[i] ; k++)
    {
      for (int j = 0 ; j < rowSize[i] ; j++)
      {
        int face = k*rowSize[i] + j;
        if (faceUsed[face])
           continue;
hrchilds's avatar
hrchilds committed
775
        unsigned char gz_standard = gza[startFace+face];
hrchilds's avatar
hrchilds committed
776 777 778 779 780 781 782 783 784 785 786 787

        //
        // Find out how far we can go along the row with the same ghost
        // zone value.
        //
        int lastRowMatch = j;
        int l, m;
        for (l = j+1 ; l < rowSize[i] ; l++)
        {
           int face = k*rowSize[i] + l;
           if (faceUsed[face])
             break;
hrchilds's avatar
hrchilds committed
788
           unsigned char gz_current = gza[startFace+face];
hrchilds's avatar
hrchilds committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
           if (gz_current != gz_standard)
             break;
           lastRowMatch = l;
        }

        //
        // Now we know we can go from k - lastRowMatch with the same ghost zone
        // value.  Now see how far we can down in columns.
        //
        int lastColumnMatch = k;
        for (m = k+1 ; m < columnSize[i] ; m++)
        {
          bool all_matches = true;
          for (l = j ; l <= lastRowMatch ; l++)
          {
            int face = m*rowSize[i] + l;
            if (faceUsed[face])
            {
              all_matches = false;
              break;
            }
hrchilds's avatar
hrchilds committed
810
            unsigned char gz_current = gza[startFace+face];
hrchilds's avatar
hrchilds committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
            if (gz_current != gz_standard)
            {
              all_matches = false;
              break;
            }
          }
            
          if (all_matches)
            lastColumnMatch = m;
          else
            break;
        }

        for (l = j ; l <= lastRowMatch ; l++)
          for (m = k ; m <= lastColumnMatch ; m++)
          {
            int face = m*rowSize[i] + l;
            faceUsed[face] = true;
          }

        //
        // We know now that we have a face that hasn't been used yet --
        // face (j, k).  In addition, we know that we can form a bigger quad
        // with face (j, k) as one corner and 
        // face (lastRowMatch, lastColumnMatch) as the opposite corner.
        //
        int quad_index[4];
        quad_index[0] = startFace + k*rowSize[i]+j;  // bottom-left
        quad_index[1] = startFace + lastColumnMatch*rowSize[i]+j; // top-left
        quad_index[2] = startFace 
                       + lastColumnMatch*rowSize[i]+lastRowMatch; // top-right
        quad_index[3] = startFace + k*rowSize[i]+lastRowMatch; // bottom-right

        //
        // We've constructed quad_index so that we want the l'th point from
        // quad 'l' to make our new, bigger quad.  Note that this heavily
        // depends on the quads in "Execute" being constructed in a consistent
        // way.
        //
        vtkIdType   quad[4];
        for (l = 0 ; l < 4 ; l++)
        {
          quad[l] = pd->GetCell(quad_index[l])->GetPointId(l);
        }
        polys->InsertNextCell(4, quad);
hrchilds's avatar
hrchilds committed
856
        ghost_zones.push_back(gz_standard);
hrchilds's avatar
hrchilds committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870

        //
        // Copy over the new cell data, too.  Just copy the cell data from
        // the original face -> (j, k).
        //
        outCellData->CopyData(inCellData, quad_index[0], nOutputCells++);
      }
    }
  }

  cpd->SetPolys(polys);
  polys->Squeeze();
  cpd->GetCellData()->Squeeze();
  polys->Delete();
hrchilds's avatar
hrchilds committed
871 872 873 874 875 876
  if (constructGZA)
  {
      cpd->GetPointData()->RemoveArray("avtGhostNodes");
      vtkUnsignedCharArray *new_gz = vtkUnsignedCharArray::New();
      new_gz->SetName("avtGhostZones");
      new_gz->SetNumberOfTuples(ghost_zones.size());
877
      for (size_t i = 0 ; i < ghost_zones.size() ; i++)
hrchilds's avatar
hrchilds committed
878 879 880 881 882 883 884
      {
          new_gz->SetValue(i, ghost_zones[i]);
      }
      cpd->GetCellData()->AddArray(new_gz);
      new_gz->Delete();
      delete [] gza;
  }
hrchilds's avatar
hrchilds committed
885 886
  
  return cpd;
hrchilds's avatar
hrchilds committed
887 888 889
}


hrchilds's avatar
hrchilds committed
890
// ****************************************************************************
891
//  Method: vtkRectilinearGridFacelistFilter::ConsolidateFacesWithoutGhostZones
hrchilds's avatar
hrchilds committed
892
//
893
//  Modifications:
hrchilds's avatar
hrchilds committed
894 895 896 897
//    Hank Childs, Sun Nov  9 12:37:15 PST 2003
//    Modified this routine to not handle ghost zones at all (since it wasn't
//    doing a very good job in the first place).  Also renamed the routine
//    to make it clear what its purpose was.
hrchilds's avatar
hrchilds committed
898
//
899 900 901 902
//    Jeremy Meredith, Tue Oct 14 15:14:39 EDT 2008
//    Handle cases where the grid is 2D but aligned with the X or Y axis, 
//    not just the Z axis.
//
hrchilds's avatar
hrchilds committed
903 904
// ****************************************************************************

905 906 907
void
vtkRectilinearGridFacelistFilter::ConsolidateFacesWithoutGhostZones(
  vtkRectilinearGrid *input, vtkPolyData *output)
hrchilds's avatar
hrchilds committed
908 909 910 911 912 913 914 915 916 917
{
  int  i;

  vtkCellData        *inCellData   = input->GetCellData();
  vtkPointData       *inPointData  = input->GetPointData();
  vtkCellData        *outCellData  = output->GetCellData();
  vtkPointData       *outPointData = output->GetPointData();

  int numOutCells;
  int numOutPoints;
918
  vtkIdType (*quads)[4];
hrchilds's avatar
hrchilds committed
919 920 921 922 923
  int *ptIds;

  int nX = input->GetXCoordinates()->GetNumberOfTuples();
  int nY = input->GetYCoordinates()->GetNumberOfTuples();
  int nZ = input->GetZCoordinates()->GetNumberOfTuples();
924 925
  if (nX == 1)
  {
926
      static vtkIdType quads2[1][4] = { { 0, 1, 2, 3 } };
927 928 929 930 931 932 933 934 935 936 937 938 939
      static int ptIds2[4];
      ptIds2[0] = 0;
      ptIds2[1] = nY-1;
      ptIds2[2] = nY*nZ-1;
      ptIds2[3] = (nZ-1)*nY;

      numOutCells  = 1;
      numOutPoints = 4;
      quads = quads2;
      ptIds = ptIds2;
  }
  else if (nY == 1)
  {
940
      static vtkIdType quads2[1][4] = { { 0, 1, 2, 3 } };
941 942 943 944 945 946 947 948 949 950 951 952 953
      static int ptIds2[4];
      ptIds2[0] = 0;
      ptIds2[1] = nX-1;
      ptIds2[2] = nX*nZ-1;
      ptIds2[3] = (nZ-1)*nX;

      numOutCells  = 1;
      numOutPoints = 4;
      quads = quads2;
      ptIds = ptIds2;
  }
  else if (nZ == 1)
  {
954
      static vtkIdType quads2[1][4] = { { 0, 1, 2, 3 } };
955 956 957 958 959 960 961 962 963 964 965 966
      static int ptIds2[4];
      ptIds2[0] = 0;
      ptIds2[1] = nX-1;
      ptIds2[2] = nX*nY-1;
      ptIds2[3] = (nY-1)*nX;

      numOutCells  = 1;
      numOutPoints = 4;
      quads = quads2;
      ptIds = ptIds2;
  }
  else
hrchilds's avatar
hrchilds committed
967
  {
968
      static vtkIdType quads3[6][4] = { { 0, 1, 2, 3 }, { 0, 4, 5, 1 }, 
hrchilds's avatar
hrchilds committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
                                  { 1, 5, 6, 2 }, { 2, 6, 7, 3 },
                                  { 3, 7, 4, 0 }, { 4, 7, 6, 5 } };
      static int ptIds3[8];
      ptIds3[0] = 0;
      ptIds3[1] = nX-1;
      ptIds3[2] = nX*nY-1;
      ptIds3[3] = (nY-1)*nX;
      ptIds3[4] = (nX*nY)*(nZ-1);
      ptIds3[5] = nX-1 + (nX*nY)*(nZ-1);
      ptIds3[6] = nX*nY-1 + (nX*nY)*(nZ-1);
      ptIds3[7] = (nY-1)*nX + (nX*nY)*(nZ-1);

      numOutCells = 6;
      numOutPoints = 8;
      quads = quads3;
      ptIds = ptIds3;
  }

  vtkCellArray *polys = vtkCellArray::New();
  polys->Allocate(numOutCells*(4+1));

  for (i = 0 ; i < numOutCells ; i++)
      polys->InsertNextCell(4, quads[i]);
  
  outCellData->CopyAllocate(inCellData, numOutCells);
  for (i = 0 ; i < numOutCells ; i++)
      outCellData->CopyData(inCellData, 0, i);

  outPointData->CopyAllocate(inPointData, numOutPoints);
bonnell's avatar
bonnell committed
998
  vtkPoints *pts = vtkVisItUtility::NewPoints(input);
hrchilds's avatar
hrchilds committed
999 1000 1001 1002
  pts->SetNumberOfPoints(numOutPoints);
  for (i = 0 ; i < numOutPoints ; i++)
  {
      outPointData->CopyData(inPointData, ptIds[i], i);
hrchilds's avatar
hrchilds committed
1003
      double pt[3];
hrchilds's avatar
hrchilds committed
1004 1005 1006 1007 1008 1009 1010 1011 1012
      input->GetPoint(ptIds[i], pt);
      pts->SetPoint(i, pt);
  }

  output->SetPolys(polys);
  polys->Delete();
  output->SetPoints(pts);
  pts->Delete();
}