vtkVisItClipper.C 67.7 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2008, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-400142
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 "vtkVisItClipper.h"
hrchilds's avatar
hrchilds committed
40
#include <vtkAppendFilter.h>
hrchilds's avatar
hrchilds committed
41 42 43
#include <vtkCellData.h>
#include <vtkClipDataSet.h>
#include <vtkFloatArray.h>
hrchilds's avatar
hrchilds committed
44
#include <vtkImplicitFunction.h>
hrchilds's avatar
hrchilds committed
45
#include <vtkObjectFactory.h>
hrchilds's avatar
hrchilds committed
46
#include <vtkPlane.h>
hrchilds's avatar
hrchilds committed
47
#include <vtkPointData.h>
hrchilds's avatar
hrchilds committed
48
#include <vtkPolyData.h>
hrchilds's avatar
hrchilds committed
49
#include <vtkQuadric.h>
hrchilds's avatar
hrchilds committed
50 51 52
#include <vtkRectilinearGrid.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>
hrchilds's avatar
hrchilds committed
53 54
#include <vtkVolumeFromVolume.h>

hrchilds's avatar
hrchilds committed
55 56 57 58 59 60 61
#include <ImproperUseException.h>

#include <DebugStream.h>

#include <math.h>
#include <vector>

hrchilds's avatar
hrchilds committed
62
#include <ClipCases.h>
hrchilds's avatar
hrchilds committed
63 64
#include <vtkTriangulationTables.h>

hrchilds's avatar
hrchilds committed
65 66
vtkCxxRevisionMacro(vtkVisItClipper, "$Revision: 1.00 $");
vtkStandardNewMacro(vtkVisItClipper);
hrchilds's avatar
hrchilds committed
67

hrchilds's avatar
hrchilds committed
68 69 70 71 72 73 74 75 76 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
//
// Function: AdjustPercentToZeroCrossing
//
// Purpose: Given coordinate array, point ids and linear estimate of
// a cut, use quadric to compute actual zero crossing and adjust the
// percent value to hit the zero crossing
//
//  Programmer: Mark C. Miller
//  Creation:   December 3, 2006 
//
static void
AdjustPercentToZeroCrossing(const float *const pts, int ptId1, int ptId2,
    vtkImplicitFunction *func, float *percent)
{
    if (func == 0)
        return;

    // we only handle general quadrics at the moment
    if (strcmp(func->GetClassName(), "vtkQuadric") != 0)
        return;

    //
    // quadric equation coefficient array indexing...
    // x^2   y^2   z^2    xy    xz    yz    x    y    z    1
    //  0     1     2     3     4     5     6    7    8    9
    //
    vtkQuadric *quadric = vtkQuadric::SafeDownCast(func);
    const double *a = quadric->GetCoefficients();

    // quick check for planar functions. They're linear and so
    // 'percent' is already correct
    if (a[0] == 0.0 && a[1] == 0.0 && a[2] == 0.0 &&
        a[3] == 0.0 && a[4] == 0.0 && a[5] == 0.0)
        return;

    //
    // We'll define a "ray" between points p0 and p1 such that a
    // point along it is defined by p(t) = p0 + t * (p1 - p0).
    // When t==0, p(t)==p0 and when t==1, p(t)==p1. So, along
    // the edge between the points p0 and p1, 0<=t<=1
    //
    const float *const p0 = pts + 3*ptId1;
    const float *const p1 = pts + 3*ptId2;

    // origin of "ray" to intersect against the quadric surface
    double x0 = p0[0];
    double y0 = p0[1];
    double z0 = p0[2];

    // direction (non-normalized) of ray to intersect quadric surface
    double xd = p1[0] - x0;
    double yd = p1[1] - y0;
    double zd = p1[2] - z0;

    //
    // compute quadratic equation coefficients for ray/quadric intersection
    // At^2 + Bt + C = 0
    //
    // These equations were obtained from various web resources. However,
    // I am suspect of the equation for the B coefficient as cited on the
    // web. Several sources cite the equation with the commented line. However,
ahern's avatar
ahern committed
129
    // there is an asymmetry in it where the coefficient of the a[5] term does
hrchilds's avatar
hrchilds committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    // not include a xd*z0 contribution analagous to the a[3] and a[4] terms.
    // Empirical results from its use have shown that indeed it is in error.
    // The commented line and this comment is left here in case anyone
    // bothers to check this math against available sources.
    //
    double A = a[0]*xd*xd + a[1]*yd*yd + a[2]*zd*zd +
               a[3]*xd*yd + a[4]*yd*zd + a[5]*xd*zd;
    double B = 2*a[0]*x0*xd + 2*a[1]*y0*yd + 2*a[2]*z0*zd +
               //a[3]*(x0*yd+y0*xd) + a[4]*(y0*zd+yd*z0) + a[5]*x0*zd +
               a[3]*(x0*yd+xd*y0) + a[4]*(y0*zd+yd*z0) + a[5]*(x0*zd+xd*z0) +
               a[6]*xd + a[7]*yd +a[8]*zd;
    double C = a[0]*x0*x0 + a[1]*y0*y0 + a[2]*z0*z0 +
               a[3]*x0*y0 + a[4]*y0*z0 + a[5]*x0*z0 +
               a[6]*x0 + a[7]*y0 + a[8]*z0 + a[9];

    //
    // compute the root(s) of the quadratic equation
    //
    double t = 0.0;
    if (A == 0)
    {
        //
        // We get here if the quadric is really just linear
        //
        if (B == 0)
            t = 0.0;
        else
            t = -C / B;
    }
    else
    {
        //
        // We get here only when the quadric is indeed non-linear
        //
        double disc = B*B - 4*A*C;
        if (disc >= 0.0)
        {
            t = (-B - sqrt(disc)) / (2*A);
            if (t < 0)
                t = (-B + sqrt(disc)) / (2*A);
        }
    }

    if (t > 0.0 && t <= 1.0)
        *percent = 1.0-t;
}

hrchilds's avatar
hrchilds committed
177 178 179 180 181 182 183 184 185 186
// ****************************************************************************
//  Constructor:  vtkVisItClipper::vtkVisItClipper
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 11, 2003
//
//  Modifications:
//    Jeremy Meredith, Tue Aug 29 13:38:08 EDT 2006
//    Added support for leaving cells whole.
//
187 188 189
//    Hank Childs, Sat Sep 29 11:14:58 PDT 2007
//    Initialize new data members.
//
hrchilds's avatar
hrchilds committed
190
// ****************************************************************************
191

hrchilds's avatar
hrchilds committed
192
vtkVisItClipper::vtkVisItClipper()
hrchilds's avatar
hrchilds committed
193 194 195 196 197
{
    CellList = NULL;
    CellListSize = 0;
    insideOut = false;
    clipFunction = NULL;
hrchilds's avatar
hrchilds committed
198
    removeWholeCells = false;
hrchilds's avatar
hrchilds committed
199 200 201
    useZeroCrossings = false;
    computeInsideAndOut = false;
    otherOutput = NULL;
202 203
    scalarArrayAsVTK = NULL;
    iOwnData = false;
hrchilds's avatar
hrchilds committed
204 205
}

hrchilds's avatar
hrchilds committed
206 207 208 209 210 211 212 213
// ****************************************************************************
//  Destructor:  vtkVisItClipper::~vtkVisItClipper
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 11, 2003
//
//  Modifications:
//
214 215 216
//    Hank Childs, Sat Sep 29 11:14:58 PDT 2007
//    Clean up new data members.
//
hrchilds's avatar
hrchilds committed
217
// ****************************************************************************
hrchilds's avatar
hrchilds committed
218
vtkVisItClipper::~vtkVisItClipper()
hrchilds's avatar
hrchilds committed
219
{
hrchilds's avatar
hrchilds committed
220 221
    if (otherOutput)
        otherOutput->Delete();
222 223 224 225
    if (iOwnData)
        delete [] scalarArray;
    if (scalarArrayAsVTK != NULL)
        scalarArrayAsVTK->Delete();
hrchilds's avatar
hrchilds committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
}

void
vtkVisItClipper::SetUseZeroCrossings(bool use)
{
    if (use && clipFunction && 
        (strcmp(clipFunction->GetClassName(), "vtkQuadric") != 0))
    {
        vtkErrorMacro("UseZeroCrossings set to true allowed only with "
                      "vtkQuadric implicit functions");
        return;
    }

    useZeroCrossings = use;
}

void
vtkVisItClipper::SetComputeInsideAndOut(bool compute)
{
    computeInsideAndOut = compute;
hrchilds's avatar
hrchilds committed
246 247 248
}

void
hrchilds's avatar
hrchilds committed
249
vtkVisItClipper::SetCellList(int *cl, int size)
hrchilds's avatar
hrchilds committed
250 251 252 253 254 255
{
    CellList = cl;
    CellListSize = size;
}

