avtPoint3DFileFormat.C 15.5 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
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
brugger's avatar
 
brugger committed
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
brugger's avatar
 
brugger committed
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
brugger's avatar
 
brugger committed
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 40 41 42 43 44
// ************************************************************************* //
//                             avtPoint3DFileFormat.C                        //
// ************************************************************************* //

#include <avtPoint3DFileFormat.h>

hrchilds's avatar
hrchilds committed
45
#include <visitstream.h>
hrchilds's avatar
hrchilds committed
46

hrchilds's avatar
hrchilds committed
47
#include <vtkCellTypes.h>
hrchilds's avatar
hrchilds committed
48 49 50
#include <vtkUnstructuredGrid.h>
#include <vtkFloatArray.h>

hrchilds's avatar
hrchilds committed
51
#include <avtDatabase.h>
hrchilds's avatar
hrchilds committed
52 53 54 55 56
#include <avtDatabaseMetaData.h>

#include <InvalidFilesException.h>
#include <InvalidVariableException.h>

57
#include <InstallationFunctions.h>
hrchilds's avatar
hrchilds committed
58
#include <DebugStream.h>
59
#include <StringHelpers.h>
hrchilds's avatar
hrchilds committed
60

61 62
#include "visit_gzstream.h"

hrchilds's avatar
hrchilds committed
63 64
const char      *avtPoint3DFileFormat::MESHNAME = "points";

hrchilds's avatar
hrchilds committed
65 66 67
#define COORDINATE_ORDER_DEFAULT 0
#define COORDINATE_ORDER_XYZV    0
#define COORDINATE_ORDER_XYVZ    1
hrchilds's avatar
hrchilds committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

using std::string;
using std::vector;

// ****************************************************************************
//  Method: avtPoint3DFileFormat constructor
//
//  Programmer: Hank Childs
//  Creation:   March 15, 2003
//
//  Modifications:
//
//    Hank Childs, Mon Apr  7 18:21:32 PDT 2003
//    Do not open files in the constructor.
//
// ****************************************************************************

avtPoint3DFileFormat::avtPoint3DFileFormat(const char *fname)
    : avtSTSDFileFormat(fname)
{
    haveReadData = false;
    column1 = NULL;
90
    column2 = NULL;
hrchilds's avatar
hrchilds committed
91 92 93 94 95 96 97 98 99 100 101 102
    column3 = NULL;
    column4 = NULL;
    points  = NULL;
}


// ****************************************************************************
//  Method: avtPoint3DFileFormat destructor
//
//  Programmer: Hank Childs
//  Creation:   March 15, 2003
//
hrchilds's avatar
hrchilds committed
103 104 105 106 107
//  Modifications:
//
//    Hank Childs, Sat Mar 17 16:29:45 PDT 2007
//    Call FreeUpResources instead of doing destructing here.
//
hrchilds's avatar
hrchilds committed
108 109 110 111
// ****************************************************************************

avtPoint3DFileFormat::~avtPoint3DFileFormat()
{
hrchilds's avatar
hrchilds committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    FreeUpResources();
}


// ****************************************************************************
//  Method: avtPoint3DFileFormat::FreeUpResources
//
//  Purpose:
//      Frees up the resources associated with this format's file.
//
//  Programmer: Hank Childs
//  Creation:   March 17, 2007
//
// ****************************************************************************

void
avtPoint3DFileFormat::FreeUpResources(void)
{
    haveReadData = false;

hrchilds's avatar
hrchilds committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    if (column1 != NULL)
    {
        column1->Delete();
        column1 = NULL;
    }
    if (column2 != NULL)
    {
        column2->Delete();
        column2 = NULL;
    }
    if (column3 != NULL)
    {
        column3->Delete();
        column3 = NULL;
    }
    if (column4 != NULL)
    {
        column4->Delete();
        column4 = NULL;
    }
    if (points != NULL)
    {
        points->Delete();
        points = NULL;
    }
}


// ****************************************************************************
//  Method: avtPoint3DFileFormat::GetMesh
//
//  Purpose:
//      Gets the mesh (a bunch of 3D points).
//
//  Notes:   The calling function believes that it owns the returned memory.
//
//  Programmer: Hank Childs
//  Creation:   March 15, 2003
//
// ****************************************************************************

