~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/stel_utility.cpp

Tags: upstream-0.9.0
Import upstream version 0.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Stellarium
3
 
 * Copyright (C) 2002 Fabien Chereau
4
 
 * 
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License
7
 
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version.
9
 
 * 
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 * 
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
 */
19
 
 
20
 
 
21
 
#include <math.h> // fmod
22
 
#include <sstream>
23
 
#include <cstdlib>
24
 
#include <ctime>
25
 
 
26
 
#if defined( CYGWIN )
27
 
 #include <malloc.h>
28
 
#endif
29
 
 
30
 
#include "stel_utility.h"
31
 
#include "stellarium.h"
32
 
#include "translator.h"
33
 
#include "stellastro.h"
34
 
 
35
 
 
36
 
wstring StelUtility::stringToWstring(const string& s)
37
 
{
38
 
        return Translator::UTF8stringToWstring(s);
39
 
}
40
 
 
41
 
 
42
 
string StelUtility::wstringToString(const wstring& ws)
43
 
{
44
 
        // Get UTF-8 string length
45
 
        size_t len = wcstombs(NULL, ws.c_str(), 0)+1;
46
 
        // Create wide string
47
 
        char* s = new char[len];
48
 
        wcstombs(s, ws.c_str(), len);
49
 
        string ss(s);
50
 
        delete [] s;
51
 
        return ss;
52
 
}
53
 
 
54
 
wstring StelUtility::doubleToWstring(double d)
55
 
{
56
 
        std::wostringstream woss;
57
 
        woss << d;
58
 
        return woss.str();
59
 
}
60
 
 
61
 
wstring StelUtility::intToWstring(int i)
62
 
{
63
 
        std::wostringstream woss;
64
 
        woss << i;
65
 
        return woss.str();
66
 
}
67
 
 
68
 
double StelUtility::hms_to_rad( unsigned int h, unsigned int m, double s )
69
 
{
70
 
        return (double)M_PI/24.*h*2.+(double)M_PI/12.*m/60.+s*M_PI/43200.;
71
 
}
72
 
 
73
 
double StelUtility::dms_to_rad(int d, int m, double s)
74
 
{
75
 
        return (double)M_PI/180.*d+(double)M_PI/10800.*m+s*M_PI/648000.;
76
 
}
77
 
 
78
 
double hms_to_rad(unsigned int h, double m)
79
 
{
80
 
        return (double)M_PI/24.*h*2.+(double)M_PI/12.*m/60.;
81
 
}
82
 
 
83
 
double dms_to_rad(int d, double m)
84
 
{
85
 
        double t = (double)M_PI/180.*d+(double)M_PI/10800.*m;
86
 
        return t;
87
 
}
88
 
 
89
 
 
90
 
void sphe_to_rect(double lng, double lat, Vec3d& v)
91
 
{
92
 
        const double cosLat = cos(lat);
93
 
        v.set(cos(lng) * cosLat, sin(lng) * cosLat, sin(lat));
94
 
}
95
 
 
96
 
void sphe_to_rect(double lng, double lat, double r, Vec3d& v)
97
 
{
98
 
        const double cosLat = cos(lat);
99
 
        v.set(cos(lng) * cosLat * r, sin(lng) * cosLat * r, sin(lat) * r);
100
 
}
101
 
 
102
 
void sphe_to_rect(float lng, float lat, Vec3f& v)
103
 
{
104
 
        const double cosLat = cos(lat);
105
 
        v.set(cos(lng) * cosLat, sin(lng) * cosLat, sin(lat));
106
 
}
107
 
 
108
 
void rect_to_sphe(double *lng, double *lat, const Vec3d& v)
109
 
{
110
 
        double r = v.length();
111
 
        *lat = asin(v[2]/r);
112
 
        *lng = atan2(v[1],v[0]);
113
 
}
114
 
 
115
 
void rect_to_sphe(float *lng, float *lat, const Vec3f& v)
116
 
{
117
 
        double r = v.length();
118
 
        *lat = asin(v[2]/r);
119
 
        *lng = atan2(v[1],v[0]);
120
 
}
121
 
 
122
 
 
123
 
// Obtains a Vec3f from a string with the form x,y,z
124
 
Vec3f StelUtility::str_to_vec3f(const string& s)
125
 
{
126
 
        float x, y, z;
127
 
        if (s.empty() || (sscanf(s.c_str(),"%f,%f,%f",&x, &y, &z)!=3)) return Vec3f(0.f,0.f,0.f);
128
 
        return Vec3f(x,y,z);
129
 
}
130
 
 
131
 
