OSDN Git Service

[TAG]2.4.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.0 / sample / sandbox / jp / nyatla / nyartoolkit / sandbox / x2 / NyARFixedFloatVertexCounter.java
1 package jp.nyatla.nyartoolkit.sandbox.x2;\r
2 \r
3 public final class NyARFixedFloatVertexCounter\r
4 {\r
5         public final int[] vertex = new int[6];// 5まで削れる\r
6 \r
7         public int number_of_vertex;\r
8 \r
9         private long thresh_16f;\r
10 \r
11         private int[] x_coord;\r
12 \r
13         private int[] y_coord;\r
14 \r
15         public boolean getVertex(int[] i_x_coord, int[] i_y_coord, int st, int ed, long i_thresh)\r
16         {\r
17                 this.number_of_vertex = 0;\r
18                 this.thresh_16f = i_thresh;\r
19                 this.x_coord = i_x_coord;\r
20                 this.y_coord = i_y_coord;\r
21                 return get_vertex(st, ed);\r
22         }\r
23 \r
24         /**\r
25          * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数\r
26          * \r
27          * @param x_coord\r
28          * @param y_coord\r
29          * @param st\r
30          * @param ed\r
31          * @param thresh\r
32          * @return\r
33          */\r
34         private boolean get_vertex(int st, int ed)\r
35         {\r
36                 int v1 = 0;\r
37                 final int[] lx_coord = this.x_coord;\r
38                 final int[] ly_coord = this.y_coord;\r
39                 final int a = ly_coord[ed] - ly_coord[st];\r
40                 final int b = lx_coord[st] - lx_coord[ed];\r
41                 final int c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st];\r
42                 long dmax = 0;\r
43                 for (int i = st + 1; i < ed; i++) {\r
44                         final long d = a * lx_coord[i] + b * ly_coord[i] + c;\r
45                         if (d * d > dmax) {\r
46                                 dmax = d * d;\r
47                                 v1 = i;\r
48                         }\r
49                 }\r
50                 if ((dmax<<16) / (long)(a * a + b * b) > this.thresh_16f) {\r
51                         if (!get_vertex(st, v1)) {\r
52                                 return false;\r
53                         }\r
54                         if (number_of_vertex > 5) {\r
55                                 return false;\r
56                         }\r
57                         vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1;\r
58                         number_of_vertex++;// (*vnum)++;\r
59 \r
60                         if (!get_vertex(v1, ed)) {\r
61                                 return false;\r
62                         }\r
63                 }\r
64                 return true;\r
65         }\r
66 }\r