~ubuntu-branches/debian/squeeze/stellarium/squeeze

« back to all changes in this revision

Viewing changes to src/observator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Stellarium
3
 
 * Copyright (C) 2003 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
 
#include <string>
21
 
#include <cstdlib>
22
 
#include <algorithm>
23
 
 
24
 
#include "stellarium.h"
25
 
#include "stel_utility.h"
26
 
#include "stellastro.h"
27
 
#include "init_parser.h"
28
 
#include "observator.h"
29
 
#include "solarsystem.h"
30
 
#include "planet.h"
31
 
#include "translator.h"
32
 
 
33
 
 
34
 
Observator::Observator(const SolarSystem &ssystem)
35
 
           :ssystem(ssystem), planet(0),
36
 
            longitude(0.), latitude(0.), altitude(0)
37
 
{
38
 
        name = L"Anonymous_Location";
39
 
        flag_move_to = 0;
40
 
}
41
 
 
42
 
Observator::~Observator()
43
 
{
44
 
}
45
 
 
46
 
Vec3d Observator::getCenterVsop87Pos(void) const {
47
 
  return planet->get_heliocentric_ecliptic_pos();
48
 
}
49
 
 
50
 
double Observator::getDistanceFromCenter(void) const {
51
 
  return planet->getRadius() + (altitude/(1000*AU));
52
 
}
53
 
 
54
 
Mat4d Observator::getRotLocalToEquatorial(double jd) const {
55
 
  double lat = latitude;
56
 
  // TODO: Figure out how to keep continuity in sky as reach poles
57
 
  // otherwise sky jumps in rotation when reach poles in equatorial mode
58
 
  // This is a kludge
59
 
  if( lat > 89.5 )  lat = 89.5;
60
 
  if( lat < -89.5 ) lat = -89.5;
61
 
  return Mat4d::zrotation((planet->getSiderealTime(jd)+longitude)*(M_PI/180.))
62
 
       * Mat4d::yrotation((90.-lat)*(M_PI/180.));
63
 
}
64
 
 
65
 
Mat4d Observator::getRotEquatorialToVsop87(void) const {
66
 
  return planet->getRotEquatorialToVsop87();
67
 
}
68
 
 
69
 
void Observator::load(const string& file, const string& section)
70
 
{
71
 
        InitParser conf;
72
 
        conf.load(file);
73
 
        if (!conf.find_entry(section))
74
 
        {
75
 
                cerr << "ERROR : Can't find observator section " << section << " in file " << file << endl;
76
 
                assert(0);
77
 
        }
78
 
        load(conf, section);
79
 
}
80
 
 
81
 
bool Observator::setHomePlanet(const string &english_name) {
82
 
  Planet *p = ssystem.searchByEnglishName(english_name);
83
 
  if (p) {
84
 
    planet = p;
85
 
    return true;
86
 
  }
87
 
  return false;
88
 
}
89
 
 
90
 
 
91
 
void Observator::load(const InitParser& conf, const string& section)
92
 
{
93
 
        name = _(conf.get_str(section, "name").c_str());
94
 
 
95
 
        for (string::size_type i=0;i<name.length();++i)
96
 
        {
97
 
                if (name[i]=='_') name[i]=' ';
98
 
        }
99
 
 
100
 
    if (!setHomePlanet(conf.get_str(section, "home_planet", "Earth"))) {
101
 
      planet = ssystem.getEarth();
102
 
    }
103
 
    
104
 
    cout << "Loading location: \"" << StelUtility::wstringToString(name) <<"\", on " << planet->getEnglishName();
105
 
    
106
 
//    printf("(home_planet should be: \"%s\" is: \"%s\") ",
107
 
//           conf.get_str(section, "home_planet").c_str(),
108
 
//           planet->getEnglishName().c_str());
109
 
        latitude  = get_dec_angle(conf.get_str(section, "latitude"));
110
 
        longitude = get_dec_angle(conf.get_str(section, "longitude"));
111
 
        altitude = conf.get_int(section, "altitude");
112
 
        set_landscape_name(conf.get_str(section, "landscape_name", "sea"));
113
 
 
114
 
        printf(" (landscape is: \"%s\")\n", landscape_name.c_str());
115
 
 
116
 
}
117
 
 
118
 
