vtkVisItClipper.C 50.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
#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>
48
#include <vtkPointSet.h>
hrchilds's avatar
hrchilds committed
49
#include <vtkPolyData.h>
hrchilds's avatar
hrchilds committed
50
#include <vtkQuadric.h>
hrchilds's avatar
hrchilds committed
51 52 53
#include <vtkRectilinearGrid.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>
hrchilds's avatar
hrchilds committed
54 55
#include <vtkVolumeFromVolume.h>

56 57
#include <vtkAccessors.h>

hrchilds's avatar
hrchilds committed
58 59 60
#include <ImproperUseException.h>

#include <DebugStream.h>
61
#include <TimingsManager.h>
hrchilds's avatar
hrchilds committed
62 63 64 65

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

hrchilds's avatar
hrchilds committed
66
#include <ClipCases.h>
hrchilds's avatar
hrchilds committed
67 68
#include <vtkTriangulationTables.h>

hrchilds's avatar
hrchilds committed
69 70
vtkCxxRevisionMacro(vtkVisItClipper, "$Revision: 1.00 $");
vtkStandardNewMacro(vtkVisItClipper);
hrchilds's avatar
hrchilds committed
71

hrchilds's avatar
hrchilds committed
72 73 74 75 76 77 78 79 80 81
//
// 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 
//
82 83 84 85
// Modifications:
//   Brad Whitlock, Fri Mar 23 17:13:49 PDT 2012
//   Pass in points.
//
hrchilds's avatar
hrchilds committed
86
static void
87 88
AdjustPercentToZeroCrossing(double p0[3], double p1[3], 
    vtkImplicitFunction *func, double *percent)
hrchilds's avatar
hrchilds committed
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
{
    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
    //

    // 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
135
    // there is an asymmetry in it where the coefficient of the a[5] term does
hrchilds's avatar
hrchilds committed
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 177 178 179 180 181 182
    // 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;
}

183 184 185 186 187 188 189
// ****************************************************************************
// Modifications:
//   Kathleen Biagas, Tue Aug 14 11:24:22 MST 2012
//   Added precomputeClipScalars.
//
// ****************************************************************************

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
vtkVisItClipper::FilterState::FilterState()
{ 
    this->CellList = NULL;
    this->CellListSize = 0;

    this->clipFunction = NULL;
    this->scalarArrayAsVTK = NULL;
    this->scalarCutoff = 0.;

    this->otherOutput = NULL;

    this->removeWholeCells = false;
    this->insideOut = false;
    this->useZeroCrossings = false;
    this->computeInsideAndOut = false;
205
    this->precomputeClipScalars = true;
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 279 280 281 282 283 284 285 286 287 288 289 290 291
}

vtkVisItClipper::FilterState::~FilterState()
{
    if (this->clipFunction != NULL)
        this->clipFunction->Delete();

    if (this->otherOutput)
        this->otherOutput->Delete();

    if (this->scalarArrayAsVTK != NULL)
        this->scalarArrayAsVTK->Delete();
}

void
vtkVisItClipper::FilterState::SetCellList(const vtkIdType *cl, vtkIdType size)
{
    this->CellList = cl;
    this->CellListSize = size;
}

void
vtkVisItClipper::FilterState::SetClipFunction(vtkImplicitFunction *func)
{
    // Set the clip function
    this->clipFunction = func;
    this->clipFunction->Register(NULL);

    // Clear the scalar array so we know to use the clip function
    if (scalarArrayAsVTK != NULL)
    {
        scalarArrayAsVTK->Delete();
        scalarArrayAsVTK = NULL;
    }
}

void
vtkVisItClipper::FilterState::SetClipScalars(vtkDataArray *array, double cutoff)
{
    // Clear the clip function so we know to use scalars
    if (this->clipFunction != NULL)
    {
        this->clipFunction->Delete();
        this->clipFunction = NULL;
    }

    if (scalarArrayAsVTK != NULL)
    {
        scalarArrayAsVTK->Delete();
        scalarArrayAsVTK = NULL;
    }

    // Set the scalar array
    scalarArrayAsVTK = array;
    scalarArrayAsVTK->Register(NULL);

    // Set the cutoff
    scalarCutoff     = cutoff;
}

void
vtkVisItClipper::FilterState::ClipDataset(vtkDataSet *in_ds,
                                          vtkUnstructuredGrid *out_ds)
{
    vtkClipDataSet *clipData = vtkClipDataSet::New();
    clipData->SetInput(in_ds);
    if (this->clipFunction)
    {
        clipData->SetClipFunction(this->clipFunction);
        clipData->GenerateClipScalarsOff();
        clipData->SetInsideOut(this->insideOut);
    }
    else
    {
        clipData->SetClipFunction(NULL);
        in_ds->GetPointData()->SetScalars(this->scalarArrayAsVTK);
        clipData->GenerateClipScalarsOff();
        clipData->SetValue(this->scalarCutoff);
        clipData->SetInsideOut(!this->insideOut);
    }
    clipData->Update();
    out_ds->ShallowCopy(clipData->GetOutput());
    clipData->Delete();
}


hrchilds's avatar
hrchilds committed
292 293 294 295 296 297 298 299 300 301
// ****************************************************************************
//  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.
//
302 303 304
//    Hank Childs, Sat Sep 29 11:14:58 PDT 2007
//    Initialize new data members.
//
305 306 307
//    Brad Whitlock, Tue Mar 27 12:02:00 PDT 2012
//    Move members to state.
//
hrchilds's avatar
hrchilds committed
308
// ****************************************************************************
309

310
vtkVisItClipper::vtkVisItClipper() : vtkDataSetToUnstructuredGridFilter(), state()
hrchilds's avatar
hrchilds committed
311 312 313
{
}

hrchilds's avatar
hrchilds committed
314 315 316 317 318 319 320 321
// ****************************************************************************
//  Destructor:  vtkVisItClipper::~vtkVisItClipper
//
//  Programmer:  Jeremy Meredith
//  Creation:    August 11, 2003
//
//  Modifications:
//
322 323 324
//    Hank Childs, Sat Sep 29 11:14:58 PDT 2007
//    Clean up new data members.
//
325 326 327
//    Brad Whitlock, Tue Mar 27 12:04:39 PDT 2012
//    Deletion is handled in FilterState now.
//
hrchilds's avatar
hrchilds committed
328
// ****************************************************************************
hrchilds's avatar
hrchilds committed
329
vtkVisItClipper::~vtkVisItClipper()
hrchilds's avatar
hrchilds committed
330
{
hrchilds's avatar
hrchilds committed
331 332 333 334 335
}

