OSDN Git Service

02821f5e9ad509aed95d3e82e3528d07caef3f2c
[mikutoga/Pmd2XML.git] / src / main / java / jp / sourceforge / mikutoga / pmd / model / binio / ShapeBuilder.java
1 /*
2  * building shape information
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 java.util.Iterator;
11 import java.util.List;
12 import java.util.RandomAccess;
13 import jp.sourceforge.mikutoga.pmd.model.ListUtil;
14 import jp.sourceforge.mikutoga.math.MkPos2D;
15 import jp.sourceforge.mikutoga.math.MkPos3D;
16 import jp.sourceforge.mikutoga.math.MkVec3D;
17 import jp.sourceforge.mikutoga.parser.ParseStage;
18 import jp.sourceforge.mikutoga.pmd.model.BoneInfo;
19 import jp.sourceforge.mikutoga.pmd.model.PmdModel;
20 import jp.sourceforge.mikutoga.pmd.model.Surface;
21 import jp.sourceforge.mikutoga.pmd.model.Vertex;
22 import jp.sourceforge.mikutoga.pmd.parser.PmdShapeHandler;
23
24 /**
25  * モデル形状に関する通知をパーサから受け取る。
26  */
27 class ShapeBuilder implements PmdShapeHandler {
28
29     private final List<Vertex> vertexList;
30     private final List<BoneInfo> boneList;
31     private final List<Surface> surfaceList;
32
33     private Iterator<Vertex> vertexIt;
34     private Vertex currentVertex = null;
35
36     private Iterator<Surface> surfaceIt;
37     private Surface currentSurface = null;
38
39     /**
40      * コンストラクタ。
41      * @param model モデル
42      */
43     ShapeBuilder(PmdModel model){
44         super();
45
46         this.vertexList  = model.getVertexList();
47         this.boneList    = model.getBoneList();
48         this.surfaceList = model.getSurfaceList();
49
50         assert this.vertexList instanceof RandomAccess;
51         assert this.surfaceList instanceof RandomAccess;
52         assert this.boneList instanceof RandomAccess;
53
54         return;
55     }
56
57     /**
58      * ボーンリスト上にボーンを用意する。
59      * すでに指定位置にボーンがあればなにもしない。
60      * @param id 0から始まるボーン番号
61      * @return 用意されたボーン
62      */
63     private BoneInfo prepareBone(int id){
64         ListUtil.extendList(this.boneList, id + 1);
65         BoneInfo bone = this.boneList.get(id);
66         if(bone == null){
67             bone = new BoneInfo();
68             bone.setSerialNumber(id);
69             this.boneList.set(id, bone);
70         }
71         return bone;
72     }
73
74     /**
75      * {@inheritDoc}
76      * @param stage {@inheritDoc}
77      * @param loops {@inheritDoc}
78      */
79     @Override
80     public void loopStart(ParseStage stage, int loops){
81         if(stage == PmdShapeHandler.VERTEX_LIST){
82             ListUtil.prepareDefConsList(this.vertexList, Vertex.class, loops);
83             ListUtil.assignIndexedSerial(this.vertexList);
84
85             this.vertexIt = this.vertexList.iterator();
86             if(this.vertexIt.hasNext()){
87                 this.currentVertex = this.vertexIt.next();
88             }
89         }else if(stage == PmdShapeHandler.SURFACE_LIST){
90             ListUtil.prepareDefConsList(this.surfaceList,
91                                         Surface.class, loops );
92             ListUtil.assignIndexedSerial(this.surfaceList);
93
94             this.surfaceIt = this.surfaceList.iterator();
95             if(this.surfaceIt.hasNext()){
96                 this.currentSurface = this.surfaceIt.next();
97             }
98         }else{
99             assert false;
100             throw new AssertionError();
101         }
102         return;
103     }
104
105     /**
106      * {@inheritDoc}
107      * @param stage {@inheritDoc}
108      */
109     @Override
110     public void loopNext(ParseStage stage){
111         if(stage == PmdShapeHandler.VERTEX_LIST){
112             if(this.vertexIt.hasNext()){
113                 this.currentVertex = this.vertexIt.next();
114             }
115         }else if(stage == PmdShapeHandler.SURFACE_LIST){
116             if(this.surfaceIt.hasNext()){
117                 this.currentSurface = this.surfaceIt.next();
118             }
119         }else{
120             assert false;
121             throw new AssertionError();
122         }
123         return;
124     }
125
126     /**
127      * {@inheritDoc}
128      * @param stage {@inheritDoc}
129      */
130     @Override
131     public void loopEnd(ParseStage stage){
132         return;
133     }
134
135     /**
136      * {@inheritDoc}
137      * @param xPos {@inheritDoc}
138      * @param yPos {@inheritDoc}
139      * @param zPos {@inheritDoc}
140      */
141     @Override
142     public void pmdVertexPosition(float xPos, float yPos, float zPos){
143         MkPos3D position = this.currentVertex.getPosition();
144         position.setXpos(xPos);
145         position.setYpos(yPos);
146         position.setZpos(zPos);
147         return;
148     }
149
150     /**
151      * {@inheritDoc}
152      * @param xVec {@inheritDoc}
153      * @param yVec {@inheritDoc}
154      * @param zVec {@inheritDoc}
155      */
156     @Override
157     public void pmdVertexNormal(float xVec, float yVec, float zVec){
158         MkVec3D normal = this.currentVertex.getNormal();
159         normal.setXVal(xVec);
160         normal.setYVal(yVec);
161         normal.setZVal(zVec);
162         return;
163     }
164
165     /**
166      * {@inheritDoc}
167      * @param uVal {@inheritDoc}
168      * @param vVal {@inheritDoc}
169      */
170     @Override
171     public void pmdVertexUV(float uVal, float vVal){
172         MkPos2D uv = this.currentVertex.getUVPosition();
173         uv.setXpos(uVal);
174         uv.setYpos(vVal);
175         return;
176     }
177
178     /**
179      * {@inheritDoc}
180      * @param boneId1 {@inheritDoc}
181      * @param boneId2 {@inheritDoc}
182      * @param weightForB1 {@inheritDoc}
183      */
184     @Override
185     public void pmdVertexWeight(int boneId1, int boneId2, int weightForB1){
186         BoneInfo bone1 = prepareBone(boneId1);
187         BoneInfo bone2 = prepareBone(boneId2);
188
189         this.currentVertex.setBonePair(bone1, bone2);
190         this.currentVertex.setWeightA(weightForB1);
191
192         return;
193     }
194
195     /**
196      * {@inheritDoc}
197      * @param hideEdge {@inheritDoc}
198      */
199     @Override
200     public void pmdVertexEdge(boolean hideEdge){
201         this.currentVertex.setEdgeAppearance( ! hideEdge );
202         return;
203     }
204
205     /**
206      * {@inheritDoc}
207      * @param vertexId1 {@inheritDoc}
208      * @param vertexId2 {@inheritDoc}
209      * @param vertexId3 {@inheritDoc}
210      */
211     @Override
212     public void pmdSurfaceTriangle(int vertexId1,
213                                       int vertexId2,
214                                       int vertexId3 ){
215         Vertex vtx1 = this.vertexList.get(vertexId1);
216         Vertex vtx2 = this.vertexList.get(vertexId2);
217         Vertex vtx3 = this.vertexList.get(vertexId3);
218
219         this.currentSurface.setTriangle(vtx1, vtx2, vtx3);
220
221         return;
222     }
223
224 }