2 * PROJECT: NyARToolkit(Extension)
\r
3 * --------------------------------------------------------------------------------
\r
4 * The NyARToolkit is Java edition ARToolKit class library.
\r
5 * Copyright (C)2008-2009 Ryo Iizuka
\r
7 * This program is free software: you can redistribute it and/or modify
\r
8 * it under the terms of the GNU General Public License as published by
\r
9 * the Free Software Foundation, either version 3 of the License, or
\r
10 * (at your option) any later version.
\r
12 * This program is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 * GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
20 * For further information please contact.
\r
21 * http://nyatla.jp/nyatoolkit/
\r
22 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
25 package jp.nyatla.nyartoolkit.core.types;
\r
29 * このクラスは、通過点とX,Yの変化量で、直線を定義します。
\r
31 public class NyARVecLinear2d
\r
43 * この関数は、指定サイズのオブジェクト配列を作ります。
\r
49 public static NyARVecLinear2d[] createArray(int i_length)
\r
51 NyARVecLinear2d[] r=new NyARVecLinear2d[i_length];
\r
52 for(int i=0;i<i_length;i++){
\r
53 r[i]=new NyARVecLinear2d();
\r
61 * 元のインスタンスを指定します。この値には、thisを指定できます。
\r
63 public final void normalVec(NyARVecLinear2d i_src)
\r
70 * この関数は、オブジェクトの値をインスタンスにセットします。
\r
74 public final void setValue(NyARVecLinear2d i_value)
\r
82 * この関数は、この直線と引数の直線とが作るCos値を返します。
\r
88 public final double getVecCos(NyARVecLinear2d i_v1)
\r
94 double d=(x1*x2+y1*y2)/Math.sqrt((x1*x1+y1*y1)*(x2*x2+y2*y2));
\r
98 * この関数は、この直線と引数の直線とが作るCos値の絶対値を返します。
\r
102 * 2直線のCOS値の絶対値(radian)
\r
104 public final double getAbsVecCos(NyARVecLinear2d i_v1)
\r
106 double d=getVecCos(i_v1);
\r
110 * この関数は、この直線とベクトルが作るCos値を返します。
\r
118 public final double getVecCos(double i_dx,double i_dy)
\r
122 return (x1*i_dx+y1*i_dy)/Math.sqrt((x1*x1+y1*y1)*(i_dx*i_dx+i_dy*i_dy));
\r
125 * この関数は、この直線とベクトルが作るCos値の絶対値を返します。
\r
131 * 2直線のCOS値の絶対値(radian)
\r
133 public final double getAbsVecCos(double i_v2_x,double i_v2_y)
\r
135 double d=getVecCos(i_v2_x,i_v2_y);
\r
139 * この関数は、この直線と線分が作るCos値を返します。
\r
147 public final double getVecCos(NyARDoublePoint2d i_pos1,NyARDoublePoint2d i_pos2)
\r
149 return getVecCos(i_pos2.x-i_pos1.x,i_pos2.y-i_pos1.y);
\r
153 * この関数は、この直線と線分が作るCos値の絶対値を返します。
\r
159 * 2直線のCOS値の絶対値(radian)
\r
161 public final double getAbsVecCos(NyARDoublePoint2d i_pos1,NyARDoublePoint2d i_pos2)
\r
163 return getAbsVecCos(i_pos2.x-i_pos1.x,i_pos2.y-i_pos1.y);
\r
167 * この関数は、直線との交点を求めます。
\r
173 * 交点が求まると、trueを返します。
\r
175 public final boolean crossPos(NyARVecLinear2d i_vector1,NyARDoublePoint2d o_point)
\r
177 double a1= i_vector1.dy;
\r
178 double b1=-i_vector1.dx;
\r
179 double c1=(i_vector1.dx*i_vector1.y-i_vector1.dy*i_vector1.x);
\r
180 double a2= this.dy;
\r
181 double b2=-this.dx;
\r
182 double c2=(this.dx*this.y-this.dy*this.x);
\r
183 final double w1 = a1 * b2 - a2 * b1;
\r
187 o_point.x = (b1 * c2 - b2 * c1) / w1;
\r
188 o_point.y = (a2 * c1 - a1 * c2) / w1;
\r
192 * この関数は、この直線と、i_sp1とi_sp2の作る線分との、二乗距離値の合計を返します。
\r
193 * 計算方法は、線分の端点を通過する直線の法線上での、端点と直線の距離の合計です。
\r
194 * 線分と直線の類似度を判定する数値になります。
\r
200 * 二乗距離値の合計。距離が取れないときは無限大です。
\r
202 public final double sqDistBySegmentLineEdge(NyARDoublePoint2d i_sp1,NyARDoublePoint2d i_sp2)
\r
207 sc=(this.dx*this.y-this.dy*this.x);
\r
215 w1 = sa * (-sa) - sb * sb;
\r
217 return Double.POSITIVE_INFINITY;
\r
219 //i_sp1と、i_linerの交点
\r
220 lc=-(sb*i_sp1.x-sa*i_sp1.y);
\r
221 x = ((sb * lc +sa * sc) / w1)-i_sp1.x;
\r
222 y = ((sb * sc - sa * lc) / w1)-i_sp1.y;
\r
223 double sqdist=x*x+y*y;
\r
225 lc=-(sb*i_sp2.x-sa*i_sp2.y);
\r
226 x = ((sb * lc + sa * sc) / w1)-i_sp2.x;
\r
227 y = ((sb * sc - sa * lc) / w1)-i_sp2.y;
\r
229 return sqdist+x*x+y*y;
\r
233 * この関数は、i_lineの直線を、インスタンスにセットします。
\r
234 * {@link #x},{@link #y}の値は、(i_x,i_y)を通過するi_lineの法線とi_lineの交点をセットします。
\r
238 * {@link #x},{@link #y}を確定するための、法線の通過点
\r
240 * {@link #x},{@link #y}を確定するための、法線の通過点
\r
242 * セットに成功すると、trueを返します。
\r
244 public boolean setLinear(NyARLinear i_line,double i_x,double i_y)
\r
246 double la=i_line.b;
\r
247 double lb=-i_line.a;
\r
248 double lc=-(la*i_x+lb*i_y);
\r
250 final double w1 = -lb * lb - la * la;
\r
254 this.x=((la * lc - lb * i_line.c) / w1);
\r
255 this.y= ((la * i_line.c +lb * lc) / w1);
\r
261 * この関数は、頂点群から最小二乗法を使用して直線を計算します。
\r
264 * @param i_number_of_data
\r
267 * 計算に成功すると、trueを返します。
\r
269 public final boolean leastSquares(NyARDoublePoint2d[] i_points,int i_number_of_data)
\r
272 double sum_xy = 0, sum_x = 0, sum_y = 0, sum_x2 = 0;
\r
273 for (i=0; i<i_number_of_data; i++){
\r
274 NyARDoublePoint2d ptr=i_points[i];
\r
276 sum_xy += xw * ptr.y;
\r
281 double la=-(i_number_of_data * sum_x2 - sum_x*sum_x);
\r
282 double lb=-(i_number_of_data * sum_xy - sum_x * sum_y);
\r
283 double cc=(sum_x2 * sum_y - sum_xy * sum_x);
\r
284 double lc=-(la*sum_x+lb*sum_y)/i_number_of_data;
\r
286 final double w1 = -lb * lb - la * la;
\r
290 this.x=((la * lc - lb * cc) / w1);
\r
291 this.y= ((la * cc +lb * lc) / w1);
\r
297 * この関数は、正規化したベクトルを出力する、{@link #leastSquares}です。
\r
300 * @param i_number_of_data
\r
303 * 計算に成功すると、trueを返します。
\r
305 public final boolean leastSquaresWithNormalize(NyARDoublePoint2d[] i_points,int i_number_of_data)
\r
307 boolean ret=this.leastSquares(i_points, i_number_of_data);
\r
308 double sq=1/Math.sqrt(this.dx*this.dx+this.dy*this.dy);
\r