void
vtkVisItClipper::SetUseZeroCrossings(bool use)
{
336 337
    if (use && state.clipFunction && 
        (strcmp(state.clipFunction->GetClassName(), "vtkQuadric") != 0))
hrchilds's avatar
hrchilds committed
338 339 340 341 342 343
    {
        vtkErrorMacro("UseZeroCrossings set to true allowed only with "
                      "vtkQuadric implicit functions");
        return;
    }

344
    state.useZeroCrossings = use;
hrchilds's avatar
hrchilds committed
345 346 347 348 349
}

void
vtkVisItClipper::SetComputeInsideAndOut(bool compute)
{
350
    state.computeInsideAndOut = compute;
hrchilds's avatar
hrchilds committed
351 352 353
}

void
354
vtkVisItClipper::SetCellList(const vtkIdType *cl, vtkIdType size)
hrchilds's avatar
hrchilds committed
355
{
356
    state.SetCellList(cl, size);
hrchilds's avatar
hrchilds committed
357 358 359
}

void
hrchilds's avatar
hrchilds committed
360
vtkVisItClipper::SetClipFunction(vtkImplicitFunction *func)
hrchilds's avatar
hrchilds committed
361
{
362
    if (state.useZeroCrossings && (strcmp(func->GetClassName(), "vtkQuadric") != 0))
hrchilds's avatar
hrchilds committed
363 364 365 366 367 368
    {
        vtkErrorMacro("Only vtkQuadric implicit functions "
                      "allowed with UseZeroCrossings set to true");
        return;
    }

369
    state.SetClipFunction(func);
hrchilds's avatar
hrchilds committed
370 371
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
// ****************************************************************************
// Method:  vtkVisItClipper::SetClipScalars
//
//  Purpose:
//    Sets a flag that when false, allows postponment of clipFunction 
//    evaluation until stepping through cells.
//
// Modifications:
//   Kathleen Biagas, Tue Aug 14 11:24:22 MST 2012
//   Added precomputeClipScalars.
//
// ****************************************************************************

void
vtkVisItClipper::SetPrecomputeClipScalars(const bool v)
{
    state.precomputeClipScalars = v;
}

hrchilds's avatar
hrchilds committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
// ****************************************************************************
//  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:
407
//
hrchilds's avatar
hrchilds committed
408 409 410 411
//    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.
//
412 413 414 415
//    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".
//
416 417 418
//    Brad Whitlock, Tue Mar 27 11:51:35 PDT 2012
//    Move to FilterState.
//
hrchilds's avatar
hrchilds committed
419
// ****************************************************************************
420

hrchilds's avatar
hrchilds committed
421
void
422
vtkVisItClipper::SetClipScalars(vtkDataArray *array, float cutoff)
hrchilds's avatar
hrchilds committed
423
{
424
    state.SetClipScalars(array, cutoff);
hrchilds's avatar
hrchilds committed
425 426 427
}

void
hrchilds's avatar
hrchilds committed
428
vtkVisItClipper::SetInsideOut(bool io)
hrchilds's avatar
hrchilds committed
429
{
430
    state.insideOut = io;
hrchilds's avatar
hrchilds committed
431 432
}

hrchilds's avatar
hrchilds committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
// ****************************************************************************
//  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)
{
450
    state.removeWholeCells = rwc;
hrchilds's avatar
hrchilds committed
451 452
}

hrchilds's avatar
hrchilds committed
453 454 455
vtkUnstructuredGrid*
vtkVisItClipper::GetOtherOutput()
{
456
    return state.otherOutput;
hrchilds's avatar
hrchilds committed
457
}
hrchilds's avatar
hrchilds committed
458

hrchilds's avatar
hrchilds committed
459
// ****************************************************************************
460
// Class: CellPointsGetter
hrchilds's avatar
hrchilds committed
461
//
462 463
// Purpose:
//   Provides a GetCellPoints method for structured meshes.
hrchilds's avatar
hrchilds committed
464
//
465 466
// Notes:      This class is separated out so it can be used for both the
//             Structured and Rectilinear bridge classes.
hrchilds's avatar
hrchilds committed
467
//
468 469
// Programmer: Brad Whitlock
// Creation:   Thu Mar 29 14:35:21 PDT 2012
hrchilds's avatar
hrchilds committed
470
//
471 472 473 474 475 476 477 478 479 480 481 482 483 484 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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
// Modifications:
//   
// ****************************************************************************

class CellPointsGetter
{
public:
    CellPointsGetter()
    {
        int d[] = {1,1,1};
        Initialize(d);
    }

    CellPointsGetter(const int  *dims)
    {
        Initialize(dims);
    }

    void Initialize(const int *dims)
    {
        pt_dims[0] = dims[0];
        pt_dims[1] = dims[1];
        pt_dims[2] = dims[2];
        cell_dims[0] = (pt_dims[0]>1) ? (pt_dims[0]-1) : pt_dims[0];
        cell_dims[1] = (pt_dims[1]>1) ? (pt_dims[1]-1) : pt_dims[1];
        cell_dims[2] = (pt_dims[2]>1) ? (pt_dims[2]-1) : pt_dims[2];
        strideY = cell_dims[0];
        strideZ = cell_dims[0]*cell_dims[1];
        ptstrideY = pt_dims[0];
        ptstrideZ = pt_dims[0]*pt_dims[1];

        cellType = (dims[2] <= 1) ? VTK_QUAD : VTK_HEXAHEDRON;
        nCellPts = (dims[2] <= 1) ? 4 : 8;
    }

    inline vtkIdType *GetCellPoints(vtkIdType cellId, vtkIdType &nCellPoints)
    {
        const int X_val[8] = { 0, 1, 1, 0, 0, 1, 1, 0 };
        const int Y_val[8] = { 0, 0, 1, 1, 0, 0, 1, 1 };
        const int Z_val[8] = { 0, 0, 0, 0, 1, 1, 1, 1 };
        vtkIdType cellI = cellId % cell_dims[0];
        vtkIdType cellJ = (cellId/strideY) % cell_dims[1];
        vtkIdType cellK = (cellId/strideZ);
        for (int j = 0; j<nCellPts; j++)
        {
            ids[j] = (cellI + X_val[j]) +
                     (cellJ + Y_val[j])*ptstrideY +
                     (cellK + Z_val[j])*ptstrideZ;
        }
        nCellPoints = nCellPts;
        return ids;
    }

    inline const int *GetDimensions() const { return pt_dims; }
    inline int GetCellType(vtkIdType) const { return cellType; }
private:
    vtkIdType   ids[8];

    int         pt_dims[3];
    vtkIdType   cell_dims[3];
    vtkIdType   strideY, strideZ;
    vtkIdType   ptstrideY, ptstrideZ;
    vtkIdType   nCellPts;
    int         cellType;
};

// ****************************************************************************
// Class: ClipperBridge
hrchilds's avatar
hrchilds committed
539
//
540 541 542
// Purpose:
//   This class is bridge that provides methods to access cells and points for
//   classes that use vtkPoints for their points.
543
//
544 545 546 547 548
// Notes:      The class is templated on PointGetter so we can access point
//             data directly as memory or using GetPoints calls.
//
// Programmer: Brad Whitlock
// Creation:   Mon Mar 26 13:38:48 PDT 2012
549
//
550 551
// Modifications:
//   
hrchilds's avatar
hrchilds committed
552
// ****************************************************************************
553 554 555

template <typename PointGetter>
class ClipperBridge
hrchilds's avatar
hrchilds committed
556
{
557 558 559 560
public:
    ClipperBridge(vtkPointSet *d) : points(d->GetPoints()), ds(d)
    {
    }
hrchilds's avatar
hrchilds committed
561

562 563 564 565
    inline vtkIdType GetNumberOfCells()  { return ds->GetNumberOfCells(); }
    inline vtkIdType GetNumberOfPoints() { return ds->GetNumberOfPoints(); }
    inline vtkCellData *GetCellData()    { return ds->GetCellData(); }
    inline vtkPointData *GetPointData()  { return ds->GetPointData(); }
566

567 568 569 570
    inline void GetPoint(vtkIdType index, double pt[3]) const
    {
        points.GetPoint(index, pt);
    }
571

572 573
    void ConstructDataSet(vtkVolumeFromVolume &vfv,
                          vtkUnstructuredGrid *output)
hrchilds's avatar
hrchilds committed
574
    {
575 576 577 578 579 580
        vfv.ConstructDataSet(GetPointData(), GetCellData(), output, ds->GetPoints());
    }
private:
    PointGetter  points;
    vtkPointSet *ds;
};
581

582 583 584 585 586 587 588 589 590 591
//
// These subclasses are necessary because GetCellPoints is not implemented in vtkPointSet.
//
template <typename PointGetter>
class ClipperBridgePolyData : public ClipperBridge<PointGetter>
{
public:
    ClipperBridgePolyData(vtkPolyData *d) : ClipperBridge<PointGetter>(d)
    {
        pd = d;
hrchilds's avatar
hrchilds committed
592
    }
593 594

    inline int GetCellType(vtkIdType cellId) const
hrchilds's avatar
hrchilds committed
595
    {
596
        return pd->GetCellType(cellId);
hrchilds's avatar
hrchilds committed
597
    }
598 599

    inline vtkIdType *GetCellPoints(vtkIdType cellId, vtkIdType &nCellPts)
hrchilds's avatar
hrchilds committed
600
    {
601 602 603 604 605 606 607
        vtkIdType *cellPts = NULL;
        pd->GetCellPoints(cellId, nCellPts, cellPts);
        return cellPts;
    }
private:
    vtkPolyData *pd;
};
608

609 610 611 612 613 614 615
template <typename PointGetter>
class ClipperBridgeUnstructuredGrid : public ClipperBridge<PointGetter>
{
public:
    ClipperBridgeUnstructuredGrid(vtkUnstructuredGrid *d) : ClipperBridge<PointGetter>(d)
    {
        ug = d;
hrchilds's avatar
hrchilds committed
616
    }
617 618

    inline int GetCellType(vtkIdType cellId) const
hrchilds's avatar
hrchilds committed
619
    {
620
        return ug->GetCellType(cellId);
hrchilds's avatar
hrchilds committed
621 622
    }

623
    inline vtkIdType *GetCellPoints(vtkIdType cellId, vtkIdType &nCellPts)
624
    {
625 626 627
        vtkIdType *cellPts = NULL;
        ug->GetCellPoints(cellId, nCellPts, cellPts);
        return cellPts;
628
    }
629 630 631
private:
    vtkUnstructuredGrid *ug;
};
632

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
template <typename PointGetter>
class ClipperBridgeStructuredGrid : public ClipperBridge<PointGetter>
{
public:
    ClipperBridgeStructuredGrid(vtkStructuredGrid *d) : ClipperBridge<PointGetter>(d), 
        cellPoints()
    {
        sg = d;
        int dims[3];
        sg->GetDimensions(dims);
        cellPoints.Initialize(dims);
    }

    inline int GetCellType(vtkIdType cellId) const
    {
        return cellPoints.GetCellType(cellId);
    }

    inline vtkIdType *GetCellPoints(vtkIdType cellId, vtkIdType &nCellPts)
    {
        return cellPoints.GetCellPoints(cellId, nCellPts);
    }
private:
    vtkStructuredGrid *sg;
    CellPointsGetter   cellPoints;
};

// ****************************************************************************
// Class: ClipperBridgeRectilinearGrid
//
// Purpose:
//   This class is bridge that provides methods to access cells and points for
//   rectilinear grids.
//
// Notes:      The class is templated on PointGetter so we can access point
//             data directly as memory or using GetTuple1 calls.
//
// Programmer: Brad Whitlock
// Creation:   Mon Mar 26 13:38:48 PDT 2012
//
// Modifications:
//   
// ****************************************************************************

template <typename PointGetter>
class ClipperBridgeRectilinearGrid
{
public:
    ClipperBridgeRectilinearGrid(vtkRectilinearGrid *rg, const int *dims, 
        vtkDataArray *Xc, vtkDataArray *Yc, vtkDataArray *Zc) : 
        points(dims, Xc, Yc, Zc), cellPoints(dims), ds(rg), X(Xc), Y(Yc), Z(Zc)
    {
    }

    inline vtkIdType GetNumberOfCells()  { return ds->GetNumberOfCells(); }
    inline vtkIdType GetNumberOfPoints() { return ds->GetNumberOfPoints(); }
    inline vtkCellData *GetCellData()    { return ds->GetCellData(); }
    inline vtkPointData *GetPointData()  { return ds->GetPointData(); }

    inline int GetCellType(vtkIdType cellId) const
    {
        return cellPoints.GetCellType(cellId);
    }

    inline vtkIdType *GetCellPoints(vtkIdType cellId, vtkIdType &nCellPoints)
    {
        return cellPoints.GetCellPoints(cellId, nCellPoints);
    }

    inline void GetPoint(vtkIdType index, double pt[3]) const
    {
        points.GetPoint(index, pt);
    }

    inline void GetPoint(vtkIdType i, vtkIdType j, vtkIdType k, double pt[3]) const
    {
        points.GetPoint(i, j, k, pt);
    }

    void ConstructDataSet(vtkVolumeFromVolume &vfv,
                          vtkUnstructuredGrid *output)
    {
        vfv.ConstructDataSet(GetPointData(), GetCellData(), output, 
                             cellPoints.GetDimensions(), X, Y, Z);
    }
private:
    PointGetter      points;
    CellPointsGetter cellPoints;

    vtkRectilinearGrid *ds;
    vtkDataArray *X, *Y, *Z;
};

// ****************************************************************************
// Method: vtkVisItClipper_Algorithm
//
// Purpose: 
//   This is the main algorithm that is applied when we clip. It used to be 
//   in the vtkVisItClipper::Execute method but I templated it so we could
//   support float/double values and coordinates.
//
// Arguments:
//
// Returns:    
//
// Note:       The code to get coordinates and such from the mesh is delegated
//             to a bridge class that implements inline methods for returning
//             the values we want. This is done so we can abstract out the
//             mesh-related things and focus on the clipping algorithm.
//
// Programmer: Jeremy Meredith
// Creation:   February 24, 2010
//
// Modifications:
//   Brad Whitlock, Mon Mar 26 13:43:04 PDT 2012
//   I cut this code out from the Execute() method and templated it on Bridge
//   and ScalarAccess so we can get data from different meshes and precisions
//   without if statements.
//
752 753 754
//   Jeremy Meredith, Mon Jul  9 16:53:41 EDT 2012
//   Added support for 5- through 8-sided polygons.
//
755 756 757 758
//   Kathleen Biagas, Tue Aug 14 11:28:31 MST 2012 
//   Added clipper argument, for access to the ModifyClip method.
//   Evaluation clip function if precomputeClipScalars is false.
//
759 760 761 762 763 764 765
// ****************************************************************************

template <typename Bridge, typename ScalarAccess>
void
vtkVisItClipper_Algorithm(Bridge &bridge, ScalarAccess scalar,
    vtkVisItClipper::FilterState &state,
    vtkUnstructuredGrid *output,
766 767
    vtkUnstructuredGrid *stuff_I_cant_clip,
    vtkVisItClipper *clipper)
768 769 770 771
{
    int t1 = visitTimer->StartTimer();
    vtkIdType nCells = bridge.GetNumberOfCells();
    vtkIdType nPts = bridge.GetNumberOfPoints();
772 773 774 775

    //
    // Do the actual clipping here
    //
776
    vtkIdType ptSizeGuess = (state.CellList == NULL
hrchilds's avatar
hrchilds committed
777
                         ? (int) pow(float(nCells), 0.6667f) * 5 + 100
778
                         : state.CellListSize*5 + 100);
hrchilds's avatar
hrchilds committed
779

780 781 782
    vtkVolumeFromVolume vfvIn(nPts, ptSizeGuess);
    vtkVolumeFromVolume vfvOut(nPts, ptSizeGuess);
    vtkVolumeFromVolume *useVFV = NULL;
hrchilds's avatar
hrchilds committed
783

784
    const int max_pts = 8;
hrchilds's avatar
hrchilds committed
785

786 787 788 789
    vtkIdType nToProcess = (state.CellList != NULL ? state.CellListSize : nCells);
    vtkIdType numIcantClip = 0;

    for (vtkIdType i = 0 ; i < nToProcess ; i++)
hrchilds's avatar
hrchilds committed
790
    {
791
        // Get the cell details
792
        vtkIdType cellId = (state.CellList != NULL ? state.CellList[i] : i);
793
        clipper->ModifyClip(cellId);
794 795 796
        int cellType = bridge.GetCellType(cellId);
        vtkIdType nCellPts = 0;
        vtkIdType *cellPts = bridge.GetCellPoints(cellId, nCellPts);
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

        // If it's something we can't clip, save it for later
        switch (cellType)
        {
          case VTK_TETRA:
          case VTK_PYRAMID:
          case VTK_WEDGE:
          case VTK_HEXAHEDRON:
          case VTK_VOXEL:
          case VTK_TRIANGLE:
          case VTK_QUAD:
          case VTK_PIXEL:
          case VTK_LINE:
          case VTK_VERTEX:
            break;

          default:
814 815 816 817 818 819 820 821
            // we now handle 5-8 sided polygons as well
            if (cellType == VTK_POLYGON &&
                nCellPts >= 5 && nCellPts <= 8)
            {
                break;
            }

            // everything else; defer for other clipper algorithm
822 823 824 825
            {
                if (numIcantClip == 0)
                    stuff_I_cant_clip->GetCellData()->
                                       CopyAllocate(bridge.GetCellData(), nCells);
826

827 828 829 830 831 832 833
                stuff_I_cant_clip->InsertNextCell(cellType, nCellPts, cellPts);
                stuff_I_cant_clip->GetCellData()->
                            CopyData(bridge.GetCellData(), cellId, numIcantClip);
                numIcantClip++;
                continue;
            }
            break;
834
        }
hrchilds's avatar
hrchilds committed
835

836 837
        // fill the dist functions and calculate lookup case
        int lookup_case = 0;
838
        double dist[max_pts];
839 840
        for (int j = nCellPts-1 ; j >= 0 ; j--)
        {
841 842 843 844 845 846 847 848 849 850
            if (state.precomputeClipScalars)
            {
                dist[j] = state.scalarCutoff - scalar.GetTuple1(cellPts[j]);
            }
            else
            {
                double pt[3];
                bridge.GetPoint(cellPts[j], pt);
                dist[j] = -state.clipFunction->EvaluateFunction(pt[0],pt[1],pt[2]);
            }
hrchilds's avatar
hrchilds committed
851

hrchilds's avatar
hrchilds committed
852 853 854 855 856
            if (dist[j] >= 0)
                lookup_case++;
            if (j > 0)
                lookup_case *= 2;
        }
hrchilds's avatar
hrchilds committed
857

858
        if (state.removeWholeCells && lookup_case != 0)
hrchilds's avatar
hrchilds committed
859 860
            lookup_case = ((1 << nCellPts) - 1);

861 862 863 864 865 866 867
        unsigned char  *splitCase = NULL;
        int             numOutput = 0;
        typedef int     edgeIndices[2];
        edgeIndices    *vertices_from_edges = NULL;

        int startIndex;
        switch (cellType)
hrchilds's avatar
hrchilds committed
868
        {
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
          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_VOXEL:
            startIndex = startClipShapesVox[lookup_case];
            splitCase  = &clipShapesVox[startIndex];
            numOutput  = numClipShapesVox[lookup_case];
            vertices_from_edges = voxVerticesFromEdges;
            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;
          case VTK_PIXEL:
            startIndex = startClipShapesPix[lookup_case];
            splitCase  = &clipShapesPix[startIndex];
            numOutput  = numClipShapesPix[lookup_case];
            vertices_from_edges = pixelVerticesFromEdges;
            break;
          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;
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
          case VTK_POLYGON:
            switch (nCellPts)
            {
              case 5:
                startIndex = startClipShapesPoly5[lookup_case];
                splitCase  = &clipShapesPoly5[startIndex];
                numOutput  = numClipShapesPoly5[lookup_case];
                vertices_from_edges = poly5VerticesFromEdges;
                break;
              case 6:
                startIndex = startClipShapesPoly6[lookup_case];
                splitCase  = &clipShapesPoly6[startIndex];
                numOutput  = numClipShapesPoly6[lookup_case];
                vertices_from_edges = poly6VerticesFromEdges;
                break;
              case 7:
                startIndex = startClipShapesPoly7[lookup_case];
                splitCase  = &clipShapesPoly7[startIndex];
                numOutput  = numClipShapesPoly7[lookup_case];
                vertices_from_edges = poly7VerticesFromEdges;
                break;
              case 8:
                startIndex = startClipShapesPoly8[lookup_case];
                splitCase  = &clipShapesPoly8[startIndex];
                numOutput  = numClipShapesPoly8[lookup_case];
                vertices_from_edges = poly8VerticesFromEdges;
                break;
            }
            break;
hrchilds's avatar
hrchilds committed
958 959
        }

960 961
        int            interpIDsIn[4];
        int            interpIDsOut[4];
962
        for (int j = 0 ; j < numOutput ; j++)
hrchilds's avatar
hrchilds committed
963 964 965 966
        {
            unsigned char shapeType = *splitCase++;
            {
                int npts;
967
                int interpID = -1;
hrchilds's avatar
hrchilds committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
                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
987 988 989 990 991 992 993 994
                  case ST_QUA:
                    npts = 4;
                    color = *splitCase++;
                    break;
                  case ST_TRI:
                    npts = 3;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
995 996 997 998 999 1000 1001 1002
                  case ST_LIN:
                    npts = 2;
                    color = *splitCase++;
                    break;
                  case ST_VTX:
                    npts = 1;
                    color = *splitCase++;
                    break;
hrchilds's avatar
hrchilds committed
1003
                  case ST_PNT:
1004
                    interpID = *splitCase++;
hrchilds's avatar
hrchilds committed
1005
                    color    = *splitCase++;
hrchilds's avatar
hrchilds committed
1006 1007 1008 1009 1010
                    npts     = *splitCase++;
                    break;
                  default:
                    EXCEPTION1(ImproperUseException,
                               "An invalid output shape was found in "
hrchilds's avatar
hrchilds committed
1011
                               "the ClipCases.");
hrchilds's avatar
hrchilds committed
1012 1013
                }

1014 1015
                bool out = ((!state.insideOut && color == COLOR0) ||
                            ( state.insideOut && color == COLOR1));
1016
                useVFV = &vfvIn;
1017
                if (out)
hrchilds's avatar
hrchilds committed
1018
                {
1019
                    if (state.computeInsideAndOut)
1020 1021 1022 1023 1024 1025 1026 1027 1028
                    {
                        useVFV = &vfvOut;
                    }
                    else
                    {
                        // We don't want this one; it's the wrong side.
                        splitCase += npts;
                        continue;
                    }
hrchilds's avatar
hrchilds committed
1029 1030
                }

1031
                vtkIdType shape[8];
hrchilds's avatar
hrchilds committed
1032 1033 1034
                for (int p = 0 ; p < npts ; p++)
                {
                    unsigned char pt = *splitCase++;
hrchilds's avatar
hrchilds committed
1035
                    if (pt <= P7)
hrchilds's avatar
hrchilds committed
1036
                    {
hrchilds's avatar
hrchilds committed
1037 1038 1039
                        // 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.
1040
                        shape[p] = cellPts[pt];
hrchilds's avatar
hrchilds committed
1041 1042 1043
                    }
                    else if (pt >= EA && pt <= EL)
                    {
1044 1045
                        int pt1 = vertices_from_edges[pt-EA][0];
                        int pt2 = vertices_from_edges[pt-EA][1];
hrchilds's avatar
hrchilds committed
1046 1047 1048 1049 1050 1051
                        if (pt2 < pt1)
                        {
                            int tmp = pt2;
                            pt2 = pt1;
                            pt1 = tmp;
                        }
1052 1053 1054
                        double dir = dist[pt2] - dist[pt1];
                        double amt = 0. - dist[pt1];
                        double percent = 1. - (amt / dir);
hrchilds's avatar
hrchilds committed
1055 1056 1057 1058

                        // We may have physically (though not logically)
                        // degenerate cells if percent==0 or percent==1.
                        // We could pretty easily and mostly safely clamp
1059
                        // percent to the range [1e-4, 1. - 1e-4] here.
1060 1061
                        vtkIdType ptId1 = cellPts[pt1];
                        vtkIdType ptId2 = cellPts[pt2];
hrchilds's avatar
hrchilds committed
1062 1063

                        // deal with exact zero crossings if requested
1064
                        if (state.clipFunction != NULL && state.useZeroCrossings)
1065
                        {
1066 1067 1068 1069 1070 1071 1072
                            double p0[3], p1[3];
                            bridge.GetPoint(ptId1, p0);
                            bridge.GetPoint(ptId2, p1);

                            AdjustPercentToZeroCrossing(p0, p1,
                                                        state.clipFunction,
                                                        &percent);
1073
                        }
hrchilds's avatar
hrchilds committed
1074
                                
1075
                        shape[p] = useVFV->AddPoint(ptId1, ptId2, percent);
hrchilds's avatar
hrchilds committed
1076 1077 1078
                    }
                    else if (pt >= N0 && pt <= N3)
                    {
1079 1080 1081 1082
                        if (useVFV == &vfvIn)
                            shape[p] = interpIDsIn[pt - N0];
                        else
                            shape[p] = interpIDsOut[pt - N0];
hrchilds's avatar
hrchilds committed
1083 1084 1085 1086 1087
                    }
                    else
                    {
                        EXCEPTION1(ImproperUseException,
                                   "An invalid output point value "
hrchilds's avatar
hrchilds committed
1088
                                   "was found in the ClipCases.");
hrchilds's avatar
hrchilds committed
1089 1090 1091 1092 1093 1094
                    }
                }

                switch (shapeType)
                {
                  case ST_HEX:
1095
                    useVFV->AddHex(cellId,
1096 1097
                                   shape[0], shape[1], shape[2], shape[3],
                                   shape[4], shape[5], shape[6], shape[7]);
hrchilds's avatar
hrchilds committed
1098 1099
                    break;
                  case ST_WDG:
1100
                    useVFV->AddWedge(cellId,
1101 1102
                                     shape[0], shape[1], shape[2],
                                     shape[3], shape[4], shape[5]);
hrchilds's avatar
hrchilds committed
1103 1104
                    break;
                  case ST_PYR:
1105
                    useVFV->AddPyramid(cellId, shape[0], shape[1],
1106
                                       shape[2], shape[3], shape[4]);
hrchilds's avatar
hrchilds committed
1107 1108
                    break;
                  case ST_TET:
1109
                    useVFV->AddTet(cellId, shape[0], shape[1], shape[2], shape[3]);
hrchilds's avatar
hrchilds committed
1110
                    break;
hrchilds's avatar
hrchilds committed
1111
                  case ST_QUA:
1112
                    useVFV->AddQuad(cellId, shape[0], shape[1], shape[2], shape[3]);
hrchilds's avatar
hrchilds committed
1113 1114
                    break;
                  case ST_TRI:
1115
                    useVFV->AddTri(cellId, shape[0], shape[1], shape[2]);
hrchilds's avatar
hrchilds committed
1116
                    break;
hrchilds's avatar
hrchilds committed
1117
                  case ST_LIN:
1118
                    useVFV->AddLine(cellId, shape[0], shape[1]);
hrchilds's avatar
hrchilds committed
1119 1120
                    break;
                  case ST_VTX:
1121
                    useVFV->AddVertex(cellId, shape[0]);
hrchilds's avatar
hrchilds committed
1122
                    break;
hrchilds's avatar
hrchilds committed
1123
                  case ST_PNT:
1124
                    interpIDsIn[interpID] = vfvIn.AddCentroidPoint(npts, shape);
1125
                    if (state.computeInsideAndOut)
1126
                        interpIDsOut[interpID] = vfvOut.AddCentroidPoint(npts, shape);
hrchilds's avatar
hrchilds committed
1127 1128 1129 1130 1131
                    break;
                }
            }
        }
    }
1132
    visitTimer->StopTimer(t1, "Performing clipping");
1133 1134 1135 1136

    //
    // Construct the output data set.
    //
1137
    int t2 = visitTimer->StartTimer();
1138
    if (numIcantClip > 0)
1139
    {
1140
        int t3 = visitTimer->StartTimer();
1141
        vtkUnstructuredGrid *not_from_zoo  = vtkUnstructuredGrid::New();
1142 1143 1144
        state.ClipDataset(stuff_I_cant_clip, not_from_zoo);
        visitTimer->StopTimer(t3, "Clipping non-zoo cells");

1145
        vtkUnstructuredGrid *just_from_zoo = vtkUnstructuredGrid::New();
1146
        bridge.ConstructDataSet(vfvIn, just_from_zoo);
hrchilds's avatar
hrchilds committed
1147

1148 1149 1150 1151
        vtkAppendFilter *appender = vtkAppendFilter::New();
        appender->AddInput(not_from_zoo);
        appender->AddInput(just_from_zoo);
        appender->GetOutput()->Update();
hrchilds's avatar
hrchilds committed
1152

1153
        output->ShallowCopy(appender->GetOutput());
hrchilds's avatar
hrchilds committed
1154

1155
        if (state.computeInsideAndOut)
hrchilds's avatar
hrchilds committed
1156
        {
1157 1158
            appender->RemoveInput(just_from_zoo);
            just_from_zoo->Delete();
hrchilds's avatar
hrchilds committed
1159

1160
            just_from_zoo = vtkUnstructuredGrid::New();
1161
            bridge.ConstructDataSet(vfvOut, just_from_zoo);
hrchilds's avatar
hrchilds committed
1162

1163 1164
            appender->AddInput(just_from_zoo);
            appender->GetOutput()->Update();
hrchilds's avatar
hrchilds committed
1165

1166 1167 1168 1169
            if (state.otherOutput != NULL)
                state.otherOutput->Delete();
            state.otherOutput = vtkUnstructuredGrid::New();
            state.otherOutput->ShallowCopy(appender->GetOutput());
hrchilds's avatar
hrchilds committed
1170
        }
1171 1172 1173 1174 1175 1176 1177

        appender->Delete();
        just_from_zoo->Delete();
        not_from_zoo->Delete();
    }
    else
    {
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
        bridge.ConstructDataSet(vfvIn, output);

        if (state.computeInsideAndOut)
        {
            if (state.otherOutput)
                state.otherOutput->Delete();
            state.otherOutput = vtkUnstructuredGrid::New();
            bridge.ConstructDataSet(vfvOut, state.otherOutput);
        }
    }
    visitTimer->StopTimer(t2, "VFV Constructing data set");
}

