Choreonoid  1.8
Seq.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_SEQ_H
7 #define CNOID_UTIL_SEQ_H
8 
9 #include "AbstractSeq.h"
10 #include <memory>
11 #include <vector>
12 #include "exportdecl.h"
13 
14 namespace cnoid {
15 
16 template <typename ElementType> class Seq : public AbstractSeq
17 {
18  typedef Seq<ElementType> SeqType;
19 
20 public:
21  typedef ElementType value_type;
22 
23  Seq(const char* seqType, int nFrames = 0.0)
25  container(nFrames)
26  {
28  offsetTime_ = 0.0;
29  }
30 
31  Seq(const SeqType& org)
32  : AbstractSeq(org),
33  container(org.container)
34  {
35  frameRate_ = org.frameRate_;
37  }
38 
39  SeqType& operator=(const SeqType& rhs)
40  {
41  if(this != &rhs){
43  container = rhs.container;
44  frameRate_ = rhs.frameRate_;
46  }
47  return *this;
48  }
49 
50  virtual AbstractSeq& operator=(const AbstractSeq& rhs) override {
51  const SeqType* rhsSeq = dynamic_cast<const SeqType*>(&rhs);
52  if(rhsSeq){
53  return operator=(*rhsSeq);
54  } else {
55  return AbstractSeq::operator=(rhs);
56  }
57  }
58 
59  virtual std::shared_ptr<AbstractSeq> cloneSeq() const override {
60  return std::make_shared<SeqType>(*this);
61  }
62 
63  virtual ~Seq() { }
64 
65  double frameRate() const {
66  return frameRate_;
67  }
68 
69  virtual double getFrameRate() const override {
70  return frameRate_;
71  }
72 
73  virtual void setFrameRate(double frameRate) override {
75  }
76 
77  int numFrames() const {
78  return container.size();
79  }
80 
81  virtual int getNumFrames() const override {
82  return container.size();
83  }
84 
85  virtual void setNumFrames(int n, bool fillNewElements = false) override {
86  if(fillNewElements){
87  const int nold = numFrames();
88  if(n > nold && nold > 0){
89  container.resize(n, container[nold - 1]);
90  } else {
91  container.resize(n, defaultValue());
92  }
93  } else {
94  container.resize(n);
95  }
96  }
97 
98  void clear() {
99  container.clear();
100  }
101 
102  bool empty() const {
103  return container.empty();
104  }
105 
106  double timeLength() const {
107  return (frameRate_ > 0.0) ? (numFrames() / frameRate_) : 0.0;
108  }
109 
110  int frameOfTime(double time) const {
111  return static_cast<int>((time - offsetTime_) * frameRate_);
112  }
113 
114  int lastFrameOfTime(double time) const {
115  int frame = frameOfTime(time);
116  const int n = numFrames();
117  if(frame >= n){
118  frame = (n > 0) ? (n - 1) : 0;
119  }
120  return frame;
121  }
122 
123  double timeOfFrame(int frame) const {
124  return (frameRate_ > 0.0) ? ((frame / frameRate_) + offsetTime_) : offsetTime_;
125  }
126 
127  virtual double getOffsetTime() const override {
128  return offsetTime_;
129  }
130 
131  virtual void setOffsetTime(double time) override {
132  offsetTime_ = time;
133  }
134 
135  int offsetTimeFrame() const {
136  return static_cast<int>(offsetTime_ * frameRate_);
137  }
138 
139  void setOffsetTimeFrame(int offset) {
140  offsetTime_ = (frameRate_ > 0) ? (offset / frameRate_) : 0.0;
141  }
142 
143  ElementType& operator[](int frameIndex) {
144  return container[frameIndex];
145  }
146 
147  const ElementType& operator[](int frameIndex) const {
148  return container[frameIndex];
149  }
150 
151  ElementType& at(int frameIndex) {
152  return container[frameIndex];
153  }
154 
155  const ElementType& at(int frameIndex) const {
156  return container[frameIndex];
157  }
158 
159  ElementType& front() {
160  return container.front();
161  }
162 
163  const ElementType& front() const {
164  return container.front();
165  }
166 
167  ElementType& back() {
168  return container.back();
169  }
170 
171  const ElementType& back() const {
172  return container.back();
173  }
174 
175  int clampFrameIndex(int frameIndex, bool& out_isValidRange){
176  if(frameIndex < 0){
177  frameIndex = 0;
178  out_isValidRange = false;
179  } else if(frameIndex >= numFrames()){
180  frameIndex = numFrames() - 1;
181  out_isValidRange = false;
182  } else {
183  out_isValidRange = true;
184  }
185  return frameIndex;
186  }
187 
188 protected:
189  std::vector<ElementType> container;
190  double frameRate_;
191  double offsetTime_;
192 
193  virtual ElementType defaultValue() const { return ElementType(); }
194 };
195 
196 }
197 
198 #endif
cnoid::Seq::clear
void clear()
Definition: Seq.h:98
cnoid::Seq::operator[]
ElementType & operator[](int frameIndex)
Definition: Seq.h:143
cnoid::AbstractSeq::seqType
const std::string & seqType() const
Definition: AbstractSeq.h:36
cnoid::Seq::frameRate_
double frameRate_
Definition: Seq.h:190
cnoid::Seq::frameOfTime
int frameOfTime(double time) const
Definition: Seq.h:110
cnoid::Seq::lastFrameOfTime
int lastFrameOfTime(double time) const
Definition: Seq.h:114
cnoid::Seq::operator=
SeqType & operator=(const SeqType &rhs)
Definition: Seq.h:39
cnoid::Seq::defaultValue
virtual ElementType defaultValue() const
Definition: Seq.h:193
cnoid::Seq::setOffsetTime
virtual void setOffsetTime(double time) override
Definition: Seq.h:131
cnoid::Seq
Definition: Seq.h:16
cnoid::Seq::front
const ElementType & front() const
Definition: Seq.h:163
cnoid::Seq::setNumFrames
virtual void setNumFrames(int n, bool fillNewElements=false) override
Definition: Seq.h:85
cnoid::Seq::at
ElementType & at(int frameIndex)
Definition: Seq.h:151
cnoid::Seq::value_type
ElementType value_type
Definition: Seq.h:21
AbstractSeq.h
cnoid::Seq::getFrameRate
virtual double getFrameRate() const override
Definition: Seq.h:69
cnoid::Seq::Seq
Seq(const SeqType &org)
Definition: Seq.h:31
cnoid::Seq::operator=
virtual AbstractSeq & operator=(const AbstractSeq &rhs) override
Definition: Seq.h:50
cnoid::Seq::at
const ElementType & at(int frameIndex) const
Definition: Seq.h:155
cnoid::Seq::operator[]
const ElementType & operator[](int frameIndex) const
Definition: Seq.h:147
cnoid::Seq::back
const ElementType & back() const
Definition: Seq.h:171
cnoid::Seq::Seq
Seq(const char *seqType, int nFrames=0.0)
Definition: Seq.h:23
cnoid::Seq::getNumFrames
virtual int getNumFrames() const override
Definition: Seq.h:81
cnoid::Seq::container
std::vector< ElementType > container
Definition: Seq.h:189
cnoid::Seq::setFrameRate
virtual void setFrameRate(double frameRate) override
Definition: Seq.h:73
cnoid::Seq::offsetTime_
double offsetTime_
Definition: Seq.h:191
cnoid::AbstractSeq::operator=
virtual AbstractSeq & operator=(const AbstractSeq &rhs)
Definition: AbstractSeq.cpp:39
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Seq::back
ElementType & back()
Definition: Seq.h:167
cnoid::Seq::empty
bool empty() const
Definition: Seq.h:102
cnoid::Seq::offsetTimeFrame
int offsetTimeFrame() const
Definition: Seq.h:135
cnoid::AbstractSeq
Definition: AbstractSeq.h:21
cnoid::Seq::numFrames
int numFrames() const
Definition: Seq.h:77
cnoid::Seq::getOffsetTime
virtual double getOffsetTime() const override
Definition: Seq.h:127
cnoid::Seq::clampFrameIndex
int clampFrameIndex(int frameIndex, bool &out_isValidRange)
Definition: Seq.h:175
cnoid::AbstractSeq::defaultFrameRate
static double defaultFrameRate()
Definition: AbstractSeq.cpp:72
cnoid::Seq::setOffsetTimeFrame
void setOffsetTimeFrame(int offset)
Definition: Seq.h:139
cnoid::Seq::front
ElementType & front()
Definition: Seq.h:159
cnoid::Seq::frameRate
double frameRate() const
Definition: Seq.h:65
cnoid::Seq::~Seq
virtual ~Seq()
Definition: Seq.h:63
cnoid::Seq::timeOfFrame
double timeOfFrame(int frame) const
Definition: Seq.h:123
cnoid::Seq::timeLength
double timeLength() const
Definition: Seq.h:106
cnoid::Seq::cloneSeq
virtual std::shared_ptr< AbstractSeq > cloneSeq() const override
Definition: Seq.h:59