summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/kernel/perspective/World3D.java
blob: d3ffcc5e05c47e5211380b8f49babde2c112b3fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Lo�c Le Coq
 * Copyright (C) 2013 Marko Zivkovic
 * 
 * Contact Information: marko88zivkovic at gmail dot com
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.  This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.  You should have received a copy of the 
 * GNU General Public License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA 02110-1301, USA.
 * 
 * 
 * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
 * during his Bachelor thesis at the computer science department of ETH Z�rich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Lo�c Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were initially written by Lo�c Le Coq,
 * modifications, extensions, refactorings might have been applied by Marko Zivkovic 
 */

package xlogo.kernel.perspective;

import xlogo.storage.WSManager;
import xlogo.storage.user.UserConfig;

public class World3D
{
	
	double				theta			= Math.toRadians(45);
	
	double				phi				= Math.toRadians(30);
	
	public double		r				= 1500;
	
	public double		xCamera			= r * Math.cos(theta) * Math.cos(phi);
	
	public double		yCamera			= r * Math.sin(theta) * Math.cos(phi);
	
	public double		zCamera			= r * Math.sin(phi);					;
	
	public double		screenDistance	= 1000;
	
	private double[][]	array3D;
	
	public World3D()
	{
		initArray3D();
		
	}
	
	/**
	 * This method converts the coordinates in coord to the screen coord
	 */
	public void toScreenCoord(double[] coord)
	{
		// Coord in the camera world
		toCameraWorld(coord);
		/*
		 * double x=array3D[0][0]*coord[0]+array3D[1][0]*coord[1];
		 * double
		 * y=array3D[0][1]*coord[0]+array3D[1][1]*coord[1]+array3D[2][1]*coord
		 * [2];
		 * double
		 * z=array3D[0][2]*coord[0]+array3D[1][2]*coord[1]+array3D[2][2]*coord
		 * [2]+r;
		 */
		cameraToScreen(coord);
	}
	
	/**
	 * This method converts coordinates conatined in coord from camera world to
	 * screen
	 */
	public void cameraToScreen(double[] coord)
	{
		UserConfig uc = WSManager.getUserConfig();
		
		double x = coord[0];
		double y = coord[1];
		double z = coord[2];
		coord[0] = screenDistance * x / z + uc.getImageWidth() / 2;
		coord[1] = uc.getImageHeight() / 2 - screenDistance * y / z;
	}
	
	/**
	 * This method initializes the 3D array
	 */
	private void initArray3D()
	{
		double cost = Math.cos(theta);
		double sint = Math.sin(theta);
		double cosp = Math.cos(phi);
		double sinp = Math.sin(phi);
		array3D = new double[4][4];
		array3D[0][0] = -sint;
		array3D[0][1] = -cost * sinp;
		array3D[0][2] = -cost * cosp;
		array3D[0][3] = 0;
		array3D[1][0] = cost;
		array3D[1][1] = -sint * sinp;
		array3D[1][2] = -sint * cosp;
		array3D[1][3] = 0;
		array3D[2][0] = 0;
		array3D[2][1] = cosp;
		array3D[2][2] = -sinp;
		array3D[2][3] = 0;
		array3D[3][0] = 0;
		array3D[3][1] = 0;
		array3D[3][2] = r;
		array3D[3][3] = 1;
	}
	
	/**
	 * This method converts coordinates from Real world to coodinates in camera
	 * world
	 * 
	 * @param coord
	 *            The real World coordinates
	 * @return Array that contains coordinates in camera world
	 */
	
	public void toCameraWorld(double[] coord)
	{
		double x = array3D[0][0] * coord[0] + array3D[1][0] * coord[1];
		double y = array3D[0][1] * coord[0] + array3D[1][1] * coord[1] + array3D[2][1] * coord[2];
		double z = array3D[0][2] * coord[0] + array3D[1][2] * coord[1] + array3D[2][2] * coord[2] + r;
		coord[0] = x;
		coord[1] = y;
		coord[2] = z;
	}
	