void
hrchilds's avatar
hrchilds committed
256
vtkVisItClipper::SetClipFunction(vtkImplicitFunction *func)
hrchilds's avatar
hrchilds committed
257
{
hrchilds's avatar
hrchilds committed
258 259 260 261 262 263 264
    if (useZeroCrossings && (strcmp(func->GetClassName(), "vtkQuadric") != 0))
    {
        vtkErrorMacro("Only vtkQuadric implicit functions "
                      "allowed with UseZeroCrossings set to true");
        return;
    }

hrchilds's avatar
hrchilds committed
265
    // Set the clip function
hrchilds's avatar
hrchilds committed
266
    clipFunction = func;
hrchilds's avatar
hrchilds committed
267 268 269 270 271

    // Clear the scalar array so we know to use the clip function
    scalarArray = NULL;
}

hrchilds's avatar
hrchilds committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
// ****************************************************************************
//  Method:  vtkVisItClipper::SetClipScalars
//
//  Purpose:
//    Set the scalar array used for clipping, and the cutoff.
//    To clip to a range, execute this filter once for the minimum
//    and once for the maximum.
//
//  Arguments:
//    array      the scalar array
//    cutoff     the cutoff
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 30, 2004
//
//  Modifications:
288
//
hrchilds's avatar
hrchilds committed
289 290 291 292
//    Jeremy Meredith, Wed May  5 14:48:23 PDT 2004
//    Made it allow only a single cutoff, and use the "insideOut"
//    value to determine if this is a min or max value.
//
293 294 295 296
//    Hank Childs, Sat Sep 29 11:14:58 PDT 2007
//    Change the array argument to be a vtk data type.  Also added support
//    for data types besides "float".
//
hrchilds's avatar
hrchilds committed
297
// ****************************************************************************
298

hrchilds's avatar
hrchilds committed
299
void
300
vtkVisItClipper::SetClipScalars(vtkDataArray *array, float cutoff)
hrchilds's avatar
hrchilds committed
301
{
302 303 304 305 306 307 308 309 310 311 312
    if (iOwnData)
    {
        delete [] scalarArray;
        iOwnData = false;
    }
    if (scalarArrayAsVTK != NULL)
    {
        scalarArrayAsVTK->Delete();
        scalarArrayAsVTK = NULL;
    }

hrchilds's avatar
hrchilds committed
313 314 315 316
    // Clear the clip function so we know to use scalars
    clipFunction = NULL;

    // Set the scalar array
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    scalarArrayAsVTK = array;
    scalarArrayAsVTK->Register(NULL);
    if (array->GetDataType() == VTK_FLOAT)
    {
        scalarArray = (float *) array->GetVoidPointer(0);
    }
    else
    {
        iOwnData = true;
        int nTuples = array->GetNumberOfTuples();
        scalarArray = new float[nTuples];
        for (int i = 0 ; i < nTuples ; i++)
        {
            scalarArray[i] = array->GetTuple1(i);
        }
    }
hrchilds's avatar
hrchilds committed
333

hrchilds's avatar
hrchilds committed
334 335
    // Set the cutoff
    scalarCutoff     = cutoff;
hrchilds's avatar
hrchilds committed
336 337 338
}

void
hrchilds's avatar
hrchilds committed
339
vtkVisItClipper::SetInsideOut(bool io)
hrchilds's avatar
hrchilds committed
340 341 342 343
{
    insideOut = io;
}

hrchilds's avatar
hrchilds committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
// ****************************************************************************
//  Method:  vtkVisItClipper::SetRemoveWholeCells
//
//  Purpose:
//    Tell the clipper if you want it to treat cells as atomic, and
//    simply remove any cell not entirely within the region.
//
//  Arguments:
//    lcw        the new setting
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 29, 2006
//
// ****************************************************************************
void
vtkVisItClipper::SetRemoveWholeCells(bool rwc)
{
    removeWholeCells = rwc;
}

hrchilds's avatar
hrchilds committed
364 365 366 367 368
vtkUnstructuredGrid*
vtkVisItClipper::GetOtherOutput()
{
    return otherOutput;
}
hrchilds's avatar
hrchilds committed
369

hrchilds's avatar
hrchilds committed
370
// ****************************************************************************
hrchilds's avatar
hrchilds committed
371
//  Method:  vtkVisItClipper::Execute
hrchilds's avatar
hrchilds committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
//
//  Purpose:
//    Main execution method.  Delegate to mesh-specific functions.
//
//  Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 11, 2003
//
//  Modifications:
//    Jeremy Meredith, Mon Feb 16 19:07:02 PST 2004
//    Added PolyData execution path.
//
// ****************************************************************************
hrchilds's avatar
hrchilds committed
387
void
hrchilds's avatar
hrchilds committed
388
vtkVisItClipper::Execute()
hrchilds's avatar
hrchilds committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
{
    vtkDataSet *input  = GetInput();

    int do_type = input->GetDataObjectType();
    if (do_type == VTK_RECTILINEAR_GRID)
    {
        RectilinearGridExecute();
    }
    else if (do_type == VTK_STRUCTURED_GRID)
    {
        StructuredGridExecute();
    }
    else if (do_type == VTK_UNSTRUCTURED_GRID)
    {
        UnstructuredGridExecute();
    }
hrchilds's avatar
hrchilds committed
405 406 407 408
    else if (do_type == VTK_POLY_DATA)
    {
        PolyDataExecute();
    }
hrchilds's avatar
hrchilds committed
409 410
    else
    {
hrchilds's avatar
hrchilds committed
411
        debug1 << "vtkVisItClipper: Can't operate on this dataset\n";
hrchilds's avatar
hrchilds committed
412 413 414 415 416 417
        GeneralExecute();
    }
}


// ****************************************************************************
hrchilds's avatar
hrchilds committed
418
//  Method:  vtkVisItClipper::StructuredGridExecute
hrchilds's avatar
hrchilds committed
419 420 421 422 423 424 425 426 427 428 429 430
//
//  Purpose:
//    Clips a structured grid.
//
//  Arguments:
//    none
//
//  Note:  most of the setup logic came from Hank's new vtkSlicer.
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 11, 2003
//
hrchilds's avatar
hrchilds committed
431 432 433 434 435 436 437 438
//  Modifications:
//    Jeremy Meredith, Mon Sep 15 17:33:03 PDT 2003
//    Added ability for centroid-points to have an associated color.
//    This was needed for material interface reconstruction when it was
//    important to know if we should interpolate COLOR0's material or
//    COLOR1's material to come up with a material volume fraction for
//    the new point; it was not needed here, but we must skip over it.
//
hrchilds's avatar
hrchilds committed
439 440 441
//    Jeremy Meredith, Fri Jan 30 17:27:23 PST 2004
//    Added support for using a scalar array to clip against.
//
hrchilds's avatar
hrchilds committed
442 443 444
//    Jeremy Meredith, Mon Feb 16 19:07:24 PST 2004
//    Added polygonal cell support.
//
hrchilds's avatar
hrchilds committed
445 446 447 448
//    Jeremy Meredith, Wed May  5 14:49:55 PDT 2004
//    Made it support 2d cases as well.  Changed it to a single cutoff
//    for scalars to make the math more robust.
//
hrchilds's avatar
hrchilds committed
449 450 451
//    Brad Whitlock, Thu Aug 12 14:48:46 PST 2004
//    Added float casts to pow() arguments so it builds on MSVC7.Net.
//
hrchilds's avatar
hrchilds committed
452 453 454
//    Kathleen Bonnell, Tue Sep  6 08:45:16 PDT 2005
//    Added call to SetUpClipFunction. 
//
hrchilds's avatar
hrchilds committed
455 456 457 458 459 460
//    Jeremy Meredith, Tue Aug 29 16:20:25 EDT 2006
//    Added support for "atomic" cells that must be removed
//    entirely if they cannot be left whole.
//    Added support for line and vertex output shapes (though
//    structured grids shouldn't be outputting any, of course).
//
hrchilds's avatar
hrchilds committed
461 462 463
//    Mark C. Miller, Sun Dec  3 12:20:11 PST 2006
//    Added code to adjust percent to new percent consistent with zero
//    crossing of implicit func.
hrchilds's avatar
hrchilds committed
464
// ****************************************************************************
hrchilds's avatar
hrchilds committed
465