// Obtains a string from a Vec3f with the form x,y,z
132
 
string StelUtility::vec3f_to_str(const Vec3f& v)
133
 
{
134
 
        ostringstream os;
135
 
        os << v[0] << "," << v[1] << "," << v[2];
136
 
        return os.str();
137
 
}
138
 
 
139
 
// Provide the luminance in cd/m^2 from the magnitude and the surface in arcmin^2
140
 
float mag_to_luminance(float mag, float surface)
141
 
{
142
 
        return expf(-0.4f * 2.3025851f * (mag - (-2.5f * log10f(surface)))) * 108064.73f;
143
 
}
144
 
 
145
 
// strips trailing whitespaces from buf.
146
 
#define iswhite(c)  ((c)== ' ' || (c)=='\t')
147
 
static char *trim(char *x)
148
 
{
149
 
        char *y;
150
 
 
151
 
        if(!x)
152
 
                return(x);
153
 
        y = x + strlen(x)-1;
154
 
        while (y >= x && iswhite(*y))
155
 
                *y-- = 0; /* skip white space */
156
 
        return x;
157
 
}
158
 
 
159
 
 
160
 
 
161
 
// salta espacios en blanco
162
 
static void skipwhite(char **s)
163
 
{
164
 
        while(iswhite(**s))
165
 
                ++(*s);
166
 
}
167
 
 
168
 
 
169
 
double get_dec_angle(const string& str)
170
 
{
171
 
        const char* s = str.c_str();
172
 
        char *mptr, *ptr, *dec, *hh;
173
 
        int negative = 0;
174
 
        char delim1[] = " :.,;DdHhMm'\n\t\xBA";  // 0xBA was old degree delimiter
175
 
        char delim2[] = " NSEWnsew\"\n\t";
176
 
        int dghh = 0, minutes = 0;
177
 
        double seconds = 0.0, pos;
178
 
        short count;
179
 
 
180
 
        enum _type{
181
 
            HOURS, DEGREES, LAT, LONG
182
 
        }type;
183
 
 
184
 
        if (s == NULL || !*s)
185
 
                return(-0.0);
186
 
        count = strlen(s) + 1;
187
 
        if ((mptr = (char *) malloc(count)) == NULL)
188
 
                return (-0.0);
189
 
        ptr = mptr;
190
 
        memcpy(ptr, s, count);
191
 
        trim(ptr);
192
 
        skipwhite(&ptr);
193
 
 
194
 
        /* the last letter has precedence over the sign */
195
 
        if (strpbrk(ptr,"SsWw") != NULL)
196
 
                negative = 1;
197
 
 
198
 
        if (*ptr == '+' || *ptr == '-')
199
 
                negative = (char) (*ptr++ == '-' ? 1 : negative);
200
 
        skipwhite(&ptr);
201
 
        if ((hh = strpbrk(ptr,"Hh")) != NULL && hh < ptr + 3)
202
 
                type = HOURS;
203
 
        else
204
 
                if (strpbrk(ptr,"SsNn") != NULL)
205
 
                        type = LAT;
206
 
                else
207
 
                        type = DEGREES; /* unspecified, the caller must control it */
208
 
 
209
 
        if ((ptr = strtok(ptr,delim1)) != NULL)
210
 
                dghh = atoi (ptr);
211
 
        else
212
 
        {
213
 
                free(mptr);
214
 
                return (-0.0);
215
 
        }
216
 
 
217
 
        if ((ptr = strtok(NULL,delim1)) != NULL)
218
 
        {
219
 
                minutes = atoi (ptr);
220
 
                if (minutes > 59)
221
 
                {
222
 
                        free(mptr);
223
 
                        return (-0.0);
224
 
                }
225
 
        }
226
 
        else
227
 
        {
228
 
                free(mptr);
229
 
                return (-0.0);
230
 
        }
231
 
 
232
 
        if ((ptr = strtok(NULL,delim2)) != NULL)
233
 
        {
234
 
                if ((dec = strchr(ptr,',')) != NULL)
235
 
                        *dec = '.';
236
 
                seconds = strtod (ptr, NULL);
237
 
                if (seconds >= 60.0)
238
 
                {
239
 
                        free(mptr);
240
 
                        return (-0.0);
241
 
                }
242
 
        }
243
 
 
244
 
        if ((ptr = strtok(NULL," \n\t")) != NULL)
245
 
        {
246
 
                skipwhite(&ptr);
247
 
                if (*ptr == 'S' || *ptr == 'W' || *ptr == 's' || *ptr == 'w') negative = 1;
248
 
        }
249
 
 
250
 
        free(mptr);
251
 
 
252
 
        pos = ((dghh*60+minutes)*60 + seconds) / 3600.0;
253
 
        if (type == HOURS && pos > 24.0)
254
 
                return (-0.0);
255
 
        if (type == LAT && pos > 90.0)
256
 
                return (-0.0);
257
 
        else
258
 
                if (pos > 180.0)
259
 
                        return (-0.0);
260
 
 
261
 
        if (negative)
262
 
                pos = -pos;
263
 
 
264
 
        return (pos);
265
 
 
266
 
}
267
 
 
268
 
 
269
 
 
270
 
 
271
 
