2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU General Public License
\r
15 * as published by the Free Software Foundation; either version 2
\r
16 * of the License, or (at your option) any later version.
\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 General Public License for more details.
\r
23 * You should have received a copy of the GNU General Public License
\r
24 * along with this framework; if not, write to the Free Software
\r
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.sandbox.x2;
\r
34 import jp.nyatla.nyartoolkit.NyARException;
\r
35 import jp.nyatla.nyartoolkit.core.NyARMat;
\r
36 import jp.nyatla.nyartoolkit.core.types.*;
\r
37 import jp.nyatla.nyartoolkit.core.param.*;
\r
38 import jp.nyatla.nyartoolkit.core2.types.matrix.*;
\r
39 import jp.nyatla.nyartoolkit.core2.types.*;
\r
41 public class NyARFixedFloatRotVector
\r
51 private NyARFixedFloat16Matrix33 _cmat = new NyARFixedFloat16Matrix33();
\r
53 private NyARPerspectiveProjectionMatrix _projection_mat_ref;
\r
55 private double[][] _inv_cpara_array_ref;
\r
57 public NyARFixedFloatRotVector(NyARPerspectiveProjectionMatrix i_cmat) throws NyARException
\r
59 final NyARMat mat_a = new NyARMat(3, 3);
\r
60 final double[][] a_array = mat_a.getArray();
\r
62 a_array[0][0] = i_cmat.m00;
\r
63 a_array[0][1] = i_cmat.m01;
\r
64 a_array[0][2] = i_cmat.m02;
\r
65 a_array[1][0] = i_cmat.m10;
\r
66 a_array[1][1] = i_cmat.m11;
\r
67 a_array[1][2] = i_cmat.m12;
\r
68 a_array[2][0] = i_cmat.m20;
\r
69 a_array[2][1] = i_cmat.m21;
\r
70 a_array[2][2] = i_cmat.m22;
\r
72 mat_a.matrixSelfInv();
\r
73 this._projection_mat_ref = i_cmat;
\r
75 this._cmat.m00 = (long)(i_cmat.m00 * NyMath.FIXEDFLOAT24_1);
\r
76 this._cmat.m01 = (long)(i_cmat.m01 * NyMath.FIXEDFLOAT24_1);
\r
77 this._cmat.m02 = (long)(i_cmat.m02 * NyMath.FIXEDFLOAT24_1);
\r
78 this._cmat.m10 = (long)(i_cmat.m10 * NyMath.FIXEDFLOAT24_1);
\r
79 this._cmat.m11 = (long)(i_cmat.m11 * NyMath.FIXEDFLOAT24_1);
\r
80 this._cmat.m12 = (long)(i_cmat.m12 * NyMath.FIXEDFLOAT24_1);
\r
81 this._cmat.m20 = (long)(i_cmat.m20 * NyMath.FIXEDFLOAT24_1);
\r
82 this._cmat.m21 = (long)(i_cmat.m21 * NyMath.FIXEDFLOAT24_1);
\r
83 this._cmat.m22 = (long)(i_cmat.m22 * NyMath.FIXEDFLOAT24_1);
\r
84 this._inv_cpara_array_ref = mat_a.getArray();
\r
85 //GCない言語のときは、ここで配列の所有権委譲してね!
\r
89 * 2直線に直交するベクトルを計算する・・・だと思う。
\r
93 public void exteriorProductFromLinear(NyARLinear i_linear1, NyARLinear i_linear2)
\r
96 NyARPerspectiveProjectionMatrix cmat = this._projection_mat_ref;
\r
97 final double w1 = i_linear1.run * i_linear2.rise - i_linear2.run * i_linear1.rise;
\r
98 final double w2 = i_linear1.rise * i_linear2.intercept - i_linear2.rise * i_linear1.intercept;
\r
99 final double w3 = i_linear1.intercept * i_linear2.run - i_linear2.intercept * i_linear1.run;
\r
101 final double m0 = w1 * (cmat.m01 * cmat.m12 - cmat.m02 * cmat.m11) + w2 * cmat.m11 - w3 * cmat.m01;//w1 * (cpara[0 * 4 + 1] * cpara[1 * 4 + 2] - cpara[0 * 4 + 2] * cpara[1 * 4 + 1]) + w2 * cpara[1 * 4 + 1] - w3 * cpara[0 * 4 + 1];
\r
102 final double m1 = -w1 * cmat.m00 * cmat.m12 + w3 * cmat.m00;//-w1 * cpara[0 * 4 + 0] * cpara[1 * 4 + 2] + w3 * cpara[0 * 4 + 0];
\r
103 final double m2 = w1 * cmat.m00 * cmat.m11;//w1 * cpara[0 * 4 + 0] * cpara[1 * 4 + 1];
\r
104 final double w = Math.sqrt(m0 * m0 + m1 * m1 + m2 * m2);
\r
105 this.v1 = (long)(m0 * NyMath.FIXEDFLOAT16_1 / w);
\r
106 this.v2 = (long)(m1 * NyMath.FIXEDFLOAT16_1 / w);
\r
107 this.v3 = (long)(m2 * NyMath.FIXEDFLOAT16_1 / w);
\r
112 * static int check_dir( double dir[3], double st[2], double ed[2],double cpara[3][4] ) Optimize:STEP[526->468]
\r
113 * ベクトルの開始/終了座標を指定して、ベクトルの方向を調整する。
\r
114 * @param i_start_vertex
\r
115 * @param i_end_vertex
\r
118 public void checkVectorByVertex(NyARFixedFloat16Point2d i_start_vertex, NyARFixedFloat16Point2d i_end_vertex) throws NyARException
\r
121 final double[][] inv_cpara = this._inv_cpara_array_ref;
\r
122 final double stx = (double)(i_start_vertex.x / NyMath.FIXEDFLOAT16_1);
\r
123 final double sty = (double)(i_start_vertex.y / NyMath.FIXEDFLOAT16_1);
\r
125 //final double[] world = __checkVectorByVertex_world;// [2][3];
\r
126 final long world0 = (long)((inv_cpara[0][0] * stx + inv_cpara[0][1] * sty + inv_cpara[0][2]) * 10.0 * NyMath.FIXEDFLOAT16_1);// mat_a->m[0]*st[0]*10.0+
\r
127 final long world1 = (long)((inv_cpara[1][0] * stx + inv_cpara[1][1] * sty + inv_cpara[1][2]) * 10.0 * NyMath.FIXEDFLOAT16_1);// mat_a->m[3]*st[0]*10.0+
\r
128 final long world2 = (long)((inv_cpara[2][0] * stx + inv_cpara[2][1] * sty + inv_cpara[2][2]) * 10.0 * NyMath.FIXEDFLOAT16_1);// mat_a->m[6]*st[0]*10.0+
\r
129 final long world3 = world0 + (this.v1);
\r
130 final long world4 = world1 + (this.v2);
\r
131 final long world5 = world2 + (this.v3);
\r
134 //final double[] camera = __checkVectorByVertex_camera;// [2][2];
\r
135 //h = cpara[2 * 4 + 0] * world0 + cpara[2 * 4 + 1] * world1 + cpara[2 * 4 + 2] * world2;
\r
136 h = (this._cmat.m20 * world0 + this._cmat.m21 * world1 + this._cmat.m22 * world2) >> 16;
\r
139 throw new NyARException();
\r
141 //final double camera0 = (cpara[0 * 4 + 0] * world0 + cpara[0 * 4 + 1] * world1 + cpara[0 * 4 + 2] * world2) / h;
\r
142 //final double camera1 = (cpara[1 * 4 + 0] * world0 + cpara[1 * 4 + 1] * world1 + cpara[1 * 4 + 2] * world2) / h;
\r
143 final long camera0 = (this._cmat.m00 * world0 + this._cmat.m01 * world1 + this._cmat.m02 * world2) / h;
\r
144 final long camera1 = (this._cmat.m10 * world0 + this._cmat.m11 * world1 + this._cmat.m12 * world2) / h;
\r
146 //h = cpara[2 * 4 + 0] * world3 + cpara[2 * 4 + 1] * world4 + cpara[2 * 4 + 2] * world5;
\r
147 h = (this._cmat.m20 * world3 + this._cmat.m21 * world4 + this._cmat.m22 * world5) >> 16;
\r
150 throw new NyARException();
\r
152 //final double camera2 = (cpara[0 * 4 + 0] * world3 + cpara[0 * 4 + 1] * world4 + cpara[0 * 4 + 2] * world5) / h;
\r
153 //final double camera3 = (cpara[1 * 4 + 0] * world3 + cpara[1 * 4 + 1] * world4 + cpara[1 * 4 + 2] * world5) / h;
\r
154 final long camera2 = (this._cmat.m00 * world3 + this._cmat.m01 * world4 + this._cmat.m02 * world5) / h;
\r
155 final long camera3 = (this._cmat.m10 * world3 + this._cmat.m11 * world4 + this._cmat.m12 * world5) / h;
\r
158 long v = ((i_end_vertex.x - i_start_vertex.x) * (camera2 - camera0) + (i_end_vertex.y - i_start_vertex.y) * (camera3 - camera1)) >> 16;
\r
162 this.v1 = -this.v1;
\r
163 this.v2 = -this.v2;
\r
164 this.v3 = -this.v3;
\r
169 private static int DIV0_CANCEL=1;
\r
171 * int check_rotation( double rot[2][3] )
\r
174 * @throws NyARException
\r
176 public final static void checkRotation(NyARFixedFloatRotVector io_vec1, NyARFixedFloatRotVector io_vec2) throws NyARException
\r
181 long vec10 = io_vec1.v1;
\r
182 long vec11 = io_vec1.v2;
\r
183 long vec12 = io_vec1.v3;
\r
184 long vec20 = io_vec2.v1;
\r
185 long vec21 = io_vec2.v2;
\r
186 long vec22 = io_vec2.v3;
\r
188 long vec30 = (vec11 * vec22 - vec12 * vec21)>>16;
\r
189 long vec31 = (vec12 * vec20 - vec10 * vec22)>>16;
\r
190 long vec32 = (vec10 * vec21 - vec11 * vec20)>>16;
\r
191 w = NyMath.sqrtFixdFloat16((vec30 * vec30 + vec31 * vec31 + vec32 * vec32)>>16);
\r
194 //throw new NyARException();
\r
196 vec30= (vec30<<16)/w;
\r
197 vec31= (vec31<<16)/w;
\r
198 vec32= (vec32<<16)/w;
\r
200 long cb = (vec10 * vec20 + vec11 * vec21 + vec12 * vec22)>>16;
\r
202 cb=-cb;//cb *= -1.0;
\r
204 final long ca = (NyMath.sqrtFixdFloat16(cb + NyMath.FIXEDFLOAT16_1) + NyMath.sqrtFixdFloat16(NyMath.FIXEDFLOAT16_1 - cb)) >>1;
\r
206 if (vec31 * vec10 - vec11 * vec30 != 0) {
\r
209 if (vec32 * vec10 - vec12 * vec30 != 0) {
\r
210 w = vec11;vec11 = vec12;vec12 = w;
\r
211 w = vec31;vec31 = vec32;vec32 = w;
\r
214 w = vec10;vec10 = vec12;vec12 = w;
\r
215 w = vec30;vec30 = vec32;vec32 = w;
\r
219 if (vec31 * vec10 - vec11 * vec30 == 0) {
\r
220 throw new NyARException();
\r
231 k1 = ((vec11 * vec32 - vec31 * vec12)) / (DIV0_CANCEL+((vec31 * vec10 - vec11 * vec30)>>16));
\r
232 k2 = (vec31 * ca) / (DIV0_CANCEL+((vec31 * vec10 - vec11 * vec30)>>16));
\r
233 k3 = (vec10 * vec32 - vec30 * vec12) / (DIV0_CANCEL+((vec30 * vec11 - vec10 * vec31)>>16));
\r
234 k4 = (vec30 * ca) / (DIV0_CANCEL+((vec30 * vec11 - vec10 * vec31)>>16));
\r
236 a = ((k1 * k1 + k3 * k3)>>16) + NyMath.FIXEDFLOAT16_1;
\r
237 b = ((k1 * k2 + k3 * k4)>>16);
\r
238 c = ((k2 * k2 + k4 * k4)>>16) - NyMath.FIXEDFLOAT16_1;
\r
239 d = (b*b - a*c)>>16;
\r
241 //誤差で計算エラーが頻発するのでExceptionはしない
\r
242 //throw new NyARException();
\r
244 r1 = ((-b + NyMath.sqrtFixdFloat16(d))<<16) / a;
\r
245 p1 = ((k1 * r1)>>16) + k2;
\r
246 q1 = ((k3 * r1)>>16) + k4;
\r
247 r2 = ((-b - NyMath.sqrtFixdFloat16(d))<<16) / a;
\r
248 p2 = ((k1 * r2)>>16) + k2;
\r
249 q2 = ((k3 * r2)>>16) + k4;
\r
251 w = q1;q1 = r1;r1 = w;
\r
252 w = q2;q2 = r2;r2 = w;
\r
253 w = vec11;vec11 = vec12;vec12 = w;
\r
254 w = vec31;vec31 = vec32;vec32 = w;
\r
258 w = p1;p1 = r1;r1 = w;
\r
259 w = p2;p2 = r2;r2 = w;
\r
260 w = vec10;vec10 = vec12;vec12 = w;
\r
261 w = vec30;vec30 = vec32;vec32 = w;
\r
265 if (vec31 * vec20 - vec21 * vec30 != 0) {
\r
268 if (vec32 * vec20 - vec22 * vec30 != 0) {
\r
269 w = vec21;vec21 = vec22;vec22 = w;
\r
270 w = vec31;vec31 = vec32;vec32 = w;
\r
273 w = vec20;vec20 = vec22;vec22 = w;
\r
274 w = vec30;vec30 = vec32;vec32 = w;
\r
278 if (vec31 * vec20 - vec21 * vec30 == 0) {
\r
279 throw new NyARException();
\r
281 k1 = (vec21 * vec32 - vec31 * vec22) / (DIV0_CANCEL+((vec31 * vec20 - vec21 * vec30)>>16));
\r
282 k2 = (vec31 * ca) / (DIV0_CANCEL+((vec31 * vec20 - vec21 * vec30)>>16));
\r
283 k3 = (vec20 * vec32 - vec30 * vec22) / ((vec30 * vec21 - vec20 * vec31)>>16);
\r
284 k4 = (vec30 * ca) / (DIV0_CANCEL+((vec30 * vec21 - vec20 * vec31)>>16));
\r
286 a = ((k1 * k1 + k3 * k3)>>16) + NyMath.FIXEDFLOAT16_1;
\r
287 b = ((k1 * k2 + k3 * k4)>>16);
\r
288 c = ((k2 * k2 + k4 * k4)>>16) - NyMath.FIXEDFLOAT16_1;
\r
290 d = (b*b - a*c)>>16;
\r
292 //誤差で計算エラーが頻発するのでExceptionはしない
\r
293 // throw new NyARException();
\r
295 r3 = ((-b + NyMath.sqrtFixdFloat16(d))<<16) / a;
\r
296 p3 = ((k1 * r3)>>16) + k2;
\r
297 q3 = ((k3 * r3)>>16) + k4;
\r
298 r4 = ((-b - NyMath.sqrtFixdFloat16(d))<<16) / a;
\r
299 p4 = ((k1 * r4)>>16) + k2;
\r
300 q4 = ((k3 * r4)>>16) + k4;
\r
302 w = q3;q3 = r3;r3 = w;
\r
303 w = q4;q4 = r4;r4 = w;
\r
304 w = vec21;vec21 = vec22;vec22 = w;
\r
305 w = vec31;vec31 = vec32;vec32 = w;
\r
309 w = p3;p3 = r3;r3 = w;
\r
310 w = p4;p4 = r4;r4 = w;
\r
311 w = vec20;vec20 = vec22;vec22 = w;
\r
312 w = vec30;vec30 = vec32;vec32 = w;
\r
316 long e1 = (p1 * p3 + q1 * q3 + r1 * r3)>>16;
\r
320 long e2 = (p1 * p4 + q1 * q4 + r1 * r4)>>16;
\r
324 long e3 = (p2 * p3 + q2 * q3 + r2 * r3)>>16;
\r
328 long e4 = (p2 * p4 + q2 * q4 + r2 * r4)>>16;
\r