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 Quat4f extends Tuple4f {
041
042        public Quat4f() {
043                this( 0, 0, 0, 0 );
044        }
045        
046        public Quat4f( float[] x ) {
047                this.x = x[0];
048                this.y = x[1];
049                this.z = x[2];
050                this.w = x[3];
051        }
052
053        public Quat4f( float x, float y, float z, float w ) {
054                this.x = x;
055                this.y = y;
056                this.z = z;
057                this.w = w;
058        }
059
060        public Quat4f( Quat4f t ) {
061                this.x = t.x;
062                this.y = t.y;
063                this.z = t.z;
064                this.w = t.w;
065        }
066
067        public Quat4f( Tuple4f t ) {
068                this.x = t.x;
069                this.y = t.y;
070                this.z = t.z;
071                this.w = t.w;
072        }
073
074        public void set( AxisAngle4f a ) {
075                float halfTheta = a.angle * 0.5f;
076                float cosHalfTheta = (float)Math.cos(halfTheta);
077                float sinHalfTheta = (float)Math.sin(halfTheta);
078                x = a.x * sinHalfTheta;
079                y = a.y * sinHalfTheta;
080                z = a.z * sinHalfTheta;
081                w = cosHalfTheta;
082        }
083
084/*
085        public void EulerToQuaternion(float roll, float pitch, float yaw)
086        {
087                float cr, cp, cy, sr, sp, sy, cpcy, spsy;
088                cr = cos(roll/2);
089                cp = cos(pitch/2);
090                cy = cos(yaw/2);
091                sr = sin(roll/2);
092                sp = sin(pitch/2);
093                sy = sin(yaw/2);
094                cpcy = cp * cy;
095                spsy = sp * sy;
096                w = cr * cpcy + sr * spsy;
097                x = sr * cpcy - cr * spsy;
098                y = cr * sp * cy + sr * cp * sy;
099                z = cr * cp * sy - sr * sp * cy;
100        }
101*/
102
103        public void normalize() {
104                float d = 1.0f/( x*x+y*y+z*z+w*w );
105                x *= d;
106                y *= d;
107                z *= d;
108                w *= d;
109        }
110
111/*
112        public void mul( Quat4f q ) {
113                Quat4f q3 = new Quat4f();
114                Vector3f vectorq1 = new Vector3f( x, y, z );
115                Vector3f vectorq2 = new Vector3f( q.x, q.y, q.z );
116
117                Vector3f tempvec1 = new Vector3f( vectorq1 );
118                Vector3f tempvec2;
119                Vector3f tempvec3;
120                q3.w = (w*q.w) - tempvec1.dot(vectorq2);
121                tempvec1.cross(vectorq2);
122                tempvec2.x = w * q.x;
123                tempvec2.y = w * q.y;
124                tempvec2.z = w * q.z;
125                tempvec3.x = q.w * x;
126                tempvec3.y = q.w * y;
127                tempvec3.z = q.w * z;
128                q3.x = tempvec1.x + tempvec2.x + tempvec3.x;
129                q3.y = tempvec1.y + tempvec2.y + tempvec3.y;
130                q3.z = tempvec1.z + tempvec2.z + tempvec3.z;
131                set(q3);
132        }
133*/
134
135        public void set( Matrix4f m ) {
136                float s;
137                int i;
138
139                float tr = m.m00 + m.m11 + m.m22;
140
141                if (tr > 0.0) {
142                        s = (float)Math.sqrt(tr + 1.0f);
143                        w = s / 2.0f;
144                        s = 0.5f / s;
145                        x = (m.m12 - m.m21) * s;
146                        y = (m.m20 - m.m02) * s;
147                        z = (m.m01 - m.m10) * s;
148                } else {                
149                        i = 0;
150                        if ( m.m11 > m.m00 ) {
151                                i = 1;
152                                if ( m.m22 > m.m11 ) {
153                                        i = 2;
154                                } else {
155                                }
156                        } else {
157                                if ( m.m22 > m.m00 ) {
158                                        i = 2;
159                                } else {
160                                }
161                        }
162
163                        switch ( i ) {
164                        case 0:
165                                s = (float)Math.sqrt ((m.m00 - (m.m11 + m.m22)) + 1.0f);
166                                x = s * 0.5f;
167                                if (s != 0.0)
168                                        s = 0.5f / s;
169                                w = (m.m12 - m.m21) * s;
170                                y = (m.m01 + m.m10) * s;
171                                z = (m.m02 + m.m20) * s;
172                                break;
173                        case 1:
174                                s = (float)Math.sqrt ((m.m11 - (m.m22 + m.m00)) + 1.0f);
175                                y = s * 0.5f;
176                                if (s != 0.0)
177                                        s = 0.5f / s;
178                                w = (m.m20 - m.m02) * s;
179                                z = (m.m12 + m.m21) * s;
180                                x = (m.m10 + m.m01) * s;
181                                break;
182                        case 2:
183                                s = (float)Math.sqrt ((m.m00 - (m.m11 + m.m22)) + 1.0f);
184                                z = s * 0.5f;
185                                if (s != 0.0)
186                                        s = 0.5f / s;
187                                w = (m.m01 - m.m10) * s;
188                                x = (m.m20 + m.m02) * s;
189                                y = (m.m21 + m.m12) * s;
190                                break;
191                        }
192
193                }
194        }
195
196}