	/**
	 * This method multiply two matrices
	 * 
	 * @param a
	 *            The first matrix
	 * @param b
	 *            The second matrix
	 * @return The matrix product
	 */
	public double[][] multiply(double[][] a, double[][] b)
	{
		int n = a.length;
		int p = b[0].length;
		int s = a[0].length;
		double[][] m = new double[n][p];
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < p; j++)
			{
				m[i][j] = 0;
				for (int k = 0; k < s; k++)
				{
					m[i][j] += a[i][k] * b[k][j];
				}
			}
		}
		return m;
	}
	
	/**
	 * This method returns a matrix for a Z Axis rotation
	 * 
	 * @param angle
	 *            The rotation angle
	 * @return The rotation matrix
	 */
	public double[][] rotationZ(double angle)
	{
		double[][] m = new double[3][3];
		double cos = Math.cos(Math.toRadians(angle));
		double sin = Math.sin(Math.toRadians(angle));
		m[0][0] = cos;
		m[1][0] = sin;
		m[0][1] = -sin;
		m[1][1] = cos;
		m[2][2] = 1;
		m[2][1] = m[2][0] = m[0][2] = m[1][2] = 0;
		return m;
	}
	
	/**
	 * This method returns a matrix for a Y Axis rotation
	 * 
	 * @param angle
	 *            The rotation angle
	 * @return The rotation matrix
	 */
	public double[][] rotationY(double angle)
	{
		double[][] m = new double[3][3];
		double cos = Math.cos(Math.toRadians(angle));
		double sin = Math.sin(Math.toRadians(angle));
		m[0][0] = cos;
		m[2][0] = sin;
		m[0][2] = -sin;
		m[2][2] = cos;
		m[1][1] = 1;
		m[0][1] = m[1][0] = m[1][2] = m[2][1] = 0;
		return m;
	}
	
	/**
	 * This method returns a matrix for a X Axis rotation
	 * 
	 * @param angle
	 *            The rotation angle
	 * @return The rotation matrix
	 */
	public double[][] rotationX(double angle)
	{
		double[][] m = new double[3][3];
		double cos = Math.cos(Math.toRadians(angle));
		double sin = Math.sin(Math.toRadians(angle));
		m[0][0] = 1;
		m[1][1] = cos;
		m[2][1] = sin;
		m[1][2] = -sin;
		m[2][2] = cos;
		m[1][0] = m[2][0] = m[0][1] = m[0][2] = 0;
		return m;
	}
	
	/**
	 * This method with the 3 Euler's angle builds a rotation matrix
	 * 
	 * @param heading
	 *            The turtle heading
	 * @param roll
	 *            The turtle roll
	 * @param pitch
	 *            The turtle pitch
	 * @return The rotation Matrix
	 */
	
	public double[][] EulerToRotation(double roll, double pitch, double heading)
	{
		double[][] m = new double[3][3];
		double rpitch = Math.toRadians(pitch);
		double rheading = Math.toRadians(heading);
		double rroll = Math.toRadians(roll);
		double a = Math.cos(rpitch);
		double b = Math.sin(rpitch);
		double c = Math.cos(rroll);
		double d = Math.sin(rroll);
		double e = Math.cos(rheading);
		double f = Math.sin(rheading);
		double bd = b * d;
		double bc = b * c;
		m[0][0] = c * e - bd * f;
		m[0][1] = -c * f - bd * e;
		m[0][2] = -a * d;
		m[1][0] = a * f;
		m[1][1] = a * e;
		m[1][2] = -b;
		m[2][0] = d * e + bc * f;
		m[2][1] = -d * f + bc * e;
		m[2][2] = a * c;
		return m;
	}
	
	/**
	  * 
	  */
	public double[] rotationToEuler(double[][] m)
	{
		double[] v = new double[3];
		
		double angle_x, angle_y, angle_z;
		double a, tr_x, tr_y;
		// Angle x
		angle_x = -Math.asin(m[1][2]);
		
		a = Math.cos(angle_x);
		// Gimbal Lock?
		if (Math.abs(a) > 0.005)
		{
			// No gimbal Lock
			// Angle z
			tr_x = m[1][1] / a;
			tr_y = m[1][0] / a;
			angle_z = Math.atan2(tr_y, tr_x);
			// Angle y
			tr_x = m[2][2] / a;
			tr_y = -m[0][2] / a;
			angle_y = Math.atan2(tr_y, tr_x);
			v[0] = Math.toDegrees(angle_x);
			v[1] = -Math.toDegrees(angle_y);
			v[2] = -Math.toDegrees(angle_z);
		}
		else
		{ // Gimbal Lock
			angle_y = 0;
			// Angle z
			tr_x = m[0][0];
			tr_y = m[2][0];
			angle_z = Math.atan2(tr_y, tr_x);
			v[0] = 270;
			v[1] = 0;
			v[2] = Math.toDegrees(angle_z);
		}
		
		for (int i = 0; i < v.length; i++)
		{
			if (v[i] < 0)
				v[i] += 360;
		}
		return v;
	}
}