vtkCSGGrid.C 116 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2017, 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.
*
*****************************************************************************/

39 40
#define REPLACE_COMPLEMENT_WITH_DIFF

hrchilds's avatar
hrchilds committed
41
#include <visit-config.h>
42
#include "vtkCSGGrid.h"
hrchilds's avatar
hrchilds committed
43
#include <vtkAppendFilter.h>
44 45 46 47 48 49
#include <vtkAppendPolyData.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkCone.h>
#include <vtkContourFilter.h>
#include <vtkCylinder.h>
50
#include <vtkDataSetWriter.h>
51
#include <vtkExtentTranslator.h>
52
#include <vtkFloatArray.h>
53 54 55 56 57 58
#include <vtkIdTypeArray.h>
#include <vtkImplicitBoolean.h>
#include <vtkGenericCell.h>
#include <vtkImplicitBoolean.h>
#include <vtkImplicitFunctionCollection.h>
#include <vtkLine.h>
59
#include <vtkMultiSplitter.h>
60 61 62 63 64 65
#include <vtkObjectFactory.h>
#include <vtkPlane.h>
#include <vtkPlanes.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkQuadric.h>
66
#include <vtkRectilinearGrid.h>
67 68
#include <vtkSampleFunction.h>
#include <vtkSphere.h>
69
#include <vtkThreshold.h>
70
#include <vtkTransform.h>
hrchilds's avatar
hrchilds committed
71 72
#include <vtkUnstructuredGrid.h>
#include <vtkVisItClipper.h>
73
#include <vtkVisItSplitter.h>
hrchilds's avatar
hrchilds committed
74

hrchilds's avatar
hrchilds committed
75
#include <DebugStream.h>
76
#include <ImproperUseException.h>
hrchilds's avatar
hrchilds committed
77

78
#ifdef HAVE_BOOST
hrchilds's avatar
hrchilds committed
79 80
#include <boost/numeric/interval.hpp>
using boost::numeric::interval;
hrchilds's avatar
hrchilds committed
81 82
#endif

hrchilds's avatar
hrchilds committed
83 84 85
#include <deque>
#include <map>
#include <vector>
86
#include <algorithm>
hrchilds's avatar
hrchilds committed
87 88 89 90 91

using std::deque;
using std::map;
using std::vector;

92 93 94 95 96 97 98 99 100 101
//
// This macro can be used to subselect out a portion of a csg
// region for debugging purposes.
//
#if 0
#define SWAP_REGION if (reg == 200) reg = 100
#else
#define SWAP_REGION
#endif

hrchilds's avatar
hrchilds committed
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 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
//
// Since we're passing Silo's CSG rep directly to this
// class, we need to know these definitions. Eventually,
// we should go through some kind of conversion from Silo's
// CSG rep to one specific to vtkCSGGrid class.
//
// The reasone these are defined in decimal format instead
// of hexidecimal has to do with fact that these ints are
// also used by Silo's Fortran interface.
//
// These definitions were copied directly from silo.h
//
#define DBCSG_QUADRIC_G         16777216   // 0x01000000
#define DBCSG_SPHERE_PR         33619968   // 0x02010000
#define DBCSG_ELLIPSOID_PRRR    33685504   // 0x02020000
#define DBCSG_PLANE_G           50331648   // 0x03000000
#define DBCSG_PLANE_X           50397184   // 0x03010000
#define DBCSG_PLANE_Y           50462720   // 0x03020000
#define DBCSG_PLANE_Z           50528256   // 0x03030000
#define DBCSG_PLANE_PN          50593792   // 0x03040000
#define DBCSG_PLANE_PPP         50659328   // 0x03050000
#define DBCSG_CYLINDER_PNLR     67108864   // 0x04000000
#define DBCSG_CYLINDER_PPR      67174400   // 0x04010000
#define DBCSG_BOX_XYZXYZ        83886080   // 0x05000000
#define DBCSG_CONE_PNLA         100663296  // 0x06000000
#define DBCSG_CONE_PPA          100728832  // 0x06010000
#define DBCSG_POLYHEDRON_KF     117440512  // 0x07000000
#define DBCSG_HEX_6F            117506048  // 0x07010000
#define DBCSG_TET_4F            117571584  // 0x07020000
#define DBCSG_PYRAMID_5F        117637120  // 0x07030000
#define DBCSG_PRISM_5F          117702656  // 0x07040000

// Definitions for 2D CSG boundary types
#define DBCSG_QUADRATIC_G       134217728  // 0x08000000
#define DBCSG_CIRCLE_PR         150994944  // 0x09000000
#define DBCSG_ELLIPSE_PRR       151060480  // 0x09010000
#define DBCSG_LINE_G            167772160  // 0x0A000000
#define DBCSG_LINE_X            167837696  // 0x0A010000
#define DBCSG_LINE_Y            167903232  // 0x0A020000
#define DBCSG_LINE_PN           167968768  // 0x0A030000
#define DBCSG_LINE_PP           168034304  // 0x0A040000
#define DBCSG_BOX_XYXY          184549376  // 0x0B000000
#define DBCSG_ANGLE_PNLA        201326592  // 0x0C000000
#define DBCSG_ANGLE_PPA         201392128  // 0x0C010000
#define DBCSG_POLYGON_KP        218103808  // 0x0D000000
#define DBCSG_TRI_3P            218169344  // 0x0D010000
#define DBCSG_QUAD_4P           218234880  // 0x0D020000

// Definitions for CSG Region operators
#define DBCSG_INNER             2130706432 // 0x7F000000
#define DBCSG_OUTER             2130771968 // 0x7F010000
#define DBCSG_ON                2130837504 // 0x7F020000
#define DBCSG_UNION             2130903040 // 0x7F030000
#define DBCSG_INTERSECT         2130968576 // 0x7F040000
#define DBCSG_DIFF              2131034112 // 0x7F050000
#define DBCSG_COMPLIMENT        2131099648 // 0x7F060000
#define DBCSG_XFORM             2131165184 // 0x7F070000
#define DBCSG_SWEEP             2131230720 // 0x7F080000


hrchilds's avatar
hrchilds committed
162 163 164 165
#ifdef _WIN32
#define M_PI 3.14159265358979323846
#endif

hrchilds's avatar
hrchilds committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#define NUM_QCOEFFS 10 // # coefficients in a quadric rep

vtkStandardNewMacro(vtkCSGGrid);

// internal type used only for implementation
typedef enum {
    FUNC_BOOLEAN,
    FUNC_CONE,
    FUNC_CYLINDER,
    FUNC_PLANE,
    FUNC_MULTIPLANE,
    FUNC_QUADRIC,
    FUNC_SPHERE,
    FUNC_UNKNOWN_IMPLICIT
} ImplicitFuncType;

182 183 184 185 186 187 188
inline double
relative_diff(double val1, double val2)
{
    return fabs(val1) + fabs(val2) == 0. ? 0. :
               fabs(val1 - val2) / (fabs(val1) + fabs(val2));
}

hrchilds's avatar
hrchilds committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static ImplicitFuncType GetImplicitFuncType(const vtkObject *obj)
{
    const char *className = obj->GetClassName();
    if      (strcmp(className, "vtkImplicitBoolean") == 0)
        return FUNC_BOOLEAN;
    else if (strcmp(className, "vtkCone") == 0)
        return FUNC_CONE;
    else if (strcmp(className, "vtkCylinder") == 0)
        return FUNC_CYLINDER;
    else if (strcmp(className, "vtkPlane") == 0)
        return FUNC_PLANE;
    else if (strcmp(className, "vtkPlanes") == 0)
        return FUNC_MULTIPLANE;
    else if (strcmp(className, "vtkQuadric") == 0)
        return FUNC_QUADRIC;
    else if (strcmp(className, "vtkSphere") == 0)
        return FUNC_SPHERE;
    else
        return FUNC_UNKNOWN_IMPLICIT; 
}
hrchilds's avatar
hrchilds committed
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

static
double ComputeRelativeTol(double absTol,
                          double minX, double maxX,
                          double minY, double maxY,
                          double minZ, double maxZ)
{
    //
    // Turn relative tolerance into an absolute tolerance
    //
    if ((maxX - minX) > (maxY - minY))
    {
        if ((maxX - minX) > (maxZ - minZ))
            absTol *= (maxX - minX);
        else
            absTol *= (maxZ - minZ);
    }
    else
    {
        if ((maxY - minY) > (maxZ - minZ))
            absTol *= (maxY - minY);
        else
            absTol *= (maxZ - minZ);
    }
    return absTol;
}
235

hrchilds's avatar
hrchilds committed
236 237 238
bool
vtkCSGGrid::Box::IsFlatEnough2(const double *const gridBoundaries,
    int boundaryId, double tol)
