vtkRectilinearLinesNoDataFilter.C 12.2 KB
Newer Older
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2017, Lawrence Livermore National Security, LLC
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* 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
*    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
*    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
* 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
* 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.
*
*****************************************************************************/

#include <vtkRectilinearLinesNoDataFilter.h>

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

#include <ImproperUseException.h>

using  std::vector;

#define AddLineToPolyData(ai,aj,ak, bi,bj,bk)                                 \
{                                                                             \
bonnell's avatar
bonnell committed
58 59 60 61
    p.SetComponent(0, x.GetComponent(ai));                                    \
    p.SetComponent(1, y.GetComponent(aj));                                    \
    p.SetComponent(2, z.GetComponent(ak));                                    \
    ++p;                                                                      \
62
    outPointData->CopyData(inPointData, ((ak*nY) + aj)*nX + ai, pointId++);   \
bonnell's avatar
bonnell committed
63 64 65 66
    p.SetComponent(0, x.GetComponent(bi));                                    \
    p.SetComponent(1, y.GetComponent(bj));                                    \
    p.SetComponent(2, z.GetComponent(bk));                                    \
    ++p;                                                                      \
67 68 69 70 71 72 73
    outPointData->CopyData(inPointData, ((bk*nY) + bj)*nX + bi, pointId++);   \
    *nl++ = 2;                                                                \
    *nl++ = pointId-2;                                                        \
    *nl++ = pointId-1;                                                        \
    cellId++;                                                                 \
}

bonnell's avatar
bonnell committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 162 163 164 165 166 167 168 169

