vtkVisItClipper.C 51.5 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
3
* Copyright (c) 2000 - 2014, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39
#include "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>
45 46
#include <vtkInformation.h>
#include <vtkInformationVector.h>
hrchilds's avatar
hrchilds committed
47
#include <vtkObjectFactory.h>
hrchilds's avatar
hrchilds committed
48
#include <vtkPlane.h>
hrchilds's avatar
hrchilds committed
49
#include <vtkPointData.h>
50
#include <vtkPointSet.h>
hrchilds's avatar
hrchilds committed
51
#include <vtkPolyData.h>
hrchilds's avatar
hrchilds committed
52
#include <vtkQuadric.h>
hrchilds's avatar
hrchilds committed
53 54 55
#include <vtkRectilinearGrid.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>
hrchilds's avatar
hrchilds committed
56 57
#include <vtkVolumeFromVolume.h>

58 59
#include <vtkAccessors.h>

hrchilds's avatar
hrchilds committed
60 61 62
#include <ImproperUseException.h>

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

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

hrchilds's avatar
hrchilds committed
68
#include <ClipCases.h>
hrchilds's avatar
hrchilds committed
69 70
#include <vtkTriangulationTables.h>

hrchilds's avatar
hrchilds committed
71
vtkStandardNewMacro(vtkVisItClipper);
hrchilds's avatar
hrchilds committed
72

73 74
// ****************************************************************************
//  Function: AdjustPercentToZeroCrossing
hrchilds's avatar
hrchilds committed
75
//
76 77 78
//    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
hrchilds's avatar
hrchilds committed
79 80
//
//  Programmer: Mark C. Miller
bonnell's avatar
bonnell committed
81
//  Creation:   December 3, 2006
hrchilds's avatar
hrchilds committed
82
//
83 84 85
//  Modifications:
//    Brad Whitlock, Fri Mar 23 17:13:49 PDT 2012
//    Pass in points.
86
//
87
// ****************************************************************************
hrchilds's avatar
hrchilds committed
88
static void
89 90
AdjustPercentToZeroCrossing(double p0[3], double p1[3], 
    vtkImplicitFunction *func, double *percent)
hrchilds's avatar
hrchilds committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
{
    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
137
    // there is an asymmetry in it where the coefficient of the a[5] term does
hrchilds's avatar
hrchilds committed
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 183 184
    // 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;
}

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

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
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;
207
    this->precomputeClipScalars = true;
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
}

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();
273
    clipData->SetInputData(in_ds);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    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
294 295 296 297 298 299 300 301 302 303
// ****************************************************************************
//  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.
//
304 305 306
//    Hank Childs, Sat Sep 29 11:14:58 PDT 2007
//    Initialize new data members.
//
307 308 309
//    Brad Whitlock, Tue Mar 27 12:02:00 PDT 2012
//    Move members to state.
//
hrchilds's avatar
hrchilds committed
310
// ****************************************************************************
311

312
vtkVisItClipper::vtkVisItClipper() : vtkUnstructuredGridAlgorithm(), state()
hrchilds's avatar
hrchilds committed
313 314 315
{
}

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

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

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

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

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

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

371
    state.SetClipFunction(func);
hrchilds's avatar
hrchilds committed
372 373
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
// ****************************************************************************
// 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
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
// ****************************************************************************
//  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:
//    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.
//
413 414 415 416
//    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".
//
417 418 419
//    Brad Whitlock, Tue Mar 27 11:51:35 PDT 2012
//    Move to FilterState.
//
hrchilds's avatar
hrchilds committed
420
// ****************************************************************************
421

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

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

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

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