//! @brief Print the passed angle with the format dd°mm'ss(.ss)"
272
 
//! @param angle Angle in radian
273
 
//! @param decimal Define if 2 decimal must also be printed
274
 
//! @param useD Define if letter "d" must be used instead of Â°
275
 
//! @return The corresponding string
276
 
wstring StelUtility::printAngleDMS(double angle, bool decimals, bool useD)
277
 
{
278
 
    wchar_t buf[32];
279
 
    buf[31]=L'\0';
280
 
    wchar_t sign = L'+';
281
 
    // wchar_t degsign = L'°'; ???
282
 
    wchar_t degsign = L'\u00B0';
283
 
    if (useD) degsign = L'd';
284
 
 
285
 
    angle *= 180./M_PI;
286
 
 
287
 
    if (angle<0) {
288
 
        angle *= -1;
289
 
        sign = '-';
290
 
    }
291
 
 
292
 
    if (decimals) {
293
 
        int d = (int)(0.5+angle*(60*60*100));
294
 
        const int centi = d % 100;
295
 
        d /= 100;
296
 
        const int s = d % 60;
297
 
        d /= 60;
298
 
        const int m = d % 60;
299
 
        d /= 60;
300
 
        swprintf(buf,
301
 
#ifndef MINGW32
302
 
                 sizeof(buf),
303
 
#endif
304
 
                 L"%lc%.2d%lc%.2d'%.2d.%02d\"",
305
 
                 sign, d, degsign, m, s, centi);
306
 
    } else {
307
 
        int d = (int)(0.5+angle*(60*60));
308
 
        const int s = d % 60;
309
 
        d /= 60;
310
 
        const int m = d % 60;
311
 
        d /= 60;
312
 
        swprintf(buf,
313
 
#ifndef MINGW32
314
 
                 sizeof(buf),
315
 
#endif
316
 
                 L"%lc%.2d%lc%.2d'%.2d\"",
317
 
                 sign, d, degsign, m, s);
318
 
    }
319
 
    return buf;
320
 
}
321
 
 
322
 
//! @brief Print the passed angle with the format +hhhmmmss(.ss)"
323
 
//! @param angle Angle in radian
324
 
//! @param decimals Define if 2 decimal must also be printed
325
 
//! @return The corresponding string
326
 
wstring StelUtility::printAngleHMS(double angle, bool decimals)
327
 
{
328
 
    wchar_t buf[16];
329
 
    buf[15] = L'\0';
330
 
    angle = fmod(angle,2.0*M_PI);
331
 
    if (angle < 0.0) angle += 2.0*M_PI; // range: [0..2.0*M_PI)
332
 
    angle *= 12./M_PI; // range: [0..24)
333
 
    if (decimals) {
334
 
        angle = 0.5+angle*(60*60*100); // range:[0.5,24*60*60*100+0.5)
335
 
        if (angle >= (24*60*60*100)) angle -= (24*60*60*100);
336
 
        int h = (int)angle;
337
 
        const int centi = h % 100;
338
 
        h /= 100;
339
 
        const int s = h % 60;
340
 
        h /= 60;
341
 
        const int m = h % 60;
342
 
        h /= 60;
343
 
        swprintf(buf,
344
 
#ifndef MINGW32
345
 
                 sizeof(buf),
346
 
#endif
347
 
                 L"%.2dh%.2dm%.2d.%02ds",h,m,s,centi);
348
 
    } else {
349
 
        angle = 0.5+angle*(60*60); // range:[0.5,24*60*60+0.5)
350
 
        if (angle >= (24*60*60)) angle -= (24*60*60);
351
 
        int h = (int)angle;
352
 
        const int s = h % 60;
353
 
        h /= 60;
354
 
        const int m = h % 60;
355
 
        h /= 60;
356
 
        swprintf(buf,
357
 
#ifndef MINGW32
358
 
                 sizeof(buf),
359
 
#endif
360
 
                 L"%.2dh%.2dm%.2ds",h,m,s);
361
 
    }
362
 
    return buf;
363
 
}
364
 
 
365
 
 
366
 
