| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.geo.math; |
| 27 |
|
| 28 |
import dr.evolution.continuous.Contrastable; |
| 29 |
|
| 30 |
public class SphericalPolarCoordinates implements Contrastable { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public SphericalPolarCoordinates(Vector3D v) { |
| 36 |
this(v, v.modulus()); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 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 |
|
| 79 |
|
| 80 |
public double getLatitude() { |
| 81 |
return 90 - (theta * 180.0 / Math.PI); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 114 |
|
| 115 |
public double distance(SphericalPolarCoordinates B) { |
| 116 |
|
| 117 |
return angle(B) * radius; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static SphericalPolarCoordinates getMidpoint(SphericalPolarCoordinates A, SphericalPolarCoordinates B) { |
| 124 |
|
| 125 |
return interpolate(A, B, 0.5); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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) ) { |
| 148 |
fracFrom = alpha; |
| 149 |
fracTo = 1.0-alpha; |
| 150 |
} else { |
| 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 |
|
| 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 |
|
| 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 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private double theta = 0.0; |
| 244 |
|
| 245 |
|
| 246 |
private double phi = 0.0; |
| 247 |
|
| 248 |
|
| 249 |
private double radius = 1.0; |
| 250 |
|
| 251 |
private static double VOLUMETRIC_RADIUS_OF_EARTH = 6371.0; |
| 252 |
} |