transform-writer.h
Go to the documentation of this file.
1 //
2 // transform-writer.h
3 // gepetto-viewer
4 //
5 // Created by Joseph Mirabel in November 2015.
6 // Copyright (c) 2015 LAAS-CNRS. All rights reserved.
7 //
8 
9 #ifndef GEPETTO_VIEWER_TRANSFORM_WRITER_VISITOR_HH
10 #define GEPETTO_VIEWER_TRANSFORM_WRITER_VISITOR_HH
11 
12 #include <iostream>
13 #include <fstream>
16 
17 namespace gepetto {
18 namespace viewer {
19 
20  DEF_CLASS_SMART_PTR(TransformWriter)
21  DEF_CLASS_SMART_PTR(BasicTransformWriter)
22  DEF_CLASS_SMART_PTR(YamlTransformWriter)
23  DEF_CLASS_SMART_PTR(TransformWriterVisitor)
24 
25  class TransformWriter : public osg::Referenced
26  {
27  public:
28  // TODO: set the buffer of the filebuf manually so as to increase buffer
29  // size.
30  TransformWriter (const std::string filename)
31  : filename_ (filename)
32  , frameCount_ (0)
33  {}
34 
35  virtual ~TransformWriter () {}
36 
37  void newFrame ();
38  virtual void writeTransform (const char* objName,
39  const osgVector3& vec, const osgQuat& quat) = 0;
40 
41  std::ofstream& out () { return file_; }
42 
43  void openFile () {
44  file_.open (filename_.c_str(),
45  std::ofstream::out | std::ofstream::app);
46  if (!file_.is_open ())
47  throw std::ios_base::failure ("Unable to open file " + filename_);
48  }
49  void closeFile () {
50  file_.close ();
51  }
52 
53  protected:
54  virtual void writeNewFrame () = 0;
55 
56  std::ofstream file_;
57  std::string filename_;
58  unsigned int frameCount_;
59  };
60 
62  {
63  public:
64  BasicTransformWriter (const std::string filename)
65  : TransformWriter (filename)
66  {}
67 
69 
70  void writeTransform (const char* objName,
71  const osgVector3& vec, const osgQuat& quat);
72 
73  protected:
74  void writeNewFrame ();
75  };
76 
78  {
79  public:
80  YamlTransformWriter (const std::string filename)
81  : TransformWriter (filename)
82  {}
83 
85 
86  void writeTransform (const char* objName,
87  const osgVector3& vec, const osgQuat& quat);
88 
89  protected:
90  void writeNewFrame ();
91  };
92 
94  {
95  public:
97  : NodeVisitor (false)
98  , writer_ (writer)
99  {}
100 
101  void captureFrame (Node& node);
102 
103  template <typename Iterator>
104  void captureFrame (Iterator begin, Iterator end);
105 
106  virtual void apply (Node& node);
107 
108  osg::ref_ptr <TransformWriter> writer_;
109  }; /* class TransformWriterVisitor */
110 
111 
112  template <typename Iterator>
113  void TransformWriterVisitor::captureFrame (Iterator begin, Iterator end)
114  {
115  writer_->openFile ();
116  writer_->newFrame ();
117  for (Iterator it = begin; it != end; ++it) apply (**it);
118  writer_->closeFile ();
119  }
120 } /* namespace viewer */
121 } /* namespace gepetto */
122 
123 #endif /* GEPETTO_VIEWER_TRANSFORM_WRITER_VISITOR_HH */
BasicTransformWriter(const std::string filename)
Definition: transform-writer.h:64
Definition: transform-writer.h:61
void writeTransform(const char *objName, const osgVector3 &vec, const osgQuat &quat)
TransformWriter(const std::string filename)
Definition: transform-writer.h:30
void closeFile()
Definition: transform-writer.h:49
~YamlTransformWriter()
Definition: transform-writer.h:84
::osg::Vec3f osgVector3
Definition: config-osg.h:109
::osg::Quat osgQuat
Definition: config-osg.h:112
std::ofstream & out()
Definition: transform-writer.h:41
virtual ~TransformWriter()
Definition: transform-writer.h:35
Definition: node-visitor.h:35
Definition: action-search-bar.hh:27
Definition: transform-writer.h:25
std::string filename_
Definition: transform-writer.h:57
Definition: transform-writer.h:93
~BasicTransformWriter()
Definition: transform-writer.h:68
Definition: transform-writer.h:77
void openFile()
Definition: transform-writer.h:43
#define DEF_CLASS_SMART_PTR(className)
Definition: macros.h:50
Definition: config-osg.h:52
YamlTransformWriter(const std::string filename)
Definition: transform-writer.h:80
TransformWriterVisitor(TransformWriter *writer)
Definition: transform-writer.h:96
osg::ref_ptr< TransformWriter > writer_
Definition: transform-writer.h:108
Abstract base class of 3D objects in a scene.
Definition: node.h:27
unsigned int frameCount_
Definition: transform-writer.h:58
std::ofstream file_
Definition: transform-writer.h:56