void Observator::set_landscape_name(const string s) {
119
 
 
120
 
        // need to lower case name because config file parser lowercases section names
121
 
        string x = s;
122
 
        transform(x.begin(), x.end(), x.begin(), ::tolower);
123
 
        landscape_name = x;
124
 
}
125
 
 
126
 
void Observator::save(const string& file, const string& section) const
127
 
{
128
 
        printf("Saving location %s to file %s\n",StelUtility::wstringToString(name).c_str(), file.c_str());
129
 
 
130
 
        InitParser conf;
131
 
        conf.load(file);
132
 
 
133
 
        setConf(conf,section);
134
 
 
135
 
        conf.save(file);
136
 
}
137
 
 
138
 
 
139
 
// change settings but don't write to files
140
 
void Observator::setConf(InitParser & conf, const string& section) const
141
 
{
142
 
 
143
 
        conf.set_str(section + ":name", StelUtility::wstringToString(name));
144
 
        conf.set_str(section + ":home_planet", planet->getEnglishName());
145
 
        conf.set_str(section + ":latitude",
146
 
                     StelUtility::wstringToString(
147
 
                       StelUtility::printAngleDMS(latitude*M_PI/180.0,
148
 
                                                  true, true)));
149
 
        conf.set_str(section + ":longitude",
150
 
                     StelUtility::wstringToString(
151
 
                       StelUtility::printAngleDMS(longitude*M_PI/180.0,
152
 
                                                  true, true)));
153
 
 
154
 
        conf.set_int(section + ":altitude", altitude);
155
 
        conf.set_str(section + ":landscape_name", landscape_name);
156
 
}
157
 
 
158
 
 
159
 
// for platforms without built in timegm function
160
 
// taken from the timegm man page
161
 
time_t my_timegm (struct tm *tm) {
162
 
        time_t ret;
163
 
        char *tz;
164
 
        char tmpstr[255];
165
 
        tz = getenv("TZ");
166
 
        putenv("TZ=");
167
 
        tzset();
168
 
        ret = mktime(tm);
169
 
        if (tz)
170
 
        {
171
 
 snprintf(tmpstr, 255, "TZ=%s", tz);
172
 
                putenv(tmpstr);
173
 
   }
174
 
    else
175
 
                putenv("");
176
 
        tzset();
177
 
        return ret;
178
 
}
179
 
 
180
 
 
181
 
 
182
 
// move gradually to a new observation location
183
 
void Observator::move_to(double lat, double lon, double alt, int duration, const wstring& _name)
184
 
{
185
 
  flag_move_to = 1;
186
 
 
187
 
  start_lat = latitude;
188
 
  end_lat = lat;
189
 
 
190
 
  start_lon = longitude;
191
 
  end_lon = lon;
192
 
 
193
 
  start_alt = altitude;
194
 
  end_alt = alt;
195
 
 
196
 
  move_to_coef = 1.0f/duration;
197
 
  move_to_mult = 0;
198
 
 
199
 
        name = _name;
200
 
  //  printf("coef = %f\n", move_to_coef);
201
 
}
202
 
 
203
 
wstring Observator::get_name(void) const
204
 
{
205
 
        return name;
206
 
}
207
 
 
208
 
string Observator::getHomePlanetEnglishName(void) const {
209
 
  return planet ? planet->getEnglishName() : "";
210
 
}
211
 
 
212
 
wstring Observator::getHomePlanetNameI18(void) const {
213
 
  return planet ? planet->getNameI18() : L"";
214
 
}
215
 
 
216
 
// for moving observator position gradually
217
 
// TODO need to work on direction of motion...
218
 
void Observator::update(int delta_time) {
219
 
  if(flag_move_to) {
220
 
    move_to_mult += move_to_coef*delta_time;
221
 
 
222
 
    if( move_to_mult >= 1) {
223
 
      move_to_mult = 1;
224
 
      flag_move_to = 0;
225
 
    }
226
 
 
227
 
    latitude = start_lat - move_to_mult*(start_lat-end_lat);
228
 
    longitude = start_lon - move_to_mult*(start_lon-end_lon);
229
 
    altitude = int(start_alt - move_to_mult*(start_alt-end_alt));
230
 
 
231
 
  }
232
 
}
233