OSDN Git Service

3af2e9e7aac93264f47e524f85a9be332d2f2fca
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / labeling / LabelOverlapChecker.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\r
14  * modify it under the terms of the GNU Lesser General Public License\r
15  * as published by the Free Software Foundation; either version 3\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 Lesser General Public License for more details\r
22  * \r
23  * You should have received a copy of the GNU Lesser General Public\r
24  * License 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.labeling;\r
32 \r
33 import java.lang.reflect.Array;\r
34 \r
35 \r
36 /**\r
37  * ラベル同士の重なり(内包関係)を調べるクラスです。 \r
38  * ラベルリストに内包するラベルを蓄積し、それにターゲットのラベルが内包されているか を確認します。\r
39  */\r
40 public class LabelOverlapChecker<T extends NyARLabelInfo>\r
41 {\r
42         private T[] _labels;\r
43         private int _length;\r
44         private Class<T> _element_type;\r
45         /*\r
46         */\r
47         @SuppressWarnings("unchecked")\r
48         public LabelOverlapChecker(int i_max_label,Class<T> i_element_type)\r
49         {\r
50                 this._element_type=i_element_type;\r
51                 this._labels = (T[])Array.newInstance(i_element_type, i_max_label);\r
52         }\r
53 \r
54         /**\r
55          * チェック対象のラベルを追加する。\r
56          * \r
57          * @param i_label_ref\r
58          */\r
59         public void push(T i_label_ref)\r
60         {\r
61                 this._labels[this._length] = i_label_ref;\r
62                 this._length++;\r
63         }\r
64 \r
65         /**\r
66          * 現在リストにあるラベルと重なっているかを返す。\r
67          * \r
68          * @param i_label\r
69          * @return 何れかのラベルの内側にあるならばfalse,独立したラベルである可能性が高ければtrueです.\r
70          */\r
71         public boolean check(T i_label)\r
72         {\r
73                 // 重なり処理かな?\r
74                 final T[] label_pt = this._labels;\r
75                 final int px1 = (int) i_label.pos_x;\r
76                 final int py1 = (int) i_label.pos_y;\r
77                 for (int i = this._length - 1; i >= 0; i--) {\r
78                         final int px2 = (int) label_pt[i].pos_x;\r
79                         final int py2 = (int) label_pt[i].pos_y;\r
80                         final int d = (px1 - px2) * (px1 - px2) + (py1 - py2) * (py1 - py2);\r
81                         if (d < label_pt[i].area / 4) {\r
82                                 // 対象外\r
83                                 return false;\r
84                         }\r
85                 }\r
86                 // 対象\r
87                 return true;\r
88         }\r
89         /**\r
90          * 最大i_max_label個のラベルを蓄積できるようにオブジェクトをリセットする\r
91          * \r
92          * @param i_max_label\r
93          */\r
94         @SuppressWarnings("unchecked")\r
95         public void setMaxLabels(int i_max_label)\r
96         {\r
97                 if (i_max_label > this._labels.length) {\r
98                         this._labels = (T[])Array.newInstance(this._element_type, i_max_label);\r
99                 }\r
100                 this._length = 0;\r
101         }       \r
102         \r
103         \r
104 }\r