~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/core/StelUtils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-02-15 20:48:39 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20100215204839-u3qgbv60rho997yk
Tags: 0.10.3-0ubuntu1
* New upstream release.
  - fixes intel rendering bug (LP: #480553)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Stellarium
3
3
 * Copyright (C) 2002 Fabien Chereau
4
 
 * 
 
4
 *
5
5
 * This program is free software; you can redistribute it and/or
6
6
 * modify it under the terms of the GNU General Public License
7
7
 * as published by the Free Software Foundation; either version 2
8
8
 * of the License, or (at your option) any later version.
9
 
 * 
 
9
 *
10
10
 * This program is distributed in the hope that it will be useful,
11
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
13
 * GNU General Public License for more details.
14
 
 * 
 
14
 *
15
15
 * You should have received a copy of the GNU General Public License
16
16
 * along with this program; if not, write to the Free Software
17
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27
27
 
28
28
#include "StelUtils.hpp"
29
29
#include "VecMath.hpp"
30
 
#include "GLee.h"
31
 
 
32
 
#include "fixx11h.h"
 
30
#include <QtOpenGL>
33
31
#include <QString>
34
32
#include <QStringList>
35
33
#include <QTextStream>
40
38
 
41
39
namespace StelUtils
42
40
{
 
41
//! Return the full name of stellarium, i.e. "stellarium 0.9.0"
 
42
QString getApplicationName()
 
43
{
 
44
#ifdef SVN_REVISION
 
45
        return QString("Stellarium")+" "+PACKAGE_VERSION+" (SVN r"+SVN_REVISION+")";
 
46
#else
 
47
        return QString("Stellarium")+" "+PACKAGE_VERSION;
 
48
#endif
 
49
}
 
50
 
 
51
//! Return the version of stellarium, i.e. "0.9.0"
 
52
QString getApplicationVersion()
 
53
{
 
54
#ifdef SVN_REVISION
 
55
        return QString(PACKAGE_VERSION)+" (SVN r"+SVN_REVISION+")";
 
56
#else
 
57
        return QString(PACKAGE_VERSION);
 
58
#endif
 
59
}
43
60
 
44
61
double hmsToRad(unsigned int h, unsigned int m, double s )
45
62
{
60
77
{
61
78
        angle = std::fmod(angle,2.0*M_PI);
62
79
        if (angle < 0.0) angle += 2.0*M_PI; // range: [0..2.0*M_PI)
63
 
                
 
80
 
64
81
        angle *= 12./M_PI;
65
82
 
66
83
        h = (unsigned int)angle;
81
98
                sign = false;
82
99
        }
83
100
        angle *= 180./M_PI;
84
 
        
 
101
 
85
102
        d = (unsigned int)angle;
86
103
        m = (unsigned int)((angle - d)*60);
87
104
        s = (angle-d)*3600-60*m;
158
175
                m=0;
159
176
                h+=1;
160
177
        }
161
 
        if (h==24 && m==0 && s==0) 
 
178
        if (h==24 && m==0 && s==0)
162
179
                h=0;
163
180
 
164
181
        return QString("%1h%2m%3s").arg(h, width).arg(m).arg(s, 0, 'f', precision);
181
198
        StelUtils::radToDms(angle+0.005*M_PI/180/(60*60)*(angle<0?-1.:1.), sign, d, m, s);
182
199
        QString str;
183
200
        QTextStream os(&str);
184
 
        
 
201
 
185
202
        os << (sign?'+':'-') << d << degsign;
186
203
        if (std::fabs(s*100-(int)s*100)>=1)
187
204
        {
229
246
                os << qSetRealNumberPrecision(0);
230
247
                width = 2;
231
248
        }
232
 
                
233
 
        os << qSetFieldWidth(width) << m << qSetFieldWidth(0) << '\'' 
234
 
                << fixed << qSetFieldWidth(width) << qSetPadChar('0') << s 
 
249
 
 
250
        os << qSetFieldWidth(width) << m << qSetFieldWidth(0) << '\''
 
251
                << fixed << qSetFieldWidth(width) << qSetPadChar('0') << s
235
252
                << qSetFieldWidth(0) << '\"';
236
253
 
237
254
        return str;
366
383
        return (value & -value) == value;
367
384
}
368
385
 
369
 
// Return the first power of two bigger than the given value 
 
386
// Return the first power of two bigger than the given value
370
387
int getBiggerPowerOfTwo(int value)
371
388
{
372
389
        int p=1;
374
391
                p<<=1;
375
392
        return p;
376
393
}
377
 
        
 
394
 
378
395
// Return the inverse sinus hyperbolic of z
379
396
double asinh(double z)
380
397
{
459
476
        // formatting a negative doesnt work the way i expect
460
477
 
461
478
        QString dt = QString("%1-%2-%3T%4:%5:%6")
462
 
                     .arg((year >= 0 ? year : -1* year),4,10,QLatin1Char('0'))
463
 
                     .arg(month,2,10,QLatin1Char('0'))
464
 
                     .arg(day,2,10,QLatin1Char('0'))
465
 
                     .arg(hour,2,10,QLatin1Char('0'))
466
 
                     .arg(minute,2,10,QLatin1Char('0'))
467
 
                     .arg(second,2,10,QLatin1Char('0'));
 
479
                                 .arg((year >= 0 ? year : -1* year),4,10,QLatin1Char('0'))
 
480
                                 .arg(month,2,10,QLatin1Char('0'))
 
481
                                 .arg(day,2,10,QLatin1Char('0'))
 
482
                                 .arg(hour,2,10,QLatin1Char('0'))
 
483
                                 .arg(minute,2,10,QLatin1Char('0'))
 
484
                                 .arg(second,2,10,QLatin1Char('0'));
468
485
 
469
486
        if (year < 0)
470
487
        {
516
533
                        out += fmt.at(i);
517
534
                }
518
535
                else if (fmt.at(i) == QLatin1Char('d') ||
519
 
                         fmt.at(i) == QLatin1Char('M') ||
520
 
                         fmt.at(i) == QLatin1Char('y'))
 
536
                                 fmt.at(i) == QLatin1Char('M') ||
 
537
                                 fmt.at(i) == QLatin1Char('y'))
521
538
                {
522
539
                        int j = i+1;
523
540
                        while ((j < fmt.length()) && (fmt.at(j) == fmt.at(i)) && (4 >= (j-i+1)))
646
663
        getDateFromJulianDay(JD, &year, &month, &day);
647
664
        getTimeFromJulianDay(JD, &hour, &minute, &second);
648
665
        // as analogous to second statement in getJDFromDate, nkerr
649
 
        if ( year <= 0 ) {
650
 
          year = year - 1;
 
666
        if ( year <= 0 )
 
667
        {
 
668
                year = year - 1;
651
669
        }
652
670
        QDateTime current(QDate(year, month, day), QTime(hour, minute, second));
653
671
        if (! current.isValid())
656
674
                // Assumes the GMT shift was always the same before year -4710
657
675
                current = QDateTime(QDate(-4710, month, day), QTime(hour, minute, second));
658
676
        }
659
 
        QDateTime c1 = QDateTime::fromString(current.toString(Qt::ISODate),Qt::ISODate);
660
 
        QDateTime u1 = QDateTime::fromString(current.toUTC().toString(Qt::ISODate),Qt::ISODate);
661
 
 
662
 
        int secsto = u1.secsTo(c1);
663
 
        float hrsto = secsto / 3600.0f;
664
 
        return hrsto;
 
677
        
 
678
        //Both timezones should be set to UTC because secsTo() converts both
 
679
        //times to UTC if their zones have different daylight saving time rules.
 
680
        QDateTime local = current; local.setTimeSpec(Qt::UTC);
 
681
        QDateTime universal = current.toUTC();
 
682
        
 
683
        int shiftInSeconds = universal.secsTo(local);
 
684
        float shiftInHours = shiftInSeconds / 3600.0f;
 
685
        return shiftInHours;
665
686
}
666
687
 
667
688
// UTC !
719
740
                case 11:
720
741
                        return 30;
721
742
                        break;
722
 
                
 
743
 
723
744
                case 2:
724
745
                        if ( year > 1582 )
725
746
                        {
758
779
                                }
759
780
                        }