vtkDataSet *
avtPoint3DFileFormat::GetMesh(const char *mesh)
{
    if (!haveReadData)
    {
        ReadData();
    }

    if (strcmp(mesh, MESHNAME) != 0)
    {
        EXCEPTION1(InvalidVariableException, mesh);
    }

    points->Register(NULL);
    return points;
}


// ****************************************************************************
//  Method; avtPoint3DFileFormat::GetVar
//
//  Purpose:
//      Gets a variable.
//
//  Notes:   The calling function believes that it owns the returned memory.
//
//  Programmer: Hank Childs
//  Creation:   March 15, 2003
//
// ****************************************************************************

vtkDataArray *
avtPoint3DFileFormat::GetVar(const char *var)
{
    if (!haveReadData)
    {
        ReadData();
    }

    vtkDataArray *rv = NULL;
    if (varnames[0] == var)
        rv = column1;
    if (varnames[1] == var)
        rv = column2;
    if (varnames[2] == var)
        rv = column3;
    if (varnames[3] == var)
        rv = column4;

    if (rv == NULL)
    {
        EXCEPTION1(InvalidVariableException, var);
    }

    rv->Register(NULL);
    return rv;
}


// ****************************************************************************
//  Method: avtPoint3DFileFormat::PopulateDatabaseMetaData
//
//  Purpose:
//      Declares which variables are available for this file format.
//
//  Programmer: Hank Childs
//  Creation:   March 15, 2003
//
//  Modifications:
//
//    Hank Childs, Mon Apr  7 18:21:32 PDT 2003
//    Read in the variable names before populating the meta-data.
//
// ****************************************************************************

void
avtPoint3DFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData *md)
{
    if (!haveReadData)
    {
        ReadData();
    }

    avtMeshMetaData *mesh = new avtMeshMetaData;
    mesh->name = MESHNAME;
    mesh->meshType = AVT_POINT_MESH;
    mesh->blockOrigin = 0;
    mesh->spatialDimension = 3;
    mesh->topologicalDimension = 0;
    mesh->hasSpatialExtents = false;
    md->Add(mesh);

    for (int i = 0 ; i < 4 ; i++)
    {
        AddScalarVarToMetaData(md, varnames[i], MESHNAME, AVT_NODECENT, NULL);
    }
}


// ****************************************************************************
//  Method: avtPoint3DFileFormat::ReadData
//
//  Purpose:
//      Does the work of actually reading in the data.
//
//  Programmer: Hank Childs
//  Creation:   March 15, 2003
//
//  Modifications:
//
//    Hank Childs, Mon Apr  7 18:21:32 PDT 2003
//    Read in the variable names as well.
//
//    Hank Childs, Wed May  7 08:45:08 PDT 2003 
//    We are consistently adding one bogus point at the end of the array.
//    Remove that.
//
hrchilds's avatar
hrchilds committed
290 291 292 293
//    Hank Childs, Wed Oct  8 15:36:36 PDT 2003
//    Removed another bogus line, this time it was at the beginning, with
//    the variable declaration.
//
hrchilds's avatar
hrchilds committed
294 295 296 297
//    Brad Whitlock, Mon Jun 5 10:58:49 PDT 2006
//    Added support for a config file as well as setting coordflag in the
//    file itself.
//
hrchilds's avatar
hrchilds committed
298 299 300
//    Hank Childs, Sat Mar 17 16:29:45 PDT 2007
//    Make reading for meta-data more lightweight.
//
301 302 303
//    Jeremy Meredith, Thu Jan  7 12:04:12 EST 2010
//    Check some lines to make sure it's ASCII.
//
304 305 306
//    Jeremy Meredith, Fri Jan  8 16:17:34 EST 2010
//    Added extra error checking and forced more parsing in strict mode.
//
307 308 309
//    Jeremy Meredith, Wed Jan 20 10:29:11 EST 2010
//    Check for eof after getline, but before we use the result.
//
310 311 312 313
//    Jeremy Meredith, Mon Feb 22 11:11:01 EST 2010
//    Only do extra ascii checks in strict mode, and never read too many
//    lines if we're in the mdserver, even in strict mode.
//
brugger's avatar
 
brugger committed
314 315 316 317 318
//    Eric Brugger, Mon Nov 15 14:19:32 PST 2010
//    Corrected a bug where the last point was being ignored.  Previously,
//    there was code to decrement the number of points read by one, but this
//    was no longer necessary because of Jeremy's change on Jan 20, 2010.
//
hrchilds's avatar
hrchilds committed
319 320 321 322 323
// ****************************************************************************

