001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.vecmath;
018
019/**
020 * Vector math package, converted to look similar to javax.vecmath.
021 */
022public class Tuple4f {
023        public float x, y, z, w;
024
025        public Tuple4f() {
026                this( 0, 0, 0, 0 );
027        }
028        
029        public Tuple4f( float[] x ) {
030                this.x = x[0];
031                this.y = x[1];
032                this.z = x[2];
033                this.w = x[2];
034        }
035
036        public Tuple4f( float x, float y, float z, float w ) {
037                this.x = x;
038                this.y = y;
039                this.z = z;
040                this.w = w;
041        }
042
043        public Tuple4f( Tuple4f t ) {
044                this.x = t.x;
045                this.y = t.y;
046                this.z = t.z;
047                this.w = t.w;
048        }
049
050        public void absolute() {
051                x = Math.abs(x);
052                y = Math.abs(y);
053                z = Math.abs(z);
054                w = Math.abs(w);
055        }
056
057        public void absolute( Tuple4f t ) {
058                x = Math.abs(t.x);
059                y = Math.abs(t.y);
060                z = Math.abs(t.z);
061                w = Math.abs(t.w);
062        }
063
064        public void clamp( float min, float max ) {
065                if ( x < min )
066                        x = min;
067                else if ( x > max )
068                        x = max;
069                if ( y < min )
070                        y = min;
071                else if ( y > max )
072                        y = max;
073                if ( z < min )
074                        z = min;
075                else if ( z > max )
076                        z = max;
077                if ( w < min )
078                        w = min;
079                else if ( w > max )
080                        w = max;
081        }
082
083        public void set( float x, float y, float z, float w ) {
084                this.x = x;
085                this.y = y;
086                this.z = z;
087                this.w = w;
088        }
089
090        public void set( float[] x ) {
091                this.x = x[0];
092                this.y = x[1];
093                this.z = x[2];
094                this.w = x[2];
095        }
096
097        public void set( Tuple4f t ) {
098                x = t.x;
099                y = t.y;
100                z = t.z;
101                w = t.w;
102        }
103
104        public void get( Tuple4f t ) {
105                t.x = x;
106                t.y = y;
107                t.z = z;
108                t.w = w;
109        }
110
111        public void get( float[] t ) {
112                t[0] = x;
113                t[1] = y;
114                t[2] = z;
115                t[3] = w;
116        }
117
118        public void negate() {
119                x = -x;
120                y = -y;
121                z = -z;
122                w = -w;
123        }
124
125        public void negate( Tuple4f t ) {
126                x = -t.x;
127                y = -t.y;
128                z = -t.z;
129                w = -t.w;
130        }
131
132        public void interpolate( Tuple4f t, float alpha ) {
133                float a = 1-alpha;
134                x = a*x + alpha*t.x;
135                y = a*y + alpha*t.y;
136                z = a*z + alpha*t.z;
137                w = a*w + alpha*t.w;
138        }
139
140        public void scale( float s ) {
141                x *= s;
142                y *= s;
143                z *= s;
144                w *= s;
145        }
146
147        public void add( Tuple4f t ) {
148                x += t.x;
149                y += t.y;
150                z += t.z;
151                w += t.w;
152        }
153
154        public void add( Tuple4f t1, Tuple4f t2 ) {
155                x = t1.x+t2.x;
156                y = t1.y+t2.y;
157                z = t1.z+t2.z;
158                w = t1.w+t2.w;
159        }
160
161        public void sub( Tuple4f t ) {
162                x -= t.x;
163                y -= t.y;
164                z -= t.z;
165                w -= t.w;
166        }
167
168        public void sub( Tuple4f t1, Tuple4f t2 ) {
169                x = t1.x-t2.x;
170                y = t1.y-t2.y;
171                z = t1.z-t2.z;
172                w = t1.w-t2.w;
173        }
174
175        public String toString() {
176                return "["+x+", "+y+", "+z+", "+w+"]";
177        }
178        
179}