OSDN Git Service

[backup]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / squaredetect / NyARCoord2SquareVertexIndexes.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software: you can redistribute it and/or modify\r
14  * it under the terms of the GNU General Public License as published by\r
15  * the Free Software Foundation, either version 3 of the License, or\r
16  * (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
21  * GNU General Public License for more details.\r
22  *\r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core.squaredetect;\r
32 \r
33 /**\r
34  * 座標店集合(輪郭線)から、四角系の頂点候補点を計算します。\r
35  *\r
36  */\r
37 public class NyARCoord2SquareVertexIndexes\r
38 {\r
39         private static final double VERTEX_FACTOR = 1.0;// 線検出のファクタ     \r
40         private final NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter();\r
41         private final NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter();\r
42         public NyARCoord2SquareVertexIndexes()\r
43         {\r
44                 return;\r
45         }\r
46         /**\r
47          * 座標集合から、頂点候補になりそうな場所を4箇所探して、そのインデクス番号を返します。\r
48          * @param i_x_coord\r
49          * @param i_y_coord\r
50          * @param i_coord_num\r
51          * @param i_area\r
52          * @param o_vertex\r
53          * @return\r
54          */\r
55         public boolean getVertexIndexes(int[] i_x_coord, int[] i_y_coord, int i_coord_num, int i_area, int[] o_vertex)\r
56         {\r
57                 final NyARVertexCounter wv1 = this.__getSquareVertex_wv1;\r
58                 final NyARVertexCounter wv2 = this.__getSquareVertex_wv2;\r
59                 int vertex1_index=getFarPoint(i_x_coord,i_y_coord,i_coord_num,0);\r
60                 int prev_vertex_index=(vertex1_index+i_coord_num)%i_coord_num;\r
61                 int v1=getFarPoint(i_x_coord,i_y_coord,i_coord_num,vertex1_index);\r
62                 final double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR;\r
63 \r
64                 o_vertex[0] = vertex1_index;\r
65 \r
66                 if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, vertex1_index, v1, thresh)) {\r
67                         return false;\r
68                 }\r
69                 if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v1,prev_vertex_index, thresh)) {\r
70                         return false;\r
71                 }\r
72 \r
73                 int v2;\r
74                 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {\r
75                         o_vertex[1] = wv1.vertex[0];\r
76                         o_vertex[2] = v1;\r
77                         o_vertex[3] = wv2.vertex[0];\r
78                 } else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0) {\r
79                         //頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。\r
80                         if(v1>=vertex1_index){\r
81                                 v2 = (v1-vertex1_index)/2+vertex1_index;\r
82                         }else{\r
83                                 v2 = ((v1+i_coord_num-vertex1_index)/2+vertex1_index)%i_coord_num;\r
84                         }\r
85                         if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, vertex1_index, v2, thresh)) {\r
86                                 return false;\r
87                         }\r
88                         if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v2, v1, thresh)) {\r
89                                 return false;\r
90                         }\r
91                         if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {\r
92                                 o_vertex[1] = wv1.vertex[0];\r
93                                 o_vertex[2] = wv2.vertex[0];\r
94                                 o_vertex[3] = v1;\r
95                         } else {\r
96                                 return false;\r
97                         }\r
98                 } else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1) {\r
99                         //v2 = (v1+ end_of_coord)/2;\r
100                         if(v1<=prev_vertex_index){\r
101                                 v2 = (v1+prev_vertex_index)/2;\r
102                         }else{\r
103                                 v2 = ((v1+i_coord_num+prev_vertex_index)/2)%i_coord_num;\r
104                                 \r
105                         }\r
106                         if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, v1, v2, thresh)) {\r
107                                 return false;\r
108                         }\r
109                         if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v2, prev_vertex_index, thresh)) {\r
110                                 return false;\r
111                         }\r
112                         if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {\r
113                                 o_vertex[1] = v1;\r
114                                 o_vertex[2] = wv1.vertex[0];\r
115                                 o_vertex[3] = wv2.vertex[0];\r
116                         } else {\r
117                                 \r
118                                 return false;\r
119                         }\r
120                 } else {\r
121                         return false;\r
122                 }\r
123                 return true;\r
124         }\r
125         /**\r
126          * i_pointの輪郭座標から、最も遠方にある輪郭座標のインデクスを探します。\r
127          * @param i_xcoord\r
128          * @param i_ycoord\r
129          * @param i_coord_num\r
130          * @return\r
131          */\r
132         private static int getFarPoint(int[] i_coord_x, int[] i_coord_y,int i_coord_num,int i_point)\r
133         {\r
134                 //\r
135                 final int sx = i_coord_x[i_point];\r
136                 final int sy = i_coord_y[i_point];\r
137                 int d = 0;\r
138                 int w, x, y;\r
139                 int ret = 0;\r
140                 for (int i = i_point+1; i < i_coord_num; i++) {\r
141                         x = i_coord_x[i] - sx;\r
142                         y = i_coord_y[i] - sy;\r
143                         w = x * x + y * y;\r
144                         if (w > d) {\r
145                                 d = w;\r
146                                 ret = i;\r
147                         }\r
148                 }\r
149                 for (int i = 0; i < i_point; i++) {\r
150                         x = i_coord_x[i] - sx;\r
151                         y = i_coord_y[i] - sy;\r
152                         w = x * x + y * y;\r
153                         if (w > d) {\r
154                                 d = w;\r
155                                 ret = i;\r
156                         }\r
157                 }               \r
158                 return ret;\r
159         }       \r
160 }\r
161 \r
162 \r
163 \r
164 \r
165 /**\r
166  * get_vertex関数を切り離すためのクラス\r
167  * \r
168  */\r
169 final class NyARVertexCounter\r
170 {\r
171         public final int[] vertex = new int[10];// 6まで削れる\r
172 \r
173         public int number_of_vertex;\r
174 \r
175         private double thresh;\r
176 \r
177         private int[] x_coord;\r
178 \r
179         private int[] y_coord;\r
180 \r
181         public boolean getVertex(int[] i_x_coord, int[] i_y_coord,int i_coord_len,int st, int ed, double i_thresh)\r
182         {\r
183                 this.number_of_vertex = 0;\r
184                 this.thresh = i_thresh;\r
185                 this.x_coord = i_x_coord;\r
186                 this.y_coord = i_y_coord;\r
187                 return get_vertex(st, ed,i_coord_len);\r
188         }\r
189 \r
190         /**\r
191          * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数\r
192          * \r
193          * @param x_coord\r
194          * @param y_coord\r
195          * @param st\r
196          * @param ed\r
197          * @param thresh\r
198          * @return\r
199          */\r
200         private boolean get_vertex(int st, int ed,int i_coord_len)\r
201         {\r
202                 //メモ:座標値は65536を超えなければint32で扱って大丈夫なので変更。\r
203                 //dmaxは4乗なのでやるとしてもint64じゃないとマズイ\r
204                 int v1 = 0;\r
205                 final int[] lx_coord = this.x_coord;\r
206                 final int[] ly_coord = this.y_coord;\r
207                 final int a = ly_coord[ed] - ly_coord[st];\r
208                 final int b = lx_coord[st] - lx_coord[ed];\r
209                 final int c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st];\r
210                 double dmax = 0;\r
211                 if(st<ed){\r
212                         //stとedが1区間\r
213                         for (int i = st + 1; i < ed; i++) {\r
214                                 final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
215                                 if (d * d > dmax) {\r
216                                         dmax = d * d;\r
217                                         v1 = i;\r
218                                 }\r
219                         }\r
220                 }else{\r
221                         //stとedが2区間\r
222                         for (int i = st + 1; i < i_coord_len; i++) {\r
223                                 final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
224                                 if (d * d > dmax) {\r
225                                         dmax = d * d;\r
226                                         v1 = i;\r
227                                 }\r
228                         }\r
229                         for (int i = 0; i < ed; i++) {\r
230                                 final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
231                                 if (d * d > dmax) {\r
232                                         dmax = d * d;\r
233                                         v1 = i;\r
234                                 }\r
235                         }\r
236                 }\r
237 \r
238                 \r
239                 if (dmax / (double)(a * a + b * b) > thresh) {\r
240                         if (!get_vertex(st, v1,i_coord_len)) {\r
241                                 return false;\r
242                         }\r
243                         if (number_of_vertex > 5) {\r
244                                 return false;\r
245                         }\r
246                         vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1;\r
247                         number_of_vertex++;// (*vnum)++;\r
248 \r
249                         if (!get_vertex(v1, ed,i_coord_len)) {\r
250                                 return false;\r
251                         }\r
252                 }\r
253                 return true;\r
254         }\r
255 }