Choreonoid  1.5
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 <boost/make_shared.hpp>
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 boost::shared_ptr<SeqType> Ptr;
22 
23  Seq(const char* seqType, int nFrames = 0.0)
24  : AbstractSeq(seqType),
25  container(nFrames) {
27  }
28 
29  Seq(const SeqType& org)
30  : AbstractSeq(org),
31  container(org.container) {
32  frameRate_ = org.frameRate_;
33  }
34 
35  SeqType& operator=(const SeqType& rhs) {
36  if(this != &rhs){
38  container = rhs.container;
39  frameRate_ = rhs.frameRate_;
40  }
41  return *this;
42  }
43 
44  virtual AbstractSeq& operator=(const AbstractSeq& rhs) {
45  const SeqType* rhsSeq = dynamic_cast<const SeqType*>(&rhs);
46  if(rhsSeq){
47  return operator=(*rhsSeq);
48  } else {
49  return AbstractSeq::operator=(rhs);
50  }
51  }
52 
53  virtual AbstractSeqPtr cloneSeq() const {
54  return boost::make_shared<SeqType>(*this);
55  }
56 
57  virtual ~Seq() { }
58 
59  virtual double getFrameRate() const {
60  return frameRate_;
61  }
62 
63  inline double frameRate() const {
64  return frameRate_;
65  }
66 
67  virtual void setFrameRate(double frameRate) {
69  }
70 
71  virtual int getNumFrames() const {
72  return container.size();
73  }
74 
75  inline int numFrames() const {
76  return container.size();
77  }
78 
79  virtual void setNumFrames(int n, bool clearNewElements = false) {
80  if(clearNewElements){
81  container.resize(n, defaultValue());
82  } else {
83  container.resize(n);
84  }
85  }
86 
87  inline bool empty() const {
88  return container.empty();
89  }
90 
91  inline int frameOfTime(double time) const {
92  return (int)(time * frameRate_);
93  }
94 
95  inline double timeOfFrame(int frame) const {
96  return (frame / frameRate_);
97  }
98 
99  inline ElementType& operator[](int frameIndex) {
100  return container[frameIndex];
101  }
102 
103  inline const ElementType& operator[](int frameIndex) const {
104  return container[frameIndex];
105  }
106 
107  inline ElementType& at(int frameIndex) {
108  return container[frameIndex];
109  }
110 
111  inline const ElementType& at(int frameIndex) const {
112  return container[frameIndex];
113  }
114 
115  int clampFrameIndex(int frameIndex, bool& out_isValidRange){
116  if(frameIndex < 0){
117  frameIndex = 0;
118  out_isValidRange = false;
119  } else if(frameIndex >= numFrames()){
120  frameIndex = numFrames() - 1;
121  out_isValidRange = false;
122  } else {
123  out_isValidRange = true;
124  }
125  return frameIndex;
126  }
127 
128 protected:
129 
130  std::vector<ElementType> container;
131  double frameRate_;
132 
133  virtual ElementType defaultValue() const { return ElementType(); }
134 };
135 }
136 
137 #endif
virtual void setFrameRate(double frameRate)
Definition: Seq.h:67
const ElementType & at(int frameIndex) const
Definition: Seq.h:111
virtual AbstractSeqPtr cloneSeq() const
Definition: Seq.h:53
int frameOfTime(double time) const
Definition: Seq.h:91
virtual AbstractSeq & operator=(const AbstractSeq &rhs)
Definition: Seq.h:44
bool empty() const
Definition: Seq.h:87
ElementType & at(int frameIndex)
Definition: Seq.h:107
virtual ElementType defaultValue() const
Definition: Seq.h:133
static const double defaultFrameRate()
Definition: AbstractSeq.h:96
const std::string & seqType() const
Definition: AbstractSeq.h:38
Seq(const char *seqType, int nFrames=0.0)
Definition: Seq.h:23
virtual ~Seq()
Definition: Seq.h:57
boost::shared_ptr< AbstractSeq > AbstractSeqPtr
Definition: AbstractSeq.h:19
Definition: Seq.h:16
SeqType & operator=(const SeqType &rhs)
Definition: Seq.h:35
boost::shared_ptr< SeqType > Ptr
Definition: Seq.h:21
Definition: AbstractSeq.h:23
std::vector< ElementType > container
Definition: Seq.h:130
Seq(const SeqType &org)
Definition: Seq.h:29
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
int clampFrameIndex(int frameIndex, bool &out_isValidRange)
Definition: Seq.h:115
virtual double getFrameRate() const
Definition: Seq.h:59
ElementType & operator[](int frameIndex)
Definition: Seq.h:99
double frameRate() const
Definition: Seq.h:63
const ElementType & operator[](int frameIndex) const
Definition: Seq.h:103
virtual AbstractSeq & operator=(const AbstractSeq &rhs)
Definition: AbstractSeq.cpp:30
int numFrames() const
Definition: Seq.h:75
double frameRate_
Definition: Seq.h:131
double timeOfFrame(int frame) const
Definition: Seq.h:95
virtual int getNumFrames() const
Definition: Seq.h:71
virtual void setNumFrames(int n, bool clearNewElements=false)
Definition: Seq.h:79