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 Tuple4f {
041        public float x, y, z, w;
042
043        public Tuple4f() {
044                this( 0, 0, 0, 0 );
045        }
046        
047        public Tuple4f( float[] x ) {
048                this.x = x[0];
049                this.y = x[1];
050                this.z = x[2];
051                this.w = x[2];
052        }
053
054        public Tuple4f( float x, float y, float z, float w ) {
055                this.x = x;
056                this.y = y;
057                this.z = z;
058                this.w = w;
059        }
060
061        public Tuple4f( Tuple4f t ) {
062                this.x = t.x;
063                this.y = t.y;
064                this.z = t.z;
065                this.w = t.w;
066        }
067
068        public void absolute() {
069                x = Math.abs(x);
070                y = Math.abs(y);
071                z = Math.abs(z);
072                w = Math.abs(w);
073        }
074
075        public void absolute( Tuple4f t ) {
076                x = Math.abs(t.x);
077                y = Math.abs(t.y);
078                z = Math.abs(t.z);
079                w = Math.abs(t.w);
080        }
081
082        public void clamp( float min, float max ) {
083                if ( x < min )
084                        x = min;
085                else if ( x > max )
086                        x = max;
087                if ( y < min )
088                        y = min;
089                else if ( y > max )
090                        y = max;
091                if ( z < min )
092                        z = min;
093                else if ( z > max )
094                        z = max;
095                if ( w < min )
096                        w = min;
097                else if ( w > max )
098                        w = max;
099        }
100
101        public void set( float x, float y, float z, float w ) {
102                this.x = x;
103                this.y = y;
104                this.z = z;
105                this.w = w;
106        }
107
108        public void set( float[] x ) {
109                this.x = x[0];
110                this.y = x[1];
111                this.z = x[2];
112                this.w = x[2];
113        }
114
115        public void set( Tuple4f t ) {
116                x = t.x;
117                y = t.y;
118                z = t.z;
119                w = t.w;
120        }
121
122        public void get( Tuple4f t ) {
123                t.x = x;
124                t.y = y;
125                t.z = z;
126                t.w = w;
127        }
128
129        public void get( float[] t ) {
130                t[0] = x;
131                t[1] = y;
132                t[2] = z;
133                t[3] = w;
134        }
135
136        public void negate() {
137                x = -x;
138                y = -y;
139                z = -z;
140                w = -w;
141        }
142
143        public void negate( Tuple4f t ) {
144                x = -t.x;
145                y = -t.y;
146                z = -t.z;
147                w = -t.w;
148        }
149
150        public void interpolate( Tuple4f t, float alpha ) {
151                float a = 1-alpha;
152                x = a*x + alpha*t.x;
153                y = a*y + alpha*t.y;
154                z = a*z + alpha*t.z;
155                w = a*w + alpha*t.w;
156        }
157
158        public void scale( float s ) {
159                x *= s;
160                y *= s;
161                z *= s;
162                w *= s;
163        }
164
165        public void add( Tuple4f t ) {
166                x += t.x;
167                y += t.y;
168                z += t.z;
169                w += t.w;
170        }
171
172        public void add( Tuple4f t1, Tuple4f t2 ) {
173                x = t1.x+t2.x;
174                y = t1.y+t2.y;
175                z = t1.z+t2.z;
176                w = t1.w+t2.w;
177        }
178
179        public void sub( Tuple4f t ) {
180                x -= t.x;
181                y -= t.y;
182                z -= t.z;
183                w -= t.w;
184        }
185
186        public void sub( Tuple4f t1, Tuple4f t2 ) {
187                x = t1.x-t2.x;
188                y = t1.y-t2.y;
189                z = t1.z-t2.z;
190                w = t1.w-t2.w;
191        }
192
193        public String toString() {
194                return "["+x+", "+y+", "+z+", "+w+"]";
195        }
196        
197}