6 #ifndef CNOID_UTIL_GENERAL_SEQ_READER_H
7 #define CNOID_UTIL_GENERAL_SEQ_READER_H
12 #include <type_traits>
19 static std::string mismatched_seq_type_message(
const std::string& type,
AbstractSeq*
seq);
20 static std::string has_frame_time_unsupported_message(
double formatVersion);
21 static std::string unkown_frame_rate_for_time_frame_seq_message();
22 static std::string frames_key_not_found_message();
23 static std::string no_frame_data_message();
24 static std::string invalid_num_parts_messaage();
25 static std::string invalid_frame_size_message();
30 double formatVersion_;
34 std::function<bool(
GeneralSeqReader& reader,
const std::string& type)> customSeqTypeChecker;
43 hasFrameTime_ =
false;
47 std::ostream&
os() {
return os_; }
55 return (type == seq_->
seqType());
59 customSeqTypeChecker = func;
66 if(!framesNode->isValid()){
69 const Listing& frames = *framesNode->toListing();
71 frames.throwException(no_frame_data_message());
82 formatVersion_ =
archive->
get(
"formatVersion", 1.0);
84 auto& typeNode = (*archive)[
"type"];
87 if(customSeqTypeChecker){
88 isTypeMatched = customSeqTypeChecker(*
this, type);
93 typeNode.throwException(mismatched_seq_type_message(type,
seq));
99 }
else if(formatVersion_ < 2.0 && archive->
read(
"purpose", content)){
103 hasFrameTime_ =
false;
104 auto node_hasFrameTime =
archive->
find(
"hasFrameTime");
105 if(node_hasFrameTime->isValid()){
106 if(formatVersion_ < 2.0){
107 node_hasFrameTime->
throwException(has_frame_time_unsupported_message(formatVersion_));
109 hasFrameTime_ = node_hasFrameTime->toBool();
120 os_ << unkown_frame_rate_for_time_frame_seq_message() << std::endl;
127 auto& node = (*archive)[
"numParts"];
128 numParts_ = node.toInt();
130 node.throwException(invalid_num_parts_messaage());
139 typename std::enable_if<
140 std::is_base_of<AbstractSeq, SeqType>::value &&
141 !std::is_base_of<AbstractMultiSeq, SeqType>::value, std::nullptr_t>::type =
nullptr
145 std::function<
void(
const Listing& srcNode,
int topIndex,
typename SeqType::value_type& seqValue)> readValue)
152 typename std::enable_if<
153 std::is_base_of<AbstractSeq, SeqType>::value &&
154 !std::is_base_of<AbstractMultiSeq, SeqType>::value, std::nullptr_t>::type =
nullptr
158 std::function<
void(
const Listing& srcNode,
int topIndex,
typename SeqType::value_type& seqValue)> readValue)
161 const int numFrames = frames.
size();
164 for(
int i=0; i < numFrames; ++i){
167 auto& seqValue = (*seq)[i];
168 readValue(srcValue, 0, seqValue);
170 double time = srcValue[0].
toDouble();
171 int frameIndex =
seq->frameOfTime(time);
172 if(frameIndex >=
seq->numFrames()){
175 auto& seqValue = (*seq)[frameIndex];
176 readValue(srcValue, 1, seqValue);
185 typename std::enable_if<std::is_base_of<AbstractMultiSeq, SeqType>::value, std::nullptr_t>::type =
nullptr
189 std::function<
void(
const ValueNode& srcNode,
typename SeqType::value_type& seqValue)> readValue)
196 typename std::enable_if<std::is_base_of<AbstractMultiSeq, SeqType>::value, std::nullptr_t>::type =
nullptr
200 std::function<
void(
const ValueNode& srcNode,
typename SeqType::value_type& seqValue)> readValue)
202 int frameDataSize = numParts_;
208 const int numFrames = frames.
size();
211 seq->setDimension(0, numParts_);
213 seq->setDimension(numFrames, numParts_);
216 for(
int i=0; i < numFrames; ++i){
218 if(srcValues.
size() != frameDataSize){
222 auto seqFrame =
seq->frame(i);
223 for(
int j=0; j < numParts_; ++j){
224 readValue(srcValues[j], seqFrame[j]);
227 double time = srcValues[0].
toDouble();
228 int frameIndex =
seq->frameOfTime(time);
229 if(frameIndex >=
seq->numFrames()){
232 auto seqFrame =
seq->frame(frameIndex);
233 for(
int j=0; j < numParts_; ++j){
234 readValue(srcValues[j+1], seqFrame[j]);