[cig-commits] r9066 - cs/benchmark/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Jan 16 11:32:12 PST 2008


Author: luis
Date: 2008-01-16 11:32:12 -0800 (Wed, 16 Jan 2008)
New Revision: 9066

Modified:
   cs/benchmark/cigma/trunk/src/vtkls.cpp
Log:
Added various improvements to vtkls.cpp

Modified: cs/benchmark/cigma/trunk/src/vtkls.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/vtkls.cpp	2008-01-16 19:32:11 UTC (rev 9065)
+++ cs/benchmark/cigma/trunk/src/vtkls.cpp	2008-01-16 19:32:12 UTC (rev 9066)
@@ -3,35 +3,157 @@
 #include <cassert>
 
 #include "AnyOption.h"
-//#include "VtkUgReader.h"
 
 #include <vtkDataSetReader.h>
 #include <vtkDataSet.h>
 #include <vtkPointData.h>
 #include <vtkCellData.h>
 
+#include <vtkDataArray.h>
 
 using namespace std;
 
 
+typedef enum {
+    VTK_FILE_NONE,
+    VTK_FILE_POLYDATA,
+    VTK_FILE_STRUCTURED_POINTS,
+    VTK_FILE_STRUCTURED_GRID,
+    VTK_FILE_UNSTRUCTURED_GRID,
+    VTK_FILE_RECTILINEAR_GRID
+} VtkFileType;
+
+VtkFileType getFileType(vtkDataSetReader *reader)
+{
+    // XXX: are these mutually exclusive?
+    if (reader->IsFilePolyData())
+        return VTK_FILE_POLYDATA;
+    if (reader->IsFileStructuredPoints())
+        return VTK_FILE_STRUCTURED_POINTS;
+    if (reader->IsFileStructuredGrid())
+        return VTK_FILE_STRUCTURED_GRID;
+    if (reader->IsFileUnstructuredGrid())
+        return VTK_FILE_UNSTRUCTURED_GRID;
+    if (reader->IsFileRectilinearGrid())
+        return VTK_FILE_RECTILINEAR_GRID;
+    return VTK_FILE_NONE;
+}
+
+const char *getFileTypeName(VtkFileType fileType)
+{
+    if (fileType == VTK_FILE_POLYDATA)
+        return "polydata";
+    if (fileType == VTK_FILE_STRUCTURED_POINTS)
+        return "structured points";
+    if (fileType == VTK_FILE_STRUCTURED_GRID)
+        return "structured grid";
+    if (fileType == VTK_FILE_UNSTRUCTURED_GRID)
+        return "unstructured grid";
+    if (fileType == VTK_FILE_RECTILINEAR_GRID)
+        return "rectilinear grid";
+    return "";
+}
+
+
 
 // ---------------------------------------------------------------------------
 int main(int argc, char *argv[])
 {
-    AnyOption *opt = new AnyOption();
+    bool verbose = false;
+    bool by_class = false;
+    bool by_arrays = true;
 
+    int i,n;
+
+    // ------------------------------------------------------
+    /*
     if (argc != 2)
     {
         cerr << "Usage: " << argv[0] << " " << "file.vtk" << endl;
         exit(1);
     }
-    cout << "Reading " << argv[1] << endl;
+    // */
 
     // ------------------------------------------------------
-    cout << "<< Creating vtkDataSetReader" << endl;
+
+    AnyOption *opt = new AnyOption();
+
+    opt->addUsage("Usage: vtkls file.vtk");
+
+    opt->setFlag("help", 'h');
+    opt->setFlag("verbose", 'v');
+    opt->setFlag('c'); // sort by class
+    opt->setFlag('a'); // sort by array
+
+    opt->processCommandArgs(argc, argv);
+
+    if (!opt->hasOptions())
+    {
+        opt->printUsage();
+        delete opt;
+        exit(1);
+    }
+
+    if (opt->getFlag("help"))
+    {
+        opt->printUsage();
+        delete opt;
+        exit(0);
+    }
+
+    if (opt->getFlag("verbose"))
+    {
+        verbose = true;
+    }
+
+    if (opt->getFlag('c') && !opt->getFlag('a'))
+    {
+        by_class = true;
+        by_arrays = false;
+    }
+    if (opt->getFlag('a') && !opt->getFlag('c'))
+    {
+        by_arrays = true;
+        by_class = false;
+    }
+    int nargs = opt->getArgc();
+    if (nargs != 1)
+    {
+        opt->printUsage();
+        delete opt;
+        exit(1);
+    }
+    char *filename = opt->getArgv(0);
+
+
+    // ------------------------------------------------------
+    // 
+    // Assuming legacy format
+    //
+    if (verbose)
+    {
+        cout << "<< Creating vtkDataSetReader" << endl;
+    }
     vtkDataSetReader *reader = vtkDataSetReader::New();
-    reader->SetFileName(argv[1]);
+    reader->SetFileName(filename);
+    cout << "Reading " << filename << endl;
 
+    int ierr;
+    ierr = reader->OpenVTKFile();   // does file exist?
+    if (ierr == 0)
+    {
+        cerr << "vtkls: Could not open " << filename << endl;
+        exit(1);
+    }
+
+    ierr = reader->ReadHeader();    // is the vtk header present?
+    if (ierr == 0)
+    {
+        cerr << "vtkls: Unrecognized file " << filename << endl;
+        exit(1);
+    }
+
+    /* // Determine type of file (direct method)
     bool is_pd = reader->IsFilePolyData();
     cout << "Is file poly data? " << is_pd << endl;
     bool is_sp = reader->IsFileStructuredPoints();
@@ -43,77 +165,158 @@
     bool is_rg = reader->IsFileRectilinearGrid();
     cout << "Is file rectilinear grid? " << is_rg << endl;
     bool is_valid = is_pd || is_sp || is_sg || is_ug || is_rg;
-    assert(is_valid);
+    assert(is_valid); // */
 
+    /* // Determine type of file (second method)
+    VtkFileType fileType = getFileType(reader);
+    const char *fileTypeName = getFileTypeName(fileType);
+    cout << "Detected " << fileTypeName << " file" << endl;
+    // */
 
-    //cout << reader->GetHeader() << endl;
     
 
+    // ------------------------------------------------------
+
     reader->Update();
     //reader->PrintSelf(cout, 0);
 
-
-    
-
-    int i,n;
-
-    n = reader->GetNumberOfFieldDataInFile();
-    cout << "Number of field data = " << n << endl;
-    for (i = 0; i < n; i++)
+    if (verbose)
     {
-        const char *name = reader->GetFieldDataNameInFile(i);
-        cout << "\t" << name << endl;
+        cout << "<< Creating vtkDataSet" << endl;
     }
+    vtkDataSet *dataset = reader->GetOutput();
+    //dataset->PrintSelf(cout, 0);
 
-    n = reader->GetNumberOfScalarsInFile();
-    cout << "Number of scalars = " << n << endl;
-    for (i = 0; i < n; i++)
+    if (verbose)
     {
-        const char *name = reader->GetScalarsNameInFile(i);
-        cout << "\t" << name << endl;
+        cout << "<< Creating vtkPointData" << endl;
     }
+    vtkPointData *pointData = dataset->GetPointData();
+    //pointData->PrintSelf(cout, 0);
 
-    n = reader->GetNumberOfVectorsInFile();
-    cout << "Number of vectors = " << n << endl;
-    for (i = 0; i < n; i++)
+    if (verbose)
     {
-        const char *name = reader->GetVectorsNameInFile(i);
-        cout << "\t" << name << endl;
+        cout << "<< Creating vtkCellData" << endl;
     }
+    vtkCellData *cellData = dataset->GetCellData();
+    //cellData->PrintSelf(cout, 0);
 
-    n = reader->GetNumberOfTensorsInFile();
-    cout << "Number of tensors = " << n << endl;
-    for (i = 0; i < n; i++)
-    {
-        const char *name = reader->GetTensorsNameInFile(i);
-        cout << "\t" << name << endl;
-    }
 
     // ------------------------------------------------------
-    cout << "<< Creating vtkDataSet" << endl;
-    vtkDataSet *dataset = reader->GetOutput();
-    //dataset->PrintSelf(cout, 0);
+    int nno,nel;
 
-    int nno,nel;
     nno = dataset->GetNumberOfPoints();
+    if (nno > 0)
+    {
+        cout << "Points = " << nno << endl;
+    }
+
     nel = dataset->GetNumberOfCells();
-    cout << "Number of points = " << nno << endl;
-    cout << "Number of cells = " << nel << endl;
+    if (nel > 0)
+    {
+        cout << "Cells = " << nel << endl;
+    }
 
 
     // ------------------------------------------------------
-    cout << "<< Creating vtkPointData" << endl;
-    vtkPointData *pointData = dataset->GetPointData();
-    //pointData->PrintSelf(cout, 0);
+    int numArrays;
+    int numTuples, numComponents;
 
+    if (by_arrays)
+    {
+        //*
+        numArrays = pointData->GetNumberOfArrays();
+        if (numArrays > 0)
+        {
+            //cout << "PointData" << endl;
+            //cout << "\tNumber of arrays = " << numArrays << endl;
+            for (i = 0; i < numArrays; i++)
+            {
+                vtkDataArray *dataArray = pointData->GetArray(i);
+                const char *name = pointData->GetArrayName(i);
+                numTuples = dataArray->GetNumberOfTuples();
+                numComponents = dataArray->GetNumberOfComponents();
+                cout << "PointDataArray[" << i << "] = " << name;
+                //cout << "  " << dataArray->GetClassName();
+                cout << "  " << "(" << numTuples << " x " << numComponents << ")";
+                cout << endl;
+            }
+        }
+
+        numArrays = cellData->GetNumberOfArrays();
+        if (numArrays > 0)
+        {
+            //cout << "CellData" << endl;
+            //cout << "\tNumber of arrays = " << numArrays << endl;
+            for (i = 0; i < numArrays; i++)
+            {
+                vtkDataArray *dataArray = cellData->GetArray(i);
+                const char *name = cellData->GetArrayName(i);
+                numTuples = dataArray->GetNumberOfTuples();
+                numComponents = dataArray->GetNumberOfComponents();
+                cout << "CellDataArray[" << i << "] = " << name;
+                //cout << "  " << dataArray->GetClassName();
+                cout << "  " << "(" << numTuples << " x " << numComponents << ")";
+                cout << endl;
+            }
+        }
+        // */
+    }
+
+
+
+
     // ------------------------------------------------------
-    cout << "<< Creating vtkCellData" << endl;
-    vtkCellData *cellData = dataset->GetCellData();
-    //cellData->PrintSelf(cout, 0);
 
+    if (by_class)
+    {
+        //*
+        n = reader->GetNumberOfFieldDataInFile();
+        //cout << "Number of field data = " << n << endl;
+        for (i = 0; i < n; i++)
+        {
+            const char *name = reader->GetFieldDataNameInFile(i);
+            cout << "FieldData[" << i << "] = " << name;
+            cout << endl;
+        }
+
+        n = reader->GetNumberOfScalarsInFile();
+        //cout << "Number of scalars = " << n << endl;
+        for (i = 0; i < n; i++)
+        {
+            const char *name = reader->GetScalarsNameInFile(i);
+            vtkDataArray *dataArray = pointData->GetArray(i);
+            cout << "Scalars[" << i << "] = " << name;
+            cout << endl;
+        }
+
+        n = reader->GetNumberOfVectorsInFile();
+        //cout << "Number of vectors = " << n << endl;
+        for (i = 0; i < n; i++)
+        {
+            const char *name = reader->GetVectorsNameInFile(i);
+            cout << "Vectors[" << i << "] = " << name;
+            cout << endl;
+        }
+
+        n = reader->GetNumberOfTensorsInFile();
+        //cout << "Number of tensors = " << n << endl;
+        for (i = 0; i < n; i++)
+        {
+            const char *name = reader->GetTensorsNameInFile(i);
+            cout << "Tensors[" << i << "] = " << name;
+            cout << endl;
+        }
+        // */
+    }
+
+
+
     // ------------------------------------------------------
-    cout << "<< Sleeping..." << endl;
-    sleep(100);
+    // Sleep for a bit so we can examine memory usage with htop
+    //cout << "<< Sleeping..." << endl;
+    //sleep(100);
 
+    delete opt;
     return 0;
 }



More information about the cig-commits mailing list