OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.0 / src.utils / java3d / jp / nyatla / nyartoolkit / java3d / utils / J3dNyARParam.java
1 /* \r
2  * PROJECT: NyARToolkit Java3D utilities.\r
3  * --------------------------------------------------------------------------------\r
4  * The MIT License\r
5  * Copyright (c) 2008 nyatla\r
6  * airmail(at)ebony.plala.or.jp\r
7  * http://nyatla.jp/nyartoolkit/\r
8  * \r
9  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
10  * of this software and associated documentation files (the "Software"), to deal\r
11  * in the Software without restriction, including without limitation the rights\r
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
13  * copies of the Software, and to permit persons to whom the Software is\r
14  * furnished to do so, subject to the following conditions:\r
15  * The above copyright notice and this permission notice shall be included in\r
16  * all copies or substantial portions of the Software.\r
17  * \r
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
24  * THE SOFTWARE.\r
25  * \r
26  */\r
27 package jp.nyatla.nyartoolkit.java3d.utils;\r
28 \r
29 import jp.nyatla.nyartoolkit.core.*;\r
30 import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
31 \r
32 import javax.media.j3d.Transform3D;\r
33 /**\r
34  * NyARParamにJava3D向け関数を追加したもの\r
35  */\r
36 public class J3dNyARParam extends NyARParam\r
37 {\r
38         private double view_distance_min = 0.01;//1cm~10.0m\r
39 \r
40         private double view_distance_max = 10.0;\r
41 \r
42         private Transform3D m_projection = null;\r
43 \r
44         /**\r
45          * 視体積の近い方をメートルで指定\r
46          * @param i_new_value\r
47          */\r
48         public void setViewDistanceMin(double i_new_value)\r
49         {\r
50                 m_projection = null;//キャッシュ済変数初期化\r
51                 view_distance_min = i_new_value;\r
52         }\r
53 \r
54         /**\r
55          * 視体積の遠い方をメートルで指定\r
56          * @param i_new_value\r
57          */\r
58         public void setViewDistanceMax(double i_new_value)\r
59         {\r
60                 m_projection = null;//キャッシュ済変数初期化\r
61                 view_distance_max = i_new_value;\r
62         }\r
63 \r
64         /**\r
65          * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16])\r
66          * 関数の置き換え\r
67          * @param focalmin\r
68          * @param focalmax\r
69          * @return\r
70          */\r
71         public Transform3D getCameraTransform()\r
72         {\r
73                 //既に値がキャッシュされていたらそれを使う\r
74                 if (m_projection != null) {\r
75                         return m_projection;\r
76                 }\r
77                 //無ければ計算\r
78 \r
79                 NyARMat trans_mat = new NyARMat(3, 4);\r
80                 NyARMat icpara_mat = new NyARMat(3, 4);\r
81                 double[][] p = new double[3][3], q = new double[4][4];\r
82                 double width, height;\r
83                 int i, j;\r
84 \r
85                 width = this._screen_size.w;\r
86                 height = this._screen_size.h;\r
87 \r
88                 this.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat);\r
89 \r
90                 double[][] icpara = icpara_mat.getArray();\r
91                 double[][] trans = trans_mat.getArray();\r
92                 for (i = 0; i < 4; i++) {\r
93                         icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i];\r
94                 }\r
95 \r
96                 for (i = 0; i < 3; i++) {\r
97                         for (j = 0; j < 3; j++) {\r
98                                 p[i][j] = icpara[i][j] / icpara[2][2];\r
99                         }\r
100                 }\r
101                 //p[0][0],p[1][1]=n\r
102                 //p[0][2],p[1][2]=t+b\r
103 \r
104                 //Projectionの計算\r
105                 q[0][0] = (2.0 * p[0][0] / (width - 1));\r
106                 q[0][1] = (2.0 * p[0][1] / (width - 1));\r
107                 q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0);\r
108                 q[0][3] = 0.0;\r
109 \r
110                 q[1][0] = 0.0;\r
111                 q[1][1] = -(2.0 * p[1][1] / (height - 1));\r
112                 q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0);\r
113                 q[1][3] = 0.0;\r
114 \r
115                 q[2][0] = 0.0;\r
116                 q[2][1] = 0.0;\r
117                 q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max);\r
118                 q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max);\r
119 \r
120                 q[3][0] = 0.0;\r
121                 q[3][1] = 0.0;\r
122                 q[3][2] = -1.0;\r
123                 q[3][3] = 0.0;\r
124 \r
125                 q[2][2] = q[2][2] * -1;\r
126                 q[2][3] = q[2][3] * -1;\r
127 \r
128                 double[] tmp_projection = new double[16];\r
129                 for (i = 0; i < 4; i++) { // Row.\r
130                         // First 3 columns of the current row.\r
131                         for (j = 0; j < 3; j++) { // Column.\r
132                                 tmp_projection[i + j * 4] = (q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j]);\r
133                         }\r
134                         // Fourth column of the current row.\r
135                         tmp_projection[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3];\r
136                 }\r
137                 m_projection = new Transform3D(tmp_projection);\r
138                 m_projection.transpose();\r
139                 return m_projection;\r
140         }\r
141 }\r