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 <gepetto/viewer/config-osg.h> |
13 |
|
|
#include <gepetto/viewer/node-visitor.h> |
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 |
|
|
|
56 |
|
|
class BasicTransformWriter : public TransformWriter { |
57 |
|
|
public: |
58 |
|
|
BasicTransformWriter(const std::string filename) |
59 |
|
|
: TransformWriter(filename) {} |
60 |
|
|
|
61 |
|
✗ |
~BasicTransformWriter() {} |
62 |
|
|
|
63 |
|
|
void writeTransform(const char* objName, const osgVector3& vec, |
64 |
|
|
const osgQuat& quat); |
65 |
|
|
|
66 |
|
|
protected: |
67 |
|
|
void writeNewFrame(); |
68 |
|
|
}; |
69 |
|
|
|
70 |
|
|
class YamlTransformWriter : public TransformWriter { |
71 |
|
|
public: |
72 |
|
✗ |
YamlTransformWriter(const std::string filename) : TransformWriter(filename) {} |
73 |
|
|
|
74 |
|
✗ |
~YamlTransformWriter() {} |
75 |
|
|
|
76 |
|
|
void writeTransform(const char* objName, const osgVector3& vec, |
77 |
|
|
const osgQuat& quat); |
78 |
|
|
|
79 |
|
|
protected: |
80 |
|
|
void writeNewFrame(); |
81 |
|
|
}; |
82 |
|
|
|
83 |
|
|
class TransformWriterVisitor : public NodeVisitor { |
84 |
|
|
public: |
85 |
|
✗ |
TransformWriterVisitor(TransformWriter* writer) |
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 */ |
109 |
|
|
|