// convert string int ISO 8601-like format [+/-]YYYY-MM-DDThh:mm:ss (no timzone offset)
367
 
// to julian day
368
 
 
369
 
int string_to_jday(string date, double &jd)
370
 
{
371
 
        char tmp;
372
 
        int year, month, day, hour, minute, second;
373
 
        year = month = day = hour = minute = second = 0;
374
 
 
375
 
        std::istringstream dstr( date );
376
 
 
377
 
        // TODO better error checking
378
 
        dstr >> year >> tmp >> month >> tmp >> day >> tmp >> hour >> tmp >> minute >> tmp >> second;
379
 
 
380
 
        // cout << year << " " << month << " " << day << " " << hour << " " << minute << " " << second << endl;
381
 
 
382
 
        // bounds checking (per s_tui time object)
383
 
        if( year > 100000 || year < -100000 ||
384
 
                month < 1 || month > 12 ||
385
 
                day < 1 || day > 31 ||
386
 
                hour < 0 || hour > 23 ||
387
 
                minute < 0 || minute > 59 ||
388
 
                second < 0 || second > 59) return 0;
389
 
 
390
 
 
391
 
        // code taken from s_tui.cpp
392
 
        if (month <= 2)
393
 
        {
394
 
                year--;
395
 
                month += 12;
396
 
        }
397
 
 
398
 
        // Correct for the lost days in Oct 1582 when the Gregorian calendar
399
 
        // replaced the Julian calendar.
400
 
        int B = -2;
401
 
        if (year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15))))
402
 
        {
403
 
                B = year / 400 - year / 100;
404
 
        }
405
 
 
406
 
        jd = ((floor(365.25 * year) +
407
 
               floor(30.6001 * (month + 1)) + B + 1720996.5 +
408
 
               day + hour / 24.0 + minute / 1440.0 + second / 86400.0));
409
 
 
410
 
        return 1;
411
 
 
412
 
}
413
 
 
414
 
 
415
 
double str_to_double(string str)
416
 
{
417
 
 
418
 
        if(str=="") return 0;
419
 
        double dbl;
420
 
        std::istringstream dstr( str );
421
 
 
422
 
        dstr >> dbl;
423
 
        return dbl;
424
 
}
425
 
 
426
 
// always positive
427
 
double str_to_pos_double(string str)
428
 
{
429
 
 
430
 
        if(str=="") return 0;
431
 
        double dbl;
432
 
        std::istringstream dstr( str );
433
 
 
434
 
        dstr >> dbl;
435
 
        if(dbl < 0 ) dbl *= -1;
436
 
        return dbl;
437
 
}
438
 
 
439
 
 
440
 
int str_to_int(string str)
441
 
{
442
 
 
443
 
        if(str=="") return 0;
444
 
        int integer;
445
 
        std::istringstream istr( str );
446
 
 
447
 
        istr >> integer;
448
 
        return integer;
449
 
}
450
 
 
451
 
 
452
 
int str_to_int(string str, int default_value)
453
 
{
454
 
 
455
 
        if(str=="") return default_value;
456
 
        int integer;
457
 
        std::istringstream istr( str );
458
 
 
459
 
        istr >> integer;
460
 
        return integer;
461
 
}
462
 
 
463
 
string double_to_str(double dbl)
464
 
{
465
 
 
466
 
        std::ostringstream oss;
467
 
        oss << dbl;
468
 
        return oss.str();
469
 
 
470
 
}
471
 
 
472
 
long int str_to_long(string str)
473
 
{
474
 
 
475
 
        if(str=="") return 0;
476
 
        long int integer;
477
 
        std::istringstream istr( str );
478
 
 
479
 
        istr >> integer;
480
 
        return integer;
481
 
}
482
 
 
483
 
int fcompare(const string& _base, const string& _sub)
484
 
