forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkSegYReaderInternal.cxx
375 lines (331 loc) · 11.9 KB
/
vtkSegYReaderInternal.cxx
1
2
3
4
5
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSegYReaderInternal.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSegYReaderInternal.h"
#include "vtkArrayData.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkSegYBinaryHeaderBytesPositions.h"
#include "vtkSegYIOUtils.h"
#include "vtkSegYTraceReader.h"
#include "vtkStructuredGrid.h"
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
namespace {
const int FIRST_TRACE_START_POS = 3600; // this->Traces start after 3200 + 400 file header
double decodeMultiplier(short multiplier)
{
return
(multiplier < 0) ?
(-1.0 / multiplier)
: (multiplier > 0 ? multiplier : 1.0);
}
};
//-----------------------------------------------------------------------------
vtkSegYReaderInternal::vtkSegYReaderInternal() :
SampleInterval(0), FormatCode(0), SampleCountPerTrace(0)
{
this->BinaryHeaderBytesPos = new vtkSegYBinaryHeaderBytesPositions();
this->VerticalCRS = 0;
this->TraceReader = new vtkSegYTraceReader();
}
//-----------------------------------------------------------------------------
vtkSegYReaderInternal::~vtkSegYReaderInternal()
{
delete this->BinaryHeaderBytesPos;
delete this->TraceReader;
for (auto trace : this->Traces)
{
delete trace;
}
}
//-----------------------------------------------------------------------------
void vtkSegYReaderInternal::SetXYCoordBytePositions(int x, int y)
{
this->TraceReader->SetXYCoordBytePositions(x, y);
}
//-----------------------------------------------------------------------------
void vtkSegYReaderInternal::SetVerticalCRS(int v)
{
this->VerticalCRS = v > 0 ? 1 : 0;
}
//-----------------------------------------------------------------------------
void vtkSegYReaderInternal::LoadTraces(int *extent)
{
std::streamoff traceStartPos = FIRST_TRACE_START_POS;
std::streamoff fileSize = vtkSegYIOUtils::Instance()->getFileSize(this->In);
// allocate traces vector
int dims[3] =
{
extent[1] - extent[0] + 1,
extent[3] - extent[2] + 1,
extent[5] - extent[4] + 1
};
bool is3d = (extent[3] - extent[2] > 1) ? true : false;
this->Traces.resize(dims[0]*dims[1],nullptr);
size_t traceCount = 0;
while (traceStartPos + 240 < fileSize)
{
vtkSegYTrace* pTrace = new vtkSegYTrace();
this->TraceReader->ReadTrace(traceStartPos, this->In, this->FormatCode, pTrace);
size_t loc = traceCount;
if (is3d)
{
loc = pTrace->CrosslineNumber - extent[0] + (pTrace->InlineNumber - extent[2])*dims[0];
}
this->Traces[loc] = pTrace;
traceCount++;
}
}
//-----------------------------------------------------------------------------
bool vtkSegYReaderInternal::ReadHeader()
{
this->SampleInterval = vtkSegYIOUtils::Instance()->readShortInteger(
this->BinaryHeaderBytesPos->SampleInterval, this->In);
this->FormatCode = vtkSegYIOUtils::Instance()->readShortInteger(
this->BinaryHeaderBytesPos->FormatCode, this->In);
this->SampleCountPerTrace = vtkSegYIOUtils::Instance()->readShortInteger(
this->BinaryHeaderBytesPos->NumSamplesPerTrace, this->In);
return true;
}
//-----------------------------------------------------------------------------
bool vtkSegYReaderInternal::Is3DComputeParameters(
int* extent, double origin[3], double spacing[3][3], int* spacingSign)
{
this->ReadHeader();
std::streamoff traceStartPos = FIRST_TRACE_START_POS;
std::streamoff fileSize = vtkSegYIOUtils::Instance()->getFileSize(this->In);
int inlineNumber = 0, crosslineNumber;
int xCoord = 0, yCoord = 0;
short coordMultiplier = 0;
// compute the dimensions of the dataset, to be safe we
// look at all the traces and compute the set of inline
// and crossline indicies
std::set<int> crossLines;
std::map<int, std::array<double,3> > crossCoordinates;
std::set<int> inLines;
int basisPointCount = 0;
double basisCoords[3][3];
int basisIndex[3][2] = { { 0, 0}, {0, 0}, {0, 0} };
double iBasis[2][3];
double basisLength[2];
size_t traceCount = 0;
while(traceStartPos + 240 < fileSize)
{
this->TraceReader->ReadInlineCrossline
(traceStartPos, this->In, this->FormatCode,
&inlineNumber, &crosslineNumber,
&xCoord, &yCoord, &coordMultiplier);
traceCount++;
double coordinateMultiplier = decodeMultiplier(coordMultiplier);
// store a third point, must have different basis from
// first two
if (basisPointCount == 2)
{
iBasis[1][0] = crosslineNumber - basisIndex[0][0];
iBasis[1][1] = inlineNumber - basisIndex[0][1];
iBasis[1][2] = 0.0;
basisLength[1] = vtkMath::Normalize(iBasis[1]);
if (fabs(vtkMath::Dot(iBasis[0], iBasis[1])) < 0.99)
{
basisCoords[basisPointCount][0] = coordinateMultiplier * xCoord;
basisCoords[basisPointCount][1] = coordinateMultiplier * yCoord;
basisCoords[basisPointCount][2] = 0;
basisIndex[basisPointCount][0] = crosslineNumber;
basisIndex[basisPointCount][1] = inlineNumber;
basisPointCount++;
}
}
// store a second point, just any point other than first
if (basisPointCount == 1)
{
basisCoords[basisPointCount][0] = coordinateMultiplier * xCoord;
basisCoords[basisPointCount][1] = coordinateMultiplier * yCoord;
basisCoords[basisPointCount][2] = 0;
basisIndex[basisPointCount][0] = crosslineNumber;
basisIndex[basisPointCount][1] = inlineNumber;
iBasis[0][0] = basisIndex[1][0] - basisIndex[0][0];
iBasis[0][1] = basisIndex[1][1] - basisIndex[0][1];
iBasis[0][2] = 0.0;
basisLength[0] = vtkMath::Normalize(iBasis[0]);
basisPointCount++;
}
// store a first point
if (basisPointCount == 0)
{
basisCoords[basisPointCount][0] = coordinateMultiplier * xCoord;
basisCoords[basisPointCount][1] = coordinateMultiplier * yCoord;
basisCoords[basisPointCount][2] = 0;
basisIndex[basisPointCount][0] = crosslineNumber;
basisIndex[basisPointCount][1] = inlineNumber;
basisPointCount++;
}
inLines.insert(inlineNumber);
crossLines.insert(crosslineNumber);
}
// find the min and max to get the extent
int startCross = *crossLines.begin();
int endCross = *crossLines.rbegin();
int crosslineCount = endCross - startCross + 1;
int startInline = *inLines.begin();
int endInline = *inLines.rbegin();
int inlineCount = endInline - startInline + 1;
auto e = {
startCross, endCross,
startInline, endInline,
0, this->SampleCountPerTrace - 1,
};
std::copy(e.begin(), e.end(), extent);
if (inlineCount <= 1) // should really be 1 in either inline or crossline?
{
// this is a 2D dataset
// watch for cases where there are more traces than crosslines as
if (static_cast<int>(traceCount) > crosslineCount)
{
extent[0] = 0;
extent[1] = static_cast<int>(traceCount) - 1;
}
return false;
}
// compute the mapping of indicies into coords if we have three
if (basisPointCount == 3)
{
// compute an orthogonal basis
double bDot = vtkMath::Dot(iBasis[0], iBasis[1]);
iBasis[1][0] -= bDot*iBasis[0][0];
iBasis[1][1] -= bDot*iBasis[0][1];
vtkMath::Normalize(iBasis[1]);
// coordinate vectors
double cBasis[2][3];
cBasis[0][0] = basisCoords[1][0] - basisCoords[0][0];
cBasis[0][1] = basisCoords[1][1] - basisCoords[0][1];
cBasis[0][2] = 0.0;
cBasis[1][0] = basisCoords[2][0] - basisCoords[0][0] - bDot*cBasis[0][0];
cBasis[1][1] = basisCoords[2][1] - basisCoords[0][1] - bDot*cBasis[0][1];
cBasis[1][2] = 0.0;
// spacing = (unitIndexDir . unitIndexBasis)*coordBasis/indexBasisLength;
spacing[0][0] = iBasis[0][0]*cBasis[0][0]/basisLength[0]
+ iBasis[1][0]*cBasis[1][0]/basisLength[1];
spacing[0][1] = iBasis[0][0]*cBasis[0][1]/basisLength[0]
+ iBasis[1][0]*cBasis[1][1]/basisLength[1];
spacing[0][2] = 0.0;
spacing[1][0] = iBasis[0][1]*cBasis[0][0]/basisLength[0]
+ iBasis[1][1]*cBasis[1][0]/basisLength[1];
spacing[1][1] = iBasis[0][1]*cBasis[0][1]/basisLength[0]
+ iBasis[1][1]*cBasis[1][1]/basisLength[1];
spacing[1][2] = 0.0;
// The samples are uniformly placed at sample interval depths
// Dividing by 1000.0 to convert from microseconds to milliseconds.
spacing[2][0] = 0.0;
spacing[2][1] = 0.0;
spacing[2][2] = this->SampleInterval / 1000.0;
spacingSign[0] = spacing[0][0] >= 0.0 ? 1.0 : -1.0;
spacingSign[1] = spacing[1][1] >= 0.0 ? 1.0 : -1.0;
spacingSign[2] = (this->VerticalCRS == 0 ? -1 : 1); // goes
origin[0] = (startCross - basisIndex[0][0]) * spacing[0][0]
+ (startInline - basisIndex[0][1]) * spacing[1][0]
+ basisCoords[0][0];
origin[1] = (startCross - basisIndex[0][0]) * spacing[0][1]
+ (startInline - basisIndex[0][1]) * spacing[1][1]
+ basisCoords[0][1];
origin[2] = - spacing[2][2] * (this->SampleCountPerTrace - 1);
}
return true;
}
//-----------------------------------------------------------------------------
void vtkSegYReaderInternal::ExportData(
vtkImageData* imageData,
int* extent, double origin[3], double spacing[3][3], int* spacingSign)
{
imageData->SetExtent(extent);
imageData->SetOrigin(origin);
imageData->SetSpacing(
vtkMath::Norm(spacing[0]),
vtkMath::Norm(spacing[1]),
vtkMath::Norm(spacing[2])
);
int* dims = imageData->GetDimensions();
vtkNew<vtkFloatArray> scalars;
scalars->SetNumberOfComponents(1);
scalars->SetNumberOfTuples(dims[0] * dims[1] * dims[2]);
scalars->SetName("trace");
imageData->GetPointData()->SetScalars(scalars);
int id = 0;
int destK, destJ, destI;
for (int k = 0; k < dims[2]; ++k)
{
destK = (spacingSign[2] > 0 ? k : dims[2] - k - 1);
for (int j = 0; j < dims[1]; ++j)
{
destJ = (spacingSign[1] > 0 ? j : dims[1] - j - 1);
for (int i = 0; i < dims[0]; ++i)
{
destI = (spacingSign[0] > 0 ? i : dims[0] - i - 1);
vtkSegYTrace* trace = this->Traces[destJ * dims[0] + destI];
scalars->SetValue(id++, trace ? trace->Data[destK] : 0.0);
}
}
}
}
//-----------------------------------------------------------------------------
void vtkSegYReaderInternal::ExportData(vtkStructuredGrid* grid,
int* extent,
double origin[3],
double spacing[3][3])
{
if (!grid)
{
return;
}
grid->SetExtent(extent);
int* dims = grid->GetDimensions();
vtkNew<vtkPoints> points;
vtkNew<vtkFloatArray> scalars;
scalars->SetName("trace");
scalars->SetNumberOfComponents(1);
scalars->Allocate(dims[0] * dims[1] * dims[2]);
int sign = this->VerticalCRS == 0 ? -1 : 1;
int id = 0;
for (int k = 0; k < dims[2]; ++k)
{
for (int j = 0; j < dims[1]; ++j)
{
for (int i = 0; i < dims[0]; ++i)
{
auto trace = this->Traces[j * dims[0] + i];
double x = origin[0] + i*spacing[0][0] + j*spacing[1][0];
double y = origin[1] + i*spacing[0][1] + j*spacing[1][1];
double z = sign * k * spacing[2][2];
if (trace)
{
double coordinateMultiplier = decodeMultiplier(trace->CoordinateMultiplier);
x = coordinateMultiplier * trace->XCoordinate;
y = coordinateMultiplier * trace->YCoordinate;
z = sign * k * (trace->SampleInterval / 1000.0);
scalars->InsertValue(id++, trace->Data[k]);
}
else
{
scalars->InsertValue(id++, 0.0);
}
points->InsertNextPoint(x, y, z);
}
}
}
grid->SetPoints(points);
grid->GetPointData()->SetScalars(scalars);
}