hrchilds's avatar
hrchilds committed
466
void
hrchilds's avatar
hrchilds committed
467
vtkVisItClipper::StructuredGridExecute(void)
hrchilds's avatar
hrchilds committed
468 469 470 471 472 473
{
    int  i, j;

    vtkStructuredGrid *sg = (vtkStructuredGrid *) GetInput();
    int pt_dims[3];
    sg->GetDimensions(pt_dims);
hrchilds's avatar
hrchilds committed
474
    bool twoD = (pt_dims[2] <= 1);
hrchilds's avatar
hrchilds committed
475 476 477 478 479 480 481 482

    int                nCells = sg->GetNumberOfCells();
    vtkPoints         *inPts  = sg->GetPoints();
    vtkCellData       *inCD   = sg->GetCellData();
    vtkPointData      *inPD   = sg->GetPointData();
    vtkUnstructuredGrid *output = (vtkUnstructuredGrid*)GetOutput();

    int ptSizeGuess = (CellList == NULL
hrchilds's avatar
hrchilds committed
483 484
                         ? (int) pow(float(nCells), 0.6667f) * 5 + 100
                         : CellListSize*5 + 100);
hrchilds's avatar
hrchilds committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

    vtkVolumeFromVolume vfv(sg->GetNumberOfPoints(), ptSizeGuess);

    float *pts_ptr = (float *) inPts->GetVoidPointer(0);

    int cell_dims[3];
    cell_dims[0] = pt_dims[0]-1;
    cell_dims[1] = pt_dims[1]-1;
    cell_dims[2] = pt_dims[2]-1;
    int strideY = cell_dims[0];
    int strideZ = cell_dims[0]*cell_dims[1];
    int ptstrideY = pt_dims[0];
    int ptstrideZ = pt_dims[0]*pt_dims[1];
    int X_val[8] = { 0, 1, 1, 0, 0, 1, 1, 0 };
    int Y_val[8] = { 0, 0, 1, 1, 0, 0, 1, 1 };
    int Z_val[8] = { 0, 0, 0, 0, 1, 1, 1, 1 };
    int nToProcess = (CellList != NULL ? CellListSize : nCells);
    for (i = 0 ; i < nToProcess ; i++)
    {
        int cellId = (CellList != NULL ? CellList[i] : i);
        int cellI = cellId % cell_dims[0];
        int cellJ = (cellId/strideY) % cell_dims[1];
        int cellK = (cellId/strideZ);
        int lookup_case = 0;
        float dist[8];
hrchilds's avatar
hrchilds committed
510
        int nCellPts = twoD ? 4 : 8;
hrchilds's avatar
hrchilds committed
511
        SetUpClipFunction(cellId);
hrchilds's avatar
hrchilds committed
512
        for (j = nCellPts-1 ; j >= 0 ; j--)
hrchilds's avatar
hrchilds committed
513 514 515
        {
            int ptId = (cellI + X_val[j]) + (cellJ + Y_val[j])*ptstrideY +
                       (cellK + Z_val[j])*ptstrideZ;
hrchilds's avatar
hrchilds committed
516 517 518 519 520 521 522 523 524

            if (clipFunction)
            {
                float *pt = pts_ptr + 3*ptId;
                dist[j] = clipFunction->EvaluateFunction(pt[0],pt[1],pt[2]);
            }
            else // if (scalarArray)
            {
                float val = scalarArray[ptId];
hrchilds's avatar
hrchilds committed
525
                dist[j] = scalarCutoff - val;
hrchilds's avatar
hrchilds committed
526 527
            }

hrchilds's avatar
hrchilds committed
528 529 530 531 532
            if (dist[j] >= 0)
                lookup_case++;
            if (j > 0)
                lookup_case *= 2;
        }
hrchilds's avatar
hrchilds committed
533 534 535 536

        if (removeWholeCells && lookup_case != 0)
            lookup_case = ((1 << nCellPts) - 1);

hrchilds's avatar
hrchilds committed
537 538
        unsigned char *splitCase;
        int            numOutput;
hrchilds's avatar
hrchilds committed
539
        int            interpIDs[4];
hrchilds's avatar
hrchilds committed
540 541 542 543 544 545 546 547 548 549 550
        if (twoD)
        {
            splitCase = &clipShapesQua[startClipShapesQua[lookup_case]];
            numOutput = numClipShapesQua[lookup_case];
        }
        else
        {
            splitCase = &clipShapesHex[startClipShapesHex[lookup_case]];
            numOutput = numClipShapesHex[lookup_case];
        }

hrchilds's avatar
hrchilds committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
        for (j = 0 ; j < numOutput ; j++)
        {
            unsigned char shapeType = *splitCase++;
            {
                int npts;
                int interpID = -1;
                int color    = -1;
                switch (shapeType)
                {
                  case ST_HEX:
                    npts = 8;
                    color = *splitCase++;
                    break;
                  case ST_WDG:
                    npts = 6;
                    color = *splitCase++;
                    break;
                  case ST_PYR:
                    npts = 5;
                    color = *splitCase++;
                    break;
                  case ST_TET:
                    npts = 4;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
576 577 578 579 580 581 582 583
                  case ST_QUA:
                    npts = 4;
                    color = *splitCase++;
                    break;
                  case ST_TRI:
                    npts = 3;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
584 585 586 587 588 589 590 591
                  case ST_LIN:
                    npts = 2;
                    color = *splitCase++;
                    break;
                  case ST_VTX:
                    npts = 1;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
592 593
                  case ST_PNT:
                    interpID = *splitCase++;
hrchilds's avatar
hrchilds committed
594
                    color    = *splitCase++;
hrchilds's avatar
hrchilds committed
595 596 597 598 599
                    npts     = *splitCase++;
                    break;
                  default:
                    EXCEPTION1(ImproperUseException,
                               "An invalid output shape was found in "
hrchilds's avatar
hrchilds committed
600
                               "the ClipCases.");
hrchilds's avatar
hrchilds committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614
                }

                if ((!insideOut && color == COLOR0) ||
                    ( insideOut && color == COLOR1))
                {
                    // We don't want this one; it's the wrong side.
                    splitCase += npts;
                    continue;
                }

                int shape[8];
                for (int p = 0 ; p < npts ; p++)
                {
                    unsigned char pt = *splitCase++;
hrchilds's avatar
hrchilds committed
615
                    if (pt <= P7)
hrchilds's avatar
hrchilds committed
616
                    {
hrchilds's avatar
hrchilds committed
617 618 619
                        // We know pt P0 must be >P0 since we already
                        // assume P0 == 0.  This is why we do not
                        // bother subtracting P0 from pt here.
hrchilds's avatar
hrchilds committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
                        shape[p] = ((cellI + X_val[pt]) +
                                    (cellJ + Y_val[pt])*ptstrideY +
                                    (cellK + Z_val[pt])*ptstrideZ);
                    }
                    else if (pt >= EA && pt <= EL)
                    {
                        int pt1 = hexVerticesFromEdges[pt-EA][0];
                        int pt2 = hexVerticesFromEdges[pt-EA][1];
                        if (pt2 < pt1)
                        {
                            int tmp = pt2;
                            pt2 = pt1;
                            pt1 = tmp;
                        }
                        float dir = dist[pt2] - dist[pt1];
                        float amt = 0. - dist[pt1];
                        float percent = 1. - (amt / dir);
hrchilds's avatar
hrchilds committed
637 638 639 640 641

                        // We may have physically (though not logically)
                        // degenerate cells if percent==0 or percent==1.
                        // We could pretty easily and mostly safely clamp
                        // percent to the range [1e-4, 1. - 1e-4] right here.
hrchilds's avatar
hrchilds committed
642 643 644 645 646 647
                        int ptId1 = ((cellI + X_val[pt1]) +
                                     (cellJ + Y_val[pt1])*ptstrideY +
                                     (cellK + Z_val[pt1])*ptstrideZ);
                        int ptId2 = ((cellI + X_val[pt2]) +
                                     (cellJ + Y_val[pt2])*ptstrideY +
                                     (cellK + Z_val[pt2])*ptstrideZ);
hrchilds's avatar
hrchilds committed
648 649 650 651 652 653

                        // deal with exact zero crossings if requested
                        if (clipFunction && useZeroCrossings)
                            AdjustPercentToZeroCrossing(pts_ptr, ptId1, ptId2,
                                clipFunction, &percent);
                                
hrchilds's avatar
hrchilds committed
654 655 656 657 658 659 660 661 662 663
                        shape[p] = vfv.AddPoint(ptId1, ptId2, percent);
                    }
                    else if (pt >= N0 && pt <= N3)
                    {
                        shape[p] = interpIDs[pt - N0];
                    }
                    else
                    {
                        EXCEPTION1(ImproperUseException,
                                   "An invalid output point value "
hrchilds's avatar
hrchilds committed
664
                                   "was found in the ClipCases.");
hrchilds's avatar
hrchilds committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
                    }
                }

                switch (shapeType)
                {
                  case ST_HEX:
                    vfv.AddHex(cellId,
                               shape[0], shape[1], shape[2], shape[3],
                               shape[4], shape[5], shape[6], shape[7]);
                    break;
                  case ST_WDG:
                    vfv.AddWedge(cellId,
                                 shape[0], shape[1], shape[2],
                                 shape[3], shape[4], shape[5]);
                    break;
                  case ST_PYR:
                    vfv.AddPyramid(cellId, shape[0], shape[1],
                                   shape[2], shape[3], shape[4]);
                    break;
                  case ST_TET:
                    vfv.AddTet(cellId, shape[0], shape[1], shape[2], shape[3]);
                    break;
hrchilds's avatar
hrchilds committed
687 688 689 690 691 692
                  case ST_QUA:
                    vfv.AddQuad(cellId, shape[0], shape[1], shape[2], shape[3]);
                    break;
                  case ST_TRI:
                    vfv.AddTri(cellId, shape[0], shape[1], shape[2]);
                    break;
hrchilds's avatar
hrchilds committed
693 694 695 696 697 698
                  case ST_LIN:
                    vfv.AddLine(cellId, shape[0], shape[1]);
                    break;
                  case ST_VTX:
                    vfv.AddVertex(cellId, shape[0]);
                    break;
hrchilds's avatar
hrchilds committed
699 700 701 702 703 704 705 706 707 708 709 710 711
                  case ST_PNT:
                    interpIDs[interpID] = vfv.AddCentroidPoint(npts, shape);
                    break;
                }
            }

        }
    }

    vfv.ConstructDataSet(inPD, inCD, output, pts_ptr);
}

