2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\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
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
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
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
31 package jp.nyatla.nyartoolkit.core.squaredetect;
\r
34 * 座標店集合(輪郭線)から、四角系の頂点候補点を計算します。
\r
37 public class NyARCoord2SquareVertexIndexes
\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
47 * 座標集合から、頂点候補になりそうな場所を4箇所探して、そのインデクス番号を返します。
\r
50 * @param i_coord_num
\r
55 public boolean getVertexIndexes(int[] i_x_coord, int[] i_y_coord, int i_coord_num, int i_area, int[] o_vertex)
\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
64 o_vertex[0] = vertex1_index;
\r
66 if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, vertex1_index, v1, thresh)) {
\r
69 if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v1,prev_vertex_index, thresh)) {
\r
74 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {
\r
75 o_vertex[1] = wv1.vertex[0];
\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
83 v2 = ((v1+i_coord_num-vertex1_index)/2+vertex1_index)%i_coord_num;
\r
85 if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, vertex1_index, v2, thresh)) {
\r
88 if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v2, v1, thresh)) {
\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
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
103 v2 = ((v1+i_coord_num+prev_vertex_index)/2)%i_coord_num;
\r
106 if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, v1, v2, thresh)) {
\r
109 if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v2, prev_vertex_index, thresh)) {
\r
112 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {
\r
114 o_vertex[2] = wv1.vertex[0];
\r
115 o_vertex[3] = wv2.vertex[0];
\r
126 * i_pointの輪郭座標から、最も遠方にある輪郭座標のインデクスを探します。
\r
129 * @param i_coord_num
\r
132 private static int getFarPoint(int[] i_coord_x, int[] i_coord_y,int i_coord_num,int i_point)
\r
135 final int sx = i_coord_x[i_point];
\r
136 final int sy = i_coord_y[i_point];
\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
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
166 * get_vertex関数を切り離すためのクラス
\r
169 final class NyARVertexCounter
\r
171 public final int[] vertex = new int[10];// 6まで削れる
\r
173 public int number_of_vertex;
\r
175 private double thresh;
\r
177 private int[] x_coord;
\r
179 private int[] y_coord;
\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
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
191 * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数
\r
200 private boolean get_vertex(int st, int ed,int i_coord_len)
\r
202 //メモ:座標値は65536を超えなければint32で扱って大丈夫なので変更。
\r
203 //dmaxは4乗なのでやるとしてもint64じゃないとマズイ
\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
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
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
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
239 if (dmax / (double)(a * a + b * b) > thresh) {
\r
240 if (!get_vertex(st, v1,i_coord_len)) {
\r
243 if (number_of_vertex > 5) {
\r
246 vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1;
\r
247 number_of_vertex++;// (*vnum)++;
\r
249 if (!get_vertex(v1, ed,i_coord_len)) {
\r