001    /*
002    *
003    
004    Licensed under the Apache License, Version 2.0 (the "License");
005    you may not use this file except in compliance with the License.
006    You may obtain a copy of the License at
007    
008       http://www.apache.org/licenses/LICENSE-2.0
009    
010    Unless required by applicable law or agreed to in writing, software
011    distributed under the License is distributed on an "AS IS" BASIS,
012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    See the License for the specific language governing permissions and
014    limitations under the License.
015    */
016    
017    package railo.runtime.img.vecmath;
018    
019    /**
020     * Vector math package, converted to look similar to javax.vecmath.
021     */
022    public class Vector3f extends Tuple3f {
023    
024            public Vector3f() {
025                    this( 0, 0, 0 );
026            }
027            
028            public Vector3f( float[] x ) {
029                    this.x = x[0];
030                    this.y = x[1];
031                    this.z = x[2];
032            }
033    
034            public Vector3f( float x, float y, float z ) {
035                    this.x = x;
036                    this.y = y;
037                    this.z = z;
038            }
039    
040            public Vector3f( Vector3f t ) {
041                    this.x = t.x;
042                    this.y = t.y;
043                    this.z = t.z;
044            }
045    
046            public Vector3f( Tuple3f t ) {
047                    this.x = t.x;
048                    this.y = t.y;
049                    this.z = t.z;
050            }
051    
052            public float angle( Vector3f v ) {
053                    return (float)Math.acos( dot(v) / (length()*v.length()) );
054            }
055    
056            public float dot( Vector3f v ) {
057                    return v.x * x + v.y * y + v.z * z;
058            }
059    
060            public void cross( Vector3f v1, Vector3f v2 ) {
061                    x = v1.y * v2.z - v1.z * v2.y;
062                    y = v1.z * v2.x - v1.x * v2.z;
063                    z = v1.x * v2.y - v1.y * v2.x;
064            }
065    
066            public float length() {
067                    return (float)Math.sqrt( x*x+y*y+z*z );
068            }
069    
070            public void normalize() {
071                    float d = 1.0f/(float)Math.sqrt( x*x+y*y+z*z );
072                    x *= d;
073                    y *= d;
074                    z *= d;
075            }
076    
077    }