760
781
                        break;
761
 
                
 
782
 
762
783
                case 0:
763
784
                        return numberOfDaysInMonthInYear(12, year-1);
764
785
                        break;
768
789
                default:
769
790
                        break;
770
791
        }
771
 
        
 
792
 
772
793
        return 0;
773
794
}
774
795
 
779
800
                                int* ry, int* rm, int* rd, int* rh, int* rmin, int* rs)
780
801
{
781
802
        bool change = false;
782
 
        
 
803
 
783
804
        while ( os > 59 ) {
784
805
                os -= 60;
785
806
                omin += 1;
790
811
                omin -= 1;
791
812
                change = true;
792
813
        }
793
 
        
 
814
 
794
815
        while (omin > 59 ) {
795
816
                omin -= 60;
796
817
                oh += 1;
801
822
                oh -= 1;
802
823
                change = true;
803
824
        }
804
 
        
 
825
 
805
826
        while ( oh > 23 ) {
806
827
                oh -= 24;
807
828
                od += 1;
812
833
                od -= 1;
813
834
                change = true;
814
835
        }
815
 
        
 
836
 
816
837
        while ( od > numberOfDaysInMonthInYear(om, oy) ) {
817
838
                od -= numberOfDaysInMonthInYear(om, oy);
818
839
                om++;
831
852
                }
832
853
                change = true;
833
854
        }
834
 
        
 
855
 
835
856
        while ( om > 12 ) {
836
857
                om -= 12;
837
858
                oy += 1;
842
863
                oy -= 1;
843
864
                change = true;
844
865
        }
845
 
        
 
866
 
846
867
        // and the julian-gregorian epoch hole: round up to the 15th
847
868
        if ( oy == 1582 && om == 10 && ( od > 4 && od < 15 ) ) {
848
869
                od = 15;
849
870
                change = true;
850
871
        }
851
 
        
 
872
 
852
873
        if ( change ) {
853
874
                *ry = oy;
854
875
                *rm = om;