hrchilds's avatar
hrchilds committed
460
// ****************************************************************************
461
//  Class: CellPointsGetter
hrchilds's avatar
hrchilds committed
462
//
463 464
//  Purpose:
//    Provides a GetCellPoints method for structured meshes.
hrchilds's avatar
hrchilds committed
465
//
466 467
//  Notes:      This class is separated out so it can be used for both the
//              Structured and Rectilinear bridge classes.
hrchilds's avatar
hrchilds committed
468
//
469 470 471 472
//  Programmer: Brad Whitlock
//  Creation:   Thu Mar 29 14:35:21 PDT 2012
//
//  Modifications:
hrchilds's avatar
hrchilds committed
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
// ****************************************************************************

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;
};

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

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

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

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

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

583 584 585 586 587 588 589 590 591 592
//
// 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
593
    }
594 595

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

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

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

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

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

// ****************************************************************************
662 663 664 665 666
//  Class: ClipperBridgeRectilinearGrid
//
//  Purpose:
//    This class is bridge that provides methods to access cells and points for
//    rectilinear grids.
667
//
668 669
//  Notes:      The class is templated on PointGetter so we can access point
//              data directly as memory or using GetTuple1 calls.
670
//
671 672
//  Programmer: Brad Whitlock
//  Creation:   Mon Mar 26 13:38:48 PDT 2012
673
//
674
//  Modifications:
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
//
// ****************************************************************************

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;
};

// ****************************************************************************
728
//  Method: vtkVisItClipper_Algorithm
729
//
730 731 732 733
//  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.
734
//
735
//  Arguments:
736
//
737
//  Returns:    
738
//
739 740 741 742
//  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.
743
//
744 745
//  Programmer: Jeremy Meredith
//  Creation:   February 24, 2010
746
//
747 748 749 750 751
//  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,
766
    vtkDataSet *input, vtkUnstructuredGrid *output,
767 768
    vtkUnstructuredGrid *stuff_I_cant_clip,
    vtkVisItClipper *clipper)
769 770 771 772
{
    int t1 = visitTimer->StartTimer();
    vtkIdType nCells = bridge.GetNumberOfCells();
    vtkIdType nPts = bridge.GetNumberOfPoints();
773 774 775 776

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

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

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

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

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

        // 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:
815 816 817 818 819 820 821 822
            // 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
823 824 825 826
            {
                if (numIcantClip == 0)
                    stuff_I_cant_clip->GetCellData()->
                                       CopyAllocate(bridge.GetCellData(), nCells);
827

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

837 838
        // fill the dist functions and calculate lookup case
        int lookup_case = 0;
839
        double dist[max_pts];
840 841
        for (int j = nCellPts-1 ; j >= 0 ; j--)
        {
842 843 844 845 846 847 848 849 850 851
            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
852

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

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

862 863 864 865 866 867 868
        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
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 929
          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;
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 958
          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
959 960
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

1164 1165
            appender->AddInputData(just_from_zoo);
            appender->Update();
hrchilds's avatar
hrchilds committed
1166

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

        appender->Delete();
        just_from_zoo->Delete();
        not_from_zoo->Delete();
    }
    else
    {
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
        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");
}

// ****************************************************************************
1193
//  Method: vtkVisItClipper_RectExecute
1194
//
1195 1196 1197 1198
//  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.
1199
//
1200
//  Arguments:
1201
//
1202
//  Returns:    
1203
//
1204
//  Note:       Same
1205
//
1206 1207 1208 1209 1210 1211 1212
//  Programmer: Brad Whitlock
//  Creation:   Mon Mar 26 13:52:27 PDT 2012
//
//  Modifications:
//    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.
1213 1214 1215 1216 1217 1218 1219
//
// ****************************************************************************

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

1245
        vtkVisItClipper_Algorithm(bridge, scalarAccess, state, input, output, 
1246
            stuff_I_cant_clip, clipper);
1247 1248 1249 1250 1251 1252
        delete [] scalar;
    }
    else if(state.scalarArrayAsVTK != NULL)
    {
        int dt = state.scalarArrayAsVTK->GetDataType();
        if(dt == VTK_FLOAT)
1253 1254
            vtkVisItClipper_Algorithm(bridge, 
                vtkAccessor<float>(state.scalarArrayAsVTK),
1255
                state, input, output, stuff_I_cant_clip, clipper);
1256
        else if(dt == VTK_DOUBLE)
1257 1258
            vtkVisItClipper_Algorithm(bridge, 
                vtkAccessor<double>(state.scalarArrayAsVTK),
1259
                state, input, output, stuff_I_cant_clip, clipper);
hrchilds's avatar
hrchilds committed
1260
        else
1261 1262
            vtkVisItClipper_Algorithm(bridge, 
                vtkGeneralAccessor(state.scalarArrayAsVTK),
1263
                state, input, output, stuff_I_cant_clip, clipper);
1264 1265 1266 1267
    }
}