template <class Accessor> inline void
vtkRectilinearLinesNoDataFilter_AddLines(int nX, int nY, int nZ,
    vtkIdType *nl, vtkPointData *outPointData, vtkPointData *inPointData,
    Accessor x, Accessor y, Accessor z, Accessor p)
{
    //
    // And now actually create the points/lines
    //
    int pointId = 0;
    int cellId = 0;

    p.InitTraversal();

    // This case is mutually exclusive with the other ones below....
    if ((nX==1 && nY==1) || (nX==1 && nZ==1) || (nY==1 && nZ==1))
    {
        AddLineToPolyData(0,0,0, nX-1,nY-1,nZ-1);
    }

    if (nX>1 && nY>1)
    {
        // even if nz==1
        {
            // Front, Top to Bottom
            for (int i = 0 ; i < nX ; i++)
                AddLineToPolyData(i   ,0   ,0   ,       i   ,nY-1,0   );

            // Front, Left to Right
            for (int j = 0 ; j < nY ; j++)
                AddLineToPolyData(0   ,j   ,0   ,       nX-1,j   ,0   );
        }

        if (nZ>1)
        {
            // Back, Top to Bottom
            for (int i = 0 ; i < nX ; i++)
                AddLineToPolyData(i   ,0   ,nZ-1,       i   ,nY-1,nZ-1);

            // Back, Left to Right
            for (int j = 0 ; j < nY ; j++)
                AddLineToPolyData(0   ,j   ,nZ-1,       nX-1,j   ,nZ-1);
        }
    }

    if (nX>1 && nZ>1)
    {
        // even if ny==1
        {
            // Top, Front to Back
            for (int i = 0 ; i < nX ; i++)
                AddLineToPolyData(i   ,0   ,0   ,       i   ,0   ,nZ-1);

            // Top, Left to Right
            for (int k = 0 ; k < nZ ; k++)
                AddLineToPolyData(0   ,0   ,k   ,       nX-1,0   ,k   );
        }

        if (nY>1)
        {
            // Bottom, Front to Back
            for (int i = 0 ; i < nX ; i++)
                AddLineToPolyData(i   ,nY-1,0   ,       i   ,nY-1,nZ-1);

            // Bottom, Left to Right
            for (int k = 0 ; k < nZ ; k++)
                AddLineToPolyData(0   ,nY-1,k   ,       nX-1,nY-1,k   );
        }
    }

    if (nY>1 && nZ>1)
    {
        // even if nx==1
        {
            // Left, Front to Back
            for (int j = 0 ; j < nY ; j++)
                AddLineToPolyData(0   ,j   ,0   ,       0   ,j   ,nZ-1);

            // Left, Top to Bottom
            for (int k = 0 ; k < nZ ; k++)
                AddLineToPolyData(0   ,0   ,k   ,       0   ,nY-1,k   );
        }

        if (nX>1)
        {
            // Right, Front to Back
            for (int j = 0 ; j < nY ; j++)
                AddLineToPolyData(nX-1,j   ,0   ,       nX-1,j   ,nZ-1);

            // Right, Top to Bottom
            for (int k = 0 ; k < nZ ; k++)
                AddLineToPolyData(nX-1,0   ,k   ,       nX-1,nY-1,k   );
        }
    }
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#undef AddLineToPolyData

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


vtkRectilinearLinesNoDataFilter::vtkRectilinearLinesNoDataFilter()
{
}

190 191

// ****************************************************************************
192
//  Method: vtkRectilinearLinesNoDataFilter::RequestData
193 194 195
//
// ****************************************************************************

196 197 198 199 200
int
vtkRectilinearLinesNoDataFilter::RequestData(
    vtkInformation *vtkNotUsed(request),
    vtkInformationVector **inputVector,
    vtkInformationVector *outputVector)
201
{
202 203 204 205 206 207 208 209 210 211 212 213
    // get the info objects
    vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
    vtkInformation *outInfo = outputVector->GetInformationObject(0);

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

214 215 216
    //
    // Set up some objects that we will be using throughout the process.
    //
217
    vtkPolyData        *outPD        = vtkPolyData::New();
218
    vtkPointData       *inPointData  = input->GetPointData();
219 220
    vtkCellData        *outCellData  = outPD->GetCellData();
    vtkPointData       *outPointData = outPD->GetPointData();
221 222 223 224 225 226

    //
    // Get the information about X, Y, and Z from the rectilinear grid.
    //
    vtkDataArray *xc = input->GetXCoordinates();
    int nX = xc->GetNumberOfTuples();
bonnell's avatar
bonnell committed
227
    int tX = xc->GetDataType();
228 229
    vtkDataArray *yc = input->GetYCoordinates();
    int nY = yc->GetNumberOfTuples();
bonnell's avatar
bonnell committed
230
    int tY = yc->GetDataType();
231 232
    vtkDataArray *zc = input->GetZCoordinates();
    int nZ = zc->GetNumberOfTuples();
bonnell's avatar
bonnell committed
233 234 235 236 237
    int tZ = zc->GetDataType();

    bool same = (tX == tY && tY == tZ); 
    int type = (same ? tX : (tX == VTK_DOUBLE ? tX : (tY == VTK_DOUBLE ? tY : 
               (tZ == VTK_DOUBLE ? tZ : VTK_FLOAT))));
238 239 240 241

    //
    // Count points.  Be wary of 1D and 2D dimensions.
    //
bonnell's avatar
bonnell committed
242
    vtkPoints *pts = vtkPoints::New(type);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    int npts = 0;
    if ((nX==1 && nY==1) || (nX==1 && nZ==1) || (nY==1 && nZ==1))
    {
        npts = 2;
    }
    else if (nX == 1)
    {
        npts = nY*2 + nZ*2;
    }
    else if (nY == 1)
    {
        npts = nX*2 + nZ*2;
    }
    else if (nZ == 1)
    {
        npts = nX*2 + nY*2;
    }
    else
    {
        npts = nX*4 + nY*4 + nZ*4;
    }
    pts->SetNumberOfPoints(npts);

    //
    // Count the cells
    //
    int   numOutCells = 0;
    if ((nX==1 && nY==1) || (nX==1 && nZ==1) || (nY==1 && nZ==1))
    {
        numOutCells = 1;
    }
    else if (nX == 1)
    {
        numOutCells = nY + nZ;
    }
    else if (nY == 1)
    {
        numOutCells = nX + nZ;
    }
    else if (nZ == 1)
    {
        numOutCells = nX + nY;
    }
    else
    {
        numOutCells = nX*2 + nY*2 + nZ*2;
    }

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

    //
    // And set up the cell arrays for creation (but not copying data)
    vtkCellArray *polys = vtkCellArray::New();
    vtkIdTypeArray *list = vtkIdTypeArray::New();
    list->SetNumberOfValues(numOutCells*(2+1));
    vtkIdType *nl = list->GetPointer(0);

    //
    // And now actually create the points/lines
    //
bonnell's avatar
bonnell committed
306
    if (same && type == VTK_FLOAT)
307
    {
bonnell's avatar
bonnell committed
308 309 310 311 312 313
        vtkRectilinearLinesNoDataFilter_AddLines(nX, nY, nZ,
            nl, outPointData, inPointData,
            vtkDirectAccessor<float>(xc), 
            vtkDirectAccessor<float>(yc), 
            vtkDirectAccessor<float>(zc), 
            vtkDirectAccessor<float>(pts->GetData())); 
314
    }
bonnell's avatar
bonnell committed
315
    else if (same && type == VTK_DOUBLE)
316
    {
bonnell's avatar
bonnell committed
317 318 319 320 321 322
        vtkRectilinearLinesNoDataFilter_AddLines(nX, nY, nZ,
            nl, outPointData, inPointData,
            vtkDirectAccessor<double>(xc), 
            vtkDirectAccessor<double>(yc), 
            vtkDirectAccessor<double>(zc), 
            vtkDirectAccessor<double>(pts->GetData())); 
323
    }
bonnell's avatar
bonnell committed
324
    else
325
    {
bonnell's avatar
bonnell committed
326 327 328 329 330 331
        vtkRectilinearLinesNoDataFilter_AddLines(nX, nY, nZ,
            nl, outPointData, inPointData,
            vtkGeneralAccessor(xc), 
            vtkGeneralAccessor(yc), 
            vtkGeneralAccessor(zc), 
            vtkGeneralAccessor(pts->GetData())); 
332 333 334 335 336
    }

    //
    // Clean up.....
    //
337
    outPD->SetPoints(pts);
338 339 340 341 342
    pts->Delete();
  
    polys->SetCells(numOutCells, list);
    list->Delete();
    outCellData->Squeeze();
343
    outPD->SetLines(polys);
344 345
    polys->Delete();

346 347
    output->ShallowCopy(outPD);
    output->GetFieldData()->ShallowCopy(input->GetFieldData());
348

349
    outPD->Delete();
350
    return 1;
351 352
}

353 354 355 356 357 358 359 360 361 362 363 364 365

// ****************************************************************************
//  Method: vtkRectilinearLinesNoDataFilter::FillInputPortInformation
//
// ****************************************************************************

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


368 369 370 371
// ****************************************************************************
//  Method: vtkRectilinearLinesNoDataFilter::PrintSelf
//
// ****************************************************************************
372

373 374
void
vtkRectilinearLinesNoDataFilter::PrintSelf(ostream& os, vtkIndent indent)
375 376 377
{
    this->Superclass::PrintSelf(os,indent);
}