hrchilds's avatar
hrchilds committed
239
{
240
#ifdef HAVE_BOOST
hrchilds's avatar
hrchilds committed
241 242 243
    const double *const a = &gridBoundaries[boundaryId * NUM_QCOEFFS]; 

    // compute spatial box
hrchilds's avatar
hrchilds committed
244 245 246
    interval<double> X(x0,x1);
    interval<double> Y(y0,y1);
    interval<double> Z(z0,z1);
hrchilds's avatar
hrchilds committed
247

hrchilds's avatar
hrchilds committed
248 249 250 251 252 253
    // quick check for planar functions. They're always flat
    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 true;

    // compute gradient box components (e.g. intervals of grad components)
hrchilds's avatar
hrchilds committed
254 255 256
    interval<double> gradX = 2*a[0]*X + a[3]*Y + a[5]*Z + a[6];
    interval<double> gradY = 2*a[1]*Y + a[3]*X + a[4]*Z + a[7];
    interval<double> gradZ = 2*a[2]*Z + a[4]*Y + a[5]*X + a[8];
hrchilds's avatar
hrchilds committed
257 258

    //
hrchilds's avatar
hrchilds committed
259 260
    // see if the grad-box contains the origin. If so, it
    // means that gradient direction varies over entire 360 deg
hrchilds's avatar
hrchilds committed
261
    //
hrchilds's avatar
hrchilds committed
262 263 264
    if (lower(gradX)<0.0 && upper(gradX)>0.0 &&
        lower(gradY)<0.0 && upper(gradY)>0.0 &&
        lower(gradZ)<0.0 && upper(gradZ)>0.0)
hrchilds's avatar
hrchilds committed
265 266 267
        return false;   

    // compute vector to center of grad box
hrchilds's avatar
hrchilds committed
268 269 270
    double vgx = (lower(gradX) + upper(gradX)) / 2.0;
    double vgy = (lower(gradY) + upper(gradY)) / 2.0;
    double vgz = (lower(gradZ) + upper(gradZ)) / 2.0;
hrchilds's avatar
hrchilds committed
271 272 273

    // find diagonal of grad box most orthogonal to vector vg 
    double mindotp = DBL_MAX;
274
    int dmxmin = 0, dmymin = 0;
hrchilds's avatar
hrchilds committed
275
    for (int d = 0; d < 4; d++)
hrchilds's avatar
hrchilds committed
276
    {
hrchilds's avatar
hrchilds committed
277 278
        int dmx = (d & 0x01) ? -1 : 1;
        int dmy = (d & 0x02) ? -1 : 1;
hrchilds's avatar
hrchilds committed
279 280 281
        double dvx = dmx * (upper(gradX) - lower(gradX));
        double dvy = dmy * (upper(gradY) - lower(gradY));
        double dvz =       (upper(gradZ) - lower(gradZ));
hrchilds's avatar
hrchilds committed
282 283 284 285

        double dotp = vgx * dvx + vgy * dvy + vgz * dvz;
        if (dotp < 0.0) dotp = -dotp;
        if (dotp < mindotp)
hrchilds's avatar
hrchilds committed
286
        {
hrchilds's avatar
hrchilds committed
287 288 289
            mindotp = dotp;
            dmxmin = dmx;
            dmymin = dmy;
hrchilds's avatar
hrchilds committed
290 291 292
        }
    }

hrchilds's avatar
hrchilds committed
293
    // build vectors spanning maximum range of angles of grad box
hrchilds's avatar
hrchilds committed
294 295 296 297 298 299
    double ax = (dmxmin ==  1) ? upper(gradX) : lower(gradX);
    double ay = (dmymin ==  1) ? upper(gradY) : lower(gradY);
    double az =                  upper(gradZ);
    double bx = (dmxmin == -1) ? upper(gradX) : lower(gradX);
    double by = (dmymin == -1) ? upper(gradY) : lower(gradY);
    double bz =                               lower(gradZ);
hrchilds's avatar
hrchilds committed
300 301 302 303 304 305 306 307 308 309
    double maga = sqrt(ax*ax + ay*ay + az*az);
    double magb = sqrt(bx*bx + by*by + bz*bz);
    double cos_theta = (ax*bx + ay*by + az*bz) / (maga * magb);
    if (cos_theta < 0.0) cos_theta = -cos_theta;
    double theta = acos(cos_theta);

    if (2*sin(theta/2) < tol)
        return true;

    return false;
hrchilds's avatar
hrchilds committed
310 311 312
#else
    return false;
#endif
hrchilds's avatar
hrchilds committed
313
}
hrchilds's avatar
hrchilds committed
314

hrchilds's avatar
hrchilds committed
315 316 317 318
bool
vtkCSGGrid::Box::CanBeCut2(const double *const gridBoundaries,
    map<int,int> boundaryToStateMap, double tol)
{
hrchilds's avatar
hrchilds committed
319
    //
hrchilds's avatar
hrchilds committed
320
    // Check to see if any of the EQ_ZERO boundaries are not flat enough
hrchilds's avatar
hrchilds committed
321
    //
hrchilds's avatar
hrchilds committed
322 323
    map<int,int>::const_iterator it;
    for (it = boundaryToStateMap.begin(); it != boundaryToStateMap.end(); it++)
hrchilds's avatar
hrchilds committed
324
    {
hrchilds's avatar
hrchilds committed
325 326
        if (it->second == EQ_ZERO && !IsFlatEnough2(gridBoundaries, it->first, tol))
            return false;
hrchilds's avatar
hrchilds committed
327 328
    }

hrchilds's avatar
hrchilds committed
329 330 331 332
    //
    // All the EQ_ZERO boundaries are flat enough 
    //
    return true;
hrchilds's avatar
hrchilds committed
333 334
}

hrchilds's avatar
hrchilds committed
335 336
vtkCSGGrid::Box::FuncState
vtkCSGGrid::Box::EvalBoxStateOfBoundary(const double *const a, double tol) const
337
{
338
#ifdef HAVE_BOOST
hrchilds's avatar
hrchilds committed
339 340 341
    interval<double> X(x0,x1);
    interval<double> Y(y0,y1);
    interval<double> Z(z0,z1);
hrchilds's avatar
hrchilds committed
342

hrchilds's avatar
hrchilds committed
343
    interval<double> v = a[0]*square(X) + a[1]*square(Y) + a[2]*square(Z) + 
hrchilds's avatar
hrchilds committed
344 345 346
                 a[3]*X*Y + a[4]*Y*Z + a[5]*X*Z + 
                 a[6]*X + a[7]*Y + a[8]*Z + a[9];

hrchilds's avatar
hrchilds committed
347
    if ((lower(v) < 0) && (upper(v) < 0))
hrchilds's avatar
hrchilds committed
348
        return LT_ZERO;
hrchilds's avatar
hrchilds committed
349
    else if ((lower(v) > 0) && (upper(v) > 0))
hrchilds's avatar
hrchilds committed
350
        return GT_ZERO;
351
    else
hrchilds's avatar
hrchilds committed
352
        return EQ_ZERO;
hrchilds's avatar
hrchilds committed
353 354 355
#else
    return EQ_ZERO;
#endif
hrchilds's avatar
hrchilds committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
}

// curBox and tol are unused args here
int
vtkCSGGrid::EvalBoxStateOfRegion(const Box *const curBox, int zoneId,
    map<int,int>& boundaryToStateMap, double tol)
{
    int bndId, left, right;
    switch (regTypeFlags[zoneId])
    {
        case DBCSG_INNER:
            bndId = leftIds[zoneId];
            return boundaryToStateMap[bndId];
        case DBCSG_OUTER:
            bndId = leftIds[zoneId];
            return -boundaryToStateMap[bndId];
        case DBCSG_ON:
            bndId = leftIds[zoneId];
            if (boundaryToStateMap[bndId] == 0)
                return -1; // INNER
            else
                return +1; // OUTER
        case DBCSG_UNION:
        {
            const int unionMatrix[][3]={ {-1, -1, -1},
                                         {-1,  0,  0},
                                         {-1,  0, +1} };
            left  = EvalBoxStateOfRegion(curBox, leftIds[zoneId], boundaryToStateMap, tol); 
            right = EvalBoxStateOfRegion(curBox, rightIds[zoneId], boundaryToStateMap, tol); 
            return unionMatrix[left+1][right+1];
        }
        case DBCSG_INTERSECT:
        {
            const int intersectMatrix[][3]={ {-1,  0, +1},
                                             { 0,  0, +1},
                                             {+1, +1, +1} };
            left  = EvalBoxStateOfRegion(curBox, leftIds[zoneId], boundaryToStateMap, tol); 
            right = EvalBoxStateOfRegion(curBox, rightIds[zoneId], boundaryToStateMap, tol); 
            return intersectMatrix[left+1][right+1];
        }
        case DBCSG_DIFF:
        {
            const int diffMatrix[][3]={ {+1,  0, -1},
                                        {+1,  0,  0},
                                        {+1, +1, +1} };
            left  = EvalBoxStateOfRegion(curBox, leftIds[zoneId], boundaryToStateMap, tol); 
            right = EvalBoxStateOfRegion(curBox, rightIds[zoneId], boundaryToStateMap, tol); 
            return diffMatrix[left+1][right+1];
        }
        case DBCSG_XFORM:
            break;
        default:
            break;
    }
hrchilds's avatar
hrchilds committed
410
    return 0;
411 412 413 414 415 416 417 418 419 420
}