// ****************************************************************************
1268
//  Method: vtkVisItClipper_Execute
1269
//
1270 1271 1272 1273
//  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.
1274
//
1275
//  Arguments:
1276
//
1277
//  Returns:    
1278
//
1279
//  Note:       Same as structured except that point traversal is 0..N.
1280
//
1281 1282 1283 1284 1285 1286 1287
//  Programmer: Brad Whitlock
//  Creation:   Mon Mar 26 13:52:27 PDT 2012
//
//  Modifications:
//    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.
1288 1289
//
// ****************************************************************************
1290

1291 1292 1293 1294
template <typename Bridge>
void
vtkVisItClipper_Execute(Bridge bridge,
    vtkVisItClipper::FilterState &state,
1295
    vtkDataSet *input, vtkUnstructuredGrid *output,
1296 1297
    vtkUnstructuredGrid *stuff_I_cant_clip,
    vtkVisItClipper *clipper)
1298 1299 1300 1301 1302
{
    if(state.clipFunction != NULL)
    {
        vtkIdType nPts = bridge.GetNumberOfPoints();
        double *scalar = new double[nPts];
1303
        if (state.precomputeClipScalars)
1304
        {
1305 1306 1307 1308 1309 1310
            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]);
            }
1311 1312 1313
        }

        vtkVisItClipper_Algorithm(bridge, vtkAccessor<double>(scalar), 
1314
                                  state, input, output, stuff_I_cant_clip, clipper);
1315 1316 1317 1318 1319 1320
        delete [] scalar;
    }
    else if(state.scalarArrayAsVTK != NULL)
    {
        int dt = state.scalarArrayAsVTK->GetDataType();
        if(dt == VTK_FLOAT)
1321 1322
            vtkVisItClipper_Algorithm(bridge, 
                 vtkAccessor<float>(state.scalarArrayAsVTK),
1323
                 state, input, output, stuff_I_cant_clip, clipper);
1324
        else if(dt == VTK_DOUBLE)
1325 1326
            vtkVisItClipper_Algorithm(bridge, 
                 vtkAccessor<double>(state.scalarArrayAsVTK),
1327
                 state, input, output, stuff_I_cant_clip, clipper);
1328
        else
1329 1330
            vtkVisItClipper_Algorithm(bridge, 
                 vtkGeneralAccessor(state.scalarArrayAsVTK),
1331
                  state, input, output, stuff_I_cant_clip, clipper);
1332 1333 1334 1335
    }
}

// ****************************************************************************
1336
//  Method:  vtkVisItClipper::RequestData
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
//
//  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
//    Eric Brugger, Thu Jul 18 17:03:54 PDT 2013
//    I corrected a bug that caused the clipping of double precision
//    structured grids to return zero cells.
//
1367
// ****************************************************************************
1368

1369 1370 1371 1372 1373
int
vtkVisItClipper::RequestData(
    vtkInformation *vtkNotUsed(request),
    vtkInformationVector **inputVector,
    vtkInformationVector *outputVector)