// ****************************************************************************
hrchilds's avatar
hrchilds committed
712
//  Method:  vtkVisItClipper::RectilinearGridExecute
hrchilds's avatar
hrchilds committed
713 714 715 716 717 718 719 720 721 722 723 724
//
//  Purpose:
//    Clips a rectilinear grid.
//
//  Arguments:
//    none
//
//  Note:  most of the setup logic came from Hank's new vtkSlicer.
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 11, 2003
//
hrchilds's avatar
hrchilds committed
725 726 727 728 729 730 731 732
//  Modifications:
//    Jeremy Meredith, Mon Sep 15 17:33:03 PDT 2003
//    Added ability for centroid-points to have an associated color.
//    This was needed for material interface reconstruction when it was
//    important to know if we should interpolate COLOR0's material or
//    COLOR1's material to come up with a material volume fraction for
//    the new point; it was not needed here, but we must skip over it.
//
hrchilds's avatar
hrchilds committed
733 734 735
//    Jeremy Meredith, Fri Jan 30 17:27:23 PST 2004
//    Added support for using a scalar array to clip against.
//
hrchilds's avatar
hrchilds committed
736 737 738
//    Jeremy Meredith, Mon Feb 16 19:07:24 PST 2004
//    Added polygonal cell support.
//
hrchilds's avatar
hrchilds committed
739 740 741 742
//    Jeremy Meredith, Wed May  5 14:49:55 PDT 2004
//    Made it support 2d cases as well.  Changed it to a single cutoff
//    for scalars to make the math more robust.
//
hrchilds's avatar
hrchilds committed
743 744 745
//    Brad Whitlock, Thu Aug 12 14:49:24 PST 2004
//    Added float casts to the pow() arguments so it builds on MSVC7.Net.
//
hrchilds's avatar
hrchilds committed
746 747 748
//    Kathleen Bonnell, Tue Sep  6 08:45:16 PDT 2005
//    Added call to SetUpClipFunction. 
//
hrchilds's avatar
hrchilds committed
749 750 751 752 753 754
//    Jeremy Meredith, Tue Aug 29 16:20:25 EDT 2006
//    Added support for "atomic" cells that must be removed
//    entirely if they cannot be left whole.
//    Added support for line and vertex output shapes (though
//    rectilinear grids shouldn't be outputting any, of course).
//
hrchilds's avatar
hrchilds committed
755 756 757
//    Mark C. Miller, Sun Dec  3 12:20:11 PST 2006
//    Added code to adjust percent to new percent consistent with zero
//    crossing of implicit func.
758 759 760 761 762
//
//    Cyrus Harrison, Tue Aug 21 08:34:29 PDT 2007
//    Fixed case where new points were created but not required resulting
//    in duplicate points and bad connectivity.
//
hrchilds's avatar
hrchilds committed
763
// ****************************************************************************
hrchilds's avatar
hrchilds committed
764

