]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Point4f.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Point4f.java
1 /*
2  * $RCSfile: Point4f.java,v $
3  *
4  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This code is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 only, as
9  * published by the Free Software Foundation.  Sun designates this
10  * particular file as subject to the "Classpath" exception as provided
11  * by Sun in the LICENSE file that accompanied this code.
12  *
13  * This code is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  * version 2 for more details (a copy is included in the LICENSE file that
17  * accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License version
20  * 2 along with this work; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  *
27  * $Revision: 1.6 $
28  * $Date: 2008/02/28 20:18:50 $
29  * $State: Exp $
30  */
31
32 package javax.vecmath;
33
34 import java.lang.Math;
35
36 /**
37  * A 4 element point represented by single precision floating point x,y,z,w 
38  * coordinates.
39  *
40  */
41 public class Point4f extends Tuple4f implements java.io.Serializable {
42
43
44     // Compatible with 1.1
45     static final long serialVersionUID = 4643134103185764459L;
46
47   /**
48    * Constructs and initializes a Point4f from the specified xyzw coordinates.
49    * @param x the x coordinate
50    * @param y the y coordinate
51    * @param z the z coordinate
52    * @param w the w coordinate
53    */
54   public Point4f(float x, float y, float z, float w)
55   {
56        super(x,y,z,w);
57   }
58
59
60   /**
61    * Constructs and initializes a Point4f from the array of length 4. 
62    * @param p the array of length 4 containing xyzw in order
63    */
64   public Point4f(float[] p)
65   {
66      super(p);
67   }
68
69
70   /**
71    * Constructs and initializes a Point4f from the specified Point4f.
72    * @param p1 the Point4f containing the initialization x y z w data
73    */
74   public Point4f(Point4f p1)
75   {
76       super(p1);
77   }
78
79
80   /**
81    * Constructs and initializes a Point4f from the specified Point4d.
82    * @param p1 the Point4d containing the initialization x y z w data
83    */
84   public Point4f(Point4d p1)
85   {
86       super(p1);
87   }
88
89
90     /** 
91      * Constructs and initializes a Point4f from the specified Tuple4f. 
92      * @param t1 the Tuple4f containing the initialization x y z w data 
93      */  
94     public Point4f(Tuple4f t1)  
95     { 
96        super(t1); 
97     }
98  
99  
100     /** 
101      * Constructs and initializes a Point4f from the specified Tuple4d.  
102      * @param t1 the Tuple4d containing the initialization x y z w data 
103      */  
104     public Point4f(Tuple4d t1)
105     {
106        super(t1);
107     }
108  
109
110     /**
111      * Constructs and initializes a Point4f from the specified Tuple3f.
112      * The x,y,z components of this point are set to the corresponding
113      * components of tuple t1.  The w component of this point
114      * is set to 1.
115      * @param t1 the tuple to be copied
116      *
117      * @since vecmath 1.2
118      */
119     public Point4f(Tuple3f t1) {
120         super(t1.x, t1.y, t1.z, 1.0f);
121     }
122
123
124   /**
125    * Constructs and initializes a Point4f to (0,0,0,0).
126    */
127   public Point4f()
128   {
129       super();
130   }
131
132
133     /**
134      * Sets the x,y,z components of this point to the corresponding
135      * components of tuple t1.  The w component of this point
136      * is set to 1.
137      * @param t1 the tuple to be copied
138      *
139      * @since vecmath 1.2
140      */
141     public final void set(Tuple3f t1) {
142         this.x = t1.x;
143         this.y = t1.y;
144         this.z = t1.z;
145         this.w = 1.0f;
146     }
147
148
149  /**
150    * Computes the square of the distance between this point and point p1.
151    * @param p1 the other point
152    * @return the square of distance between these two points as a float
153    */
154   public final float distanceSquared(Point4f p1)
155     {
156       float dx, dy, dz, dw;
157
158       dx = this.x-p1.x;
159       dy = this.y-p1.y;
160       dz = this.z-p1.z;
161       dw = this.w-p1.w;
162       return (dx*dx+dy*dy+dz*dz+dw*dw);
163     }
164
165
166   /**
167    * Computes the distance between this point and point p1.
168    * @param p1 the other point
169    * @return the distance between the two points
170    */
171   public final float distance(Point4f p1)
172     {
173       float dx, dy, dz, dw;
174
175       dx = this.x-p1.x;
176       dy = this.y-p1.y;
177       dz = this.z-p1.z;
178       dw = this.w-p1.w;
179       return (float) Math.sqrt(dx*dx+dy*dy+dz*dz+dw*dw);
180     }
181
182
183   /**
184     * Computes the L-1 (Manhattan) distance between this point and
185     * point p1.  The L-1 distance is equal to:
186     *  abs(x1-x2) + abs(y1-y2) + abs(z1-z2) + abs(w1-w2).
187     * @param p1 the other point
188     * @return  the L-1 distance
189     */
190   public final float distanceL1(Point4f p1)
191     {
192        return( Math.abs(this.x-p1.x) + Math.abs(this.y-p1.y) + Math.abs(this.z-p1.z) + Math.abs(this.w-p1.w));
193     }
194
195
196   /**
197     * Computes the L-infinite distance between this point and
198     * point p1.  The L-infinite distance is equal to
199     * MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)].
200     * @param p1 the other point
201     * @return  the L-infinite distance
202     */
203   public final float distanceLinf(Point4f p1)
204     {
205        float t1, t2;
206        t1 = Math.max( Math.abs(this.x-p1.x), Math.abs(this.y-p1.y));
207        t2 = Math.max( Math.abs(this.z-p1.z), Math.abs(this.w-p1.w));
208  
209        return(Math.max(t1,t2));
210
211     }
212
213   /**
214     *  Multiplies each of the x,y,z components of the Point4f parameter 
215     *  by 1/w, places the projected values into this point, and places
216     *  a 1 as the w parameter of this point. 
217     *  @param  p1  the source Point4f, which is not modified 
218     */   
219    public final void project(Point4f p1)
220    { 
221      float oneOw;
222
223      oneOw = 1/p1.w;
224      x = p1.x*oneOw;
225      y = p1.y*oneOw;
226      z = p1.z*oneOw;
227      w = 1.0f;
228
229    } 
230
231 }