ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CompressedVectorNodeImpl.cpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright 2009 - 2010 Kevin Ackley (kackley@gwi.net)
3  * Modified work Copyright 2018 - 2020 Andy Maloney <asmaloney@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person or organization
6  * obtaining a copy of the software and accompanying documentation covered by
7  * this license (the "Software") to use, reproduce, display, distribute,
8  * execute, and transmit the Software, and to prepare derivative works of the
9  * Software, and to permit third-parties to whom the Software is furnished to
10  * do so, all subject to the following:
11  *
12  * The copyright notices in the Software and this entire statement, including
13  * the above license grant, this restriction and the following disclaimer,
14  * must be included in all copies of the Software, in whole or in part, and
15  * all derivative works of the Software, unless such copies or derivative
16  * works are solely in the form of machine-executable object code generated by
17  * a source language processor.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
29 #include "CheckedFile.h"
32 #include "ImageFileImpl.h"
33 #include "VectorNodeImpl.h"
34 
35 namespace e57
36 {
38  {
39  // don't checkImageFileOpen, NodeImpl() will do it
40  }
41 
43  {
44  // don't checkImageFileOpen, ctor did it
45 
46  //??? check ok for proto, no Blob CompressedVector, empty?
47  //??? throw E57_EXCEPTION2(E57_ERROR_BAD_PROTOTYPE)
48 
50  if ( prototype_ )
51  {
52  throw E57_EXCEPTION2( E57_ERROR_SET_TWICE, "this->pathName=" + this->pathName() );
53  }
54 
56  if ( !prototype->isRoot() )
57  {
59  "this->pathName=" + this->pathName() + " prototype->pathName=" + prototype->pathName() );
60  }
61 
64  ImageFileImplSharedPtr prototypeDest( prototype->destImageFile() );
65  if ( thisDest != prototypeDest )
66  {
67  throw E57_EXCEPTION2( E57_ERROR_DIFFERENT_DEST_IMAGEFILE, "this->destImageFile" + thisDest->fileName() +
68  " prototype->destImageFile" +
69  prototypeDest->fileName() );
70  }
71 
73  prototype_ = prototype;
74 
77  }
78 
80  {
81  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
82  return ( prototype_ ); //??? check defined
83  }
84 
85  void CompressedVectorNodeImpl::setCodecs( const std::shared_ptr<VectorNodeImpl> &codecs )
86  {
87  // don't checkImageFileOpen, ctor did it
88 
89  //??? check ok for codecs, empty vector, or each element has "inputs" vector
90  // of strings, codec
91  // substruct
92 
94  if ( codecs_ )
95  {
96  throw E57_EXCEPTION2( E57_ERROR_SET_TWICE, "this->pathName=" + this->pathName() );
97  }
98 
100  if ( !codecs->isRoot() )
101  {
103  "this->pathName=" + this->pathName() + " codecs->pathName=" + codecs->pathName() );
104  }
105 
108  ImageFileImplSharedPtr codecsDest( codecs->destImageFile() );
109  if ( thisDest != codecsDest )
110  {
111  throw E57_EXCEPTION2( E57_ERROR_DIFFERENT_DEST_IMAGEFILE, "this->destImageFile" + thisDest->fileName() +
112  " codecs->destImageFile" +
113  codecsDest->fileName() );
114  }
115 
116  codecs_ = codecs;
117 
120  }
121 
122  std::shared_ptr<VectorNodeImpl> CompressedVectorNodeImpl::getCodecs() const
123  {
124  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
125  return ( codecs_ ); //??? check defined
126  }
127 
129  {
130  // don't checkImageFileOpen
131 
132  //??? is this test a good idea?
133 
135  if ( ni->type() != E57_COMPRESSED_VECTOR )
136  {
137  return ( false );
138  }
139 
140  std::shared_ptr<CompressedVectorNodeImpl> cvi( std::static_pointer_cast<CompressedVectorNodeImpl>( ni ) );
141 
143  if ( recordCount_ != cvi->recordCount_ )
144  {
145  return ( false );
146  }
147 
149  if ( !prototype_->isTypeEquivalent( cvi->prototype_ ) )
150  {
151  return ( false );
152  }
153  if ( !codecs_->isTypeEquivalent( cvi->codecs_ ) )
154  {
155  return ( false );
156  }
157 
158  return ( true );
159  }
160 
162  {
163  throw E57_EXCEPTION2( E57_ERROR_NOT_IMPLEMENTED, "this->pathName=" + this->pathName() + " pathName=" + pathName );
164  }
165 
167  {
169  isAttached_ = true;
170 
172  if ( prototype_ )
173  {
174  prototype_->setAttachedRecursive();
175  }
176 
178  if ( codecs_ )
179  {
180  codecs_->setAttachedRecursive();
181  }
182  }
183 
185  {
186  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
187  return ( recordCount_ );
188  }
189 
191  {
192  // don't checkImageFileOpen
193 
196  throw E57_EXCEPTION2( E57_ERROR_INTERNAL, "this->pathName=" + this->pathName() );
197  }
198 
200  const char *forcedFieldName )
201  {
202  // don't checkImageFileOpen
203 
204  ustring fieldName;
205  if ( forcedFieldName != nullptr )
206  {
207  fieldName = forcedFieldName;
208  }
209  else
210  {
211  fieldName = elementName_;
212  }
213 
214  uint64_t physicalStart = cf.logicalToPhysical( binarySectionLogicalStart_ );
215 
216  cf << space( indent ) << "<" << fieldName << " type=\"CompressedVector\"";
217  cf << " fileOffset=\"" << physicalStart;
218  cf << "\" recordCount=\"" << recordCount_ << "\">\n";
219 
220  if ( prototype_ )
221  {
222  prototype_->writeXml( imf, cf, indent + 2, "prototype" );
223  }
224  if ( codecs_ )
225  {
226  codecs_->writeXml( imf, cf, indent + 2, "codecs" );
227  }
228  cf << space( indent ) << "</" << fieldName << ">\n";
229  }
230 
231 #ifdef E57_DEBUG
232  void CompressedVectorNodeImpl::dump( int indent, std::ostream &os ) const
233  {
234  os << space( indent ) << "type: CompressedVector"
235  << " (" << type() << ")" << std::endl;
236  NodeImpl::dump( indent, os );
237  if ( prototype_ )
238  {
239  os << space( indent ) << "prototype:" << std::endl;
240  prototype_->dump( indent + 2, os );
241  }
242  else
243  {
244  os << space( indent ) << "prototype: <empty>" << std::endl;
245  }
246  if ( codecs_ )
247  {
248  os << space( indent ) << "codecs:" << std::endl;
249  codecs_->dump( indent + 2, os );
250  }
251  else
252  {
253  os << space( indent ) << "codecs: <empty>" << std::endl;
254  }
255  os << space( indent ) << "recordCount: " << recordCount_ << std::endl;
256  os << space( indent ) << "binarySectionLogicalStart: " << binarySectionLogicalStart_ << std::endl;
257  }
258 #endif
259 
260  std::shared_ptr<CompressedVectorWriterImpl> CompressedVectorNodeImpl::writer( std::vector<SourceDestBuffer> sbufs )
261  {
262  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
263 
265 
267  if ( destImageFile->writerCount() > 0 )
268  {
270  "fileName=" + destImageFile->fileName() +
271  " writerCount=" + toString( destImageFile->writerCount() ) +
272  " readerCount=" + toString( destImageFile->readerCount() ) );
273  }
274  if ( destImageFile->readerCount() > 0 )
275  {
277  "fileName=" + destImageFile->fileName() +
278  " writerCount=" + toString( destImageFile->writerCount() ) +
279  " readerCount=" + toString( destImageFile->readerCount() ) );
280  }
281 
283  if ( sbufs.empty() )
284  {
285  throw E57_EXCEPTION2( E57_ERROR_BAD_API_ARGUMENT, "fileName=" + destImageFile->fileName() );
286  }
287 
288  if ( !destImageFile->isWriter() )
289  {
290  throw E57_EXCEPTION2( E57_ERROR_FILE_IS_READ_ONLY, "fileName=" + destImageFile->fileName() );
291  }
292 
293  if ( !isAttached() )
294  {
295  throw E57_EXCEPTION2( E57_ERROR_NODE_UNATTACHED, "fileName=" + destImageFile->fileName() );
296  }
297 
299  NodeImplSharedPtr ni( shared_from_this() );
300 
302  std::shared_ptr<CompressedVectorNodeImpl> cai( std::static_pointer_cast<CompressedVectorNodeImpl>( ni ) );
303 
305  std::shared_ptr<CompressedVectorWriterImpl> cvwi( new CompressedVectorWriterImpl( cai, sbufs ) );
306  return ( cvwi );
307  }
308 
309  std::shared_ptr<CompressedVectorReaderImpl> CompressedVectorNodeImpl::reader( std::vector<SourceDestBuffer> dbufs )
310  {
311  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
312 
314 
316  if ( destImageFile->writerCount() > 0 )
317  {
319  "fileName=" + destImageFile->fileName() +
320  " writerCount=" + toString( destImageFile->writerCount() ) +
321  " readerCount=" + toString( destImageFile->readerCount() ) );
322  }
323  if ( destImageFile->readerCount() > 0 )
324  {
326  "fileName=" + destImageFile->fileName() +
327  " writerCount=" + toString( destImageFile->writerCount() ) +
328  " readerCount=" + toString( destImageFile->readerCount() ) );
329  }
330 
332  if ( dbufs.empty() )
333  {
334  throw E57_EXCEPTION2( E57_ERROR_BAD_API_ARGUMENT, "fileName=" + destImageFile->fileName() );
335  }
336 
338  if ( !isAttached() )
339  {
340  throw E57_EXCEPTION2( E57_ERROR_NODE_UNATTACHED, "fileName=" + destImageFile->fileName() );
341  }
342 
344  NodeImplSharedPtr ni( shared_from_this() );
345 #ifdef E57_MAX_VERBOSE
346  // cout << "constructing CAReader, ni:" << std::endl;
347  // ni->dump(4);
348 #endif
349 
351  std::shared_ptr<CompressedVectorNodeImpl> cai( std::static_pointer_cast<CompressedVectorNodeImpl>( ni ) );
352 #ifdef E57_MAX_VERBOSE
353  // cout<<"constructing CAReader, cai:"<<endl;
354  // cai->dump(4);
355 #endif
357  std::shared_ptr<CompressedVectorReaderImpl> cvri( new CompressedVectorReaderImpl( cai, dbufs ) );
358  return ( cvri );
359  }
360 }
#define E57_EXCEPTION2(ecode, context)
Definition: Common.h:68
static uint64_t logicalToPhysical(uint64_t logicalOffset)
Definition: CheckedFile.h:112
void setPrototype(const NodeImplSharedPtr &prototype)
bool isDefined(const ustring &pathName) override
CompressedVectorNodeImpl(ImageFileImplWeakPtr destImageFile)
void checkLeavesInSet(const StringSet &pathNames, NodeImplSharedPtr origin) override
void setCodecs(const std::shared_ptr< VectorNodeImpl > &codecs)
void dump(int indent=0, std::ostream &os=std::cout) const override
std::shared_ptr< CompressedVectorWriterImpl > writer(std::vector< SourceDestBuffer > sbufs)
Iterator constructors.
std::shared_ptr< CompressedVectorReaderImpl > reader(std::vector< SourceDestBuffer > dbufs)
bool isTypeEquivalent(NodeImplSharedPtr ni) override
void writeXml(ImageFileImplSharedPtr imf, CheckedFile &cf, int indent, const char *forcedFieldName=nullptr) override
NodeImplSharedPtr getPrototype() const
std::shared_ptr< VectorNodeImpl > getCodecs() const
ImageFileImplWeakPtr destImageFile_
Definition: NodeImpl.h:94
ustring elementName_
Definition: NodeImpl.h:96
ImageFileImplSharedPtr destImageFile()
Definition: NodeImpl.cpp:126
bool isAttached_
Definition: NodeImpl.h:97
bool isAttached() const
Definition: NodeImpl.cpp:132
void checkImageFileOpen(const char *srcFileName, int srcLineNumber, const char *srcFunctionName) const
Definition: NodeImpl.cpp:41
virtual void dump(int indent=0, std::ostream &os=std::cout) const
Definition: NodeImpl.cpp:372
ustring pathName() const
Definition: NodeImpl.cpp:74
friend class CompressedVectorWriterImpl
Definition: NodeImpl.h:82
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
std::shared_ptr< class NodeImpl > NodeImplSharedPtr
Definition: Common.h:190
std::weak_ptr< class ImageFileImpl > ImageFileImplWeakPtr
Definition: Common.h:189
std::shared_ptr< class ImageFileImpl > ImageFileImplSharedPtr
Definition: Common.h:188
@ E57_ERROR_FILE_IS_READ_ONLY
can't modify read only file
Definition: E57Exception.h:60
@ E57_ERROR_TOO_MANY_READERS
too many open CompressedVectorReaders of an ImageFile
Definition: E57Exception.h:94
@ E57_ERROR_INTERNAL
An unrecoverable inconsistent internal state was detected.
Definition: E57Exception.h:56
@ E57_ERROR_SET_TWICE
attempted to set an existing child element to a new value
Definition: E57Exception.h:47
@ E57_ERROR_BAD_API_ARGUMENT
bad API function argument provided by user
Definition: E57Exception.h:59
@ E57_ERROR_NODE_UNATTACHED
node is not yet attached to tree of ImageFile
Definition: E57Exception.h:88
@ E57_ERROR_NOT_IMPLEMENTED
functionality not implemented
Definition: E57Exception.h:84
@ E57_ERROR_TOO_MANY_WRITERS
too many open CompressedVectorWriters of an ImageFile
Definition: E57Exception.h:93
@ E57_ERROR_ALREADY_HAS_PARENT
node already has a parent
Definition: E57Exception.h:89
@ E57_ERROR_DIFFERENT_DEST_IMAGEFILE
nodes were constructed with different destImageFiles
Definition: E57Exception.h:90
std::string ustring
UTF-8 encodeded Unicode string.
Definition: E57Format.h:54
std::set< std::string > StringSet
Definition: Common.h:194
@ E57_COMPRESSED_VECTOR
CompressedVectorNode class.
Definition: E57Format.h:61
std::string toString(T x)
Definition: Common.h:80
std::string space(size_t n)
Definition: Common.h:73