hrchilds's avatar
hrchilds committed
765
void vtkVisItClipper::RectilinearGridExecute(void)
hrchilds's avatar
hrchilds committed
766 767 768 769 770 771
{
    int  i, j;

    vtkRectilinearGrid *rg = (vtkRectilinearGrid *) GetInput();
    int pt_dims[3];
    rg->GetDimensions(pt_dims);
hrchilds's avatar
hrchilds committed
772
    bool twoD = (pt_dims[2] <= 1);
hrchilds's avatar
hrchilds committed
773 774 775 776 777 778 779 780 781 782

    int           nCells = rg->GetNumberOfCells();
    float        *X      = (float* ) rg->GetXCoordinates()->GetVoidPointer(0);
    float        *Y      = (float* ) rg->GetYCoordinates()->GetVoidPointer(0);
    float        *Z      = (float* ) rg->GetZCoordinates()->GetVoidPointer(0);
    vtkCellData  *inCD   = rg->GetCellData();
    vtkPointData *inPD   = rg->GetPointData();
    vtkUnstructuredGrid *output = (vtkUnstructuredGrid*)GetOutput();

    int ptSizeGuess = (CellList == NULL
hrchilds's avatar
hrchilds committed
783 784
                         ? (int) pow(float(nCells), 0.6667f) * 5 + 100
                         : CellListSize*5 + 100);
hrchilds's avatar
hrchilds committed
785 786 787 788 789 790 791 792 793 794 795 796

    vtkVolumeFromVolume vfv(rg->GetNumberOfPoints(), ptSizeGuess);

    int cell_dims[3];
    cell_dims[0] = pt_dims[0]-1;
    cell_dims[1] = pt_dims[1]-1;
    cell_dims[2] = pt_dims[2]-1;
    int strideY = cell_dims[0];
    int strideZ = cell_dims[0]*cell_dims[1];
    int ptstrideY = pt_dims[0];
    int ptstrideZ = pt_dims[0]*pt_dims[1];
    int X_val[8] = { 0, 1, 1, 0, 0, 1, 1, 0 };
hrchilds's avatar
hrchilds committed
797 798
    int Y_val[8] = { 0, 0, 1, 1, 0, 0, 1, 1 };
    int Z_val[8] = { 0, 0, 0, 0, 1, 1, 1, 1 };
hrchilds's avatar
hrchilds committed
799 800 801 802 803 804 805 806 807
    int nToProcess = (CellList != NULL ? CellListSize : nCells);
    for (i = 0 ; i < nToProcess ; i++)
    {
        int cellId = (CellList != NULL ? CellList[i] : i);
        int cellI = cellId % cell_dims[0];
        int cellJ = (cellId/strideY) % cell_dims[1];
        int cellK = (cellId/strideZ);
        int lookup_case = 0;
        float dist[8];
hrchilds's avatar
hrchilds committed
808
        int nCellPts = twoD ? 4 : 8;
hrchilds's avatar
hrchilds committed
809
        SetUpClipFunction(cellId);
hrchilds's avatar
hrchilds committed
810
        for (j = nCellPts-1 ; j >= 0 ; j--)
hrchilds's avatar
hrchilds committed
811
        {
hrchilds's avatar
hrchilds committed
812 813 814 815 816 817 818 819 820 821 822 823 824
            if (clipFunction)
            {
                float pt[3];
                pt[0] = X[cellI + X_val[j]];
                pt[1] = Y[cellJ + Y_val[j]];
                pt[2] = Z[cellK + Z_val[j]];
                dist[j] = clipFunction->EvaluateFunction(pt[0],pt[1],pt[2]);
            }
            else // if (scalarArray)
            {
                float val = scalarArray[(cellK + Z_val[j])*ptstrideZ +
                                        (cellJ + Y_val[j])*ptstrideY +
                                        (cellI + X_val[j])];
hrchilds's avatar
hrchilds committed
825
                dist[j] = scalarCutoff - val;
hrchilds's avatar
hrchilds committed
826 827
            }

hrchilds's avatar
hrchilds committed
828 829 830 831 832
            if (dist[j] >= 0)
                lookup_case++;
            if (j > 0)
                lookup_case *= 2;
        }
hrchilds's avatar
hrchilds committed
833 834 835 836

        if (removeWholeCells && lookup_case != 0)
            lookup_case = ((1 << nCellPts) - 1);

hrchilds's avatar
hrchilds committed
837 838
        unsigned char *splitCase;
        int            numOutput;
hrchilds's avatar
hrchilds committed
839
        int            interpIDs[4];
hrchilds's avatar
hrchilds committed
840 841 842 843 844 845 846 847 848 849 850
        if (twoD)
        {
            splitCase = &clipShapesQua[startClipShapesQua[lookup_case]];
            numOutput = numClipShapesQua[lookup_case];
        }
        else
        {
            splitCase = &clipShapesHex[startClipShapesHex[lookup_case]];
            numOutput = numClipShapesHex[lookup_case];
        }

hrchilds's avatar
hrchilds committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
        for (j = 0 ; j < numOutput ; j++)
        {
            unsigned char shapeType = *splitCase++;
            {
                int npts;
                int interpID = -1;
                int color    = -1;
                switch (shapeType)
                {
                  case ST_HEX:
                    npts = 8;
                    color = *splitCase++;
                    break;
                  case ST_WDG:
                    npts = 6;
                    color = *splitCase++;
                    break;
                  case ST_PYR:
                    npts = 5;
                    color = *splitCase++;
                    break;
                  case ST_TET:
                    npts = 4;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
876 877 878 879 880 881 882 883
                  case ST_QUA:
                    npts = 4;
                    color = *splitCase++;
                    break;
                  case ST_TRI:
                    npts = 3;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
884 885 886 887 888 889 890 891
                  case ST_LIN:
                    npts = 2;
                    color = *splitCase++;
                    break;
                  case ST_VTX:
                    npts = 1;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
892 893
                  case ST_PNT:
                    interpID = *splitCase++;
hrchilds's avatar
hrchilds committed
894
                    color    = *splitCase++;
hrchilds's avatar
hrchilds committed
895 896 897 898 899
                    npts     = *splitCase++;
                    break;
                  default:
                    EXCEPTION1(ImproperUseException,
                               "An invalid output shape was found in "
hrchilds's avatar
hrchilds committed
900
                               "the ClipCases.");
hrchilds's avatar
hrchilds committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914
                }

                if ((!insideOut && color == COLOR0) ||
                    ( insideOut && color == COLOR1))
                {
                    // We don't want this one; it's the wrong side.
                    splitCase += npts;
                    continue;
                }

                int shape[8];
                for (int p = 0 ; p < npts ; p++)
                {
                    unsigned char pt = *splitCase++;
hrchilds's avatar
hrchilds committed
915
                    if (pt <= P7)
hrchilds's avatar
hrchilds committed
916
                    {
hrchilds's avatar
hrchilds committed
917 918 919
                        // We know pt P0 must be >P0 since we already
                        // assume P0 == 0.  This is why we do not
                        // bother subtracting P0 from pt here.
hrchilds's avatar
hrchilds committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
                        shape[p] = ((cellI + X_val[pt]) +
                                    (cellJ + Y_val[pt])*ptstrideY +
                                    (cellK + Z_val[pt])*ptstrideZ);
                    }
                    else if (pt >= EA && pt <= EL)
                    {
                        int pt1 = hexVerticesFromEdges[pt-EA][0];
                        int pt2 = hexVerticesFromEdges[pt-EA][1];
                        if (pt2 < pt1)
                        {
                            int tmp = pt2;
                            pt2 = pt1;
                            pt1 = tmp;
                        }
                        float dir = dist[pt2] - dist[pt1];
                        float amt = 0. - dist[pt1];
                        float percent = 1. - (amt / dir);
hrchilds's avatar
hrchilds committed
937 938 939 940 941

                        // We may have physically (though not logically)
                        // degenerate cells if percent==0 or percent==1.
                        // We could pretty easily and mostly safely clamp
                        // percent to the range [1e-4, 1. - 1e-4] right here.
hrchilds's avatar
hrchilds committed
942 943 944 945 946 947
                        int ptId1 = ((cellI + X_val[pt1]) +
                                     (cellJ + Y_val[pt1])*ptstrideY +
                                     (cellK + Z_val[pt1])*ptstrideZ);
                        int ptId2 = ((cellI + X_val[pt2]) +
                                     (cellJ + Y_val[pt2])*ptstrideY +
                                     (cellK + Z_val[pt2])*ptstrideZ);
hrchilds's avatar
hrchilds committed
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962

                        // deal with exact zero crossings if requested
                        if (clipFunction && useZeroCrossings)
                        {
                            float pt[6];
                            pt[0] = X[cellI + X_val[pt1]];
                            pt[1] = Y[cellJ + Y_val[pt1]];
                            pt[2] = Z[cellK + Z_val[pt1]];
                            pt[3] = X[cellI + X_val[pt2]];
                            pt[4] = Y[cellJ + Y_val[pt2]];
                            pt[5] = Z[cellK + Z_val[pt2]];
                            AdjustPercentToZeroCrossing(pt, 0, 1,
                                clipFunction, &percent);
                        }

963 964 965 966 967 968
                        if( percent == 1.0)
                            shape[p] = ptId1;
                        else if( percent == 0.0)
                            shape[p] = ptId2;
                        else
                            shape[p] = vfv.AddPoint(ptId1, ptId2, percent);
hrchilds's avatar
hrchilds committed
969 970 971 972 973 974 975 976 977
                    }
                    else if (pt >= N0 && pt <= N3)
                    {
                        shape[p] = interpIDs[pt - N0];
                    }
                    else
                    {
                        EXCEPTION1(ImproperUseException,
                                   "An invalid output point value "
hrchilds's avatar
hrchilds committed
978
                                   "was found in the ClipCases.");
hrchilds's avatar
hrchilds committed
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
                    }
                }

                switch (shapeType)
                {
                  case ST_HEX:
                    vfv.AddHex(cellId,
                               shape[0], shape[1], shape[2], shape[3],
                               shape[4], shape[5], shape[6], shape[7]);
                    break;
                  case ST_WDG:
                    vfv.AddWedge(cellId,
                                 shape[0], shape[1], shape[2],
                                 shape[3], shape[4], shape[5]);
                    break;
                  case ST_PYR:
                    vfv.AddPyramid(cellId, shape[0], shape[1],
                                   shape[2], shape[3], shape[4]);
                    break;
                  case ST_TET:
                    vfv.AddTet(cellId, shape[0], shape[1], shape[2], shape[3]);
                    break;
hrchilds's avatar
hrchilds committed
1001 1002 1003 1004 1005 1006
                  case ST_QUA:
                    vfv.AddQuad(cellId, shape[0], shape[1], shape[2], shape[3]);
                    break;
                  case ST_TRI:
                    vfv.AddTri(cellId, shape[0], shape[1], shape[2]);
                    break;
hrchilds's avatar
hrchilds committed
1007 1008 1009 1010 1011 1012
                  case ST_LIN:
                    vfv.AddLine(cellId, shape[0], shape[1]);
                    break;
                  case ST_VTX:
                    vfv.AddVertex(cellId, shape[0]);
                    break;
hrchilds's avatar
hrchilds committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
                  case ST_PNT:
                    interpIDs[interpID] = vfv.AddCentroidPoint(npts, shape);
                    break;
                }
            }
        }
    }

    vfv.ConstructDataSet(inPD, inCD, output, pt_dims, X, Y, Z);
}

// ****************************************************************************
hrchilds's avatar
hrchilds committed
1025
//  Method:  vtkVisItClipper::UnstructuredGridExecute
hrchilds's avatar
hrchilds committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
//
//  Purpose:
//    Clips an unstructured grid.
//
//  Arguments:
//    none
//
//  Note:  most of the setup logic came from Hank's new vtkSlicer.
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 11, 2003
//
hrchilds's avatar
hrchilds committed
1038 1039 1040 1041 1042 1043 1044 1045
//  Modifications:
//    Jeremy Meredith, Mon Sep 15 17:33:03 PDT 2003
//    Added ability for centroid-points to have an associated color.
//    This was needed for material interface reconstruction when it was
//    important to know if we should interpolate COLOR0's material or
//    COLOR1's material to come up with a material volume fraction for
//    the new point; it was not needed here, but we must skip over it.
//
hrchilds's avatar
hrchilds committed
1046 1047 1048
//    Jeremy Meredith, Fri Jan 30 17:27:23 PST 2004
//    Added support for using a scalar array to clip against.
//
hrchilds's avatar
hrchilds committed
1049 1050 1051
//    Jeremy Meredith, Mon Feb 16 19:07:24 PST 2004
//    Added polygonal cell support.
//
hrchilds's avatar
hrchilds committed
1052 1053 1054
//    Jeremy Meredith, Wed May  5 14:49:55 PDT 2004
//    Changed it to a single cutoff for scalars to make the math more robust.
//
hrchilds's avatar
hrchilds committed
1055 1056 1057 1058
//    Jeremy Meredith, Thu Jun 24 09:39:31 PDT 2004
//    Added support for unstructured voxels and pixels, letting us use this
//    fast algorithm on thresholded/onionpeeled structured meshes.
//
hrchilds's avatar
hrchilds committed
1059 1060 1061
//    Kathleen Bonnell, Tue Sep  6 08:45:16 PDT 2005
//    Added call to SetUpClipFunction. 
//
hrchilds's avatar
hrchilds committed
1062 1063 1064 1065 1066
//    Jeremy Meredith, Tue Aug 29 16:20:25 EDT 2006
//    Added support for polydata types (line and vertex).
//    Added support for "atomic" cells that must be removed
//    entirely if they cannot be left whole.
//
hrchilds's avatar
hrchilds committed
1067 1068 1069
//    Mark C. Miller, Sun Dec  3 12:20:11 PST 2006
//    Added code to compute both sides of clip in one execute. Added code
//    to adjust percent to zero crossings if requested.
1070
//
1071 1072 1073
//    Kathleen Bonnell, Wed Apr  9 09:01:20 PDT 2008 
//    Initialize interpIDtmp, so it will not be used before being set.
//
hrchilds's avatar
hrchilds committed
1074
// ****************************************************************************
1075

