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> |
---|
13 | #include <vtkDataSetReader.h> |
---|
14 | #include <vtkCharArray.h> |
---|
15 | |
---|
16 | #include <vrmath/Vector3f.h> |
---|
17 | |
---|
18 | #include "ReaderCommon.h" |
---|
19 | #include "DataSetResample.h" |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | #include "nanovis.h" |
---|
23 | #include "VtkDataSetReader.h" |
---|
24 | #include "Volume.h" |
---|
25 | #include "Trace.h" |
---|
26 | |
---|
27 | using namespace nv; |
---|
28 | |
---|
29 | Volume * |
---|
30 | nv::load_vtk_volume_stream(const char *tag, const char *bytes, int nBytes) |
---|
31 | { |
---|
32 | TRACE("Enter tag:%s", tag); |
---|
33 | |
---|
34 | vtkSmartPointer<vtkImageData> resampledDataSet; |
---|
35 | { |
---|
36 | vtkSmartPointer<vtkDataSet> dataSet; |
---|
37 | { |
---|
38 | vtkSmartPointer<vtkDataSetReader> reader = vtkSmartPointer<vtkDataSetReader>::New(); |
---|
39 | vtkSmartPointer<vtkCharArray> dataSetString = vtkSmartPointer<vtkCharArray>::New(); |
---|
40 | |
---|
41 | dataSetString->SetArray(const_cast<char *>(bytes), nBytes, 1); |
---|
42 | reader->SetInputArray(dataSetString); |
---|
43 | reader->ReadFromInputStringOn(); |
---|
44 | //reader->ReadAllNormalsOn(); |
---|
45 | //reader->ReadAllTCoordsOn(); |
---|
46 | reader->ReadAllScalarsOn(); |
---|
47 | //reader->ReadAllColorScalarsOn(); |
---|
48 | reader->ReadAllVectorsOn(); |
---|
49 | //reader->ReadAllTensorsOn(); |
---|
50 | //reader->ReadAllFieldsOn(); |
---|
51 | reader->SetLookupTableName(""); |
---|
52 | reader->Update(); |
---|
53 | |
---|
54 | dataSet = reader->GetOutput(); |
---|
55 | } |
---|
56 | |
---|
57 | if (dataSet == NULL) |
---|
58 | return NULL; |
---|
59 | |
---|
60 | int maxDim = 64; |
---|
61 | |
---|
62 | resampledDataSet = vtkImageData::SafeDownCast(dataSet.GetPointer()); |
---|
63 | if (resampledDataSet != NULL) { |
---|
64 | // Have a uniform grid, check if we need to resample |
---|
65 | #ifdef DOWNSAMPLE_DATA |
---|
66 | if (resampledDataSet->GetDimensions()[0] > maxDim || |
---|
67 | resampledDataSet->GetDimensions()[1] > maxDim || |
---|
68 | resampledDataSet->GetDimensions()[2] > maxDim) { |
---|
69 | resampledDataSet = resampleVTKDataSet(dataSet, maxDim); |
---|
70 | } |
---|
71 | #endif |
---|
72 | } else { |
---|
73 | resampledDataSet = resampleVTKDataSet(dataSet, maxDim); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | int nx, ny, nz, npts; |
---|
78 | // origin |
---|
79 | double x0, y0, z0; |
---|
80 | // deltas (cell dimensions) |
---|
81 | double dx, dy, dz; |
---|
82 | // length of volume edges |
---|
83 | double lx, ly, lz; |
---|
84 | |
---|
85 | nx = resampledDataSet->GetDimensions()[0]; |
---|
86 | ny = resampledDataSet->GetDimensions()[1]; |
---|
87 | nz = resampledDataSet->GetDimensions()[2]; |
---|
88 | npts = nx * ny * nz; |
---|
89 | resampledDataSet->GetSpacing(dx, dy, dz); |
---|
90 | resampledDataSet->GetOrigin(x0, y0, z0); |
---|
91 | |
---|
92 | lx = (nx - 1) * dx; |
---|
93 | ly = (ny - 1) * dy; |
---|
94 | lz = (nz - 1) * dz; |
---|
95 | |
---|
96 | Volume *volume = NULL; |
---|
97 | double vmin = DBL_MAX; |
---|
98 | double nzero_min = DBL_MAX; |
---|
99 | double vmax = -DBL_MAX; |
---|
100 | |
---|
101 | float *data = new float[npts * 4]; |
---|
102 | memset(data, 0, npts * 4); |
---|
103 | |
---|
104 | int ix = 0; |
---|
105 | int iy = 0; |
---|
106 | int iz = 0; |
---|
107 | for (int p = 0; p < npts; p++) { |
---|
108 | int nindex = p * 4; |
---|
109 | double val = resampledDataSet->GetScalarComponentAsDouble(ix, iy, iz, 0); |
---|
110 | data[nindex] = (float)val; |
---|
111 | if (val < vmin) { |
---|
112 | vmin = val; |
---|
113 | } else if (val > vmax) { |
---|
114 | vmax = val; |
---|
115 | } |
---|
116 | if (val != 0.0 && val < nzero_min) { |
---|
117 | nzero_min = val; |
---|
118 | } |
---|
119 | |
---|
120 | if (++ix >= nx) { |
---|
121 | ix = 0; |
---|
122 | if (++iy >= ny) { |
---|
123 | iy = 0; |
---|
124 | ++iz; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | // scale all values [0-1], -1 => out of bounds |
---|
130 | normalizeScalar(data, npts, 4, vmin, vmax); |
---|
131 | computeSimpleGradient(data, nx, ny, nz, |
---|
132 | dx, dy, dz); |
---|
133 | |
---|
134 | TRACE("nx = %i ny = %i nz = %i", nx, ny, nz); |
---|
135 | TRACE("x0 = %lg y0 = %lg z0 = %lg", x0, y0, z0); |
---|
136 | TRACE("lx = %lg ly = %lg lz = %lg", lx, ly, lz); |
---|
137 | TRACE("dx = %lg dy = %lg dz = %lg", dx, dy, dz); |
---|
138 | TRACE("dataMin = %lg dataMax = %lg nzero_min = %lg", |
---|
139 | vmin, vmax, nzero_min); |
---|
140 | |
---|
141 | volume = NanoVis::loadVolume(tag, nx, ny, nz, 4, data, |
---|
142 | vmin, vmax, nzero_min); |
---|
143 | volume->xAxis.setRange(x0, x0 + lx); |
---|
144 | volume->yAxis.setRange(y0, y0 + ly); |
---|
145 | volume->zAxis.setRange(z0, z0 + lz); |
---|
146 | volume->updatePending = true; |
---|
147 | |
---|
148 | delete [] data; |
---|
149 | |
---|
150 | // |
---|
151 | // Center this new volume on the origin. |
---|
152 | // |
---|
153 | float dx0 = -0.5; |
---|
154 | float dy0 = -0.5*ly/lx; |
---|
155 | float dz0 = -0.5*lz/lx; |
---|
156 | if (volume) { |
---|
157 | volume->setPosition(vrmath::Vector3f(dx0, dy0, dz0)); |
---|
158 | TRACE("Set volume position to %g %g %g", dx0, dy0, dz0); |
---|
159 | } |
---|
160 | |
---|
161 | TRACE("Leave"); |
---|
162 | return volume; |
---|
163 | } |
---|