//----------------------------------------------------------------------------
vtkCSGGrid::vtkCSGGrid()
{
  this->Boundaries = vtkImplicitFunctionCollection::New();
  this->Regions    = vtkImplicitFunctionCollection::New();
  this->CellRegionIds = vtkIdTypeArray::New();

  // setup the Universe set
hrchilds's avatar
hrchilds committed
421
  this->Universe = vtkPlanes::New();
422 423
  this->Universe->SetBounds(-FLT_MAX, FLT_MAX, -FLT_MAX, FLT_MAX, -FLT_MAX, FLT_MAX);
  funcMap[Universe] = -1;
hrchilds's avatar
hrchilds committed
424 425 426 427 428 429

  gridBoundaries = 0;
  leftIds = 0;
  rightIds = 0;
  regTypeFlags = 0;
  gridZones = 0;
430 431 432

  multipassProcessedGrid = NULL;
  multipassTags = NULL;
433 434 435 436 437 438 439
}

//----------------------------------------------------------------------------
vtkCSGGrid::~vtkCSGGrid()
{
  this->Initialize();
  this->Universe->Delete();
hrchilds's avatar
hrchilds committed
440 441 442 443 444 445 446 447 448 449
  if (gridBoundaries) delete [] gridBoundaries;
  gridBoundaries = 0;
  if (leftIds) delete [] leftIds;
  leftIds = 0;
  if (rightIds) delete [] rightIds;
  rightIds = 0;
  if (regTypeFlags) delete [] regTypeFlags;
  regTypeFlags = 0;
  if (gridZones) delete [] gridZones;
  gridZones = 0;
450 451 452 453
  if (multipassProcessedGrid) multipassProcessedGrid->Delete();
  multipassProcessedGrid = 0;
  if (multipassTags) delete multipassTags;
  multipassTags = 0;
454 455 456
}

//----------------------------------------------------------------------------
457 458
void
vtkCSGGrid::Initialize()
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
{
  vtkDataSet::Initialize();

  if ( this->Boundaries ) 
    {
    this->Boundaries->UnRegister(this);
    this->Boundaries = NULL;
    }

  if ( this->Regions ) 
    {
    this->Regions->UnRegister(this);
    this->Regions = NULL;
    }

  if ( this->CellRegionIds ) 
    {
    this->CellRegionIds->UnRegister(this);
    this->CellRegionIds = NULL;
    }
}

//----------------------------------------------------------------------------
// Copy the geometric and topological structure of an input rectilinear grid
// object.
484 485 486 487 488 489 490 491
//
// Modifications:
//   Jeremy Meredith, Fri Feb 26 14:01:44 EST 2010
//   numBoundaries has already been increased by six and stored that way.
//   If we do it again, we walk off the end of the array.
//
void
vtkCSGGrid::CopyStructure(vtkDataSet *ds)
492
{
hrchilds's avatar
hrchilds committed
493
  int i;
494 495 496 497 498 499
  vtkCSGGrid *csgGrid=(vtkCSGGrid *)ds;
  this->Initialize();

  this->SetBoundaries(csgGrid->GetBoundaries());
  this->SetRegions(csgGrid->GetRegions());
  this->SetCellRegionIds(csgGrid->GetCellRegionIds());
hrchilds's avatar
hrchilds committed
500 501

  this->numBoundaries = csgGrid->numBoundaries;
502 503
  this->gridBoundaries = new double[NUM_QCOEFFS*this->numBoundaries];
  for (i = 0; i < NUM_QCOEFFS*this->numBoundaries; i++)
hrchilds's avatar
hrchilds committed
504 505 506 507 508 509 510 511 512 513 514 515
    this->gridBoundaries[i] = csgGrid->gridBoundaries[i];

  this->numRegions = csgGrid->numRegions;
  this->leftIds = new int[this->numRegions];
  this->rightIds = new int[this->numRegions];
  this->regTypeFlags = new int[this->numRegions];
  for (i = 0; i < this->numRegions; i++)
    {
    this->leftIds[i] = csgGrid->leftIds[i];
    this->rightIds[i] = csgGrid->rightIds[i];
    this->regTypeFlags[i] = csgGrid->regTypeFlags[i];
    }
516

hrchilds's avatar
hrchilds committed
517 518 519 520 521 522 523
  this->numZones = csgGrid->numZones;
  this->gridZones = new int[this->numZones];
  for (i = 0; i < this->numZones; i++)
    this->gridZones[i] = csgGrid->gridZones[i];

  for (i = 0; i < 6; i++)
    this->Bounds[i] = csgGrid->Bounds[i];
524 525 526
}

//----------------------------------------------------------------------------
527 528
vtkCell *
vtkCSGGrid::GetCell(vtkIdType cellId)
529
{
hrchilds's avatar
hrchilds committed
530
//#warning GetCell NOT IMPLEMENTED
531 532 533 534
  return NULL;
}

//----------------------------------------------------------------------------
535 536
void
vtkCSGGrid::GetCell(vtkIdType cellId, vtkGenericCell *cell)
537
{
hrchilds's avatar
hrchilds committed
538
//#warning GetCell NOT IMPLEMENTED
539 540 541
  cell->SetCellTypeToEmptyCell();
}

542 543
int
vtkCSGGrid::GetCellType(vtkIdType cellId)
544
{
hrchilds's avatar
hrchilds committed
545
//#warning GetCellType NOT IMPLEMENTED
546 547 548
  return VTK_EMPTY_CELL;
}

549 550
int
vtkCSGGrid::GetMaxCellSize()
551
{
hrchilds's avatar
hrchilds committed
552
//#warning GetMaxCellSize NOT IMPLEMENTED
553 554 555 556
  return -1;
}

//----------------------------------------------------------------------------
557 558
double *
vtkCSGGrid::GetPoint(vtkIdType ptId)
559 560 561 562 563 564
{
  vtkErrorMacro("For a vtkCSGGrid, GetPoint() means GetBoundary()");
  vtkErrorMacro("Use GetBoundary() to avoid this message");
  int dummy, n;
  double *p = 0;
  this->GetBoundary(ptId, &dummy, &n, &p);
hrchilds's avatar
hrchilds committed
565
  const int k = sizeof(tmpFloats) / sizeof(double);
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
  int m = n;
  if (n >= k)
  {
      vtkErrorMacro("GetPoint() too many coefficients");
      m = k;
  }
  for (int i = 0; i < m; i++)
      tmpFloats[i] = p[i];
  delete [] p;
  return tmpFloats;
}

//----------------------------------------------------------------------------
// Fast implementation of GetCellBounds().  Bounds are calculated without
// constructing a cell.
581 582
void
vtkCSGGrid::GetCellBounds(vtkIdType cellId, double bounds[6])
583
{
hrchilds's avatar
hrchilds committed
584
//#warning GetCellBounds NOT IMPLEMENTED
585 586 587
  return;
}

588 589
void
vtkCSGGrid::GetPoint(vtkIdType ptId, double x[3])
590 591 592 593 594 595 596 597
{
  vtkErrorMacro("Requesting a point[3] from a vtkCSGGrid");
  vtkErrorMacro("Use GetBoundary() to avoid this message");
  x[0] = x[1] = x[2] = 0.0;
  return;
}

//----------------------------------------------------------------------------
598 599
vtkIdType
vtkCSGGrid::FindPoint(double x[3])
600 601 602 603 604
{
  vtkErrorMacro("Finding a point on a vtkCSGGrid not yet implemented.");
  return -1;
}

605 606
vtkIdType
vtkCSGGrid::FindCell(double x[3], vtkCell *vtkNotUsed(cell), 
607 608
                                       vtkGenericCell *vtkNotUsed(gencell),
                                       vtkIdType vtkNotUsed(cellId), 
hrchilds's avatar
hrchilds committed
609 610 611
                                       double vtkNotUsed(tol2), 
                                       int& subId, double pcoords[3], 
                                       double *weights)
612 613 614 615 616 617
{
  return
    this->FindCell( x, (vtkCell *)NULL, 0, 0.0, subId, pcoords, weights );
}

//----------------------------------------------------------------------------
618 619
vtkIdType
vtkCSGGrid::FindCell(double x[3], vtkCell *vtkNotUsed(cell), 
620
                                       vtkIdType vtkNotUsed(cellId),
hrchilds's avatar
hrchilds committed
621 622 623
                                       double vtkNotUsed(tol2), 
                                       int& subId, double pcoords[3],
                                       double *weights)
624
{
hrchilds's avatar
hrchilds committed
625
//#warning FindCell NOT IMPLEMENTED
626 627 628 629
  return -1;
}

//----------------------------------------------------------------------------
630 631
vtkCell *
vtkCSGGrid::FindAndGetCell(double x[3],
632 633
                                            vtkCell *vtkNotUsed(cell), 
                                            vtkIdType vtkNotUsed(cellId),
hrchilds's avatar
hrchilds committed
634
                                            double vtkNotUsed(tol2),
635
                                            int& subId, 
hrchilds's avatar
hrchilds committed
636
                                            double pcoords[3], double *weights)