{
485
 
        unsigned int i = 0;
486
 
        while (i < _sub.length())
487
 
        {
488
 
                if (toupper(_base[i]) == toupper(_sub[i])) i++;
489
 
                else return -1;
490
 
        }
491
 
        return 0;
492
 
}
493
 
 
494
 
int fcompare(const wstring& _base, const wstring& _sub)
495
 
{
496
 
        unsigned int i = 0;
497
 
        while (i < _sub.length())
498
 
        {
499
 
                if (toupper(_base[i]) == toupper(_sub[i])) i++;
500
 
                else return -1;
501
 
        }
502
 
        return 0;
503
 
}
504
 
 
505
 
 
506
 
// Return the time zone name taken from system locale
507
 
wstring StelUtility::get_time_zone_name_from_system(double JD)
508
 
{
509
 
        // Windows will crash if date before 1970
510
 
        // And no changes on Linux before that year either
511
 
        // TODO: ALSO, on Win XP timezone never changes anyway??? 
512
 
        if(JD < 2440588 ) JD = 2440588;
513
 
 
514
 
        // The timezone name depends on the day because of the summer time
515
 
        time_t rawtime = get_time_t_from_julian(JD);
516
 
 
517
 
        struct tm * timeinfo;
518
 
        timeinfo = localtime(&rawtime);
519
 
        static char timez[255];
520
 
        timez[0] = 0;
521
 
        StelUtility::my_strftime(timez, 254, "%Z", timeinfo);
522
 
        return StelUtility::stringToWstring(timez);
523
 
}
524
 
 
525
 
 
526
 
// Return the number of hours to add to gmt time to get the local time in day JD
527
 
// taking the parameters from system. This takes into account the daylight saving
528
 
// time if there is. (positive for Est of GMT)
529
 
// TODO : %z in strftime only works on GNU compiler
530
 
// Fixed 31-05-2004 Now use the extern variables set by tzset()
531
 
float StelUtility::get_GMT_shift_from_system(double JD, bool _local)
532
 
{
533
 
        /* Doesn't seem like MACOSX is a special case... ??? rob
534
 
    #if defined( MACOSX ) || defined(WIN32)
535
 
        struct tm *timeinfo;
536
 
        time_t rawtime; time(&rawtime);
537
 
        timeinfo = localtime(&rawtime);
538
 
        return (float)timeinfo->tm_gmtoff/3600 + (timeinfo->tm_isdst!=0); 
539
 
        #else */
540
 
 
541
 
#if !defined(MINGW32)
542
 
 
543
 
        struct tm * timeinfo;
544
 
 
545
 
        if(!_local)
546
 
        {
547
 
                // JD is UTC
548
 
                struct tm rawtime;
549
 
                get_tm_from_julian(JD, &rawtime);
550
 
                
551
 
#ifdef HAVE_TIMEGM
552
 
                time_t ltime = timegm(&rawtime);
553
 
#else
554
 
                // This does not work
555
 
                time_t ltime = my_timegm(&rawtime);
556
 
#endif
557
 
                
558
 
                timeinfo = localtime(&ltime);
559
 
        } else {
560
 
          time_t rtime;
561
 
          rtime = get_time_t_from_julian(JD);
562
 
          timeinfo = localtime(&rtime);
563
 
        }
564
 
 
565
 
        static char heure[20];
566
 
        heure[0] = '\0';
567
 
 
568
 
        my_strftime(heure, 19, "%z", timeinfo);
569
 
        //      cout << heure << endl;
570
 
 
571
 
        //cout << timezone << endl;
572
 
        
573
 
        heure[5] = '\0';
574
 
        float min = 1.f/60.f * atoi(&heure[3]);
575
 
        heure[3] = '\0';
576
 
        return min + atoi(heure);
577
 
#else
578
 
     struct tm *timeinfo;
579
 
     time_t rawtime;
580
 
     time(&rawtime);
581
 
     timeinfo = localtime(&rawtime);
582
 
     return -(float)timezone/3600 + (timeinfo->tm_isdst!=0);
583
 
#endif
584
 
}
585
 
 
586
 
// Return the time in ISO 8601 format that is : %Y-%m-%d %H:%M:%S
587
 
string StelUtility::get_ISO8601_time_UTC(double JD)
588
 
{
589
 
        struct tm time_utc;
590
 
        get_tm_from_julian(JD, &time_utc);
591
 
 
592
 
        static char isotime[255];
593
 
        my_strftime(isotime, 254, "%Y-%m-%d %H:%M:%S", &time_utc);
594
 
        return isotime;
595
 
}