OSDN Git Service

[TAG]NyARToolkit/2.5.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.0 / src / jp / nyatla / nyartoolkit / core / squaredetect / NyARCoord2SquareVertexIndexes.java
diff --git a/tags/2.5.0/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARCoord2SquareVertexIndexes.java b/tags/2.5.0/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARCoord2SquareVertexIndexes.java
new file mode 100644 (file)
index 0000000..fe19625
--- /dev/null
@@ -0,0 +1,255 @@
+/* \r
+ * PROJECT: NyARToolkit\r
+ * --------------------------------------------------------------------------------\r
+ * This work is based on the original ARToolKit developed by\r
+ *   Hirokazu Kato\r
+ *   Mark Billinghurst\r
+ *   HITLab, University of Washington, Seattle\r
+ * http://www.hitl.washington.edu/artoolkit/\r
+ *\r
+ * The NyARToolkit is Java edition ARToolKit class library.\r
+ * Copyright (C)2008-2009 Ryo Iizuka\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ * \r
+ * For further information please contact.\r
+ *     http://nyatla.jp/nyatoolkit/\r
+ *     <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.core.squaredetect;\r
+\r
+/**\r
+ * 座標店集合(輪郭線)から、四角系の頂点候補点を計算します。\r
+ *\r
+ */\r
+public class NyARCoord2SquareVertexIndexes\r
+{\r
+       private static final double VERTEX_FACTOR = 1.0;// 線検出のファクタ     \r
+       private final NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter();\r
+       private final NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter();\r
+       public NyARCoord2SquareVertexIndexes()\r
+       {\r
+               return;\r
+       }\r
+       /**\r
+        * 座標集合から、頂点候補になりそうな場所を4箇所探して、そのインデクス番号を返します。\r
+        * @param i_x_coord\r
+        * @param i_y_coord\r
+        * @param i_coord_num\r
+        * @param i_area\r
+        * @param o_vertex\r
+        * @return\r
+        */\r
+       public boolean getVertexIndexes(int[] i_x_coord, int[] i_y_coord, int i_coord_num, int i_area, int[] o_vertex)\r
+       {\r
+               final NyARVertexCounter wv1 = this.__getSquareVertex_wv1;\r
+               final NyARVertexCounter wv2 = this.__getSquareVertex_wv2;\r
+               int vertex1_index=getFarPoint(i_x_coord,i_y_coord,i_coord_num,0);\r
+               int prev_vertex_index=(vertex1_index+i_coord_num)%i_coord_num;\r
+               int v1=getFarPoint(i_x_coord,i_y_coord,i_coord_num,vertex1_index);\r
+               final double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR;\r
+\r
+               o_vertex[0] = vertex1_index;\r
+\r
+               if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, vertex1_index, v1, thresh)) {\r
+                       return false;\r
+               }\r
+               if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v1,prev_vertex_index, thresh)) {\r
+                       return false;\r
+               }\r
+\r
+               int v2;\r
+               if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {\r
+                       o_vertex[1] = wv1.vertex[0];\r
+                       o_vertex[2] = v1;\r
+                       o_vertex[3] = wv2.vertex[0];\r
+               } else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0) {\r
+                       //頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。\r
+                       if(v1>=vertex1_index){\r
+                               v2 = (v1-vertex1_index)/2+vertex1_index;\r
+                       }else{\r
+                               v2 = ((v1+i_coord_num-vertex1_index)/2+vertex1_index)%i_coord_num;\r
+                       }\r
+                       if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, vertex1_index, v2, thresh)) {\r
+                               return false;\r
+                       }\r
+                       if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v2, v1, thresh)) {\r
+                               return false;\r
+                       }\r
+                       if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {\r
+                               o_vertex[1] = wv1.vertex[0];\r
+                               o_vertex[2] = wv2.vertex[0];\r
+                               o_vertex[3] = v1;\r
+                       } else {\r
+                               return false;\r
+                       }\r
+               } else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1) {\r
+                       //v2 = (v1+ end_of_coord)/2;\r
+                       if(v1<=prev_vertex_index){\r
+                               v2 = (v1+prev_vertex_index)/2;\r
+                       }else{\r
+                               v2 = ((v1+i_coord_num+prev_vertex_index)/2)%i_coord_num;\r
+                               \r
+                       }\r
+                       if (!wv1.getVertex(i_x_coord, i_y_coord,i_coord_num, v1, v2, thresh)) {\r
+                               return false;\r
+                       }\r
+                       if (!wv2.getVertex(i_x_coord, i_y_coord,i_coord_num, v2, prev_vertex_index, thresh)) {\r
+                               return false;\r
+                       }\r
+                       if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {\r
+                               o_vertex[1] = v1;\r
+                               o_vertex[2] = wv1.vertex[0];\r
+                               o_vertex[3] = wv2.vertex[0];\r
+                       } else {\r
+                               \r
+                               return false;\r
+                       }\r
+               } else {\r
+                       return false;\r
+               }\r
+               return true;\r
+       }\r
+       /**\r
+        * i_pointの輪郭座標から、最も遠方にある輪郭座標のインデクスを探します。\r
+        * @param i_xcoord\r
+        * @param i_ycoord\r
+        * @param i_coord_num\r
+        * @return\r
+        */\r
+       private static int getFarPoint(int[] i_coord_x, int[] i_coord_y,int i_coord_num,int i_point)\r
+       {\r
+               //\r
+               final int sx = i_coord_x[i_point];\r
+               final int sy = i_coord_y[i_point];\r
+               int d = 0;\r
+               int w, x, y;\r
+               int ret = 0;\r
+               for (int i = i_point+1; i < i_coord_num; i++) {\r
+                       x = i_coord_x[i] - sx;\r
+                       y = i_coord_y[i] - sy;\r
+                       w = x * x + y * y;\r
+                       if (w > d) {\r
+                               d = w;\r
+                               ret = i;\r
+                       }\r
+               }\r
+               for (int i = 0; i < i_point; i++) {\r
+                       x = i_coord_x[i] - sx;\r
+                       y = i_coord_y[i] - sy;\r
+                       w = x * x + y * y;\r
+                       if (w > d) {\r
+                               d = w;\r
+                               ret = i;\r
+                       }\r
+               }               \r
+               return ret;\r
+       }       \r
+}\r
+\r
+\r
+\r
+\r
+/**\r
+ * get_vertex関数を切り離すためのクラス\r
+ * \r
+ */\r
+final class NyARVertexCounter\r
+{\r
+       public final int[] vertex = new int[10];// 6まで削れる\r
+\r
+       public int number_of_vertex;\r
+\r
+       private double thresh;\r
+\r
+       private int[] x_coord;\r
+\r
+       private int[] y_coord;\r
+\r
+       public boolean getVertex(int[] i_x_coord, int[] i_y_coord,int i_coord_len,int st, int ed, double i_thresh)\r
+       {\r
+               this.number_of_vertex = 0;\r
+               this.thresh = i_thresh;\r
+               this.x_coord = i_x_coord;\r
+               this.y_coord = i_y_coord;\r
+               return get_vertex(st, ed,i_coord_len);\r
+       }\r
+\r
+       /**\r
+        * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数\r
+        * \r
+        * @param x_coord\r
+        * @param y_coord\r
+        * @param st\r
+        * @param ed\r
+        * @param thresh\r
+        * @return\r
+        */\r
+       private boolean get_vertex(int st, int ed,int i_coord_len)\r
+       {\r
+               //メモ:座標値は65536を超えなければint32で扱って大丈夫なので変更。\r
+               //dmaxは4乗なのでやるとしてもint64じゃないとマズイ\r
+               int v1 = 0;\r
+               final int[] lx_coord = this.x_coord;\r
+               final int[] ly_coord = this.y_coord;\r
+               final int a = ly_coord[ed] - ly_coord[st];\r
+               final int b = lx_coord[st] - lx_coord[ed];\r
+               final int c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st];\r
+               double dmax = 0;\r
+               if(st<ed){\r
+                       //stとedが1区間\r
+                       for (int i = st + 1; i < ed; i++) {\r
+                               final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
+                               if (d * d > dmax) {\r
+                                       dmax = d * d;\r
+                                       v1 = i;\r
+                               }\r
+                       }\r
+               }else{\r
+                       //stとedが2区間\r
+                       for (int i = st + 1; i < i_coord_len; i++) {\r
+                               final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
+                               if (d * d > dmax) {\r
+                                       dmax = d * d;\r
+                                       v1 = i;\r
+                               }\r
+                       }\r
+                       for (int i = 0; i < ed; i++) {\r
+                               final double d = a * lx_coord[i] + b * ly_coord[i] + c;\r
+                               if (d * d > dmax) {\r
+                                       dmax = d * d;\r
+                                       v1 = i;\r
+                               }\r
+                       }\r
+               }\r
+\r
+               \r
+               if (dmax / (double)(a * a + b * b) > thresh) {\r
+                       if (!get_vertex(st, v1,i_coord_len)) {\r
+                               return false;\r
+                       }\r
+                       if (number_of_vertex > 5) {\r
+                               return false;\r
+                       }\r
+                       vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1;\r
+                       number_of_vertex++;// (*vnum)++;\r
+\r
+                       if (!get_vertex(v1, ed,i_coord_len)) {\r
+                               return false;\r
+                       }\r
+               }\r
+               return true;\r
+       }\r
+}
\ No newline at end of file