File: vtkAMRVelodyneReader.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (356 lines) | stat: -rw-r--r-- 9,908 bytes parent folder | download
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
#include "vtkAMRVelodyneReader.h"
#include "vtkOverlappingAMR.h"

#include "vtkAMRBox.h"
#include "vtkAlgorithm.h"
#include "vtkByteSwap.h"
#include "vtkDataArraySelection.h"
#include "vtkObjectFactory.h"
#include "vtkUniformGrid.h"

#include "vtkCellData.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkLongArray.h"
#include "vtkLongLongArray.h"
#include "vtkShortArray.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkTimerLog.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedShortArray.h"

#include <cassert>
#include <cfloat>
#include <cmath>
#include <map>
#include <vector>
#define H5_USE_16_API
#include "vtk_hdf5.h"

#include "vtkAMRVelodyneReaderInternal.h"
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkAMRVelodyneReader);

//-------------------------------------------------------------------
vtkAMRVelodyneReader::vtkAMRVelodyneReader()
{
  this->IsReady = false;
  this->Internal = new vtkAMRVelodyneReaderInternal;
  this->currentIndex = 0;
  this->Initialize();
}

vtkAMRVelodyneReader::~vtkAMRVelodyneReader()
{
  delete this->Internal;
  Internal = nullptr;
  this->Metadata = nullptr;
  for (unsigned int i = 0; i < this->amrVector.size(); i++)
  {
    this->amrVector[i]->Delete();
    this->amrVector[i] = nullptr;
  }
  this->amrVector.clear();
}

void vtkAMRVelodyneReader::PrintSelf(std::ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

void vtkAMRVelodyneReader::SetFileName(const char* fileName)
{
  assert("pre: Internal Velodyne Reader is nullptr" && (this->Internal != nullptr));
  if (fileName && *fileName &&
    ((this->FileName == nullptr) || strcmp(fileName, this->FileName) != 0))
  {
    if (this->FileName)
    {
      delete[] this->FileName;
      this->FileName = nullptr;
    }

    this->FileName = new char[strlen(fileName) + 1];
    strcpy(this->FileName, fileName);
    this->FileName[strlen(fileName)] = '\0';

    this->IsReady = true;
    this->Internal->SetFileName(this->FileName);
    this->LoadedMetaData = false;

    this->SetUpDataArraySelections();
  }
  this->Modified();
}

void vtkAMRVelodyneReader::UpdateFileName(int index)
{
  const char* fN = this->fileList[index].c_str();
  if (this->FileName)
  {
    delete[] this->FileName;
    this->FileName = nullptr;
  }

  this->FileName = new char[strlen(fN) + 1];
  strcpy(this->FileName, fN);
  this->FileName[strlen(fN)] = '\0';
  this->Internal->SetFileName(this->FileName);
  this->Metadata = this->amrVector[index];
  this->currentIndex = static_cast<unsigned int>(index);
}

int vtkAMRVelodyneReader::RequestInformation(
  vtkInformation* rqst, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
  this->Superclass::RequestInformation(rqst, inputVector, outputVector);
  this->FillMetaData();
  vtkInformation* info = outputVector->GetInformationObject(0);
  info->Remove(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
  info->Remove(vtkStreamingDemandDrivenPipeline::TIME_RANGE());
  assert("pre: output information object is nullptr" && (info != nullptr));
  info->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), this->timeList.data(),
    static_cast<int>(this->timeList.size()));
  double timeRange[2];
  timeRange[0] = this->timeList.front();
  timeRange[1] = this->timeList.back();
  info->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(), timeRange, 2);

  info->Set(CAN_HANDLE_PIECE_REQUEST(), 1);
  return 1;
}

int vtkAMRVelodyneReader::RequestData(vtkInformation* vtkNotUsed(request),
  vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
{
  vtkInformation* info = outputVector->GetInformationObject(0);
  double requestedTime = info->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());

  int length = info->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
  int closestStep = 0;
  double minDist = -1;
  for (int cnt = 0; cnt < length; cnt++)
  {
    double tdist = (this->timeList[cnt] - requestedTime > requestedTime - this->timeList[cnt])
      ? this->timeList[cnt] - requestedTime
      : requestedTime - this->timeList[cnt];
    if (minDist < 0 || tdist < minDist)
    {
      minDist = tdist;
      closestStep = cnt;
    }
  }
  this->UpdateFileName(closestStep);
  this->ReadMetaData();
  if (!this->Metadata->HasChildrenInformation())
  {
    vtkTimerLog::MarkStartEvent("vtkAMRVelodyneReader::GenerateParentChildInformation");
    this->Metadata->GenerateParentChildInformation();
    vtkTimerLog::MarkEndEvent("vtkAMRVelodyneReader::GenerateParentChildInformation");
  }

  vtkInformation* dummy;
  dummy = nullptr;
  vtkInformationVector** dummyV;
  dummyV = nullptr;
  this->Modified();
  return this->Superclass::RequestData(dummy, dummyV, outputVector);
}

void vtkAMRVelodyneReader::ReadMetaData()
{
  assert("pre: Internal Velodyne Reader is nullptr" && (this->Internal != nullptr));
  this->Internal->ReadMetaData();
}

int vtkAMRVelodyneReader::GetNumberOfBlocks()
{
  assert("pre: Internal Velodyne Reader is nullptr" && (this->Internal != nullptr));
  if (!this->IsReady)
  {
    return 0;
  }
  this->Internal->ReadMetaData();
  return (this->Internal->nBlocks);
}

