3 using UnityEngine.Serialization;
7 public enum AltitudeMode {
20 [FormerlySerializedAs(
"latitude")] [Tooltip(
"The latitude, in degrees.")]
21 public double Latitude;
23 [FormerlySerializedAs(
"longitude")] [Tooltip(
"The longitude, in degrees.")]
24 public double Longitude;
26 [FormerlySerializedAs(
"altitude")] [Tooltip(
"The altitude, in meters.")]
27 public double Altitude;
29 [FormerlySerializedAs(
"altitudeMode")]
32 [Tooltip(
"The altitude mode. 'Absolute' means absolute altitude, relative to the sea level. 'DeviceRelative' meas it is " +
33 "relative to the device's initial position. 'GroundRelative' means relative to the nearest detected plane, and 'Ignore' means the " +
34 "altitude is ignored (equivalent to setting it to zero).")]
35 public AltitudeMode AltitudeMode = AltitudeMode.GroundRelative;
37 [FormerlySerializedAs(
"label")] [Tooltip(
"An optional label for the location.")]
38 public string Label =
"";
40 public bool IgnoreAltitude => AltitudeMode == AltitudeMode.Ignore;
48 public Location(
double latitude = 0.0,
double longitude = 0.0,
double altitude = 0.0)
51 Longitude = longitude;
65 Longitude = Longitude,
67 AltitudeMode = AltitudeMode
71 public override string ToString()
73 return "(" + Latitude +
", " + Longitude +
", " + Altitude +
")";
76 public DVector3 ToDVector3()
78 return new DVector3(Longitude, Altitude, Latitude);
81 public Vector3 ToVector3()
92 var rad = Math.PI / 180;
94 var lat = l.Latitude * rad;
95 var lon = l.Longitude * rad;
96 var a =
ARLocation.Config.EarthEquatorialRadiusInKM * 1000;
97 var e2 =
ARLocation.Config.EarthFirstEccentricitySquared;
98 var N = a / Math.Sqrt(1 - e2 * Math.Pow(Math.Sin(lat), 2));
100 var x = N * Math.Cos(lat) * Math.Cos(lon);
101 var y = N * Math.Cos(lat) * Math.Sin(lon);
102 var z = (1 - e2) * N * Math.Sin(lat);
114 var rad = Math.PI / 180;
115 var lat = l1.Latitude * rad;
116 var lon = l1.Longitude * rad;
121 var slat = Math.Sin(lat);
122 var clat = Math.Cos(lat);
123 var slon = Math.Sin(lon);
124 var clon = Math.Cos(lon);
126 var e = -slon * delta.x + clon * delta.y;
127 var n = -clon * slat * delta.x -slat * slon * delta.y+ clat*delta.z;
142 var lat = DegToRad(center.Latitude);
143 var lon = DegToRad(center.Longitude);
145 var slat = Math.Sin(lat);
146 var clat = Math.Cos(lat);
147 var slon = Math.Sin(lon);
148 var clon = Math.Cos(lon);
150 var dx = -e * slon - n * clon * slat + u * clon * clat;
151 var dy = e * clon - n * slon * slat + u * slon * clat;
152 var dz = n * clat + u * slat;
156 var pointEcef = centerEcef +
new DVector3(dx, dy, dz);
160 return pointLocation;
170 var a =
ARLocation.Config.EarthEquatorialRadiusInKM * 1000.0;
171 var e2 =
ARLocation.Config.EarthFirstEccentricitySquared;
172 var b = a * Math.Sqrt(1 - e2);
178 var r = Math.Sqrt(x * x + y * y);
179 var E2 = (a * a - b * b) / (b * b);
180 var F = 54 * b * b * z * z;
181 var G = r * r + (1 - e2) * z * z - e2 * (a * a - b * b);
182 var c = (e2 * e2 * F * r * r) / (G * G * G);
183 var s = Math.Pow(1 + c + Math.Sqrt(c*c + 2*c), 1.0 / 3.0);
184 var P = F / (3 * Math.Pow(s + (1.0/s) +1, 2.0) * G * G);
185 var Q = Math.Sqrt(1 + 2 * e2 * e2 * P);
186 var r0 = -(P * e2 * r) / (1 + Q) + Math.Sqrt(((a * a * 0.5) * (1.0 + (1.0 / Q))) - ((P * (1 - e2) * z * z) / (Q * (1.0 + Q))) - (P*r*r*0.5));
187 var U = Math.Sqrt(Math.Pow(r - e2 * r0, 2) + z*z);
188 var V = Math.Sqrt(Math.Pow(r - e2 * r0, 2) + (1 - e2) * z * z);
189 var z0 = (b * b * z) / (a * V);
191 var h = U * (1 - ((b * b) / (a * V)));
192 var phi = Math.Atan((z + E2 * z0) / r);
193 var lambda = Math.Atan2(y, x);
195 var rad2deg = 180.0 / Math.PI;
199 Latitude = rad2deg * phi,
200 Longitude = rad2deg * lambda,
202 AltitudeMode = AltitudeMode.GroundRelative
216 #if ARGPS_CUSTOM_GEO_CALC
217 return ArGpsCustomGeoCalc.HorizontalVectorFromTo(l1, l2).magnitude;
232 var h = Math.Abs(l1.Altitude - l2.Altitude);
234 return Math.Sqrt(d * d + h * h);
245 #if ARGPS_USE_CUSTOM_GEO_CALC
246 return ArGpsCustomGeoCalc.HorizontalVectorFromTo(l1, l2);
262 var height = l2.Altitude - l1.Altitude;
264 return new DVector3(horizontal.y, ignoreHeight ? 0 : height, horizontal.x);
278 var displacementVector =
VectorFromTo(userLocation, objectLocation, objectLocation.IgnoreAltitude || heightIsRelative)
281 var displacementPosition = arLocationRoot ? arLocationRoot.TransformVector(displacementVector) : displacementVector;
283 return userPosition + displacementPosition +
new Vector3(0, (heightIsRelative && !objectLocation.IgnoreAltitude) ? ((
float)objectLocation.Altitude - userPosition.y) : 0, 0);
326 var cameraLocalPosition = arLocationRoot.InverseTransformVector(center);
327 var position = arLocationRoot.InverseTransformVector(worldPosition);
328 var n = position.z - center.z;
329 var e = position.x - center.x;
340 return (Math.Abs(a.Latitude - b.Latitude) <= eps) &&
341 (Math.Abs(a.Longitude - b.Longitude) <= eps) &&
342 (Math.Abs(a.Altitude - b.Altitude) <= eps);
345 public static double RadToDeg(
double rad)
347 return (180.0 * Math.PI) * rad;
350 public static double DegToRad(
double deg)
352 return (Math.PI/ 180.0) * deg;