637
{
hrchilds's avatar
hrchilds committed
638
//#warning FindAndGetCell NOT IMPLEMENTED
639 640 641 642
  return NULL;
}

//----------------------------------------------------------------------------
643 644
void
vtkCSGGrid::GetCellPoints(vtkIdType cellId, vtkIdList *ptIds)
645
{
hrchilds's avatar
hrchilds committed
646
//#warning CellPoints NOT IMPLEMENTED
647 648 649
}

//----------------------------------------------------------------------------
650 651
void
vtkCSGGrid::GetPointCells(vtkIdType ptId, vtkIdList *cellIds)
652
{
hrchilds's avatar
hrchilds committed
653
//#warning GetPointCells NOT IMPLEMENTED
654 655 656 657
}


//----------------------------------------------------------------------------
658 659
void
vtkCSGGrid::ComputeBounds()
660 661 662 663 664 665 666 667 668
{
}

static unsigned long GetActualMemorySizeOfImplicitFunc(vtkImplicitFunction *func)
{
    unsigned long size = 0;

    switch (GetImplicitFuncType(func))
    {
hrchilds's avatar
hrchilds committed
669
        case FUNC_BOOLEAN:
670 671 672 673 674 675 676 677 678 679 680
        {
            vtkImplicitBoolean *boolFunc = vtkImplicitBoolean::SafeDownCast(func);
            vtkImplicitFunctionCollection *funcs = boolFunc->GetFunction();
            vtkImplicitFunction *leftFunc =
                vtkImplicitFunction::SafeDownCast(funcs->GetItemAsObject(0));
            vtkImplicitFunction *rightFunc =
                vtkImplicitFunction::SafeDownCast(funcs->GetItemAsObject(1));
            size += GetActualMemorySizeOfImplicitFunc(leftFunc);
            size += GetActualMemorySizeOfImplicitFunc(rightFunc);
            break;
        }
hrchilds's avatar
hrchilds committed
681 682 683 684 685
        case FUNC_CONE:     size += sizeof(vtkCone);     break;
        case FUNC_CYLINDER: size += sizeof(vtkCylinder); break;
        case FUNC_PLANE:    size += sizeof(vtkPlane);    break;
        case FUNC_SPHERE:   size += sizeof(vtkSphere);   break;
        case FUNC_QUADRIC:  size += sizeof(vtkQuadric);  break;
hrchilds's avatar
hrchilds committed
686
        case FUNC_MULTIPLANE:
687 688 689 690 691 692 693
        {
            vtkPlanes *planes = vtkPlanes::SafeDownCast(func);
            size += planes->GetPoints()->GetActualMemorySize();
            size += planes->GetNormals()->GetActualMemorySize();
            size += planes->GetNumberOfPlanes() * sizeof(vtkPlane);
            break;
        }
hrchilds's avatar
hrchilds committed
694
        case FUNC_UNKNOWN_IMPLICIT:
695 696 697 698 699 700
        default:       size += 0; break;
    }
    return size;
}

//----------------------------------------------------------------------------
701 702
unsigned long
vtkCSGGrid::GetActualMemorySize()
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
{
    int i;
    unsigned long size=this->vtkDataSet::GetActualMemorySize();

    if (this->Boundaries)
    {
        for (i = 0; i < this->Boundaries->GetNumberOfItems(); i++)
        {
            vtkImplicitFunction *func =
                vtkImplicitFunction::SafeDownCast(this->Boundaries->GetItemAsObject(i));
            size += GetActualMemorySizeOfImplicitFunc(func); 
        }
    }

    if (this->Regions)
    {
        for (i = 0; i < this->Regions->GetNumberOfItems(); i++)
        {
            vtkImplicitFunction *func =
                vtkImplicitFunction::SafeDownCast(this->Regions->GetItemAsObject(i));
            size += GetActualMemorySizeOfImplicitFunc(func); 
        }
    }

    if (this->CellRegionIds)
    {
        size += this->CellRegionIds->GetActualMemorySize();
    }

    return size;

}

//----------------------------------------------------------------------------
737 738
void
vtkCSGGrid::GetCellNeighbors(vtkIdType cellId, vtkIdList *ptIds,
739 740 741
                                          vtkIdList *cellIds)
{

hrchilds's avatar
hrchilds committed
742
//#warning GetCellNeighbors NOT IMPLEMENTED
743 744 745 746 747
  cellIds->Reset();
  return;
}

//----------------------------------------------------------------------------
748 749
void
vtkCSGGrid::ShallowCopy(vtkDataObject *dataObject)
750
{
miller86's avatar
miller86 committed
751 752
    // There is no shallow copy
    DeepCopy(dataObject);
753 754 755
}

//----------------------------------------------------------------------------
756 757 758 759 760 761 762
// Modifications:
//   Jeremy Meredith, Fri Feb 26 14:01:44 EST 2010
//   numBoundaries has already been increased by six and stored that way.
//   If we do it again, we walk off the end of the array.
//
void
vtkCSGGrid::DeepCopy(vtkDataObject *srcObject)
763 764 765 766 767
{
  vtkCSGGrid *grid = vtkCSGGrid::SafeDownCast(srcObject);

  if ( grid != NULL )
    {
miller86's avatar
miller86 committed
768 769
      int i;
      this->numBoundaries = grid->numBoundaries;
770 771 772
      this->gridBoundaries = new double[NUM_QCOEFFS*this->numBoundaries];
      for (i = 0; i < NUM_QCOEFFS*this->numBoundaries; i++)
          this->gridBoundaries[i] = grid->gridBoundaries[i];
miller86's avatar
miller86 committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

      this->numRegions = grid->numRegions;
      this->leftIds = new int[this->numRegions];
      this->rightIds = new int[this->numRegions];
      this->regTypeFlags = new int[this->numRegions];
      for (i = 0; i < this->numRegions; i++)
        {
          this->leftIds[i] = grid->leftIds[i];
          this->rightIds[i] = grid->rightIds[i];
          this->regTypeFlags[i] = grid->regTypeFlags[i];
        }

      this->numZones = grid->numZones;
      this->gridZones = new int[this->numZones];
      for (i = 0; i < this->numZones; i++)
        this->gridZones[i] = grid->gridZones[i];

      for (i = 0; i < 6; i++)
        this->Bounds[i] = grid->Bounds[i];
792 793 794 795 796 797
    }

  // Do superclass
  this->vtkDataSet::DeepCopy(srcObject);
}

