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 * 定点と傾きのパラメータで、直線を表現します。
\r
32 public class NyARVecLinear2d
\r
38 public static NyARVecLinear2d[] createArray(int i_length)
\r
40 NyARVecLinear2d[] r=new NyARVecLinear2d[i_length];
\r
41 for(int i=0;i<i_length;i++){
\r
42 r[i]=new NyARVecLinear2d();
\r
49 * 元のベクトルを指定します。この値には、thisを指定できます。
\r
51 public final void normalVec(NyARVecLinear2d i_src)
\r
57 public final void setValue(NyARVecLinear2d i_value)
\r
65 * このベクトルと指定した直線が作るCos値を返します。
\r
69 public final double getVecCos(NyARVecLinear2d i_v1)
\r
75 double d=(x1*x2+y1*y2)/Math.sqrt((x1*x1+y1*y1)*(x2*x2+y2*y2));
\r
78 public final double getAbsVecCos(NyARVecLinear2d i_v1)
\r
84 double d=(x1*x2+y1*y2)/Math.sqrt((x1*x1+y1*y1)*(x2*x2+y2*y2));
\r
88 * このベクトルと指定したベクトルが作るCos値を返します。
\r
93 public final double getVecCos(double i_dx,double i_dy)
\r
97 double d=(x1*i_dx+y1*i_dy)/Math.sqrt((x1*x1+y1*y1)*(i_dx*i_dx+i_dy*i_dy));
\r
100 public final double getVecCos(NyARDoublePoint2d i_pos1,NyARDoublePoint2d i_pos2)
\r
102 double d=getAbsVecCos(i_pos2.x-i_pos1.x,i_pos2.y-i_pos1.y);
\r
105 public final double getAbsVecCos(double i_v2_x,double i_v2_y)
\r
109 double d=(x1*i_v2_x+y1*i_v2_y)/Math.sqrt((x1*x1+y1*y1)*(i_v2_x*i_v2_x+i_v2_y*i_v2_y));
\r
113 * このベクトルと、i_pos1-<i_pos2を結ぶ線分が作るcos値の絶対値を返します。
\r
118 public final double getAbsVecCos(NyARDoublePoint2d i_pos1,NyARDoublePoint2d i_pos2)
\r
120 double d=getAbsVecCos(i_pos2.x-i_pos1.x,i_pos2.y-i_pos1.y);
\r
131 public final boolean crossPos(NyARVecLinear2d i_vector1,NyARDoublePoint2d o_point)
\r
133 double a1= i_vector1.dy;
\r
134 double b1=-i_vector1.dx;
\r
135 double c1=(i_vector1.dx*i_vector1.y-i_vector1.dy*i_vector1.x);
\r
136 double a2= this.dy;
\r
137 double b2=-this.dx;
\r
138 double c2=(this.dx*this.y-this.dy*this.x);
\r
139 final double w1 = a1 * b2 - a2 * b1;
\r
143 o_point.x = (b1 * c2 - b2 * c1) / w1;
\r
144 o_point.y = (a2 * c1 - a1 * c2) / w1;
\r
148 * 直線と、i_sp1とi_sp2の作る線分との二乗距離値の合計を返します。
\r
156 public final double sqDistBySegmentLineEdge(NyARDoublePoint2d i_sp1,NyARDoublePoint2d i_sp2)
\r
161 sc=(this.dx*this.y-this.dy*this.x);
\r
169 w1 = sa * (-sa) - sb * sb;
\r
171 return Double.POSITIVE_INFINITY;
\r
173 //i_sp1と、i_linerの交点
\r
174 lc=-(sb*i_sp1.x-sa*i_sp1.y);
\r
175 x = ((sb * lc +sa * sc) / w1)-i_sp1.x;
\r
176 y = ((sb * sc - sa * lc) / w1)-i_sp1.y;
\r
177 double sqdist=x*x+y*y;
\r
179 lc=-(sb*i_sp2.x-sa*i_sp2.y);
\r
180 x = ((sb * lc + sa * sc) / w1)-i_sp2.x;
\r
181 y = ((sb * sc - sa * lc) / w1)-i_sp2.y;
\r
183 return sqdist+x*x+y*y;
\r
187 * i_lineの直線をセットします。x,yの値は、(i_x,i_y)を通過するi_lineの法線とi_lineの交点をセットします。
\r
192 public boolean setLinear(NyARLinear i_line,double i_x,double i_y)
\r
194 double la=i_line.b;
\r
195 double lb=-i_line.a;
\r
196 double lc=-(la*i_x+lb*i_y);
\r
198 final double w1 = -lb * lb - la * la;
\r
202 this.x=((la * lc - lb * i_line.c) / w1);
\r
203 this.y= ((la * i_line.c +lb * lc) / w1);
\r
209 * 点群から最小二乗法で直線を計算してセットします。
\r
210 * 通過点x,yは、点群の中央値を通過する、算出された直線の法線との交点です。
\r
212 * @param i_number_of_data
\r
215 public final boolean leastSquares(NyARDoublePoint2d[] i_points,int i_number_of_data)
\r
218 double sum_xy = 0, sum_x = 0, sum_y = 0, sum_x2 = 0;
\r
219 for (i=0; i<i_number_of_data; i++){
\r
220 NyARDoublePoint2d ptr=i_points[i];
\r
222 sum_xy += xw * ptr.y;
\r
227 double la=-(i_number_of_data * sum_x2 - sum_x*sum_x);
\r
228 double lb=-(i_number_of_data * sum_xy - sum_x * sum_y);
\r
229 double cc=(sum_x2 * sum_y - sum_xy * sum_x);
\r
230 double lc=-(la*sum_x+lb*sum_y)/i_number_of_data;
\r
232 final double w1 = -lb * lb - la * la;
\r
236 this.x=((la * lc - lb * cc) / w1);
\r
237 this.y= ((la * cc +lb * lc) / w1);
\r
243 * 正規化したベクトルを出力する{@link #leastSquares}です。
\r
245 * @param i_number_of_data
\r
248 public final boolean leastSquaresWithNormalize(NyARDoublePoint2d[] i_points,int i_number_of_data)
\r
250 boolean ret=this.leastSquares(i_points, i_number_of_data);
\r
251 double sq=1/Math.sqrt(this.dx*this.dx+this.dy*this.dy);
\r