001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/*
020*
021
022Licensed under the Apache License, Version 2.0 (the "License");
023you may not use this file except in compliance with the License.
024You may obtain a copy of the License at
025
026   http://www.apache.org/licenses/LICENSE-2.0
027
028Unless required by applicable law or agreed to in writing, software
029distributed under the License is distributed on an "AS IS" BASIS,
030WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
031See the License for the specific language governing permissions and
032limitations under the License.
033*/
034
035package lucee.runtime.img.vecmath;
036
037/**
038 * Vector math package, converted to look similar to javax.vecmath.
039 */
040public class Matrix4f {
041        public float m00, m01, m02, m03;
042        public float m10, m11, m12, m13;
043        public float m20, m21, m22, m23;
044        public float m30, m31, m32, m33;
045
046        public Matrix4f() {
047                setIdentity();
048        }
049
050        public Matrix4f( Matrix4f m ) {
051                set( m );
052        }
053
054        public Matrix4f(float[] m) {
055                set( m );
056        }
057
058        public void set( Matrix4f m) {
059                m00 = m.m00;
060                m01 = m.m01;
061                m02 = m.m02;
062                m03 = m.m03;
063                m10 = m.m10;
064                m11 = m.m11;
065                m12 = m.m12;
066                m13 = m.m13;
067                m20 = m.m20;
068                m21 = m.m21;
069                m22 = m.m22;
070                m23 = m.m23;
071                m30 = m.m30;
072                m31 = m.m31;
073                m32 = m.m32;
074                m33 = m.m33;
075        }
076
077        public void set(float[] m) {
078                m00 = m[0];
079                m01 = m[1];
080                m02 = m[2];
081                m03 = m[3];
082                m10 = m[4];
083                m11 = m[5];
084                m12 = m[6];
085                m13 = m[7];
086                m20 = m[8];
087                m21 = m[9];
088                m22 = m[10];
089                m23 = m[11];
090                m30 = m[12];
091                m31 = m[13];
092                m32 = m[14];
093                m33 = m[15];
094        }
095
096        public void get( Matrix4f m) {
097                m.m00 = m00;
098                m.m01 = m01;
099                m.m02 = m02;
100                m.m03 = m03;
101                m.m10 = m10;
102                m.m11 = m11;
103                m.m12 = m12;
104                m.m13 = m13;
105                m.m20 = m20;
106                m.m21 = m21;
107                m.m22 = m22;
108                m.m23 = m23;
109                m.m30 = m30;
110                m.m31 = m31;
111                m.m32 = m32;
112                m.m33 = m33;
113        }
114
115        public void get(float[] m) {
116                m[0] = m00;
117                m[1] = m01;
118                m[2] = m02;
119                m[3] = m03;
120                m[4] = m10;
121                m[5] = m11;
122                m[6] = m12;
123                m[7] = m13;
124                m[8] = m20;
125                m[9] = m21;
126                m[10] = m22;
127                m[11] = m23;
128                m[12] = m30;
129                m[13] = m31;
130                m[14] = m32;
131                m[15] = m33;
132        }
133
134        public void setIdentity() {
135                m00 = 1.0f;
136                m01 = 0.0f;
137                m02 = 0.0f;
138                m03 = 0.0f;
139
140                m10 = 0.0f;
141                m11 = 1.0f;
142                m12 = 0.0f;
143                m13 = 0.0f;
144
145                m20 = 0.0f;
146                m21 = 0.0f;
147                m22 = 1.0f;
148                m23 = 0.0f;
149
150                m30 = 0.0f;
151                m31 = 0.0f;
152                m32 = 0.0f;
153                m33 = 1.0f;
154        }
155
156        public void mul( Matrix4f m ) {
157                float tm00 = m00;
158                float tm01 = m01;
159                float tm02 = m02;
160                float tm03 = m03;
161                float tm10 = m10;
162                float tm11 = m11;
163                float tm12 = m12;
164                float tm13 = m13;
165                float tm20 = m20;
166                float tm21 = m21;
167                float tm22 = m22;
168                float tm23 = m23;
169                float tm30 = m30;
170                float tm31 = m31;
171                float tm32 = m32;
172                float tm33 = m33;
173
174                m00 = tm00*m.m00 + tm10*m.m01 + tm20*m.m02 + tm30*m.m03;
175                m01 = tm01*m.m00 + tm11*m.m01 + tm21*m.m02 + tm31*m.m03;
176                m02 = tm02*m.m00 + tm12*m.m01 + tm22*m.m02 + tm32*m.m03;
177                m03 = tm03*m.m00 + tm13*m.m01 + tm23*m.m02 + tm33*m.m03;
178                m10 = tm00*m.m10 + tm10*m.m11 + tm20*m.m12 + tm30*m.m13;
179                m11 = tm01*m.m10 + tm11*m.m11 + tm21*m.m12 + tm31*m.m13;
180                m12 = tm02*m.m10 + tm12*m.m11 + tm22*m.m12 + tm32*m.m13;
181                m13 = tm03*m.m10 + tm13*m.m11 + tm23*m.m12 + tm33*m.m13;
182                m20 = tm00*m.m20 + tm10*m.m21 + tm20*m.m22 + tm30*m.m23;
183                m21 = tm01*m.m20 + tm11*m.m21 + tm21*m.m22 + tm31*m.m23;
184                m22 = tm02*m.m20 + tm12*m.m21 + tm22*m.m22 + tm32*m.m23;
185                m23 = tm03*m.m20 + tm13*m.m21 + tm23*m.m22 + tm33*m.m23;
186                m30 = tm00*m.m30 + tm10*m.m31 + tm20*m.m32 + tm30*m.m33;
187                m31 = tm01*m.m30 + tm11*m.m31 + tm21*m.m32 + tm31*m.m33;
188                m32 = tm02*m.m30 + tm12*m.m31 + tm22*m.m32 + tm32*m.m33;
189                m33 = tm03*m.m30 + tm13*m.m31 + tm23*m.m32 + tm33*m.m33;
190        }
191
192        public void invert() {
193                Matrix4f t = new Matrix4f( this );
194                invert( t );
195        }
196
197        public void invert( Matrix4f t ) {
198                m00 = t.m00;
199                m01 = t.m10;
200                m02 = t.m20;
201                m03 = t.m03;
202
203                m10 = t.m01;
204                m11 = t.m11;
205                m12 = t.m21;
206                m13 = t.m13;
207
208                m20 = t.m02;
209                m21 = t.m12;
210                m22 = t.m22;
211                m23 = t.m23;
212
213                m30 *= -1.0f;
214                m31 *= -1.0f;
215                m32 *= -1.0f;
216                m33 = t.m33;
217        }
218
219        public void set( AxisAngle4f a ) {
220                float halfTheta = a.angle * 0.5f;
221                float cosHalfTheta = (float)Math.cos(halfTheta);
222                float sinHalfTheta = (float)Math.sin(halfTheta);
223                set( new Quat4f( a.x * sinHalfTheta, a.y * sinHalfTheta, a.z * sinHalfTheta, cosHalfTheta ) );
224        }
225
226        public void set( Quat4f q ) {
227                float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
228                x2 = q.x + q.x;
229                y2 = q.y + q.y;
230                z2 = q.z + q.z;
231                xx = q.x * x2;
232                xy = q.x * y2;
233                xz = q.x * z2;
234                yy = q.y * y2;
235                yz = q.y * z2;
236                zz = q.z * z2;
237                wx = q.w * x2;
238                wy = q.w * y2;
239                wz = q.w * z2;
240                m00 = 1.0f - (yy + zz);
241                m01 = xy - wz;
242                m02 = xz + wy;
243                m03 = 0.0f;
244                m10 = xy + wz;
245                m11 = 1.0f - (xx + zz);
246                m12 = yz - wx;
247                m13 = 0.0f;
248                m20 = xz - wy;
249                m21 = yz + wx;
250                m22 = 1.0f - (xx + yy);
251                m23 = 0.0f;
252                m30 = 0;
253                m31 = 0;
254                m32 = 0;
255                m33 = 1;
256        }
257
258        public void transform( Point3f v ) {
259                float x = v.x * m00 + v.y * m10 + v.z * m20 + m30;
260                float y = v.x * m01 + v.y * m11 + v.z * m21 + m31;
261                float z = v.x * m02 + v.y * m12 + v.z * m22 + m32;
262                v.x = x;
263                v.y = y;
264                v.z = z;
265        }
266
267        public void transform( Vector3f v ) {
268                float x = v.x * m00 + v.y * m10 + v.z * m20;
269                float y = v.x * m01 + v.y * m11 + v.z * m21;
270                float z = v.x * m02 + v.y * m12 + v.z * m22;
271                v.x = x;
272                v.y = y;
273                v.z = z;
274        }
275
276        public void setTranslation( Vector3f v ) {
277                m30 = v.x;
278                m31 = v.y;
279                m32 = v.z;
280        }
281
282        public void set( float scale ) {
283                m00 = scale;
284                m11 = scale;
285                m22 = scale;
286        }
287
288        public void rotX( float angle ) {
289                float s = (float)Math.sin( angle );
290                float c = (float)Math.cos( angle );
291                m00 = 1.0f;
292                m01 = 0.0f;
293                m02 = 0.0f;
294                m03 = 0.0f;
295
296                m10 = 0.0f;
297                m11 = c;
298                m12 = s;
299                m13 = 0.0f;
300
301                m20 = 0.0f;
302                m21 = -s;
303                m22 = c;
304                m23 = 0.0f;
305
306                m30 = 0.0f;
307                m31 = 0.0f;
308                m32 = 0.0f;
309                m33 = 1.0f;
310        }
311
312        public void rotY( float angle ) {
313                float s = (float)Math.sin( angle );
314                float c = (float)Math.cos( angle );
315                m00 = c;
316                m01 = 0.0f;
317                m02 = -s;
318                m03 = 0.0f;
319
320                m10 = 0.0f;
321                m11 = 1.0f;
322                m12 = 0.0f;
323                m13 = 0.0f;
324
325                m20 = s;
326                m21 = 0.0f;
327                m22 = c;
328                m23 = 0.0f;
329
330                m30 = 0.0f;
331                m31 = 0.0f;
332                m32 = 0.0f;
333                m33 = 1.0f;
334        }
335
336        public void rotZ( float angle ) {
337                float s = (float)Math.sin( angle );
338                float c = (float)Math.cos( angle );
339                m00 = c;
340                m01 = s;
341                m02 = 0.0f;
342                m03 = 0.0f;
343
344                m10 = -s;
345                m11 = c;
346                m12 = 0.0f;
347                m13 = 0.0f;
348
349                m20 = 0.0f;
350                m21 = 0.0f;
351                m22 = 1.0f;
352                m23 = 0.0f;
353
354                m30 = 0.0f;
355                m31 = 0.0f;
356                m32 = 0.0f;
357                m33 = 1.0f;
358        }
359/*
360        void rotate(float angle, float x, float y, float z) {
361                Matrix4f m = new Matrix4f();//FIXME
362                m.MatrixFromAxisAngle(Vector3f(x, y, z), angle);
363                Multiply(m);
364        }
365*/
366}