hrchilds's avatar
hrchilds committed
798 799 800 801 802 803
//----------------------------------------------------------------------------
// Modifications:
//
//   Hank Childs, Fri Jun  9 12:54:36 PDT 2006
//   Add "default" to switch statement
//
804
//----------------------------------------------------------------------------
805 806
void
vtkCSGGrid::PrintSelf(ostream& os, vtkIndent indent)
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
{
    int i;
    vtkImplicitFunction *func;

    this->Superclass::PrintSelf(os,indent);

    os << indent << "Contents of \"" << GetClassName() << "\"..." << endl;
    os << indent << "Number of Boundaries = " << Boundaries->GetNumberOfItems() << endl;
    os << indent << "Number of Regions    = " << Regions->GetNumberOfItems() << endl;
    os << indent << "Number of Cells      = " << CellRegionIds->GetNumberOfTuples() << endl;
    os << endl;

    //
    // Print the boundaries
    //
    os << indent << "Boundaries:" << endl;
    for (i = 0; i < Boundaries->GetNumberOfItems(); i++)
    {
        vtkIndent indent2 = indent.GetNextIndent();
        os << indent2 << "Item: " << i;

        func = GetBoundaryFunc(i);
        switch (GetImplicitFuncType(func))
        {
hrchilds's avatar
hrchilds committed
831
            case FUNC_CYLINDER:
832 833 834 835 836 837
            {
                os << ", is a cylinder" << endl;
                vtkCylinder *cylinder = vtkCylinder::SafeDownCast(func);
                  cylinder->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
838
            case FUNC_PLANE:
839 840 841 842 843 844
            {
                os << ", is a plane" << endl;
                vtkPlane *plane = vtkPlane::SafeDownCast(func);
                  plane->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
845
            case FUNC_SPHERE:
846 847 848 849 850 851
            {
                os << ", is a sphere" << endl;
                vtkSphere *sphere = vtkSphere::SafeDownCast(func);
                  sphere->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
852
            case FUNC_QUADRIC:
853 854 855 856 857 858
            {
                os << ", is a quadric" << endl;
                vtkQuadric *quadric = vtkQuadric::SafeDownCast(func);
                  quadric->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
859 860 861 862 863
            default:
            {
                os << ", is an unexpected implicit function type" << endl;
                break;
            }
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
        }
    }

    //
    // Print the regions
    //
    os << indent << "Regions:" << endl;
    for (i = 0; i < Regions->GetNumberOfItems(); i++)
    {
        vtkIndent indent2 = indent.GetNextIndent();
        os << indent2 << "Item: " << i;

        func = GetRegionFunc(i);
        switch (GetImplicitFuncType(func))
        {
hrchilds's avatar
hrchilds committed
879
            case FUNC_BOOLEAN:
880 881 882 883 884 885 886 887 888 889 890 891
            {
                vtkImplicitBoolean *boolFunc = vtkImplicitBoolean::SafeDownCast(func);
                vtkImplicitFunctionCollection *funcs = boolFunc->GetFunction();
                vtkImplicitFunction *leftFunc =
                    vtkImplicitFunction::SafeDownCast(funcs->GetItemAsObject(0));
                vtkImplicitFunction *rightFunc =
                    vtkImplicitFunction::SafeDownCast(funcs->GetItemAsObject(1));

                if (leftFunc == Universe)
                {
                    switch (GetImplicitFuncType(rightFunc))
                    {
hrchilds's avatar
hrchilds committed
892
                        case FUNC_CYLINDER:
893 894 895 896 897 898
                        {
                            os << ", is the OUTER of a cylinder" << endl;
                            vtkCylinder *cylinder = vtkCylinder::SafeDownCast(rightFunc);
                              cylinder->PrintSelf(os, indent2.GetNextIndent());
                            break;
                        }
hrchilds's avatar
hrchilds committed
899
                        case FUNC_SPHERE:
900 901 902 903 904 905
                        {
                            os << ", is the OUTER of a sphere" << endl;
                            vtkSphere *sphere = vtkSphere::SafeDownCast(rightFunc);
                              sphere->PrintSelf(os, indent2.GetNextIndent());
                            break;
                        }
hrchilds's avatar
hrchilds committed
906
                        case FUNC_QUADRIC:
907 908 909 910 911 912
                        {
                            os << ", is the OUTER of a quadric" << endl;
                            vtkQuadric *quadric = vtkQuadric::SafeDownCast(rightFunc);
                              quadric->PrintSelf(os, indent2.GetNextIndent());
                            break;
                        }
hrchilds's avatar
hrchilds committed
913 914 915 916 917 918
                        default:
                        {
                            os << "is an unexpected implicit function type"
                               << endl;
                            break;
                        }
919 920 921 922 923 924 925
                    }
                }
                else
                {
                    os << ", is a boolean ";
                    switch (boolFunc->GetOperationType())
                    {
bonnell's avatar
bonnell committed
926 927 928
                        case vtkImplicitBoolean::VTK_INTERSECTION: os << "intersection "; break;
                        case vtkImplicitBoolean::VTK_UNION:        os << "union "; break;
                        case vtkImplicitBoolean::VTK_DIFFERENCE:   os << "difference "; break;
929 930 931 932 933
                    }

                    vtkIdType leftId  = funcMap[leftFunc];
                    vtkIdType rightId = funcMap[rightFunc];

hrchilds's avatar
hrchilds committed
934
                    os << "of items " << leftId << " and " << rightId << endl;
935 936 937
                }
                break;
            }
hrchilds's avatar
hrchilds committed
938
            case FUNC_CYLINDER:
939 940 941 942 943 944
            {
                os << ", is the INNER of a cylinder" << endl;
                vtkCylinder *cylinder = vtkCylinder::SafeDownCast(func);
                  cylinder->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
945
            case FUNC_PLANE:
946 947 948 949 950 951
            {
                os << ", is the INNER of a plane" << endl;
                vtkPlane *plane = vtkPlane::SafeDownCast(func);
                  plane->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
952
            case FUNC_SPHERE:
953 954 955 956 957 958
            {
                os << ", is the INNER of a sphere" << endl;
                vtkSphere *sphere = vtkSphere::SafeDownCast(func);
                  sphere->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
959
            case FUNC_QUADRIC:
960 961 962 963 964 965
            {
                os << ", is the INNER of a quadric" << endl;
                vtkQuadric *quadric = vtkQuadric::SafeDownCast(func);
                  quadric->PrintSelf(os, indent2.GetNextIndent());
                break;
            }
hrchilds's avatar
hrchilds committed
966 967 968 969 970 971
            default:
            {
                os << "is an unexpected implicit function type"
                   << endl;
                break;
            }
972 973 974 975
        }
    }
}

hrchilds's avatar
hrchilds committed
976 977 978
//
// Quadric equation coefficient indices...
//
hrchilds's avatar
hrchilds committed
979
// x^2   y^2   z^2    xy    yz    xz    x    y    z    1
hrchilds's avatar
hrchilds committed
980 981 982 983 984 985 986 987
//  0     1     2     3     4     5     6    7    8    9
//
static void SpherePRToQuadric(const double *const sphere, double *quadric)
{
    quadric[0] = 1.0; // x^2 term
    quadric[1] = 1.0; // y^2 term
    quadric[2] = 1.0; // z^2 term
    quadric[6] = -2.0 * sphere[0]; // x term 
988 989
    quadric[7] = -2.0 * sphere[1]; // y term 
    quadric[8] = -2.0 * sphere[2]; // z term 
hrchilds's avatar
hrchilds committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    quadric[9] = sphere[0]*sphere[0] + // constant term
                 sphere[1]*sphere[1] +
                 sphere[2]*sphere[2] -
                 sphere[3]*sphere[3];
}
static void PlaneXToQuadric(const double *const plane, double *quadric)
{
    quadric[6] = 1.0; 
    quadric[9] = -plane[0];
}
static void PlaneYToQuadric(const double *const plane, double *quadric)
{
    quadric[7] = 1.0;
    quadric[9] = -plane[0];
}
static void PlaneZToQuadric(const double *const plane, double *quadric)
{
    quadric[8] = 1.0;
    quadric[9] = -plane[0];
}
static void PlaneGToQuadric(const double *const plane, double *quadric)
{
    quadric[6] = plane[0]; 
    quadric[7] = plane[1]; 
    quadric[8] = plane[2]; 
    quadric[9] = plane[3]; 
}
static void PlanePNToQuadric(const double *const plane, double *quadric)
{
    quadric[6] = plane[3];
    quadric[7] = plane[4];
    quadric[8] = plane[5];
    quadric[9] = -plane[0]*plane[3] -
                  plane[1]*plane[4] -
                  plane[2]*plane[5];
}
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
static void QuadraticGToQuadric(const double *const q, double *quadric)
{
    quadric[0] = q[0]; // x^2 term
    quadric[1] = q[1]; // y^2 term
    quadric[3] = q[2]; // xy term 
    quadric[6] = q[3]; // x term 
    quadric[7] = q[4]; // y term
    quadric[9] = q[5]; // constant term
}
static void CirclePRToQuadric(const double *const circle, double *quadric)
{
    quadric[0] = 1.0; // x^2 term
    quadric[1] = 1.0; // y^2 term
    quadric[6] = -2.0 * circle[0]; // x term 
    quadric[7] = -2.0 * circle[1]; // y term 
    quadric[9] = circle[0]*circle[0] + // constant term
                 circle[1]*circle[1] -
                 circle[2]*circle[2];
}
static void LineXToQuadric(const double *const line, double *quadric)
{
    quadric[6] = 1.0;
    quadric[9] = -line[0]; 
}
static void LineYToQuadric(const double *const line, double *quadric)
{
    quadric[7] = 1.0;
    quadric[9] = -line[0]; 
}
static void LineGToQuadric(const double *const line, double *quadric)
{
    quadric[6] = line[0]; 
    quadric[7] = line[1]; 
    quadric[9] = line[2]; 
}
static void LinePNToQuadric(const double *const line, double *quadric)
{
    quadric[6] = line[2];
    quadric[7] = line[3];
    quadric[9] = -line[0]*line[2] -
                  line[1]*line[3];
}
hrchilds's avatar
hrchilds committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
static void PlanePPPToQuadric(const double *const plane, double *quadric)
{
    // The three points are 'a', 'b' and 'c', the middle point being the
    // vertex used to compute vectors and serve as point on the plane
    const double *a = &plane[0];
    const double *b = &plane[3];
    const double *c = &plane[6];
    double ab[3];
    double cb[3];
    for (int i = 0; i < 3; i++)
    {
        ab[i] = a[i] - b[i];
        cb[i] = c[i] - b[i];
    }

    // compute normalized cross-product of ab and cb vectors
    double xprod[3];
    xprod[0] = ab[1]*cb[2] - ab[2]*cb[1];
    xprod[1] = ab[0]*cb[2] - ab[2]*cb[0];
    xprod[2] = ab[0]*cb[1] - ab[1]*cb[0];
    double d = xprod[0]*xprod[0] + xprod[1]*xprod[1] + xprod[2]*xprod[2];
    if (d > 0.0)
    {
        xprod[0] /= d;
        xprod[1] /= d;
        xprod[2] /= d;
    }
    
    // use the PlanePN routine to finish it off
    double coeffs[6];
    coeffs[0] = b[0];
    coeffs[1] = b[1];
    coeffs[2] = b[2];
    coeffs[3] = xprod[0];
    coeffs[4] = xprod[1];
    coeffs[5] = xprod[2];
    PlanePNToQuadric(coeffs, quadric);
}
1106

hrchilds's avatar
hrchilds committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 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 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
static void CylinderPNLRToQuadric(const double *const cyl, double *quadric)
{
    // normal
    double nx = cyl[3];
    double ny = cyl[4];
    double nz = cyl[5];

    // angle of rotation about x
    double phi  = asin(ny);

    // angle of rotation about y
    double theta;
    if (nx == 0 && nz == 0)
        theta = 0.0;
    else
        theta = asin(nx / sqrt(nx*nx + nz*nz));

    double a = cos(theta);
    double b = sin(theta);
    double q = cos(-phi);
    double r = sin(-phi);
    double a2 = a*a;
    double b2 = b*b;
    double q2 = q*q;
    double r2 = r*r;
    double R = cyl[7];
    quadric[0] = a2+r2*b2;
    quadric[1] = q2;
    quadric[2] = b2+r2*a2;
    quadric[3] = 2*q*r*b;
    quadric[4] = 2*q*r*a;
    quadric[5] = 2*a*b*r2-2*a*b;
    quadric[9] = -R*R;
}
static void CylinderPPRToQuadric(const double *const cyl, double *quadric)
{
    double dx = cyl[3] - cyl[0];
    double dy = cyl[4] - cyl[1];
    double dz = cyl[5] - cyl[2];
    double l = sqrt(dx*dx + dy*dy + dz*dz);
    double coeffs[8];
    coeffs[0] = cyl[0];
    coeffs[1] = cyl[1];
    coeffs[2] = cyl[2];
    coeffs[3] = dx / l;
    coeffs[4] = dy / l;
    coeffs[5] = dz / l;
    coeffs[6] = l;
    coeffs[7] = cyl[6];
    CylinderPNLRToQuadric(coeffs, quadric);
}
static void ConePNLAToQuadric(const double *const cone, double *quadric)
{

    // normal
    double nx = cone[3];
    double ny = cone[4];
    double nz = cone[5];

    // cone angle
    double alpha = cone[7];

    // angle of rotation about x
    double phi  = asin(ny);

    // angle of rotation about y
    double theta;
    if (nx == 0 && nz == 0)
        theta = 0.0;
    else
        theta = asin(nx / sqrt(nx*nx + nz*nz));

    double a = cos(theta);
    double b = sin(theta);
    double q = cos(-phi);
    double r = sin(-phi);
    double u = tan(alpha*M_PI/180.0);
    double a2 = a*a;
    double b2 = b*b;
    double q2 = q*q;
    double r2 = r*r;
    double u2 = u*u;

    quadric[0] = a2+r2*b2-u2*q2*b2;
    quadric[1] = q2-u2*r2;
    quadric[2] = b2+r2*a2-u2*q2*a2;
    quadric[3] = 2*q*r*b+2*u2*q*r*b;
    quadric[4] = 2*q*r*a+2*u2*q*r*a;
    quadric[5] = 2*a*b*r2-2*a*b-2*u2*q2*a*b;
}
static void ConePPAToQuadric(const double *const cone, double *quadric)
{
    double dx = cone[3] - cone[0];
    double dy = cone[4] - cone[1];
    double dz = cone[5] - cone[2];
    double l = sqrt(dx*dx + dy*dy + dz*dz);
    double coeffs[8];
    coeffs[0] = cone[0];
    coeffs[1] = cone[1];
    coeffs[2] = cone[2];
    coeffs[3] = dx / l;
    coeffs[4] = dy / l;
    coeffs[5] = dz / l;
    coeffs[6] = l;
    coeffs[7] = cone[6];
    ConePNLAToQuadric(coeffs, quadric);
}
static void QuadricToQuadric(const double *const inquad, double *quadric)
{
    for (int i = 0; i < NUM_QCOEFFS; i++)
        quadric[i] = inquad[i];
}

1220 1221 1222 1223 1224 1225 1226
//----------------------------------------------------------------------------
// Modifications:
//   Jeremy Meredith, Fri Feb 26 14:01:44 EST 2010
//   Added ifdef to wrap the replacement of complement with a diff op.
//   It's not needed for all algorithms and hurts performance, so
//   this will make it easier to disable later if/when it becoms possible.
//
1227 1228 1229 1230 1231
//   Jeremy Meredith, Mon Oct 24 16:03:54 EDT 2011
//   Added some of the 2D primitives.  Not all of them, but we're
//   missing a number of the 3D primitives as well, so I only tried to
//   get a few of the most common ones.
//
1232 1233
void
vtkCSGGrid::AddBoundaries(int nbounds,
hrchilds's avatar
hrchilds committed
1234 1235 1236 1237
    const int *const typeflags, int lcoeffs, const double *const coeffs)
{
    int i;

1238
#ifdef REPLACE_COMPLEMENT_WITH_DIFF
hrchilds's avatar
hrchilds committed
1239 1240 1241 1242
    // the '+6' is so we can define the 6 faces of the "universe" box
    gridBoundaries = new double[NUM_QCOEFFS * (nbounds + 6)];
    for (i = 0; i < NUM_QCOEFFS * (nbounds + 6); i++)
        gridBoundaries[i] = 0.0;
1243 1244 1245 1246 1247
#else // not REPLACE_COMPLEMENT_WITH_DIFF
    gridBoundaries = new double[NUM_QCOEFFS * nbounds];
    for (i = 0; i < NUM_QCOEFFS * nbounds; i++)
        gridBoundaries[i] = 0.0;
#endif
hrchilds's avatar
hrchilds committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283

    int coeffidx = 0;
    int quadidx = 0;
    for (i = 0; i < nbounds; i++)
    {
        switch (typeflags[i])
        {
            case DBCSG_SPHERE_PR:
                SpherePRToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=4; break;
            case DBCSG_CYLINDER_PNLR:
                CylinderPNLRToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=8; break;
            case DBCSG_CYLINDER_PPR:
                CylinderPPRToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=7; break;
            case DBCSG_PLANE_X:
                PlaneXToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=1; break;
            case DBCSG_PLANE_Y:
                PlaneYToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=1; break;
            case DBCSG_PLANE_Z:
                PlaneZToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=1; break;
            case DBCSG_PLANE_PN:
                PlanePNToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=6; break;
            case DBCSG_PLANE_G:
                PlaneGToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=4; break;
            case DBCSG_PLANE_PPP:
                PlanePPPToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=9; break;
#if 0
            // we don't handle compound surfaces yet. They'll break indexing of gridBoundaries
            case DBCSG_BOX_XYZXYZ:
                BoxXYZXYZToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=6; quadidx += 5*NUM_QCOEFFS; break;
#endif
            case DBCSG_CONE_PNLA:
                ConePNLAToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=8; break;
            case DBCSG_CONE_PPA:
                ConePPAToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=7; break;
            case DBCSG_QUADRIC_G:
                QuadricToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=NUM_QCOEFFS; break;
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

            // some 2d stuff
                // missing: ELLIPSE_PRR, LINE_RR
                //  (at least of ones with equations in the silo manual)
            case DBCSG_QUADRATIC_G:
                QuadraticGToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=6; break;
            case DBCSG_LINE_X:
                LineXToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=1; break;
            case DBCSG_LINE_Y:
                LineYToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=1; break;
            case DBCSG_LINE_PN:
                LinePNToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=4; break;
            case DBCSG_LINE_G:
                LineGToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=3; break;
            case DBCSG_CIRCLE_PR:
                CirclePRToQuadric(&coeffs[coeffidx], &gridBoundaries[quadidx]); coeffidx+=3; break;
hrchilds's avatar
hrchilds committed
1300 1301 1302 1303
        }
        quadidx += NUM_QCOEFFS;
    }

1304
#ifdef REPLACE_COMPLEMENT_WITH_DIFF
hrchilds's avatar
hrchilds committed
1305
    numBoundaries = nbounds + 6;
1306 1307 1308
#else // not REPLACE_COMPLEMENT_WITH_DIFF
    numBoundaries = nbounds;
#endif
hrchilds's avatar
hrchilds committed
1309
}
1310 1311 1312

void
vtkCSGGrid::AddBoundaries(int nbounds,
hrchilds's avatar
hrchilds committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    const int *const typeflags, int lcoeffs, const float *const coeffs)
{
    double *tmpcoeffs = new double[lcoeffs];
    for (int i = 0; i < lcoeffs; i++)
        tmpcoeffs[i] = coeffs[i];

    AddBoundaries(nbounds, typeflags, lcoeffs, tmpcoeffs);

    delete [] tmpcoeffs;
}
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332

//----------------------------------------------------------------------------
// Modifications:
//   Jeremy Meredith, Fri Feb 26 14:01:44 EST 2010
//   Added ifdef to wrap the replacement of complement with a diff op.
//   It's not needed for all algorithms and hurts performance, so
//   this will make it easier to disable later if/when it becoms possible.
//
void
vtkCSGGrid::AddRegions(int nregions,
hrchilds's avatar
hrchilds committed
1333 1334 1335
    const int *const lids, const int *const rids,
    const int *const typeflags, int lxforms, const double *const xforms)
{
1336
#ifdef REPLACE_COMPLEMENT_WITH_DIFF
hrchilds's avatar
hrchilds committed
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 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
    // the '+11' is so we can define the universe (bounding box of whole mesh) 
    leftIds = new int[nregions+11];
    rightIds = new int[nregions+11];
    regTypeFlags = new int[nregions+11];

    const int universeId = nregions + 11 - 1; 
    for (int i = 0; i < nregions; i++)
    {
        leftIds[i] = lids[i];
        rightIds[i] = rids[i];
        regTypeFlags[i] = typeflags[i];

        // replace all compliment operations with diffs with "universe"
        if (regTypeFlags[i] == DBCSG_COMPLIMENT)
        {
            regTypeFlags[i] = DBCSG_DIFF;
            rightIds[i] = leftIds[i];
            leftIds[i] = universeId;
        }
    }

    // add the universe
    int bndidx = numBoundaries - 6;
    int quadidx = NUM_QCOEFFS * bndidx; 
    int regidx = nregions;

    // add the min,max X plane boundaries
    PlaneXToQuadric(&Bounds[0], &gridBoundaries[quadidx]); quadidx += NUM_QCOEFFS;
    leftIds[regidx] = bndidx++; rightIds[regidx] = -1; regTypeFlags[regidx++] = DBCSG_OUTER;
    PlaneXToQuadric(&Bounds[1], &gridBoundaries[quadidx]); quadidx += NUM_QCOEFFS;
    leftIds[regidx] = bndidx++; rightIds[regidx] = -1; regTypeFlags[regidx++] = DBCSG_INNER;

    // add the min,max Y plane boundaries
    PlaneYToQuadric(&Bounds[2], &gridBoundaries[quadidx]); quadidx += NUM_QCOEFFS;
    leftIds[regidx] = bndidx++; rightIds[regidx] = -1; regTypeFlags[regidx++] = DBCSG_OUTER;
    PlaneYToQuadric(&Bounds[3], &gridBoundaries[quadidx]); quadidx += NUM_QCOEFFS;
    leftIds[regidx] = bndidx++; rightIds[regidx] = -1; regTypeFlags[regidx++] = DBCSG_INNER;

    // add the min,max Z plane boundaries
    PlaneZToQuadric(&Bounds[4], &gridBoundaries[quadidx]); quadidx += NUM_QCOEFFS;
    leftIds[regidx] = bndidx++; rightIds[regidx] = -1; regTypeFlags[regidx++] = DBCSG_OUTER;
    PlaneZToQuadric(&Bounds[5], &gridBoundaries[quadidx]); quadidx += NUM_QCOEFFS;
    leftIds[regidx] = bndidx++; rightIds[regidx] = -1; regTypeFlags[regidx++] = DBCSG_INNER;

    // add region between X planes
    int xplanes = regidx;
    leftIds[regidx] = nregions + 0;
    rightIds[regidx] = nregions + 1;
    regTypeFlags[regidx++] = DBCSG_INTERSECT;

    // add region between Y planes
    int yplanes = regidx;
    leftIds[regidx] = nregions + 2;
    rightIds[regidx] = nregions + 3;
    regTypeFlags[regidx++] = DBCSG_INTERSECT;

    // add region between Z planes
    int zplanes = regidx;
    leftIds[regidx] = nregions + 4;
    rightIds[regidx] = nregions + 5;
    regTypeFlags[regidx++] = DBCSG_INTERSECT;

    // add region between X & Y planes
    int xyplanes = regidx;
    leftIds[regidx] = xplanes; 
    rightIds[regidx] = yplanes; 
    regTypeFlags[regidx++] = DBCSG_INTERSECT;

    // add region between X, Y & Z planes
    leftIds[regidx] = xyplanes;
    rightIds[regidx] = zplanes; 
    regTypeFlags[regidx++] = DBCSG_INTERSECT;

    numRegions = regidx;
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
#else // not REPLACE_COMPLEMENT_WITH_DIFF
    leftIds = new int[nregions];
    rightIds = new int[nregions];
    regTypeFlags = new int[nregions];
    for (int i = 0; i < nregions; i++)
    {
        leftIds[i] = lids[i];
        rightIds[i] = rids[i];
        regTypeFlags[i] = typeflags[i];
    }
    numRegions = nregions;
#endif
hrchilds's avatar
hrchilds committed
1423 1424
}

1425 1426
void
vtkCSGGrid::AddZones(int nzones, const int *const zoneIds)
hrchilds's avatar
hrchilds committed
1427 1428 1429 1430 1431 1432 1433 1434
{
    gridZones = new int[nzones];
    for (int i = 0; i < nzones; i++)
        gridZones[i] = zoneIds[i];

    numZones = nzones;
}

hrchilds's avatar
hrchilds committed
1435
//----------------------------------------------------------------------------
1436
// Programmer: Mark C. Miller, Tue Feb 17 17:54:04 PST 2009
1437 1438 1439 1440 1441
//
// Modifications:
//    Mark C. Miller, Tue Feb 17 20:38:14 PST 2009
//    Fixed bug where NUM_QCOEFFS multiplier was missing from loop over
//    boundaries.
1442
//----------------------------------------------------------------------------
1443 1444
bool
vtkCSGGrid::operator==(const vtkCSGGrid &grid) const
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
{
    int i;

    if (numBoundaries != grid.numBoundaries ||
        numRegions != grid.numRegions ||
        numZones != grid.numZones)
        return false;

    for (i = 0; i < numZones; i++)
    {
        if (gridZones[i] != grid.gridZones[i])
            return false;
    }
    for (i = 0; i < numRegions; i++)
    {
        if (regTypeFlags[i] != grid.regTypeFlags[i] ||
            leftIds[i] != grid.leftIds[i] ||
            rightIds[i] != grid.rightIds[i])
            return false;
    }
1465
    for (i = 0; i < NUM_QCOEFFS * numBoundaries; i++)
1466 1467 1468 1469 1470 1471 1472 1473 1474
    {
        if (gridBoundaries[i] != grid.gridBoundaries[i])
            return false;
    }

    return true;
}

//----------------------------------------------------------------------------
hrchilds's avatar
hrchilds committed
1475 1476 1477 1478 1479
// Modifications:
//
//   Hank Childs, Fri Jun  9 12:54:36 PDT 2006
//   Add "default" to switch statement
//
1480
//----------------------------------------------------------------------------
1481 1482
vtkIdType
vtkCSGGrid::AddBoundary(BoundaryType type, int numcoeffs,
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
                                  const double *coeffs)
{
    vtkImplicitFunction *newBoundary = 0;

    switch (type)
    {
        case SPHERE_PR:
        {
            vtkSphere *sphere = vtkSphere::New();

hrchilds's avatar
hrchilds committed
1493 1494
            sphere->SetCenter(const_cast<double*>(coeffs));
            sphere->SetRadius(coeffs[3]);
1495 1496 1497 1498 1499 1500 1501 1502 1503

            newBoundary = sphere;
            break;
        }

        case CYLINDER_PNLR:
        {
            vtkCylinder *cylinder = vtkCylinder::New();

hrchilds's avatar
hrchilds committed
1504 1505 1506 1507 1508
            cylinder->SetCenter((float) coeffs[0] + coeffs[3] * coeffs[6]/2.0,
                                (float) coeffs[1] + coeffs[4] * coeffs[6]/2.0,
                                (float) coeffs[2] + coeffs[5] * coeffs[6]/2.0);
            cylinder->SetRadius((float) coeffs[7]);

1509
            // if the desired cylinder is y-axis aligned, we don't need xform
hrchilds's avatar
hrchilds committed
1510 1511
            if (coeffs[3] != 0.0 || coeffs[5] != 0.0 ||
               (coeffs[4] != 1.0 && coeffs[4] != -1.0))
1512
            {
hrchilds's avatar
hrchilds committed
1513
                double Nx = coeffs[3], Ny = coeffs[4];
hrchilds's avatar
hrchilds committed
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
                double Nz = coeffs[5];
                double Nl = sqrt(Nx*Nx + Ny*Ny + Nz*Nz);

                if (Nl != 0.0)
                {
                    if (Nl != 1.0) // make sure its normalized
                    {
                        Nx /= Nl; Ny /= Nl; Nz /= Nl;
                    }

                    double rotz = acos(Ny) * 180.0 / M_PI;
                    double roty = acos(Nx/sqrt(1-Ny*Ny)) * 180.0 / M_PI;

                    vtkTransform *xform = vtkTransform::New();
                    xform->RotateZ(-rotz);
                    xform->RotateY(-roty);
                    xform->Translate(-coeffs[0], -coeffs[1], -coeffs[2]);

                    cylinder->SetTransform(xform);
                    xform->Delete();
                }
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
            }

            newBoundary = cylinder;
            break;
        }

        case PLANE_X:
        {
            vtkPlane *plane = vtkPlane::New();

hrchilds's avatar
hrchilds committed
1545
            plane->SetOrigin(coeffs[0], 0.0, 0.0);
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
            plane->SetNormal(1.0, 0.0, 0.0);

            newBoundary = plane;
            break;
        }

        case PLANE_Y:
        {
            vtkPlane *plane = vtkPlane::New();

hrchilds's avatar
hrchilds committed
1556
            plane->SetOrigin(0.0, coeffs[0], 0.0);
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
            plane->SetNormal(0.0, 1.0, 0.0);

            newBoundary = plane;
            break;
        }

        case PLANE_Z:
        {
            vtkPlane *plane = vtkPlane::New();

hrchilds's avatar
hrchilds committed
1567
            plane->SetOrigin(0.0, 0.0, coeffs[0]);
1568 1569 1570 1571 1572 1573
            plane->SetNormal(0.0, 0.0, 1.0);

            newBoundary = plane;
            break;
        }

hrchilds's avatar
hrchilds committed
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
        case PLANE_PN:
        {
            vtkPlane *plane = vtkPlane::New();

            plane->SetOrigin((float) coeffs[0], (float) coeffs[1], (float) coeffs[2]);
            plane->SetNormal((float) coeffs[3], (float) coeffs[4], (float) coeffs[5]);

            newBoundary = plane;
            break;
        }

1585 1586 1587
        case QUADRIC_G:
        {
            vtkQuadric *quadric = vtkQuadric::New();
hrchilds's avatar
hrchilds committed
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
            quadric->SetCoefficients( coeffs[0], // x^2 term
                                      coeffs[1], // y^2 term
                                      coeffs[2], // z^2 term
                                      coeffs[3], // xy term
                                      coeffs[4], // yz term
                                      coeffs[5], // xz term
                                      coeffs[6], // x^1 term
                                      coeffs[7], // y^1 term
                                      coeffs[8], // z^1 term
                                      coeffs[9]); // constant term
1598 1599 1600 1601 1602

            newBoundary = quadric;
            break;
        }

hrchilds's avatar
hrchilds committed
1603 1604 1605 1606 1607 1608
        default:
        {
            // No-op, logic below will handle.
            break;
        }

1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    } // switch

    if (newBoundary)
    {
        Boundaries->AddItem(newBoundary);
        newBoundary->Delete();
        funcMap[newBoundary] = Boundaries->GetNumberOfItems()-1;
        return funcMap[newBoundary]; 
    }

    return -1;
}

//----------------------------------------------------------------------------
1623 1624
vtkImplicitFunction *
vtkCSGGrid::GetBoundaryFunc(vtkIdType id) const
1625 1626 1627 1628 1629 1630 1631 1632
{
    if (id >= 0 && id < Boundaries->GetNumberOfItems())
        return vtkImplicitFunction::SafeDownCast(Boundaries->GetItemAsObject(id));
    cerr << "id \"" << id << "\" out of range \"" << Boundaries->GetNumberOfItems() << "\"" << endl;
    return 0;
}

//----------------------------------------------------------------------------
1633 1634
vtkImplicitFunction *
vtkCSGGrid::GetRegionFunc(vtkIdType id) const
1635 1636 1637 1638 1639 1640 1641 1642
{
    if (id >= 0 && id < Regions->GetNumberOfItems())
        return vtkImplicitFunction::SafeDownCast(Regions->GetItemAsObject(id));
    cerr << "id \"" << id << "\" out of range \"" << Regions->GetNumberOfItems() << "\"" << endl;
    return 0;
}

//----------------------------------------------------------------------------
1643 1644
void
vtkCSGGrid::GetBoundary(vtkIdType id, int *type, int *numcoeffs,
1645 1646
                             double **coeffs) const
{
hrchilds's avatar
hrchilds committed
1647
//#warning GetBoundary NOT IMPLEMENTED
1648 1649 1650
}

//----------------------------------------------------------------------------
1651 1652
vtkIdType
vtkCSGGrid::AddRegion(vtkIdType bndId, RegionOp op)
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
{
    vtkImplicitFunction *newRegion = 0;
    vtkImplicitFunction *bnd = GetBoundaryFunc(bndId);

    //
    // INNER means replace the '=' in the equation of the boundary with '<'
    // OUTER means replace the '=' in the equation of the boundary with '>'
    // For planes, INNER means the side against the normal while OUTER 
    // means the side with the normal.
    //

    //
    // For closed objects like sphere and cylinder, OUTER means we need
    // to compliment it. For planes, we need to invert the normal.
    // To make a compliment, we do a diff with Universe.
    //

    bool doBoolDiff = false;
    switch (GetImplicitFuncType(bnd))
    {
hrchilds's avatar
hrchilds committed
1673
        case FUNC_SPHERE:
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
        {
            //
            // Copy the sphere boundary
            //
            vtkSphere *sphereBnd = vtkSphere::SafeDownCast(bnd);
            vtkSphere *sphereReg = vtkSphere::New();
            sphereReg->SetCenter(sphereBnd->GetCenter());
            sphereReg->SetRadius(sphereBnd->GetRadius());

            if (op == OUTER)
                doBoolDiff = true;

            newRegion = sphereReg;
            break;
        }
hrchilds's avatar
hrchilds committed
1689
        case FUNC_PLANE:
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
        {
            //
            // Copy the plane boundary
            //
            vtkPlane *planeBnd = vtkPlane::SafeDownCast(bnd);
            vtkPlane *planeReg = vtkPlane::New();
            planeReg->SetOrigin(planeBnd->GetOrigin());

            //
            // Invert the normal for the OUTER case
            //
            if (op == OUTER)
            {
                planeReg->SetNormal(-planeBnd->GetNormal()[0],
                                    -planeBnd->GetNormal()[1],
                                    -planeBnd->GetNormal()[2]);
            }
            else
            {
                planeReg->SetNormal(planeBnd->GetNormal());
            }

            newRegion = planeReg;
            break;
        }
hrchilds's avatar
hrchilds committed
1715
        case FUNC_CYLINDER:
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
        {
            //
            // Copy the cylinder boundary
            //
            vtkCylinder *cylBnd = vtkCylinder::SafeDownCast(bnd);
            vtkCylinder *cylReg = vtkCylinder::New();
            cylReg->SetCenter(cylBnd->GetCenter());
            cylReg->SetRadius(cylBnd->GetRadius());

            if (op == OUTER)
                doBoolDiff = true;

            newRegion = cylReg;
            break;
        }

hrchilds's avatar
hrchilds committed
1732
        case FUNC_QUADRIC:
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
        {
            //
            //
            //
            vtkQuadric *quadricBnd = vtkQuadric::SafeDownCast(bnd);
            vtkQuadric *quadricReg = vtkQuadric::New();
            quadricReg->SetCoefficients(quadricBnd->GetCoefficients());

            if (op == OUTER)
                doBoolDiff = true;

            newRegion = quadricReg;
            break;
        }

hrchilds's avatar
hrchilds committed
1748 1749 1750 1751 1752 1753
        default:
        {
            // No-op, logic below will handle
            break;
        }

1754 1755 1756 1757 1758 1759
    } // switch

    if (newRegion)
    {
        //
        // Copy the xform if there is one 
hrchilds's avatar
hrchilds committed
1760
        //
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
        vtkTransform *bndXform = vtkTransform::SafeDownCast(bnd->GetTransform());
        if (bndXform)
        {
            vtkTransform *regXform = vtkTransform::New();
            regXform->DeepCopy(bndXform);
            newRegion->SetTransform(regXform);
            regXform->Delete();
        }

        //
        // Diff with universe if necessary
        //
        if (doBoolDiff)
        {
            vtkImplicitBoolean *notReg = vtkImplicitBoolean::New();
            notReg->SetOperationTypeToDifference();
            notReg->AddFunction(Universe);
            notReg->AddFunction(newRegion);
            newRegion->Delete();
            newRegion = notReg;
        }

        Regions->AddItem(newRegion);
        newRegion->Delete();
        funcMap[newRegion] = Regions->GetNumberOfItems()-1;
        return funcMap[newRegion]; 
    }

    return -1;
}

hrchilds's avatar
hrchilds committed
1792 1793 1794 1795 1796 1797
//----------------------------------------------------------------------------
// Modifications:
//
//   Hank Childs, Fri Jun  9 12:54:36 PDT 2006
//   Add "default" to switch statement
//
1798
//----------------------------------------------------------------------------
1799 1800
vtkIdType
vtkCSGGrid::AddRegion(vtkIdType regIdLeft, vtkIdType regIdRight,
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
                                RegionOp op)
{
    vtkImplicitBoolean *boolReg = vtkImplicitBoolean::New();

    vtkImplicitFunction *left = GetRegionFunc(regIdLeft);
    vtkImplicitFunction *right = GetRegionFunc(regIdRight);

    switch (op)
    {
        case INTERSECT: boolReg->SetOperationTypeToIntersection(); break;
        case UNION:     boolReg->SetOperationTypeToUnion();        break;
        case DIFF:      boolReg->SetOperationTypeToDifference();   break;
hrchilds's avatar
hrchilds committed
1813
        default:        break;  // Avoid compiler warning
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
    }

    boolReg->AddFunction(left);
    boolReg->AddFunction(right);

    Regions->AddItem(boolReg);
    boolReg->Delete();
    funcMap[boolReg] = Regions->GetNumberOfItems()-1;
    return funcMap[boolReg]; 
}

hrchilds's avatar
hrchilds committed
1825 1826 1827 1828 1829 1830
//--------------------------------