hrchilds's avatar
hrchilds committed
1076
void vtkVisItClipper::UnstructuredGridExecute(void)
hrchilds's avatar
hrchilds committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
{
    // The routine here is a bit trickier than for the Rectilinear or
    // Structured grids.  We want to clip an unstructured grid -- but that
    // could mean any cell type.  We only have triangulation tables for
    // the finite element zoo.  So the gameplan is to clip any of the
    // elements of the finite element zoo.  If there are more elements left
    // over, clip them using the conventional VTK filters.  Finally,
    // append together the clips from the zoo with the clips from the
    // non-zoo elements.  If all the elements are from the zoo, then just
    // clip them with no appending.

    int   i, j;

    vtkUnstructuredGrid *ug = (vtkUnstructuredGrid *) GetInput();

    int                nCells = ug->GetNumberOfCells();
    vtkPoints         *inPts  = ug->GetPoints();
    vtkCellData       *inCD   = ug->GetCellData();
    vtkPointData      *inPD   = ug->GetPointData();
    vtkUnstructuredGrid *output = (vtkUnstructuredGrid*)GetOutput();

    int ptSizeGuess = (CellList == NULL
hrchilds's avatar
hrchilds committed
1099 1100
                         ? (int) pow(float(nCells), 0.6667f) * 5 + 100
                         : CellListSize*5 + 100);
hrchilds's avatar
hrchilds committed
1101 1102

    vtkVolumeFromVolume vfv(ug->GetNumberOfPoints(), ptSizeGuess);
hrchilds's avatar
hrchilds committed
1103 1104
    vtkVolumeFromVolume vfvOut(ug->GetNumberOfPoints(), ptSizeGuess);
    vtkVolumeFromVolume *useVFV;
hrchilds's avatar
hrchilds committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

    vtkUnstructuredGrid *stuff_I_cant_clip = vtkUnstructuredGrid::New();
    stuff_I_cant_clip->SetPoints(ug->GetPoints());
    stuff_I_cant_clip->GetPointData()->ShallowCopy(ug->GetPointData());
    stuff_I_cant_clip->Allocate(nCells);

    float *pts_ptr = (float *) inPts->GetVoidPointer(0);

    int nToProcess = (CellList != NULL ? CellListSize : nCells);
    int numIcantClip = 0;
    for (i = 0 ; i < nToProcess ; i++)
    {
        int        cellId = (CellList != NULL ? CellList[i] : i);
        int        cellType = ug->GetCellType(cellId);
        int        npts;
        vtkIdType *pts;
        ug->GetCellPoints(cellId, npts, pts);
        bool canClip = false;
        switch (cellType)
        {
          case VTK_TETRA:
          case VTK_PYRAMID:
          case VTK_WEDGE:
          case VTK_HEXAHEDRON:
hrchilds's avatar
hrchilds committed
1129
          case VTK_VOXEL:
hrchilds's avatar
hrchilds committed
1130 1131
          case VTK_TRIANGLE:
          case VTK_QUAD:
hrchilds's avatar
hrchilds committed
1132
          case VTK_PIXEL:
hrchilds's avatar
hrchilds committed
1133 1134
          case VTK_LINE:
          case VTK_VERTEX:
hrchilds's avatar
hrchilds committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
            canClip = true;
            break;

          default:
            canClip = false;
            break;
        }
 
        if (canClip)
        {
hrchilds's avatar
hrchilds committed
1145
            SetUpClipFunction(cellId);
hrchilds's avatar
hrchilds committed
1146 1147 1148 1149 1150
            const int max_pts = 8;
            float dist[max_pts];
            int lookup_case = 0;
            for (j = npts-1 ; j >= 0 ; j--)
            {
hrchilds's avatar
hrchilds committed
1151 1152 1153 1154 1155 1156 1157 1158
                if (clipFunction)
                {
                    float *pt = pts_ptr + 3*pts[j];
                    dist[j] = clipFunction->EvaluateFunction(pt[0],pt[1],pt[2]);
                }
                else // if (scalarArray)
                {
                    float val = scalarArray[pts[j]];
hrchilds's avatar
hrchilds committed
1159
                    dist[j] = scalarCutoff - val;
hrchilds's avatar
hrchilds committed
1160 1161
                }

hrchilds's avatar
hrchilds committed
1162 1163 1164 1165 1166 1167
                if (dist[j] >= 0)
                    lookup_case++;
                if (j > 0)
                    lookup_case *= 2;
            }

hrchilds's avatar
hrchilds committed
1168 1169 1170
            if (removeWholeCells && lookup_case != 0)
                lookup_case = ((1 << npts) - 1);

hrchilds's avatar
hrchilds committed
1171 1172 1173 1174 1175 1176 1177 1178 1179
            int             startIndex = 0;
            unsigned char  *splitCase = NULL;
            int             numOutput = 0;
            typedef int     edgeIndices[2];
            edgeIndices    *vertices_from_edges = NULL;

            switch (cellType)
            {
              case VTK_TETRA:
hrchilds's avatar
hrchilds committed
1180 1181 1182
                startIndex = startClipShapesTet[lookup_case];
                splitCase  = &clipShapesTet[startIndex];
                numOutput  = numClipShapesTet[lookup_case];
hrchilds's avatar
hrchilds committed
1183 1184 1185
                vertices_from_edges = tetVerticesFromEdges;
                break;
              case VTK_PYRAMID:
hrchilds's avatar
hrchilds committed
1186 1187 1188
                startIndex = startClipShapesPyr[lookup_case];
                splitCase  = &clipShapesPyr[startIndex];
                numOutput  = numClipShapesPyr[lookup_case];
hrchilds's avatar
hrchilds committed
1189 1190 1191
                vertices_from_edges = pyramidVerticesFromEdges;
                break;
              case VTK_WEDGE:
hrchilds's avatar
hrchilds committed
1192 1193 1194
                startIndex = startClipShapesWdg[lookup_case];
                splitCase  = &clipShapesWdg[startIndex];
                numOutput  = numClipShapesWdg[lookup_case];
hrchilds's avatar
hrchilds committed
1195 1196 1197
                vertices_from_edges = wedgeVerticesFromEdges;
                break;
              case VTK_HEXAHEDRON:
hrchilds's avatar
hrchilds committed
1198 1199 1200
                startIndex = startClipShapesHex[lookup_case];
                splitCase  = &clipShapesHex[startIndex];
                numOutput  = numClipShapesHex[lookup_case];
hrchilds's avatar
hrchilds committed
1201 1202
                vertices_from_edges = hexVerticesFromEdges;
                break;
hrchilds's avatar
hrchilds committed
1203 1204 1205 1206 1207 1208
              case VTK_VOXEL:
                startIndex = startClipShapesVox[lookup_case];
                splitCase  = &clipShapesVox[startIndex];
                numOutput  = numClipShapesVox[lookup_case];
                vertices_from_edges = voxVerticesFromEdges;
                break;
hrchilds's avatar
hrchilds committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
              case VTK_TRIANGLE:
                startIndex = startClipShapesTri[lookup_case];
                splitCase  = &clipShapesTri[startIndex];
                numOutput  = numClipShapesTri[lookup_case];
                vertices_from_edges = triVerticesFromEdges;
                break;
              case VTK_QUAD:
                startIndex = startClipShapesQua[lookup_case];
                splitCase  = &clipShapesQua[startIndex];
                numOutput  = numClipShapesQua[lookup_case];
                vertices_from_edges = quadVerticesFromEdges;
                break;
hrchilds's avatar
hrchilds committed
1221 1222 1223 1224 1225 1226
              case VTK_PIXEL:
                startIndex = startClipShapesPix[lookup_case];
                splitCase  = &clipShapesPix[startIndex];
                numOutput  = numClipShapesPix[lookup_case];
                vertices_from_edges = pixelVerticesFromEdges;
                break;
hrchilds's avatar
hrchilds committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
              case VTK_LINE:
                startIndex = startClipShapesLin[lookup_case];
                splitCase  = &clipShapesLin[startIndex];
                numOutput  = numClipShapesLin[lookup_case];
                vertices_from_edges = lineVerticesFromEdges;
                break;
              case VTK_VERTEX:
                startIndex = startClipShapesVtx[lookup_case];
                splitCase  = &clipShapesVtx[startIndex];
                numOutput  = numClipShapesVtx[lookup_case];
                vertices_from_edges = NULL;
                break;
hrchilds's avatar
hrchilds committed
1239 1240 1241
            }

            int            interpIDs[4];
hrchilds's avatar
hrchilds committed
1242
            int            interpIDsOut[4];
hrchilds's avatar
hrchilds committed
1243 1244 1245 1246 1247 1248
            for (j = 0 ; j < numOutput ; j++)
            {
                unsigned char shapeType = *splitCase++;
                {
                    int npts;
                    int interpID = -1;
hrchilds's avatar
hrchilds committed
1249
                    int interpIDOut = -1;
1250
                    int interpIDtmp = -1;
hrchilds's avatar
hrchilds committed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
                    int color    = -1;
                    switch (shapeType)
                    {
                      case ST_HEX:
                        npts = 8;
                        color = *splitCase++;
                        break;
                      case ST_WDG:
                        npts = 6;
                        color = *splitCase++;
                        break;
                      case ST_PYR:
                        npts = 5;
                        color = *splitCase++;
                        break;
                      case ST_TET:
                        npts = 4;
                        color = *splitCase++;
                        break;
hrchilds's avatar
hrchilds committed
1270 1271 1272 1273 1274 1275 1276 1277
                      case ST_QUA:
                        npts = 4;
                        color = *splitCase++;
                        break;
                      case ST_TRI:
                        npts = 3;
                        color = *splitCase++;
                        break;
hrchilds's avatar
hrchilds committed
1278 1279 1280 1281 1282 1283 1284 1285
                      case ST_LIN:
                        npts = 2;
                        color = *splitCase++;
                        break;
                      case ST_VTX:
                        npts = 1;
                        color = *splitCase++;
                        break;
hrchilds's avatar
hrchilds committed
1286
                      case ST_PNT:
hrchilds's avatar
hrchilds committed
1287
                        interpIDtmp = *splitCase++;
hrchilds's avatar
hrchilds committed
1288
                        color    = *splitCase++;
hrchilds's avatar
hrchilds committed
1289 1290 1291 1292 1293
                        npts     = *splitCase++;
                        break;
                      default:
                        EXCEPTION1(ImproperUseException,
                                   "An invalid output shape was found in "
hrchilds's avatar
hrchilds committed
1294
                                   "the ClipCases.");
hrchilds's avatar
hrchilds committed
1295 1296
                    }

hrchilds's avatar
hrchilds committed
1297
                    useVFV = &vfv;
hrchilds's avatar
hrchilds committed
1298 1299 1300
                    if ((!insideOut && color == COLOR0) ||
                        ( insideOut && color == COLOR1))
                    {
hrchilds's avatar
hrchilds committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
                        if (computeInsideAndOut)
                        {
                            useVFV = &vfvOut;
                        }
                        else
                        {
                            // We don't want this one; it's the wrong side.
                            splitCase += npts;
                            continue;
                        }
hrchilds's avatar
hrchilds committed
1311
                    }
hrchilds's avatar
hrchilds committed
1312 1313 1314 1315
                    if (useVFV == &vfv)
                        interpID = interpIDtmp;
                    else
                        interpIDOut = interpIDtmp;
hrchilds's avatar
hrchilds committed
1316 1317 1318 1319 1320

                    int shape[8];
                    for (int p = 0 ; p < npts ; p++)
                    {
                        unsigned char pt = *splitCase++;
hrchilds's avatar
hrchilds committed
1321
                        if (pt <= P7)
hrchilds's avatar
hrchilds committed
1322
                        {
hrchilds's avatar
hrchilds committed
1323 1324 1325
                            // We know pt P0 must be >P0 since we already
                            // assume P0 == 0.  This is why we do not
                            // bother subtracting P0 from pt here.
hrchilds's avatar
hrchilds committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
                            shape[p] = pts[pt];
                        }
                        else if (pt >= EA && pt <= EL)
                        {
                            int pt1 = vertices_from_edges[pt-EA][0];
                            int pt2 = vertices_from_edges[pt-EA][1];
                            if (pt2 < pt1)
                            {
                                int tmp = pt2;
                                pt2 = pt1;
                                pt1 = tmp;
                            }
                            float dir = dist[pt2] - dist[pt1];
                            float amt = 0. - dist[pt1];
                            float percent = 1. - (amt / dir);
hrchilds's avatar
hrchilds committed
1341 1342 1343 1344 1345

                            // We may have physically (though not logically)
                            // degenerate cells if percent==0 or percent==1.
                            // We could pretty easily and mostly safely clamp
                            // percent to the range [1e-4, 1. - 1e-4] here.
hrchilds's avatar
hrchilds committed
1346 1347
                            int ptId1 = pts[pt1];
                            int ptId2 = pts[pt2];
hrchilds's avatar
hrchilds committed
1348 1349 1350 1351 1352 1353 1354

                            // deal with exact zero crossings if requested
                            if (clipFunction && useZeroCrossings)
                                AdjustPercentToZeroCrossing(pts_ptr, ptId1, ptId2,
                                    clipFunction, &percent);
                                
                            shape[p] = useVFV->AddPoint(ptId1, ptId2, percent);
hrchilds's avatar
hrchilds committed
1355 1356 1357
                        }
                        else if (pt >= N0 && pt <= N3)
                        {
hrchilds's avatar
hrchilds committed
1358 1359 1360 1361
                            if (useVFV == &vfv)
                                shape[p] = interpIDs[pt - N0];
                            else
                                shape[p] = interpIDsOut[pt - N0];
hrchilds's avatar
hrchilds committed
1362 1363 1364 1365 1366
                        }
                        else
                        {
                            EXCEPTION1(ImproperUseException,
                                       "An invalid output point value "
hrchilds's avatar
hrchilds committed
1367
                                       "was found in the ClipCases.");
hrchilds's avatar
hrchilds committed
1368 1369 1370 1371 1372 1373
                        }
                    }

                    switch (shapeType)
                    {
                      case ST_HEX:
hrchilds's avatar
hrchilds committed
1374
                        useVFV->AddHex(cellId,
hrchilds's avatar
hrchilds committed
1375 1376 1377 1378
                                   shape[0], shape[1], shape[2], shape[3],
                                   shape[4], shape[5], shape[6], shape[7]);
                        break;
                      case ST_WDG:
hrchilds's avatar
hrchilds committed
1379
                        useVFV->AddWedge(cellId,
hrchilds's avatar
hrchilds committed
1380 1381 1382 1383
                                     shape[0], shape[1], shape[2],
                                     shape[3], shape[4], shape[5]);
                        break;
                      case ST_PYR:
hrchilds's avatar
hrchilds committed
1384
                        useVFV->AddPyramid(cellId, shape[0], shape[1],
hrchilds's avatar
hrchilds committed
1385 1386 1387
                                       shape[2], shape[3], shape[4]);
                        break;
                      case ST_TET:
hrchilds's avatar
hrchilds committed
1388
                        useVFV->AddTet(cellId, shape[0], shape[1], shape[2], shape[3]);
hrchilds's avatar
hrchilds committed
1389
                        break;
hrchilds's avatar
hrchilds committed
1390
                      case ST_QUA:
hrchilds's avatar
hrchilds committed
1391
                        useVFV->AddQuad(cellId, shape[0], shape[1], shape[2], shape[3]);
hrchilds's avatar
hrchilds committed
1392 1393
                        break;
                      case ST_TRI:
hrchilds's avatar
hrchilds committed
1394
                        useVFV->AddTri(cellId, shape[0], shape[1], shape[2]);
hrchilds's avatar
hrchilds committed
1395
                        break;
hrchilds's avatar
hrchilds committed
1396
                      case ST_LIN:
hrchilds's avatar
hrchilds committed
1397
                        useVFV->AddLine(cellId, shape[0], shape[1]);
hrchilds's avatar
hrchilds committed
1398 1399
                        break;
                      case ST_VTX:
hrchilds's avatar
hrchilds committed
1400
                        useVFV->AddVertex(cellId, shape[0]);
hrchilds's avatar
hrchilds committed
1401
                        break;
hrchilds's avatar
hrchilds committed
1402
                      case ST_PNT:
hrchilds's avatar
hrchilds committed
1403 1404 1405 1406
                        if (useVFV == &vfv)
                            interpIDs[interpID] = useVFV->AddCentroidPoint(npts, shape);
                        else
                            interpIDsOut[interpIDOut] = useVFV->AddCentroidPoint(npts, shape);
hrchilds's avatar
hrchilds committed
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
                        break;
                    }
                }
            }
        }
        else
        {
            if (numIcantClip == 0)
                stuff_I_cant_clip->GetCellData()->
                                       CopyAllocate(ug->GetCellData(), nCells);

            stuff_I_cant_clip->InsertNextCell(cellType, npts, pts);
            stuff_I_cant_clip->GetCellData()->
                            CopyData(ug->GetCellData(), cellId, numIcantClip);
            numIcantClip++;
        }
    }

    if (numIcantClip > 0)
    {
        vtkUnstructuredGrid *not_from_zoo  = vtkUnstructuredGrid::New();
        ClipDataset(stuff_I_cant_clip, not_from_zoo);
        
        vtkUnstructuredGrid *just_from_zoo = vtkUnstructuredGrid::New();
        vfv.ConstructDataSet(inPD, inCD, just_from_zoo, pts_ptr);

        vtkAppendFilter *appender = vtkAppendFilter::New();
        appender->AddInput(not_from_zoo);
        appender->AddInput(just_from_zoo);
        appender->GetOutput()->Update();

        output->ShallowCopy(appender->GetOutput());
hrchilds's avatar
hrchilds committed
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

        if (computeInsideAndOut)
        {
            appender->RemoveInput(just_from_zoo);
            just_from_zoo->Delete();

            just_from_zoo = vtkUnstructuredGrid::New();
            vfvOut.ConstructDataSet(inPD, inCD, just_from_zoo, pts_ptr);

            appender->AddInput(just_from_zoo);
            appender->GetOutput()->Update();

            if (otherOutput) otherOutput->Delete();
            otherOutput = vtkUnstructuredGrid::New();
            otherOutput->ShallowCopy(appender->GetOutput());
        }

hrchilds's avatar
hrchilds committed
1456 1457
        appender->Delete();
        just_from_zoo->Delete();
hrchilds's avatar
hrchilds committed
1458
        not_from_zoo->Delete();
hrchilds's avatar
hrchilds committed
1459 1460 1461 1462
    }
    else
    {
        vfv.ConstructDataSet(inPD, inCD, output, pts_ptr);
hrchilds's avatar
hrchilds committed
1463 1464 1465 1466 1467 1468
        if (computeInsideAndOut)
        {
            if (otherOutput) otherOutput->Delete();
            otherOutput = vtkUnstructuredGrid::New();
            vfvOut.ConstructDataSet(inPD, inCD, otherOutput, pts_ptr);
        }
hrchilds's avatar
hrchilds committed
1469 1470 1471 1472 1473
    }

    stuff_I_cant_clip->Delete();
}

