GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/gepetto/viewer/transform-writer.h Lines: 0 23 0.0 %
Date: 2020-05-14 11:23:33 Branches: 0 16 0.0 %

Line Branch Exec Source
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>
14
#include <gepetto/viewer/config-osg.h>
15
#include <gepetto/viewer/node-visitor.h>
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
61
    class BasicTransformWriter : public TransformWriter
62
    {
63
      public:
64
        BasicTransformWriter (const std::string filename)
65
          : TransformWriter (filename)
66
        {}
67
68
        ~BasicTransformWriter () {}
69
70
        void writeTransform (const char* objName,
71
            const osgVector3& vec, const osgQuat& quat);
72
73
      protected:
74
        void writeNewFrame ();
75
    };
76
77
    class YamlTransformWriter : public TransformWriter
78
    {
79
      public:
80
        YamlTransformWriter (const std::string filename)
81
          : TransformWriter (filename)
82
        {}
83
84
        ~YamlTransformWriter () {}
85
86
        void writeTransform (const char* objName,
87
            const osgVector3& vec, const osgQuat& quat);
88
89
      protected:
90
        void writeNewFrame ();
91
    };
92
93
    class TransformWriterVisitor :  public NodeVisitor
94
    {
95
      public:
96
        TransformWriterVisitor (TransformWriter* writer)
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 */