// ****************************************************************************
// Method: vtkVisItClipper_RectExecute
//
// Purpose: 
//   Execute the clipping algorithm to different data via a bridge thet lets
//   us deal with different mesh types and coordinate precisions. This function
//   sets up structured meshes specially.
//
// Arguments:
//
// Returns:    
//
// Note:       Same
//
// Programmer: Brad Whitlock
// Creation:   Mon Mar 26 13:52:27 PDT 2012
//
// Modifications:
1209 1210 1211
//   Kathleen Biagas, Tue Aug 14 11:28:31 MST 2012 
//   Added clipper argument, for access to the ModifyClip method.
//   Only evaluate clip function if precomputeClipScalars is true.
1212 1213 1214 1215 1216 1217 1218 1219
//   
// ****************************************************************************

template <typename Bridge>
void
vtkVisItClipper_RectExecute(Bridge bridge, const int *pt_dims,
    vtkVisItClipper::FilterState &state,
    vtkUnstructuredGrid *output,
1220 1221
    vtkUnstructuredGrid *stuff_I_cant_clip,
    vtkVisItClipper *clipper) 
1222 1223 1224 1225 1226 1227
{
    if(state.clipFunction != NULL)
    {
        double *scalar = new double[bridge.GetNumberOfPoints()];
        vtkAccessor<double> scalarAccess(scalar);
        vtkIdType id = 0;
1228
        if (state.precomputeClipScalars)
1229
        {
1230
            for (vtkIdType k=0; k<pt_dims[2]; k++)
1231
            {
1232
                for (vtkIdType j=0; j<pt_dims[1]; j++)
1233
                {
1234 1235 1236 1237 1238 1239
                    for (vtkIdType i=0; i<pt_dims[0]; i++, id++)
                    {
                        double pt[3];
                        bridge.GetPoint(i,j,k,pt);
                        scalar[id] = -state.clipFunction->EvaluateFunction(pt[0],pt[1],pt[2]);
                    }
1240 1241 1242 1243
                }
            }
        }

1244 1245
        vtkVisItClipper_Algorithm(bridge, scalarAccess, state, output, 
            stuff_I_cant_clip, clipper);
1246 1247 1248 1249 1250 1251
        delete [] scalar;
    }
    else if(state.scalarArrayAsVTK != NULL)
    {
        int dt = state.scalarArrayAsVTK->GetDataType();
        if(dt == VTK_FLOAT)
1252 1253 1254
            vtkVisItClipper_Algorithm(bridge, 
                vtkAccessor<float>(state.scalarArrayAsVTK),
                state, output, stuff_I_cant_clip, clipper);
1255
        else if(dt == VTK_DOUBLE)
1256 1257 1258
            vtkVisItClipper_Algorithm(bridge, 
                vtkAccessor<double>(state.scalarArrayAsVTK),
                state, output, stuff_I_cant_clip, clipper);
hrchilds's avatar
hrchilds committed
1259
        else
1260 1261 1262
            vtkVisItClipper_Algorithm(bridge, 
                vtkGeneralAccessor(state.scalarArrayAsVTK),
                state, output, stuff_I_cant_clip, clipper);
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
    }
}

