OSDN Git Service

173b1ab0aca20e0cfbce78d56facc2caa8e5c4d2
[mikutoga/Pmd2XML.git] / src / main / java / jp / sourceforge / mikutoga / pmd / model / binio / PmdExporterExt3.java
1 /*
2  * model exporter for pmd-file(Ext3)
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.pmd.model.binio;
9
10 import jp.sourceforge.mikutoga.pmd.IllegalPmdDataException;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.util.List;
14 import jp.sourceforge.mikutoga.binio.IllegalTextExportException;
15 import jp.sourceforge.mikutoga.pmd.Deg3d;
16 import jp.sourceforge.mikutoga.pmd.Rad3d;
17 import jp.sourceforge.mikutoga.pmd.RigidShapeType;
18 import jp.sourceforge.mikutoga.pmd.TripletRange;
19 import jp.sourceforge.mikutoga.pmd.model.BoneInfo;
20 import jp.sourceforge.mikutoga.pmd.model.DynamicsInfo;
21 import jp.sourceforge.mikutoga.pmd.model.JointInfo;
22 import jp.sourceforge.mikutoga.pmd.model.PmdModel;
23 import jp.sourceforge.mikutoga.pmd.model.RigidGroup;
24 import jp.sourceforge.mikutoga.pmd.model.RigidInfo;
25 import jp.sourceforge.mikutoga.pmd.model.RigidShape;
26 import jp.sourceforge.mikutoga.pmd.parser.PmdLimits;
27
28 /**
29  * PMDファイルのエクスポーター(拡張3:物理演算対応)。
30  * <p>
31  * 物理演算対応のPMDファイルフォーマットを
32  * 使いたい場合はこのエクスポーターを用いて出力せよ。
33  */
34 public class PmdExporterExt3 extends PmdExporterExt2{
35
36     private static final short MASK_FULLCOLLISION = (short) 0xffff;
37
38     /**
39      * コンストラクタ。
40      * @param stream 出力ストリーム
41      * @throws NullPointerException 引数がnull
42      */
43     public PmdExporterExt3(OutputStream stream)
44             throws NullPointerException{
45         super(stream);
46         return;
47     }
48
49     /**
50      * {@inheritDoc}
51      * @param model {@inheritDoc}
52      * @throws IOException {@inheritDoc}
53      * @throws IllegalPmdDataException {@inheritDoc}
54      */
55     @Override
56     public void dumpPmdModel(PmdModel model)
57             throws IOException, IllegalPmdDataException{
58         super.dumpPmdModel(model);
59
60         try{
61             dumpRigidList(model);
62             dumpJointList(model);
63         }catch(IllegalTextExportException e){
64             throw new IllegalPmdDataException(e);
65         }
66
67         return;
68     }
69
70     /**
71      * 剛体リストを出力する。
72      * @param model モデルデータ
73      * @throws IOException 出力エラー
74      * @throws IllegalPmdTextException 長すぎる剛体名
75      */
76     private void dumpRigidList(PmdModel model)
77             throws IOException, IllegalTextExportException{
78         List<RigidInfo> rigidList = model.getRigidList();
79         int rigidNum = rigidList.size();
80         dumpLeInt(rigidNum);
81
82         for(RigidInfo rigid : rigidList){
83             dumpRigid(rigid);
84         }
85
86         flush();
87
88         return;
89     }
90
91     /**
92      * 個別の剛体情報を出力する。
93      * @param rigid 剛体
94      * @throws IOException 出力エラー
95      * @throws IllegalPmdTextException 長すぎる剛体名
96      */
97     private void dumpRigid(RigidInfo rigid)
98             throws IOException, IllegalTextExportException{
99         String rigidName = rigid.getRigidName().getPrimaryText();
100         dumpText(rigidName, PmdLimits.MAXBYTES_RIGIDNAME);
101
102         BoneInfo linkedBone = rigid.getLinkedBone();
103         if(linkedBone == null){
104             dumpLeShort(-1);
105         }else{
106             dumpLeShort(linkedBone.getSerialNumber());
107         }
108
109         RigidGroup group = rigid.getRigidGroup();
110         dumpByte(group.getSerialNumber());
111
112         short mask = MASK_FULLCOLLISION;
113         for(RigidGroup throughGroup : rigid.getThroughGroupColl()){
114             int serialId = throughGroup.getSerialNumber();
115             mask &= ~(0x0001 << serialId);
116         }
117         dumpLeShort(mask);
118
119         dumpRigidShape(rigid.getRigidShape());
120
121         dumpPos3D(rigid.getPosition());
122         dumpRad3d(rigid.getRotation());
123
124         dumpDynamics(rigid.getDynamicsInfo());
125
126         dumpByte(rigid.getBehaviorType().encode());
127
128         return;
129     }
130
131     /**
132      * 剛体形状を出力する。
133      * @param shape 剛体形状
134      * @throws IOException 出力エラー
135      */
136     private void dumpRigidShape(RigidShape shape)
137             throws IOException{
138         RigidShapeType type = shape.getShapeType();
139         dumpByte(type.encode());
140
141         float width = shape.getWidth();
142         float height = shape.getHeight();
143         float depth = shape.getDepth();
144
145         dumpLeFloat(width);
146         dumpLeFloat(height);
147         dumpLeFloat(depth);
148
149         return;
150     }
151
152     /**
153      * 力学設定を出力する。
154      * @param dynamics 力学設定
155      * @throws IOException 出力エラー
156      */
157     private void dumpDynamics(DynamicsInfo dynamics)
158             throws IOException{
159         float mass        = dynamics.getMass();
160         float dampPos     = dynamics.getDampingPosition();
161         float dampRot     = dynamics.getDampingRotation();
162         float restitution = dynamics.getRestitution();
163         float friction    = dynamics.getFriction();
164
165         dumpLeFloat(mass);
166         dumpLeFloat(dampPos);
167         dumpLeFloat(dampRot);
168         dumpLeFloat(restitution);
169         dumpLeFloat(friction);
170
171         return;
172     }
173
174     /**
175      * ジョイントリストを出力する。
176      * @param model モデルデータ
177      * @throws IOException 出力エラー
178      * @throws IllegalPmdTextException 長すぎるジョイント名
179      */
180     private void dumpJointList(PmdModel model)
181             throws IOException, IllegalTextExportException{
182         List<JointInfo> jointList = model.getJointList();
183         int jointNum = jointList.size();
184         dumpLeInt(jointNum);
185
186         for(JointInfo joint : jointList){
187             dumpJoint(joint);
188         }
189
190         flush();
191
192         return;
193     }
194
195     /**
196      * 個別のジョイント情報を出力する。
197      * @param joint ジョイント
198      * @throws IOException 出力エラー
199      * @throws IllegalPmdTextException 長すぎるジョイント名
200      */
201     private void dumpJoint(JointInfo joint)
202             throws IOException, IllegalTextExportException{
203         String jointName = joint.getJointName().getPrimaryText();
204         dumpText(jointName, PmdLimits.MAXBYTES_JOINTNAME);
205
206         RigidInfo rigidA = joint.getRigidA();
207         RigidInfo rigidB = joint.getRigidB();
208
209         dumpLeInt(rigidA.getSerialNumber());
210         dumpLeInt(rigidB.getSerialNumber());
211
212         dumpPos3D(joint.getPosition());
213         dumpRad3d(joint.getRotation());
214
215         dumpTripletRange(joint.getPositionRange());
216         dumpTripletRange(joint.getRotationRange());
217
218         dumpPos3D(joint.getElasticPosition());
219         dumpDeg3d(joint.getElasticRotation());
220
221         return;
222     }
223
224     /**
225      * 3次元範囲制約を出力する。
226      * @param range 3次元範囲制約
227      * @throws IOException 出力エラー
228      */
229     protected void dumpTripletRange(TripletRange range) throws IOException{
230         float xFrom = range.getXFrom();
231         float yFrom = range.getYFrom();
232         float zFrom = range.getZFrom();
233
234         dumpLeFloat(xFrom);
235         dumpLeFloat(yFrom);
236         dumpLeFloat(zFrom);
237
238         float xTo = range.getXTo();
239         float yTo = range.getYTo();
240         float zTo = range.getZTo();
241
242         dumpLeFloat(xTo);
243         dumpLeFloat(yTo);
244         dumpLeFloat(zTo);
245
246         return;
247     }
248
249     /**
250      * ラジアンによる3次元姿勢情報を出力する。
251      * @param rad 3次元姿勢情報
252      * @throws IOException 出力エラー
253      */
254     protected void dumpRad3d(Rad3d rad) throws IOException{
255         float xVal = rad.getXRad();
256         float yVal = rad.getYRad();
257         float zVal = rad.getZRad();
258
259         dumpLeFloat(xVal);
260         dumpLeFloat(yVal);
261         dumpLeFloat(zVal);
262
263         return;
264     }
265
266     /**
267      * 度数法による3次元姿勢情報を出力する。
268      * @param deg 3次元姿勢情報
269      * @throws IOException 出力エラー
270      */
271     protected void dumpDeg3d(Deg3d deg) throws IOException{
272         float xVal = deg.getXDeg();
273         float yVal = deg.getYDeg();
274         float zVal = deg.getZDeg();
275
276         dumpLeFloat(xVal);
277         dumpLeFloat(yVal);
278         dumpLeFloat(zVal);
279
280         return;
281     }
282
283 }