AGX Dynamics  2.35.0.1
StorageStream.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/agxPhysics_export.h>
20 #include <iostream>
21 #include <agx/Vec3.h>
22 #include <agx/AffineMatrix4x4.h>
23 #include <stack>
24 #include <memory>
25 
26 namespace agx
27 {
28  class RigidBody;
29 }
30 
31 namespace agxWire
32 {
33  class Node;
34 }
35 
36 namespace agxCollide
37 {
38  class Geometry;
39 }
40 
41 namespace agxStream
42 {
49  {
50  public:
51 
52  enum Mode
53  {
55  STORE
56  };
57 
58  enum FloatMode
59  {
60  AS_DOUBLE = 0,
61  AS_FLOAT = 1
62  };
63 
69  StorageStream(Mode mode, bool useBlocks=false);
70 
72  virtual ~StorageStream();
73 
77  bool read(const agx::Vector<char>& buffer);
78 
82  bool write(agx::Vector<char>& buffer);
83 
87  void clear();
88 
89  enum Status
90  {
100  MIXING_MODES
101  };
102 
105 
108 
111 
113  Status getStatus() const { return m_status; }
114 #ifndef SWIG
122  virtual bool read(agx::RigidBody** body);
123 
132  virtual bool write(agx::RigidBody* body);
133 
141  virtual bool read(agxCollide::Geometry** geometry);
142 
151  virtual bool write(agxCollide::Geometry* geometry);
152 
158  virtual bool read(agxWire::Node* node);
159 
168  virtual bool write(agxWire::Node* node);
169 
174  virtual std::ostream& write(std::ostream& str);
175 
180  virtual std::istream& read(std::istream& str);
181 
182  inline Mode getMode() const;
183 
184  AGXPHYSICS_EXPORT friend std::ostream& operator<<(std::ostream& str, StorageStream& storage);
185  AGXPHYSICS_EXPORT friend std::istream& operator>>(std::istream& str, StorageStream& storage);
186 
187  // --------- Restoring/reading
188  virtual void read(agx::UInt8& val);
189  virtual void read(agx::UInt16& val);
190  virtual void read(agx::UInt64& val);
191  virtual void read(agx::Int32& val);
192 
193  virtual void read(char& val);
194  virtual void read(bool& val);
195 
197  virtual void read(float& val, bool readAsFloat = false);
199  virtual void read(double& val, bool readAsFloat = false);
200 
201  virtual void read(std::string& val);
202 
204  virtual void read(agx::Vec3d& val, bool readAsFloat = false);
205 
207  virtual void read(agx::Vec3f& val, bool readAsFloat = false);
208 
210  virtual void read(agx::Quat64& val, bool readAsFloat = false);
211 
213  virtual void read(agx::Quat32& val, bool readAsFloat = false);
214 
216  virtual void read(agx::AffineMatrix4x4d& val, bool readAsFloat = false);
217 
219  virtual void read(agx::AffineMatrix4x4f& val, bool readAsFloat = false);
220 
221  // -------- Storing/writing
222  virtual void write(agx::UInt8 val);
223  virtual void write(agx::UInt16 val);
224  virtual void write(agx::UInt64 val);
225  virtual void write(agx::Int32 val);
226 
227  virtual void write(char val);
228  virtual void write(bool val);
229 
230 
232  virtual void write(float val, FloatMode writeAsFloat = AS_FLOAT);
233 
235  virtual void write(double val, FloatMode writeAsFloat = AS_DOUBLE);
236 
237  virtual void write(const std::string& val);
238 
240  virtual void write(const agx::Vec3d& val, FloatMode writeAsFloat = AS_DOUBLE);
241 
243  virtual void write(const agx::Vec3f& val, FloatMode writeAsFloat = AS_DOUBLE);
244 
246  virtual void write(const agx::Quat64& val, FloatMode writeAsFloat = AS_DOUBLE);
247 
249  virtual void write(const agx::Quat32& val, FloatMode writeAsFloat = AS_DOUBLE);
250 
252  virtual void write(const agx::AffineMatrix4x4d& val, FloatMode writeAsFloat = AS_DOUBLE);
253 
255  virtual void write(const agx::AffineMatrix4x4f& val, FloatMode writeAsFloat = AS_DOUBLE);
256 
264  void beginBlock(const char* blockName);
265 
270  bool endBlock(const char* blockName);
271 
272  void skipBlock(const char* blockName);
273 
275  bool good() const;
276 
281  {
282  public:
288  ScopedBlock(StorageStream& stream, const char* blockName);
290  private:
291  ScopedBlock(const ScopedBlock&);
292  ScopedBlock& operator=(const ScopedBlock&);
293  private:
294  StorageStream& m_stream;
295  const char* m_blockName;
296  };
297 
302  void setEnableMD5(bool flag) { m_checkMD5 = flag; }
303 
307  bool getEnableMD5() const { return m_checkMD5; }
308 
310  std::stringstream &getStream();
311  const std::stringstream &getStream() const;
312 
313 #endif /* SWIG */
314  protected:
315 
316  void readHeader(std::istream& str);
317  void writeHeader(std::ostream& str, const char* data);
318 
319 
320  void checkMD5();
321 
322  void checkMode(Mode newMode);
323 
326 
327 
328  private:
329 
330  // Internal class for storing stack of blocks
331  class AGXPHYSICS_EXPORT StorageStack
332  {
333  public:
334 
336  StorageStack(std::shared_ptr<std::stringstream> root);
337  StorageStack() { agxAbort(); }
338 
340  virtual ~StorageStack();
341 
343  std::string name() const;
344 
346  std::shared_ptr<std::stringstream> top();
347 
348  std::shared_ptr<const std::stringstream> top() const;
349 
351  uint32_t size() { return uint32_t(m_stack.size()); }
352 
354  void push(const std::string& blockName, std::shared_ptr<std::stringstream>);
355 
357  void pop();
358 
360  void clear(std::shared_ptr <std::stringstream> root);
361 
363  uint32_t numBytes();
364  std::shared_ptr<std::stringstream> getNewStream();
365 
366 
367  void flush();
368 
369  protected:
370 
371  void writeBuffer(const char* data, size_t numBytes, std::stringstream* stream);
372 
373  typedef std::stack<std::pair<std::string, std::shared_ptr<std::stringstream> > > StreamStack;
374  StreamStack m_stack;
375  std::shared_ptr<std::stringstream> m_root;
376  std::stack<std::shared_ptr<std::stringstream> > m_streamPool;
377 
378  agx::VectorPOD<char> m_writeBuffer;
379  agx::UInt32 m_bufferPosition;
380  agx::UInt32 m_bufferSize;
381 
382  };
383 
384  private:
385 
386 
387  bool m_checkMD5;
388 
389  std::string m_md5;
390  agx::UInt8 m_generation, m_major, m_minor, m_patch;
391  agx::UInt16 m_serializationVersion;
392  agx::UInt16 m_storageStreamVersion;
393 
394  Status m_status;
395  Mode m_mode;
396 
397  std::shared_ptr<std::stringstream> m_data;
398  agx::UInt32 m_dataSize;
399 
400  StorageStack m_stack;
401  bool m_useBlocks;
402  };
403 
404  AGXPHYSICS_EXPORT std::ostream& operator<<(std::ostream& str, StorageStream& storage);
405  AGXPHYSICS_EXPORT std::istream& operator>>(std::istream& str, StorageStream& storage);
406 
407  inline bool StorageStream::read(agx::RigidBody ** /*body*/) { return false; }
408  inline bool StorageStream::write(agx::RigidBody * /*body*/) { return false; }
409  inline bool StorageStream::read(agxCollide::Geometry ** /*geometry*/) { return false; }
410  inline bool StorageStream::write(agxCollide::Geometry * /*geometry*/) { return false; }
411  inline bool StorageStream::read(agxWire::Node * /*node*/) { return false; }
412  inline bool StorageStream::write(agxWire::Node * /*node*/) { return false; }
413 
414 
415  // ------------------- Reading
417  {
418  in.read(val);
419  return in;
420  }
421 
423  {
424  in.read(val);
425  return in;
426  }
427 
429  {
430  in.read(val);
431  return in;
432  }
433 
435  {
436  in.read(val);
437  return in;
438  }
439 
441  {
442  int32_t readVal = 0;
443  in.read(readVal);
444  val = int(readVal);
445  return in;
446  }
447 
448  inline StorageStream& operator>>(StorageStream& in, size_t& val)
449  {
450  uint64_t readVal = 0;
451  in.read(readVal);
452  val = size_t(readVal);
453  return in;
454  }
455 
456 #ifdef __APPLE__
457  inline StorageStream& operator>>(StorageStream& in, agx::UInt& val)
458  {
459  uint64_t readVal = 0;
460  in.read(readVal);
461  val = agx::UInt(readVal);
462  return in;
463  }
464 #endif
465 
466  inline StorageStream& operator>>(StorageStream& in, float& val)
467  {
468  in.read(val);
469  return in;
470  }
471 
472  inline StorageStream& operator>>(StorageStream& in, double& val)
473  {
474  in.read(val);
475  return in;
476  }
477 
478  inline StorageStream& operator>>(StorageStream& in, std::string& val)
479  {
480  in.read(val);
481  return in;
482  }
483 
485  {
486  in.read(rb);
487  return in;
488  }
489 
491  {
492  in.read(geometry);
493  return in;
494  }
495 
497  {
498  in.read(val);
499  return in;
500  }
501 
503  {
504  in.read(val);
505  return in;
506  }
507 
509  {
510  in.read(q);
511  return in;
512  }
513 
515  {
516  in.read(q);
517  return in;
518  }
519 
521  {
522  in.read(val);
523  return in;
524  }
525 
527  {
528  in.read(val);
529  return in;
530  }
531 
533  {
534  in.read(node);
535  return in;
536  }
537 
538  // ---------------- Writing
540  {
541  out.write(val);
542  return out;
543  }
544 
546  {
547  out.write(val);
548  return out;
549  }
550 
551 
553  {
554  out.write(val);
555  return out;
556  }
557 
559  {
560  out.write(val);
561  return out;
562  }
563 
564 
566  {
567  out.write(int32_t(val));
568  return out;
569  }
570 
572  {
573  out.write(uint64_t(val));
574  return out;
575  }
576 
577 #ifdef __APPLE__
578  inline StorageStream& operator<<(StorageStream& out, agx::UInt val)
579  {
580  out.write(uint64_t(val));
581  return out;
582  }
583 #endif
584 
586  {
587  out.write(val);
588  return out;
589  }
590 
592  {
593  out.write(val);
594  return out;
595  }
596 
597  inline StorageStream& operator<<(StorageStream& out, const std::string& val)
598  {
599  out.write(val);
600  return out;
601  }
602 
604  {
605  out.write(const_cast< agx::RigidBody* >(rb));
606  return out;
607  }
608 
610  {
611  out.write(const_cast< agxCollide::Geometry* >(geometry));
612  return out;
613  }
614 
616  {
617  out.write(v);
618  return out;
619  }
620 
622  {
623  out.write(v);
624  return out;
625  }
626 
628  {
629  out.write(v);
630  return out;
631  }
632 
634  {
635  out.write(v);
636  return out;
637  }
638 
640  {
641  out.write(q);
642  return out;
643  }
644 
646  {
647  out.write(q);
648  return out;
649  }
650 
652  {
653  out.write(const_cast< agxWire::Node* >(node));
654  return out;
655  }
656 
658  {
659  return m_mode;
660  }
661 } // namespace agxStream
662 
#define AGXPHYSICS_EXPORT
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
Ties a beginBlock/endBlock pair to a scope.
ScopedBlock(StorageStream &stream, const char *blockName)
The data pointed to by 'blockName' is not copied.
Abstract base class for storing/restoring a line/drums with version control.
Definition: StorageStream.h:49
void readHeader(std::istream &str)
virtual void read(agx::UInt64 &val)
const std::stringstream & getStream() const
virtual void write(const agx::Vec3f &val, FloatMode writeAsFloat=AS_DOUBLE)
writeAsFloat there to make this method compatible with write( double, bool ), it WILL be written as f...
StorageStream(Mode mode, bool useBlocks=false)
virtual std::istream & read(std::istream &str)
Method that will read data from a stream into this object.Derive your own if you want to read any loc...
virtual void read(agx::Quat64 &val, bool readAsFloat=false)
If readAsFloat==true, then this Vec3 will be read from the stream as a 3 floats.
virtual void read(agx::Vec3d &val, bool readAsFloat=false)
If readAsFloat==true, then this Vec3 will be read from the stream as a 3 floats.
agx::UInt16 getStorageStreamVersion() const
virtual void write(char val)
virtual std::ostream & write(std::ostream &str)
Method that will write this object to an output stream, derive your own if you want to write any loca...
virtual void write(double val, FloatMode writeAsFloat=AS_DOUBLE)
If writeAsFloat==true, then this variable will be written to the stream as a float.
virtual void write(const agx::Vec3d &val, FloatMode writeAsFloat=AS_DOUBLE)
If writeAsFloat==true, then this vector will be written to the stream as 3*float.
void setEnableMD5(bool flag)
Enable/Disable the MD5 check of the data.
virtual void read(bool &val)
virtual void write(const agx::Quat64 &val, FloatMode writeAsFloat=AS_DOUBLE)
If writeAsFloat==true, then this vector will be written to the stream as 3*float.
agx::UInt32 getVersion() const
agx::UInt16 getSerializationVersion() const
virtual ~StorageStream()
Destructor.
void clear()
Re-initialize the stream so it has no data stored, just as it was when it was created initially.
AGXPHYSICS_EXPORT friend std::ostream & operator<<(std::ostream &str, StorageStream &storage)
virtual void write(const std::string &val)
virtual void read(agx::Vec3f &val, bool readAsFloat=false)
readAsFloat there to make this method compatible with write( double, bool ), it will always be read a...
Status getStatus() const
virtual void write(agx::UInt8 val)
virtual void write(agx::UInt16 val)
AGXPHYSICS_EXPORT friend std::istream & operator>>(std::istream &str, StorageStream &storage)
std::stringstream & getStream()
Return a direct reference to the current block stream, use with caution.
void writeHeader(std::ostream &str, const char *data)
virtual void read(std::string &val)
virtual void write(const agx::AffineMatrix4x4f &val, FloatMode writeAsFloat=AS_DOUBLE)
writeAsFloat there to make this method compatible with write( double, bool ), it WILL be written as f...
StorageStream(const StorageStream &)
virtual void read(agx::UInt16 &val)
bool endBlock(const char *blockName)
End the block started by the corresponding beginBlock.
virtual void write(float val, FloatMode writeAsFloat=AS_FLOAT)
writeAsFloat there to make this method compatible with write( double, bool ), it WILL be written as f...
void beginBlock(const char *blockName)
Begin a new logical block within the stream.
bool write(agx::Vector< char > &buffer)
Write data from the stream into the buffer for external storage.
virtual void read(agx::AffineMatrix4x4f &val, bool readAsFloat=false)
readAsFloat there to make this method compatible with write( double, bool ), it will always be read a...
virtual void write(agx::Int32 val)
virtual void write(const agx::Quat32 &val, FloatMode writeAsFloat=AS_DOUBLE)
writeAsFloat there to make this method compatible with write( double, bool ), it WILL be written as f...
virtual void read(agx::AffineMatrix4x4d &val, bool readAsFloat=false)
If readAsFloat==true, then this matrix will be read from the stream as a 16 floats.
virtual void read(float &val, bool readAsFloat=false)
readAsFloat there to make this method compatible with write( double, bool ), it will be read as float...
virtual void read(char &val)
virtual void write(const agx::AffineMatrix4x4d &val, FloatMode writeAsFloat=AS_DOUBLE)
If writeAsFloat==true, then this Matrix will be written to the stream as 12*float.
virtual void read(agx::Quat32 &val, bool readAsFloat=false)
readAsFloat there to make this method compatible with write( double, bool ), it will always be read a...
virtual void write(agx::UInt64 val)
bool read(const agx::Vector< char > &buffer)
Read data from the buffer into the stream for restore.
virtual void read(agx::Int32 &val)
void checkMode(Mode newMode)
virtual void read(agx::UInt8 &val)
virtual void write(bool val)
virtual void read(double &val, bool readAsFloat=false)
If readAsFloat==true, then this variable will be read from the stream as a float.
void skipBlock(const char *blockName)
Class defining a node that is part of a wire.
Definition: agxWire/Node.h:512
Matrix class for rigid transformations (translation, rotation).
The object holding quaternions and providing operations on these.
Definition: QuatTemplate.h:57
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
A class holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3Template.h:46
#define agxAbort()
Definition: debug.h:164
This namespace consists of a set of classes for handling geometric intersection tests including boole...
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
InputArchive & operator>>(InputArchive &in, InputVal< T > val)
Definition: InputArchive.h:462
OutputArchive & operator<<(OutputArchive &out, OutputRef< T > val)
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
bool AGXPHYSICS_EXPORT read(const std::string &filename, agxStream::SerializablePtrVector &readObjects)
Open and read serializable objects from a file on disk with previously stored objects.
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive.
AGXTERRAIN_EXPORT void clear()
Implements a Wire model with adaptive resolution.
Definition: MergedBody.h:40
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint16_t UInt16
Definition: Integer.h:39
int32_t Int32
Definition: Integer.h:45
uint32_t UInt32
Definition: Integer.h:40
uint64_t UInt64
Definition: Integer.h:41
AGXCORE_EXPORT Component * root()
uint32_t UInt
Definition: Integer.h:34
uint8_t UInt8
Definition: Integer.h:38