// ****************************************************************************
// Method: vtkVisItClipper_Execute
//
// Purpose: 
//   Execute the clipping algorithm to different data via a bridge thet lets
//   us deal with different mesh types and coordinate precisions. This function
//   sets up unstructured meshes specially.
//
// Arguments:
//
// Returns:    
//
// Note:       Same as structured except that point traversal is 0..N.
//
// Programmer: Brad Whitlock
// Creation:   Mon Mar 26 13:52:27 PDT 2012
//
// Modifications:
1284 1285 1286
//   Kathleen Biagas, Tue Aug 14 11:28:31 MST 2012 
//   Added clipper argument, for access to the ModifyClip method.
//   Only evaluate clip function if precomputeClipScalars is true.
1287 1288
//   
// ****************************************************************************
1289

1290 1291 1292 1293 1294
template <typename Bridge>
void
vtkVisItClipper_Execute(Bridge bridge,
    vtkVisItClipper::FilterState &state,
    vtkUnstructuredGrid *output,
1295 1296
    vtkUnstructuredGrid *stuff_I_cant_clip,
    vtkVisItClipper *clipper)
1297 1298 1299 1300 1301
{
    if(state.clipFunction != NULL)
    {
        vtkIdType nPts = bridge.GetNumberOfPoints();
        double *scalar = new double[nPts];
1302
        if (state.precomputeClipScalars)
1303
        {
1304 1305 1306 1307 1308 1309
            for (vtkIdType i=0; i < nPts; i++)
            {
                double pt[3];
                bridge.GetPoint(i, pt);
                scalar[i] = -state.clipFunction->EvaluateFunction(pt[0],pt[1],pt[2]);
            }
1310 1311 1312
        }

        vtkVisItClipper_Algorithm(bridge, vtkAccessor<double>(scalar), 
1313
                                  state, output, stuff_I_cant_clip, clipper);
1314 1315 1316 1317 1318 1319
        delete [] scalar;
    }
    else if(state.scalarArrayAsVTK != NULL)
    {
        int dt = state.scalarArrayAsVTK->GetDataType();
        if(dt == VTK_FLOAT)
1320 1321 1322
            vtkVisItClipper_Algorithm(bridge, 
                 vtkAccessor<float>(state.scalarArrayAsVTK),
                 state, output, stuff_I_cant_clip, clipper);
1323
        else if(dt == VTK_DOUBLE)
1324 1325 1326
            vtkVisItClipper_Algorithm(bridge, 
                 vtkAccessor<double>(state.scalarArrayAsVTK),
                 state, output, stuff_I_cant_clip, clipper);
1327
        else
1328 1329 1330
            vtkVisItClipper_Algorithm(bridge, 
                 vtkGeneralAccessor(state.scalarArrayAsVTK),
                  state, output, stuff_I_cant_clip, clipper);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
    }
}