void
avtPoint3DFileFormat::ReadData(void)
{
hrchilds's avatar
hrchilds committed
324
    const char *mName = "avtPoint3DFileFormat::ReadData: ";
hrchilds's avatar
hrchilds committed
325 326
    int      i;

327 328
    visit_ifstream ifile(filename);
    if (ifile().fail())
hrchilds's avatar
hrchilds committed
329 330 331 332
    {
        EXCEPTION1(InvalidFilesException, filename);
    }

hrchilds's avatar
hrchilds committed
333 334 335 336 337
    // Read the coordinate ordering out of the file.
    int coordFlag = COORDINATE_ORDER_XYZV;
    ReadConfigFile(coordFlag);

    // Read the variable names.
hrchilds's avatar
hrchilds committed
338 339
    for (i = 0 ; i < 4 ; i++)
    {
hrchilds's avatar
hrchilds committed
340
        char buf[1024];
341
        ifile() >> buf;
342
        if (GetStrictMode() && !StringHelpers::IsPureASCII(buf, 1024))
343
            EXCEPTION2(InvalidFilesException, filename, "Not ASCII.");
hrchilds's avatar
hrchilds committed
344
        varnames.push_back(buf);
hrchilds's avatar
hrchilds committed
345 346
    }

347 348
    // If we're in the mdserver and not in strict mode, we can
    // assume the file's valid.  Otherwise, read a little more.
349
    if (avtDatabase::OnlyServeUpMetaData() && !GetStrictMode())
hrchilds's avatar
hrchilds committed
350 351
        return;

hrchilds's avatar
hrchilds committed
352
    char     line[1024];
hrchilds's avatar
hrchilds committed
353 354 355

    // We read out the four variables, but not the newline at the end.
    // Get that now.
356
    ifile().getline(line, 1024);
357 358
    if (!StringHelpers::IsPureASCII(line, 1024))
        EXCEPTION2(InvalidFilesException, filename, "Not ASCII.");
hrchilds's avatar
hrchilds committed
359

hrchilds's avatar
hrchilds committed
360 361 362 363 364
    vector<float> var1;
    vector<float> var2;
    vector<float> var3;
    vector<float> var4;

365
    int linesToCheck = 100;
366 367

    line[0] = '\0';
368
    ifile().getline(line, 1024);
369
    if (GetStrictMode() && !StringHelpers::IsPureASCII(line, 1024))
370
        EXCEPTION2(InvalidFilesException, filename, "Not ASCII.");
371
    while (!ifile().eof())
hrchilds's avatar
hrchilds committed
372
    {
hrchilds's avatar
hrchilds committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        // Allow the user to specify "coordflag" in the file.
        if(line[0] == '#')
        {
            if(strncmp(line+1, "coordflag", 9) == 0)
            {
                debug4 << mName << "Reading coordflag value from file: ";
                if(strncmp(line+9+2, "xyzv", 4) == 0)
                {
                    coordFlag = COORDINATE_ORDER_XYZV;
                    debug4 << "xyzv";
                }
                else if(strncmp(line+9+2, "xyvz", 4) == 0)
                {
                    coordFlag = COORDINATE_ORDER_XYVZ;
                    debug4 << "xyvz";
                }
                debug4 << endl;
            }
        }
        else
        {
394 395 396 397 398 399 400
            float a=0, b=0, c=0, d=0;
            int n = sscanf(line, "%f %f %f %f", &a, &b, &c, &d);
            if (GetStrictMode() && n != 4)
            {
                EXCEPTION2(InvalidFilesException, filename,
                           "Bad line in file; less than four values");
            }
hrchilds's avatar
hrchilds committed
401 402 403 404 405
            var1.push_back(a);
            var2.push_back(b);
            var3.push_back(c);
            var4.push_back(d);
        }
406 407

        line[0] = '\0';
408
        ifile().getline(line, 1024);
409 410
        --linesToCheck;
        if (GetStrictMode() && linesToCheck > 0 &&
411
            !StringHelpers::IsPureASCII(line, 1024))
412 413 414 415 416 417 418 419 420 421
        {
                EXCEPTION2(InvalidFilesException, filename, "Not ASCII.");
        }
        else if (linesToCheck <= 0 && avtDatabase::OnlyServeUpMetaData())
        {
            // We've now checked the first ~100 lines; the mdserver
            // is probably safe to call this a point3d file.
            return;
        }
            
hrchilds's avatar
hrchilds committed
422 423
    }

bonnell's avatar
bonnell committed
424
    int npts = (int)var1.size();
hrchilds's avatar
hrchilds committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

    column1 = vtkFloatArray::New();
    column1->SetNumberOfTuples(npts);
    for (i = 0 ; i < npts ; i++)
    {
        column1->SetTuple1(i, var1[i]);
    }

    column2 = vtkFloatArray::New();
    column2->SetNumberOfTuples(npts);
    for (i = 0 ; i < npts ; i++)
    {
        column2->SetTuple1(i, var2[i]);
    }

    column3 = vtkFloatArray::New();
    column3->SetNumberOfTuples(npts);
    for (i = 0 ; i < npts ; i++)
    {
        column3->SetTuple1(i, var3[i]);
    }

    column4 = vtkFloatArray::New();
    column4->SetNumberOfTuples(npts);
    for (i = 0 ; i < npts ; i++)
    {
        column4->SetTuple1(i, var4[i]);
    }

    vtkPoints *p = vtkPoints::New();
    p->SetNumberOfPoints(npts);
hrchilds's avatar
hrchilds committed
456 457 458 459 460 461
    if(coordFlag == COORDINATE_ORDER_XYZV)
    {
        for (i = 0 ; i < npts ; i++)
             p->SetPoint(i, var1[i], var2[i], var3[i]);
    }
    else // COORDINATE_ORDER_XYVZ
hrchilds's avatar
hrchilds committed
462
    {
hrchilds's avatar
hrchilds committed
463 464
        for (i = 0 ; i < npts ; i++)
             p->SetPoint(i, var1[i], var2[i], var4[i]);
hrchilds's avatar
hrchilds committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    }

    points = vtkUnstructuredGrid::New();
    points->SetPoints(p);
    p->Delete();

    points->Allocate(npts);
    vtkIdType onevertex[1];
    for (i = 0 ; i < npts ; i++)
    {
         onevertex[0] = i;
         points->InsertNextCell(VTK_VERTEX, 1, onevertex);
    }

    haveReadData = true;
}

