Choreonoid  1.8
MultiSeq.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_MULTI_SEQ_H
7 #define CNOID_UTIL_MULTI_SEQ_H
8 
9 #include "AbstractSeq.h"
10 #include "Deque2D.h"
11 #include <algorithm>
12 #include <memory>
13 
14 namespace cnoid {
15 
16 template <typename ElementType, typename Allocator = std::allocator<ElementType>>
17 class MultiSeq : public Deque2D<ElementType, Allocator>, public AbstractMultiSeq
18 {
20 
21 public:
23 
25  typedef typename Container::Row Frame;
26  typedef typename Container::Column Part;
27  typedef value_type Element;
28 
29  MultiSeq(const char* seqType)
30  : Container(0, 1),
32  frameRate_ = 0.0;
33  offsetTime_ = 0.0;
34  }
35 
36  MultiSeq(const char* seqType, int numFrames, int numParts)
39  frameRate_ = 0.0;
40  offsetTime_ = 0.0;
41  }
42 
43  MultiSeq(const MultiSeqType& org)
44  : Container(org),
45  AbstractMultiSeq(org) {
46  frameRate_ = org.frameRate_;
48  }
49 
50  virtual ~MultiSeq() { }
51 
53  if(this != &rhs){
56  frameRate_ = rhs.frameRate_;
58  }
59  return *this;
60  }
61 
62  virtual AbstractSeq& operator=(const AbstractSeq& rhs) override {
63  const MultiSeqType* rhsSeq = dynamic_cast<const MultiSeqType*>(&rhs);
64  if(rhsSeq){
65  return operator=(*rhsSeq);
66  } else {
67  return AbstractSeq::operator=(rhs);
68  }
69  }
70 
71  virtual std::shared_ptr<AbstractSeq> cloneSeq() const override {
72  return std::make_shared<MultiSeqType>(*this);
73  }
74 
75  void copySeqProperties(const MultiSeqType& source) {
77  frameRate_ = source.frameRate_;
78  setNumParts(source.numParts());
79  }
80 
81  virtual void setDimension(int newNumFrames, int newNumParts, bool fillNewElements = false) override {
82 
83  const int prevNumParts = numParts();
84  const int prevNumFrames = numFrames();
85 
86  Container::resize(newNumFrames, newNumParts);
87 
88  if(fillNewElements){
89  if(newNumParts != prevNumParts){
90  std::fill(Container::begin(), Container::end(), defaultValue());
91  } else {
92  if(newNumFrames > prevNumFrames){
93  if(prevNumFrames == 0){
94  std::fill(Container::begin() + prevNumFrames * newNumParts, Container::end(), defaultValue());
95  } else {
96  Frame last = frame(prevNumFrames - 1);
97  for(int i=prevNumFrames; i < newNumFrames; ++i){
98  std::copy(last.begin(), last.end(), frame(i).begin());
99  }
100  }
101  }
102  }
103  }
104  }
105 
106  virtual double getFrameRate() const override {
107  return frameRate_;
108  }
109 
110  double frameRate() const {
111  return frameRate_;
112  }
113 
114  virtual void setFrameRate(double frameRate) override {
116  }
117 
118  const double timeStep() const {
119  const double r = getFrameRate();
120  return (r > 0.0) ? 1.0 / r : 0.0;
121  }
122 
123  virtual void setNumParts(int newNumParts, bool fillNewElements = false) override {
124  setDimension(numFrames(), newNumParts, fillNewElements);
125  }
126 
127  int numFrames() const {
128  return Container::rowSize();
129  }
130 
131  virtual int getNumFrames() const override {
132  return numFrames();
133  }
134 
135  virtual void setNumFrames(int newNumFrames, bool fillNewElements = false) override {
136  setDimension(newNumFrames, numParts(), fillNewElements);
137  }
138 
139  void clearFrames(){
140  setNumFrames(0);
141  }
142 
143  virtual int getNumParts() const override {
144  return Container::colSize();
145  }
146 
147  int numParts() const {
148  return Container::colSize();
149  }
150 
151  double timeLength() const {
152  return (frameRate_ > 0.0) ? (numFrames() / frameRate_) : 0.0;
153  }
154 
155  int frameOfTime(double time) const {
156  return static_cast<int>((time - offsetTime_) * frameRate_);
157  }
158 
159  double timeOfFrame(int frame) const {
160  return (frameRate_ > 0.0) ? ((frame / frameRate_) + offsetTime_) : offsetTime_;
161  }
162 
163  double offsetTime() const {
164  return offsetTime_;
165  }
166 
167  virtual double getOffsetTime() const override {
168  return offsetTime_;
169  }
170 
171  virtual void setOffsetTime(double time) override {
172  offsetTime_ = time;
173  }
174 
175  int offsetTimeFrame() const {
176  return static_cast<int>(offsetTime_ * frameRate_);
177  }
178 
179  void setOffsetTimeFrame(int offset) {
180  offsetTime_ = (frameRate_ > 0) ? (offset / frameRate_) : 0.0;
181  }
182 
183  const Part part(int index) const {
184  return Container::column(index);
185  }
186 
187  Part part(int index) {
188  return Container::column(index);
189  }
190 
191  Frame frame(int index) {
192  return Container::row(index);
193  }
194 
195  const Frame frame(int index) const {
196  return Container::row(index);
197  }
198 
199  void popFrontFrame() {
201  }
202 
204  return Container::append();
205  }
206 
207  int clampFrameIndex(int frameIndex){
208  if(frameIndex < 0){
209  return 0;
210  } else if(frameIndex >= numFrames()){
211  return numFrames() - 1;
212  }
213  return frameIndex;
214  }
215 
216 protected:
217  double frameRate_;
218  double offsetTime_;
219 
220  virtual value_type defaultValue() const { return value_type(); }
221 };
222 
223 }
224 
225 #endif
cnoid::MultiSeq::Container
Deque2D< ElementType, Allocator > Container
Definition: MultiSeq.h:22
cnoid::MultiSeq::setNumParts
virtual void setNumParts(int newNumParts, bool fillNewElements=false) override
Definition: MultiSeq.h:123
cnoid::Deque2D::end
iterator end()
Definition: Deque2D.h:146
cnoid::Deque2D::column
Column column(int colIndex)
Definition: Deque2D.h:592
cnoid::MultiSeq::frameOfTime
int frameOfTime(double time) const
Definition: MultiSeq.h:155
cnoid::Deque2D::append
Row append()
Definition: Deque2D.h:600
cnoid::MultiSeq::timeOfFrame
double timeOfFrame(int frame) const
Definition: MultiSeq.h:159
cnoid::AbstractSeq::seqType
const std::string & seqType() const
Definition: AbstractSeq.h:36
cnoid::MultiSeq::operator=
virtual AbstractSeq & operator=(const AbstractSeq &rhs) override
Definition: MultiSeq.h:62
cnoid::MultiSeq::MultiSeq
MultiSeq(const MultiSeqType &org)
Definition: MultiSeq.h:43
cnoid::Deque2D::operator=
Deque2DType & operator=(const Deque2DType &rhs)
Definition: Deque2D.h:342
cnoid::MultiSeq::frameRate
double frameRate() const
Definition: MultiSeq.h:110
Deque2D.h
cnoid::AbstractMultiSeq::operator=
AbstractMultiSeq & operator=(const AbstractMultiSeq &rhs)
Definition: AbstractSeq.cpp:210
cnoid::MultiSeq::~MultiSeq
virtual ~MultiSeq()
Definition: MultiSeq.h:50
cnoid::MultiSeq::MultiSeq
MultiSeq(const char *seqType, int numFrames, int numParts)
Definition: MultiSeq.h:36
cnoid::AbstractMultiSeq
Definition: AbstractSeq.h:99
cnoid::MultiSeq::numParts
int numParts() const
Definition: MultiSeq.h:147
cnoid::MultiSeq::getNumFrames
virtual int getNumFrames() const override
Definition: MultiSeq.h:131
cnoid::MultiSeq::clearFrames
void clearFrames()
Definition: MultiSeq.h:139
cnoid::MultiSeq::operator=
MultiSeqType & operator=(const MultiSeqType &rhs)
Definition: MultiSeq.h:52
cnoid::AbstractMultiSeq::copySeqProperties
void copySeqProperties(const AbstractMultiSeq &source)
Definition: AbstractSeq.cpp:217
AbstractSeq.h
cnoid::MultiSeq::offsetTime_
double offsetTime_
Definition: MultiSeq.h:218
cnoid::Deque2D< ElementType, std::allocator< ElementType > >::last
Row last()
Definition: Deque2D.h:584
cnoid::MultiSeq
Definition: MultiSeq.h:17
cnoid::Deque2D::pop_front
void pop_front(int numRows=1)
Definition: Deque2D.h:609
cnoid::MultiSeq::clampFrameIndex
int clampFrameIndex(int frameIndex)
Definition: MultiSeq.h:207
cnoid::MultiSeq::numFrames
int numFrames() const
Definition: MultiSeq.h:127
cnoid::MultiSeq::value_type
Container::value_type value_type
Definition: MultiSeq.h:24
cnoid::MultiSeq::setOffsetTimeFrame
void setOffsetTimeFrame(int offset)
Definition: MultiSeq.h:179
cnoid::MultiSeq::getNumParts
virtual int getNumParts() const override
Definition: MultiSeq.h:143
cnoid::MultiSeq::MultiSeq
MultiSeq(const char *seqType)
Definition: MultiSeq.h:29
cnoid::Deque2D::resize
void resize(int newRowSize, int newColSize)
Definition: Deque2D.h:525
cnoid::MultiSeq::getOffsetTime
virtual double getOffsetTime() const override
Definition: MultiSeq.h:167
cnoid::MultiSeq::copySeqProperties
void copySeqProperties(const MultiSeqType &source)
Definition: MultiSeq.h:75
cnoid::MultiSeq::offsetTimeFrame
int offsetTimeFrame() const
Definition: MultiSeq.h:175
cnoid::MultiSeq::part
Part part(int index)
Definition: MultiSeq.h:187
cnoid::Deque2D< Vector3, Eigen::aligned_allocator< Vector3 > >::value_type
Vector3 value_type
Definition: Deque2D.h:20
cnoid::MultiSeq::timeLength
double timeLength() const
Definition: MultiSeq.h:151
cnoid::MultiSeq::Part
Container::Column Part
Definition: MultiSeq.h:26
cnoid::MultiSeq::Frame
Container::Row Frame
Definition: MultiSeq.h:25
cnoid::AbstractSeq::operator=
virtual AbstractSeq & operator=(const AbstractSeq &rhs)
Definition: AbstractSeq.cpp:39
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::MultiSeq::setOffsetTime
virtual void setOffsetTime(double time) override
Definition: MultiSeq.h:171
cnoid::MultiSeq::frameRate_
double frameRate_
Definition: MultiSeq.h:217
cnoid::MultiSeq::cloneSeq
virtual std::shared_ptr< AbstractSeq > cloneSeq() const override
Definition: MultiSeq.h:71
cnoid::Deque2D::row
Row row(int rowIndex)
Definition: Deque2D.h:576
cnoid::MultiSeq::setFrameRate
virtual void setFrameRate(double frameRate) override
Definition: MultiSeq.h:114
cnoid::Deque2D
Definition: Deque2D.h:15
cnoid::MultiSeq::setNumFrames
virtual void setNumFrames(int newNumFrames, bool fillNewElements=false) override
Definition: MultiSeq.h:135
cnoid::MultiSeq::offsetTime
double offsetTime() const
Definition: MultiSeq.h:163
cnoid::MultiSeq::timeStep
const double timeStep() const
Definition: MultiSeq.h:118
cnoid::MultiSeq::appendFrame
Frame appendFrame()
Definition: MultiSeq.h:203
cnoid::AbstractSeq
Definition: AbstractSeq.h:21
cnoid::MultiSeq::Element
value_type Element
Definition: MultiSeq.h:27
cnoid::MultiSeq::frame
Frame frame(int index)
Definition: MultiSeq.h:191
cnoid::Deque2D::begin
iterator begin()
Definition: Deque2D.h:138
cnoid::MultiSeq::part
const Part part(int index) const
Definition: MultiSeq.h:183
cnoid::Deque2D::colSize
int colSize() const
Definition: Deque2D.h:544
cnoid::MultiSeq::setDimension
virtual void setDimension(int newNumFrames, int newNumParts, bool fillNewElements=false) override
Definition: MultiSeq.h:81
cnoid::MultiSeq::defaultValue
virtual value_type defaultValue() const
Definition: MultiSeq.h:220
cnoid::MultiSeq::popFrontFrame
void popFrontFrame()
Definition: MultiSeq.h:199
cnoid::MultiSeq::getFrameRate
virtual double getFrameRate() const override
Definition: MultiSeq.h:106
cnoid::MultiSeq::frame
const Frame frame(int index) const
Definition: MultiSeq.h:195
cnoid::Deque2D::rowSize
int rowSize() const
Definition: Deque2D.h:533