]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Point4d.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Point4d.java
1 /*
2  * $RCSfile: Point4d.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 vector represented by double precision floating point 
38  * x,y,z,w coordinates.
39  *
40  */
41 public class Point4d extends Tuple4d implements java.io.Serializable {
42
43     // Compatible with 1.1
44     static final long serialVersionUID = 1733471895962736949L;
45
46
47     /**
48      * Constructs and initializes a Point4d 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 Point4d(double x, double y, double z, double w)
55     {
56         super(x,y,z,w);
57     }
58
59     /**
60      * Constructs and initializes a Point4d from the coordinates contained
61      * in the array.
62      * @param p the array of length 4 containing xyzw in order
63      */
64     public Point4d(double[] p)
65     {
66         super(p);
67     }
68
69
70     /**
71      * Constructs and initializes a Point4d from the specified Point4d.
72      * @param p1 the Point4d containing the initialization x y z w data
73      */
74     public Point4d(Point4d p1)
75     {
76          super(p1);
77     }
78
79
80     /**
81      * Constructs and initializes a Point4d from the specified Point4f.
82      * @param p1 the Point4f containing the initialization x y z w data
83      */
84     public Point4d(Point4f p1)
85     {
86        super(p1);
87     }
88
89
90     /**  
91      * Constructs and initializes a Point4d from the specified Tuple4f.  
92      * @param t1 the Tuple4f containing the initialization x y z w data  
93      */  
94     public Point4d(Tuple4f t1)  
95     {    
96        super(t1);  
97     } 
98   
99   
100     /**  
101      * Constructs and initializes a Point4d from the specified Tuple4d.  
102      * @param t1 the Tuple4d containing the initialization x y z w data  
103      */   
104     public Point4d(Tuple4d t1) 
105     { 
106        super(t1); 
107     } 
108   
109
110     /**
111      * Constructs and initializes a Point4d from the specified Tuple3d.
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 Point4d(Tuple3d t1) {
120         super(t1.x, t1.y, t1.z, 1.0);
121     }
122
123
124     /**
125      * Constructs and initializes a Point4d to (0,0,0,0).
126      */
127     public Point4d()
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(Tuple3d t1) {
142         this.x = t1.x;
143         this.y = t1.y;
144         this.z = t1.z;
145         this.w = 1.0;
146     }
147
148
149  /**
150    * Returns the square of the distance between this point and point p1.
151    * @param p1 the first point
152    * @return the square of distance between this point and point p1
153    */
154     public final double distanceSquared(Point4d p1)
155     {
156       double 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    * Returns the distance between this point and point p1.
168    * @param p1 the first point
169    * @return the distance between these this point and point p1.
170    */
171     public final double distance(Point4d p1)
172     {
173       double 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 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 double distanceL1(Point4d p1) {
191         return Math.abs(this.x-p1.x) + Math.abs(this.y-p1.y) +
192             Math.abs(this.z-p1.z) + Math.abs(this.w-p1.w);
193     }
194
195     /**
196      * Computes the L-infinite distance between this point and
197      * point p1.  The L-infinite distance is equal to
198      * MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)].
199      * @param p1 the other point
200      * @return  the L-infinite distance
201      */
202     public final double distanceLinf(Point4d p1) {
203         double t1, t2;
204         t1 = Math.max( Math.abs(this.x-p1.x), Math.abs(this.y-p1.y));
205         t2 = Math.max( Math.abs(this.z-p1.z), Math.abs(this.w-p1.w));
206  
207         return Math.max(t1,t2);
208     }
209
210   /**
211     *  Multiplies each of the x,y,z components of the Point4d parameter 
212     *  by 1/w, places the projected values into this point, and places
213     *  a 1 as the w parameter of this point. 
214     *  @param  p1  the source Point4d, which is not modified 
215     */   
216    public final void project(Point4d p1)
217    { 
218      double oneOw;
219
220      oneOw = 1/p1.w;
221      x = p1.x*oneOw;
222      y = p1.y*oneOw;
223      z = p1.z*oneOw;
224      w = 1.0;
225
226    } 
227
228
229 }