1374
{
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    // get the info objects
    vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
    vtkInformation *outInfo = outputVector->GetInformationObject(0);

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

1387 1388 1389 1390 1391 1392 1393 1394
    int t0 = visitTimer->StartTimer();
    int do_type = ds->GetDataObjectType();

    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
1395
        {
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
            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)
            {
1408 1409
                ClipperBridgeRectilinearGrid<vtkRectPointAccessor<float> > 
                    bridge(rg, pt_dims, X, Y, Z);
1410
                vtkVisItClipper_RectExecute(bridge, pt_dims, 
1411
                    this->state, ds, output, stuff_I_cant_clip, this);
1412 1413 1414
            }
            else if(same && tx == VTK_DOUBLE)
            {
1415 1416
                ClipperBridgeRectilinearGrid<vtkRectPointAccessor<double> > 
                    bridge(rg, pt_dims, X, Y, Z);
1417
                vtkVisItClipper_RectExecute(bridge, pt_dims, 
1418
                    this->state, ds, output, stuff_I_cant_clip, this);
1419 1420 1421
            }
            else
            {
1422 1423
                ClipperBridgeRectilinearGrid<vtkGeneralRectPointAccessor> 
                    bridge(rg, pt_dims, X, Y, Z);
1424
                vtkVisItClipper_RectExecute(bridge, pt_dims, 
1425
                    this->state, ds, output, stuff_I_cant_clip, this);
1426 1427 1428 1429 1430 1431 1432 1433
            }
        }
        else // do_type == VTK_STRUCTURED_GRID
        {
            vtkStructuredGrid *sg = (vtkStructuredGrid*)ds;
            sg->GetDimensions(pt_dims);
            if(sg->GetPoints()->GetDataType() == VTK_FLOAT)
            {
1434 1435
                ClipperBridgeStructuredGrid<vtkPointAccessor<float> > 
                    bridge(sg);
1436
                vtkVisItClipper_Execute(bridge,  this->state, ds, output, 
1437
                    stuff_I_cant_clip, this);
1438
            }
1439
            else if(sg->GetPoints()->GetDataType() == VTK_DOUBLE)
1440 1441
            {
                ClipperBridgeStructuredGrid<vtkPointAccessor<double> > bridge(sg);
1442
                vtkVisItClipper_Execute(bridge,  this->state, ds, output, 
1443
                    stuff_I_cant_clip, this);
1444 1445
            }
/* This case probably does not happen...
1446
            else
1447 1448
            {
                ClipperBridge<vtkGeneralPointAccessor> bridge(sg);
1449
                vtkVisItClipper_Execute(bridge, this->state, ds, output,
1450
                    stuff_I_cant_clip, this);
1451 1452
            }
*/
hrchilds's avatar
hrchilds committed
1453
        }
1454 1455

        stuff_I_cant_clip->Delete();
1456
    }
1457 1458 1459
    else if (do_type == VTK_UNSTRUCTURED_GRID)
    {
        vtkUnstructuredGrid *ug = (vtkUnstructuredGrid *)ds;
hrchilds's avatar
hrchilds committed
1460

1461 1462 1463 1464 1465 1466 1467 1468
        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);
1469
            vtkVisItClipper_Execute(bridge, this->state, ds, output, 
1470
                stuff_I_cant_clip, this);
1471 1472 1473 1474
        }
        else if(ug->GetPoints()->GetDataType() == VTK_DOUBLE)
        {
            ClipperBridgeUnstructuredGrid<vtkPointAccessor<double> > bridge(ug);
1475
            vtkVisItClipper_Execute(bridge, this->state, ds, output, 
1476
                stuff_I_cant_clip, this);
1477 1478 1479 1480 1481
        }
/* This case probably does not happen...
        else
        {
            ClipperBridgeUnstructuredGrid<vtkGeneralPointAccessor> bridge(ug);
1482 1483
            vtkVisItClipper_UnstructuredExecute(bridge, this->state, ds,
                output, stuff_I_cant_clip, this);
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
        }
*/

        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);
1501
            vtkVisItClipper_Execute(bridge, this->state, ds, output, 
1502
                stuff_I_cant_clip, this);
1503 1504 1505 1506