Debian Codegraph @

src/dr/geo/math/SphericalPolarCoordinates.java

raw symbols SCIP
1 /*
2 * SphericalPolarCoordinates.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.geo.math;
27
28 import dr.evolution.continuous.Contrastable;
29
30 public class SphericalPolarCoordinates implements Contrastable {
31
32 /**
33 * Create spherical polar coordinates from the given vector
34 */
35 public SphericalPolarCoordinates(Vector3D v) {
36 this(v, v.modulus());
37 }
38
39 /**
40 * Create spherical polar coordinates from the given vector
41 */
42 public SphericalPolarCoordinates(Vector3D v, double newRadius) {
43
44 radius = v.modulus();
45 theta = Math.acos(v.getZ()/radius);
46 phi = Math.atan(v.getY()/v.getX());
47 if (v.getY() < 0) phi += Math.PI;
48
49 radius = newRadius;
50 }
51
52 /**
53 * Create spherical polar coordinates from given latitude and longitude
54 */
55 public SphericalPolarCoordinates(double latitude, double longitude) {
56 this(latitude,longitude,VOLUMETRIC_RADIUS_OF_EARTH);
57 }
58
59 public SphericalPolarCoordinates(double latitude, double longitude, double radius) {
60
61 this.radius = radius;
62 theta = (90.0-latitude)*Math.PI/180.0;
63
64 if (longitude < 0) longitude += 360;
65
66 phi = longitude * Math.PI/180;
67 }
68
69 public Vector3D getCartesianCoordinates() {
70 double x = radius * Math.sin(theta) * Math.cos(phi);
71 double y = radius * Math.sin(theta) * Math.sin(phi);
72 double z = radius * Math.cos(theta);
73
74 return new Vector3D(x, y, z);
75 }
76
77 /**
78 * @return the latitude of these spherical polar coordinates in degrees.
79 */
80 public double getLatitude() {
81 return 90 - (theta * 180.0 / Math.PI);
82 }
83
84 /**
85 * @return the longitude of these spherical polar coordinates in degrees.
86 */
87 public double getLongitude() {
88 double degrees = phi * 180.0 / Math.PI;
89 if (degrees > 180.0) {
90 degrees -= 360;
91 }
92 return degrees;
93 }
94
95 /**
96 * @return the angle (along the great circle) between this position and the given position.
97 */
98 public double angle(SphericalPolarCoordinates B) {
99
100 Vector3D v1 = getCartesianCoordinates().normalized();
101 Vector3D v2 = B.getCartesianCoordinates().normalized();
102
103 double sinAngle = v1.cross(v2).modulus();
104
105 double cosAngle = v1.dot(v2);
106
107 double angle = Math.atan2(sinAngle, cosAngle);
108
109 return angle;
110 }
111
112 /**
113 * @return the great circle distance between this position and the given position.
114 */
115 public double distance(SphericalPolarCoordinates B) {
116
117 return angle(B) * radius;
118 }
119
120 /**
121 * @return the mid point of the two positions.
122 */
123 public static SphericalPolarCoordinates getMidpoint(SphericalPolarCoordinates A, SphericalPolarCoordinates B) {
124
125 return interpolate(A, B, 0.5);
126
127 }
128
129 /**
130 * alpha = 0 implies returning A, alpha = 1 implies returning B
131 * @return a point between the two positions.
132 */
133 public static SphericalPolarCoordinates interpolate(SphericalPolarCoordinates A, SphericalPolarCoordinates B, double alpha) {
134
135 Vector3D from = A.getCartesianCoordinates().normalized();
136 Vector3D to = B.getCartesianCoordinates().normalized();
137
138 double cosang = from.dot(to);
139 if (cosang < 0) {
140 cosang = -cosang;
141 to.negate();
142 System.out.println("Argg");
143 }
144
145 double fracFrom, fracTo;
146
147 if (cosang > (0.999995) ) { // small angle limit
148 fracFrom = alpha;
149 fracTo = 1.0-alpha;
150 } else { // normal case
151 double ang = Math.acos( cosang );
152 fracFrom = Math.sin( alpha * ang);
153 fracTo = Math.sin( (1.0-alpha) * ang );
154 }
155
156 Vector3D inbetween = from.mul(fracFrom).add(to.mul(fracTo)).normalized();
157
158 return new SphericalPolarCoordinates(inbetween, A.getRadius());
159 }
160
161 public double getRadius() { return radius; }
162
163 public String toString() {
164
165 return toLatLongString();
166 //return "polarCoordinates[theta="+theta+", phi=" + phi + ", r=" + radius + "]";
167 }
168
169 public String toLatLongString() {
170
171 double lat1 = getLatitude();
172 double long1 = getLongitude();
173
174 String latSuffix = " N";
175 if (lat1 < 0) {
176 lat1 = -lat1;
177 latSuffix = " S";
178 }
179
180 String longSuffix = " E";
181 if (long1 < 0) {
182 long1 = -long1;
183 longSuffix = " W";
184 }
185
186 return lat1 + latSuffix + ", " + long1 + longSuffix;
187 }
188
189 public static void main(String[] args) {
190
191 double latitude1 = 66.0;
192 double longitude1 = -174.0;
193
194 double latitude2 = 64.0;
195 double longitude2 = -142.0;
196
197 SphericalPolarCoordinates p1 = new SphericalPolarCoordinates(latitude1, longitude1);
198 SphericalPolarCoordinates p2 = new SphericalPolarCoordinates(latitude2, longitude2);
199
200 SphericalPolarCoordinates p1a = new SphericalPolarCoordinates(p1.getCartesianCoordinates());
201 SphericalPolarCoordinates p2a = new SphericalPolarCoordinates(p2.getCartesianCoordinates());
202
203
204 System.out.println("point1="+p1.toLatLongString());
205 System.out.println("point2="+p2.toLatLongString());
206
207 System.out.println("point1_test="+p1a.toLatLongString());
208 System.out.println("point2_test="+p2a.toLatLongString());
209
210
211 System.out.println("distance=" + p1.distance(p2) + " kilometres");
212
213 System.out.println("midpoint=" + getMidpoint(p1, p2).toLatLongString());
214 }
215
216
217
218 //************************************************************************
219 // Contrastable interface
220 //************************************************************************
221
222 public double getDifference(Contrastable spc) {
223
224 if (spc instanceof SphericalPolarCoordinates) {
225 return distance((SphericalPolarCoordinates)spc);
226 } else throw new IllegalArgumentException("Expected a spherical polar coordinate");
227 }
228
229 public Contrastable getWeightedMean(double weight1, Contrastable spc1, double weight2, Contrastable spc2) {
230
231 double alpha = weight1 / (weight1 + weight2);
232
233 if (spc1 instanceof SphericalPolarCoordinates && spc2 instanceof SphericalPolarCoordinates) {
234 return interpolate((SphericalPolarCoordinates)spc1, (SphericalPolarCoordinates)spc2, alpha);
235 } else throw new IllegalArgumentException("Expected a spherical polar coordinate");
236 }
237
238 //************************************************************************
239 // Private members
240 //************************************************************************
241
242 // angle off of north pole (0 to pi)
243 private double theta = 0.0;
244
245 // angle east of greenwich (0 to 2*pi)
246 private double phi = 0.0;
247
248 // radius
249 private double radius = 1.0;
250
251 private static double VOLUMETRIC_RADIUS_OF_EARTH = 6371.0;
252 }