VTK
vtkMultiBlockPLOT3DReaderInternals.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkMultiBlockPLOT3DReaderInternals.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #ifndef vtkMultiBlockPLOT3DReaderInternals_h
16 #define vtkMultiBlockPLOT3DReaderInternals_h
17 
18 #include "vtkIOParallelModule.h" // For export macro
19 #include "vtkByteSwap.h"
21 #include "vtkSmartPointer.h"
22 #include "vtkStructuredGrid.h"
23 
24 #include <exception>
25 #include <vector>
26 
28 
29 #ifdef _WIN64
30 # define vtk_fseek _fseeki64
31 # define vtk_ftell _ftelli64
32 # define vtk_off_t __int64
33 #else
34 # define vtk_fseek fseek
35 # define vtk_ftell ftell
36 # define vtk_off_t long
37 #endif
38 
40 {
41  struct Dims
42  {
43 
44  Dims()
45  {
46  memset(this->Values, 0, 3*sizeof(int));
47  }
48 
49  int Values[3];
50  };
51 
52  std::vector<Dims> Dimensions;
53  std::vector<vtkSmartPointer<vtkStructuredGrid> > Blocks;
54 
56  {
58  int ByteOrder;
60  int MultiGrid;
62  int Precision; // in bytes
63  int IBlanking;
65  BinaryFile(1),
66  ByteOrder(vtkMultiBlockPLOT3DReader::FILE_BIG_ENDIAN),
67  HasByteCount(1),
68  MultiGrid(0),
69  NumberOfDimensions(3),
70  Precision(4),
71  IBlanking(0)
72  {
73  }
74  };
75 
78 
80  NeedToCheckXYZFile(true)
81  {
82  }
83 
84  int ReadInts(FILE* fp, int n, int* val);
85  void CheckBinaryFile(FILE *fp, size_t fileSize);
86  int CheckByteOrder(FILE* fp);
87  int CheckByteCount(FILE* fp);
88  int CheckMultiGrid(FILE* fp);
89  int Check2DGeom(FILE* fp);
90  int CheckBlankingAndPrecision(FILE* fp);
91  int CheckCFile(FILE* fp, size_t fileSize);
92  size_t CalculateFileSize(int mgrid,
93  int precision, // in bytes
94  int blanking,
95  int ndims,
96  int hasByteCount,
97  int nGrids,
98  int* gridDims);
99  size_t CalculateFileSizeForBlock(int precision, // in bytes
100  int blanking,
101  int ndims,
102  int hasByteCount,
103  int* gridDims);
104 
105  static void CalculateSkips(const int extent[6], const int wextent[6],
106  vtkIdType& preskip, vtkIdType& postskip)
107  {
108  vtkIdType nPtsInPlane = static_cast<vtkIdType>(wextent[1]+1)*(wextent[3]+1);
109  preskip = nPtsInPlane * extent[4];
110  postskip = nPtsInPlane * (wextent[5] - extent[5]);
111  }
112 };
113 
114 namespace
115 {
116 class Plot3DException : public std::exception
117 {
118 };
119 }
120 
121 // Description:
122 // vtkMultiBlockPLOT3DReaderRecord represents a data record in the file. For
123 // binary Plot3D files with record separators (i.e. leading and trailing length
124 // field per record see: https://software.intel.com/en-us/node/525311), if the
125 // record length is greater than 2,147,483,639 bytes, the record get split into
126 // multiple records. This class allows use to manage that.
127 // It corresponds to a complete record i.e. including all the records when split
128 // among multiple records due to length limit.
129 class VTKIOPARALLEL_EXPORT vtkMultiBlockPLOT3DReaderRecord
130 {
131  struct vtkSubRecord
132  {
133  vtkTypeUInt64 HeaderOffset;
134  vtkTypeUInt64 FooterOffset;
135  };
136 
137  typedef std::vector<vtkSubRecord> VectorOfSubRecords;
138  VectorOfSubRecords SubRecords;
139 
140 public:
141  // Description:
142  // A type for collection of sub-record separators i.e. separators encountered
143  // within a record when the record length is greater than 2,147,483,639 bytes.
144  typedef std::vector<vtkTypeUInt64> SubRecordSeparators;
145 
146  // Description:
147  // Since a sub-record separator is made up of the trailing length field of a
148  // sub-record and the leading length field of the next sub-record, it's length
149  // is two ints.
150  static const int SubRecordSeparatorWidth = sizeof(int) * 2;
151 
152  // Description:
153  // Initialize metadata about the record located at the given offset.
154  // This reads the file on the root node to populate record information,
155  // seeking and marching forward through the file if the record comprises of
156  // multiple sub-records. The file is reset back to the original starting
157  // position when done.
158  //
159  // This method has no effect for non-binary files or files that don't have
160  // record separators i.e. HasByteCount == 0.
161  bool Initialize(FILE* fp, vtkTypeUInt64 offset,
163  vtkMultiProcessController* controller);
164 
165  // Description:
166  // Returns true if:
167  // 1. file doesn't comprise of records i.e. ASCII or doesn't have byte-count markers.
168  // 2. offset is same as the start offset for this record.
169  bool AtStart(vtkTypeUInt64 offset)
170  {
171  return (this->SubRecords.size()==0 || this->SubRecords.front().HeaderOffset == offset);
172  }
173 
174  // Description:
175  // Returns true if:
176  // 1. file doesn't comprise of records i.e. ASCII or doesn't have byte-count markers.
177  // 2. offset is at the end of this record i.e. the start of the next record.
178  bool AtEnd(vtkTypeUInt64 offset)
179  {
180  return (this->SubRecords.size()==0 ||
181  (this->SubRecords.back().FooterOffset + sizeof(int) == offset));
182  }
183 
184  // Description:
185  // Returns the location of SubRecordSeparators (bad two 4-byte ints) between startOffset and
186  // (startOffset + length).
187  SubRecordSeparators GetSubRecordSeparators(vtkTypeUInt64 startOffset, vtkTypeUInt64 length) const;
188 
189  // Description:
190  // When reading between file offsets \c start and \c (start + length) from the file, if it has any
191  // sub-record separators, this method splits the read into chunks so that it
192  // skips the sub-record separators. The returned value is a vector of pairs
193  // (offset, length-in-bytes).
194  static std::vector<std::pair<vtkTypeUInt64, vtkTypeUInt64> > GetChunksToRead(
195  vtkTypeUInt64 start, vtkTypeUInt64 length, const std::vector<vtkTypeUInt64> &markers);
196 
197  // Description:
198  // If the block in file (start, start+length) steps over sub-record separators
199  // within this record, then this method will return a new length that includes
200  // the bytes for the separators to be skipped. Otherwise, simply returns the
201  // length.
202  vtkTypeUInt64 GetLengthWithSeparators(vtkTypeUInt64 start, vtkTypeUInt64 length) const;
203 
204  std::vector<std::pair<vtkTypeUInt64, vtkTypeUInt64> > GetChunksToRead(
205  vtkTypeUInt64 start, vtkTypeUInt64 length) const
206  {
207  return this->GetChunksToRead(start, length, this->GetSubRecordSeparators(start, length));
208  }
209 
210 };
211 
212 #endif
213 // VTK-HeaderTest-Exclude: vtkMultiBlockPLOT3DReaderInternals.h
int ReadInts(FILE *fp, int n, int *val)
int vtkIdType
Definition: vtkType.h:287
static void CalculateSkips(const int extent[6], const int wextent[6], vtkIdType &preskip, vtkIdType &postskip)
size_t CalculateFileSize(int mgrid, int precision, int blanking, int ndims, int hasByteCount, int nGrids, int *gridDims)
size_t CalculateFileSizeForBlock(int precision, int blanking, int ndims, int hasByteCount, int *gridDims)
std::vector< vtkSmartPointer< vtkStructuredGrid > > Blocks
void CheckBinaryFile(FILE *fp, size_t fileSize)
int CheckCFile(FILE *fp, size_t fileSize)
std::vector< std::pair< vtkTypeUInt64, vtkTypeUInt64 > > GetChunksToRead(vtkTypeUInt64 start, vtkTypeUInt64 length) const
Multiprocessing communication superclass.