OSDN Git Service

[Backup]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / 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 version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU General Public License\r
15  * as published by the Free Software Foundation; either version 2\r
16  * of the License, or (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 framework; if not, write to the Free Software\r
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.core;\r
33 \r
34 /**\r
35  * get_vertex関数を切り離すためのクラス\r
36  * \r
37  */\r
38 final public class NyARVertexCounter\r
39 {\r
40         public final int[] vertex = new int[10];// 6まで削れる\r
41 \r
42         public int number_of_vertex;\r
43 \r
44         private double thresh;\r
45 \r
46         private int[] x_coord;\r
47 \r
48         private int[] y_coord;\r
49 \r
50         public boolean getVertex(int[] i_x_coord, int[] i_y_coord, int st, int ed, double i_thresh)\r
51         {\r
52                 this.number_of_vertex = 0;\r
53                 this.thresh = i_thresh;\r
54                 this.x_coord = i_x_coord;\r
55                 this.y_coord = i_y_coord;\r
56                 return get_vertex(st, ed);\r
57         }\r
58 \r
59         /**\r
60          * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数\r
61          * \r
62          * @param x_coord\r
63          * @param y_coord\r
64          * @param st\r
65          * @param ed\r
66          * @param thresh\r
67          * @return\r
68          */\r
69         private boolean get_vertex(int st, int ed)\r
70         {\r
71                 //メモ:座標値は65536を超えなければint32で扱って大丈夫なので変更。\r
72                 //dmaxは4乗なのでやるとしてもint64じゃないとマズイ\r
73                 int v1 = 0;\r
74                 final int[] lx_coord = this.x_coord;\r
75                 final int[] ly_coord = this.y_coord;\r
76                 final int a = ly_coord[ed] - ly_coord[st];\r
77                 final int b = lx_coord[st] - lx_coord[ed];\r
78                 final int c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st];\r
79                 double dmax = 0;\r
80                 for (int i = st + 1; i < ed; i++) {\r
81                         final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
82                         if (d * d > dmax) {\r
83                                 dmax = d * d;\r
84                                 v1 = i;\r
85                         }\r
86                 }\r
87                 if (dmax / (double)(a * a + b * b) > thresh) {\r
88                         if (!get_vertex(st, v1)) {\r
89                                 return false;\r
90                         }\r
91                         if (number_of_vertex > 5) {\r
92                                 return false;\r
93                         }\r
94                         vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1;\r
95                         number_of_vertex++;// (*vnum)++;\r
96 \r
97                         if (!get_vertex(v1, ed)) {\r
98                                 return false;\r
99                         }\r
100                 }\r
101                 return true;\r
102         }\r
103 }