1
2
3
4
5
6 package nl.tudelft.simulation.language.d3;
7
8 import java.awt.geom.Point2D;
9
10 import javax.vecmath.Point3f;
11 import javax.vecmath.Tuple3d;
12 import javax.vecmath.Tuple3f;
13
14 /***
15 * The Point3D class with utilities to convert to point2D where the z-axis is neglected.
16 * <p>
17 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
18 * Netherlands.
19 * <p>
20 * See for project information <a
21 * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
22 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
23 * </a>, no warranty
24 *
25 * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:22 $
26 * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
27 */
28 public class CartesianPoint extends javax.vecmath.Point3d
29 {
30 /***
31 * constructs a new CartesianPoint.
32 *
33 * @param x
34 * x
35 * @param y
36 * y
37 * @param z
38 * z
39 */
40 public CartesianPoint(final double x, final double y, final double z)
41 {
42 super(x, y, z);
43 }
44
45 /***
46 * constructs a new CartesianPoint.
47 *
48 * @param xyz
49 * x,y,z
50 */
51 public CartesianPoint(final double[] xyz)
52 {
53 super(xyz);
54 }
55
56 /***
57 * constructs a new CartesianPoint.
58 *
59 * @param point
60 * point3d
61 */
62 public CartesianPoint(final javax.vecmath.Point3d point)
63 {
64 super(point);
65 }
66
67 /***
68 * constructs a new CartesianPoint.
69 *
70 * @param point
71 * point3d
72 */
73 public CartesianPoint(final Point3f point)
74 {
75 super(point);
76 }
77
78 /***
79 * constructs a new CartesianPoint.
80 *
81 * @param tuple
82 * tuple
83 */
84 public CartesianPoint(final Tuple3f tuple)
85 {
86 super(tuple);
87 }
88
89 /***
90 * constructs a new CartesianPoint.
91 *
92 * @param tuple
93 * point3d
94 */
95 public CartesianPoint(final Tuple3d tuple)
96 {
97 super(tuple);
98 }
99
100 /***
101 * constructs a new CartesianPoint.
102 *
103 * @param point2D
104 * a 2D point
105 */
106 public CartesianPoint(final Point2D point2D)
107 {
108 this(point2D.getX(), point2D.getY(), 0);
109 }
110
111 /***
112 * constructs a new CartesianPoint.
113 */
114 public CartesianPoint()
115 {
116 super();
117 }
118
119 /***
120 * returns the 2D representation of the point.
121 *
122 * @return Point2D the result
123 */
124 public final Point2D to2D()
125 {
126 return new Point2D.Double(this.x, this.y);
127 }
128
129 /***
130 * converts the point to a sperical point.
131 *
132 * @return the spherical point
133 */
134 public final SphericalPoint toCartesianPoint()
135 {
136 return CartesianPoint.toSphericalPoint(this);
137 }
138
139 /***
140 * converts a cartesian point to a sperical point.
141 *
142 * @param point
143 * the cartesian point
144 * @return the spherical point
145 */
146 public static SphericalPoint toSphericalPoint(final CartesianPoint point)
147 {
148 double rho = Math.sqrt(Math.pow(point.x, 2) + Math.pow(point.y, 2) + Math.pow(point.z, 2));
149 double s = Math.sqrt(Math.pow(point.x, 2) + Math.pow(point.y, 2));
150 double phi = Math.acos(point.z / rho);
151 double theta = Math.asin(point.y / s);
152 if (point.x >= 0)
153 {
154 theta = Math.PI - theta;
155 }
156 return new SphericalPoint(phi, rho, theta);
157 }
158 }