OSDN Git Service

b037d88bcb07837deaf1f32f66fa46e5113c1eb4
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd / model / Vertex.java
1 /*
2  * vertex information
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd.model;
9
10 import jp.sfjp.mikutoga.math.MkPos2D;
11 import jp.sfjp.mikutoga.math.MkPos3D;
12 import jp.sfjp.mikutoga.math.MkVec3D;
13
14 /**
15  * 頂点情報。
16  */
17 public class Vertex implements SerialNumbered {
18
19     private static final int MIN_WEIGHT = 0;
20     private static final int MAX_WEIGHT = 100;
21     private static final int BALANCED   = 50;
22
23
24     private final MkPos3D position = new MkPos3D();
25     private final MkVec3D normal = new MkVec3D();
26
27     private final MkPos2D uvPosition = new MkPos2D();
28
29     private BoneInfo boneA = null;
30     private BoneInfo boneB = null;
31
32     private int boneWeight = BALANCED;
33
34     private boolean edgeAppearance = true;
35
36     private int vertexSerialNo = -1;
37
38
39     /**
40      * コンストラクタ。
41      */
42     public Vertex(){
43         super();
44         return;
45     }
46
47
48     /**
49      * 頂点位置座標を返す。
50      * @return 頂点の位置座標
51      */
52     public MkPos3D getPosition(){
53         return this.position;
54     }
55
56     /**
57      * 法線ベクトルを返す。
58      * @return 法線ベクトル
59      */
60     public MkVec3D getNormal(){
61         return this.normal;
62     }
63
64     /**
65      * UVマップ座標を返す。
66      * @return UVマップ情報
67      */
68     public MkPos2D getUVPosition(){
69         return this.uvPosition;
70     }
71
72     /**
73      * 頂点の属するボーンを設定する。
74      * @param boneAArg ボーンA
75      * @param boneBArg ボーンB
76      * @throws NullPointerException 引数がnull
77      */
78     public void setBonePair(BoneInfo boneAArg, BoneInfo boneBArg)
79             throws NullPointerException{
80         if(boneAArg == null || boneBArg == null)
81             throw new NullPointerException();
82
83         this.boneA = boneAArg;
84         this.boneB = boneBArg;
85
86         return;
87     }
88
89     /**
90      * ボーンAを返す。
91      * @return ボーンA
92      */
93     public BoneInfo getBoneA(){
94         return this.boneA;
95     }
96
97     /**
98      * ボーンBを返す。
99      * @return ボーンB
100      */
101     public BoneInfo getBoneB(){
102         return this.boneB;
103     }
104
105     /**
106      * ボーンAのウェイト値を設定する。
107      * @param weight ウェイト値。0(影響小)-100(影響大)
108      * @throws IllegalArgumentException ウェイト値が範囲外
109      */
110     public void setWeightA(int weight) throws IllegalArgumentException{
111         if(   weight < MIN_WEIGHT
112            || MAX_WEIGHT < weight ){
113             throw new IllegalArgumentException();
114         }
115         this.boneWeight = weight;
116         return;
117     }
118
119     /**
120      * ボーンBのウェイト値を設定する。
121      * @param weight ウェイト値。0(影響小)-100(影響大)
122      * @throws IllegalArgumentException ウェイト値が範囲外
123      */
124     public void setWeightB(int weight) throws IllegalArgumentException{
125         setWeightA(MAX_WEIGHT - weight);
126         return;
127     }
128
129     /**
130      * ボーンAのウェイト値を返す。
131      * @return ウェイト値
132      */
133     public int getWeightA(){
134         return this.boneWeight;
135     }
136
137     /**
138      * ボーンBのウェイト値を返す。
139      * @return ウェイト値
140      */
141     public int getWeightB(){
142         int result = MAX_WEIGHT - this.boneWeight;
143         return result;
144     }
145
146     /**
147      * ボーンAのウェイト率を返す。
148      * @return ウェイト率。0.0(影響小)-1.0(影響大)
149      */
150     public float getWeightRatioA(){
151         return ((float)this.boneWeight) / (float)MAX_WEIGHT;
152     }
153
154     /**
155      * ボーンBのウェイト率を返す。
156      * @return ウェイト率。0.0(影響小)-1.0(影響大)
157      */
158     public float getWeightRatioB(){
159         return ((float)MAX_WEIGHT - (float)this.boneWeight)
160                 / (float)MAX_WEIGHT;
161     }
162
163     /**
164      * エッジを表示するか設定する。
165      * マテリアル材質単位の設定より優先度は高い。
166      * @param show 表示するならtrue
167      */
168     public void setEdgeAppearance(boolean show){
169         this.edgeAppearance = show;
170         return;
171     }
172
173     /**
174      * エッジを表示するか判定する。
175      * マテリアル材質単位の設定より優先度は高い。
176      * @return 表示するならtrue
177      */
178     public boolean getEdgeAppearance(){
179         return this.edgeAppearance;
180     }
181
182     /**
183      * {@inheritDoc}
184      * @param num {@inheritDoc}
185      */
186     @Override
187     public void setSerialNumber(int num){
188         this.vertexSerialNo = num;
189         return;
190     }
191
192     /**
193      * {@inheritDoc}
194      * @return {@inheritDoc}
195      */
196     @Override
197     public int getSerialNumber(){
198         return this.vertexSerialNo;
199     }
200
201     /**
202      * {@inheritDoc}
203      * @return {@inheritDoc}
204      */
205     @Override
206     public String toString(){
207         StringBuilder result = new StringBuilder();
208
209         result.append("Vertex(").append(this.vertexSerialNo).append(") ");
210         result.append(this.position).append(' ');
211         result.append(this.normal).append(' ');
212         result.append("UV").append(this.uvPosition).append(' ');
213
214         result.append("[")
215               .append(this.boneA.getBoneName())
216               .append("<>")
217               .append(this.boneB.getBoneName())
218               .append("] ");
219
220         result.append("weight=").append(this.boneWeight).append(' ');
221
222         if(this.edgeAppearance) result.append("showEdge");
223         else                    result.append("hideEdge");
224
225         return result.toString();
226     }
227
228 }