Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / geom / PolarPoint.java
blob5c4b14f4351a45978566e15e134222c015f15171
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.geom;
9 import gov.nasa.worldwind.*;
11 /**
12 * Represents a point in space defined by a latitude, longitude and distance from the origin.
13 * <p/>
14 * Instances of <code>PolarPoint</code> are immutable.
16 * @author Tom Gaskins
17 * @version $Id: PolarPoint.java 1754 2007-05-06 23:19:22Z tgaskins $
19 public class PolarPoint
21 public static final PolarPoint ZERO = new PolarPoint(Angle.ZERO, Angle.ZERO, 0d);
23 private final Angle latitude;
24 private final Angle longitude;
25 private final double radius;
27 /**
28 * Obtains a <code>PolarPoint</code> from radians and a radius.
30 * @param latitude the latitude in radians
31 * @param longitude the longitude in radians
32 * @param radius the distance form the center
33 * @return a new <code>PolarPoint</code>
35 public static PolarPoint fromRadians(double latitude, double longitude, double radius)
37 return new PolarPoint(Angle.fromRadians(latitude), Angle.fromRadians(longitude), radius);
40 /**
41 * Obtains a <code>PolarPoint</code> from degrees and a radius.
43 * @param latitude the latitude in degrees
44 * @param longitude the longitude in degrees
45 * @param radius the distance form the center
46 * @return a new <code>PolarPoint</code>
48 public static PolarPoint fromDegrees(double latitude, double longitude, double radius)
50 return new PolarPoint(Angle.fromDegrees(latitude), Angle.fromDegrees(longitude), radius);
53 /**
54 * Obtains a <code>PolarPoint</code> from a cartesian point.
56 * @param cartesianPoint the point to convert
57 * @return the cartesian point expressed as a polar point
58 * @throws IllegalArgumentException if <code>cartesianPoint</code> is null
60 public static PolarPoint fromCartesian(Point cartesianPoint)
62 if (cartesianPoint == null)
64 String message = WorldWind.retrieveErrMsg("nullValue.PointIsNull");
65 WorldWind.logger().log(java.util.logging.Level.FINE, message);
66 throw new IllegalArgumentException(message);
69 return PolarPoint.fromCartesian(cartesianPoint.x(), cartesianPoint.y(), cartesianPoint.z());
72 /**
73 * Obtains a <code>PolarPoint</code> from cartesian coordinates.
75 * @param x the x coordinate of the cartesian point
76 * @param y the y coordinate of the cartesian point
77 * @param z the z coordinate of the cartesian point
78 * @return a polar point located at (x,y,z) in cartesian space
80 public static PolarPoint fromCartesian(double x, double y, double z)
82 double radius = Math.sqrt(x * x + y * y + z * z);
83 double latRads = Math.atan2(y, Math.sqrt(x * x + z * z));
84 double lonRads = Math.atan2(x, z);
85 return PolarPoint.fromRadians(latRads, lonRads, radius);
88 /**
89 * Obtains a <code>PolarPoint</code> from two <code>angles</code> and a radius.
91 * @param latitude the latitude
92 * @param longitude the longitude
93 * @param radius the distance from the center
94 * @throws IllegalArgumentException if <code>latitude</code> or <code>longitude</code> is null
96 public PolarPoint(Angle latitude, Angle longitude, double radius)
98 if (latitude == null || longitude == null)
100 String message = WorldWind.retrieveErrMsg("nullValue.LatitudeOrLongitudeIsNull");
101 WorldWind.logger().log(java.util.logging.Level.FINE, message);
102 throw new IllegalArgumentException(message);
105 this.latitude = latitude;
106 this.longitude = longitude;
107 this.radius = radius;
111 * Obtains the latitude of this polar point
113 * @return this polar point's latitude
115 public final Angle getLatitude()
117 return this.latitude;
121 * Obtains the longitude of this polar point
123 * @return this polar point's longitude
125 public final Angle getLongitude()
127 return this.longitude;
131 * Obtains the radius of this polar point
133 * @return the distance from this polar point to its origin
135 public final double getRadius()
137 return radius;
141 * Obtains a cartesian point equivalent to this <code>PolarPoint</code>, except in cartesian space.
143 * @return this polar point in cartesian coordinates
145 public final Point toCartesian()
147 return toCartesian(this.latitude, this.longitude, this.radius);
151 * Obtains a cartesian point from a given latitude, longitude and distance from center. This method is equivalent
152 * to, but may perform faster than <code>Point p = new PolarPoint(latitude, longitude, radius).toCartesian()</code>
154 * @param latitude the latitude
155 * @param longitude the longitude
156 * @param radius the distance from the origin
157 * @return a cartesian point from two angles and a radius
158 * @throws IllegalArgumentException if <code>latitude</code> or <code>longitude</code> is null
160 public static Point toCartesian(Angle latitude, Angle longitude, double radius)
162 if (latitude == null || longitude == null)
164 String message = WorldWind.retrieveErrMsg("nullValue.LatitudeOrLongitudeIsNull");
165 WorldWind.logger().log(java.util.logging.Level.FINE, message);
166 throw new IllegalArgumentException(message);
169 double x = radius * longitude.sin() * latitude.cos();
170 double y = radius * latitude.sin();
171 double z = radius * longitude.cos() * latitude.cos();
172 return new Point(x, y, z);
175 @Override
176 public boolean equals(Object o)
178 if (this == o)
179 return true;
180 if (o == null || getClass() != o.getClass())
181 return false;
183 final gov.nasa.worldwind.geom.PolarPoint that = (gov.nasa.worldwind.geom.PolarPoint) o;
185 if (Double.compare(that.radius, radius) != 0)
186 return false;
187 if (!latitude.equals(that.latitude))
188 return false;
189 //noinspection RedundantIfStatement
190 if (!longitude.equals(that.longitude))
191 return false;
193 return true;
196 @Override
197 public int hashCode()
199 int result;
200 long temp;
201 result = latitude.hashCode();
202 result = 29 * result + longitude.hashCode();
203 temp = radius != +0.0d ? Double.doubleToLongBits(radius) : 0L;
204 result = 29 * result + (int) (temp ^ (temp >>> 32));
205 return result;
208 @Override
209 public String toString()
211 return "(lat: " + this.latitude.toString() + ", lon: " + this.longitude.toString() + ", r: " + this.radius
212 + ")";