gepetto-viewer  6.0.0
An user-friendly Graphical Interface
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 
14 
15 #include <fstream>
16 #include <iostream>
17 
18 namespace gepetto {
19 namespace viewer {
20 
21 DEF_CLASS_SMART_PTR(TransformWriter)
22 DEF_CLASS_SMART_PTR(BasicTransformWriter)
23 DEF_CLASS_SMART_PTR(YamlTransformWriter)
24 DEF_CLASS_SMART_PTR(TransformWriterVisitor)
25 
26 class TransformWriter : public osg::Referenced {
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), frameCount_(0) {}
32 
33  virtual ~TransformWriter() {}
34 
35  void newFrame();
36  virtual void writeTransform(const char* objName, const osgVector3& vec,
37  const osgQuat& quat) = 0;
38 
39  std::ofstream& out() { return file_; }
40 
41  void openFile() {
42  file_.open(filename_.c_str(), std::ofstream::out | std::ofstream::app);
43  if (!file_.is_open())
44  throw std::ios_base::failure("Unable to open file " + filename_);
45  }
46  void closeFile() { file_.close(); }
47 
48  protected:
49  virtual void writeNewFrame() = 0;
50 
51  std::ofstream file_;
52  std::string filename_;
53  unsigned int frameCount_;
54 };
55 
57  public:
58  BasicTransformWriter(const std::string filename)
59  : TransformWriter(filename) {}
60 
62 
63  void writeTransform(const char* objName, const osgVector3& vec,
64  const osgQuat& quat);
65 
66  protected:
67  void writeNewFrame();
68 };
69 
71  public:
72  YamlTransformWriter(const std::string filename) : TransformWriter(filename) {}
73 
75 
76  void writeTransform(const char* objName, const osgVector3& vec,
77  const osgQuat& quat);
78 
79  protected:
80  void writeNewFrame();
81 };
82 
84  public:
86  : NodeVisitor(false), writer_(writer) {}
87 
88  void captureFrame(Node& node);
89 
90  template <typename Iterator>
91  void captureFrame(Iterator begin, Iterator end);
92 
93  virtual void apply(Node& node);
94 
95  osg::ref_ptr<TransformWriter> writer_;
96 }; /* class TransformWriterVisitor */
97 
98 template <typename Iterator>
99 void TransformWriterVisitor::captureFrame(Iterator begin, Iterator end) {
100  writer_->openFile();
101  writer_->newFrame();
102  for (Iterator it = begin; it != end; ++it) apply(**it);
103  writer_->closeFile();
104 }
105 } /* namespace viewer */
106 } /* namespace gepetto */
107 
108 #endif /* GEPETTO_VIEWER_TRANSFORM_WRITER_VISITOR_HH */
Definition: transform-writer.h:56
BasicTransformWriter(const std::string filename)
Definition: transform-writer.h:58
void writeTransform(const char *objName, const osgVector3 &vec, const osgQuat &quat)
~BasicTransformWriter()
Definition: transform-writer.h:61
Definition: node-visitor.h:35
Abstract base class of 3D objects in a scene.
Definition: node.h:25
Definition: transform-writer.h:83
osg::ref_ptr< TransformWriter > writer_
Definition: transform-writer.h:95
TransformWriterVisitor(TransformWriter *writer)
Definition: transform-writer.h:85
Definition: transform-writer.h:26
virtual void writeTransform(const char *objName, const osgVector3 &vec, const osgQuat &quat)=0
std::ofstream & out()
Definition: transform-writer.h:39
void openFile()
Definition: transform-writer.h:41
unsigned int frameCount_
Definition: transform-writer.h:53
std::string filename_
Definition: transform-writer.h:52
virtual ~TransformWriter()
Definition: transform-writer.h:33
std::ofstream file_
Definition: transform-writer.h:51
TransformWriter(const std::string filename)
Definition: transform-writer.h:30
void closeFile()
Definition: transform-writer.h:46
Definition: transform-writer.h:70
YamlTransformWriter(const std::string filename)
Definition: transform-writer.h:72
~YamlTransformWriter()
Definition: transform-writer.h:74
void writeTransform(const char *objName, const osgVector3 &vec, const osgQuat &quat)
::osg::Vec3f osgVector3
Definition: config-osg.h:99
::osg::Quat osgQuat
Definition: config-osg.h:102
#define DEF_CLASS_SMART_PTR(className)
Definition: macros.h:51
Definition: action-search-bar.hh:27
Definition: config-osg.h:44