OSDN Git Service

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