int vtkAMRVelodyneReader::GetNumberOfLevels()
{
  assert("pre: Internal Velodyne Reader is nullptr" && (this->Internal != nullptr));
  if (!this->IsReady)
  {
    return 0;
  }
  this->Internal->ReadMetaData();
  return (this->Internal->nLevels);
}

int vtkAMRVelodyneReader::GetBlockLevel(const int blockIdx)
{
  assert("pre: Internal Velodyne Reader is nullptr" && (this->Internal != nullptr));
  if (!this->IsReady)
  {
    return -1;
  }
  this->Internal->ReadMetaData();
  return (this->Internal->Blocks[blockIdx].Level);
}

int vtkAMRVelodyneReader::FillMetaData()
{
  assert("pre: Internal Velodyne Reader is nullptr" && (this->Internal != nullptr));
  // assert( "pre: metadata object is nullptr" && (this->Metadata != nullptr) );
  if (this->IsFileRead(this->FileName))
  {
    return (1);
  }

  this->ReadMetaData();
  vtkOverlappingAMR* cAMR = vtkOverlappingAMR::New();
  cAMR->Initialize(this->Internal->nLevels, this->Internal->blocksPerLevel.data());
  cAMR->SetGridDescription(VTK_XYZ_GRID);
  cAMR->SetOrigin(this->Internal->globalOrigin.data());
  int dims[3];
  double spacing[3];
  for (int i = 0; i < this->Internal->nBlocks; i++)
  {
    vtkAMRVelodyneReaderInternal::Block& theBlock = this->Internal->Blocks[i];
    int level = theBlock.Level;
    int id = theBlock.Index;
    CalculateBlockDims(this->Internal->blockDims.data(), theBlock.isFull, dims);
    CalculateSpacing(this->Internal->rootDX.data(), level, spacing);
    vtkAMRBox box(
      theBlock.Origin, dims, spacing, this->Internal->globalOrigin.data(), VTK_XYZ_GRID);
    cAMR->SetSpacing(level, spacing);
    cAMR->SetAMRBox(level, id, box);
    cAMR->SetAMRBlockSourceIndex(level, id, i);
  }
  cAMR->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(), this->Internal->dataTime);
  this->amrVector.push_back(cAMR);
  this->timeList.push_back(this->Internal->dataTime);
  this->MarkFileAsRead(this->FileName);
  return 1;
}

vtkUniformGrid* vtkAMRVelodyneReader::GetAMRGrid(const int blockIdx)
{
  if (!this->IsReady)
  {
    return nullptr;
  }
  vtkAMRVelodyneReaderInternal::Block& theBlock = this->Internal->Blocks[blockIdx];
  int dims[3];
  CalculateBlockDims(this->Internal->blockDims.data(), theBlock.isFull, dims);
  vtkUniformGrid* ug = vtkUniformGrid::New();
  ug->SetDimensions(dims);
  ug->SetOrigin(theBlock.Origin);
  int level = theBlock.Level;
  double spacing[3];
  CalculateSpacing(this->Internal->rootDX.data(), level, spacing);
  ug->SetSpacing(spacing);
  return ug;
}

void vtkAMRVelodyneReader::GetAMRGridData(
  const int blockIdx, vtkUniformGrid* block, const char* field)
{
  assert("pre: Internal Velodyne Reader is nullptr" && (this->Internal != nullptr));
  this->Internal->ReadMetaData();
  this->Internal->GetBlockAttribute(field, blockIdx, block);
}

void vtkAMRVelodyneReader::SetUpDataArraySelections()
{
  if (this->IsFileRead(this->FileName))
  {
    return;
  }
  this->Internal->ReadMetaData();
  int numAttr = static_cast<int>(this->Internal->AttributeNames.size());
  for (int i = 0; i < numAttr; i++)
  {
    this->CellDataArraySelection->AddArray(this->Internal->AttributeNames[i].c_str());
  }
}

void vtkAMRVelodyneReader::CalculateSpacing(double* dx, int lvl, double* spacing)
{
  for (int i = 0; i < 3; i++)
  {
    spacing[i] = dx[i] / (static_cast<double>(std::pow(2, lvl)));
  }
}

void vtkAMRVelodyneReader::CalculateBlockDims(int* bDims, bool isFull, int* curDims)
{
  if (isFull)
  {
    for (int i = 0; i < 3; i++)
    {
      curDims[i] = 2 * bDims[i] + 1;
    }
  }
  else
  {
    for (int i = 0; i < 3; i++)
    {
      curDims[i] = bDims[i] + 1;
    }
  }
}

void vtkAMRVelodyneReader::MarkFileAsRead(char* fN)
{
  std::string tmp(fN);
  bool res = this->LoadedHash.insert(std::make_pair(tmp, true)).second;
  if (res)
  {
    this->fileList.push_back(tmp);
  }
}

bool vtkAMRVelodyneReader::IsFileRead(char* fN)
{
  if (this->LoadedHash.empty())
  {
    return false;
  }
  std::string tmp(fN);
  auto it = this->LoadedHash.find(tmp);
  if (it == this->LoadedHash.end())
  {
    return false;
  }

  return it->second;
}

bool vtkAMRVelodyneReader::IsFileRead(const char* fN)
{
  if (this->LoadedHash.empty())
  {
    return false;
  }
  std::string tmp(fN);
  auto it = this->LoadedHash.find(tmp);
  if (it == this->LoadedHash.end())
  {
    return false;
  }
  return it->second;
}

vtkOverlappingAMR* vtkAMRVelodyneReader::GetOutput()
{
  this->FillMetaData();
  vtkOverlappingAMR* amr = this->amrVector[this->currentIndex];
  amr->GenerateParentChildInformation();
  return amr;
}
VTK_ABI_NAMESPACE_END