hrchilds's avatar
hrchilds committed
1474
// ****************************************************************************
hrchilds's avatar
hrchilds committed
1475
//  Method:  vtkVisItClipper::PolyDataExecute
hrchilds's avatar
hrchilds committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
//
//  Purpose:
//    Clips a polydata object.
//
//  Arguments:
//    none
//
//  Note:  Copied from UnstructuredGridExecute.  Probably could unify
//         them for ease of maintenance.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 16, 2004
//
//  Modifications:
hrchilds's avatar
hrchilds committed
1490 1491
//    Jeremy Meredith, Wed May  5 14:49:55 PDT 2004
//    Changed it to a single cutoff for scalars to make the math more robust.
hrchilds's avatar
hrchilds committed
1492
//
hrchilds's avatar
hrchilds committed
1493 1494 1495
//    Kathleen Bonnell, Tue Sep  6 08:45:16 PDT 2005
//    Added call to SetUpClipFunction. 
//
hrchilds's avatar
hrchilds committed
1496 1497 1498 1499 1500
//    Jeremy Meredith, Tue Aug 29 16:20:25 EDT 2006
//    Added support for polydata types (line and vertex).
//    Added support for "atomic" cells that must be removed
//    entirely if they cannot be left whole.
//
hrchilds's avatar
hrchilds committed
1501 1502 1503
//    Mark C. Miller, Sun Dec  3 12:20:11 PST 2006
//    Added code to adjust percent to new percent consistent with zero
//    crossing of implicit func.
1504
//
hrchilds's avatar
hrchilds committed
1505
// ****************************************************************************
1506

