source: nanovis/trunk/VtkDataSetReader.cpp @ 5704

Last change on this file since 5704 was 5672, checked in by ldelgass, 9 years ago

Add debug output to VTK reader

File size: 9.5 KB
RevLine 
[3870]1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2004-2013  HUBzero Foundation, LLC
4 *
5 */
6
7#include <iostream>
8#include <float.h>
9
10#include <vtkSmartPointer.h>
11#include <vtkDataSet.h>
12#include <vtkImageData.h>
[3935]13#include <vtkPointData.h>
[5672]14#include <vtkCellData.h>
[3935]15#include <vtkDataArray.h>
[3870]16#include <vtkDataSetReader.h>
17#include <vtkCharArray.h>
18
19#include <vrmath/Vector3f.h>
20
21#include "ReaderCommon.h"
22#include "DataSetResample.h"
23
24#include "config.h"
25#include "nanovis.h"
26#include "VtkDataSetReader.h"
27#include "Volume.h"
28#include "Trace.h"
29
30using namespace nv;
31
[5672]32static void print(vtkDataSet *ds)
33{
34    if (ds == NULL)
35        return;
36
37    TRACE("DataSet class: %s", ds->GetClassName());
38
39    TRACE("DataSet memory: %g MiB", ds->GetActualMemorySize()/1024.);
40
41    double bounds[6];
42    ds->GetBounds(bounds);
43
44    // Topology
45    TRACE("DataSet bounds: %g %g %g %g %g %g",
46          bounds[0], bounds[1],
47          bounds[2], bounds[3],
48          bounds[4], bounds[5]);
49    TRACE("Points: %d Cells: %d", ds->GetNumberOfPoints(), ds->GetNumberOfCells());
50
51    double dataRange[2];
52    if (ds->GetPointData() != NULL) {
53        TRACE("PointData arrays: %d", ds->GetPointData()->GetNumberOfArrays());
54        for (int i = 0; i < ds->GetPointData()->GetNumberOfArrays(); i++) {
55            if (ds->GetPointData()->GetArray(i) != NULL) {
56                ds->GetPointData()->GetArray(i)->GetRange(dataRange, -1);
57                TRACE("PointData[%d]: '%s' comp:%d, (%g,%g)", i,
58                      ds->GetPointData()->GetArrayName(i),
59                      ds->GetPointData()->GetAbstractArray(i)->GetNumberOfComponents(),
60                      dataRange[0], dataRange[1]);
61            } else {
62                TRACE("PointData[%d]: '%s' comp:%d", i,
63                      ds->GetPointData()->GetArrayName(i),
64                      ds->GetPointData()->GetAbstractArray(i)->GetNumberOfComponents());
65            }
66        }
67        if (ds->GetPointData()->GetScalars() != NULL) {
68            TRACE("Active point scalars: %s", ds->GetPointData()->GetScalars()->GetName());
69        }
70        if (ds->GetPointData()->GetVectors() != NULL) {
71            TRACE("Active point vectors: %s", ds->GetPointData()->GetVectors()->GetName());
72        }
73    }
74    if (ds->GetCellData() != NULL) {
75        TRACE("CellData arrays: %d", ds->GetCellData()->GetNumberOfArrays());
76        for (int i = 0; i < ds->GetCellData()->GetNumberOfArrays(); i++) {
77            if (ds->GetCellData()->GetArray(i) != NULL) {
78                ds->GetCellData()->GetArray(i)->GetRange(dataRange, -1);
79                TRACE("CellData[%d]: '%s' comp:%d, (%g,%g)", i,
80                      ds->GetCellData()->GetArrayName(i),
81                      ds->GetCellData()->GetAbstractArray(i)->GetNumberOfComponents(),
82                      dataRange[0], dataRange[1]);
83            } else {
84                TRACE("CellData[%d]: '%s' comp:%d", i,
85                      ds->GetCellData()->GetArrayName(i),
86                      ds->GetCellData()->GetAbstractArray(i)->GetNumberOfComponents());
87            }
88        }
89        if (ds->GetCellData()->GetScalars() != NULL) {
90            TRACE("Active cell scalars: %s", ds->GetCellData()->GetScalars()->GetName());
91        }
92        if (ds->GetCellData()->GetVectors() != NULL) {
93            TRACE("Active cell vectors: %s", ds->GetCellData()->GetVectors()->GetName());
94        }
95    }
96    if (ds->GetFieldData() != NULL) {
97        TRACE("FieldData arrays: %d", ds->GetFieldData()->GetNumberOfArrays());
98        for (int i = 0; i < ds->GetFieldData()->GetNumberOfArrays(); i++) {
99            if (ds->GetFieldData()->GetArray(i) != NULL) {
100                ds->GetFieldData()->GetArray(i)->GetRange(dataRange, -1);
101                TRACE("FieldData[%d]: '%s' comp:%d, tuples:%d (%g,%g)", i,
102                      ds->GetFieldData()->GetArrayName(i),
103                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfComponents(),
104                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfTuples(),
105                      dataRange[0], dataRange[1]);
106            } else {
107                TRACE("FieldData[%d]: '%s' comp:%d, tuples:%d", i,
108                      ds->GetFieldData()->GetArrayName(i),
109                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfComponents(),
110                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfTuples());
111            }
112        }
113    }
114}
115
[3870]116Volume *
[4063]117nv::load_vtk_volume_stream(const char *tag, const char *bytes, int nBytes)
[3870]118{
119    TRACE("Enter tag:%s", tag);
120
121    vtkSmartPointer<vtkImageData> resampledDataSet;
122    {
123        vtkSmartPointer<vtkDataSet> dataSet;
124        {
125            vtkSmartPointer<vtkDataSetReader> reader = vtkSmartPointer<vtkDataSetReader>::New();
126            vtkSmartPointer<vtkCharArray> dataSetString = vtkSmartPointer<vtkCharArray>::New();
127
128            dataSetString->SetArray(const_cast<char *>(bytes), nBytes, 1);
129            reader->SetInputArray(dataSetString);
130            reader->ReadFromInputStringOn();
131            //reader->ReadAllNormalsOn();
132            //reader->ReadAllTCoordsOn();
133            reader->ReadAllScalarsOn();
134            //reader->ReadAllColorScalarsOn();
135            reader->ReadAllVectorsOn();
136            //reader->ReadAllTensorsOn();
[4164]137            reader->ReadAllFieldsOn();
[3870]138            reader->SetLookupTableName("");
139            reader->Update();
140
141            dataSet = reader->GetOutput();
142        }
143
144        if (dataSet == NULL)
145            return NULL;
146
[5672]147        print(dataSet);
148
[3870]149        int maxDim = 64;
150
151        resampledDataSet = vtkImageData::SafeDownCast(dataSet.GetPointer());
152        if (resampledDataSet != NULL) {
153            // Have a uniform grid, check if we need to resample
154#ifdef DOWNSAMPLE_DATA
155            if (resampledDataSet->GetDimensions()[0] > maxDim ||
156                resampledDataSet->GetDimensions()[1] > maxDim ||
157                resampledDataSet->GetDimensions()[2] > maxDim) {
158                resampledDataSet = resampleVTKDataSet(dataSet, maxDim);
159            }
160#endif
161        } else {
162            resampledDataSet = resampleVTKDataSet(dataSet, maxDim);
163        }
164    }
165
166    int nx, ny, nz, npts;
167    // origin
168    double x0, y0, z0;
169    // deltas (cell dimensions)
170    double dx, dy, dz;
171    // length of volume edges
172    double lx, ly, lz;
173
174    nx = resampledDataSet->GetDimensions()[0];
175    ny = resampledDataSet->GetDimensions()[1];
176    nz = resampledDataSet->GetDimensions()[2];
177    npts = nx * ny * nz;
178    resampledDataSet->GetSpacing(dx, dy, dz);
179    resampledDataSet->GetOrigin(x0, y0, z0);
180
181    lx = (nx - 1) * dx;
182    ly = (ny - 1) * dy;
183    lz = (nz - 1) * dz;
184
185    Volume *volume = NULL;
186    double vmin = DBL_MAX;
187    double nzero_min = DBL_MAX;
188    double vmax = -DBL_MAX;
189
190    float *data = new float[npts * 4];
191    memset(data, 0, npts * 4);
192
[3935]193    bool isVectorData = (resampledDataSet->GetPointData()->GetVectors() != NULL);
194
[3870]195    int ix = 0;
196    int iy = 0;
197    int iz = 0;
[4385]198    vtkDataArray *mask = resampledDataSet->GetPointData()->GetArray("vtkValidPointMask");
[5672]199    int numValidPoints = 0;
[3870]200    for (int p = 0; p < npts; p++) {
201        int nindex = p * 4;
[3935]202        double val;
[4385]203        int loc[3];
204        loc[0] = ix; loc[1] = iy; loc[2] = iz;
205        vtkIdType idx = resampledDataSet->ComputePointId(loc);
[4503]206        bool valid = (mask == NULL) ? true : (mask->GetComponent(idx, 0) != 0.0);
[3935]207        if (isVectorData) {
208            double vec[3];
209            resampledDataSet->GetPointData()->GetVectors()->GetTuple(idx, vec);
[3943]210            val = sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
[3935]211            data[nindex] = (float)val;
212            data[nindex+1] = (float)vec[0];
213            data[nindex+2] = (float)vec[1];
214            data[nindex+3] = (float)vec[2];
215        } else {
[4385]216            //val = resampledDataSet->GetScalarComponentAsDouble(ix, iy, iz, 0);
217            val = resampledDataSet->GetPointData()->GetScalars()->GetComponent(idx, 0);
218            data[nindex] = valid ? (float)val : -FLT_MAX;
[3935]219        }
[4385]220        if (valid) {
[5672]221            numValidPoints++;
[4385]222            if (val < vmin) {
223                vmin = val;
224            } else if (val > vmax) {
225                vmax = val;
226            }
227            if (val != 0.0 && val < nzero_min) {
228                nzero_min = val;
229            }
[3870]230        }
231
232        if (++ix >= nx) {
233            ix = 0;
234            if (++iy >= ny) {
235                iy = 0;
236                ++iz;
237            }
238        }
239    }
240
[5672]241    if (numValidPoints < 1) {
242        ERROR("No valid points in resample volume");
243        vmin = 0.0;
244        vmax = 1.0;
245    } else {
246        TRACE("Valid points: %d of %d", numValidPoints, resampledDataSet->GetNumberOfPoints());
247    }
248
[3935]249    if (isVectorData) {
250        // Normalize magnitude [0,1] and vector components [0,1]
251        normalizeVector(data, npts, vmin, vmax);
252    } else {
[5672]253        // Normalize all values [0,1], -1 => out of bounds
[3935]254        normalizeScalar(data, npts, 4, vmin, vmax);
255        computeSimpleGradient(data, nx, ny, nz,
256                              dx, dy, dz);
257    }
[3870]258
[3943]259    TRACE("isVectorData: %s", isVectorData ? "yes" : "no");
[3870]260    TRACE("nx = %i ny = %i nz = %i", nx, ny, nz);
[5549]261    TRACE("x0 = %g y0 = %g z0 = %g", x0, y0, z0);
262    TRACE("lx = %g ly = %g lz = %g", lx, ly, lz);
263    TRACE("dx = %g dy = %g dz = %g", dx, dy, dz);
264    TRACE("dataMin = %g dataMax = %g nzero_min = %g",
[3870]265          vmin, vmax, nzero_min);
266
267    volume = NanoVis::loadVolume(tag, nx, ny, nz, 4, data,
268                                 vmin, vmax, nzero_min);
269    volume->xAxis.setRange(x0, x0 + lx);
270    volume->yAxis.setRange(y0, y0 + ly);
271    volume->zAxis.setRange(z0, z0 + lz);
272    volume->updatePending = true;
273
274    delete [] data;
275
276    TRACE("Leave");
277    return volume;
278}
Note: See TracBrowser for help on using the repository browser.