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 Tuple3f {
023        public float x, y, z;
024
025        public Tuple3f() {
026                this( 0, 0, 0 );
027        }
028        
029        public Tuple3f( float[] x ) {
030                this.x = x[0];
031                this.y = x[1];
032                this.z = x[2];
033        }
034
035        public Tuple3f( float x, float y, float z ) {
036                this.x = x;
037                this.y = y;
038                this.z = z;
039        }
040
041        public Tuple3f( Tuple3f t ) {
042                this.x = t.x;
043                this.y = t.y;
044                this.z = t.z;
045        }
046
047        public void absolute() {
048                x = Math.abs(x);
049                y = Math.abs(y);
050                z = Math.abs(z);
051        }
052
053        public void absolute( Tuple3f t ) {
054                x = Math.abs(t.x);
055                y = Math.abs(t.y);
056                z = Math.abs(t.z);
057        }
058
059        public void clamp( float min, float max ) {
060                if ( x < min )
061                        x = min;
062                else if ( x > max )
063                        x = max;
064                if ( y < min )
065                        y = min;
066                else if ( y > max )
067                        y = max;
068                if ( z < min )
069                        z = min;
070                else if ( z > max )
071                        z = max;
072        }
073
074        public void set( float x, float y, float z ) {
075                this.x = x;
076                this.y = y;
077                this.z = z;
078        }
079
080        public void set( float[] x ) {
081                this.x = x[0];
082                this.y = x[1];
083                this.z = x[2];
084        }
085
086        public void set( Tuple3f t ) {
087                x = t.x;
088                y = t.y;
089                z = t.z;
090        }
091
092        public void get( Tuple3f t ) {
093                t.x = x;
094                t.y = y;
095                t.z = z;
096        }
097
098        public void get( float[] t ) {
099                t[0] = x;
100                t[1] = y;
101                t[2] = z;
102        }
103
104        public void negate() {
105                x = -x;
106                y = -y;
107                z = -z;
108        }
109
110        public void negate( Tuple3f t ) {
111                x = -t.x;
112                y = -t.y;
113                z = -t.z;
114        }
115
116        public void interpolate( Tuple3f t, float alpha ) {
117                float a = 1-alpha;
118                x = a*x + alpha*t.x;
119                y = a*y + alpha*t.y;
120                z = a*z + alpha*t.z;
121        }
122
123        public void scale( float s ) {
124                x *= s;
125                y *= s;
126                z *= s;
127        }
128
129        public void add( Tuple3f t ) {
130                x += t.x;
131                y += t.y;
132                z += t.z;
133        }
134
135        public void add( Tuple3f t1, Tuple3f t2 ) {
136                x = t1.x+t2.x;
137                y = t1.y+t2.y;
138                z = t1.z+t2.z;
139        }
140
141        public void sub( Tuple3f t ) {
142                x -= t.x;
143                y -= t.y;
144                z -= t.z;
145        }
146
147        public void sub( Tuple3f t1, Tuple3f t2 ) {
148                x = t1.x-t2.x;
149                y = t1.y-t2.y;
150                z = t1.z-t2.z;
151        }
152
153        public void scaleAdd( float s, Tuple3f t ) {
154                x += s*t.x;
155                y += s*t.y;
156                z += s*t.z;
157        }
158        
159        public void scaleAdd( float s, Tuple3f t1, Tuple3f t2 ) {
160                x = s*t1.x + t2.x;
161                y = s*t1.y + t2.y;
162                z = s*t1.z + t2.z;
163        }
164        
165        public String toString() {
166                return "["+x+", "+y+", "+z+"]";
167        }
168        
169}