hrchilds's avatar
hrchilds committed
1507
void vtkVisItClipper::PolyDataExecute(void)
hrchilds's avatar
hrchilds committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
{
    // The routine here is a bit trickier than for the Rectilinear or
    // Structured grids.  We want to clip an unstructured grid -- but that
    // could mean any cell type.  We only have triangulation tables for
    // the finite element zoo.  So the gameplan is to clip any of the
    // elements of the finite element zoo.  If there are more elements left
    // over, clip them using the conventional VTK filters.  Finally,
    // append together the clips from the zoo with the clips from the
    // non-zoo elements.  If all the elements are from the zoo, then just
    // clip them with no appending.

    int   i, j;

    vtkPolyData *pd = (vtkPolyData *) GetInput();

    int                nCells = pd->GetNumberOfCells();
    vtkPoints         *inPts  = pd->GetPoints();
    vtkCellData       *inCD   = pd->GetCellData();
    vtkPointData      *inPD   = pd->GetPointData();
    vtkUnstructuredGrid *output = (vtkUnstructuredGrid*)GetOutput();

    int ptSizeGuess = (CellList == NULL
hrchilds's avatar
hrchilds committed
1530 1531
                         ? (int) pow(float(nCells), 0.6667f) * 5 + 100
                         : CellListSize*5 + 100);
hrchilds's avatar
hrchilds committed
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

    vtkVolumeFromVolume vfv(pd->GetNumberOfPoints(), ptSizeGuess);

    vtkUnstructuredGrid *stuff_I_cant_clip = vtkUnstructuredGrid::New();
    stuff_I_cant_clip->SetPoints(pd->GetPoints());
    stuff_I_cant_clip->GetPointData()->ShallowCopy(pd->GetPointData());
    stuff_I_cant_clip->Allocate(nCells);

    float *pts_ptr = (float *) inPts->GetVoidPointer(0);

    int nToProcess = (CellList != NULL ? CellListSize : nCells);
    int numIcantClip = 0;
    for (i = 0 ; i < nToProcess ; i++)
    {
        int        cellId = (CellList != NULL ? CellList[i] : i);
        int        cellType = pd->GetCellType(cellId);
        int        npts;
        vtkIdType *pts;
        pd->GetCellPoints(cellId, npts, pts);
        bool canClip = false;
        switch (cellType)
        {
          case VTK_TETRA:
          case VTK_PYRAMID:
          case VTK_WEDGE:
          case VTK_HEXAHEDRON:
          case VTK_TRIANGLE:
          case VTK_QUAD:
hrchilds's avatar
hrchilds committed
1560 1561
          case VTK_LINE:
          case VTK_VERTEX:
hrchilds's avatar
hrchilds committed
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
            canClip = true;
            break;

          default:
            canClip = false;
            break;
        }
 
        if (canClip)
        {
            const int max_pts = 8;
            float dist[max_pts];
            int lookup_case = 0;
hrchilds's avatar
hrchilds committed
1575
            SetUpClipFunction(cellId);
hrchilds's avatar
hrchilds committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
            for (j = npts-1 ; j >= 0 ; j--)
            {
                if (clipFunction)
                {
                    float *pt = pts_ptr + 3*pts[j];
                    dist[j] = clipFunction->EvaluateFunction(pt[0],pt[1],pt[2]);
                }
                else // if (scalarArray)
                {
                    float val = scalarArray[pts[j]];
hrchilds's avatar
hrchilds committed
1586
                    dist[j] = scalarCutoff - val;
hrchilds's avatar
hrchilds committed
1587 1588 1589 1590 1591 1592 1593 1594
                }

                if (dist[j] >= 0)
                    lookup_case++;
                if (j > 0)
                    lookup_case *= 2;
            }

hrchilds's avatar
hrchilds committed
1595 1596 1597
            if (removeWholeCells && lookup_case != 0)
                lookup_case = ((1 << npts) - 1);

hrchilds's avatar
hrchilds committed
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
            int             startIndex = 0;
            unsigned char  *splitCase = NULL;
            int             numOutput = 0;
            typedef int     edgeIndices[2];
            edgeIndices    *vertices_from_edges = NULL;

            switch (cellType)
            {
              case VTK_TETRA:
                startIndex = startClipShapesTet[lookup_case];
                splitCase  = &clipShapesTet[startIndex];
                numOutput  = numClipShapesTet[lookup_case];
                vertices_from_edges = tetVerticesFromEdges;
                break;
              case VTK_PYRAMID:
                startIndex = startClipShapesPyr[lookup_case];
                splitCase  = &clipShapesPyr[startIndex];
                numOutput  = numClipShapesPyr[lookup_case];
                vertices_from_edges = pyramidVerticesFromEdges;
                break;
              case VTK_WEDGE:
                startIndex = startClipShapesWdg[lookup_case];
                splitCase  = &clipShapesWdg[startIndex];
                numOutput  = numClipShapesWdg[lookup_case];
                vertices_from_edges = wedgeVerticesFromEdges;
                break;
              case VTK_HEXAHEDRON:
                startIndex = startClipShapesHex[lookup_case];
                splitCase  = &clipShapesHex[startIndex];
                numOutput  = numClipShapesHex[lookup_case];
                vertices_from_edges = hexVerticesFromEdges;
                break;
              case VTK_TRIANGLE:
                startIndex = startClipShapesTri[lookup_case];
                splitCase  = &clipShapesTri[startIndex];
                numOutput  = numClipShapesTri[lookup_case];
                vertices_from_edges = triVerticesFromEdges;
                break;
              case VTK_QUAD:
                startIndex = startClipShapesQua[lookup_case];
                splitCase  = &clipShapesQua[startIndex];
                numOutput  = numClipShapesQua[lookup_case];
                vertices_from_edges = quadVerticesFromEdges;
                break;
hrchilds's avatar
hrchilds committed
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
              case VTK_LINE:
                startIndex = startClipShapesLin[lookup_case];
                splitCase  = &clipShapesLin[startIndex];
                numOutput  = numClipShapesLin[lookup_case];
                vertices_from_edges = lineVerticesFromEdges;
                break;
              case VTK_VERTEX:
                startIndex = startClipShapesVtx[lookup_case];
                splitCase  = &clipShapesVtx[startIndex];
                numOutput  = numClipShapesVtx[lookup_case];
                vertices_from_edges = NULL;
                break;