OSDN Git Service

[Backup]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / squaredetect / NyARVertexCounter.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  * get_vertex関数を切り離すためのクラス\r
35  * \r
36  */\r
37 final public class NyARVertexCounter\r
38 {\r
39         public final int[] vertex = new int[10];// 6まで削れる\r
40 \r
41         public int number_of_vertex;\r
42 \r
43         private double thresh;\r
44 \r
45         private int[] x_coord;\r
46 \r
47         private int[] y_coord;\r
48 \r
49         public boolean getVertex(int[] i_x_coord, int[] i_y_coord, int st, int ed, double i_thresh)\r
50         {\r
51                 this.number_of_vertex = 0;\r
52                 this.thresh = i_thresh;\r
53                 this.x_coord = i_x_coord;\r
54                 this.y_coord = i_y_coord;\r
55                 return get_vertex(st, ed);\r
56         }\r
57 \r
58         /**\r
59          * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数\r
60          * \r
61          * @param x_coord\r
62          * @param y_coord\r
63          * @param st\r
64          * @param ed\r
65          * @param thresh\r
66          * @return\r
67          */\r
68         private boolean get_vertex(int st, int ed)\r
69         {\r
70                 //メモ:座標値は65536を超えなければint32で扱って大丈夫なので変更。\r
71                 //dmaxは4乗なのでやるとしてもint64じゃないとマズイ\r
72                 int v1 = 0;\r
73                 final int[] lx_coord = this.x_coord;\r
74                 final int[] ly_coord = this.y_coord;\r
75                 final int a = ly_coord[ed] - ly_coord[st];\r
76                 final int b = lx_coord[st] - lx_coord[ed];\r
77                 final int c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st];\r
78                 double dmax = 0;\r
79                 for (int i = st + 1; i < ed; i++) {\r
80                         final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
81                         if (d * d > dmax) {\r
82                                 dmax = d * d;\r
83                                 v1 = i;\r
84                         }\r
85                 }\r
86                 if (dmax / (double)(a * a + b * b) > thresh) {\r
87                         if (!get_vertex(st, v1)) {\r
88                                 return false;\r
89                         }\r
90                         if (number_of_vertex > 5) {\r
91                                 return false;\r
92                         }\r
93                         vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1;\r
94                         number_of_vertex++;// (*vnum)++;\r
95 \r
96                         if (!get_vertex(v1, ed)) {\r
97                                 return false;\r
98                         }\r
99                 }\r
100                 return true;\r
101         }\r
102 }