OSDN Git Service

1d494baa616af91dcadf16a8724b63d687869c79
[meshio/meshio.git] / src / vmd.cpp
1 #include "vmd.h"
2 #include "text.h"
3 #include <algorithm>
4 #include <string>
5
6 namespace meshio {
7 namespace vmd {
8
9 template<typename T>
10 struct SortKeyFrameList
11 {
12         typedef T MAP;
13         void operator()(typename MAP::value_type &channel)
14         {
15                 channel.second.sort();
16         }
17 };
18
19 template<class READER>
20         void
21         read(READER &reader, IO::BoneMap &channels)
22         {
23                 std::string name=reader.getString(15);
24                 unsigned int frame=reader.getUint();
25                 IO::BoneMap::iterator found=channels.find(name);
26                 if(found==channels.end()){
27                         // not found
28                         found=channels.insert(
29                                                 std::make_pair(name, KeyFrameList<BoneKey>())).first;
30                 }
31                 BoneKey &key=found->second.push(frame).key;
32
33                 reader.get(key.pos);
34                 reader.get(key.q);
35                 reader.get(key.interpolationX);
36                 reader.get(key.interpolationY);
37                 reader.get(key.interpolationZ);
38                 reader.get(key.interpolationRot);
39         }
40
41 template<class READER>
42         void
43         read(READER &reader, IO::MorphMap &channels)
44         {
45                 std::string name=reader.getString(15);
46                 unsigned int frame=reader.getUint();
47                 IO::MorphMap::iterator found=channels.find(name);
48                 if(found==channels.end()){
49                         // not found
50                         found=channels.insert(
51                                         std::make_pair(name, KeyFrameList<MorphKey>())).first;
52                 }
53                 MorphKey &key=found->second.push(frame).key;
54
55                 reader.get(key.weight);
56         }
57
58 class Implementation
59 {
60         IO &io_;
61         binary::IReader &reader_;
62
63 public:
64         Implementation(IO &io, binary::IReader &reader)
65                 : io_(io), reader_(reader)
66                 {}
67
68         bool parse()
69         {
70                 // check header
71                 std::string line=reader_.getString(30, true);
72                 if(line=="Vocaloid Motion Data file"){
73                         io_.version="1";
74                         text::copyStringAndFillZero(io_.name, reader_.getString(10));
75                         return parseBody();
76                 }
77                 else if(line=="Vocaloid Motion Data 0002"){
78                         io_.version="2";
79                         text::copyStringAndFillZero(io_.name, reader_.getString(20));
80                         return parseBody();
81                 }
82                 else{
83                         //std::cout << "unknown header:" << line << std::endl;
84                         return false;
85                 }
86         }
87
88
89 private:
90         bool parseBody()
91         {
92                 if(!parseFrame()){
93                         return false;
94                 }
95                 if(!parseMorph()){
96                         return false;
97                 }
98                 if(!parseCamera()){
99                         return false;
100                 }
101                 if(!parseLight()){
102                         return false;
103                 }
104                 // sort
105                 std::for_each(io_.boneMap.begin(), io_.boneMap.end(), 
106                                 SortKeyFrameList<IO::BoneMap>());
107                 std::for_each(io_.morphMap.begin(), io_.morphMap.end(), 
108                                 SortKeyFrameList<IO::MorphMap>());
109                 return true;
110         }
111
112         bool parseMorph()
113         {
114                 unsigned int count=reader_.getUint();
115                 for(unsigned int i=0; i<count; ++i){
116                         read(reader_, io_.morphMap);
117                 }
118                 return true;
119         }
120
121         bool parseFrame()
122         {
123                 unsigned int count=reader_.getUint();
124                 for(unsigned int i=0; i<count; ++i){
125                         read(reader_, io_.boneMap);
126                 }
127                 return true;
128         }
129
130         bool parseCamera()
131         {
132                 return true;
133         }
134
135         bool parseLight()
136         {
137                 return true;
138         }
139 };
140
141
142 ///////////////////////////////////////////////////////////////////////////////
143 //! IO
144 ///////////////////////////////////////////////////////////////////////////////
145 IO::IO()
146 {
147 }
148
149 bool IO::read(binary::IReader &reader)
150 {
151         return Implementation(*this, reader).parse();
152 }
153
154 bool IO::write(std::ostream &os)
155 {
156         return false;
157 }
158
159 } // namespace vmd
160 } // namespace meshio