Astro.mm
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:10k
源码类别:

OpenGL

开发平台:

Visual C++

  1. //
  2. //  Astro.mm
  3. //  celestia
  4. //
  5. //  Created by Bob Ippolito on Fri Jun 07 2002.
  6. //  Copyright (C) 2001-9, the Celestia Development Team
  7. //
  8. #import "Astro.h"
  9. #import "Observer.h"
  10. #import "CelestiaUniversalCoord_PrivateAPI.h"
  11. #import "CelestiaVector_PrivateAPI.h"
  12. NSDictionary* coordinateDict;
  13. @implementation NSDate(AstroAPI)
  14. +(NSDate*)dateWithJulian:(NSNumber*)jd
  15. {
  16.     NSDate *date = nil;
  17.     astro::Date astroDate([jd doubleValue]);
  18.     int year = astroDate.year;
  19. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
  20.     NSCalendar *currentCalendar = [NSCalendar currentCalendar];
  21.     [currentCalendar setTimeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
  22.     NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
  23.     int era = 1;
  24.     if (year < 1)
  25.     {
  26.         era  = 0;
  27.         year = 1 - year;
  28.     }
  29.     [comps setEra:    era];
  30.     [comps setYear:   year];
  31.     [comps setMonth:  astroDate.month];
  32.     [comps setDay:    astroDate.day];
  33.     [comps setHour:   astroDate.hour];
  34.     [comps setMinute: astroDate.minute];
  35.     [comps setSecond: (int)astroDate.seconds];
  36.     date = [currentCalendar dateFromComponents: comps];
  37. #else
  38.     date = [NSCalendarDate dateWithYear: year
  39.                                   month: astroDate.month
  40.                                     day: astroDate.day
  41.                                    hour: astroDate.hour
  42.                                  minute: astroDate.minute
  43.                                  second: (int)astroDate.seconds
  44.                                timeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
  45. #endif
  46.     return date;
  47. }
  48. @end
  49. @implementation Astro
  50. +(NSString*)stringWithCoordinateSystem:(NSNumber*)n
  51. {
  52.     NSArray* keys = [coordinateDict allKeys];
  53.     unsigned int i;
  54.     for (i=0;i<[keys count];i++) {
  55.         if ([[coordinateDict objectForKey:[keys objectAtIndex:i]] isEqualToNumber:n])
  56.             return [keys objectAtIndex:i];
  57.     }
  58.     return nil;
  59. }
  60. +(void)initialize
  61. {
  62.     // compiler macro would be prettier I guess
  63.     coordinateDict = [[NSDictionary alloc] initWithObjectsAndKeys:
  64.                        [NSNumber numberWithInt:ObserverFrame::Universal],  @"Universal",
  65.                        [NSNumber numberWithInt:ObserverFrame::Ecliptical], @"Ecliptical",
  66.                        [NSNumber numberWithInt:ObserverFrame::Equatorial], @"Equatorial",
  67.                        [NSNumber numberWithInt:ObserverFrame::BodyFixed],  @"Geographic",
  68.                        [NSNumber numberWithInt:ObserverFrame::ObserverLocal], @"ObserverLocal",
  69.                        [NSNumber numberWithInt:ObserverFrame::PhaseLock],  @"PhaseLock",
  70.                        [NSNumber numberWithInt:ObserverFrame::Chase],      @"Chase",
  71.                        nil];
  72. }
  73. +(NSNumber*)sphereIlluminationFraction:(CelestiaVector*)spherePos viewerPosition:(CelestiaVector*)viewerPos
  74. {
  75.     return [NSNumber numberWithFloat:astro::sphereIlluminationFraction(
  76.         [spherePos point3d],
  77.         [viewerPos point3d])];
  78. }
  79. +(CelestiaVector*)heliocentricPosition:(CelestiaUniversalCoord*)universal starPosition:(CelestiaVector*)starPosition
  80. {
  81.     return [CelestiaVector vectorWithPoint3d:astro::heliocentricPosition([universal universalCoord], [starPosition point3f])];
  82. }
  83. +(CelestiaUniversalCoord*)universalPosition:(CelestiaVector*)heliocentric starPosition:(CelestiaVector*)starPosition
  84. {
  85.     return [[[CelestiaUniversalCoord alloc] initWithUniversalCoord:astro::universalPosition([heliocentric point3d],[starPosition point3f])] autorelease];
  86. }
  87. +(CelestiaVector*)equatorialToCelestialCart:(NSNumber*)ra declination:(NSNumber*)dec distance:(NSNumber*)distance
  88. {
  89.     return [CelestiaVector vectorWithPoint3d:astro::equatorialToCelestialCart([ra doubleValue], [dec doubleValue], [distance doubleValue])];
  90. }
  91. +(NSNumber*)coordinateSystem:(NSString*)coord
  92. {
  93.     return [coordinateDict objectForKey:coord];
  94. }
  95. +(NSNumber*)julianDate:(NSDate *)date
  96. {
  97.     NSTimeZone *prevTimeZone = [NSTimeZone defaultTimeZone];
  98.     // UTCtoTDB() expects GMT
  99.     [NSTimeZone setDefaultTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"GMT"]];
  100.     NSDate *roundedDate = nil;
  101. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
  102.     NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
  103.     NSDateComponents *comps = [currentCalendar components:
  104.         NSEraCalendarUnit  |
  105.         NSYearCalendarUnit | NSMonthCalendarUnit  | NSDayCalendarUnit | 
  106.         NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
  107.                                                  fromDate: date];
  108.     int era  = [comps era];
  109.     int year = [comps year];
  110.     if (era < 1) year = 1 - year;
  111.     astro::Date astroDate(year, [comps month], [comps day]);
  112.     astroDate.hour    = [comps hour];
  113.     astroDate.minute  = [comps minute];
  114.     astroDate.seconds = [comps second];
  115.     // -[NSDateComponents second] is rounded to an integer,
  116.     // so have to calculate and add decimal part
  117.     roundedDate = [currentCalendar dateFromComponents: comps];
  118.     [currentCalendar release];
  119. #else
  120.     NSCalendarDate *cd =  [date dateWithCalendarFormat: nil timeZone: nil];
  121.     astro::Date astroDate([cd yearOfCommonEra],
  122.                           [cd monthOfYear],
  123.                           [cd dayOfMonth]);
  124.     astroDate.hour    = [cd hourOfDay];    // takes DST in to account
  125.     astroDate.minute  = [cd minuteOfHour];
  126.     astroDate.seconds = [cd secondOfMinute];
  127.     roundedDate = cd;
  128. #endif
  129.     NSTimeInterval extraSeconds = [date timeIntervalSinceDate: roundedDate];
  130.     astroDate.seconds += extraSeconds;
  131.     [NSTimeZone setDefaultTimeZone: prevTimeZone];
  132.     double jd = astro::UTCtoTDB(astroDate);
  133.     return [NSNumber numberWithDouble: jd];
  134. }
  135. +(NSNumber*)speedOfLight
  136. {
  137.     return [NSNumber numberWithDouble:astro::speedOfLight];
  138. }
  139. +(NSNumber*)J2000
  140. {
  141.     return [NSNumber numberWithDouble:astro::J2000];
  142. }
  143. +(NSNumber*)G
  144. {
  145.     return [NSNumber numberWithDouble:astro::G];
  146. }
  147. +(NSNumber*)solarMass
  148. {
  149.     return [NSNumber numberWithDouble:astro::SolarMass];
  150. }
  151. +(NSNumber*)earthMass
  152. {
  153.     return [NSNumber numberWithDouble:astro::EarthMass];
  154. }
  155. +(NSNumber*)lumToAbsMag:(NSNumber*)lum
  156. {
  157.     return [NSNumber numberWithFloat:astro::lumToAbsMag([lum floatValue])];
  158. }
  159. +(NSNumber*)lumToAppMag:(NSNumber*)lum lightYears:(NSNumber*)lyrs
  160. {
  161.     return [NSNumber numberWithFloat:astro::lumToAppMag([lum floatValue], [lyrs floatValue])];
  162. }
  163. +(NSNumber*)absMagToLum:(NSNumber*)mag
  164. {
  165.     return [NSNumber numberWithFloat:astro::absMagToLum([mag floatValue])];
  166. }
  167. +(NSNumber*)absToAppMag:(NSNumber*)mag lightYears:(NSNumber*)lyrs
  168. {
  169.     return [NSNumber numberWithFloat:astro::absToAppMag([mag floatValue], [lyrs floatValue])];
  170. }
  171. +(NSNumber*)appToAbsMag:(NSNumber*)mag lightYears:(NSNumber*)lyrs
  172. {
  173.     return [NSNumber numberWithFloat:astro::appToAbsMag([mag floatValue], [lyrs floatValue])];
  174. }
  175. +(NSNumber*)lightYearsToParsecs:(NSNumber*)ly
  176. {
  177.     return [NSNumber numberWithFloat:astro::lightYearsToParsecs([ly floatValue])];
  178. }
  179. +(NSNumber*)parsecsToLightYears:(NSNumber*)pc
  180. {
  181.     return [NSNumber numberWithFloat:astro::parsecsToLightYears([pc floatValue])];
  182. }
  183. +(NSNumber*)lightYearsToKilometers:(NSNumber*)ly
  184. {
  185.     return [NSNumber numberWithDouble:astro::lightYearsToKilometers([ly doubleValue])];
  186. }
  187. +(NSNumber*)kilometersToLightYears:(NSNumber*)km
  188. {
  189.     return [NSNumber numberWithDouble:astro::kilometersToLightYears([km doubleValue])];
  190. }
  191. +(NSNumber*)lightYearsToAU:(NSNumber*)ly
  192. {
  193.     return [NSNumber numberWithDouble:astro::lightYearsToAU([ly doubleValue])];
  194. }
  195. +(NSNumber*)AUtoLightYears:(NSNumber*)au
  196. {
  197.     return [NSNumber numberWithFloat:astro::AUtoLightYears([au floatValue])];
  198. }
  199. +(NSNumber*)kilometersToAU:(NSNumber*)km
  200. {
  201.     return [NSNumber numberWithDouble:astro::kilometersToAU([km doubleValue])];
  202. }
  203. +(NSNumber*)AUtoKilometers:(NSNumber*)au
  204. {
  205.     return [NSNumber numberWithFloat:astro::AUtoKilometers([au floatValue])];
  206. }
  207. +(NSNumber*)microLightYearsToKilometers:(NSNumber*)mly
  208. {
  209.     return [NSNumber numberWithDouble:astro::microLightYearsToKilometers([mly doubleValue])];
  210. }
  211. +(NSNumber*)kilometersToMicroLightYears:(NSNumber*)km
  212. {
  213.     return [NSNumber numberWithDouble:astro::kilometersToMicroLightYears([km doubleValue])];
  214. }
  215. +(NSNumber*)microLightYearsToAU:(NSNumber*)mly
  216. {
  217.     return [NSNumber numberWithDouble:astro::microLightYearsToAU([mly doubleValue])];
  218. }
  219. +(NSNumber*)AUtoMicroLightYears:(NSNumber*)au
  220. {
  221.     return [NSNumber numberWithDouble:astro::AUtoMicroLightYears([au doubleValue])];
  222. }
  223. +(NSNumber*)secondsToJulianDate:(NSNumber*)sec
  224. {
  225.     return [NSNumber numberWithDouble:astro::secondsToJulianDate([sec doubleValue])];
  226. }
  227. +(NSNumber*)julianDateToSeconds:(NSNumber*)jd
  228. {
  229.     return [NSNumber numberWithDouble:astro::julianDateToSeconds([jd doubleValue])];
  230. }
  231. +(NSArray*)decimalToDegMinSec:(NSNumber*)angle
  232. {
  233.     int hours, minutes;
  234.     double seconds;
  235.     astro::decimalToDegMinSec([angle doubleValue], hours, minutes, seconds);
  236.     return [NSArray arrayWithObjects:[NSNumber numberWithInt:hours],[NSNumber numberWithInt:minutes],[NSNumber numberWithDouble:seconds],nil];
  237. }
  238. +(NSNumber*)degMinSecToDecimal:(NSArray*)dms
  239. {
  240.     return [NSNumber numberWithDouble:astro::degMinSecToDecimal([[dms objectAtIndex:0] intValue], [[dms objectAtIndex:1] intValue], [[dms objectAtIndex:2] intValue])];
  241. }
  242. +(NSArray*)anomaly:(NSNumber*)meanAnamaly eccentricity:(NSNumber*)eccentricity
  243. {
  244.     double trueAnomaly,eccentricAnomaly;
  245.     astro::anomaly([meanAnamaly doubleValue], [eccentricity doubleValue], trueAnomaly, eccentricAnomaly);
  246.     return [NSArray arrayWithObjects:[NSNumber numberWithDouble:trueAnomaly], [NSNumber numberWithDouble:eccentricAnomaly],nil];
  247. }
  248. +(NSNumber*)meanEclipticObliquity:(NSNumber*)jd
  249. {
  250.     return [NSNumber numberWithDouble:astro::meanEclipticObliquity([jd doubleValue])];
  251. }
  252. @end