/// <summary> /// Find the intersection of a ray with the terrain. /// </summary> /// <param name="p1">Cartesian coordinate of starting point</param> /// <param name="p2">Cartesian coordinate of end point</param> /// <param name="samplingPrecision">Sample length in meter</param> /// <param name="resultPrecision">Final sampling length in meter</param> /// <param name="latitude">Out : intersection latitude</param> /// <param name="longitude">Out : intersection longitude</param> /// <param name="world">Current world</param> /// <returns>NaN if no intersection found</returns> public static void RayIntersectionWithTerrain( Point3d p1, Point3d p2, double samplingPrecision, double resultPrecision, out Angle latitude, out Angle longitude, World world) { // Check for sphere intersection first // Note : checks for world radius + highest possible elevation float vertEx = World.Settings.VerticalExaggeration; double maxRadius = world.EquatorialRadius + 9000 * vertEx; // Max altitude for earth - should be dependant on world double a = (p2.X - p1.X) * (p2.X - p1.X) + (p2.Y - p1.Y) * (p2.Y - p1.Y) + (p2.Z - p1.Z) * (p2.Z - p1.Z); double b = 2.0 * ((p2.X - p1.X) * (p1.X) + (p2.Y - p1.Y) * (p1.Y) + (p2.Z - p1.Z) * (p1.Z)); double c = p1.X * p1.X + p1.Y * p1.Y + p1.Z * p1.Z - maxRadius * maxRadius; double discriminant = b * b - 4 * a * c; if (discriminant <= 0) { // No intersection with sphere latitude = Angle.NaN; longitude = Angle.NaN; return; } // Factor to intersection // Note : if t1 > 0 intersection is forward, < 0 is behind us double t1 = ((-1.0) * b - Math.Sqrt(discriminant)) / (2 * a); Point3d p1LatLon = MathEngine.CartesianToSphericalD(p1.X, p1.Y, p1.Z); if (t1 > 0 && p1LatLon.X > maxRadius) { // Looking from above max altitude : move p1 forward to intersection with max alt sphere p1 = new Point3d(p1.X + t1 * (p2.X - p1.X), p1.Y + t1 * (p2.Y - p1.Y), p1.Z + t1 * (p2.Z - p1.Z)); } // Ray sample Vector3 sample = new Vector3((float)(p2.X - p1.X), (float)(p2.Y - p1.Y), (float)(p2.Z - p1.Z)); double maxLength = sample.Length(); // Max length for ray tracing double sampleLength = samplingPrecision; // Sampling steps length sample.Normalize(); sample.Scale((float)sampleLength); // Casting Point3d ray = p1; double rayLength = 0; while (rayLength < maxLength) { Point3d rayLatLon = MathEngine.CartesianToSphericalD(ray.X, ray.Y, ray.Z); // Altitude at ray position double rayAlt = rayLatLon.X - world.EquatorialRadius; // Altitude at terrain position - from cached data (no download) double terrainAlt = world.TerrainAccessor.GetCachedElevationAt(MathEngine.RadiansToDegrees(rayLatLon.Y), MathEngine.RadiansToDegrees(rayLatLon.Z)); // best loaded data if (double.IsNaN(terrainAlt)) { terrainAlt = 0; } terrainAlt *= vertEx; if (terrainAlt > rayAlt) { // Intersection found if (sampleLength > resultPrecision) { // Go back one step ray.X -= sample.X; ray.Y -= sample.Y; ray.Z -= sample.Z; rayLength -= sampleLength; // and refine sampling sampleLength /= 10; sample.Normalize(); sample.Scale((float)sampleLength); } else { // return location latitude = Angle.FromRadians(rayLatLon.Y); longitude = Angle.FromRadians(rayLatLon.Z); return; } } // Move forward ray.X += sample.X; ray.Y += sample.Y; ray.Z += sample.Z; rayLength += sampleLength; } // No intersection with terrain found latitude = Angle.NaN; longitude = Angle.NaN; }
public static Point3d GetGeocentricPosition(System.DateTime utcDateTime) { if (World.Settings.SunSynchedWithTime) { // Sun synched with time and date double JD = getJulianDay(utcDateTime); double T = (JD - 2451545.0) / 36525; // number of Julian centuries since Jan 1, 2000, 12 UT double k = Math.PI / 180.0; double M = 357.52910 + 35999.05030 * T - 0.0001559 * T * T - 0.00000048 * T * T * T; // mean anomaly, degree double L0 = 280.46645 + 36000.76983 * T + 0.0003032 * T * T; // mean longitude, degree double DL = (1.914600 - 0.004817 * T - 0.000014 * T * T) * Math.Sin(k * M) + (0.019993 - 0.000101 * T) * Math.Sin(k * 2 * M) + 0.000290 * Math.Sin(k * 3 * M); double L = L0 + DL; // true longitude, degree L = L % 360; // obliquity eps of ecliptic: double eps = 23.0 + 26.0 / 60.0 + 21.448 / 3600.0 - (46.8150 * T + 0.00059 * T * T - 0.001813 * T * T * T) / 3600; double X = Math.Cos(k * L); double Y = Math.Cos(k * eps) * Math.Sin(k * L); double Z = Math.Sin(k * eps) * Math.Sin(k * L); double R = Math.Sqrt(1.0 - Z * Z); double dec = (180 / Math.PI) * Math.Atan(Z / R); // in degrees double RA = (24 / Math.PI) * Math.Atan(Y / (X + R)); // in hours double theta0 = 280.46061837 + 360.98564736629 * (JD - 2451545.0) + 0.000387933 * T * T - T * T * T / 38710000.0; // Greenwich Sidereal Time theta0 = theta0 % 360; RA *= 15; // convert from hours to degrees double tau = theta0 - RA; Point3d pos = MathEngine.SphericalToCartesianD( Angle.FromDegrees(-dec), Angle.FromDegrees(-(tau - 180)), 1); return(pos); } else { // Fixed sun heading and elevation double worldRadius = 6378137; // Earth meter Vector3 position = MathEngine.SphericalToCartesian(World.Settings.CameraLatitude, World.Settings.CameraLongitude, worldRadius); return(GetGeocentricPosition(position, Angle.FromRadians(World.Settings.SunHeading), Angle.FromRadians(World.Settings.SunElevation), World.Settings.SunDistance)); } }
/// <summary> /// Returns the absolute value of the specified angle /// </summary> public static Angle Abs(Angle a) { return(Angle.FromRadians(Math.Abs(a.Radians))); }
public static Angle operator /(Angle a, double divisor) { return(Angle.FromRadians(a.Radians / divisor)); }
public static Angle operator *(double times, Angle a) { return(Angle.FromRadians(a.Radians * times)); }
public static Angle operator -(Angle a, Angle b) { double res = a.Radians - b.Radians; return(Angle.FromRadians(res)); }