// ****************************************************************************
//  Method:  vtkVisItClipper::Execute
//
//  Purpose:
//    Main execution method.  
//
//  Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 24, 2010
//
//  Modifications:
//    Jeremy Meredith, Wed Feb 24 10:18:33 EST 2010
//    Initial creation: unified the old rectilinear, structured, unstructured,
//    and polydata execution functions into this single function.
//
//    Jeremy Meredith, Thu Feb 25 11:08:03 EST 2010
//    Don't forget to exit early if we have a dataset we can't understand.
//
//    Jeremy Meredith, Thu Feb 25 15:14:28 EST 2010
//    Allowing clipFunction usage to precalculate (most) values.  This
//    saves a good chunk of time in this mode since we were re-calculating
//    these values a number of times.
//
//    Brad Whitlock, Mon Mar 26 13:26:44 PDT 2012
//    Massive refactor to support float/double values and coordinates.
//
// ****************************************************************************
1363

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
void
vtkVisItClipper::Execute()
{
    vtkDataSet *ds = GetInput();
    int t0 = visitTimer->StartTimer();
    int do_type = ds->GetDataObjectType();

    // Set general input/output data
    vtkUnstructuredGrid *output = (vtkUnstructuredGrid*)GetOutput();

    if (do_type == VTK_RECTILINEAR_GRID || do_type == VTK_STRUCTURED_GRID)
    {
        vtkUnstructuredGrid *stuff_I_cant_clip = vtkUnstructuredGrid::New();
        int pt_dims[3] = {0,0,0};
        if (do_type == VTK_RECTILINEAR_GRID)
hrchilds's avatar
hrchilds committed
1379
        {
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
            vtkRectilinearGrid *rg = (vtkRectilinearGrid*)ds;
            rg->GetDimensions(pt_dims);

            vtkDataArray *X = rg->GetXCoordinates();
            vtkDataArray *Y = rg->GetYCoordinates();
            vtkDataArray *Z = rg->GetZCoordinates();
            int tx = X->GetDataType();
            int ty = Y->GetDataType();
            int tz = Z->GetDataType();
            bool same = (tx == ty) && (ty == tz);
            if(same && tx == VTK_FLOAT)
            {
1392 1393
                ClipperBridgeRectilinearGrid<vtkRectPointAccessor<float> > 
                    bridge(rg, pt_dims, X, Y, Z);
1394
                vtkVisItClipper_RectExecute(bridge, pt_dims, 
1395
                    this->state, output, stuff_I_cant_clip, this);
1396 1397 1398
            }
            else if(same && tx == VTK_DOUBLE)
            {
1399 1400
                ClipperBridgeRectilinearGrid<vtkRectPointAccessor<double> > 
                    bridge(rg, pt_dims, X, Y, Z);
1401
                vtkVisItClipper_RectExecute(bridge, pt_dims, 
1402
                    this->state, output, stuff_I_cant_clip, this);
1403 1404 1405
            }
            else
            {
1406 1407
                ClipperBridgeRectilinearGrid<vtkGeneralRectPointAccessor> 
                    bridge(rg, pt_dims, X, Y, Z);
1408
                vtkVisItClipper_RectExecute(bridge, pt_dims, 
1409
                    this->state, output, stuff_I_cant_clip, this);
1410 1411 1412 1413 1414 1415 1416 1417
            }
        }
        else // do_type == VTK_STRUCTURED_GRID
        {
            vtkStructuredGrid *sg = (vtkStructuredGrid*)ds;
            sg->GetDimensions(pt_dims);
            if(sg->GetPoints()->GetDataType() == VTK_FLOAT)
            {
1418 1419 1420 1421
                ClipperBridgeStructuredGrid<vtkPointAccessor<float> > 
                    bridge(sg);
                vtkVisItClipper_Execute(bridge,  this->state, output, 
                    stuff_I_cant_clip, this);
1422 1423 1424 1425
            }
            else if(sg->GetPoints()->GetDataType() == VTK_FLOAT)
            {
                ClipperBridgeStructuredGrid<vtkPointAccessor<double> > bridge(sg);
1426 1427
                vtkVisItClipper_Execute(bridge,  this->state, output, 
                    stuff_I_cant_clip, this);
1428 1429
            }
/* This case probably does not happen...
1430
            else
1431 1432
            {
                ClipperBridge<vtkGeneralPointAccessor> bridge(sg);
1433 1434
                vtkVisItClipper_Execute(bridge, this->state, output,
                    stuff_I_cant_clip, this);
1435 1436
            }
*/
hrchilds's avatar
hrchilds committed
1437
        }
1438 1439

        stuff_I_cant_clip->Delete();
1440
    }
1441 1442 1443
    else if (do_type == VTK_UNSTRUCTURED_GRID)
    {
        vtkUnstructuredGrid *ug = (vtkUnstructuredGrid *)ds;
hrchilds's avatar
hrchilds committed
1444

1445 1446 1447 1448 1449 1450 1451 1452
        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(ug->GetNumberOfCells());

        if(ug->GetPoints()->GetDataType() == VTK_FLOAT)
        {
            ClipperBridgeUnstructuredGrid<vtkPointAccessor<float> > bridge(ug);
1453 1454
            vtkVisItClipper_Execute(bridge, this->state, output, 
                stuff_I_cant_clip, this);
1455 1456 1457 1458
        }
        else if(ug->GetPoints()->GetDataType() == VTK_DOUBLE)
        {
            ClipperBridgeUnstructuredGrid<vtkPointAccessor<double> > bridge(ug);
1459 1460
            vtkVisItClipper_Execute(bridge, this->state, output, 
                stuff_I_cant_clip, this);
1461 1462 1463 1464 1465
        }
/* This case probably does not happen...
        else
        {
            ClipperBridgeUnstructuredGrid<vtkGeneralPointAccessor> bridge(ug);
1466 1467
            vtkVisItClipper_UnstructuredExecute(bridge, this->state, output, 
                stuff_I_cant_clip, this);
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
        }
*/

        stuff_I_cant_clip->Delete();
    }
    else if (do_type == VTK_POLY_DATA)
    {
        vtkPolyData *pd = (vtkPolyData *)ds;

        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(pd->GetNumberOfCells());

        if(pd->GetPoints()->GetDataType() == VTK_FLOAT)
        {
            ClipperBridgePolyData<vtkPointAccessor<float> > bridge(pd);
1485 1486
            vtkVisItClipper_