X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=src%2Fvmd.cpp;h=e69c79a1ccaf37898849322f1521aedf423fd56a;hb=HEAD;hp=f69b55c528d5e7879314c7ac756b64da5ddb53e5;hpb=d55fe8aa17829d33f535e1daef808e74a0e61d0f;p=meshio%2Fmeshio.git diff --git a/src/vmd.cpp b/src/vmd.cpp old mode 100755 new mode 100644 index f69b55c..e69c79a --- a/src/vmd.cpp +++ b/src/vmd.cpp @@ -4,211 +4,189 @@ #include namespace meshio { -namespace vmd { - -template -struct SortKeyFrameList -{ - typedef T MAP; - void operator()(typename MAP::value_type &channel) - { - channel.second->sort(); - } -}; - -template - void - readBoneMap(READER &reader, IO &io) - { - std::wstring name= - text::trim(text::cp932_to_unicode(reader.getString(15, true))); - unsigned int frame=reader.getUint(); - IO::BoneMap::iterator found=io.boneMap.find(name); - if(found==io.boneMap.end()){ - // not found - found=io.boneMap.insert( - std::make_pair(name, - new KeyFrameList >())).first; - io.boneKeys.push_back(name); - } - BoneKey &key=found->second->push(frame).key; - - reader.get(key.pos); - reader.get(key.q); - reader.get(key.interpolationX); - reader.get(key.interpolationY); - reader.get(key.interpolationZ); - reader.get(key.interpolationRot); - } - -template - void - readMorphMap(READER &reader, IO &io) - { - std::wstring name= - text::trim(text::cp932_to_unicode(reader.getString(15, true))); - unsigned int frame=reader.getUint(); - IO::MorphMap::iterator found=io.morphMap.find(name); - if(found==io.morphMap.end()){ - // not found - found=io.morphMap.insert( - std::make_pair(name, - new KeyFrameList >())).first; - io.morphKeys.push_back(name); - } - MorphKey &key=found->second->push(frame).key; - - reader.get(key.weight); - } - -class Implementation -{ - IO &io_; - binary::IReader &reader_; - -public: - Implementation(IO &io, binary::IReader &reader) - : io_(io), reader_(reader) - {} - - bool parse() - { - // check header - std::string line=reader_.getString(30, true); - if(line=="Vocaloid Motion Data file"){ - io_.version="1"; - text::copyStringAndFillZero(io_.name, reader_.getString(10)); - return parseBody(); - } - else if(line=="Vocaloid Motion Data 0002"){ - io_.version="2"; - text::copyStringAndFillZero(io_.name, reader_.getString(20)); - return parseBody(); - } - else{ - //std::cout << "unknown header:" << line << std::endl; - return false; - } - } - - -private: - bool parseBody() - { - if(!parseFrame()){ - return false; - } - if(!parseMorph()){ - return false; - } - if(!parseCamera()){ - return false; - } - if(!parseLight()){ - return false; - } - // sort - std::for_each(io_.boneMap.begin(), io_.boneMap.end(), - SortKeyFrameList()); - std::for_each(io_.morphMap.begin(), io_.morphMap.end(), - SortKeyFrameList()); - return true; - } - - bool parseMorph() - { - unsigned int count=reader_.getUint(); - for(unsigned int i=0; isecond; - } - boneMap.clear(); - - for(MorphMap::iterator it=morphMap.begin(); it!=morphMap.end(); ++it){ - delete it->second; - } - morphMap.clear(); - -} - -bool IO::read(binary::IReader &reader) -{ - return Implementation(*this, reader).parse(); -} - -#ifdef _WIN32 -bool IO::read(const wchar_t *path) -{ - std::vector all; - binary::readAll(path, all); - if(all.empty()){ - return false; - } - binary::MemoryReader reader(&all[0], all.size()); - return read(reader); -} -#endif - -bool IO::read(const char *path) -{ - std::vector all; - binary::readAll(path, all); - if(all.empty()){ - return false; - } - binary::MemoryReader reader(&all[0], all.size()); - return read(reader); -} - -bool IO::write(std::ostream &os) -{ - return false; -} - -BoneKeyFrameList* IO::getBoneKeyFrameList(const std::wstring &name) -{ - return boneMap[name]; -} - -MorphKeyFrameList* IO::getMorphKeyFrameList(const std::wstring &name) -{ - return morphMap[name]; -} - -} // namespace vmd + namespace vmd { + + template + struct SortKeyFrameList + { + typedef T MAP; + void operator()(typename MAP::value_type &channel) + { + channel.second->sort(); + } + }; + + template + void + readBoneMap(READER &reader, IO &io) + { + std::wstring name= + trim(cp932_to_unicode(reader.getString(15, true))); + unsigned int frame=reader.getUint(); + IO::BoneMap::iterator found=io.boneMap.find(name); + if(found==io.boneMap.end()){ + // not found + found=io.boneMap.insert( + std::make_pair(name, + new KeyFrameList >())).first; + io.boneKeys.push_back(name); + } + BoneKey &key=found->second->push(frame).key; + + reader.get(key.pos); + reader.get(key.q); + reader.get(key.interpolationX); + reader.get(key.interpolationY); + reader.get(key.interpolationZ); + reader.get(key.interpolationRot); + } + + template + void + readMorphMap(READER &reader, IO &io) + { + std::wstring name= + trim(cp932_to_unicode(reader.getString(15, true))); + unsigned int frame=reader.getUint(); + IO::MorphMap::iterator found=io.morphMap.find(name); + if(found==io.morphMap.end()){ + // not found + found=io.morphMap.insert( + std::make_pair(name, + new KeyFrameList >())).first; + io.morphKeys.push_back(name); + } + MorphKey &key=found->second->push(frame).key; + + reader.get(key.weight); + } + + class Implementation + { + IO &io_; + binary::IReader &reader_; + + public: + Implementation(IO &io, binary::IReader &reader) + : io_(io), reader_(reader) + {} + + bool parse() + { + // check header + std::string line=reader_.getString(30, true); + if(line=="Vocaloid Motion Data file"){ + io_.version="1"; + reader_.get(io_.name); + return parseBody(); + } + else if(line=="Vocaloid Motion Data 0002"){ + io_.version="2"; + reader_.get(io_.name); + return parseBody(); + } + else{ + //std::cout << "unknown header:" << line << std::endl; + return false; + } + } + + + private: + bool parseBody() + { + if(!parseFrame()){ + return false; + } + if(!parseMorph()){ + return false; + } + if(!parseCamera()){ + return false; + } + if(!parseLight()){ + return false; + } + // sort + std::for_each(io_.boneMap.begin(), io_.boneMap.end(), + SortKeyFrameList()); + std::for_each(io_.morphMap.begin(), io_.morphMap.end(), + SortKeyFrameList()); + return true; + } + + bool parseMorph() + { + unsigned int count=reader_.getUint(); + for(unsigned int i=0; isecond; + } + boneMap.clear(); + + for(MorphMap::iterator it=morphMap.begin(); it!=morphMap.end(); ++it){ + delete it->second; + } + morphMap.clear(); + + } + + bool IO::read(binary::IReader &reader) + { + return Implementation(*this, reader).parse(); + } + + bool IO::read(const char *path) + { + std::vector all; + binary::readAll(path, all); + if(all.empty()){ + return false; + } + binary::MemoryReader reader(&all[0], all.size()); + return read(reader); + } + + bool IO::write(std::ostream &os) + { + // not implemented + return false; + } + + } // namespace vmd } // namespace meshio