hrchilds's avatar
hrchilds committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
// ****************************************************************************
// Method: avtPoint3DFileFormat::ReadConfigFile
//
// Purpose: 
//   Reads a config file out of the home VisIt directory that allows the user
//   to configure the Point3D file format.
//
// Arguments:
//   coordFlag : The order of the coordinates.
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Mon Jun 5 10:51:59 PDT 2006
//
// Modifications:
//   
// ****************************************************************************
hrchilds's avatar
hrchilds committed
502

hrchilds's avatar
hrchilds committed
503 504 505 506 507 508 509 510 511 512 513 514
bool
avtPoint3DFileFormat::ReadConfigFile(int &coordFlag)
{
    const char *mName = "avtPoint3DFileFormat::ReadConfigFile: ";
    bool retval = false;

    std::string configFile(GetUserVisItDirectory());
    configFile += "Point3D.ini";
 
    coordFlag = COORDINATE_ORDER_DEFAULT;
 
    // Open the file.
515 516
    visit_ifstream ifile(configFile.c_str());
    if (ifile().fail())
hrchilds's avatar
hrchilds committed
517 518 519 520 521 522 523 524 525 526
    {
        debug4 << mName << "Could not open config file: "
               << configFile.c_str() << endl;
    }
    else
    {
        debug4 << mName << "Opened config file: "
               << configFile.c_str() << endl;

        char line[1024];
527
        for(int lineIndex = 0; !ifile().eof(); ++lineIndex)
hrchilds's avatar
hrchilds committed
528 529
        {
            // Get the line
530
            ifile().getline(line, 1024);
hrchilds's avatar
hrchilds committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

            if(strncmp(line, "coordflag", 9) == 0)
            {
                debug4 << mName << "Reading coordflag value from file: ";
                if(strncmp(line+9+1, "xyzv", 4) == 0)
                {
                    coordFlag = COORDINATE_ORDER_XYZV;
                    debug4 << "xyzv";
                }
                else if(strncmp(line+9+1, "xyvz", 4) == 0)
                {
                    coordFlag = COORDINATE_ORDER_XYVZ;
                    debug4 << "xyvz";
                }
                debug4 << endl;
            }
        }

        retval = true;
    }

    debug4 << mName << "coordFlag=" << coordFlag << endl;

    return retval;
}